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