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