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