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