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