Debian Codegraph @

gcc/lto-cgraph.cc

raw
1 /* Write and read the cgraph to the memory mapped representation of a
2 .o file.
3
4 Copyright (C) 2009-2025 Free Software Foundation, Inc.
5 Contributed by Kenneth Zadeck <[email protected]>
6
7 This file is part of GCC.
8
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.
13
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.
18
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/>. */
22
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"
47
48 /* True when asm nodes has been output. */
49 bool asm_nodes_output = false;
50
51 static void output_cgraph_opt_summary (void);
52 static void input_cgraph_opt_summary (vec<symtab_node *> nodes);
53
54 /* Number of LDPR values known to GCC. */
55 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
56
57 /* Cgraph streaming is organized as set of record whose type
58 is indicated by a tag. */
59 enum LTO_symtab_tags
60 {
61 /* Must leave 0 for the stopper. */
62
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_indirect_function,
72 LTO_symtab_last_tag
73 };
74
75 /* Create a new symtab encoder.
76 if FOR_INPUT, the encoder allocate only datastructures needed
77 to read the symtab. */
78
79 lto_symtab_encoder_t
80 lto_symtab_encoder_new (bool for_input)
81 {
82 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
83
84 if (!for_input)
85 encoder->map = new hash_map<symtab_node *, size_t>;
86 encoder->nodes.create (0);
87 return encoder;
88 }
89
90
91 /* Delete ENCODER and its components. */
92
93 void
94 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
95 {
96 encoder->nodes.release ();
97 if (encoder->map)
98 delete encoder->map;
99 if (encoder->order_remap)
100 delete encoder->order_remap;
101 free (encoder);
102 }
103
104
105 /* Return the existing reference number of NODE in the symtab encoder in
106 output block OB. Assign a new reference if this is the first time
107 NODE is encoded. */
108
109 int
110 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
111 symtab_node *node)
112 {
113 int ref;
114
115 if (!encoder->map)
116 {
117 lto_encoder_entry entry (node);
118
119 ref = encoder->nodes.length ();
120 encoder->nodes.safe_push (entry);
121 return ref;
122 }
123
124 size_t *slot = encoder->map->get (node);
125 if (!slot || !*slot)
126 {
127 lto_encoder_entry entry (node);
128 ref = encoder->nodes.length ();
129 if (!slot)
130 encoder->map->put (node, ref + 1);
131 encoder->nodes.safe_push (entry);
132 }
133 else
134 ref = *slot - 1;
135
136 return ref;
137 }
138
139 /* Remove NODE from encoder. */
140
141 bool
142 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
143 symtab_node *node)
144 {
145 int index;
146
147 size_t *slot = encoder->map->get (node);
148 if (slot == NULL || !*slot)
149 return false;
150
151 index = *slot - 1;
152 gcc_checking_assert (encoder->nodes[index].node == node);
153
154 /* Remove from vector. We do this by swapping node with the last element
155 of the vector. */
156 lto_encoder_entry last_node = encoder->nodes.pop ();
157 if (last_node.node != node)
158 {
159 bool existed = encoder->map->put (last_node.node, index + 1);
160 gcc_assert (existed);
161
162 /* Move the last element to the original spot of NODE. */
163 encoder->nodes[index] = last_node;
164 }
165
166 /* Remove element from hash table. */
167 encoder->map->remove (node);
168 return true;
169 }
170
171 /* Return TRUE if the NODE and its clones are always inlined. */
172
173 bool
174 lto_symtab_encoder_only_for_inlining_p (lto_symtab_encoder_t encoder,
175 struct cgraph_node *node)
176 {
177 int index = lto_symtab_encoder_lookup (encoder, node);
178 return encoder->nodes[index].only_for_inlining;
179 }
180
181 /* Return TRUE if we should encode the body of NODE (if any). */
182
183 bool
184 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
185 struct cgraph_node *node)
186 {
187 int index = lto_symtab_encoder_lookup (encoder, node);
188 return encoder->nodes[index].body;
189 }
190
191 /* Return TRUE if we should encode initializer of NODE (if any). */
192
193 bool
194 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
195 varpool_node *node)
196 {
197 int index = lto_symtab_encoder_lookup (encoder, node);
198 if (index == LCC_NOT_FOUND)
199 return false;
200 return encoder->nodes[index].initializer;
201 }
202
203 /* Specify that we should encode initializer of NODE (if any). */
204
205 static void
206 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
207 varpool_node *node)
208 {
209 int index = lto_symtab_encoder_lookup (encoder, node);
210 encoder->nodes[index].initializer = true;
211 }
212
213 /* Return TRUE if NODE is in this partition. */
214
215 bool
216 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
217 symtab_node *node)
218 {
219 int index = lto_symtab_encoder_lookup (encoder, node);
220 if (index == LCC_NOT_FOUND)
221 return false;
222 return encoder->nodes[index].in_partition;
223 }
224
225 /* Specify that NODE is in this partition. */
226
227 void
228 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
229 symtab_node *node)
230 {
231 int index = lto_symtab_encoder_encode (encoder, node);
232 if (dump_file)
233 fprintf(dump_file, "Node %s, index %d\n", node->asm_name(), index);
234 encoder->nodes[index].in_partition = true;
235 }
236
237 /* Output the cgraph EDGE to OB using ENCODER. */
238
239 static void
240 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
241 lto_symtab_encoder_t encoder)
242 {
243 unsigned int uid;
244 intptr_t ref;
245 struct bitpack_d bp;
246
247 if (edge->indirect_unknown_callee)
248 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
249 LTO_symtab_indirect_edge);
250 else
251 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
252 LTO_symtab_edge);
253
254 ref = lto_symtab_encoder_lookup (encoder, edge->caller);
255 gcc_assert (ref != LCC_NOT_FOUND);
256 streamer_write_hwi_stream (ob->main_stream, ref);
257
258 if (!edge->indirect_unknown_callee)
259 {
260 ref = lto_symtab_encoder_lookup (encoder, edge->callee);
261 gcc_assert (ref != LCC_NOT_FOUND);
262 streamer_write_hwi_stream (ob->main_stream, ref);
263 }
264
265 edge->count.stream_out (ob->main_stream);
266
267 bp = bitpack_create (ob->main_stream);
268 uid = !edge->call_stmt ? edge->lto_stmt_uid
269 : gimple_uid (edge->call_stmt) + 1;
270 bp_pack_enum (&bp, cgraph_inline_failed_t,
271 CIF_N_REASONS, edge->inline_failed);
272 gcc_checking_assert (uid || edge->caller->thunk);
273 bp_pack_var_len_unsigned (&bp, uid);
274 bp_pack_value (&bp, edge->speculative_id, 16);
275 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
276 bp_pack_value (&bp, edge->speculative, 1);
277 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
278 gcc_assert (!edge->call_stmt_cannot_inline_p
279 || edge->inline_failed != CIF_BODY_NOT_AVAILABLE);
280 bp_pack_value (&bp, edge->can_throw_external, 1);
281 bp_pack_value (&bp, edge->in_polymorphic_cdtor, 1);
282 if (edge->indirect_unknown_callee)
283 {
284 int flags = edge->indirect_info->ecf_flags;
285 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
286 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
287 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
288 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
289 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
290 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
291 /* Flags that should not appear on indirect calls. */
292 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
293 | ECF_MAY_BE_ALLOCA
294 | ECF_SIBCALL
295 | ECF_LEAF
296 | ECF_NOVOPS)));
297
298 bp_pack_value (&bp, edge->indirect_info->num_speculative_call_targets,
299 16);
300 }
301 streamer_write_bitpack (&bp);
302 }
303
304 /* Return if NODE contain references from other partitions. */
305
306 bool
307 referenced_from_other_partition_p (symtab_node *node, lto_symtab_encoder_t encoder)
308 {
309 int i;
310 struct ipa_ref *ref = NULL;
311
312 for (i = 0; node->iterate_referring (i, ref); i++)
313 {
314 /* Ignore references from non-offloadable nodes while streaming NODE into
315 offload LTO section. */
316 if (!ref->referring->need_lto_streaming)
317 continue;
318
319 if (ref->referring->in_other_partition
320 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
321 return true;
322 }
323 return false;
324 }
325
326 /* Return true when node is reachable from other partition. */
327
328 bool
329 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
330 {
331 struct cgraph_edge *e;
332 if (!node->definition)
333 return false;
334 if (node->inlined_to)
335 return false;
336 for (e = node->callers; e; e = e->next_caller)
337 {
338 /* Ignore references from non-offloadable nodes while streaming NODE into
339 offload LTO section. */
340 if (!e->caller->need_lto_streaming)
341 continue;
342
343 if (e->caller->in_other_partition
344 || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
345 return true;
346 }
347 return false;
348 }
349
350 /* Return if NODE contain references from other partitions. */
351
352 bool
353 referenced_from_this_partition_p (symtab_node *node,
354 lto_symtab_encoder_t encoder)
355 {
356 int i;
357 struct ipa_ref *ref = NULL;
358
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;
363 }
364
365 /* Return true when node is reachable from other partition. */
366
367 bool
368 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
369 {
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;
375 }
376
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. */
384
385 static void
386 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
387 lto_symtab_encoder_t encoder)
388 {
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;
400
401 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
402
403 if (node->analyzed && (!boundary_p || node->alias
404 || (node->thunk && !node->inlined_to)))
405 tag = LTO_symtab_analyzed_node;
406 else
407 tag = LTO_symtab_unavail_node;
408
409 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
410 tag);
411 int output_order = *encoder->order_remap->get (node->order);
412 streamer_write_hwi_stream (ob->main_stream, output_order);
413
414 /* In WPA mode, we only output part of the call-graph. Also, we
415 fake cgraph node attributes. There are two cases that we care.
416
417 Boundary nodes: There are nodes that are not part of SET but are
418 called from within SET. We artificially make them look like
419 externally visible nodes with no function body.
420
421 Cherry-picked nodes: These are nodes we pulled from other
422 translation units into SET during IPA-inlining. We make them as
423 local static nodes to prevent clashes with other local statics. */
424 if (boundary_p && node->analyzed
425 && node->get_partitioning_class () == SYMBOL_PARTITION)
426 {
427 /* Inline clones cannot be part of boundary.
428 gcc_assert (!node->inlined_to);
429
430 FIXME: At the moment they can be, when partition contains an inline
431 clone that is clone of inline clone from outside partition. We can
432 reshape the clone tree and make other tree to be the root, but it
433 needs a bit extra work and will be promplty done by cgraph_remove_node
434 after reading back. */
435 in_other_partition = 1;
436 }
437 else if (UNLIKELY (lto_stream_offload_p
438 && lookup_attribute ("omp target device_ancestor_host",
439 DECL_ATTRIBUTES (node->decl))))
440 /* This symbol is only used as argument to IFN_GOMP_TARGET_REV; this IFN
441 is ignored on ACCEL_COMPILER. Thus, mark it as in_other_partition to silence
442 verify_node_partition diagnostic. */
443 in_other_partition = 1;
444
445 clone_of = node->clone_of;
446 while (clone_of
447 && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
448 if (clone_of->prev_sibling_clone)
449 clone_of = clone_of->prev_sibling_clone;
450 else
451 clone_of = clone_of->clone_of;
452
453 /* See if body of the master function is output. If not, we are seeing only
454 an declaration and we do not need to pass down clone tree. */
455 ultimate_clone_of = clone_of;
456 while (ultimate_clone_of && ultimate_clone_of->clone_of)
457 ultimate_clone_of = ultimate_clone_of->clone_of;
458
459 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
460 clone_of = NULL;
461
462 if (tag == LTO_symtab_analyzed_node)
463 gcc_assert (clone_of || !node->clone_of);
464 if (!clone_of)
465 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
466 else
467 streamer_write_hwi_stream (ob->main_stream, ref);
468
469
470 lto_output_fn_decl_ref (ob->decl_state, ob->main_stream, node->decl);
471 node->count.stream_out (ob->main_stream);
472 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
473
474 streamer_write_hwi_stream (ob->main_stream,
475 node->ipa_transforms_to_apply.length ());
476 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
477 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
478
479 if (tag == LTO_symtab_analyzed_node)
480 {
481 if (node->inlined_to)
482 {
483 ref = lto_symtab_encoder_lookup (encoder, node->inlined_to);
484 gcc_assert (ref != LCC_NOT_FOUND);
485 }
486 else
487 ref = LCC_NOT_FOUND;
488
489 streamer_write_hwi_stream (ob->main_stream, ref);
490 }
491
492 group = node->get_comdat_group ();
493 if (group)
494 comdat = IDENTIFIER_POINTER (group);
495 else
496 comdat = "";
497 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
498
499 if (group)
500 {
501 if (node->same_comdat_group)
502 {
503 ref = LCC_NOT_FOUND;
504 for (struct symtab_node *n = node->same_comdat_group;
505 ref == LCC_NOT_FOUND && n != node; n = n->same_comdat_group)
506 ref = lto_symtab_encoder_lookup (encoder, n);
507 }
508 else
509 ref = LCC_NOT_FOUND;
510 streamer_write_hwi_stream (ob->main_stream, ref);
511 }
512
513 section = node->get_section ();
514 if (!section)
515 section = "";
516
517 streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
518
519 bp = bitpack_create (ob->main_stream);
520 bp_pack_value (&bp, node->local, 1);
521 bp_pack_value (&bp, node->externally_visible, 1);
522 bp_pack_value (&bp, node->no_reorder, 1);
523 bp_pack_value (&bp, node->definition, 1);
524 bp_pack_value (&bp, node->versionable, 1);
525 bp_pack_value (&bp, node->can_change_signature, 1);
526 bp_pack_value (&bp, node->redefined_extern_inline, 1);
527 bp_pack_value (&bp, node->force_output, 1);
528 bp_pack_value (&bp, node->forced_by_abi, 1);
529 bp_pack_value (&bp, node->unique_name, 1);
530 bp_pack_value (&bp, node->body_removed, 1);
531 bp_pack_value (&bp, node->semantic_interposition, 1);
532 bp_pack_value (&bp, node->implicit_section, 1);
533 bp_pack_value (&bp, node->address_taken, 1);
534 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
535 && node->get_partitioning_class () == SYMBOL_PARTITION
536 && (reachable_from_other_partition_p (node, encoder)
537 || referenced_from_other_partition_p (node, encoder)), 1);
538 bp_pack_value (&bp, node->lowered, 1);
539 bp_pack_value (&bp, in_other_partition, 1);
540 bp_pack_value (&bp, node->alias, 1);
541 bp_pack_value (&bp, node->transparent_alias, 1);
542 bp_pack_value (&bp, node->weakref, 1);
543 bp_pack_value (&bp, node->symver, 1);
544 bp_pack_value (&bp, node->frequency, 2);
545 bp_pack_value (&bp, node->only_called_at_startup, 1);
546 bp_pack_value (&bp, node->only_called_at_exit, 1);
547 bp_pack_value (&bp, node->tm_clone, 1);
548 bp_pack_value (&bp, node->calls_comdat_local, 1);
549 bp_pack_value (&bp, node->icf_merged, 1);
550 bp_pack_value (&bp, node->nonfreeing_fn, 1);
551 bp_pack_value (&bp, node->merged_comdat, 1);
552 bp_pack_value (&bp, node->merged_extern_inline, 1);
553 bp_pack_value (&bp, node->thunk, 1);
554 bp_pack_value (&bp, node->parallelized_function, 1);
555 bp_pack_value (&bp, node->has_omp_variant_constructs, 1);
556
557 /* Stream thunk info always because we use it in
558 ipa_polymorphic_call_context::ipa_polymorphic_call_context
559 to properly interpret THIS pointers for thunks that has been converted
560 to Gimple. */
561 struct thunk_info *thunk = node->definition ? thunk_info::get (node) : NULL;
562
563 bp_pack_value (&bp, thunk != NULL, 1);
564
565 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
566 LDPR_NUM_KNOWN,
567 /* When doing incremental link, we will get new resolution
568 info next time we process the file. */
569 flag_incremental_link == INCREMENTAL_LINK_LTO
570 ? LDPR_UNKNOWN : node->resolution);
571 bp_pack_value (&bp, node->split_part, 1);
572 streamer_write_bitpack (&bp);
573 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
574
575 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
576 streamer_write_hwi_stream (ob->main_stream, node->unit_id);
577 if (DECL_STATIC_CONSTRUCTOR (node->decl))
578 streamer_write_hwi_stream (ob->main_stream, node->get_init_priority ());
579 if (DECL_STATIC_DESTRUCTOR (node->decl))
580 streamer_write_hwi_stream (ob->main_stream, node->get_fini_priority ());
581
582 if (thunk)
583 thunk_info::get (node)->stream_out (ob);
584 }
585
586 /* Output the varpool NODE to OB.
587 If NODE is not in SET, then NODE is a boundary. */
588
589 static void
590 lto_output_varpool_node (struct lto_simple_output_block *ob, varpool_node *node,
591 lto_symtab_encoder_t encoder)
592 {
593 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
594 bool encode_initializer_p
595 = (node->definition
596 && lto_symtab_encoder_encode_initializer_p (encoder, node));
597 struct bitpack_d bp;
598 int ref;
599 const char *comdat;
600 const char *section;
601 tree group;
602
603 gcc_assert (!encode_initializer_p || node->definition);
604 gcc_assert (boundary_p || encode_initializer_p);
605
606 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
607 LTO_symtab_variable);
608 int output_order = *encoder->order_remap->get (node->order);
609 streamer_write_hwi_stream (ob->main_stream, output_order);
610 lto_output_var_decl_ref (ob->decl_state, ob->main_stream, node->decl);
611 bp = bitpack_create (ob->main_stream);
612 bp_pack_value (&bp, node->externally_visible, 1);
613 bp_pack_value (&bp, node->no_reorder, 1);
614 bp_pack_value (&bp, node->force_output, 1);
615 bp_pack_value (&bp, node->forced_by_abi, 1);
616 bp_pack_value (&bp, node->unique_name, 1);
617 bp_pack_value (&bp,
618 node->body_removed
619 || (!encode_initializer_p && !node->alias && node->definition),
620 1);
621 bp_pack_value (&bp, node->semantic_interposition, 1);
622 bp_pack_value (&bp, node->implicit_section, 1);
623 bp_pack_value (&bp, node->writeonly, 1);
624 bp_pack_value (&bp, node->definition && (encode_initializer_p || node->alias),
625 1);
626 bp_pack_value (&bp, node->alias, 1);
627 bp_pack_value (&bp, node->transparent_alias, 1);
628 bp_pack_value (&bp, node->weakref, 1);
629 bp_pack_value (&bp, node->symver, 1);
630 bp_pack_value (&bp, node->analyzed && (!boundary_p || node->alias), 1);
631 gcc_assert (node->definition || !node->analyzed);
632 /* Constant pool initializers can be de-unified into individual ltrans units.
633 FIXME: Alternatively at -Os we may want to avoid generating for them the local
634 labels and share them across LTRANS partitions. */
635 if (node->get_partitioning_class () != SYMBOL_PARTITION)
636 {
637 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
638 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
639 }
640 else
641 {
642 bp_pack_value (&bp, node->definition
643 && referenced_from_other_partition_p (node, encoder), 1);
644 bp_pack_value (&bp, node->analyzed
645 && boundary_p && !DECL_EXTERNAL (node->decl), 1);
646 /* in_other_partition. */
647 }
648 bp_pack_value (&bp, node->tls_model, 3);
649 bp_pack_value (&bp, node->used_by_single_function, 1);
650 bp_pack_value (&bp, node->dynamically_initialized, 1);
651 streamer_write_bitpack (&bp);
652
653 group = node->get_comdat_group ();
654 if (group)
655 comdat = IDENTIFIER_POINTER (group);
656 else
657 comdat = "";
658 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
659
660 if (group)
661 {
662 if (node->same_comdat_group)
663 {
664 ref = LCC_NOT_FOUND;
665 for (struct symtab_node *n = node->same_comdat_group;
666 ref == LCC_NOT_FOUND && n != node; n = n->same_comdat_group)
667 ref = lto_symtab_encoder_lookup (encoder, n);
668 }
669 else
670 ref = LCC_NOT_FOUND;
671 streamer_write_hwi_stream (ob->main_stream, ref);
672 }
673
674 section = node->get_section ();
675 if (!section)
676 section = "";
677 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
678
679 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
680 LDPR_NUM_KNOWN, node->resolution);
681 }
682
683 /* Output the varpool NODE to OB.
684 If NODE is not in SET, then NODE is a boundary. */
685
686 static void
687 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
688 lto_symtab_encoder_t encoder)
689 {
690 struct bitpack_d bp;
691 int nref;
692 int uid = !ref->stmt ? ref->lto_stmt_uid : gimple_uid (ref->stmt) + 1;
693 struct cgraph_node *node;
694
695 bp = bitpack_create (ob->main_stream);
696 bp_pack_value (&bp, ref->use, 3);
697 bp_pack_value (&bp, ref->speculative, 1);
698 streamer_write_bitpack (&bp);
699 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
700 gcc_assert (nref != LCC_NOT_FOUND);
701 streamer_write_hwi_stream (ob->main_stream, nref);
702
703 node = dyn_cast <cgraph_node *> (ref->referring);
704 if (node)
705 {
706 if (ref->stmt)
707 uid = gimple_uid (ref->stmt) + 1;
708 streamer_write_hwi_stream (ob->main_stream, uid);
709 bp_pack_value (&bp, ref->speculative_id, 16);
710 streamer_write_bitpack (&bp);
711 }
712 }
713
714 /* Stream out profile_summary to OB. */
715
716 static void
717 output_profile_summary (struct lto_simple_output_block *ob)
718 {
719 if (profile_info)
720 {
721 /* We do not output num and run_max, they are not used by
722 GCC profile feedback and they are difficult to merge from multiple
723 units. */
724 unsigned runs = (profile_info->runs);
725 streamer_write_uhwi_stream (ob->main_stream, runs);
726
727 /* IPA-profile computes hot bb threshold based on cumulated
728 whole program profile. We need to stream it down to ltrans. */
729 if (flag_wpa)
730 streamer_write_gcov_count_stream (ob->main_stream,
731 get_hot_bb_threshold ());
732 }
733 else
734 streamer_write_uhwi_stream (ob->main_stream, 0);
735 }
736
737 /* Output all callees or indirect outgoing edges. EDGE must be the first such
738 edge. */
739
740 static void
741 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
742 struct lto_simple_output_block *ob,
743 lto_symtab_encoder_t encoder)
744 {
745 if (!edge)
746 return;
747
748 /* Output edges in backward direction, so the reconstructed callgraph match
749 and it is easy to associate call sites in the IPA pass summaries. */
750 while (edge->next_callee)
751 edge = edge->next_callee;
752 for (; edge; edge = edge->prev_callee)
753 lto_output_edge (ob, edge, encoder);
754 }
755
756 /* Output the part of the cgraph in SET. */
757
758 static void
759 output_refs (lto_symtab_encoder_t encoder)
760 {
761 struct lto_simple_output_block *ob;
762 int count;
763 struct ipa_ref *ref;
764
765 ob = lto_create_simple_output_block (LTO_section_refs);
766
767 for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
768 {
769 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
770
771 /* IPA_REF_ALIAS references are always preserved
772 in the boundary. Alias node can't have other references and
773 can be always handled as if it's not in the boundary. */
774 if (!node->alias && !lto_symtab_encoder_in_partition_p (encoder, node))
775 continue;
776
777 count = node->ref_list.nreferences ();
778 if (count)
779 {
780 streamer_write_gcov_count_stream (ob->main_stream, count);
781 streamer_write_uhwi_stream (ob->main_stream,
782 lto_symtab_encoder_lookup (encoder, node));
783 for (int i = 0; node->iterate_reference (i, ref); i++)
784 lto_output_ref (ob, ref, encoder);
785 }
786 }
787
788 streamer_write_uhwi_stream (ob->main_stream, 0);
789
790 lto_destroy_simple_output_block (ob);
791 }
792
793 /* Add NODE into encoder as well as nodes it is cloned from.
794 Do it in a way so clones appear first. */
795
796 static void
797 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
798 bool include_body, bool not_inlined)
799 {
800 if (node->clone_of)
801 add_node_to (encoder, node->clone_of, include_body, not_inlined);
802
803 int index = lto_symtab_encoder_encode (encoder, node);
804 gcc_checking_assert (encoder->nodes[index].node == node);
805
806 if (include_body)
807 encoder->nodes[index].body = true;
808 if (not_inlined)
809 encoder->nodes[index].only_for_inlining = false;
810 }
811
812 /* Add NODE into encoder as well as nodes it is cloned from.
813 Do it in a way so clones appear first. */
814
815 static void
816 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
817 bool include_body)
818 {
819 add_node_to (encoder, node, include_body, include_body && !node->inlined_to);
820 }
821
822 /* Add all references in NODE to encoders. */
823
824 static void
825 create_references (lto_symtab_encoder_t encoder, symtab_node *node)
826 {
827 int i;
828 struct ipa_ref *ref = NULL;
829 for (i = 0; node->iterate_reference (i, ref); i++)
830 if (is_a <cgraph_node *> (ref->referred))
831 add_node_to (encoder, dyn_cast <cgraph_node *> (ref->referred), false);
832 else
833 lto_symtab_encoder_encode (encoder, ref->referred);
834 }
835
836 /* Select what needs to be streamed out. In regular lto mode stream everything.
837 In offload lto mode stream only nodes marked as offloadable. */
838 void
839 select_what_to_stream (void)
840 {
841 struct symtab_node *snode;
842 FOR_EACH_SYMBOL (snode)
843 snode->need_lto_streaming = !lto_stream_offload_p || snode->offloadable;
844 }
845
846 /* Find all symbols we want to stream into given partition and insert them
847 to encoders.
848
849 The function actually replaces IN_ENCODER by new one. The reason is that
850 streaming code needs clone's origin to be streamed before clone. This
851 means that we need to insert the nodes in specific order. This order is
852 ignored by the partitioning logic earlier. */
853
854 lto_symtab_encoder_t
855 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
856 {
857 struct cgraph_edge *edge;
858 int i;
859 lto_symtab_encoder_t encoder;
860 lto_symtab_encoder_iterator lsei;
861 hash_set<void *> reachable_call_targets;
862
863 encoder = lto_symtab_encoder_new (false);
864
865 /* Go over all entries in the IN_ENCODER and duplicate them to
866 ENCODER. At the same time insert masters of clones so
867 every master appears before clone. */
868 for (lsei = lsei_start_function_in_partition (in_encoder);
869 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
870 {
871 struct cgraph_node *node = lsei_cgraph_node (lsei);
872 if (!node->need_lto_streaming)
873 continue;
874 add_node_to (encoder, node, true);
875 lto_set_symtab_encoder_in_partition (encoder, node);
876 create_references (encoder, node);
877 }
878 for (lsei = lsei_start_variable_in_partition (in_encoder);
879 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
880 {
881 varpool_node *vnode = lsei_varpool_node (lsei);
882
883 if (!vnode->need_lto_streaming)
884 continue;
885 lto_set_symtab_encoder_in_partition (encoder, vnode);
886 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
887 create_references (encoder, vnode);
888 }
889 /* Pickle in also the initializer of all referenced readonly variables
890 to help folding. Constant pool variables are not shared, so we must
891 pickle those too. */
892 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
893 {
894 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
895 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
896 {
897 if (!lto_symtab_encoder_encode_initializer_p (encoder,
898 vnode)
899 && (((vnode->ctor_useable_for_folding_p ()
900 && (!DECL_VIRTUAL_P (vnode->decl)
901 || !flag_wpa
902 || flag_ltrans_devirtualize)))))
903 {
904 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
905 create_references (encoder, vnode);
906 }
907 }
908 }
909
910 /* Go over all the nodes again to include callees that are not in
911 SET. */
912 for (lsei = lsei_start_function_in_partition (encoder);
913 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
914 {
915 struct cgraph_node *node = lsei_cgraph_node (lsei);
916 for (edge = node->callees; edge; edge = edge->next_callee)
917 {
918 struct cgraph_node *callee = edge->callee;
919 if (!lto_symtab_encoder_in_partition_p (encoder, callee))
920 {
921 /* We should have moved all the inlines. */
922 gcc_assert (!callee->inlined_to);
923 add_node_to (encoder, callee, false);
924 }
925 }
926 /* Add all possible targets for late devirtualization. */
927 if (flag_ltrans_devirtualize || !flag_wpa)
928 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
929 if (edge->indirect_info->polymorphic)
930 {
931 unsigned int i;
932 void *cache_token;
933 bool final;
934 vec <cgraph_node *>targets
935 = possible_polymorphic_call_targets
936 (edge, &final, &cache_token);
937 if (cache_token != NULL
938 && !reachable_call_targets.add (cache_token))
939 {
940 for (i = 0; i < targets.length (); i++)
941 {
942 struct cgraph_node *callee = targets[i];
943
944 /* Adding an external declarations into the unit serves
945 no purpose and just increases its boundary. */
946 if (callee->definition
947 && !lto_symtab_encoder_in_partition_p
948 (encoder, callee))
949 {
950 gcc_assert (!callee->inlined_to);
951 add_node_to (encoder, callee, false);
952 }
953 }
954 }
955 }
956 }
957 /* Be sure to also insert alias targert and thunk callees. These needs
958 to stay to aid local calling conventions. */
959 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
960 {
961 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
962 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
963
964 if (node->alias && node->analyzed)
965 create_references (encoder, node);
966 if (cnode
967 && cnode->thunk && !cnode->inlined_to)
968 add_node_to (encoder, cnode->callees->callee, false);
969 while (node->transparent_alias && node->analyzed)
970 {
971 node = node->get_alias_target ();
972 if (is_a <cgraph_node *> (node))
973 add_node_to (encoder, dyn_cast <cgraph_node *> (node),
974 false);
975 else
976 lto_symtab_encoder_encode (encoder, node);
977 }
978 }
979 lto_symtab_encoder_delete (in_encoder);
980 return encoder;
981 }
982
983 /* Output the part of the symtab in SET and VSET. */
984
985 void
986 output_symtab (void)
987 {
988 struct cgraph_node *node;
989 struct lto_simple_output_block *ob;
990 int i, n_nodes;
991 lto_symtab_encoder_t encoder;
992
993 if (flag_wpa)
994 output_cgraph_opt_summary ();
995
996 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
997
998 output_profile_summary (ob);
999
1000 /* An encoder for cgraph nodes should have been created by
1001 ipa_write_summaries_1. */
1002 gcc_assert (ob->decl_state->symtab_node_encoder);
1003 encoder = ob->decl_state->symtab_node_encoder;
1004
1005 /* Write out the nodes. We must first output a node and then its clones,
1006 otherwise at a time reading back the node there would be nothing to clone
1007 from. */
1008 n_nodes = lto_symtab_encoder_size (encoder);
1009 for (i = 0; i < n_nodes; i++)
1010 {
1011 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1012 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1013 lto_output_node (ob, cnode, encoder);
1014 else
1015 lto_output_varpool_node (ob, dyn_cast<varpool_node *> (node), encoder);
1016 }
1017
1018 /* Go over the nodes in SET again to write edges. */
1019 for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
1020 {
1021 node = dyn_cast <cgraph_node *> (lto_symtab_encoder_deref (encoder, i));
1022 if (node
1023 && ((node->thunk && !node->inlined_to)
1024 || lto_symtab_encoder_in_partition_p (encoder, node)))
1025 {
1026 output_outgoing_cgraph_edges (node->callees, ob, encoder);
1027 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
1028 }
1029 }
1030
1031 streamer_write_uhwi_stream (ob->main_stream, 0);
1032
1033 lto_destroy_simple_output_block (ob);
1034
1035 /* Emit toplevel asms.
1036 When doing WPA we must output every asm just once. Since we do not partition asm
1037 nodes at all, output them to first output. This is kind of hack, but should work
1038 well. */
1039 if (!asm_nodes_output && !lto_stream_offload_p)
1040 {
1041 asm_nodes_output = true;
1042 lto_output_toplevel_asms ();
1043 }
1044
1045 output_refs (encoder);
1046 }
1047
1048 /* Return identifier encoded in IB as a plain string. */
1049
1050 static tree
1051 read_identifier (class lto_input_block *ib)
1052 {
1053 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1054 tree id;
1055
1056 if (ib->data[ib->p + len])
1057 lto_section_overrun (ib);
1058 if (!len)
1059 {
1060 ib->p++;
1061 return NULL;
1062 }
1063 id = get_identifier (ib->data + ib->p);
1064 ib->p += len + 1;
1065 return id;
1066 }
1067
1068 /* Return string encoded in IB, NULL if string is empty. */
1069
1070 static const char *
1071 read_string (class lto_input_block *ib)
1072 {
1073 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1074 const char *str;
1075
1076 if (ib->data[ib->p + len])
1077 lto_section_overrun (ib);
1078 if (!len)
1079 {
1080 ib->p++;
1081 return NULL;
1082 }
1083 str = ib->data + ib->p;
1084 ib->p += len + 1;
1085 return str;
1086 }
1087
1088 /* Output function/variable tables that will allow libgomp to look up offload
1089 target code.
1090 OFFLOAD_FUNCS is filled in expand_omp_target, OFFLOAD_VARS is filled in
1091 varpool_node::get_create. In WHOPR (partitioned) mode during the WPA stage
1092 both OFFLOAD_FUNCS and OFFLOAD_VARS are filled by input_offload_tables. */
1093
1094 void
1095 output_offload_tables (void)
1096 {
1097 bool output_requires = (flag_openmp
1098 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0);
1099 if (vec_safe_is_empty (offload_funcs) && vec_safe_is_empty (offload_vars)
1100 && !output_requires)
1101 return;
1102
1103 struct lto_simple_output_block *ob
1104 = lto_create_simple_output_block (LTO_section_offload_table);
1105
1106 for (unsigned i = 0; i < vec_safe_length (offload_funcs); i++)
1107 {
1108 symtab_node *node = symtab_node::get ((*offload_funcs)[i]);
1109 if (!node)
1110 continue;
1111 node->force_output = true;
1112 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1113 LTO_symtab_last_tag, LTO_symtab_unavail_node);
1114 lto_output_fn_decl_ref (ob->decl_state, ob->main_stream,
1115 (*offload_funcs)[i]);
1116 }
1117
1118 for (unsigned i = 0; i < vec_safe_length (offload_vars); i++)
1119 {
1120 symtab_node *node = symtab_node::get ((*offload_vars)[i]);
1121 if (!node)
1122 continue;
1123 node->force_output = true;
1124 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1125 LTO_symtab_last_tag, LTO_symtab_variable);
1126 lto_output_var_decl_ref (ob->decl_state, ob->main_stream,
1127 (*offload_vars)[i]);
1128 }
1129
1130 for (unsigned i = 0; i < vec_safe_length (offload_ind_funcs); i++)
1131 {
1132 symtab_node *node = symtab_node::get ((*offload_ind_funcs)[i]);
1133 if (!node)
1134 continue;
1135 node->force_output = true;
1136 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1137 LTO_symtab_last_tag, LTO_symtab_indirect_function);
1138 lto_output_fn_decl_ref (ob->decl_state, ob->main_stream,
1139 (*offload_ind_funcs)[i]);
1140 }
1141
1142 if (output_requires)
1143 {
1144 HOST_WIDE_INT val = ((HOST_WIDE_INT) omp_requires_mask
1145 & (OMP_REQUIRES_UNIFIED_ADDRESS
1146 | OMP_REQUIRES_UNIFIED_SHARED_MEMORY
1147 | OMP_REQUIRES_SELF_MAPS
1148 | OMP_REQUIRES_REVERSE_OFFLOAD
1149 | OMP_REQUIRES_TARGET_USED));
1150 /* (Mis)use LTO_symtab_edge for this variable. */
1151 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1152 LTO_symtab_last_tag, LTO_symtab_edge);
1153 streamer_write_hwi_stream (ob->main_stream, val);
1154 }
1155
1156 streamer_write_uhwi_stream (ob->main_stream, 0);
1157 lto_destroy_simple_output_block (ob);
1158 }
1159
1160 /* Verify the partitioning of NODE. */
1161
1162 static inline void
1163 verify_node_partition (symtab_node *node)
1164 {
1165 if (flag_ltrans)
1166 return;
1167
1168 #ifdef ACCEL_COMPILER
1169 if (node->in_other_partition)
1170 {
1171 if (TREE_CODE (node->decl) == FUNCTION_DECL)
1172 {
1173 if (lookup_attribute ("omp target device_ancestor_host",
1174 DECL_ATTRIBUTES (node->decl)) != NULL)
1175 return;
1176 error_at (DECL_SOURCE_LOCATION (node->decl),
1177 "function %qs has been referenced in offloaded code but"
1178 " hasn%'t been marked to be included in the offloaded code",
1179 node->name ());
1180 }
1181 else if (VAR_P (node->decl))
1182 error_at (DECL_SOURCE_LOCATION (node->decl),
1183 "variable %qs has been referenced in offloaded code but"
1184 " hasn%'t been marked to be included in the offloaded code",
1185 node->name ());
1186 else
1187 gcc_unreachable ();
1188 }
1189 #else
1190 gcc_assert (!node->in_other_partition
1191 && !node->used_from_other_partition);
1192 #endif
1193 }
1194
1195 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
1196 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
1197 NODE or to replace the values in it, for instance because the first
1198 time we saw it, the function body was not available but now it
1199 is. BP is a bitpack with all the bitflags for NODE read from the
1200 stream. Initialize HAS_THUNK_INFO to indicate if thunk info should
1201 be streamed in. */
1202
1203 static void
1204 input_overwrite_node (struct lto_file_decl_data *file_data,
1205 struct cgraph_node *node,
1206 enum LTO_symtab_tags tag,
1207 struct bitpack_d *bp, bool *has_thunk_info)
1208 {
1209 node->aux = (void *) tag;
1210 node->lto_file_data = file_data;
1211
1212 node->local = bp_unpack_value (bp, 1);
1213 node->externally_visible = bp_unpack_value (bp, 1);
1214 node->no_reorder = bp_unpack_value (bp, 1);
1215 node->definition = bp_unpack_value (bp, 1);
1216 node->versionable = bp_unpack_value (bp, 1);
1217 node->can_change_signature = bp_unpack_value (bp, 1);
1218 node->redefined_extern_inline = bp_unpack_value (bp, 1);
1219 node->force_output = bp_unpack_value (bp, 1);
1220 node->forced_by_abi = bp_unpack_value (bp, 1);
1221 node->unique_name = bp_unpack_value (bp, 1);
1222 node->body_removed = bp_unpack_value (bp, 1);
1223 node->semantic_interposition = bp_unpack_value (bp, 1);
1224 node->implicit_section = bp_unpack_value (bp, 1);
1225 node->address_taken = bp_unpack_value (bp, 1);
1226 node->used_from_other_partition = bp_unpack_value (bp, 1);
1227 node->lowered = bp_unpack_value (bp, 1);
1228 node->analyzed = tag == LTO_symtab_analyzed_node;
1229 node->in_other_partition = bp_unpack_value (bp, 1);
1230 if (node->in_other_partition
1231 /* Avoid updating decl when we are seeing just inline clone.
1232 When inlining function that has functions already inlined into it,
1233 we produce clones of inline clones.
1234
1235 WPA partitioning might put each clone into different unit and
1236 we might end up streaming inline clone from other partition
1237 to support clone we are interested in. */
1238 && (!node->clone_of
1239 || node->clone_of->decl != node->decl))
1240 {
1241 DECL_EXTERNAL (node->decl) = 1;
1242 TREE_STATIC (node->decl) = 0;
1243 }
1244 node->alias = bp_unpack_value (bp, 1);
1245 node->transparent_alias = bp_unpack_value (bp, 1);
1246 node->weakref = bp_unpack_value (bp, 1);
1247 node->symver = bp_unpack_value (bp, 1);
1248 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
1249 node->only_called_at_startup = bp_unpack_value (bp, 1);
1250 node->only_called_at_exit = bp_unpack_value (bp, 1);
1251 node->tm_clone = bp_unpack_value (bp, 1);
1252 node->calls_comdat_local = bp_unpack_value (bp, 1);
1253 node->icf_merged = bp_unpack_value (bp, 1);
1254 node->nonfreeing_fn = bp_unpack_value (bp, 1);
1255 node->merged_comdat = bp_unpack_value (bp, 1);
1256 node->merged_extern_inline = bp_unpack_value (bp, 1);
1257 node->thunk = bp_unpack_value (bp, 1);
1258 node->parallelized_function = bp_unpack_value (bp, 1);
1259 node->has_omp_variant_constructs = bp_unpack_value (bp, 1);
1260 *has_thunk_info = bp_unpack_value (bp, 1);
1261 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
1262 LDPR_NUM_KNOWN);
1263 node->split_part = bp_unpack_value (bp, 1);
1264 verify_node_partition (node);
1265 }
1266
1267 /* Return string alias is alias of. */
1268
1269 static tree
1270 get_alias_symbol (tree decl)
1271 {
1272 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1273 return get_identifier (TREE_STRING_POINTER
1274 (TREE_VALUE (TREE_VALUE (alias))));
1275 }
1276
1277 /* Read a node from input_block IB. TAG is the node's tag just read.
1278 Return the node read or overwriten. */
1279
1280 static struct cgraph_node *
1281 input_node (struct lto_file_decl_data *file_data,
1282 class lto_input_block *ib,
1283 enum LTO_symtab_tags tag,
1284 vec<symtab_node *> nodes)
1285 {
1286 gcc::pass_manager *passes = g->get_passes ();
1287 tree fn_decl;
1288 struct cgraph_node *node;
1289 struct bitpack_d bp;
1290 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1291 int clone_ref;
1292 int order;
1293 int i, count;
1294 tree group;
1295 const char *section;
1296 order = streamer_read_hwi (ib) + file_data->order_base;
1297 clone_ref = streamer_read_hwi (ib);
1298 bool has_thunk_info;
1299
1300 fn_decl = lto_input_fn_decl_ref (ib, file_data);
1301
1302 if (clone_ref != LCC_NOT_FOUND)
1303 {
1304 node = dyn_cast<cgraph_node *> (nodes[clone_ref])->create_clone (fn_decl,
1305 profile_count::uninitialized (), false,
1306 vNULL, false, NULL, NULL);
1307 }
1308 else
1309 {
1310 /* Declaration of functions can be already merged with a declaration
1311 from other input file. We keep cgraph unmerged until after streaming
1312 of ipa passes is done. Alays forcingly create a fresh node. */
1313 node = symtab->create_empty ();
1314 node->decl = fn_decl;
1315 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (fn_decl)))
1316 node->ifunc_resolver = 1;
1317 node->register_symbol ();
1318 }
1319
1320 node->order = order;
1321 if (order >= symtab->order)
1322 symtab->order = order + 1;
1323
1324 node->count = profile_count::stream_in (ib);
1325 node->count_materialization_scale = streamer_read_hwi (ib);
1326
1327 count = streamer_read_hwi (ib);
1328 node->ipa_transforms_to_apply = vNULL;
1329 for (i = 0; i < count; i++)
1330 {
1331 opt_pass *pass;
1332 int pid = streamer_read_hwi (ib);
1333
1334 gcc_assert (pid < passes->passes_by_id_size);
1335 pass = passes->passes_by_id[pid];
1336 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *) pass);
1337 }
1338
1339 if (tag == LTO_symtab_analyzed_node)
1340 ref = streamer_read_hwi (ib);
1341
1342 group = read_identifier (ib);
1343 if (group)
1344 ref2 = streamer_read_hwi (ib);
1345
1346 /* Make sure that we have not read this node before. Nodes that
1347 have already been read will have their tag stored in the 'aux'
1348 field. Since built-in functions can be referenced in multiple
1349 functions, they are expected to be read more than once. */
1350 if (node->aux && !fndecl_built_in_p (node->decl))
1351 internal_error ("bytecode stream: found multiple instances of cgraph "
1352 "node with uid %d", node->get_uid ());
1353
1354 node->tp_first_run = streamer_read_uhwi (ib);
1355
1356 bp = streamer_read_bitpack (ib);
1357
1358 input_overwrite_node (file_data, node, tag, &bp, &has_thunk_info);
1359
1360 /* Store a reference for now, and fix up later to be a pointer. */
1361 node->inlined_to = (cgraph_node *) (intptr_t) ref;
1362
1363 if (group)
1364 {
1365 node->set_comdat_group (group);
1366 /* Store a reference for now, and fix up later to be a pointer. */
1367 node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
1368 }
1369 else
1370 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1371 section = read_string (ib);
1372 if (section)
1373 node->set_section_for_node (section);
1374
1375 if (node->alias && !node->analyzed && node->weakref)
1376 node->alias_target = get_alias_symbol (node->decl);
1377 node->profile_id = streamer_read_hwi (ib);
1378 node->unit_id = streamer_read_hwi (ib) + file_data->unit_base;
1379 if (symtab->max_unit < node->unit_id)
1380 symtab->max_unit = node->unit_id;
1381 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1382 node->set_init_priority (streamer_read_hwi (ib));
1383 if (DECL_STATIC_DESTRUCTOR (node->decl))
1384 node->set_fini_priority (streamer_read_hwi (ib));
1385
1386 if (has_thunk_info)
1387 thunk_info::get_create (node)->stream_in (ib);
1388
1389 return node;
1390 }
1391
1392 /* Read a node from input_block IB. TAG is the node's tag just read.
1393 Return the node read or overwriten. */
1394
1395 static varpool_node *
1396 input_varpool_node (struct lto_file_decl_data *file_data,
1397 class lto_input_block *ib)
1398 {
1399 tree var_decl;
1400 varpool_node *node;
1401 struct bitpack_d bp;
1402 int ref = LCC_NOT_FOUND;
1403 int order;
1404 tree group;
1405 const char *section;
1406
1407 order = streamer_read_hwi (ib) + file_data->order_base;
1408 var_decl = lto_input_var_decl_ref (ib, file_data);
1409
1410 /* Declaration of functions can be already merged with a declaration
1411 from other input file. We keep cgraph unmerged until after streaming
1412 of ipa passes is done. Alays forcingly create a fresh node. */
1413 node = varpool_node::create_empty ();
1414 node->decl = var_decl;
1415 node->register_symbol ();
1416
1417 node->order = order;
1418 if (order >= symtab->order)
1419 symtab->order = order + 1;
1420 node->lto_file_data = file_data;
1421
1422 bp = streamer_read_bitpack (ib);
1423 node->externally_visible = bp_unpack_value (&bp, 1);
1424 node->no_reorder = bp_unpack_value (&bp, 1);
1425 node->force_output = bp_unpack_value (&bp, 1);
1426 node->forced_by_abi = bp_unpack_value (&bp, 1);
1427 node->unique_name = bp_unpack_value (&bp, 1);
1428 node->body_removed = bp_unpack_value (&bp, 1);
1429 node->semantic_interposition = bp_unpack_value (&bp, 1);
1430 node->implicit_section = bp_unpack_value (&bp, 1);
1431 node->writeonly = bp_unpack_value (&bp, 1);
1432 node->definition = bp_unpack_value (&bp, 1);
1433 node->alias = bp_unpack_value (&bp, 1);
1434 node->transparent_alias = bp_unpack_value (&bp, 1);
1435 node->weakref = bp_unpack_value (&bp, 1);
1436 node->symver = bp_unpack_value (&bp, 1);
1437 node->analyzed = bp_unpack_value (&bp, 1);
1438 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1439 node->in_other_partition = bp_unpack_value (&bp, 1);
1440 if (node->in_other_partition)
1441 {
1442 DECL_EXTERNAL (node->decl) = 1;
1443 TREE_STATIC (node->decl) = 0;
1444 }
1445 if (node->alias && !node->analyzed && node->weakref)
1446 node->alias_target = get_alias_symbol (node->decl);
1447 node->tls_model = (enum tls_model)bp_unpack_value (&bp, 3);
1448 node->used_by_single_function = (enum tls_model)bp_unpack_value (&bp, 1);
1449 node->dynamically_initialized = bp_unpack_value (&bp, 1);
1450 group = read_identifier (ib);
1451 if (group)
1452 {
1453 node->set_comdat_group (group);
1454 ref = streamer_read_hwi (ib);
1455 /* Store a reference for now, and fix up later to be a pointer. */
1456 node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1457 }
1458 else
1459 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1460 section = read_string (ib);
1461 if (section)
1462 node->set_section_for_node (section);
1463 node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1464 LDPR_NUM_KNOWN);
1465 verify_node_partition (node);
1466 return node;
1467 }
1468
1469 /* Read a node from input_block IB. TAG is the node's tag just read.
1470 Return the node read or overwriten. */
1471
1472 static void
1473 input_ref (class lto_input_block *ib,
1474 symtab_node *referring_node,
1475 vec<symtab_node *> nodes)
1476 {
1477 symtab_node *node = NULL;
1478 struct bitpack_d bp;
1479 enum ipa_ref_use use;
1480 bool speculative;
1481 struct ipa_ref *ref;
1482
1483 bp = streamer_read_bitpack (ib);
1484 use = (enum ipa_ref_use) bp_unpack_value (&bp, 3);
1485 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1486 node = nodes[streamer_read_hwi (ib)];
1487 ref = referring_node->create_reference (node, use);
1488 ref->speculative = speculative;
1489 if (is_a <cgraph_node *> (referring_node))
1490 {
1491 ref->lto_stmt_uid = streamer_read_hwi (ib);
1492 bp = streamer_read_bitpack (ib);
1493 ref->speculative_id = bp_unpack_value (&bp, 16);
1494 }
1495 }
1496
1497 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1498 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1499 edge being read is indirect (in the sense that it has
1500 indirect_unknown_callee set). */
1501
1502 static void
1503 input_edge (class lto_input_block *ib, vec<symtab_node *> nodes,
1504 bool indirect)
1505 {
1506 struct cgraph_node *caller, *callee;
1507 struct cgraph_edge *edge;
1508 unsigned int stmt_id, speculative_id;
1509 profile_count count;
1510 cgraph_inline_failed_t inline_failed;
1511 struct bitpack_d bp;
1512 int ecf_flags = 0;
1513
1514 caller = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1515 if (caller == NULL || caller->decl == NULL_TREE)
1516 internal_error ("bytecode stream: no caller found while reading edge");
1517
1518 if (!indirect)
1519 {
1520 callee = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1521 if (callee == NULL || callee->decl == NULL_TREE)
1522 internal_error ("bytecode stream: no callee found while reading edge");
1523 }
1524 else
1525 callee = NULL;
1526
1527 count = profile_count::stream_in (ib);
1528
1529 bp = streamer_read_bitpack (ib);
1530 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_t, CIF_N_REASONS);
1531 stmt_id = bp_unpack_var_len_unsigned (&bp);
1532 speculative_id = bp_unpack_value (&bp, 16);
1533
1534 if (indirect)
1535 edge = caller->create_indirect_edge (NULL, 0, count);
1536 else
1537 edge = caller->create_edge (callee, NULL, count);
1538
1539 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1540 edge->speculative = bp_unpack_value (&bp, 1);
1541 edge->lto_stmt_uid = stmt_id;
1542 edge->speculative_id = speculative_id;
1543 edge->inline_failed = inline_failed;
1544 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1545 edge->can_throw_external = bp_unpack_value (&bp, 1);
1546 edge->in_polymorphic_cdtor = bp_unpack_value (&bp, 1);
1547 if (indirect)
1548 {
1549 if (bp_unpack_value (&bp, 1))
1550 ecf_flags |= ECF_CONST;
1551 if (bp_unpack_value (&bp, 1))
1552 ecf_flags |= ECF_PURE;
1553 if (bp_unpack_value (&bp, 1))
1554 ecf_flags |= ECF_NORETURN;
1555 if (bp_unpack_value (&bp, 1))
1556 ecf_flags |= ECF_MALLOC;
1557 if (bp_unpack_value (&bp, 1))
1558 ecf_flags |= ECF_NOTHROW;
1559 if (bp_unpack_value (&bp, 1))
1560 ecf_flags |= ECF_RETURNS_TWICE;
1561 edge->indirect_info->ecf_flags = ecf_flags;
1562
1563 edge->indirect_info->num_speculative_call_targets
1564 = bp_unpack_value (&bp, 16);
1565 }
1566 }
1567
1568
1569 /* Read a cgraph from IB using the info in FILE_DATA. */
1570
1571 static vec<symtab_node *>
1572 input_cgraph_1 (struct lto_file_decl_data *file_data,
1573 class lto_input_block *ib)
1574 {
1575 enum LTO_symtab_tags tag;
1576 vec<symtab_node *> nodes = vNULL;
1577 symtab_node *node;
1578 unsigned i;
1579
1580 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1581 file_data->order_base = symtab->order;
1582 file_data->unit_base = symtab->max_unit + 1;
1583 while (tag)
1584 {
1585 if (tag == LTO_symtab_edge)
1586 input_edge (ib, nodes, false);
1587 else if (tag == LTO_symtab_indirect_edge)
1588 input_edge (ib, nodes, true);
1589 else if (tag == LTO_symtab_variable)
1590 {
1591 node = input_varpool_node (file_data, ib);
1592 nodes.safe_push (node);
1593 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1594 }
1595 else
1596 {
1597 node = input_node (file_data, ib, tag, nodes);
1598 if (node == NULL || node->decl == NULL_TREE)
1599 internal_error ("bytecode stream: found empty cgraph node");
1600 nodes.safe_push (node);
1601 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1602 }
1603
1604 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1605 }
1606
1607 lto_input_toplevel_asms (file_data, file_data->order_base);
1608
1609 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1610 if (flag_checking)
1611 {
1612 FOR_EACH_VEC_ELT (nodes, i, node)
1613 gcc_assert (node->aux || !is_a <cgraph_node *> (node));
1614 }
1615 FOR_EACH_VEC_ELT (nodes, i, node)
1616 {
1617 int ref;
1618 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1619 {
1620 ref = (int) (intptr_t) cnode->inlined_to;
1621
1622 /* We share declaration of builtins, so we may read same node twice. */
1623 if (!node->aux)
1624 continue;
1625 node->aux = NULL;
1626
1627 /* Fixup inlined_to from reference to pointer. */
1628 if (ref != LCC_NOT_FOUND)
1629 dyn_cast<cgraph_node *> (node)->inlined_to
1630 = dyn_cast<cgraph_node *> (nodes[ref]);
1631 else
1632 cnode->inlined_to = NULL;
1633 }
1634
1635 ref = (int) (intptr_t) node->same_comdat_group;
1636
1637 /* Fixup same_comdat_group from reference to pointer. */
1638 if (ref != LCC_NOT_FOUND)
1639 node->same_comdat_group = nodes[ref];
1640 else
1641 node->same_comdat_group = NULL;
1642 }
1643 FOR_EACH_VEC_ELT (nodes, i, node)
1644 node->aux = is_a <cgraph_node *> (node) ? (void *)1 : NULL;
1645 return nodes;
1646 }
1647
1648 /* Input ipa_refs. */
1649
1650 static void
1651 input_refs (class lto_input_block *ib,
1652 vec<symtab_node *> nodes)
1653 {
1654 int count;
1655 int idx;
1656 while (true)
1657 {
1658 symtab_node *node;
1659 count = streamer_read_uhwi (ib);
1660 if (!count)
1661 break;
1662 idx = streamer_read_uhwi (ib);
1663 node = nodes[idx];
1664 while (count)
1665 {
1666 input_ref (ib, node, nodes);
1667 count--;
1668 }
1669 }
1670 }
1671
1672 /* Input profile_info from IB. */
1673 static void
1674 input_profile_summary (class lto_input_block *ib,
1675 struct lto_file_decl_data *file_data)
1676 {
1677 unsigned int runs = streamer_read_uhwi (ib);
1678 if (runs)
1679 {
1680 file_data->profile_info.runs = runs;
1681
1682 /* IPA-profile computes hot bb threshold based on cumulated
1683 whole program profile. We need to stream it down to ltrans. */
1684 if (flag_ltrans)
1685 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1686 }
1687
1688 }
1689
1690 /* Rescale profile summaries to the same number of runs in the whole unit. */
1691
1692 static void
1693 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1694 {
1695 struct lto_file_decl_data *file_data;
1696 unsigned int j;
1697 gcov_unsigned_t max_runs = 0;
1698 struct cgraph_node *node;
1699 struct cgraph_edge *edge;
1700
1701 /* Find unit with maximal number of runs. If we ever get serious about
1702 roundoff errors, we might also consider computing smallest common
1703 multiply. */
1704 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1705 if (max_runs < file_data->profile_info.runs)
1706 max_runs = file_data->profile_info.runs;
1707
1708 if (!max_runs)
1709 return;
1710
1711 /* Simple overflow check. We probably don't need to support that many train
1712 runs. Such a large value probably imply data corruption anyway. */
1713 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1714 {
1715 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1716 INT_MAX / REG_BR_PROB_BASE);
1717 return;
1718 }
1719
1720 profile_info = XCNEW (gcov_summary);
1721 profile_info->runs = max_runs;
1722
1723 /* If merging already happent at WPA time, we are done. */
1724 if (flag_ltrans)
1725 return;
1726
1727 /* Now compute count_materialization_scale of each node.
1728 During LTRANS we already have values of count_materialization_scale
1729 computed, so just update them. */
1730 FOR_EACH_FUNCTION (node)
1731 if (node->lto_file_data
1732 && node->lto_file_data->profile_info.runs)
1733 {
1734 int scale;
1735
1736 scale = RDIV (node->count_materialization_scale * max_runs,
1737 node->lto_file_data->profile_info.runs);
1738 node->count_materialization_scale = scale;
1739 if (scale < 0)
1740 fatal_error (input_location, "Profile information in %s corrupted",
1741 file_data->file_name);
1742
1743 if (scale == REG_BR_PROB_BASE)
1744 continue;
1745 for (edge = node->callees; edge; edge = edge->next_callee)
1746 if (edge->count.ipa ().nonzero_p ())
1747 edge->count = edge->count.apply_scale (scale, REG_BR_PROB_BASE);
1748 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1749 if (edge->count.ipa ().nonzero_p ())
1750 edge->count = edge->count.apply_scale (scale, REG_BR_PROB_BASE);
1751 if (node->count.ipa ().nonzero_p ())
1752 node->count = node->count.apply_scale (scale, REG_BR_PROB_BASE);
1753 }
1754 }
1755
1756 /* Input and merge the symtab from each of the .o files passed to
1757 lto1. */
1758
1759 void
1760 input_symtab (void)
1761 {
1762 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1763 struct lto_file_decl_data *file_data;
1764 unsigned int j = 0;
1765 struct cgraph_node *node;
1766
1767 while ((file_data = file_data_vec[j++]))
1768 {
1769 const char *data;
1770 size_t len;
1771 class lto_input_block *ib;
1772 vec<symtab_node *> nodes;
1773
1774 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1775 &data, &len);
1776 if (!ib)
1777 fatal_error (input_location,
1778 "cannot find LTO cgraph in %s", file_data->file_name);
1779 input_profile_summary (ib, file_data);
1780 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1781 nodes = input_cgraph_1 (file_data, ib);
1782 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1783 ib, data, len);
1784
1785 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1786 &data, &len);
1787 if (!ib)
1788 fatal_error (input_location, "cannot find LTO section refs in %s",
1789 file_data->file_name);
1790 input_refs (ib, nodes);
1791 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1792 ib, data, len);
1793 if (flag_ltrans)
1794 input_cgraph_opt_summary (nodes);
1795 nodes.release ();
1796 }
1797
1798 merge_profile_summaries (file_data_vec);
1799
1800 /* Clear out the aux field that was used to store enough state to
1801 tell which nodes should be overwritten. */
1802 FOR_EACH_FUNCTION (node)
1803 {
1804 /* Some nodes may have been created by cgraph_node. This
1805 happens when the callgraph contains nested functions. If the
1806 node for the parent function was never emitted to the gimple
1807 file, cgraph_node will create a node for it when setting the
1808 context of the nested function. */
1809 if (node->lto_file_data)
1810 node->aux = NULL;
1811 }
1812 }
1813
1814 static void
1815 omp_requires_to_name (char *buf, size_t size, HOST_WIDE_INT requires_mask)
1816 {
1817 char *end = buf + size, *p = buf;
1818 if (requires_mask & GOMP_REQUIRES_UNIFIED_ADDRESS)
1819 p += snprintf (p, end - p, "unified_address");
1820 if (requires_mask & GOMP_REQUIRES_UNIFIED_SHARED_MEMORY)
1821 p += snprintf (p, end - p, "%sunified_shared_memory",
1822 (p == buf ? "" : ", "));
1823 if (requires_mask & GOMP_REQUIRES_SELF_MAPS)
1824 p += snprintf (p, end - p, "%sself_maps",
1825 (p == buf ? "" : ", "));
1826 if (requires_mask & GOMP_REQUIRES_REVERSE_OFFLOAD)
1827 p += snprintf (p, end - p, "%sreverse_offload",
1828 (p == buf ? "" : ", "));
1829 }
1830
1831 /* Input function/variable tables that will allow libgomp to look up offload
1832 target code, and store them into OFFLOAD_FUNCS and OFFLOAD_VARS. */
1833
1834 void
1835 input_offload_tables (bool do_force_output)
1836 {
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 const char *requires_fn = NULL;
1841 tree requires_decl = NULL_TREE;
1842
1843 omp_requires_mask = (omp_requires) 0;
1844
1845 while ((file_data = file_data_vec[j++]))
1846 {
1847 const char *data;
1848 size_t len;
1849 class lto_input_block *ib
1850 = lto_create_simple_input_block (file_data, LTO_section_offload_table,
1851 &data, &len);
1852 if (!ib)
1853 continue;
1854
1855 tree tmp_decl = NULL_TREE;
1856 enum LTO_symtab_tags tag
1857 = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1858 while (tag)
1859 {
1860 if (tag == LTO_symtab_unavail_node)
1861 {
1862 tree fn_decl
1863 = lto_input_fn_decl_ref (ib, file_data);
1864 vec_safe_push (offload_funcs, fn_decl);
1865
1866 /* Prevent IPA from removing fn_decl as unreachable, since there
1867 may be no refs from the parent function to child_fn in offload
1868 LTO mode. */
1869 if (do_force_output)
1870 cgraph_node::get (fn_decl)->mark_force_output ();
1871 tmp_decl = fn_decl;
1872 }
1873 else if (tag == LTO_symtab_variable)
1874 {
1875 tree var_decl
1876 = lto_input_var_decl_ref (ib, file_data);
1877 vec_safe_push (offload_vars, var_decl);
1878
1879 /* Prevent IPA from removing var_decl as unused, since there
1880 may be no refs to var_decl in offload LTO mode. */
1881 if (do_force_output)
1882 varpool_node::get (var_decl)->force_output = 1;
1883 tmp_decl = var_decl;
1884 }
1885 else if (tag == LTO_symtab_indirect_function)
1886 {
1887 tree fn_decl
1888 = lto_input_fn_decl_ref (ib, file_data);
1889 vec_safe_push (offload_ind_funcs, fn_decl);
1890
1891 /* Prevent IPA from removing fn_decl as unreachable, since there
1892 may be no refs from the parent function to child_fn in offload
1893 LTO mode. */
1894 if (do_force_output)
1895 cgraph_node::get (fn_decl)->mark_force_output ();
1896 tmp_decl = fn_decl;
1897 }
1898 else if (tag == LTO_symtab_edge)
1899 {
1900 static bool error_emitted = false;
1901 HOST_WIDE_INT val = streamer_read_hwi (ib);
1902
1903 if (omp_requires_mask == 0)
1904 {
1905 omp_requires_mask = (omp_requires) val;
1906 requires_decl = tmp_decl;
1907 requires_fn = file_data->file_name;
1908 }
1909 else if (omp_requires_mask != val && !error_emitted)
1910 {
1911 const char *fn1 = requires_fn;
1912 if (requires_decl != NULL_TREE)
1913 {
1914 while (DECL_CONTEXT (requires_decl) != NULL_TREE
1915 && TREE_CODE (requires_decl) != TRANSLATION_UNIT_DECL)
1916 requires_decl = DECL_CONTEXT (requires_decl);
1917 if (requires_decl != NULL_TREE)
1918 fn1 = IDENTIFIER_POINTER (DECL_NAME (requires_decl));
1919 }
1920
1921 const char *fn2 = file_data->file_name;
1922 if (tmp_decl != NULL_TREE)
1923 {
1924 while (DECL_CONTEXT (tmp_decl) != NULL_TREE
1925 && TREE_CODE (tmp_decl) != TRANSLATION_UNIT_DECL)
1926 tmp_decl = DECL_CONTEXT (tmp_decl);
1927 if (tmp_decl != NULL_TREE)
1928 fn2 = IDENTIFIER_POINTER (DECL_NAME (tmp_decl));
1929 }
1930 if (fn1 == fn2)
1931 {
1932 fn1 = requires_fn;
1933 fn2 = file_data->file_name;
1934 }
1935
1936 char buf1[sizeof ("unified_address, unified_shared_memory, "
1937 "reverse_offload")];
1938 char buf2[sizeof ("unified_address, unified_shared_memory, "
1939 "reverse_offload")];
1940 omp_requires_to_name (buf2, sizeof (buf2),
1941 val != OMP_REQUIRES_TARGET_USED
1942 ? val
1943 : (HOST_WIDE_INT) omp_requires_mask);
1944 if (val != OMP_REQUIRES_TARGET_USED
1945 && omp_requires_mask != OMP_REQUIRES_TARGET_USED)
1946 {
1947 omp_requires_to_name (buf1, sizeof (buf1),
1948 omp_requires_mask);
1949 error ("OpenMP %<requires%> directive with non-identical "
1950 "clauses in multiple compilation units: %qs vs. "
1951 "%qs", buf1, buf2);
1952 inform (UNKNOWN_LOCATION, "%qs has %qs", fn1, buf1);
1953 inform (UNKNOWN_LOCATION, "%qs has %qs", fn2, buf2);
1954 }
1955 else
1956 {
1957 error ("OpenMP %<requires%> directive with %qs specified "
1958 "only in some compilation units", buf2);
1959 inform (UNKNOWN_LOCATION, "%qs has %qs",
1960 val != OMP_REQUIRES_TARGET_USED ? fn2 : fn1,
1961 buf2);
1962 inform (UNKNOWN_LOCATION, "but %qs has not",
1963 val != OMP_REQUIRES_TARGET_USED ? fn1 : fn2);
1964 }
1965 error_emitted = true;
1966 }
1967 }
1968 else
1969 fatal_error (input_location,
1970 "invalid offload table in %s", file_data->file_name);
1971
1972 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1973 }
1974
1975 lto_destroy_simple_input_block (file_data, LTO_section_offload_table,
1976 ib, data, len);
1977 }
1978 #ifdef ACCEL_COMPILER
1979 char *omp_requires_file = getenv ("GCC_OFFLOAD_OMP_REQUIRES_FILE");
1980 if (omp_requires_file == NULL || omp_requires_file[0] == '\0')
1981 fatal_error (input_location, "GCC_OFFLOAD_OMP_REQUIRES_FILE unset");
1982 FILE *f = fopen (omp_requires_file, "wb");
1983 if (!f)
1984 fatal_error (input_location, "Cannot open omp_requires file %qs",
1985 omp_requires_file);
1986 uint32_t req_mask = omp_requires_mask;
1987 fwrite (&req_mask, sizeof (req_mask), 1, f);
1988 fclose (f);
1989 #endif
1990 }
1991
1992 /* True when we need optimization summary for NODE. */
1993
1994 static int
1995 output_cgraph_opt_summary_p (struct cgraph_node *node)
1996 {
1997 if (node->clone_of || node->former_clone_of)
1998 return true;
1999 clone_info *info = clone_info::get (node);
2000 return info && (info->tree_map || info->param_adjustments);
2001 }
2002
2003 /* Output optimization summary for EDGE to OB. */
2004 static void
2005 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
2006 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
2007 {
2008 }
2009
2010 /* Output optimization summary for NODE to OB. */
2011
2012 static void
2013 output_node_opt_summary (struct output_block *ob,
2014 struct cgraph_node *node,
2015 lto_symtab_encoder_t encoder)
2016 {
2017 struct ipa_replace_map *map;
2018 int i;
2019 struct cgraph_edge *e;
2020
2021 /* TODO: Should this code be moved to ipa-param-manipulation? */
2022 struct bitpack_d bp;
2023 bp = bitpack_create (ob->main_stream);
2024 clone_info *info = clone_info::get (node);
2025
2026 bp_pack_value (&bp, (info && info->param_adjustments != NULL), 1);
2027 streamer_write_bitpack (&bp);
2028 if (ipa_param_adjustments *adjustments
2029 = info ? info->param_adjustments : NULL)
2030 {
2031 streamer_write_uhwi (ob, vec_safe_length (adjustments->m_adj_params));
2032 ipa_adjusted_param *adj;
2033 FOR_EACH_VEC_SAFE_ELT (adjustments->m_adj_params, i, adj)
2034 {
2035 bp = bitpack_create (ob->main_stream);
2036 bp_pack_value (&bp, adj->base_index, IPA_PARAM_MAX_INDEX_BITS);
2037 bp_pack_value (&bp, adj->prev_clone_index, IPA_PARAM_MAX_INDEX_BITS);
2038 bp_pack_value (&bp, adj->op, 2);
2039 bp_pack_value (&bp, adj->param_prefix_index, 2);
2040 bp_pack_value (&bp, adj->prev_clone_adjustment, 1);
2041 bp_pack_value (&bp, adj->reverse, 1);
2042 bp_pack_value (&bp, adj->user_flag, 1);
2043 streamer_write_bitpack (&bp);
2044 if (adj->op == IPA_PARAM_OP_SPLIT
2045 || adj->op == IPA_PARAM_OP_NEW)
2046 {
2047 stream_write_tree (ob, adj->type, true);
2048 if (adj->op == IPA_PARAM_OP_SPLIT)
2049 {
2050 stream_write_tree (ob, adj->alias_ptr_type, true);
2051 streamer_write_uhwi (ob, adj->unit_offset);
2052 }
2053 }
2054 }
2055 streamer_write_hwi (ob, adjustments->m_always_copy_start);
2056 bp = bitpack_create (ob->main_stream);
2057 bp_pack_value (&bp, info->param_adjustments->m_skip_return, 1);
2058 streamer_write_bitpack (&bp);
2059 }
2060
2061 streamer_write_uhwi (ob, info ? vec_safe_length (info->tree_map) : 0);
2062 if (info)
2063 FOR_EACH_VEC_SAFE_ELT (info->tree_map, i, map)
2064 {
2065 streamer_write_uhwi (ob, map->parm_num);
2066 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
2067 stream_write_tree (ob, map->new_tree, true);
2068 }
2069
2070 if (lto_symtab_encoder_in_partition_p (encoder, node))
2071 {
2072 for (e = node->callees; e; e = e->next_callee)
2073 output_edge_opt_summary (ob, e);
2074 for (e = node->indirect_calls; e; e = e->next_callee)
2075 output_edge_opt_summary (ob, e);
2076 }
2077 }
2078
2079 /* Output optimization summaries stored in callgraph.
2080 At the moment it is the clone info structure. */
2081
2082 static void
2083 output_cgraph_opt_summary (void)
2084 {
2085 int i, n_nodes;
2086 lto_symtab_encoder_t encoder;
2087 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
2088 unsigned count = 0;
2089
2090 ob->symbol = NULL;
2091 encoder = ob->decl_state->symtab_node_encoder;
2092 n_nodes = lto_symtab_encoder_size (encoder);
2093 for (i = 0; i < n_nodes; i++)
2094 {
2095 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
2096 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2097 if (cnode && output_cgraph_opt_summary_p (cnode))
2098 count++;
2099 }
2100 streamer_write_uhwi (ob, count);
2101 for (i = 0; i < n_nodes; i++)
2102 {
2103 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
2104 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2105 if (cnode && output_cgraph_opt_summary_p (cnode))
2106 {
2107 streamer_write_uhwi (ob, i);
2108 output_node_opt_summary (ob, cnode, encoder);
2109 }
2110 }
2111 produce_asm (ob);
2112 destroy_output_block (ob);
2113 }
2114
2115 /* Input optimisation summary of EDGE. */
2116
2117 static void
2118 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
2119 class lto_input_block *ib_main ATTRIBUTE_UNUSED)
2120 {
2121 }
2122
2123 /* Input optimisation summary of NODE. */
2124
2125 static void
2126 input_node_opt_summary (struct cgraph_node *node,
2127 class lto_input_block *ib_main,
2128 class data_in *data_in)
2129 {
2130 int i;
2131 int count;
2132 struct cgraph_edge *e;
2133
2134 /* TODO: Should this code be moved to ipa-param-manipulation? */
2135 struct bitpack_d bp;
2136 bp = streamer_read_bitpack (ib_main);
2137 bool have_adjustments = bp_unpack_value (&bp, 1);
2138 clone_info *info = clone_info::get_create (node);
2139
2140 if (have_adjustments)
2141 {
2142 count = streamer_read_uhwi (ib_main);
2143 vec<ipa_adjusted_param, va_gc> *new_params = NULL;
2144 for (i = 0; i < count; i++)
2145 {
2146 ipa_adjusted_param adj;
2147 memset (&adj, 0, sizeof (adj));
2148 bp = streamer_read_bitpack (ib_main);
2149 adj.base_index = bp_unpack_value (&bp, IPA_PARAM_MAX_INDEX_BITS);
2150 adj.prev_clone_index
2151 = bp_unpack_value (&bp, IPA_PARAM_MAX_INDEX_BITS);
2152 adj.op = (enum ipa_parm_op) bp_unpack_value (&bp, 2);
2153 adj.param_prefix_index = bp_unpack_value (&bp, 2);
2154 adj.prev_clone_adjustment = bp_unpack_value (&bp, 1);
2155 adj.reverse = bp_unpack_value (&bp, 1);
2156 adj.user_flag = bp_unpack_value (&bp, 1);
2157 if (adj.op == IPA_PARAM_OP_SPLIT
2158 || adj.op == IPA_PARAM_OP_NEW)
2159 {
2160 adj.type = stream_read_tree (ib_main, data_in);
2161 if (adj.op == IPA_PARAM_OP_SPLIT)
2162 {
2163 adj.alias_ptr_type = stream_read_tree (ib_main, data_in);
2164 adj.unit_offset = streamer_read_uhwi (ib_main);
2165 }
2166 }
2167 vec_safe_push (new_params, adj);
2168 }
2169 int always_copy_start = streamer_read_hwi (ib_main);
2170 bp = streamer_read_bitpack (ib_main);
2171 bool skip_return = bp_unpack_value (&bp, 1);
2172 info->param_adjustments
2173 = (new (ggc_alloc <ipa_param_adjustments> ())
2174 ipa_param_adjustments (new_params, always_copy_start, skip_return));
2175 }
2176
2177 count = streamer_read_uhwi (ib_main);
2178 for (i = 0; i < count; i++)
2179 {
2180 struct ipa_replace_map *map = ggc_alloc<ipa_replace_map> ();
2181
2182 vec_safe_push (info->tree_map, map);
2183 map->parm_num = streamer_read_uhwi (ib_main);
2184 map->new_tree = stream_read_tree (ib_main, data_in);
2185 }
2186 for (e = node->callees; e; e = e->next_callee)
2187 input_edge_opt_summary (e, ib_main);
2188 for (e = node->indirect_calls; e; e = e->next_callee)
2189 input_edge_opt_summary (e, ib_main);
2190 }
2191
2192 /* Read section in file FILE_DATA of length LEN with data DATA. */
2193
2194 static void
2195 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
2196 const char *data, size_t len,
2197 vec<symtab_node *> nodes)
2198 {
2199 const struct lto_function_header *header =
2200 (const struct lto_function_header *) data;
2201 const int cfg_offset = sizeof (struct lto_function_header);
2202 const int main_offset = cfg_offset + header->cfg_size;
2203 const int string_offset = main_offset + header->main_size;
2204 class data_in *data_in;
2205 unsigned int i;
2206 unsigned int count;
2207
2208 lto_input_block ib_main ((const char *) data + main_offset,
2209 header->main_size, file_data);
2210
2211 data_in =
2212 lto_data_in_create (file_data, (const char *) data + string_offset,
2213 header->string_size, vNULL);
2214 count = streamer_read_uhwi (&ib_main);
2215
2216 for (i = 0; i < count; i++)
2217 {
2218 int ref = streamer_read_uhwi (&ib_main);
2219 input_node_opt_summary (dyn_cast<cgraph_node *> (nodes[ref]),
2220 &ib_main, data_in);
2221 }
2222 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
2223 len);
2224 lto_data_in_delete (data_in);
2225 }
2226
2227 /* Input optimization summary of cgraph. */
2228
2229 static void
2230 input_cgraph_opt_summary (vec<symtab_node *> nodes)
2231 {
2232 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2233 struct lto_file_decl_data *file_data;
2234 unsigned int j = 0;
2235
2236 while ((file_data = file_data_vec[j++]))
2237 {
2238 size_t len;
2239 const char *data
2240 = lto_get_summary_section_data (file_data, LTO_section_cgraph_opt_sum,
2241 &len);
2242 if (data)
2243 input_cgraph_opt_section (file_data, data, len, nodes);
2244 }
2245 }