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