Skip -fwhole-program when merging LTO options.
[official-gcc.git] / gcc / lto-cgraph.cc
blobad0005f01d21d3b9e18d94aab18c351350f65bf4
1 /* Write and read the cgraph to the memory mapped representation of a
2 .o file.
4 Copyright (C) 2009-2022 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 "omp-general.h"
41 #include "stringpool.h"
42 #include "attribs.h"
43 #include "alloc-pool.h"
44 #include "symbol-summary.h"
45 #include "symtab-thunks.h"
46 #include "symtab-clones.h"
48 /* True when asm nodes has been output. */
49 bool asm_nodes_output = false;
51 static void output_cgraph_opt_summary (void);
52 static void input_cgraph_opt_summary (vec<symtab_node *> nodes);
54 /* Number of LDPR values known to GCC. */
55 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
57 /* Cgraph streaming is organized as set of record whose type
58 is indicated by a tag. */
59 enum LTO_symtab_tags
61 /* Must leave 0 for the stopper. */
63 /* Cgraph node without body available. */
64 LTO_symtab_unavail_node = 1,
65 /* Cgraph node with function body. */
66 LTO_symtab_analyzed_node,
67 /* Cgraph edges. */
68 LTO_symtab_edge,
69 LTO_symtab_indirect_edge,
70 LTO_symtab_variable,
71 LTO_symtab_last_tag
74 /* Create a new symtab encoder.
75 if FOR_INPUT, the encoder allocate only datastructures needed
76 to read the symtab. */
78 lto_symtab_encoder_t
79 lto_symtab_encoder_new (bool for_input)
81 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
83 if (!for_input)
84 encoder->map = new hash_map<symtab_node *, size_t>;
85 encoder->nodes.create (0);
86 return encoder;
90 /* Delete ENCODER and its components. */
92 void
93 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
95 encoder->nodes.release ();
96 if (encoder->map)
97 delete encoder->map;
98 free (encoder);
102 /* Return the existing reference number of NODE in the symtab encoder in
103 output block OB. Assign a new reference if this is the first time
104 NODE is encoded. */
107 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
108 symtab_node *node)
110 int ref;
112 if (!encoder->map)
114 lto_encoder_entry entry = {node, false, false, false};
116 ref = encoder->nodes.length ();
117 encoder->nodes.safe_push (entry);
118 return ref;
121 size_t *slot = encoder->map->get (node);
122 if (!slot || !*slot)
124 lto_encoder_entry entry = {node, false, false, false};
125 ref = encoder->nodes.length ();
126 if (!slot)
127 encoder->map->put (node, ref + 1);
128 encoder->nodes.safe_push (entry);
130 else
131 ref = *slot - 1;
133 return ref;
136 /* Remove NODE from encoder. */
138 bool
139 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
140 symtab_node *node)
142 int index;
143 lto_encoder_entry last_node;
145 size_t *slot = encoder->map->get (node);
146 if (slot == NULL || !*slot)
147 return false;
149 index = *slot - 1;
150 gcc_checking_assert (encoder->nodes[index].node == node);
152 /* Remove from vector. We do this by swapping node with the last element
153 of the vector. */
154 last_node = encoder->nodes.pop ();
155 if (last_node.node != node)
157 gcc_assert (encoder->map->put (last_node.node, index + 1));
159 /* Move the last element to the original spot of NODE. */
160 encoder->nodes[index] = last_node;
163 /* Remove element from hash table. */
164 encoder->map->remove (node);
165 return true;
169 /* Return TRUE if we should encode the body of NODE (if any). */
171 bool
172 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
173 struct cgraph_node *node)
175 int index = lto_symtab_encoder_lookup (encoder, node);
176 return encoder->nodes[index].body;
179 /* Specify that we encode the body of NODE in this partition. */
181 static void
182 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
183 struct cgraph_node *node)
185 int index = lto_symtab_encoder_encode (encoder, node);
186 gcc_checking_assert (encoder->nodes[index].node == node);
187 encoder->nodes[index].body = true;
190 /* Return TRUE if we should encode initializer of NODE (if any). */
192 bool
193 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
194 varpool_node *node)
196 int index = lto_symtab_encoder_lookup (encoder, node);
197 if (index == LCC_NOT_FOUND)
198 return false;
199 return encoder->nodes[index].initializer;
202 /* Specify that we should encode initializer of NODE (if any). */
204 static void
205 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
206 varpool_node *node)
208 int index = lto_symtab_encoder_lookup (encoder, node);
209 encoder->nodes[index].initializer = true;
212 /* Return TRUE if NODE is in this partition. */
214 bool
215 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
216 symtab_node *node)
218 int index = lto_symtab_encoder_lookup (encoder, node);
219 if (index == LCC_NOT_FOUND)
220 return false;
221 return encoder->nodes[index].in_partition;
224 /* Specify that NODE is in this partition. */
226 void
227 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
228 symtab_node *node)
230 int index = lto_symtab_encoder_encode (encoder, node);
231 encoder->nodes[index].in_partition = true;
234 /* Output the cgraph EDGE to OB using ENCODER. */
236 static void
237 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
238 lto_symtab_encoder_t encoder)
240 unsigned int uid;
241 intptr_t ref;
242 struct bitpack_d bp;
244 if (edge->indirect_unknown_callee)
245 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
246 LTO_symtab_indirect_edge);
247 else
248 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
249 LTO_symtab_edge);
251 ref = lto_symtab_encoder_lookup (encoder, edge->caller);
252 gcc_assert (ref != LCC_NOT_FOUND);
253 streamer_write_hwi_stream (ob->main_stream, ref);
255 if (!edge->indirect_unknown_callee)
257 ref = lto_symtab_encoder_lookup (encoder, edge->callee);
258 gcc_assert (ref != LCC_NOT_FOUND);
259 streamer_write_hwi_stream (ob->main_stream, ref);
262 edge->count.stream_out (ob->main_stream);
264 bp = bitpack_create (ob->main_stream);
265 uid = !edge->call_stmt ? edge->lto_stmt_uid
266 : gimple_uid (edge->call_stmt) + 1;
267 bp_pack_enum (&bp, cgraph_inline_failed_t,
268 CIF_N_REASONS, edge->inline_failed);
269 gcc_checking_assert (uid || edge->caller->thunk);
270 bp_pack_var_len_unsigned (&bp, uid);
271 bp_pack_value (&bp, edge->speculative_id, 16);
272 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
273 bp_pack_value (&bp, edge->speculative, 1);
274 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
275 gcc_assert (!edge->call_stmt_cannot_inline_p
276 || edge->inline_failed != CIF_BODY_NOT_AVAILABLE);
277 bp_pack_value (&bp, edge->can_throw_external, 1);
278 bp_pack_value (&bp, edge->in_polymorphic_cdtor, 1);
279 if (edge->indirect_unknown_callee)
281 int flags = edge->indirect_info->ecf_flags;
282 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
283 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
284 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
285 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
286 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
287 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
288 /* Flags that should not appear on indirect calls. */
289 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
290 | ECF_MAY_BE_ALLOCA
291 | ECF_SIBCALL
292 | ECF_LEAF
293 | ECF_NOVOPS)));
295 bp_pack_value (&bp, edge->indirect_info->num_speculative_call_targets,
296 16);
298 streamer_write_bitpack (&bp);
301 /* Return if NODE contain references from other partitions. */
303 bool
304 referenced_from_other_partition_p (symtab_node *node, lto_symtab_encoder_t encoder)
306 int i;
307 struct ipa_ref *ref = NULL;
309 for (i = 0; node->iterate_referring (i, ref); i++)
311 /* Ignore references from non-offloadable nodes while streaming NODE into
312 offload LTO section. */
313 if (!ref->referring->need_lto_streaming)
314 continue;
316 if (ref->referring->in_other_partition
317 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
318 return true;
320 return false;
323 /* Return true when node is reachable from other partition. */
325 bool
326 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
328 struct cgraph_edge *e;
329 if (!node->definition)
330 return false;
331 if (node->inlined_to)
332 return false;
333 for (e = node->callers; e; e = e->next_caller)
335 /* Ignore references from non-offloadable nodes while streaming NODE into
336 offload LTO section. */
337 if (!e->caller->need_lto_streaming)
338 continue;
340 if (e->caller->in_other_partition
341 || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
342 return true;
344 return false;
347 /* Return if NODE contain references from other partitions. */
349 bool
350 referenced_from_this_partition_p (symtab_node *node,
351 lto_symtab_encoder_t encoder)
353 int i;
354 struct ipa_ref *ref = NULL;
356 for (i = 0; node->iterate_referring (i, ref); i++)
357 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
358 return true;
359 return false;
362 /* Return true when node is reachable from other partition. */
364 bool
365 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
367 struct cgraph_edge *e;
368 for (e = node->callers; e; e = e->next_caller)
369 if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
370 return true;
371 return false;
374 /* Output the cgraph NODE to OB. ENCODER is used to find the
375 reference number of NODE->inlined_to. SET is the set of nodes we
376 are writing to the current file. If NODE is not in SET, then NODE
377 is a boundary of a cgraph_node_set and we pretend NODE just has a
378 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
379 that have had their callgraph node written so far. This is used to
380 determine if NODE is a clone of a previously written node. */
382 static void
383 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
384 lto_symtab_encoder_t encoder)
386 unsigned int tag;
387 struct bitpack_d bp;
388 bool boundary_p;
389 intptr_t ref;
390 bool in_other_partition = false;
391 struct cgraph_node *clone_of, *ultimate_clone_of;
392 ipa_opt_pass_d *pass;
393 int i;
394 const char *comdat;
395 const char *section;
396 tree group;
398 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
400 if (node->analyzed && (!boundary_p || node->alias
401 || (node->thunk && !node->inlined_to)))
402 tag = LTO_symtab_analyzed_node;
403 else
404 tag = LTO_symtab_unavail_node;
406 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
407 tag);
408 streamer_write_hwi_stream (ob->main_stream, node->order);
410 /* In WPA mode, we only output part of the call-graph. Also, we
411 fake cgraph node attributes. There are two cases that we care.
413 Boundary nodes: There are nodes that are not part of SET but are
414 called from within SET. We artificially make them look like
415 externally visible nodes with no function body.
417 Cherry-picked nodes: These are nodes we pulled from other
418 translation units into SET during IPA-inlining. We make them as
419 local static nodes to prevent clashes with other local statics. */
420 if (boundary_p && node->analyzed
421 && node->get_partitioning_class () == SYMBOL_PARTITION)
423 /* Inline clones cannot be part of boundary.
424 gcc_assert (!node->inlined_to);
426 FIXME: At the moment they can be, when partition contains an inline
427 clone that is clone of inline clone from outside partition. We can
428 reshape the clone tree and make other tree to be the root, but it
429 needs a bit extra work and will be promplty done by cgraph_remove_node
430 after reading back. */
431 in_other_partition = 1;
433 else if (UNLIKELY (lto_stream_offload_p
434 && lookup_attribute ("omp target device_ancestor_host",
435 DECL_ATTRIBUTES (node->decl))))
436 /* This symbol is only used as argument to IFN_GOMP_TARGET_REV; this IFN
437 is ignored on ACCEL_COMPILER. Thus, mark it as in_other_partition to silence
438 verify_node_partition diagnostic. */
439 in_other_partition = 1;
441 clone_of = node->clone_of;
442 while (clone_of
443 && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
444 if (clone_of->prev_sibling_clone)
445 clone_of = clone_of->prev_sibling_clone;
446 else
447 clone_of = clone_of->clone_of;
449 /* See if body of the master function is output. If not, we are seeing only
450 an declaration and we do not need to pass down clone tree. */
451 ultimate_clone_of = clone_of;
452 while (ultimate_clone_of && ultimate_clone_of->clone_of)
453 ultimate_clone_of = ultimate_clone_of->clone_of;
455 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
456 clone_of = NULL;
458 if (tag == LTO_symtab_analyzed_node)
459 gcc_assert (clone_of || !node->clone_of);
460 if (!clone_of)
461 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
462 else
463 streamer_write_hwi_stream (ob->main_stream, ref);
466 lto_output_fn_decl_ref (ob->decl_state, ob->main_stream, node->decl);
467 node->count.stream_out (ob->main_stream);
468 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
470 streamer_write_hwi_stream (ob->main_stream,
471 node->ipa_transforms_to_apply.length ());
472 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
473 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
475 if (tag == LTO_symtab_analyzed_node)
477 if (node->inlined_to)
479 ref = lto_symtab_encoder_lookup (encoder, node->inlined_to);
480 gcc_assert (ref != LCC_NOT_FOUND);
482 else
483 ref = LCC_NOT_FOUND;
485 streamer_write_hwi_stream (ob->main_stream, ref);
488 group = node->get_comdat_group ();
489 if (group)
490 comdat = IDENTIFIER_POINTER (group);
491 else
492 comdat = "";
493 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
495 if (group)
497 if (node->same_comdat_group)
499 ref = LCC_NOT_FOUND;
500 for (struct symtab_node *n = node->same_comdat_group;
501 ref == LCC_NOT_FOUND && n != node; n = n->same_comdat_group)
502 ref = lto_symtab_encoder_lookup (encoder, n);
504 else
505 ref = LCC_NOT_FOUND;
506 streamer_write_hwi_stream (ob->main_stream, ref);
509 section = node->get_section ();
510 if (!section)
511 section = "";
513 streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
515 bp = bitpack_create (ob->main_stream);
516 bp_pack_value (&bp, node->local, 1);
517 bp_pack_value (&bp, node->externally_visible, 1);
518 bp_pack_value (&bp, node->no_reorder, 1);
519 bp_pack_value (&bp, node->definition, 1);
520 bp_pack_value (&bp, node->versionable, 1);
521 bp_pack_value (&bp, node->can_change_signature, 1);
522 bp_pack_value (&bp, node->redefined_extern_inline, 1);
523 bp_pack_value (&bp, node->force_output, 1);
524 bp_pack_value (&bp, node->forced_by_abi, 1);
525 bp_pack_value (&bp, node->unique_name, 1);
526 bp_pack_value (&bp, node->body_removed, 1);
527 bp_pack_value (&bp, node->semantic_interposition, 1);
528 bp_pack_value (&bp, node->implicit_section, 1);
529 bp_pack_value (&bp, node->address_taken, 1);
530 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
531 && node->get_partitioning_class () == SYMBOL_PARTITION
532 && (reachable_from_other_partition_p (node, encoder)
533 || referenced_from_other_partition_p (node, encoder)), 1);
534 bp_pack_value (&bp, node->lowered, 1);
535 bp_pack_value (&bp, in_other_partition, 1);
536 bp_pack_value (&bp, node->alias, 1);
537 bp_pack_value (&bp, node->transparent_alias, 1);
538 bp_pack_value (&bp, node->weakref, 1);
539 bp_pack_value (&bp, node->symver, 1);
540 bp_pack_value (&bp, node->frequency, 2);
541 bp_pack_value (&bp, node->only_called_at_startup, 1);
542 bp_pack_value (&bp, node->only_called_at_exit, 1);
543 bp_pack_value (&bp, node->tm_clone, 1);
544 bp_pack_value (&bp, node->calls_comdat_local, 1);
545 bp_pack_value (&bp, node->icf_merged, 1);
546 bp_pack_value (&bp, node->nonfreeing_fn, 1);
547 bp_pack_value (&bp, node->merged_comdat, 1);
548 bp_pack_value (&bp, node->merged_extern_inline, 1);
549 bp_pack_value (&bp, node->thunk, 1);
550 bp_pack_value (&bp, node->parallelized_function, 1);
551 bp_pack_value (&bp, node->declare_variant_alt, 1);
552 bp_pack_value (&bp, node->calls_declare_variant_alt, 1);
554 /* Stream thunk info always because we use it in
555 ipa_polymorphic_call_context::ipa_polymorphic_call_context
556 to properly interpret THIS pointers for thunks that has been converted
557 to Gimple. */
558 struct thunk_info *thunk = node->definition ? thunk_info::get (node) : NULL;
560 bp_pack_value (&bp, thunk != NULL, 1);
562 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
563 LDPR_NUM_KNOWN,
564 /* When doing incremental link, we will get new resolution
565 info next time we process the file. */
566 flag_incremental_link == INCREMENTAL_LINK_LTO
567 ? LDPR_UNKNOWN : node->resolution);
568 bp_pack_value (&bp, node->split_part, 1);
569 streamer_write_bitpack (&bp);
570 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
572 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
573 streamer_write_hwi_stream (ob->main_stream, node->unit_id);
574 if (DECL_STATIC_CONSTRUCTOR (node->decl))
575 streamer_write_hwi_stream (ob->main_stream, node->get_init_priority ());
576 if (DECL_STATIC_DESTRUCTOR (node->decl))
577 streamer_write_hwi_stream (ob->main_stream, node->get_fini_priority ());
579 if (thunk)
580 thunk_info::get (node)->stream_out (ob);
583 /* Output the varpool NODE to OB.
584 If NODE is not in SET, then NODE is a boundary. */
586 static void
587 lto_output_varpool_node (struct lto_simple_output_block *ob, varpool_node *node,
588 lto_symtab_encoder_t encoder)
590 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
591 bool encode_initializer_p
592 = (node->definition
593 && lto_symtab_encoder_encode_initializer_p (encoder, node));
594 struct bitpack_d bp;
595 int ref;
596 const char *comdat;
597 const char *section;
598 tree group;
600 gcc_assert (!encode_initializer_p || node->definition);
601 gcc_assert (boundary_p || encode_initializer_p);
603 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
604 LTO_symtab_variable);
605 streamer_write_hwi_stream (ob->main_stream, node->order);
606 lto_output_var_decl_ref (ob->decl_state, ob->main_stream, node->decl);
607 bp = bitpack_create (ob->main_stream);
608 bp_pack_value (&bp, node->externally_visible, 1);
609 bp_pack_value (&bp, node->no_reorder, 1);
610 bp_pack_value (&bp, node->force_output, 1);
611 bp_pack_value (&bp, node->forced_by_abi, 1);
612 bp_pack_value (&bp, node->unique_name, 1);
613 bp_pack_value (&bp,
614 node->body_removed
615 || (!encode_initializer_p && !node->alias && node->definition),
617 bp_pack_value (&bp, node->semantic_interposition, 1);
618 bp_pack_value (&bp, node->implicit_section, 1);
619 bp_pack_value (&bp, node->writeonly, 1);
620 bp_pack_value (&bp, node->definition && (encode_initializer_p || node->alias),
622 bp_pack_value (&bp, node->alias, 1);
623 bp_pack_value (&bp, node->transparent_alias, 1);
624 bp_pack_value (&bp, node->weakref, 1);
625 bp_pack_value (&bp, node->symver, 1);
626 bp_pack_value (&bp, node->analyzed && (!boundary_p || node->alias), 1);
627 gcc_assert (node->definition || !node->analyzed);
628 /* Constant pool initializers can be de-unified into individual ltrans units.
629 FIXME: Alternatively at -Os we may want to avoid generating for them the local
630 labels and share them across LTRANS partitions. */
631 if (node->get_partitioning_class () != SYMBOL_PARTITION)
633 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
634 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
636 else
638 bp_pack_value (&bp, node->definition
639 && referenced_from_other_partition_p (node, encoder), 1);
640 bp_pack_value (&bp, node->analyzed
641 && boundary_p && !DECL_EXTERNAL (node->decl), 1);
642 /* in_other_partition. */
644 bp_pack_value (&bp, node->tls_model, 3);
645 bp_pack_value (&bp, node->used_by_single_function, 1);
646 bp_pack_value (&bp, node->dynamically_initialized, 1);
647 streamer_write_bitpack (&bp);
649 group = node->get_comdat_group ();
650 if (group)
651 comdat = IDENTIFIER_POINTER (group);
652 else
653 comdat = "";
654 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
656 if (group)
658 if (node->same_comdat_group)
660 ref = LCC_NOT_FOUND;
661 for (struct symtab_node *n = node->same_comdat_group;
662 ref == LCC_NOT_FOUND && n != node; n = n->same_comdat_group)
663 ref = lto_symtab_encoder_lookup (encoder, n);
665 else
666 ref = LCC_NOT_FOUND;
667 streamer_write_hwi_stream (ob->main_stream, ref);
670 section = node->get_section ();
671 if (!section)
672 section = "";
673 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
675 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
676 LDPR_NUM_KNOWN, node->resolution);
679 /* Output the varpool NODE to OB.
680 If NODE is not in SET, then NODE is a boundary. */
682 static void
683 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
684 lto_symtab_encoder_t encoder)
686 struct bitpack_d bp;
687 int nref;
688 int uid = !ref->stmt ? ref->lto_stmt_uid : gimple_uid (ref->stmt) + 1;
689 struct cgraph_node *node;
691 bp = bitpack_create (ob->main_stream);
692 bp_pack_value (&bp, ref->use, 3);
693 bp_pack_value (&bp, ref->speculative, 1);
694 streamer_write_bitpack (&bp);
695 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
696 gcc_assert (nref != LCC_NOT_FOUND);
697 streamer_write_hwi_stream (ob->main_stream, nref);
699 node = dyn_cast <cgraph_node *> (ref->referring);
700 if (node)
702 if (ref->stmt)
703 uid = gimple_uid (ref->stmt) + 1;
704 streamer_write_hwi_stream (ob->main_stream, uid);
705 bp_pack_value (&bp, ref->speculative_id, 16);
706 streamer_write_bitpack (&bp);
710 /* Stream out profile_summary to OB. */
712 static void
713 output_profile_summary (struct lto_simple_output_block *ob)
715 if (profile_info)
717 /* We do not output num and run_max, they are not used by
718 GCC profile feedback and they are difficult to merge from multiple
719 units. */
720 unsigned runs = (profile_info->runs);
721 streamer_write_uhwi_stream (ob->main_stream, runs);
723 /* IPA-profile computes hot bb threshold based on cumulated
724 whole program profile. We need to stream it down to ltrans. */
725 if (flag_wpa)
726 streamer_write_gcov_count_stream (ob->main_stream,
727 get_hot_bb_threshold ());
729 else
730 streamer_write_uhwi_stream (ob->main_stream, 0);
733 /* Output all callees or indirect outgoing edges. EDGE must be the first such
734 edge. */
736 static void
737 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
738 struct lto_simple_output_block *ob,
739 lto_symtab_encoder_t encoder)
741 if (!edge)
742 return;
744 /* Output edges in backward direction, so the reconstructed callgraph match
745 and it is easy to associate call sites in the IPA pass summaries. */
746 while (edge->next_callee)
747 edge = edge->next_callee;
748 for (; edge; edge = edge->prev_callee)
749 lto_output_edge (ob, edge, encoder);
752 /* Output the part of the cgraph in SET. */
754 static void
755 output_refs (lto_symtab_encoder_t encoder)
757 struct lto_simple_output_block *ob;
758 int count;
759 struct ipa_ref *ref;
761 ob = lto_create_simple_output_block (LTO_section_refs);
763 for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
765 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
767 /* IPA_REF_ALIAS references are always preserved
768 in the boundary. Alias node can't have other references and
769 can be always handled as if it's not in the boundary. */
770 if (!node->alias && !lto_symtab_encoder_in_partition_p (encoder, node))
771 continue;
773 count = node->ref_list.nreferences ();
774 if (count)
776 streamer_write_gcov_count_stream (ob->main_stream, count);
777 streamer_write_uhwi_stream (ob->main_stream,
778 lto_symtab_encoder_lookup (encoder, node));
779 for (int i = 0; node->iterate_reference (i, ref); i++)
780 lto_output_ref (ob, ref, encoder);
782 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
783 if (cnode->declare_variant_alt)
784 omp_lto_output_declare_variant_alt (ob, cnode, encoder);
787 streamer_write_uhwi_stream (ob->main_stream, 0);
789 lto_destroy_simple_output_block (ob);
792 /* Add NODE into encoder as well as nodes it is cloned from.
793 Do it in a way so clones appear first. */
795 static void
796 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
797 bool include_body)
799 if (node->clone_of)
800 add_node_to (encoder, node->clone_of, include_body);
801 if (include_body)
802 lto_set_symtab_encoder_encode_body (encoder, node);
803 lto_symtab_encoder_encode (encoder, node);
806 /* Add all references in NODE to encoders. */
808 static void
809 create_references (lto_symtab_encoder_t encoder, symtab_node *node)
811 int i;
812 struct ipa_ref *ref = NULL;
813 for (i = 0; node->iterate_reference (i, ref); i++)
814 if (is_a <cgraph_node *> (ref->referred))
815 add_node_to (encoder, dyn_cast <cgraph_node *> (ref->referred), false);
816 else
817 lto_symtab_encoder_encode (encoder, ref->referred);
820 /* Select what needs to be streamed out. In regular lto mode stream everything.
821 In offload lto mode stream only nodes marked as offloadable. */
822 void
823 select_what_to_stream (void)
825 struct symtab_node *snode;
826 FOR_EACH_SYMBOL (snode)
827 snode->need_lto_streaming = !lto_stream_offload_p || snode->offloadable;
830 /* Find all symbols we want to stream into given partition and insert them
831 to encoders.
833 The function actually replaces IN_ENCODER by new one. The reason is that
834 streaming code needs clone's origin to be streamed before clone. This
835 means that we need to insert the nodes in specific order. This order is
836 ignored by the partitioning logic earlier. */
838 lto_symtab_encoder_t
839 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
841 struct cgraph_edge *edge;
842 int i;
843 lto_symtab_encoder_t encoder;
844 lto_symtab_encoder_iterator lsei;
845 hash_set<void *> reachable_call_targets;
847 encoder = lto_symtab_encoder_new (false);
849 /* Go over all entries in the IN_ENCODER and duplicate them to
850 ENCODER. At the same time insert masters of clones so
851 every master appears before clone. */
852 for (lsei = lsei_start_function_in_partition (in_encoder);
853 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
855 struct cgraph_node *node = lsei_cgraph_node (lsei);
856 if (!node->need_lto_streaming)
857 continue;
858 add_node_to (encoder, node, true);
859 lto_set_symtab_encoder_in_partition (encoder, node);
860 create_references (encoder, node);
862 for (lsei = lsei_start_variable_in_partition (in_encoder);
863 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
865 varpool_node *vnode = lsei_varpool_node (lsei);
867 if (!vnode->need_lto_streaming)
868 continue;
869 lto_set_symtab_encoder_in_partition (encoder, vnode);
870 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
871 create_references (encoder, vnode);
873 /* Pickle in also the initializer of all referenced readonly variables
874 to help folding. Constant pool variables are not shared, so we must
875 pickle those too. */
876 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
878 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
879 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
881 if (!lto_symtab_encoder_encode_initializer_p (encoder,
882 vnode)
883 && (((vnode->ctor_useable_for_folding_p ()
884 && (!DECL_VIRTUAL_P (vnode->decl)
885 || !flag_wpa
886 || flag_ltrans_devirtualize)))))
888 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
889 create_references (encoder, vnode);
894 /* Go over all the nodes again to include callees that are not in
895 SET. */
896 for (lsei = lsei_start_function_in_partition (encoder);
897 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
899 struct cgraph_node *node = lsei_cgraph_node (lsei);
900 for (edge = node->callees; edge; edge = edge->next_callee)
902 struct cgraph_node *callee = edge->callee;
903 if (!lto_symtab_encoder_in_partition_p (encoder, callee))
905 /* We should have moved all the inlines. */
906 gcc_assert (!callee->inlined_to);
907 add_node_to (encoder, callee, false);
910 /* Add all possible targets for late devirtualization. */
911 if (flag_ltrans_devirtualize || !flag_wpa)
912 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
913 if (edge->indirect_info->polymorphic)
915 unsigned int i;
916 void *cache_token;
917 bool final;
918 vec <cgraph_node *>targets
919 = possible_polymorphic_call_targets
920 (edge, &final, &cache_token);
921 if (!reachable_call_targets.add (cache_token))
923 for (i = 0; i < targets.length (); i++)
925 struct cgraph_node *callee = targets[i];
927 /* Adding an external declarations into the unit serves
928 no purpose and just increases its boundary. */
929 if (callee->definition
930 && !lto_symtab_encoder_in_partition_p
931 (encoder, callee))
933 gcc_assert (!callee->inlined_to);
934 add_node_to (encoder, callee, false);
940 /* Be sure to also insert alias targert and thunk callees. These needs
941 to stay to aid local calling conventions. */
942 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
944 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
945 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
947 if (node->alias && node->analyzed)
948 create_references (encoder, node);
949 if (cnode
950 && cnode->thunk && !cnode->inlined_to)
951 add_node_to (encoder, cnode->callees->callee, false);
952 while (node->transparent_alias && node->analyzed)
954 node = node->get_alias_target ();
955 if (is_a <cgraph_node *> (node))
956 add_node_to (encoder, dyn_cast <cgraph_node *> (node),
957 false);
958 else
959 lto_symtab_encoder_encode (encoder, node);
962 lto_symtab_encoder_delete (in_encoder);
963 return encoder;
966 /* Output the part of the symtab in SET and VSET. */
968 void
969 output_symtab (void)
971 struct cgraph_node *node;
972 struct lto_simple_output_block *ob;
973 int i, n_nodes;
974 lto_symtab_encoder_t encoder;
976 if (flag_wpa)
977 output_cgraph_opt_summary ();
979 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
981 output_profile_summary (ob);
983 /* An encoder for cgraph nodes should have been created by
984 ipa_write_summaries_1. */
985 gcc_assert (ob->decl_state->symtab_node_encoder);
986 encoder = ob->decl_state->symtab_node_encoder;
988 /* Write out the nodes. We must first output a node and then its clones,
989 otherwise at a time reading back the node there would be nothing to clone
990 from. */
991 n_nodes = lto_symtab_encoder_size (encoder);
992 for (i = 0; i < n_nodes; i++)
994 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
995 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
996 lto_output_node (ob, cnode, encoder);
997 else
998 lto_output_varpool_node (ob, dyn_cast<varpool_node *> (node), encoder);
1001 /* Go over the nodes in SET again to write edges. */
1002 for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
1004 node = dyn_cast <cgraph_node *> (lto_symtab_encoder_deref (encoder, i));
1005 if (node
1006 && ((node->thunk && !node->inlined_to)
1007 || lto_symtab_encoder_in_partition_p (encoder, node)))
1009 output_outgoing_cgraph_edges (node->callees, ob, encoder);
1010 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
1014 streamer_write_uhwi_stream (ob->main_stream, 0);
1016 lto_destroy_simple_output_block (ob);
1018 /* Emit toplevel asms.
1019 When doing WPA we must output every asm just once. Since we do not partition asm
1020 nodes at all, output them to first output. This is kind of hack, but should work
1021 well. */
1022 if (!asm_nodes_output)
1024 asm_nodes_output = true;
1025 lto_output_toplevel_asms ();
1028 output_refs (encoder);
1031 /* Return identifier encoded in IB as a plain string. */
1033 static tree
1034 read_identifier (class lto_input_block *ib)
1036 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1037 tree id;
1039 if (ib->data[ib->p + len])
1040 lto_section_overrun (ib);
1041 if (!len)
1043 ib->p++;
1044 return NULL;
1046 id = get_identifier (ib->data + ib->p);
1047 ib->p += len + 1;
1048 return id;
1051 /* Return string encoded in IB, NULL if string is empty. */
1053 static const char *
1054 read_string (class lto_input_block *ib)
1056 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1057 const char *str;
1059 if (ib->data[ib->p + len])
1060 lto_section_overrun (ib);
1061 if (!len)
1063 ib->p++;
1064 return NULL;
1066 str = ib->data + ib->p;
1067 ib->p += len + 1;
1068 return str;
1071 /* Output function/variable tables that will allow libgomp to look up offload
1072 target code.
1073 OFFLOAD_FUNCS is filled in expand_omp_target, OFFLOAD_VARS is filled in
1074 varpool_node::get_create. In WHOPR (partitioned) mode during the WPA stage
1075 both OFFLOAD_FUNCS and OFFLOAD_VARS are filled by input_offload_tables. */
1077 void
1078 output_offload_tables (void)
1080 bool output_requires = (flag_openmp
1081 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0);
1082 if (vec_safe_is_empty (offload_funcs) && vec_safe_is_empty (offload_vars)
1083 && !output_requires)
1084 return;
1086 struct lto_simple_output_block *ob
1087 = lto_create_simple_output_block (LTO_section_offload_table);
1089 for (unsigned i = 0; i < vec_safe_length (offload_funcs); i++)
1091 symtab_node *node = symtab_node::get ((*offload_funcs)[i]);
1092 if (!node)
1093 continue;
1094 node->force_output = true;
1095 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1096 LTO_symtab_last_tag, LTO_symtab_unavail_node);
1097 lto_output_fn_decl_ref (ob->decl_state, ob->main_stream,
1098 (*offload_funcs)[i]);
1101 for (unsigned i = 0; i < vec_safe_length (offload_vars); i++)
1103 symtab_node *node = symtab_node::get ((*offload_vars)[i]);
1104 if (!node)
1105 continue;
1106 node->force_output = true;
1107 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1108 LTO_symtab_last_tag, LTO_symtab_variable);
1109 lto_output_var_decl_ref (ob->decl_state, ob->main_stream,
1110 (*offload_vars)[i]);
1113 if (output_requires)
1115 HOST_WIDE_INT val = ((HOST_WIDE_INT) omp_requires_mask
1116 & (OMP_REQUIRES_UNIFIED_ADDRESS
1117 | OMP_REQUIRES_UNIFIED_SHARED_MEMORY
1118 | OMP_REQUIRES_REVERSE_OFFLOAD
1119 | OMP_REQUIRES_TARGET_USED));
1120 /* (Mis)use LTO_symtab_edge for this variable. */
1121 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1122 LTO_symtab_last_tag, LTO_symtab_edge);
1123 streamer_write_hwi_stream (ob->main_stream, val);
1126 streamer_write_uhwi_stream (ob->main_stream, 0);
1127 lto_destroy_simple_output_block (ob);
1129 /* In WHOPR mode during the WPA stage the joint offload tables need to be
1130 streamed to one partition only. That's why we free offload_funcs and
1131 offload_vars after the first call of output_offload_tables. */
1132 if (flag_wpa)
1134 vec_free (offload_funcs);
1135 vec_free (offload_vars);
1139 /* Verify the partitioning of NODE. */
1141 static inline void
1142 verify_node_partition (symtab_node *node)
1144 if (flag_ltrans)
1145 return;
1147 #ifdef ACCEL_COMPILER
1148 if (node->in_other_partition)
1150 if (TREE_CODE (node->decl) == FUNCTION_DECL)
1152 if (lookup_attribute ("omp target device_ancestor_host",
1153 DECL_ATTRIBUTES (node->decl)) != NULL)
1154 return;
1155 error_at (DECL_SOURCE_LOCATION (node->decl),
1156 "function %qs has been referenced in offloaded code but"
1157 " hasn%'t been marked to be included in the offloaded code",
1158 node->name ());
1160 else if (VAR_P (node->decl))
1161 error_at (DECL_SOURCE_LOCATION (node->decl),
1162 "variable %qs has been referenced in offloaded code but"
1163 " hasn%'t been marked to be included in the offloaded code",
1164 node->name ());
1165 else
1166 gcc_unreachable ();
1168 #else
1169 gcc_assert (!node->in_other_partition
1170 && !node->used_from_other_partition);
1171 #endif
1174 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
1175 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
1176 NODE or to replace the values in it, for instance because the first
1177 time we saw it, the function body was not available but now it
1178 is. BP is a bitpack with all the bitflags for NODE read from the
1179 stream. Initialize HAS_THUNK_INFO to indicate if thunk info should
1180 be streamed in. */
1182 static void
1183 input_overwrite_node (struct lto_file_decl_data *file_data,
1184 struct cgraph_node *node,
1185 enum LTO_symtab_tags tag,
1186 struct bitpack_d *bp, bool *has_thunk_info)
1188 node->aux = (void *) tag;
1189 node->lto_file_data = file_data;
1191 node->local = bp_unpack_value (bp, 1);
1192 node->externally_visible = bp_unpack_value (bp, 1);
1193 node->no_reorder = bp_unpack_value (bp, 1);
1194 node->definition = bp_unpack_value (bp, 1);
1195 node->versionable = bp_unpack_value (bp, 1);
1196 node->can_change_signature = bp_unpack_value (bp, 1);
1197 node->redefined_extern_inline = bp_unpack_value (bp, 1);
1198 node->force_output = bp_unpack_value (bp, 1);
1199 node->forced_by_abi = bp_unpack_value (bp, 1);
1200 node->unique_name = bp_unpack_value (bp, 1);
1201 node->body_removed = bp_unpack_value (bp, 1);
1202 node->semantic_interposition = bp_unpack_value (bp, 1);
1203 node->implicit_section = bp_unpack_value (bp, 1);
1204 node->address_taken = bp_unpack_value (bp, 1);
1205 node->used_from_other_partition = bp_unpack_value (bp, 1);
1206 node->lowered = bp_unpack_value (bp, 1);
1207 node->analyzed = tag == LTO_symtab_analyzed_node;
1208 node->in_other_partition = bp_unpack_value (bp, 1);
1209 if (node->in_other_partition
1210 /* Avoid updating decl when we are seeing just inline clone.
1211 When inlining function that has functions already inlined into it,
1212 we produce clones of inline clones.
1214 WPA partitioning might put each clone into different unit and
1215 we might end up streaming inline clone from other partition
1216 to support clone we are interested in. */
1217 && (!node->clone_of
1218 || node->clone_of->decl != node->decl))
1220 DECL_EXTERNAL (node->decl) = 1;
1221 TREE_STATIC (node->decl) = 0;
1223 node->alias = bp_unpack_value (bp, 1);
1224 node->transparent_alias = bp_unpack_value (bp, 1);
1225 node->weakref = bp_unpack_value (bp, 1);
1226 node->symver = bp_unpack_value (bp, 1);
1227 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
1228 node->only_called_at_startup = bp_unpack_value (bp, 1);
1229 node->only_called_at_exit = bp_unpack_value (bp, 1);
1230 node->tm_clone = bp_unpack_value (bp, 1);
1231 node->calls_comdat_local = bp_unpack_value (bp, 1);
1232 node->icf_merged = bp_unpack_value (bp, 1);
1233 node->nonfreeing_fn = bp_unpack_value (bp, 1);
1234 node->merged_comdat = bp_unpack_value (bp, 1);
1235 node->merged_extern_inline = bp_unpack_value (bp, 1);
1236 node->thunk = bp_unpack_value (bp, 1);
1237 node->parallelized_function = bp_unpack_value (bp, 1);
1238 node->declare_variant_alt = bp_unpack_value (bp, 1);
1239 node->calls_declare_variant_alt = bp_unpack_value (bp, 1);
1240 *has_thunk_info = bp_unpack_value (bp, 1);
1241 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
1242 LDPR_NUM_KNOWN);
1243 node->split_part = bp_unpack_value (bp, 1);
1244 verify_node_partition (node);
1247 /* Return string alias is alias of. */
1249 static tree
1250 get_alias_symbol (tree decl)
1252 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1253 return get_identifier (TREE_STRING_POINTER
1254 (TREE_VALUE (TREE_VALUE (alias))));
1257 /* Read a node from input_block IB. TAG is the node's tag just read.
1258 Return the node read or overwriten. */
1260 static struct cgraph_node *
1261 input_node (struct lto_file_decl_data *file_data,
1262 class lto_input_block *ib,
1263 enum LTO_symtab_tags tag,
1264 vec<symtab_node *> nodes)
1266 gcc::pass_manager *passes = g->get_passes ();
1267 tree fn_decl;
1268 struct cgraph_node *node;
1269 struct bitpack_d bp;
1270 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1271 int clone_ref;
1272 int order;
1273 int i, count;
1274 tree group;
1275 const char *section;
1276 order = streamer_read_hwi (ib) + file_data->order_base;
1277 clone_ref = streamer_read_hwi (ib);
1278 bool has_thunk_info;
1280 fn_decl = lto_input_fn_decl_ref (ib, file_data);
1282 if (clone_ref != LCC_NOT_FOUND)
1284 node = dyn_cast<cgraph_node *> (nodes[clone_ref])->create_clone (fn_decl,
1285 profile_count::uninitialized (), false,
1286 vNULL, false, NULL, NULL);
1288 else
1290 /* Declaration of functions can be already merged with a declaration
1291 from other input file. We keep cgraph unmerged until after streaming
1292 of ipa passes is done. Alays forcingly create a fresh node. */
1293 node = symtab->create_empty ();
1294 node->decl = fn_decl;
1295 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (fn_decl)))
1296 node->ifunc_resolver = 1;
1297 node->register_symbol ();
1300 node->order = order;
1301 if (order >= symtab->order)
1302 symtab->order = order + 1;
1304 node->count = profile_count::stream_in (ib);
1305 node->count_materialization_scale = streamer_read_hwi (ib);
1307 count = streamer_read_hwi (ib);
1308 node->ipa_transforms_to_apply = vNULL;
1309 for (i = 0; i < count; i++)
1311 opt_pass *pass;
1312 int pid = streamer_read_hwi (ib);
1314 gcc_assert (pid < passes->passes_by_id_size);
1315 pass = passes->passes_by_id[pid];
1316 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *) pass);
1319 if (tag == LTO_symtab_analyzed_node)
1320 ref = streamer_read_hwi (ib);
1322 group = read_identifier (ib);
1323 if (group)
1324 ref2 = streamer_read_hwi (ib);
1326 /* Make sure that we have not read this node before. Nodes that
1327 have already been read will have their tag stored in the 'aux'
1328 field. Since built-in functions can be referenced in multiple
1329 functions, they are expected to be read more than once. */
1330 if (node->aux && !fndecl_built_in_p (node->decl))
1331 internal_error ("bytecode stream: found multiple instances of cgraph "
1332 "node with uid %d", node->get_uid ());
1334 node->tp_first_run = streamer_read_uhwi (ib);
1336 bp = streamer_read_bitpack (ib);
1338 input_overwrite_node (file_data, node, tag, &bp, &has_thunk_info);
1340 /* Store a reference for now, and fix up later to be a pointer. */
1341 node->inlined_to = (cgraph_node *) (intptr_t) ref;
1343 if (group)
1345 node->set_comdat_group (group);
1346 /* Store a reference for now, and fix up later to be a pointer. */
1347 node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
1349 else
1350 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1351 section = read_string (ib);
1352 if (section)
1353 node->set_section_for_node (section);
1355 if (node->alias && !node->analyzed && node->weakref)
1356 node->alias_target = get_alias_symbol (node->decl);
1357 node->profile_id = streamer_read_hwi (ib);
1358 node->unit_id = streamer_read_hwi (ib) + file_data->unit_base;
1359 if (symtab->max_unit < node->unit_id)
1360 symtab->max_unit = node->unit_id;
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 (has_thunk_info)
1367 thunk_info::get_create (node)->stream_in (ib);
1369 return node;
1372 /* Read a node from input_block IB. TAG is the node's tag just read.
1373 Return the node read or overwriten. */
1375 static varpool_node *
1376 input_varpool_node (struct lto_file_decl_data *file_data,
1377 class lto_input_block *ib)
1379 tree var_decl;
1380 varpool_node *node;
1381 struct bitpack_d bp;
1382 int ref = LCC_NOT_FOUND;
1383 int order;
1384 tree group;
1385 const char *section;
1387 order = streamer_read_hwi (ib) + file_data->order_base;
1388 var_decl = lto_input_var_decl_ref (ib, file_data);
1390 /* Declaration of functions can be already merged with a declaration
1391 from other input file. We keep cgraph unmerged until after streaming
1392 of ipa passes is done. Alays forcingly create a fresh node. */
1393 node = varpool_node::create_empty ();
1394 node->decl = var_decl;
1395 node->register_symbol ();
1397 node->order = order;
1398 if (order >= symtab->order)
1399 symtab->order = order + 1;
1400 node->lto_file_data = file_data;
1402 bp = streamer_read_bitpack (ib);
1403 node->externally_visible = bp_unpack_value (&bp, 1);
1404 node->no_reorder = bp_unpack_value (&bp, 1);
1405 node->force_output = bp_unpack_value (&bp, 1);
1406 node->forced_by_abi = bp_unpack_value (&bp, 1);
1407 node->unique_name = bp_unpack_value (&bp, 1);
1408 node->body_removed = bp_unpack_value (&bp, 1);
1409 node->semantic_interposition = bp_unpack_value (&bp, 1);
1410 node->implicit_section = bp_unpack_value (&bp, 1);
1411 node->writeonly = bp_unpack_value (&bp, 1);
1412 node->definition = bp_unpack_value (&bp, 1);
1413 node->alias = bp_unpack_value (&bp, 1);
1414 node->transparent_alias = bp_unpack_value (&bp, 1);
1415 node->weakref = bp_unpack_value (&bp, 1);
1416 node->symver = bp_unpack_value (&bp, 1);
1417 node->analyzed = bp_unpack_value (&bp, 1);
1418 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1419 node->in_other_partition = bp_unpack_value (&bp, 1);
1420 if (node->in_other_partition)
1422 DECL_EXTERNAL (node->decl) = 1;
1423 TREE_STATIC (node->decl) = 0;
1425 if (node->alias && !node->analyzed && node->weakref)
1426 node->alias_target = get_alias_symbol (node->decl);
1427 node->tls_model = (enum tls_model)bp_unpack_value (&bp, 3);
1428 node->used_by_single_function = (enum tls_model)bp_unpack_value (&bp, 1);
1429 node->dynamically_initialized = bp_unpack_value (&bp, 1);
1430 group = read_identifier (ib);
1431 if (group)
1433 node->set_comdat_group (group);
1434 ref = streamer_read_hwi (ib);
1435 /* Store a reference for now, and fix up later to be a pointer. */
1436 node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1438 else
1439 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1440 section = read_string (ib);
1441 if (section)
1442 node->set_section_for_node (section);
1443 node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1444 LDPR_NUM_KNOWN);
1445 verify_node_partition (node);
1446 return node;
1449 /* Read a node from input_block IB. TAG is the node's tag just read.
1450 Return the node read or overwriten. */
1452 static void
1453 input_ref (class lto_input_block *ib,
1454 symtab_node *referring_node,
1455 vec<symtab_node *> nodes)
1457 symtab_node *node = NULL;
1458 struct bitpack_d bp;
1459 enum ipa_ref_use use;
1460 bool speculative;
1461 struct ipa_ref *ref;
1463 bp = streamer_read_bitpack (ib);
1464 use = (enum ipa_ref_use) bp_unpack_value (&bp, 3);
1465 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1466 node = nodes[streamer_read_hwi (ib)];
1467 ref = referring_node->create_reference (node, use);
1468 ref->speculative = speculative;
1469 if (is_a <cgraph_node *> (referring_node))
1471 ref->lto_stmt_uid = streamer_read_hwi (ib);
1472 bp = streamer_read_bitpack (ib);
1473 ref->speculative_id = bp_unpack_value (&bp, 16);
1477 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1478 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1479 edge being read is indirect (in the sense that it has
1480 indirect_unknown_callee set). */
1482 static void
1483 input_edge (class lto_input_block *ib, vec<symtab_node *> nodes,
1484 bool indirect)
1486 struct cgraph_node *caller, *callee;
1487 struct cgraph_edge *edge;
1488 unsigned int stmt_id, speculative_id;
1489 profile_count count;
1490 cgraph_inline_failed_t inline_failed;
1491 struct bitpack_d bp;
1492 int ecf_flags = 0;
1494 caller = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1495 if (caller == NULL || caller->decl == NULL_TREE)
1496 internal_error ("bytecode stream: no caller found while reading edge");
1498 if (!indirect)
1500 callee = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1501 if (callee == NULL || callee->decl == NULL_TREE)
1502 internal_error ("bytecode stream: no callee found while reading edge");
1504 else
1505 callee = NULL;
1507 count = profile_count::stream_in (ib);
1509 bp = streamer_read_bitpack (ib);
1510 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_t, CIF_N_REASONS);
1511 stmt_id = bp_unpack_var_len_unsigned (&bp);
1512 speculative_id = bp_unpack_value (&bp, 16);
1514 if (indirect)
1515 edge = caller->create_indirect_edge (NULL, 0, count);
1516 else
1517 edge = caller->create_edge (callee, NULL, count);
1519 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1520 edge->speculative = bp_unpack_value (&bp, 1);
1521 edge->lto_stmt_uid = stmt_id;
1522 edge->speculative_id = speculative_id;
1523 edge->inline_failed = inline_failed;
1524 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1525 edge->can_throw_external = bp_unpack_value (&bp, 1);
1526 edge->in_polymorphic_cdtor = bp_unpack_value (&bp, 1);
1527 if (indirect)
1529 if (bp_unpack_value (&bp, 1))
1530 ecf_flags |= ECF_CONST;
1531 if (bp_unpack_value (&bp, 1))
1532 ecf_flags |= ECF_PURE;
1533 if (bp_unpack_value (&bp, 1))
1534 ecf_flags |= ECF_NORETURN;
1535 if (bp_unpack_value (&bp, 1))
1536 ecf_flags |= ECF_MALLOC;
1537 if (bp_unpack_value (&bp, 1))
1538 ecf_flags |= ECF_NOTHROW;
1539 if (bp_unpack_value (&bp, 1))
1540 ecf_flags |= ECF_RETURNS_TWICE;
1541 edge->indirect_info->ecf_flags = ecf_flags;
1543 edge->indirect_info->num_speculative_call_targets
1544 = bp_unpack_value (&bp, 16);
1549 /* Read a cgraph from IB using the info in FILE_DATA. */
1551 static vec<symtab_node *>
1552 input_cgraph_1 (struct lto_file_decl_data *file_data,
1553 class lto_input_block *ib)
1555 enum LTO_symtab_tags tag;
1556 vec<symtab_node *> nodes = vNULL;
1557 symtab_node *node;
1558 unsigned i;
1560 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1561 file_data->order_base = symtab->order;
1562 file_data->unit_base = symtab->max_unit + 1;
1563 while (tag)
1565 if (tag == LTO_symtab_edge)
1566 input_edge (ib, nodes, false);
1567 else if (tag == LTO_symtab_indirect_edge)
1568 input_edge (ib, nodes, true);
1569 else if (tag == LTO_symtab_variable)
1571 node = input_varpool_node (file_data, ib);
1572 nodes.safe_push (node);
1573 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1575 else
1577 node = input_node (file_data, ib, tag, nodes);
1578 if (node == NULL || node->decl == NULL_TREE)
1579 internal_error ("bytecode stream: found empty cgraph node");
1580 nodes.safe_push (node);
1581 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1584 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1587 lto_input_toplevel_asms (file_data, file_data->order_base);
1589 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1590 if (flag_checking)
1592 FOR_EACH_VEC_ELT (nodes, i, node)
1593 gcc_assert (node->aux || !is_a <cgraph_node *> (node));
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->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)->inlined_to
1610 = dyn_cast<cgraph_node *> (nodes[ref]);
1611 else
1612 cnode->inlined_to = NULL;
1615 ref = (int) (intptr_t) node->same_comdat_group;
1617 /* Fixup same_comdat_group from reference to pointer. */
1618 if (ref != LCC_NOT_FOUND)
1619 node->same_comdat_group = nodes[ref];
1620 else
1621 node->same_comdat_group = NULL;
1623 FOR_EACH_VEC_ELT (nodes, i, node)
1624 node->aux = is_a <cgraph_node *> (node) ? (void *)1 : NULL;
1625 return nodes;
1628 /* Input ipa_refs. */
1630 static void
1631 input_refs (class lto_input_block *ib,
1632 vec<symtab_node *> nodes)
1634 int count;
1635 int idx;
1636 while (true)
1638 symtab_node *node;
1639 count = streamer_read_uhwi (ib);
1640 if (!count)
1641 break;
1642 idx = streamer_read_uhwi (ib);
1643 node = nodes[idx];
1644 while (count)
1646 input_ref (ib, node, nodes);
1647 count--;
1649 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1650 if (cnode->declare_variant_alt)
1651 omp_lto_input_declare_variant_alt (ib, cnode, nodes);
1655 /* Input profile_info from IB. */
1656 static void
1657 input_profile_summary (class lto_input_block *ib,
1658 struct lto_file_decl_data *file_data)
1660 unsigned int runs = streamer_read_uhwi (ib);
1661 if (runs)
1663 file_data->profile_info.runs = runs;
1665 /* IPA-profile computes hot bb threshold based on cumulated
1666 whole program profile. We need to stream it down to ltrans. */
1667 if (flag_ltrans)
1668 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1673 /* Rescale profile summaries to the same number of runs in the whole unit. */
1675 static void
1676 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1678 struct lto_file_decl_data *file_data;
1679 unsigned int j;
1680 gcov_unsigned_t max_runs = 0;
1681 struct cgraph_node *node;
1682 struct cgraph_edge *edge;
1684 /* Find unit with maximal number of runs. If we ever get serious about
1685 roundoff errors, we might also consider computing smallest common
1686 multiply. */
1687 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1688 if (max_runs < file_data->profile_info.runs)
1689 max_runs = file_data->profile_info.runs;
1691 if (!max_runs)
1692 return;
1694 /* Simple overflow check. We probably don't need to support that many train
1695 runs. Such a large value probably imply data corruption anyway. */
1696 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1698 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1699 INT_MAX / REG_BR_PROB_BASE);
1700 return;
1703 profile_info = XCNEW (gcov_summary);
1704 profile_info->runs = max_runs;
1706 /* If merging already happent at WPA time, we are done. */
1707 if (flag_ltrans)
1708 return;
1710 /* Now compute count_materialization_scale of each node.
1711 During LTRANS we already have values of count_materialization_scale
1712 computed, so just update them. */
1713 FOR_EACH_FUNCTION (node)
1714 if (node->lto_file_data
1715 && node->lto_file_data->profile_info.runs)
1717 int scale;
1719 scale = RDIV (node->count_materialization_scale * max_runs,
1720 node->lto_file_data->profile_info.runs);
1721 node->count_materialization_scale = scale;
1722 if (scale < 0)
1723 fatal_error (input_location, "Profile information in %s corrupted",
1724 file_data->file_name);
1726 if (scale == REG_BR_PROB_BASE)
1727 continue;
1728 for (edge = node->callees; edge; edge = edge->next_callee)
1729 if (edge->count.ipa ().nonzero_p ())
1730 edge->count = edge->count.apply_scale (scale, REG_BR_PROB_BASE);
1731 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1732 if (edge->count.ipa ().nonzero_p ())
1733 edge->count = edge->count.apply_scale (scale, REG_BR_PROB_BASE);
1734 if (node->count.ipa ().nonzero_p ())
1735 node->count = node->count.apply_scale (scale, REG_BR_PROB_BASE);
1739 /* Input and merge the symtab from each of the .o files passed to
1740 lto1. */
1742 void
1743 input_symtab (void)
1745 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1746 struct lto_file_decl_data *file_data;
1747 unsigned int j = 0;
1748 struct cgraph_node *node;
1750 while ((file_data = file_data_vec[j++]))
1752 const char *data;
1753 size_t len;
1754 class lto_input_block *ib;
1755 vec<symtab_node *> nodes;
1757 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1758 &data, &len);
1759 if (!ib)
1760 fatal_error (input_location,
1761 "cannot find LTO cgraph in %s", file_data->file_name);
1762 input_profile_summary (ib, file_data);
1763 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1764 nodes = input_cgraph_1 (file_data, ib);
1765 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1766 ib, data, len);
1768 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1769 &data, &len);
1770 if (!ib)
1771 fatal_error (input_location, "cannot find LTO section refs in %s",
1772 file_data->file_name);
1773 input_refs (ib, nodes);
1774 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1775 ib, data, len);
1776 if (flag_ltrans)
1777 input_cgraph_opt_summary (nodes);
1778 nodes.release ();
1781 merge_profile_summaries (file_data_vec);
1783 /* Clear out the aux field that was used to store enough state to
1784 tell which nodes should be overwritten. */
1785 FOR_EACH_FUNCTION (node)
1787 /* Some nodes may have been created by cgraph_node. This
1788 happens when the callgraph contains nested functions. If the
1789 node for the parent function was never emitted to the gimple
1790 file, cgraph_node will create a node for it when setting the
1791 context of the nested function. */
1792 if (node->lto_file_data)
1793 node->aux = NULL;
1797 static void
1798 omp_requires_to_name (char *buf, size_t size, HOST_WIDE_INT requires_mask)
1800 char *end = buf + size, *p = buf;
1801 if (requires_mask & GOMP_REQUIRES_UNIFIED_ADDRESS)
1802 p += snprintf (p, end - p, "unified_address");
1803 if (requires_mask & GOMP_REQUIRES_UNIFIED_SHARED_MEMORY)
1804 p += snprintf (p, end - p, "%sunified_shared_memory",
1805 (p == buf ? "" : ", "));
1806 if (requires_mask & GOMP_REQUIRES_REVERSE_OFFLOAD)
1807 p += snprintf (p, end - p, "%sreverse_offload",
1808 (p == buf ? "" : ", "));
1811 /* Input function/variable tables that will allow libgomp to look up offload
1812 target code, and store them into OFFLOAD_FUNCS and OFFLOAD_VARS. */
1814 void
1815 input_offload_tables (bool do_force_output)
1817 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1818 struct lto_file_decl_data *file_data;
1819 unsigned int j = 0;
1820 const char *requires_fn = NULL;
1821 tree requires_decl = NULL_TREE;
1823 omp_requires_mask = (omp_requires) 0;
1825 while ((file_data = file_data_vec[j++]))
1827 const char *data;
1828 size_t len;
1829 class lto_input_block *ib
1830 = lto_create_simple_input_block (file_data, LTO_section_offload_table,
1831 &data, &len);
1832 if (!ib)
1833 continue;
1835 tree tmp_decl = NULL_TREE;
1836 enum LTO_symtab_tags tag
1837 = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1838 while (tag)
1840 if (tag == LTO_symtab_unavail_node)
1842 tree fn_decl
1843 = lto_input_fn_decl_ref (ib, file_data);
1844 vec_safe_push (offload_funcs, fn_decl);
1846 /* Prevent IPA from removing fn_decl as unreachable, since there
1847 may be no refs from the parent function to child_fn in offload
1848 LTO mode. */
1849 if (do_force_output)
1850 cgraph_node::get (fn_decl)->mark_force_output ();
1851 tmp_decl = fn_decl;
1853 else if (tag == LTO_symtab_variable)
1855 tree var_decl
1856 = lto_input_var_decl_ref (ib, file_data);
1857 vec_safe_push (offload_vars, var_decl);
1859 /* Prevent IPA from removing var_decl as unused, since there
1860 may be no refs to var_decl in offload LTO mode. */
1861 if (do_force_output)
1862 varpool_node::get (var_decl)->force_output = 1;
1863 tmp_decl = var_decl;
1865 else if (tag == LTO_symtab_edge)
1867 static bool error_emitted = false;
1868 HOST_WIDE_INT val = streamer_read_hwi (ib);
1870 if (omp_requires_mask == 0)
1872 omp_requires_mask = (omp_requires) val;
1873 requires_decl = tmp_decl;
1874 requires_fn = file_data->file_name;
1876 else if (omp_requires_mask != val && !error_emitted)
1878 const char *fn1 = requires_fn;
1879 if (requires_decl != NULL_TREE)
1881 while (DECL_CONTEXT (requires_decl) != NULL_TREE
1882 && TREE_CODE (requires_decl) != TRANSLATION_UNIT_DECL)
1883 requires_decl = DECL_CONTEXT (requires_decl);
1884 if (requires_decl != NULL_TREE)
1885 fn1 = IDENTIFIER_POINTER (DECL_NAME (requires_decl));
1888 const char *fn2 = file_data->file_name;
1889 if (tmp_decl != NULL_TREE)
1891 while (DECL_CONTEXT (tmp_decl) != NULL_TREE
1892 && TREE_CODE (tmp_decl) != TRANSLATION_UNIT_DECL)
1893 tmp_decl = DECL_CONTEXT (tmp_decl);
1894 if (tmp_decl != NULL_TREE)
1895 fn2 = IDENTIFIER_POINTER (DECL_NAME (tmp_decl));
1897 if (fn1 == fn2)
1899 fn1 = requires_fn;
1900 fn2 = file_data->file_name;
1903 char buf1[sizeof ("unified_address, unified_shared_memory, "
1904 "reverse_offload")];
1905 char buf2[sizeof ("unified_address, unified_shared_memory, "
1906 "reverse_offload")];
1907 omp_requires_to_name (buf2, sizeof (buf2),
1908 val != OMP_REQUIRES_TARGET_USED
1909 ? val
1910 : (HOST_WIDE_INT) omp_requires_mask);
1911 if (val != OMP_REQUIRES_TARGET_USED
1912 && omp_requires_mask != OMP_REQUIRES_TARGET_USED)
1914 omp_requires_to_name (buf1, sizeof (buf1),
1915 omp_requires_mask);
1916 error ("OpenMP %<requires%> directive with non-identical "
1917 "clauses in multiple compilation units: %qs vs. "
1918 "%qs", buf1, buf2);
1919 inform (UNKNOWN_LOCATION, "%qs has %qs", fn1, buf1);
1920 inform (UNKNOWN_LOCATION, "%qs has %qs", fn2, buf2);
1922 else
1924 error ("OpenMP %<requires%> directive with %qs specified "
1925 "only in some compilation units", buf2);
1926 inform (UNKNOWN_LOCATION, "%qs has %qs",
1927 val != OMP_REQUIRES_TARGET_USED ? fn2 : fn1,
1928 buf2);
1929 inform (UNKNOWN_LOCATION, "but %qs has not",
1930 val != OMP_REQUIRES_TARGET_USED ? fn1 : fn2);
1932 error_emitted = true;
1935 else
1936 fatal_error (input_location,
1937 "invalid offload table in %s", file_data->file_name);
1939 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1942 lto_destroy_simple_input_block (file_data, LTO_section_offload_table,
1943 ib, data, len);
1945 #ifdef ACCEL_COMPILER
1946 char *omp_requires_file = getenv ("GCC_OFFLOAD_OMP_REQUIRES_FILE");
1947 if (omp_requires_file == NULL || omp_requires_file[0] == '\0')
1948 fatal_error (input_location, "GCC_OFFLOAD_OMP_REQUIRES_FILE unset");
1949 FILE *f = fopen (omp_requires_file, "wb");
1950 if (!f)
1951 fatal_error (input_location, "Cannot open omp_requires file %qs",
1952 omp_requires_file);
1953 uint32_t req_mask = omp_requires_mask;
1954 fwrite (&req_mask, sizeof (req_mask), 1, f);
1955 fclose (f);
1956 #endif
1959 /* True when we need optimization summary for NODE. */
1961 static int
1962 output_cgraph_opt_summary_p (struct cgraph_node *node)
1964 if (node->clone_of || node->former_clone_of)
1965 return true;
1966 clone_info *info = clone_info::get (node);
1967 return info && (info->tree_map || info->param_adjustments);
1970 /* Output optimization summary for EDGE to OB. */
1971 static void
1972 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1973 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1977 /* Output optimization summary for NODE to OB. */
1979 static void
1980 output_node_opt_summary (struct output_block *ob,
1981 struct cgraph_node *node,
1982 lto_symtab_encoder_t encoder)
1984 struct ipa_replace_map *map;
1985 int i;
1986 struct cgraph_edge *e;
1988 /* TODO: Should this code be moved to ipa-param-manipulation? */
1989 struct bitpack_d bp;
1990 bp = bitpack_create (ob->main_stream);
1991 clone_info *info = clone_info::get (node);
1993 bp_pack_value (&bp, (info && info->param_adjustments != NULL), 1);
1994 streamer_write_bitpack (&bp);
1995 if (ipa_param_adjustments *adjustments
1996 = info ? info->param_adjustments : NULL)
1998 streamer_write_uhwi (ob, vec_safe_length (adjustments->m_adj_params));
1999 ipa_adjusted_param *adj;
2000 FOR_EACH_VEC_SAFE_ELT (adjustments->m_adj_params, i, adj)
2002 bp = bitpack_create (ob->main_stream);
2003 bp_pack_value (&bp, adj->base_index, IPA_PARAM_MAX_INDEX_BITS);
2004 bp_pack_value (&bp, adj->prev_clone_index, IPA_PARAM_MAX_INDEX_BITS);
2005 bp_pack_value (&bp, adj->op, 2);
2006 bp_pack_value (&bp, adj->param_prefix_index, 2);
2007 bp_pack_value (&bp, adj->prev_clone_adjustment, 1);
2008 bp_pack_value (&bp, adj->reverse, 1);
2009 bp_pack_value (&bp, adj->user_flag, 1);
2010 streamer_write_bitpack (&bp);
2011 if (adj->op == IPA_PARAM_OP_SPLIT
2012 || adj->op == IPA_PARAM_OP_NEW)
2014 stream_write_tree (ob, adj->type, true);
2015 if (adj->op == IPA_PARAM_OP_SPLIT)
2017 stream_write_tree (ob, adj->alias_ptr_type, true);
2018 streamer_write_uhwi (ob, adj->unit_offset);
2022 streamer_write_hwi (ob, adjustments->m_always_copy_start);
2023 bp = bitpack_create (ob->main_stream);
2024 bp_pack_value (&bp, info->param_adjustments->m_skip_return, 1);
2025 streamer_write_bitpack (&bp);
2028 streamer_write_uhwi (ob, info ? vec_safe_length (info->tree_map) : 0);
2029 if (info)
2030 FOR_EACH_VEC_SAFE_ELT (info->tree_map, i, map)
2032 streamer_write_uhwi (ob, map->parm_num);
2033 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
2034 stream_write_tree (ob, map->new_tree, true);
2037 if (lto_symtab_encoder_in_partition_p (encoder, node))
2039 for (e = node->callees; e; e = e->next_callee)
2040 output_edge_opt_summary (ob, e);
2041 for (e = node->indirect_calls; e; e = e->next_callee)
2042 output_edge_opt_summary (ob, e);
2046 /* Output optimization summaries stored in callgraph.
2047 At the moment it is the clone info structure. */
2049 static void
2050 output_cgraph_opt_summary (void)
2052 int i, n_nodes;
2053 lto_symtab_encoder_t encoder;
2054 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
2055 unsigned count = 0;
2057 ob->symbol = NULL;
2058 encoder = ob->decl_state->symtab_node_encoder;
2059 n_nodes = lto_symtab_encoder_size (encoder);
2060 for (i = 0; i < n_nodes; i++)
2062 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
2063 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2064 if (cnode && output_cgraph_opt_summary_p (cnode))
2065 count++;
2067 streamer_write_uhwi (ob, count);
2068 for (i = 0; i < n_nodes; i++)
2070 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
2071 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2072 if (cnode && output_cgraph_opt_summary_p (cnode))
2074 streamer_write_uhwi (ob, i);
2075 output_node_opt_summary (ob, cnode, encoder);
2078 produce_asm (ob, NULL);
2079 destroy_output_block (ob);
2082 /* Input optimisation summary of EDGE. */
2084 static void
2085 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
2086 class lto_input_block *ib_main ATTRIBUTE_UNUSED)
2090 /* Input optimisation summary of NODE. */
2092 static void
2093 input_node_opt_summary (struct cgraph_node *node,
2094 class lto_input_block *ib_main,
2095 class data_in *data_in)
2097 int i;
2098 int count;
2099 struct cgraph_edge *e;
2101 /* TODO: Should this code be moved to ipa-param-manipulation? */
2102 struct bitpack_d bp;
2103 bp = streamer_read_bitpack (ib_main);
2104 bool have_adjustments = bp_unpack_value (&bp, 1);
2105 clone_info *info = clone_info::get_create (node);
2107 if (have_adjustments)
2109 count = streamer_read_uhwi (ib_main);
2110 vec<ipa_adjusted_param, va_gc> *new_params = NULL;
2111 for (i = 0; i < count; i++)
2113 ipa_adjusted_param adj;
2114 memset (&adj, 0, sizeof (adj));
2115 bp = streamer_read_bitpack (ib_main);
2116 adj.base_index = bp_unpack_value (&bp, IPA_PARAM_MAX_INDEX_BITS);
2117 adj.prev_clone_index
2118 = bp_unpack_value (&bp, IPA_PARAM_MAX_INDEX_BITS);
2119 adj.op = (enum ipa_parm_op) bp_unpack_value (&bp, 2);
2120 adj.param_prefix_index = bp_unpack_value (&bp, 2);
2121 adj.prev_clone_adjustment = bp_unpack_value (&bp, 1);
2122 adj.reverse = bp_unpack_value (&bp, 1);
2123 adj.user_flag = bp_unpack_value (&bp, 1);
2124 if (adj.op == IPA_PARAM_OP_SPLIT
2125 || adj.op == IPA_PARAM_OP_NEW)
2127 adj.type = stream_read_tree (ib_main, data_in);
2128 if (adj.op == IPA_PARAM_OP_SPLIT)
2130 adj.alias_ptr_type = stream_read_tree (ib_main, data_in);
2131 adj.unit_offset = streamer_read_uhwi (ib_main);
2134 vec_safe_push (new_params, adj);
2136 int always_copy_start = streamer_read_hwi (ib_main);
2137 bp = streamer_read_bitpack (ib_main);
2138 bool skip_return = bp_unpack_value (&bp, 1);
2139 info->param_adjustments
2140 = (new (ggc_alloc <ipa_param_adjustments> ())
2141 ipa_param_adjustments (new_params, always_copy_start, skip_return));
2144 count = streamer_read_uhwi (ib_main);
2145 for (i = 0; i < count; i++)
2147 struct ipa_replace_map *map = ggc_alloc<ipa_replace_map> ();
2149 vec_safe_push (info->tree_map, map);
2150 map->parm_num = streamer_read_uhwi (ib_main);
2151 map->new_tree = stream_read_tree (ib_main, data_in);
2153 for (e = node->callees; e; e = e->next_callee)
2154 input_edge_opt_summary (e, ib_main);
2155 for (e = node->indirect_calls; e; e = e->next_callee)
2156 input_edge_opt_summary (e, ib_main);
2159 /* Read section in file FILE_DATA of length LEN with data DATA. */
2161 static void
2162 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
2163 const char *data, size_t len,
2164 vec<symtab_node *> nodes)
2166 const struct lto_function_header *header =
2167 (const struct lto_function_header *) data;
2168 const int cfg_offset = sizeof (struct lto_function_header);
2169 const int main_offset = cfg_offset + header->cfg_size;
2170 const int string_offset = main_offset + header->main_size;
2171 class data_in *data_in;
2172 unsigned int i;
2173 unsigned int count;
2175 lto_input_block ib_main ((const char *) data + main_offset,
2176 header->main_size, file_data->mode_table);
2178 data_in =
2179 lto_data_in_create (file_data, (const char *) data + string_offset,
2180 header->string_size, vNULL);
2181 count = streamer_read_uhwi (&ib_main);
2183 for (i = 0; i < count; i++)
2185 int ref = streamer_read_uhwi (&ib_main);
2186 input_node_opt_summary (dyn_cast<cgraph_node *> (nodes[ref]),
2187 &ib_main, data_in);
2189 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
2190 len);
2191 lto_data_in_delete (data_in);
2194 /* Input optimization summary of cgraph. */
2196 static void
2197 input_cgraph_opt_summary (vec<symtab_node *> nodes)
2199 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2200 struct lto_file_decl_data *file_data;
2201 unsigned int j = 0;
2203 while ((file_data = file_data_vec[j++]))
2205 size_t len;
2206 const char *data
2207 = lto_get_summary_section_data (file_data, LTO_section_cgraph_opt_sum,
2208 &len);
2209 if (data)
2210 input_cgraph_opt_section (file_data, data, len, nodes);