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