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