PR rtl-optimization/82913
[official-gcc.git] / gcc / lto-cgraph.c
blob15f0eaadf206e5a5f566947874fa9ceeb99b7f50
1 /* Write and read the cgraph to the memory mapped representation of a
2 .o file.
4 Copyright (C) 2009-2017 Free Software Foundation, Inc.
5 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "backend.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "predict.h"
31 #include "stringpool.h"
32 #include "tree-streamer.h"
33 #include "cgraph.h"
34 #include "tree-pass.h"
35 #include "profile.h"
36 #include "context.h"
37 #include "pass_manager.h"
38 #include "ipa-utils.h"
39 #include "omp-offload.h"
40 #include "ipa-chkp.h"
41 #include "stringpool.h"
42 #include "attribs.h"
44 /* True when asm nodes has been output. */
45 bool asm_nodes_output = false;
47 static void output_cgraph_opt_summary (void);
48 static void input_cgraph_opt_summary (vec<symtab_node *> nodes);
50 /* Number of LDPR values known to GCC. */
51 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
53 /* All node orders are ofsetted by ORDER_BASE. */
54 static int order_base;
56 /* Cgraph streaming is organized as set of record whose type
57 is indicated by a tag. */
58 enum LTO_symtab_tags
60 /* Must leave 0 for the stopper. */
62 /* Cgraph node without body available. */
63 LTO_symtab_unavail_node = 1,
64 /* Cgraph node with function body. */
65 LTO_symtab_analyzed_node,
66 /* Cgraph edges. */
67 LTO_symtab_edge,
68 LTO_symtab_indirect_edge,
69 LTO_symtab_variable,
70 LTO_symtab_last_tag
73 /* Create a new symtab encoder.
74 if FOR_INPUT, the encoder allocate only datastructures needed
75 to read the symtab. */
77 lto_symtab_encoder_t
78 lto_symtab_encoder_new (bool for_input)
80 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
82 if (!for_input)
83 encoder->map = new hash_map<symtab_node *, size_t>;
84 encoder->nodes.create (0);
85 return encoder;
89 /* Delete ENCODER and its components. */
91 void
92 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
94 encoder->nodes.release ();
95 if (encoder->map)
96 delete encoder->map;
97 free (encoder);
101 /* Return the existing reference number of NODE in the symtab encoder in
102 output block OB. Assign a new reference if this is the first time
103 NODE is encoded. */
106 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
107 symtab_node *node)
109 int ref;
111 if (!encoder->map)
113 lto_encoder_entry entry = {node, false, false, false};
115 ref = encoder->nodes.length ();
116 encoder->nodes.safe_push (entry);
117 return ref;
120 size_t *slot = encoder->map->get (node);
121 if (!slot || !*slot)
123 lto_encoder_entry entry = {node, false, false, false};
124 ref = encoder->nodes.length ();
125 if (!slot)
126 encoder->map->put (node, ref + 1);
127 encoder->nodes.safe_push (entry);
129 else
130 ref = *slot - 1;
132 return ref;
135 /* Remove NODE from encoder. */
137 bool
138 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
139 symtab_node *node)
141 int index;
142 lto_encoder_entry last_node;
144 size_t *slot = encoder->map->get (node);
145 if (slot == NULL || !*slot)
146 return false;
148 index = *slot - 1;
149 gcc_checking_assert (encoder->nodes[index].node == node);
151 /* Remove from vector. We do this by swapping node with the last element
152 of the vector. */
153 last_node = encoder->nodes.pop ();
154 if (last_node.node != node)
156 gcc_assert (encoder->map->put (last_node.node, index + 1));
158 /* Move the last element to the original spot of NODE. */
159 encoder->nodes[index] = last_node;
162 /* Remove element from hash table. */
163 encoder->map->remove (node);
164 return true;
168 /* Return TRUE if we should encode the body of NODE (if any). */
170 bool
171 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
172 struct cgraph_node *node)
174 int index = lto_symtab_encoder_lookup (encoder, node);
175 return encoder->nodes[index].body;
178 /* Specify that we encode the body of NODE in this partition. */
180 static void
181 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
182 struct cgraph_node *node)
184 int index = lto_symtab_encoder_encode (encoder, node);
185 gcc_checking_assert (encoder->nodes[index].node == node);
186 encoder->nodes[index].body = true;
189 /* Return TRUE if we should encode initializer of NODE (if any). */
191 bool
192 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
193 varpool_node *node)
195 int index = lto_symtab_encoder_lookup (encoder, node);
196 if (index == LCC_NOT_FOUND)
197 return false;
198 return encoder->nodes[index].initializer;
201 /* Specify that we should encode initializer of NODE (if any). */
203 static void
204 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
205 varpool_node *node)
207 int index = lto_symtab_encoder_lookup (encoder, node);
208 encoder->nodes[index].initializer = true;
211 /* Return TRUE if NODE is in this partition. */
213 bool
214 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
215 symtab_node *node)
217 int index = lto_symtab_encoder_lookup (encoder, node);
218 if (index == LCC_NOT_FOUND)
219 return false;
220 return encoder->nodes[index].in_partition;
223 /* Specify that NODE is in this partition. */
225 void
226 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
227 symtab_node *node)
229 int index = lto_symtab_encoder_encode (encoder, node);
230 encoder->nodes[index].in_partition = true;
233 /* Output the cgraph EDGE to OB using ENCODER. */
235 static void
236 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
237 lto_symtab_encoder_t encoder)
239 unsigned int uid;
240 intptr_t ref;
241 struct bitpack_d bp;
243 if (edge->indirect_unknown_callee)
244 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
245 LTO_symtab_indirect_edge);
246 else
247 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
248 LTO_symtab_edge);
250 ref = lto_symtab_encoder_lookup (encoder, edge->caller);
251 gcc_assert (ref != LCC_NOT_FOUND);
252 streamer_write_hwi_stream (ob->main_stream, ref);
254 if (!edge->indirect_unknown_callee)
256 ref = lto_symtab_encoder_lookup (encoder, edge->callee);
257 gcc_assert (ref != LCC_NOT_FOUND);
258 streamer_write_hwi_stream (ob->main_stream, ref);
261 edge->count.stream_out (ob->main_stream);
263 bp = bitpack_create (ob->main_stream);
264 uid = (!gimple_has_body_p (edge->caller->decl) || edge->caller->thunk.thunk_p
265 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt) + 1);
266 bp_pack_enum (&bp, cgraph_inline_failed_t,
267 CIF_N_REASONS, edge->inline_failed);
268 bp_pack_var_len_unsigned (&bp, uid);
269 bp_pack_var_len_unsigned (&bp, edge->frequency);
270 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
271 bp_pack_value (&bp, edge->speculative, 1);
272 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
273 gcc_assert (!edge->call_stmt_cannot_inline_p
274 || edge->inline_failed != CIF_BODY_NOT_AVAILABLE);
275 bp_pack_value (&bp, edge->can_throw_external, 1);
276 bp_pack_value (&bp, edge->in_polymorphic_cdtor, 1);
277 if (edge->indirect_unknown_callee)
279 int flags = edge->indirect_info->ecf_flags;
280 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
281 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
282 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
283 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
284 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
285 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
286 /* Flags that should not appear on indirect calls. */
287 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
288 | ECF_MAY_BE_ALLOCA
289 | ECF_SIBCALL
290 | ECF_LEAF
291 | ECF_NOVOPS)));
293 streamer_write_bitpack (&bp);
294 if (edge->indirect_unknown_callee)
296 streamer_write_hwi_stream (ob->main_stream,
297 edge->indirect_info->common_target_id);
298 if (edge->indirect_info->common_target_id)
299 streamer_write_hwi_stream
300 (ob->main_stream, edge->indirect_info->common_target_probability);
304 /* Return if NODE contain references from other partitions. */
306 bool
307 referenced_from_other_partition_p (symtab_node *node, lto_symtab_encoder_t encoder)
309 int i;
310 struct ipa_ref *ref = NULL;
312 for (i = 0; node->iterate_referring (i, ref); i++)
314 /* Ignore references from non-offloadable nodes while streaming NODE into
315 offload LTO section. */
316 if (!ref->referring->need_lto_streaming)
317 continue;
319 if (ref->referring->in_other_partition
320 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
321 return true;
323 return false;
326 /* Return true when node is reachable from other partition. */
328 bool
329 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
331 struct cgraph_edge *e;
332 if (!node->definition)
333 return false;
334 if (node->global.inlined_to)
335 return false;
336 for (e = node->callers; e; e = e->next_caller)
338 /* Ignore references from non-offloadable nodes while streaming NODE into
339 offload LTO section. */
340 if (!e->caller->need_lto_streaming)
341 continue;
343 if (e->caller->in_other_partition
344 || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
345 return true;
347 return false;
350 /* Return if NODE contain references from other partitions. */
352 bool
353 referenced_from_this_partition_p (symtab_node *node,
354 lto_symtab_encoder_t encoder)
356 int i;
357 struct ipa_ref *ref = NULL;
359 for (i = 0; node->iterate_referring (i, ref); i++)
360 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
361 return true;
362 return false;
365 /* Return true when node is reachable from other partition. */
367 bool
368 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
370 struct cgraph_edge *e;
371 for (e = node->callers; e; e = e->next_caller)
372 if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
373 return true;
374 return false;
377 /* Output the cgraph NODE to OB. ENCODER is used to find the
378 reference number of NODE->inlined_to. SET is the set of nodes we
379 are writing to the current file. If NODE is not in SET, then NODE
380 is a boundary of a cgraph_node_set and we pretend NODE just has a
381 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
382 that have had their callgraph node written so far. This is used to
383 determine if NODE is a clone of a previously written node. */
385 static void
386 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
387 lto_symtab_encoder_t encoder)
389 unsigned int tag;
390 struct bitpack_d bp;
391 bool boundary_p;
392 intptr_t ref;
393 bool in_other_partition = false;
394 struct cgraph_node *clone_of, *ultimate_clone_of;
395 ipa_opt_pass_d *pass;
396 int i;
397 const char *comdat;
398 const char *section;
399 tree group;
401 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
403 if (node->analyzed && (!boundary_p || node->alias
404 || (node->thunk.thunk_p && !node->global.inlined_to)))
405 tag = LTO_symtab_analyzed_node;
406 else
407 tag = LTO_symtab_unavail_node;
409 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
410 tag);
411 streamer_write_hwi_stream (ob->main_stream, node->order);
413 /* In WPA mode, we only output part of the call-graph. Also, we
414 fake cgraph node attributes. There are two cases that we care.
416 Boundary nodes: There are nodes that are not part of SET but are
417 called from within SET. We artificially make them look like
418 externally visible nodes with no function body.
420 Cherry-picked nodes: These are nodes we pulled from other
421 translation units into SET during IPA-inlining. We make them as
422 local static nodes to prevent clashes with other local statics. */
423 if (boundary_p && node->analyzed
424 && node->get_partitioning_class () == SYMBOL_PARTITION)
426 /* Inline clones can not be part of boundary.
427 gcc_assert (!node->global.inlined_to);
429 FIXME: At the moment they can be, when partition contains an inline
430 clone that is clone of inline clone from outside partition. We can
431 reshape the clone tree and make other tree to be the root, but it
432 needs a bit extra work and will be promplty done by cgraph_remove_node
433 after reading back. */
434 in_other_partition = 1;
437 clone_of = node->clone_of;
438 while (clone_of
439 && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
440 if (clone_of->prev_sibling_clone)
441 clone_of = clone_of->prev_sibling_clone;
442 else
443 clone_of = clone_of->clone_of;
445 /* See if body of the master function is output. If not, we are seeing only
446 an declaration and we do not need to pass down clone tree. */
447 ultimate_clone_of = clone_of;
448 while (ultimate_clone_of && ultimate_clone_of->clone_of)
449 ultimate_clone_of = ultimate_clone_of->clone_of;
451 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
452 clone_of = NULL;
454 if (tag == LTO_symtab_analyzed_node)
455 gcc_assert (clone_of || !node->clone_of);
456 if (!clone_of)
457 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
458 else
459 streamer_write_hwi_stream (ob->main_stream, ref);
462 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
463 node->count.stream_out (ob->main_stream);
464 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
466 streamer_write_hwi_stream (ob->main_stream,
467 node->ipa_transforms_to_apply.length ());
468 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
469 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
471 if (tag == LTO_symtab_analyzed_node)
473 if (node->global.inlined_to)
475 ref = lto_symtab_encoder_lookup (encoder, node->global.inlined_to);
476 gcc_assert (ref != LCC_NOT_FOUND);
478 else
479 ref = LCC_NOT_FOUND;
481 streamer_write_hwi_stream (ob->main_stream, ref);
484 group = node->get_comdat_group ();
485 if (group)
486 comdat = IDENTIFIER_POINTER (group);
487 else
488 comdat = "";
489 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
491 if (group)
493 if (node->same_comdat_group)
495 ref = LCC_NOT_FOUND;
496 for (struct symtab_node *n = node->same_comdat_group;
497 ref == LCC_NOT_FOUND && n != node; n = n->same_comdat_group)
498 ref = lto_symtab_encoder_lookup (encoder, n);
500 else
501 ref = LCC_NOT_FOUND;
502 streamer_write_hwi_stream (ob->main_stream, ref);
505 section = node->get_section ();
506 if (!section)
507 section = "";
509 streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
511 bp = bitpack_create (ob->main_stream);
512 bp_pack_value (&bp, node->local.local, 1);
513 bp_pack_value (&bp, node->externally_visible, 1);
514 bp_pack_value (&bp, node->no_reorder, 1);
515 bp_pack_value (&bp, node->definition, 1);
516 bp_pack_value (&bp, node->local.versionable, 1);
517 bp_pack_value (&bp, node->local.can_change_signature, 1);
518 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
519 bp_pack_value (&bp, node->force_output, 1);
520 bp_pack_value (&bp, node->forced_by_abi, 1);
521 bp_pack_value (&bp, node->unique_name, 1);
522 bp_pack_value (&bp, node->body_removed, 1);
523 bp_pack_value (&bp, node->implicit_section, 1);
524 bp_pack_value (&bp, node->address_taken, 1);
525 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
526 && node->get_partitioning_class () == SYMBOL_PARTITION
527 && (reachable_from_other_partition_p (node, encoder)
528 || referenced_from_other_partition_p (node, encoder)), 1);
529 bp_pack_value (&bp, node->lowered, 1);
530 bp_pack_value (&bp, in_other_partition, 1);
531 bp_pack_value (&bp, node->alias, 1);
532 bp_pack_value (&bp, node->transparent_alias, 1);
533 bp_pack_value (&bp, node->weakref, 1);
534 bp_pack_value (&bp, node->frequency, 2);
535 bp_pack_value (&bp, node->only_called_at_startup, 1);
536 bp_pack_value (&bp, node->only_called_at_exit, 1);
537 bp_pack_value (&bp, node->tm_clone, 1);
538 bp_pack_value (&bp, node->calls_comdat_local, 1);
539 bp_pack_value (&bp, node->icf_merged, 1);
540 bp_pack_value (&bp, node->nonfreeing_fn, 1);
541 bp_pack_value (&bp, node->thunk.thunk_p, 1);
542 bp_pack_value (&bp, node->parallelized_function, 1);
543 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
544 LDPR_NUM_KNOWN, node->resolution);
545 bp_pack_value (&bp, node->instrumentation_clone, 1);
546 bp_pack_value (&bp, node->split_part, 1);
547 streamer_write_bitpack (&bp);
548 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
550 if (node->thunk.thunk_p)
552 streamer_write_uhwi_stream
553 (ob->main_stream,
554 1 + (node->thunk.this_adjusting != 0) * 2
555 + (node->thunk.virtual_offset_p != 0) * 4
556 + (node->thunk.add_pointer_bounds_args != 0) * 8);
557 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
558 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
560 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
561 if (DECL_STATIC_CONSTRUCTOR (node->decl))
562 streamer_write_hwi_stream (ob->main_stream, node->get_init_priority ());
563 if (DECL_STATIC_DESTRUCTOR (node->decl))
564 streamer_write_hwi_stream (ob->main_stream, node->get_fini_priority ());
566 if (node->instrumentation_clone)
567 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->orig_decl);
570 /* Output the varpool NODE to OB.
571 If NODE is not in SET, then NODE is a boundary. */
573 static void
574 lto_output_varpool_node (struct lto_simple_output_block *ob, varpool_node *node,
575 lto_symtab_encoder_t encoder)
577 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
578 bool encode_initializer_p
579 = (node->definition
580 && lto_symtab_encoder_encode_initializer_p (encoder, node));
581 struct bitpack_d bp;
582 int ref;
583 const char *comdat;
584 const char *section;
585 tree group;
587 gcc_assert (!encode_initializer_p || node->definition);
588 gcc_assert (boundary_p || encode_initializer_p);
590 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
591 LTO_symtab_variable);
592 streamer_write_hwi_stream (ob->main_stream, node->order);
593 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
594 bp = bitpack_create (ob->main_stream);
595 bp_pack_value (&bp, node->externally_visible, 1);
596 bp_pack_value (&bp, node->no_reorder, 1);
597 bp_pack_value (&bp, node->force_output, 1);
598 bp_pack_value (&bp, node->forced_by_abi, 1);
599 bp_pack_value (&bp, node->unique_name, 1);
600 bp_pack_value (&bp,
601 node->body_removed
602 || (!encode_initializer_p && !node->alias && node->definition),
604 bp_pack_value (&bp, node->implicit_section, 1);
605 bp_pack_value (&bp, node->writeonly, 1);
606 bp_pack_value (&bp, node->definition && (encode_initializer_p || node->alias),
608 bp_pack_value (&bp, node->alias, 1);
609 bp_pack_value (&bp, node->transparent_alias, 1);
610 bp_pack_value (&bp, node->weakref, 1);
611 bp_pack_value (&bp, node->analyzed && (!boundary_p || node->alias), 1);
612 gcc_assert (node->definition || !node->analyzed);
613 /* Constant pool initializers can be de-unified into individual ltrans units.
614 FIXME: Alternatively at -Os we may want to avoid generating for them the local
615 labels and share them across LTRANS partitions. */
616 if (node->get_partitioning_class () != SYMBOL_PARTITION)
618 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
619 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
621 else
623 bp_pack_value (&bp, node->definition
624 && referenced_from_other_partition_p (node, encoder), 1);
625 bp_pack_value (&bp, node->analyzed
626 && boundary_p && !DECL_EXTERNAL (node->decl), 1);
627 /* in_other_partition. */
629 bp_pack_value (&bp, node->tls_model, 3);
630 bp_pack_value (&bp, node->used_by_single_function, 1);
631 bp_pack_value (&bp, node->dynamically_initialized, 1);
632 bp_pack_value (&bp, node->need_bounds_init, 1);
633 streamer_write_bitpack (&bp);
635 group = node->get_comdat_group ();
636 if (group)
637 comdat = IDENTIFIER_POINTER (group);
638 else
639 comdat = "";
640 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
642 if (group)
644 if (node->same_comdat_group)
646 ref = LCC_NOT_FOUND;
647 for (struct symtab_node *n = node->same_comdat_group;
648 ref == LCC_NOT_FOUND && n != node; n = n->same_comdat_group)
649 ref = lto_symtab_encoder_lookup (encoder, n);
651 else
652 ref = LCC_NOT_FOUND;
653 streamer_write_hwi_stream (ob->main_stream, ref);
656 section = node->get_section ();
657 if (!section)
658 section = "";
659 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
661 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
662 LDPR_NUM_KNOWN, node->resolution);
665 /* Output the varpool NODE to OB.
666 If NODE is not in SET, then NODE is a boundary. */
668 static void
669 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
670 lto_symtab_encoder_t encoder)
672 struct bitpack_d bp;
673 int nref;
674 int uid = ref->lto_stmt_uid;
675 struct cgraph_node *node;
677 bp = bitpack_create (ob->main_stream);
678 bp_pack_value (&bp, ref->use, 3);
679 bp_pack_value (&bp, ref->speculative, 1);
680 streamer_write_bitpack (&bp);
681 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
682 gcc_assert (nref != LCC_NOT_FOUND);
683 streamer_write_hwi_stream (ob->main_stream, nref);
685 node = dyn_cast <cgraph_node *> (ref->referring);
686 if (node)
688 if (ref->stmt)
689 uid = gimple_uid (ref->stmt) + 1;
690 streamer_write_hwi_stream (ob->main_stream, uid);
694 /* Stream out profile_summary to OB. */
696 static void
697 output_profile_summary (struct lto_simple_output_block *ob)
699 unsigned h_ix;
700 struct bitpack_d bp;
702 if (profile_info)
704 /* We do not output num and run_max, they are not used by
705 GCC profile feedback and they are difficult to merge from multiple
706 units. */
707 gcc_assert (profile_info->runs);
708 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
709 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
711 /* sum_all is needed for computing the working set with the
712 histogram. */
713 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
715 /* Create and output a bitpack of non-zero histogram entries indices. */
716 bp = bitpack_create (ob->main_stream);
717 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
718 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
719 streamer_write_bitpack (&bp);
720 /* Now stream out only those non-zero entries. */
721 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
723 if (!profile_info->histogram[h_ix].num_counters)
724 continue;
725 streamer_write_gcov_count_stream (ob->main_stream,
726 profile_info->histogram[h_ix].num_counters);
727 streamer_write_gcov_count_stream (ob->main_stream,
728 profile_info->histogram[h_ix].min_value);
729 streamer_write_gcov_count_stream (ob->main_stream,
730 profile_info->histogram[h_ix].cum_value);
732 /* IPA-profile computes hot bb threshold based on cumulated
733 whole program profile. We need to stream it down to ltrans. */
734 if (flag_wpa)
735 streamer_write_gcov_count_stream (ob->main_stream,
736 get_hot_bb_threshold ());
738 else
739 streamer_write_uhwi_stream (ob->main_stream, 0);
742 /* Output all callees or indirect outgoing edges. EDGE must be the first such
743 edge. */
745 static void
746 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
747 struct lto_simple_output_block *ob,
748 lto_symtab_encoder_t encoder)
750 if (!edge)
751 return;
753 /* Output edges in backward direction, so the reconstructed callgraph match
754 and it is easy to associate call sites in the IPA pass summaries. */
755 while (edge->next_callee)
756 edge = edge->next_callee;
757 for (; edge; edge = edge->prev_callee)
758 lto_output_edge (ob, edge, encoder);
761 /* Output the part of the cgraph in SET. */
763 static void
764 output_refs (lto_symtab_encoder_t encoder)
766 struct lto_simple_output_block *ob;
767 int count;
768 struct ipa_ref *ref;
770 ob = lto_create_simple_output_block (LTO_section_refs);
772 for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
774 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
776 /* IPA_REF_ALIAS and IPA_REF_CHKP references are always preserved
777 in the boundary. Alias node can't have other references and
778 can be always handled as if it's not in the boundary. */
779 if (!node->alias && !lto_symtab_encoder_in_partition_p (encoder, node))
781 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
782 /* Output IPA_REF_CHKP reference. */
783 if (cnode
784 && cnode->instrumented_version
785 && !cnode->instrumentation_clone)
787 for (int i = 0; node->iterate_reference (i, ref); i++)
788 if (ref->use == IPA_REF_CHKP)
790 if (lto_symtab_encoder_lookup (encoder, ref->referred)
791 != LCC_NOT_FOUND)
793 int nref = lto_symtab_encoder_lookup (encoder, node);
794 streamer_write_gcov_count_stream (ob->main_stream, 1);
795 streamer_write_uhwi_stream (ob->main_stream, nref);
796 lto_output_ref (ob, ref, encoder);
798 break;
801 continue;
804 count = node->ref_list.nreferences ();
805 if (count)
807 streamer_write_gcov_count_stream (ob->main_stream, count);
808 streamer_write_uhwi_stream (ob->main_stream,
809 lto_symtab_encoder_lookup (encoder, node));
810 for (int i = 0; node->iterate_reference (i, ref); i++)
811 lto_output_ref (ob, ref, encoder);
815 streamer_write_uhwi_stream (ob->main_stream, 0);
817 lto_destroy_simple_output_block (ob);
820 /* Add NODE into encoder as well as nodes it is cloned from.
821 Do it in a way so clones appear first. */
823 static void
824 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
825 bool include_body)
827 if (node->clone_of)
828 add_node_to (encoder, node->clone_of, include_body);
829 else if (include_body)
830 lto_set_symtab_encoder_encode_body (encoder, node);
831 lto_symtab_encoder_encode (encoder, node);
834 /* Add all references in NODE to encoders. */
836 static void
837 create_references (lto_symtab_encoder_t encoder, symtab_node *node)
839 int i;
840 struct ipa_ref *ref = NULL;
841 for (i = 0; node->iterate_reference (i, ref); i++)
842 if (is_a <cgraph_node *> (ref->referred))
843 add_node_to (encoder, dyn_cast <cgraph_node *> (ref->referred), false);
844 else
845 lto_symtab_encoder_encode (encoder, ref->referred);
848 /* Select what needs to be streamed out. In regular lto mode stream everything.
849 In offload lto mode stream only nodes marked as offloadable. */
850 void
851 select_what_to_stream (void)
853 struct symtab_node *snode;
854 FOR_EACH_SYMBOL (snode)
855 snode->need_lto_streaming = !lto_stream_offload_p || snode->offloadable;
858 /* Find all symbols we want to stream into given partition and insert them
859 to encoders.
861 The function actually replaces IN_ENCODER by new one. The reason is that
862 streaming code needs clone's origin to be streamed before clone. This
863 means that we need to insert the nodes in specific order. This order is
864 ignored by the partitioning logic earlier. */
866 lto_symtab_encoder_t
867 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
869 struct cgraph_edge *edge;
870 int i;
871 lto_symtab_encoder_t encoder;
872 lto_symtab_encoder_iterator lsei;
873 hash_set<void *> reachable_call_targets;
875 encoder = lto_symtab_encoder_new (false);
877 /* Go over all entries in the IN_ENCODER and duplicate them to
878 ENCODER. At the same time insert masters of clones so
879 every master appears before clone. */
880 for (lsei = lsei_start_function_in_partition (in_encoder);
881 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
883 struct cgraph_node *node = lsei_cgraph_node (lsei);
884 if (!node->need_lto_streaming)
885 continue;
886 add_node_to (encoder, node, true);
887 lto_set_symtab_encoder_in_partition (encoder, node);
888 create_references (encoder, node);
890 for (lsei = lsei_start_variable_in_partition (in_encoder);
891 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
893 varpool_node *vnode = lsei_varpool_node (lsei);
895 if (!vnode->need_lto_streaming)
896 continue;
897 lto_set_symtab_encoder_in_partition (encoder, vnode);
898 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
899 create_references (encoder, vnode);
901 /* Pickle in also the initializer of all referenced readonly variables
902 to help folding. Constant pool variables are not shared, so we must
903 pickle those too. */
904 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
906 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
907 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
909 if (!lto_symtab_encoder_encode_initializer_p (encoder,
910 vnode)
911 && (((vnode->ctor_useable_for_folding_p ()
912 && (!DECL_VIRTUAL_P (vnode->decl)
913 || !flag_wpa
914 || flag_ltrans_devirtualize))
915 || POINTER_BOUNDS_P (vnode->decl))))
917 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
918 create_references (encoder, vnode);
923 /* Go over all the nodes again to include callees that are not in
924 SET. */
925 for (lsei = lsei_start_function_in_partition (encoder);
926 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
928 struct cgraph_node *node = lsei_cgraph_node (lsei);
929 for (edge = node->callees; edge; edge = edge->next_callee)
931 struct cgraph_node *callee = edge->callee;
932 if (!lto_symtab_encoder_in_partition_p (encoder, callee))
934 /* We should have moved all the inlines. */
935 gcc_assert (!callee->global.inlined_to);
936 add_node_to (encoder, callee, false);
939 /* Add all possible targets for late devirtualization. */
940 if (flag_ltrans_devirtualize || !flag_wpa)
941 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
942 if (edge->indirect_info->polymorphic)
944 unsigned int i;
945 void *cache_token;
946 bool final;
947 vec <cgraph_node *>targets
948 = possible_polymorphic_call_targets
949 (edge, &final, &cache_token);
950 if (!reachable_call_targets.add (cache_token))
952 for (i = 0; i < targets.length (); i++)
954 struct cgraph_node *callee = targets[i];
956 /* Adding an external declarations into the unit serves
957 no purpose and just increases its boundary. */
958 if (callee->definition
959 && !lto_symtab_encoder_in_partition_p
960 (encoder, callee))
962 gcc_assert (!callee->global.inlined_to);
963 add_node_to (encoder, callee, false);
969 /* Be sure to also insert alias targert and thunk callees. These needs
970 to stay to aid local calling conventions. */
971 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
973 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
974 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
976 if (node->alias && node->analyzed)
977 create_references (encoder, node);
978 if (cnode
979 && cnode->thunk.thunk_p && !cnode->global.inlined_to)
980 add_node_to (encoder, cnode->callees->callee, false);
981 while (node->transparent_alias && node->analyzed)
983 node = node->get_alias_target ();
984 if (is_a <cgraph_node *> (node))
985 add_node_to (encoder, dyn_cast <cgraph_node *> (node),
986 false);
987 else
988 lto_symtab_encoder_encode (encoder, node);
991 lto_symtab_encoder_delete (in_encoder);
992 return encoder;
995 /* Output the part of the symtab in SET and VSET. */
997 void
998 output_symtab (void)
1000 struct cgraph_node *node;
1001 struct lto_simple_output_block *ob;
1002 int i, n_nodes;
1003 lto_symtab_encoder_t encoder;
1005 if (flag_wpa)
1006 output_cgraph_opt_summary ();
1008 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
1010 output_profile_summary (ob);
1012 /* An encoder for cgraph nodes should have been created by
1013 ipa_write_summaries_1. */
1014 gcc_assert (ob->decl_state->symtab_node_encoder);
1015 encoder = ob->decl_state->symtab_node_encoder;
1017 /* Write out the nodes. We must first output a node and then its clones,
1018 otherwise at a time reading back the node there would be nothing to clone
1019 from. */
1020 n_nodes = lto_symtab_encoder_size (encoder);
1021 for (i = 0; i < n_nodes; i++)
1023 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1024 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1025 lto_output_node (ob, cnode, encoder);
1026 else
1027 lto_output_varpool_node (ob, dyn_cast<varpool_node *> (node), encoder);
1030 /* Go over the nodes in SET again to write edges. */
1031 for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
1033 node = dyn_cast <cgraph_node *> (lto_symtab_encoder_deref (encoder, i));
1034 if (node
1035 && ((node->thunk.thunk_p && !node->global.inlined_to)
1036 || lto_symtab_encoder_in_partition_p (encoder, node)))
1038 output_outgoing_cgraph_edges (node->callees, ob, encoder);
1039 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
1043 streamer_write_uhwi_stream (ob->main_stream, 0);
1045 lto_destroy_simple_output_block (ob);
1047 /* Emit toplevel asms.
1048 When doing WPA we must output every asm just once. Since we do not partition asm
1049 nodes at all, output them to first output. This is kind of hack, but should work
1050 well. */
1051 if (!asm_nodes_output)
1053 asm_nodes_output = true;
1054 lto_output_toplevel_asms ();
1057 output_refs (encoder);
1060 /* Return identifier encoded in IB as a plain string. */
1062 static tree
1063 read_identifier (struct lto_input_block *ib)
1065 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1066 tree id;
1068 if (ib->data[ib->p + len])
1069 lto_section_overrun (ib);
1070 if (!len)
1072 ib->p++;
1073 return NULL;
1075 id = get_identifier (ib->data + ib->p);
1076 ib->p += len + 1;
1077 return id;
1080 /* Return string encoded in IB, NULL if string is empty. */
1082 static const char *
1083 read_string (struct lto_input_block *ib)
1085 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1086 const char *str;
1088 if (ib->data[ib->p + len])
1089 lto_section_overrun (ib);
1090 if (!len)
1092 ib->p++;
1093 return NULL;
1095 str = ib->data + ib->p;
1096 ib->p += len + 1;
1097 return str;
1100 /* Output function/variable tables that will allow libgomp to look up offload
1101 target code.
1102 OFFLOAD_FUNCS is filled in expand_omp_target, OFFLOAD_VARS is filled in
1103 varpool_node::get_create. In WHOPR (partitioned) mode during the WPA stage
1104 both OFFLOAD_FUNCS and OFFLOAD_VARS are filled by input_offload_tables. */
1106 void
1107 output_offload_tables (void)
1109 if (vec_safe_is_empty (offload_funcs) && vec_safe_is_empty (offload_vars))
1110 return;
1112 struct lto_simple_output_block *ob
1113 = lto_create_simple_output_block (LTO_section_offload_table);
1115 for (unsigned i = 0; i < vec_safe_length (offload_funcs); i++)
1117 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1118 LTO_symtab_last_tag, LTO_symtab_unavail_node);
1119 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
1120 (*offload_funcs)[i]);
1123 for (unsigned i = 0; i < vec_safe_length (offload_vars); i++)
1125 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1126 LTO_symtab_last_tag, LTO_symtab_variable);
1127 lto_output_var_decl_index (ob->decl_state, ob->main_stream,
1128 (*offload_vars)[i]);
1131 streamer_write_uhwi_stream (ob->main_stream, 0);
1132 lto_destroy_simple_output_block (ob);
1134 /* In WHOPR mode during the WPA stage the joint offload tables need to be
1135 streamed to one partition only. That's why we free offload_funcs and
1136 offload_vars after the first call of output_offload_tables. */
1137 if (flag_wpa)
1139 vec_free (offload_funcs);
1140 vec_free (offload_vars);
1144 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
1145 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
1146 NODE or to replace the values in it, for instance because the first
1147 time we saw it, the function body was not available but now it
1148 is. BP is a bitpack with all the bitflags for NODE read from the
1149 stream. */
1151 static void
1152 input_overwrite_node (struct lto_file_decl_data *file_data,
1153 struct cgraph_node *node,
1154 enum LTO_symtab_tags tag,
1155 struct bitpack_d *bp)
1157 node->aux = (void *) tag;
1158 node->lto_file_data = file_data;
1160 node->local.local = bp_unpack_value (bp, 1);
1161 node->externally_visible = bp_unpack_value (bp, 1);
1162 node->no_reorder = bp_unpack_value (bp, 1);
1163 node->definition = bp_unpack_value (bp, 1);
1164 node->local.versionable = bp_unpack_value (bp, 1);
1165 node->local.can_change_signature = bp_unpack_value (bp, 1);
1166 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
1167 node->force_output = bp_unpack_value (bp, 1);
1168 node->forced_by_abi = bp_unpack_value (bp, 1);
1169 node->unique_name = bp_unpack_value (bp, 1);
1170 node->body_removed = bp_unpack_value (bp, 1);
1171 node->implicit_section = bp_unpack_value (bp, 1);
1172 node->address_taken = bp_unpack_value (bp, 1);
1173 node->used_from_other_partition = bp_unpack_value (bp, 1);
1174 node->lowered = bp_unpack_value (bp, 1);
1175 node->analyzed = tag == LTO_symtab_analyzed_node;
1176 node->in_other_partition = bp_unpack_value (bp, 1);
1177 if (node->in_other_partition
1178 /* Avoid updating decl when we are seeing just inline clone.
1179 When inlining function that has functions already inlined into it,
1180 we produce clones of inline clones.
1182 WPA partitioning might put each clone into different unit and
1183 we might end up streaming inline clone from other partition
1184 to support clone we are interested in. */
1185 && (!node->clone_of
1186 || node->clone_of->decl != node->decl))
1188 DECL_EXTERNAL (node->decl) = 1;
1189 TREE_STATIC (node->decl) = 0;
1191 node->alias = bp_unpack_value (bp, 1);
1192 node->transparent_alias = bp_unpack_value (bp, 1);
1193 node->weakref = bp_unpack_value (bp, 1);
1194 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
1195 node->only_called_at_startup = bp_unpack_value (bp, 1);
1196 node->only_called_at_exit = bp_unpack_value (bp, 1);
1197 node->tm_clone = bp_unpack_value (bp, 1);
1198 node->calls_comdat_local = bp_unpack_value (bp, 1);
1199 node->icf_merged = bp_unpack_value (bp, 1);
1200 node->nonfreeing_fn = bp_unpack_value (bp, 1);
1201 node->thunk.thunk_p = bp_unpack_value (bp, 1);
1202 node->parallelized_function = bp_unpack_value (bp, 1);
1203 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
1204 LDPR_NUM_KNOWN);
1205 node->instrumentation_clone = bp_unpack_value (bp, 1);
1206 node->split_part = bp_unpack_value (bp, 1);
1207 gcc_assert (flag_ltrans
1208 || (!node->in_other_partition
1209 && !node->used_from_other_partition));
1212 /* Return string alias is alias of. */
1214 static tree
1215 get_alias_symbol (tree decl)
1217 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1218 return get_identifier (TREE_STRING_POINTER
1219 (TREE_VALUE (TREE_VALUE (alias))));
1222 /* Read a node from input_block IB. TAG is the node's tag just read.
1223 Return the node read or overwriten. */
1225 static struct cgraph_node *
1226 input_node (struct lto_file_decl_data *file_data,
1227 struct lto_input_block *ib,
1228 enum LTO_symtab_tags tag,
1229 vec<symtab_node *> nodes)
1231 gcc::pass_manager *passes = g->get_passes ();
1232 tree fn_decl;
1233 struct cgraph_node *node;
1234 struct bitpack_d bp;
1235 unsigned decl_index;
1236 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1237 int clone_ref;
1238 int order;
1239 int i, count;
1240 tree group;
1241 const char *section;
1242 order = streamer_read_hwi (ib) + order_base;
1243 clone_ref = streamer_read_hwi (ib);
1245 decl_index = streamer_read_uhwi (ib);
1246 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1248 if (clone_ref != LCC_NOT_FOUND)
1250 node = dyn_cast<cgraph_node *> (nodes[clone_ref])->create_clone (fn_decl,
1251 profile_count::uninitialized (), CGRAPH_FREQ_BASE, false,
1252 vNULL, false, NULL, NULL);
1254 else
1256 /* Declaration of functions can be already merged with a declaration
1257 from other input file. We keep cgraph unmerged until after streaming
1258 of ipa passes is done. Alays forcingly create a fresh node. */
1259 node = symtab->create_empty ();
1260 node->decl = fn_decl;
1261 node->register_symbol ();
1264 node->order = order;
1265 if (order >= symtab->order)
1266 symtab->order = order + 1;
1268 node->count = profile_count::stream_in (ib);
1269 node->count_materialization_scale = streamer_read_hwi (ib);
1271 count = streamer_read_hwi (ib);
1272 node->ipa_transforms_to_apply = vNULL;
1273 for (i = 0; i < count; i++)
1275 opt_pass *pass;
1276 int pid = streamer_read_hwi (ib);
1278 gcc_assert (pid < passes->passes_by_id_size);
1279 pass = passes->passes_by_id[pid];
1280 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *) pass);
1283 if (tag == LTO_symtab_analyzed_node)
1284 ref = streamer_read_hwi (ib);
1286 group = read_identifier (ib);
1287 if (group)
1288 ref2 = streamer_read_hwi (ib);
1290 /* Make sure that we have not read this node before. Nodes that
1291 have already been read will have their tag stored in the 'aux'
1292 field. Since built-in functions can be referenced in multiple
1293 functions, they are expected to be read more than once. */
1294 if (node->aux && !DECL_BUILT_IN (node->decl))
1295 internal_error ("bytecode stream: found multiple instances of cgraph "
1296 "node with uid %d", node->uid);
1298 node->tp_first_run = streamer_read_uhwi (ib);
1300 bp = streamer_read_bitpack (ib);
1302 input_overwrite_node (file_data, node, tag, &bp);
1304 /* Store a reference for now, and fix up later to be a pointer. */
1305 node->global.inlined_to = (cgraph_node *) (intptr_t) ref;
1307 if (group)
1309 node->set_comdat_group (group);
1310 /* Store a reference for now, and fix up later to be a pointer. */
1311 node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
1313 else
1314 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1315 section = read_string (ib);
1316 if (section)
1317 node->set_section_for_node (section);
1319 if (node->thunk.thunk_p)
1321 int type = streamer_read_uhwi (ib);
1322 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1323 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1325 node->thunk.fixed_offset = fixed_offset;
1326 node->thunk.this_adjusting = (type & 2);
1327 node->thunk.virtual_value = virtual_value;
1328 node->thunk.virtual_offset_p = (type & 4);
1329 node->thunk.add_pointer_bounds_args = (type & 8);
1331 if (node->alias && !node->analyzed && node->weakref)
1332 node->alias_target = get_alias_symbol (node->decl);
1333 node->profile_id = streamer_read_hwi (ib);
1334 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1335 node->set_init_priority (streamer_read_hwi (ib));
1336 if (DECL_STATIC_DESTRUCTOR (node->decl))
1337 node->set_fini_priority (streamer_read_hwi (ib));
1339 if (node->instrumentation_clone)
1341 decl_index = streamer_read_uhwi (ib);
1342 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1343 node->orig_decl = fn_decl;
1346 return node;
1349 /* Read a node from input_block IB. TAG is the node's tag just read.
1350 Return the node read or overwriten. */
1352 static varpool_node *
1353 input_varpool_node (struct lto_file_decl_data *file_data,
1354 struct lto_input_block *ib)
1356 int decl_index;
1357 tree var_decl;
1358 varpool_node *node;
1359 struct bitpack_d bp;
1360 int ref = LCC_NOT_FOUND;
1361 int order;
1362 tree group;
1363 const char *section;
1365 order = streamer_read_hwi (ib) + order_base;
1366 decl_index = streamer_read_uhwi (ib);
1367 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1369 /* Declaration of functions can be already merged with a declaration
1370 from other input file. We keep cgraph unmerged until after streaming
1371 of ipa passes is done. Alays forcingly create a fresh node. */
1372 node = varpool_node::create_empty ();
1373 node->decl = var_decl;
1374 node->register_symbol ();
1376 node->order = order;
1377 if (order >= symtab->order)
1378 symtab->order = order + 1;
1379 node->lto_file_data = file_data;
1381 bp = streamer_read_bitpack (ib);
1382 node->externally_visible = bp_unpack_value (&bp, 1);
1383 node->no_reorder = bp_unpack_value (&bp, 1);
1384 node->force_output = bp_unpack_value (&bp, 1);
1385 node->forced_by_abi = bp_unpack_value (&bp, 1);
1386 node->unique_name = bp_unpack_value (&bp, 1);
1387 node->body_removed = bp_unpack_value (&bp, 1);
1388 node->implicit_section = bp_unpack_value (&bp, 1);
1389 node->writeonly = bp_unpack_value (&bp, 1);
1390 node->definition = bp_unpack_value (&bp, 1);
1391 node->alias = bp_unpack_value (&bp, 1);
1392 node->transparent_alias = bp_unpack_value (&bp, 1);
1393 node->weakref = bp_unpack_value (&bp, 1);
1394 node->analyzed = bp_unpack_value (&bp, 1);
1395 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1396 node->in_other_partition = bp_unpack_value (&bp, 1);
1397 if (node->in_other_partition)
1399 DECL_EXTERNAL (node->decl) = 1;
1400 TREE_STATIC (node->decl) = 0;
1402 if (node->alias && !node->analyzed && node->weakref)
1403 node->alias_target = get_alias_symbol (node->decl);
1404 node->tls_model = (enum tls_model)bp_unpack_value (&bp, 3);
1405 node->used_by_single_function = (enum tls_model)bp_unpack_value (&bp, 1);
1406 node->dynamically_initialized = bp_unpack_value (&bp, 1);
1407 node->need_bounds_init = bp_unpack_value (&bp, 1);
1408 group = read_identifier (ib);
1409 if (group)
1411 node->set_comdat_group (group);
1412 ref = streamer_read_hwi (ib);
1413 /* Store a reference for now, and fix up later to be a pointer. */
1414 node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1416 else
1417 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1418 section = read_string (ib);
1419 if (section)
1420 node->set_section_for_node (section);
1421 node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1422 LDPR_NUM_KNOWN);
1423 gcc_assert (flag_ltrans
1424 || (!node->in_other_partition
1425 && !node->used_from_other_partition));
1427 return node;
1430 /* Read a node from input_block IB. TAG is the node's tag just read.
1431 Return the node read or overwriten. */
1433 static void
1434 input_ref (struct lto_input_block *ib,
1435 symtab_node *referring_node,
1436 vec<symtab_node *> nodes)
1438 symtab_node *node = NULL;
1439 struct bitpack_d bp;
1440 enum ipa_ref_use use;
1441 bool speculative;
1442 struct ipa_ref *ref;
1444 bp = streamer_read_bitpack (ib);
1445 use = (enum ipa_ref_use) bp_unpack_value (&bp, 3);
1446 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1447 node = nodes[streamer_read_hwi (ib)];
1448 ref = referring_node->create_reference (node, use);
1449 ref->speculative = speculative;
1450 if (is_a <cgraph_node *> (referring_node))
1451 ref->lto_stmt_uid = streamer_read_hwi (ib);
1454 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1455 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1456 edge being read is indirect (in the sense that it has
1457 indirect_unknown_callee set). */
1459 static void
1460 input_edge (struct lto_input_block *ib, vec<symtab_node *> nodes,
1461 bool indirect)
1463 struct cgraph_node *caller, *callee;
1464 struct cgraph_edge *edge;
1465 unsigned int stmt_id;
1466 profile_count count;
1467 int freq;
1468 cgraph_inline_failed_t inline_failed;
1469 struct bitpack_d bp;
1470 int ecf_flags = 0;
1472 caller = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1473 if (caller == NULL || caller->decl == NULL_TREE)
1474 internal_error ("bytecode stream: no caller found while reading edge");
1476 if (!indirect)
1478 callee = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1479 if (callee == NULL || callee->decl == NULL_TREE)
1480 internal_error ("bytecode stream: no callee found while reading edge");
1482 else
1483 callee = NULL;
1485 count = profile_count::stream_in (ib);
1487 bp = streamer_read_bitpack (ib);
1488 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_t, CIF_N_REASONS);
1489 stmt_id = bp_unpack_var_len_unsigned (&bp);
1490 freq = (int) bp_unpack_var_len_unsigned (&bp);
1492 if (indirect)
1493 edge = caller->create_indirect_edge (NULL, 0, count, freq);
1494 else
1495 edge = caller->create_edge (callee, NULL, count, freq);
1497 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1498 edge->speculative = bp_unpack_value (&bp, 1);
1499 edge->lto_stmt_uid = stmt_id;
1500 edge->inline_failed = inline_failed;
1501 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1502 edge->can_throw_external = bp_unpack_value (&bp, 1);
1503 edge->in_polymorphic_cdtor = bp_unpack_value (&bp, 1);
1504 if (indirect)
1506 if (bp_unpack_value (&bp, 1))
1507 ecf_flags |= ECF_CONST;
1508 if (bp_unpack_value (&bp, 1))
1509 ecf_flags |= ECF_PURE;
1510 if (bp_unpack_value (&bp, 1))
1511 ecf_flags |= ECF_NORETURN;
1512 if (bp_unpack_value (&bp, 1))
1513 ecf_flags |= ECF_MALLOC;
1514 if (bp_unpack_value (&bp, 1))
1515 ecf_flags |= ECF_NOTHROW;
1516 if (bp_unpack_value (&bp, 1))
1517 ecf_flags |= ECF_RETURNS_TWICE;
1518 edge->indirect_info->ecf_flags = ecf_flags;
1519 edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1520 if (edge->indirect_info->common_target_id)
1521 edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1526 /* Read a cgraph from IB using the info in FILE_DATA. */
1528 static vec<symtab_node *>
1529 input_cgraph_1 (struct lto_file_decl_data *file_data,
1530 struct lto_input_block *ib)
1532 enum LTO_symtab_tags tag;
1533 vec<symtab_node *> nodes = vNULL;
1534 symtab_node *node;
1535 unsigned i;
1537 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1538 order_base = symtab->order;
1539 while (tag)
1541 if (tag == LTO_symtab_edge)
1542 input_edge (ib, nodes, false);
1543 else if (tag == LTO_symtab_indirect_edge)
1544 input_edge (ib, nodes, true);
1545 else if (tag == LTO_symtab_variable)
1547 node = input_varpool_node (file_data, ib);
1548 nodes.safe_push (node);
1549 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1551 else
1553 node = input_node (file_data, ib, tag, nodes);
1554 if (node == NULL || node->decl == NULL_TREE)
1555 internal_error ("bytecode stream: found empty cgraph node");
1556 nodes.safe_push (node);
1557 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1560 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1563 lto_input_toplevel_asms (file_data, order_base);
1565 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1566 if (flag_checking)
1568 FOR_EACH_VEC_ELT (nodes, i, node)
1569 gcc_assert (node->aux || !is_a <cgraph_node *> (node));
1571 FOR_EACH_VEC_ELT (nodes, i, node)
1573 int ref;
1574 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1576 ref = (int) (intptr_t) cnode->global.inlined_to;
1578 /* We share declaration of builtins, so we may read same node twice. */
1579 if (!node->aux)
1580 continue;
1581 node->aux = NULL;
1583 /* Fixup inlined_to from reference to pointer. */
1584 if (ref != LCC_NOT_FOUND)
1585 dyn_cast<cgraph_node *> (node)->global.inlined_to
1586 = dyn_cast<cgraph_node *> (nodes[ref]);
1587 else
1588 cnode->global.inlined_to = NULL;
1590 /* Compute instrumented_version. */
1591 if (cnode->instrumentation_clone)
1593 gcc_assert (cnode->orig_decl);
1595 cnode->instrumented_version = cgraph_node::get (cnode->orig_decl);
1596 if (cnode->instrumented_version)
1598 /* We may have multiple nodes for a single function which
1599 will be merged later. To have a proper merge we need
1600 to keep instrumentation_version reference between nodes
1601 consistent: each instrumented_version reference should
1602 have proper reverse reference. Thus don't break existing
1603 instrumented_version reference if it already exists. */
1604 if (cnode->instrumented_version->instrumented_version)
1605 cnode->instrumented_version = NULL;
1606 else
1607 cnode->instrumented_version->instrumented_version = cnode;
1610 /* Restore decl names reference except for wrapper functions. */
1611 if (!chkp_wrap_function (cnode->orig_decl))
1613 tree name = DECL_ASSEMBLER_NAME (cnode->decl);
1614 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
1615 TREE_CHAIN (name) = DECL_ASSEMBLER_NAME (cnode->orig_decl);
1620 ref = (int) (intptr_t) node->same_comdat_group;
1622 /* Fixup same_comdat_group from reference to pointer. */
1623 if (ref != LCC_NOT_FOUND)
1624 node->same_comdat_group = nodes[ref];
1625 else
1626 node->same_comdat_group = NULL;
1628 FOR_EACH_VEC_ELT (nodes, i, node)
1629 node->aux = is_a <cgraph_node *> (node) ? (void *)1 : NULL;
1630 return nodes;
1633 /* Input ipa_refs. */
1635 static void
1636 input_refs (struct lto_input_block *ib,
1637 vec<symtab_node *> nodes)
1639 int count;
1640 int idx;
1641 while (true)
1643 symtab_node *node;
1644 count = streamer_read_uhwi (ib);
1645 if (!count)
1646 break;
1647 idx = streamer_read_uhwi (ib);
1648 node = nodes[idx];
1649 while (count)
1651 input_ref (ib, node, nodes);
1652 count--;
1658 static struct gcov_ctr_summary lto_gcov_summary;
1660 /* Input profile_info from IB. */
1661 static void
1662 input_profile_summary (struct lto_input_block *ib,
1663 struct lto_file_decl_data *file_data)
1665 unsigned h_ix;
1666 struct bitpack_d bp;
1667 unsigned int runs = streamer_read_uhwi (ib);
1668 if (runs)
1670 file_data->profile_info.runs = runs;
1671 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1672 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1674 memset (file_data->profile_info.histogram, 0,
1675 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1676 /* Input the bitpack of non-zero histogram indices. */
1677 bp = streamer_read_bitpack (ib);
1678 /* Read in and unpack the full bitpack, flagging non-zero
1679 histogram entries by setting the num_counters non-zero. */
1680 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1682 file_data->profile_info.histogram[h_ix].num_counters
1683 = bp_unpack_value (&bp, 1);
1685 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1687 if (!file_data->profile_info.histogram[h_ix].num_counters)
1688 continue;
1690 file_data->profile_info.histogram[h_ix].num_counters
1691 = streamer_read_gcov_count (ib);
1692 file_data->profile_info.histogram[h_ix].min_value
1693 = streamer_read_gcov_count (ib);
1694 file_data->profile_info.histogram[h_ix].cum_value
1695 = streamer_read_gcov_count (ib);
1697 /* IPA-profile computes hot bb threshold based on cumulated
1698 whole program profile. We need to stream it down to ltrans. */
1699 if (flag_ltrans)
1700 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1705 /* Rescale profile summaries to the same number of runs in the whole unit. */
1707 static void
1708 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1710 struct lto_file_decl_data *file_data;
1711 unsigned int j, h_ix;
1712 gcov_unsigned_t max_runs = 0;
1713 struct cgraph_node *node;
1714 struct cgraph_edge *edge;
1715 gcov_type saved_sum_all = 0;
1716 gcov_ctr_summary *saved_profile_info = 0;
1717 int saved_scale = 0;
1719 /* Find unit with maximal number of runs. If we ever get serious about
1720 roundoff errors, we might also consider computing smallest common
1721 multiply. */
1722 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1723 if (max_runs < file_data->profile_info.runs)
1724 max_runs = file_data->profile_info.runs;
1726 if (!max_runs)
1727 return;
1729 /* Simple overflow check. We probably don't need to support that many train
1730 runs. Such a large value probably imply data corruption anyway. */
1731 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1733 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1734 INT_MAX / REG_BR_PROB_BASE);
1735 return;
1738 profile_info = &lto_gcov_summary;
1739 lto_gcov_summary.runs = max_runs;
1740 lto_gcov_summary.sum_max = 0;
1741 memset (lto_gcov_summary.histogram, 0,
1742 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1744 /* Rescale all units to the maximal number of runs.
1745 sum_max can not be easily merged, as we have no idea what files come from
1746 the same run. We do not use the info anyway, so leave it 0. */
1747 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1748 if (file_data->profile_info.runs)
1750 int scale = GCOV_COMPUTE_SCALE (max_runs,
1751 file_data->profile_info.runs);
1752 lto_gcov_summary.sum_max
1753 = MAX (lto_gcov_summary.sum_max,
1754 apply_scale (file_data->profile_info.sum_max, scale));
1755 lto_gcov_summary.sum_all
1756 = MAX (lto_gcov_summary.sum_all,
1757 apply_scale (file_data->profile_info.sum_all, scale));
1758 /* Save a pointer to the profile_info with the largest
1759 scaled sum_all and the scale for use in merging the
1760 histogram. */
1761 if (!saved_profile_info
1762 || lto_gcov_summary.sum_all > saved_sum_all)
1764 saved_profile_info = &file_data->profile_info;
1765 saved_sum_all = lto_gcov_summary.sum_all;
1766 saved_scale = scale;
1770 gcc_assert (saved_profile_info);
1772 /* Scale up the histogram from the profile that had the largest
1773 scaled sum_all above. */
1774 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1776 /* Scale up the min value as we did the corresponding sum_all
1777 above. Use that to find the new histogram index. */
1778 gcov_type scaled_min
1779 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1780 saved_scale);
1781 /* The new index may be shared with another scaled histogram entry,
1782 so we need to account for a non-zero histogram entry at new_ix. */
1783 unsigned new_ix = gcov_histo_index (scaled_min);
1784 lto_gcov_summary.histogram[new_ix].min_value
1785 = (lto_gcov_summary.histogram[new_ix].num_counters
1786 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1787 : scaled_min);
1788 /* Some of the scaled counter values would ostensibly need to be placed
1789 into different (larger) histogram buckets, but we keep things simple
1790 here and place the scaled cumulative counter value in the bucket
1791 corresponding to the scaled minimum counter value. */
1792 lto_gcov_summary.histogram[new_ix].cum_value
1793 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1794 saved_scale);
1795 lto_gcov_summary.histogram[new_ix].num_counters
1796 += saved_profile_info->histogram[h_ix].num_counters;
1799 /* Watch roundoff errors. */
1800 if (lto_gcov_summary.sum_max < max_runs)
1801 lto_gcov_summary.sum_max = max_runs;
1803 /* If merging already happent at WPA time, we are done. */
1804 if (flag_ltrans)
1805 return;
1807 /* Now compute count_materialization_scale of each node.
1808 During LTRANS we already have values of count_materialization_scale
1809 computed, so just update them. */
1810 FOR_EACH_FUNCTION (node)
1811 if (node->lto_file_data
1812 && node->lto_file_data->profile_info.runs)
1814 int scale;
1816 scale = RDIV (node->count_materialization_scale * max_runs,
1817 node->lto_file_data->profile_info.runs);
1818 node->count_materialization_scale = scale;
1819 if (scale < 0)
1820 fatal_error (input_location, "Profile information in %s corrupted",
1821 file_data->file_name);
1823 if (scale == REG_BR_PROB_BASE)
1824 continue;
1825 for (edge = node->callees; edge; edge = edge->next_callee)
1826 edge->count = edge->count.apply_scale (scale, REG_BR_PROB_BASE);
1827 node->count = node->count.apply_scale (scale, REG_BR_PROB_BASE);
1831 /* Input and merge the symtab from each of the .o files passed to
1832 lto1. */
1834 void
1835 input_symtab (void)
1837 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1838 struct lto_file_decl_data *file_data;
1839 unsigned int j = 0;
1840 struct cgraph_node *node;
1842 while ((file_data = file_data_vec[j++]))
1844 const char *data;
1845 size_t len;
1846 struct lto_input_block *ib;
1847 vec<symtab_node *> nodes;
1849 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1850 &data, &len);
1851 if (!ib)
1852 fatal_error (input_location,
1853 "cannot find LTO cgraph in %s", file_data->file_name);
1854 input_profile_summary (ib, file_data);
1855 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1856 nodes = input_cgraph_1 (file_data, ib);
1857 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1858 ib, data, len);
1860 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1861 &data, &len);
1862 if (!ib)
1863 fatal_error (input_location, "cannot find LTO section refs in %s",
1864 file_data->file_name);
1865 input_refs (ib, nodes);
1866 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1867 ib, data, len);
1868 if (flag_ltrans)
1869 input_cgraph_opt_summary (nodes);
1870 nodes.release ();
1873 merge_profile_summaries (file_data_vec);
1875 if (!flag_auto_profile)
1876 get_working_sets ();
1879 /* Clear out the aux field that was used to store enough state to
1880 tell which nodes should be overwritten. */
1881 FOR_EACH_FUNCTION (node)
1883 /* Some nodes may have been created by cgraph_node. This
1884 happens when the callgraph contains nested functions. If the
1885 node for the parent function was never emitted to the gimple
1886 file, cgraph_node will create a node for it when setting the
1887 context of the nested function. */
1888 if (node->lto_file_data)
1889 node->aux = NULL;
1893 /* Input function/variable tables that will allow libgomp to look up offload
1894 target code, and store them into OFFLOAD_FUNCS and OFFLOAD_VARS. */
1896 void
1897 input_offload_tables (bool do_force_output)
1899 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1900 struct lto_file_decl_data *file_data;
1901 unsigned int j = 0;
1903 while ((file_data = file_data_vec[j++]))
1905 const char *data;
1906 size_t len;
1907 struct lto_input_block *ib
1908 = lto_create_simple_input_block (file_data, LTO_section_offload_table,
1909 &data, &len);
1910 if (!ib)
1911 continue;
1913 enum LTO_symtab_tags tag
1914 = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1915 while (tag)
1917 if (tag == LTO_symtab_unavail_node)
1919 int decl_index = streamer_read_uhwi (ib);
1920 tree fn_decl
1921 = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1922 vec_safe_push (offload_funcs, fn_decl);
1924 /* Prevent IPA from removing fn_decl as unreachable, since there
1925 may be no refs from the parent function to child_fn in offload
1926 LTO mode. */
1927 if (do_force_output)
1928 cgraph_node::get (fn_decl)->mark_force_output ();
1930 else if (tag == LTO_symtab_variable)
1932 int decl_index = streamer_read_uhwi (ib);
1933 tree var_decl
1934 = lto_file_decl_data_get_var_decl (file_data, decl_index);
1935 vec_safe_push (offload_vars, var_decl);
1937 /* Prevent IPA from removing var_decl as unused, since there
1938 may be no refs to var_decl in offload LTO mode. */
1939 if (do_force_output)
1940 varpool_node::get (var_decl)->force_output = 1;
1942 else
1943 fatal_error (input_location,
1944 "invalid offload table in %s", file_data->file_name);
1946 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1949 lto_destroy_simple_input_block (file_data, LTO_section_offload_table,
1950 ib, data, len);
1954 /* True when we need optimization summary for NODE. */
1956 static int
1957 output_cgraph_opt_summary_p (struct cgraph_node *node)
1959 return (node->clone_of
1960 && (node->clone.tree_map
1961 || node->clone.args_to_skip
1962 || node->clone.combined_args_to_skip));
1965 /* Output optimization summary for EDGE to OB. */
1966 static void
1967 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1968 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1972 /* Output optimization summary for NODE to OB. */
1974 static void
1975 output_node_opt_summary (struct output_block *ob,
1976 struct cgraph_node *node,
1977 lto_symtab_encoder_t encoder)
1979 unsigned int index;
1980 bitmap_iterator bi;
1981 struct ipa_replace_map *map;
1982 struct bitpack_d bp;
1983 int i;
1984 struct cgraph_edge *e;
1986 if (node->clone.args_to_skip)
1988 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1989 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1990 streamer_write_uhwi (ob, index);
1992 else
1993 streamer_write_uhwi (ob, 0);
1994 if (node->clone.combined_args_to_skip)
1996 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1997 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1998 streamer_write_uhwi (ob, index);
2000 else
2001 streamer_write_uhwi (ob, 0);
2002 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
2003 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
2005 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
2006 mechanism to store function local declarations into summaries. */
2007 gcc_assert (!map->old_tree);
2008 streamer_write_uhwi (ob, map->parm_num);
2009 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
2010 stream_write_tree (ob, map->new_tree, true);
2011 bp = bitpack_create (ob->main_stream);
2012 bp_pack_value (&bp, map->replace_p, 1);
2013 bp_pack_value (&bp, map->ref_p, 1);
2014 streamer_write_bitpack (&bp);
2017 if (lto_symtab_encoder_in_partition_p (encoder, node))
2019 for (e = node->callees; e; e = e->next_callee)
2020 output_edge_opt_summary (ob, e);
2021 for (e = node->indirect_calls; e; e = e->next_callee)
2022 output_edge_opt_summary (ob, e);
2026 /* Output optimization summaries stored in callgraph.
2027 At the moment it is the clone info structure. */
2029 static void
2030 output_cgraph_opt_summary (void)
2032 int i, n_nodes;
2033 lto_symtab_encoder_t encoder;
2034 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
2035 unsigned count = 0;
2037 ob->symbol = NULL;
2038 encoder = ob->decl_state->symtab_node_encoder;
2039 n_nodes = lto_symtab_encoder_size (encoder);
2040 for (i = 0; i < n_nodes; i++)
2042 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
2043 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2044 if (cnode && output_cgraph_opt_summary_p (cnode))
2045 count++;
2047 streamer_write_uhwi (ob, count);
2048 for (i = 0; i < n_nodes; i++)
2050 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
2051 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2052 if (cnode && output_cgraph_opt_summary_p (cnode))
2054 streamer_write_uhwi (ob, i);
2055 output_node_opt_summary (ob, cnode, encoder);
2058 produce_asm (ob, NULL);
2059 destroy_output_block (ob);
2062 /* Input optimisation summary of EDGE. */
2064 static void
2065 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
2066 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
2070 /* Input optimisation summary of NODE. */
2072 static void
2073 input_node_opt_summary (struct cgraph_node *node,
2074 struct lto_input_block *ib_main,
2075 struct data_in *data_in)
2077 int i;
2078 int count;
2079 int bit;
2080 struct bitpack_d bp;
2081 struct cgraph_edge *e;
2083 count = streamer_read_uhwi (ib_main);
2084 if (count)
2085 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
2086 for (i = 0; i < count; i++)
2088 bit = streamer_read_uhwi (ib_main);
2089 bitmap_set_bit (node->clone.args_to_skip, bit);
2091 count = streamer_read_uhwi (ib_main);
2092 if (count)
2093 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
2094 for (i = 0; i < count; i++)
2096 bit = streamer_read_uhwi (ib_main);
2097 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
2099 count = streamer_read_uhwi (ib_main);
2100 for (i = 0; i < count; i++)
2102 struct ipa_replace_map *map = ggc_alloc<ipa_replace_map> ();
2104 vec_safe_push (node->clone.tree_map, map);
2105 map->parm_num = streamer_read_uhwi (ib_main);
2106 map->old_tree = NULL;
2107 map->new_tree = stream_read_tree (ib_main, data_in);
2108 bp = streamer_read_bitpack (ib_main);
2109 map->replace_p = bp_unpack_value (&bp, 1);
2110 map->ref_p = bp_unpack_value (&bp, 1);
2112 for (e = node->callees; e; e = e->next_callee)
2113 input_edge_opt_summary (e, ib_main);
2114 for (e = node->indirect_calls; e; e = e->next_callee)
2115 input_edge_opt_summary (e, ib_main);
2118 /* Read section in file FILE_DATA of length LEN with data DATA. */
2120 static void
2121 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
2122 const char *data, size_t len,
2123 vec<symtab_node *> nodes)
2125 const struct lto_function_header *header =
2126 (const struct lto_function_header *) data;
2127 const int cfg_offset = sizeof (struct lto_function_header);
2128 const int main_offset = cfg_offset + header->cfg_size;
2129 const int string_offset = main_offset + header->main_size;
2130 struct data_in *data_in;
2131 unsigned int i;
2132 unsigned int count;
2134 lto_input_block ib_main ((const char *) data + main_offset,
2135 header->main_size, file_data->mode_table);
2137 data_in =
2138 lto_data_in_create (file_data, (const char *) data + string_offset,
2139 header->string_size, vNULL);
2140 count = streamer_read_uhwi (&ib_main);
2142 for (i = 0; i < count; i++)
2144 int ref = streamer_read_uhwi (&ib_main);
2145 input_node_opt_summary (dyn_cast<cgraph_node *> (nodes[ref]),
2146 &ib_main, data_in);
2148 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
2149 len);
2150 lto_data_in_delete (data_in);
2153 /* Input optimization summary of cgraph. */
2155 static void
2156 input_cgraph_opt_summary (vec<symtab_node *> nodes)
2158 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2159 struct lto_file_decl_data *file_data;
2160 unsigned int j = 0;
2162 while ((file_data = file_data_vec[j++]))
2164 size_t len;
2165 const char *data =
2166 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
2167 &len);
2169 if (data)
2170 input_cgraph_opt_section (file_data, data, len, nodes);