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