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