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