Rebase.
[official-gcc.git] / gcc / lto-cgraph.c
blobf6aac3f920025f8f6c1d1377570a8537dd6d2f8c
1 /* Write and read the cgraph to the memory mapped representation of a
2 .o file.
4 Copyright (C) 2009-2014 Free Software Foundation, Inc.
5 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "stringpool.h"
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"
34 #include "gimple.h"
35 #include "expr.h"
36 #include "flags.h"
37 #include "params.h"
38 #include "input.h"
39 #include "hashtab.h"
40 #include "hash-set.h"
41 #include "langhooks.h"
42 #include "bitmap.h"
43 #include "function.h"
44 #include "diagnostic-core.h"
45 #include "except.h"
46 #include "timevar.h"
47 #include "lto-streamer.h"
48 #include "data-streamer.h"
49 #include "tree-streamer.h"
50 #include "gcov-io.h"
51 #include "tree-pass.h"
52 #include "profile.h"
53 #include "context.h"
54 #include "pass_manager.h"
55 #include "ipa-utils.h"
57 /* True when asm nodes has been output. */
58 bool asm_nodes_output = false;
60 static void output_cgraph_opt_summary (void);
61 static void input_cgraph_opt_summary (vec<symtab_node *> nodes);
63 /* Number of LDPR values known to GCC. */
64 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
66 /* All node orders are ofsetted by ORDER_BASE. */
67 static int order_base;
69 /* Cgraph streaming is organized as set of record whose type
70 is indicated by a tag. */
71 enum LTO_symtab_tags
73 /* Must leave 0 for the stopper. */
75 /* Cgraph node without body available. */
76 LTO_symtab_unavail_node = 1,
77 /* Cgraph node with function body. */
78 LTO_symtab_analyzed_node,
79 /* Cgraph edges. */
80 LTO_symtab_edge,
81 LTO_symtab_indirect_edge,
82 LTO_symtab_variable,
83 LTO_symtab_last_tag
86 /* Create a new symtab encoder.
87 if FOR_INPUT, the encoder allocate only datastructures needed
88 to read the symtab. */
90 lto_symtab_encoder_t
91 lto_symtab_encoder_new (bool for_input)
93 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
95 if (!for_input)
96 encoder->map = new hash_map<symtab_node *, size_t>;
97 encoder->nodes.create (0);
98 return encoder;
102 /* Delete ENCODER and its components. */
104 void
105 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
107 encoder->nodes.release ();
108 if (encoder->map)
109 delete encoder->map;
110 free (encoder);
114 /* Return the existing reference number of NODE in the symtab encoder in
115 output block OB. Assign a new reference if this is the first time
116 NODE is encoded. */
119 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
120 symtab_node *node)
122 int ref;
124 if (!encoder->map)
126 lto_encoder_entry entry = {node, false, false, false};
128 ref = encoder->nodes.length ();
129 encoder->nodes.safe_push (entry);
130 return ref;
133 size_t *slot = encoder->map->get (node);
134 if (!slot || !*slot)
136 lto_encoder_entry entry = {node, false, false, false};
137 ref = encoder->nodes.length ();
138 if (!slot)
139 encoder->map->put (node, ref + 1);
140 encoder->nodes.safe_push (entry);
142 else
143 ref = *slot - 1;
145 return ref;
148 /* Remove NODE from encoder. */
150 bool
151 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
152 symtab_node *node)
154 int index;
155 lto_encoder_entry last_node;
157 size_t *slot = encoder->map->get (node);
158 if (slot == NULL || !*slot)
159 return false;
161 index = *slot - 1;
162 gcc_checking_assert (encoder->nodes[index].node == node);
164 /* Remove from vector. We do this by swapping node with the last element
165 of the vector. */
166 last_node = encoder->nodes.pop ();
167 if (last_node.node != node)
169 gcc_assert (encoder->map->put (last_node.node, index + 1));
171 /* Move the last element to the original spot of NODE. */
172 encoder->nodes[index] = last_node;
175 /* Remove element from hash table. */
176 encoder->map->remove (node);
177 return true;
181 /* Return TRUE if we should encode initializer of NODE (if any). */
183 bool
184 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
185 struct cgraph_node *node)
187 int index = lto_symtab_encoder_lookup (encoder, node);
188 return encoder->nodes[index].body;
191 /* Return TRUE if we should encode body of NODE (if any). */
193 static void
194 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
195 struct cgraph_node *node)
197 int index = lto_symtab_encoder_encode (encoder, node);
198 gcc_checking_assert (encoder->nodes[index].node == node);
199 encoder->nodes[index].body = true;
202 /* Return TRUE if we should encode initializer of NODE (if any). */
204 bool
205 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
206 varpool_node *node)
208 int index = lto_symtab_encoder_lookup (encoder, node);
209 if (index == LCC_NOT_FOUND)
210 return false;
211 return encoder->nodes[index].initializer;
214 /* Return TRUE if we should encode initializer of NODE (if any). */
216 static void
217 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
218 varpool_node *node)
220 int index = lto_symtab_encoder_lookup (encoder, node);
221 encoder->nodes[index].initializer = true;
224 /* Return TRUE if we should encode initializer of NODE (if any). */
226 bool
227 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
228 symtab_node *node)
230 int index = lto_symtab_encoder_lookup (encoder, node);
231 if (index == LCC_NOT_FOUND)
232 return false;
233 return encoder->nodes[index].in_partition;
236 /* Return TRUE if we should encode body of NODE (if any). */
238 void
239 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
240 symtab_node *node)
242 int index = lto_symtab_encoder_encode (encoder, node);
243 encoder->nodes[index].in_partition = true;
246 /* Output the cgraph EDGE to OB using ENCODER. */
248 static void
249 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
250 lto_symtab_encoder_t encoder)
252 unsigned int uid;
253 intptr_t ref;
254 struct bitpack_d bp;
256 if (edge->indirect_unknown_callee)
257 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
258 LTO_symtab_indirect_edge);
259 else
260 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
261 LTO_symtab_edge);
263 ref = lto_symtab_encoder_lookup (encoder, edge->caller);
264 gcc_assert (ref != LCC_NOT_FOUND);
265 streamer_write_hwi_stream (ob->main_stream, ref);
267 if (!edge->indirect_unknown_callee)
269 ref = lto_symtab_encoder_lookup (encoder, edge->callee);
270 gcc_assert (ref != LCC_NOT_FOUND);
271 streamer_write_hwi_stream (ob->main_stream, ref);
274 streamer_write_gcov_count_stream (ob->main_stream, edge->count);
276 bp = bitpack_create (ob->main_stream);
277 uid = (!gimple_has_body_p (edge->caller->decl)
278 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt) + 1);
279 bp_pack_enum (&bp, cgraph_inline_failed_t,
280 CIF_N_REASONS, edge->inline_failed);
281 bp_pack_var_len_unsigned (&bp, uid);
282 bp_pack_var_len_unsigned (&bp, edge->frequency);
283 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
284 bp_pack_value (&bp, edge->speculative, 1);
285 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
286 bp_pack_value (&bp, edge->can_throw_external, 1);
287 if (edge->indirect_unknown_callee)
289 int flags = edge->indirect_info->ecf_flags;
290 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
291 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
292 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
293 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
294 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
295 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
296 /* Flags that should not appear on indirect calls. */
297 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
298 | ECF_MAY_BE_ALLOCA
299 | ECF_SIBCALL
300 | ECF_LEAF
301 | ECF_NOVOPS)));
303 streamer_write_bitpack (&bp);
304 if (edge->indirect_unknown_callee)
306 streamer_write_hwi_stream (ob->main_stream,
307 edge->indirect_info->common_target_id);
308 if (edge->indirect_info->common_target_id)
309 streamer_write_hwi_stream
310 (ob->main_stream, edge->indirect_info->common_target_probability);
314 /* Return if NODE contain references from other partitions. */
316 bool
317 referenced_from_other_partition_p (symtab_node *node, lto_symtab_encoder_t encoder)
319 int i;
320 struct ipa_ref *ref = NULL;
322 for (i = 0; node->iterate_referring (i, ref); i++)
324 if (ref->referring->in_other_partition
325 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
326 return true;
328 return false;
331 /* Return true when node is reachable from other partition. */
333 bool
334 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
336 struct cgraph_edge *e;
337 if (!node->definition)
338 return false;
339 if (node->global.inlined_to)
340 return false;
341 for (e = node->callers; e; e = e->next_caller)
342 if (e->caller->in_other_partition
343 || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
344 return true;
345 return false;
348 /* Return if NODE contain references from other partitions. */
350 bool
351 referenced_from_this_partition_p (symtab_node *node,
352 lto_symtab_encoder_t encoder)
354 int i;
355 struct ipa_ref *ref = NULL;
357 for (i = 0; node->iterate_referring (i, ref); i++)
358 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
359 return true;
360 return false;
363 /* Return true when node is reachable from other partition. */
365 bool
366 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
368 struct cgraph_edge *e;
369 for (e = node->callers; e; e = e->next_caller)
370 if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
371 return true;
372 return false;
375 /* Output the cgraph NODE to OB. ENCODER is used to find the
376 reference number of NODE->inlined_to. SET is the set of nodes we
377 are writing to the current file. If NODE is not in SET, then NODE
378 is a boundary of a cgraph_node_set and we pretend NODE just has a
379 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
380 that have had their callgraph node written so far. This is used to
381 determine if NODE is a clone of a previously written node. */
383 static void
384 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
385 lto_symtab_encoder_t encoder)
387 unsigned int tag;
388 struct bitpack_d bp;
389 bool boundary_p;
390 intptr_t ref;
391 bool in_other_partition = false;
392 struct cgraph_node *clone_of, *ultimate_clone_of;
393 ipa_opt_pass_d *pass;
394 int i;
395 bool alias_p;
396 const char *comdat;
397 const char *section;
398 tree group;
400 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
402 if (node->analyzed && !boundary_p)
403 tag = LTO_symtab_analyzed_node;
404 else
405 tag = LTO_symtab_unavail_node;
407 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
408 tag);
409 streamer_write_hwi_stream (ob->main_stream, node->order);
411 /* In WPA mode, we only output part of the call-graph. Also, we
412 fake cgraph node attributes. There are two cases that we care.
414 Boundary nodes: There are nodes that are not part of SET but are
415 called from within SET. We artificially make them look like
416 externally visible nodes with no function body.
418 Cherry-picked nodes: These are nodes we pulled from other
419 translation units into SET during IPA-inlining. We make them as
420 local static nodes to prevent clashes with other local statics. */
421 if (boundary_p && node->analyzed
422 && node->get_partitioning_class () == SYMBOL_PARTITION)
424 /* Inline clones can not be part of boundary.
425 gcc_assert (!node->global.inlined_to);
427 FIXME: At the moment they can be, when partition contains an inline
428 clone that is clone of inline clone from outside partition. We can
429 reshape the clone tree and make other tree to be the root, but it
430 needs a bit extra work and will be promplty done by cgraph_remove_node
431 after reading back. */
432 in_other_partition = 1;
435 clone_of = node->clone_of;
436 while (clone_of
437 && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
438 if (clone_of->prev_sibling_clone)
439 clone_of = clone_of->prev_sibling_clone;
440 else
441 clone_of = clone_of->clone_of;
443 /* See if body of the master function is output. If not, we are seeing only
444 an declaration and we do not need to pass down clone tree. */
445 ultimate_clone_of = clone_of;
446 while (ultimate_clone_of && ultimate_clone_of->clone_of)
447 ultimate_clone_of = ultimate_clone_of->clone_of;
449 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
450 clone_of = NULL;
452 if (tag == LTO_symtab_analyzed_node)
453 gcc_assert (clone_of || !node->clone_of);
454 if (!clone_of)
455 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
456 else
457 streamer_write_hwi_stream (ob->main_stream, ref);
460 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
461 streamer_write_gcov_count_stream (ob->main_stream, node->count);
462 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
464 streamer_write_hwi_stream (ob->main_stream,
465 node->ipa_transforms_to_apply.length ());
466 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
467 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
469 if (tag == LTO_symtab_analyzed_node)
471 if (node->global.inlined_to)
473 ref = lto_symtab_encoder_lookup (encoder, node->global.inlined_to);
474 gcc_assert (ref != LCC_NOT_FOUND);
476 else
477 ref = LCC_NOT_FOUND;
479 streamer_write_hwi_stream (ob->main_stream, ref);
482 group = node->get_comdat_group ();
483 if (group)
484 comdat = IDENTIFIER_POINTER (group);
485 else
486 comdat = "";
487 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
489 if (group)
491 if (node->same_comdat_group && !boundary_p)
493 ref = lto_symtab_encoder_lookup (encoder,
494 node->same_comdat_group);
495 gcc_assert (ref != LCC_NOT_FOUND);
497 else
498 ref = LCC_NOT_FOUND;
499 streamer_write_hwi_stream (ob->main_stream, ref);
502 section = node->get_section ();
503 if (!section)
504 section = "";
506 streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
508 bp = bitpack_create (ob->main_stream);
509 bp_pack_value (&bp, node->local.local, 1);
510 bp_pack_value (&bp, node->externally_visible, 1);
511 bp_pack_value (&bp, node->definition, 1);
512 bp_pack_value (&bp, node->local.versionable, 1);
513 bp_pack_value (&bp, node->local.can_change_signature, 1);
514 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
515 bp_pack_value (&bp, node->force_output, 1);
516 bp_pack_value (&bp, node->forced_by_abi, 1);
517 bp_pack_value (&bp, node->unique_name, 1);
518 bp_pack_value (&bp, node->body_removed, 1);
519 bp_pack_value (&bp, node->implicit_section, 1);
520 bp_pack_value (&bp, node->address_taken, 1);
521 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
522 && node->get_partitioning_class () == SYMBOL_PARTITION
523 && (reachable_from_other_partition_p (node, encoder)
524 || referenced_from_other_partition_p (node, encoder)), 1);
525 bp_pack_value (&bp, node->lowered, 1);
526 bp_pack_value (&bp, in_other_partition, 1);
527 /* Real aliases in a boundary become non-aliases. However we still stream
528 alias info on weakrefs.
529 TODO: We lose a bit of information here - when we know that variable is
530 defined in other unit, we may use the info on aliases to resolve
531 symbol1 != symbol2 type tests that we can do only for locally defined objects
532 otherwise. */
533 alias_p = node->alias && (!boundary_p || node->weakref);
534 bp_pack_value (&bp, alias_p, 1);
535 bp_pack_value (&bp, node->weakref, 1);
536 bp_pack_value (&bp, node->frequency, 2);
537 bp_pack_value (&bp, node->only_called_at_startup, 1);
538 bp_pack_value (&bp, node->only_called_at_exit, 1);
539 bp_pack_value (&bp, node->tm_clone, 1);
540 bp_pack_value (&bp, node->calls_comdat_local, 1);
541 bp_pack_value (&bp, node->thunk.thunk_p && !boundary_p, 1);
542 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
543 LDPR_NUM_KNOWN, node->resolution);
544 bp_pack_value (&bp, node->instrumentation_clone, 1);
545 streamer_write_bitpack (&bp);
546 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
548 if (node->thunk.thunk_p && !boundary_p)
550 streamer_write_uhwi_stream
551 (ob->main_stream,
552 1 + (node->thunk.this_adjusting != 0) * 2
553 + (node->thunk.virtual_offset_p != 0) * 4
554 + (node->thunk.add_pointer_bounds_args != 0) * 8);
555 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
556 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
558 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
559 if (DECL_STATIC_CONSTRUCTOR (node->decl))
560 streamer_write_hwi_stream (ob->main_stream, node->get_init_priority ());
561 if (DECL_STATIC_DESTRUCTOR (node->decl))
562 streamer_write_hwi_stream (ob->main_stream, node->get_fini_priority ());
564 if (node->instrumentation_clone)
565 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->orig_decl);
568 /* Output the varpool NODE to OB.
569 If NODE is not in SET, then NODE is a boundary. */
571 static void
572 lto_output_varpool_node (struct lto_simple_output_block *ob, varpool_node *node,
573 lto_symtab_encoder_t encoder)
575 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
576 struct bitpack_d bp;
577 int ref;
578 bool alias_p;
579 const char *comdat;
580 const char *section;
581 tree group;
583 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
584 LTO_symtab_variable);
585 streamer_write_hwi_stream (ob->main_stream, node->order);
586 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
587 bp = bitpack_create (ob->main_stream);
588 bp_pack_value (&bp, node->externally_visible, 1);
589 bp_pack_value (&bp, node->force_output, 1);
590 bp_pack_value (&bp, node->forced_by_abi, 1);
591 bp_pack_value (&bp, node->unique_name, 1);
592 bp_pack_value (&bp, node->body_removed, 1);
593 bp_pack_value (&bp, node->implicit_section, 1);
594 bp_pack_value (&bp, node->writeonly, 1);
595 bp_pack_value (&bp, node->definition, 1);
596 alias_p = node->alias && (!boundary_p || node->weakref);
597 bp_pack_value (&bp, alias_p, 1);
598 bp_pack_value (&bp, node->weakref, 1);
599 bp_pack_value (&bp, node->analyzed && !boundary_p, 1);
600 gcc_assert (node->definition || !node->analyzed);
601 /* Constant pool initializers can be de-unified into individual ltrans units.
602 FIXME: Alternatively at -Os we may want to avoid generating for them the local
603 labels and share them across LTRANS partitions. */
604 if (node->get_partitioning_class () != SYMBOL_PARTITION)
606 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
607 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
609 else
611 bp_pack_value (&bp, node->definition
612 && referenced_from_other_partition_p (node, encoder), 1);
613 bp_pack_value (&bp, node->analyzed
614 && boundary_p && !DECL_EXTERNAL (node->decl), 1);
615 /* in_other_partition. */
617 bp_pack_value (&bp, node->tls_model, 3);
618 bp_pack_value (&bp, node->used_by_single_function, 1);
619 bp_pack_value (&bp, node->need_bounds_init, 1);
620 streamer_write_bitpack (&bp);
622 group = node->get_comdat_group ();
623 if (group)
624 comdat = IDENTIFIER_POINTER (group);
625 else
626 comdat = "";
627 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
629 if (group)
631 if (node->same_comdat_group && !boundary_p)
633 ref = lto_symtab_encoder_lookup (encoder,
634 node->same_comdat_group);
635 gcc_assert (ref != LCC_NOT_FOUND);
637 else
638 ref = LCC_NOT_FOUND;
639 streamer_write_hwi_stream (ob->main_stream, ref);
642 section = node->get_section ();
643 if (!section)
644 section = "";
645 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
647 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
648 LDPR_NUM_KNOWN, node->resolution);
651 /* Output the varpool NODE to OB.
652 If NODE is not in SET, then NODE is a boundary. */
654 static void
655 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
656 lto_symtab_encoder_t encoder)
658 struct bitpack_d bp;
659 int nref;
660 int uid = ref->lto_stmt_uid;
661 struct cgraph_node *node;
663 bp = bitpack_create (ob->main_stream);
664 bp_pack_value (&bp, ref->use, 3);
665 bp_pack_value (&bp, ref->speculative, 1);
666 streamer_write_bitpack (&bp);
667 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
668 gcc_assert (nref != LCC_NOT_FOUND);
669 streamer_write_hwi_stream (ob->main_stream, nref);
671 node = dyn_cast <cgraph_node *> (ref->referring);
672 if (node)
674 if (ref->stmt)
675 uid = gimple_uid (ref->stmt) + 1;
676 streamer_write_hwi_stream (ob->main_stream, uid);
680 /* Stream out profile_summary to OB. */
682 static void
683 output_profile_summary (struct lto_simple_output_block *ob)
685 unsigned h_ix;
686 struct bitpack_d bp;
688 if (profile_info)
690 /* We do not output num and run_max, they are not used by
691 GCC profile feedback and they are difficult to merge from multiple
692 units. */
693 gcc_assert (profile_info->runs);
694 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
695 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
697 /* sum_all is needed for computing the working set with the
698 histogram. */
699 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
701 /* Create and output a bitpack of non-zero histogram entries indices. */
702 bp = bitpack_create (ob->main_stream);
703 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
704 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
705 streamer_write_bitpack (&bp);
706 /* Now stream out only those non-zero entries. */
707 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
709 if (!profile_info->histogram[h_ix].num_counters)
710 continue;
711 streamer_write_gcov_count_stream (ob->main_stream,
712 profile_info->histogram[h_ix].num_counters);
713 streamer_write_gcov_count_stream (ob->main_stream,
714 profile_info->histogram[h_ix].min_value);
715 streamer_write_gcov_count_stream (ob->main_stream,
716 profile_info->histogram[h_ix].cum_value);
718 /* IPA-profile computes hot bb threshold based on cumulated
719 whole program profile. We need to stream it down to ltrans. */
720 if (flag_wpa)
721 streamer_write_gcov_count_stream (ob->main_stream,
722 get_hot_bb_threshold ());
724 else
725 streamer_write_uhwi_stream (ob->main_stream, 0);
728 /* Output all callees or indirect outgoing edges. EDGE must be the first such
729 edge. */
731 static void
732 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
733 struct lto_simple_output_block *ob,
734 lto_symtab_encoder_t encoder)
736 if (!edge)
737 return;
739 /* Output edges in backward direction, so the reconstructed callgraph match
740 and it is easy to associate call sites in the IPA pass summaries. */
741 while (edge->next_callee)
742 edge = edge->next_callee;
743 for (; edge; edge = edge->prev_callee)
744 lto_output_edge (ob, edge, encoder);
747 /* Output the part of the cgraph in SET. */
749 static void
750 output_refs (lto_symtab_encoder_t encoder)
752 lto_symtab_encoder_iterator lsei;
753 struct lto_simple_output_block *ob;
754 int count;
755 struct ipa_ref *ref;
756 int i;
758 ob = lto_create_simple_output_block (LTO_section_refs);
760 for (lsei = lsei_start_in_partition (encoder); !lsei_end_p (lsei);
761 lsei_next_in_partition (&lsei))
763 symtab_node *node = lsei_node (lsei);
765 count = node->ref_list.nreferences ();
766 if (count)
768 streamer_write_gcov_count_stream (ob->main_stream, count);
769 streamer_write_uhwi_stream (ob->main_stream,
770 lto_symtab_encoder_lookup (encoder, node));
771 for (i = 0; node->iterate_reference (i, ref); i++)
772 lto_output_ref (ob, ref, encoder);
776 streamer_write_uhwi_stream (ob->main_stream, 0);
778 lto_destroy_simple_output_block (ob);
781 /* Add NODE into encoder as well as nodes it is cloned from.
782 Do it in a way so clones appear first. */
784 static void
785 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
786 bool include_body)
788 if (node->clone_of)
789 add_node_to (encoder, node->clone_of, include_body);
790 else if (include_body)
791 lto_set_symtab_encoder_encode_body (encoder, node);
792 lto_symtab_encoder_encode (encoder, node);
795 /* Add all references in NODE to encoders. */
797 static void
798 add_references (lto_symtab_encoder_t encoder, symtab_node *node)
800 int i;
801 struct ipa_ref *ref = NULL;
802 for (i = 0; node->iterate_reference (i, ref); i++)
803 if (is_a <cgraph_node *> (ref->referred))
804 add_node_to (encoder, dyn_cast <cgraph_node *> (ref->referred), false);
805 else
806 lto_symtab_encoder_encode (encoder, ref->referred);
809 /* Find all symbols we want to stream into given partition and insert them
810 to encoders.
812 The function actually replaces IN_ENCODER by new one. The reason is that
813 streaming code needs clone's origin to be streamed before clone. This
814 means that we need to insert the nodes in specific order. This order is
815 ignored by the partitioning logic earlier. */
817 lto_symtab_encoder_t
818 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
820 struct cgraph_edge *edge;
821 int i;
822 lto_symtab_encoder_t encoder;
823 lto_symtab_encoder_iterator lsei;
824 hash_set<void *> reachable_call_targets;
826 encoder = lto_symtab_encoder_new (false);
828 /* Go over all entries in the IN_ENCODER and duplicate them to
829 ENCODER. At the same time insert masters of clones so
830 every master appears before clone. */
831 for (lsei = lsei_start_function_in_partition (in_encoder);
832 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
834 struct cgraph_node *node = lsei_cgraph_node (lsei);
835 add_node_to (encoder, node, true);
836 lto_set_symtab_encoder_in_partition (encoder, node);
837 add_references (encoder, node);
838 /* For proper debug info, we need to ship the origins, too. */
839 if (DECL_ABSTRACT_ORIGIN (node->decl))
841 struct cgraph_node *origin_node
842 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node->decl));
843 add_node_to (encoder, origin_node, true);
846 for (lsei = lsei_start_variable_in_partition (in_encoder);
847 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
849 varpool_node *vnode = lsei_varpool_node (lsei);
851 lto_set_symtab_encoder_in_partition (encoder, vnode);
852 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
853 add_references (encoder, vnode);
854 /* For proper debug info, we need to ship the origins, too. */
855 if (DECL_ABSTRACT_ORIGIN (vnode->decl))
857 varpool_node *origin_node
858 = varpool_node::get (DECL_ABSTRACT_ORIGIN (vnode->decl));
859 lto_set_symtab_encoder_in_partition (encoder, origin_node);
862 /* Pickle in also the initializer of all referenced readonly variables
863 to help folding. Constant pool variables are not shared, so we must
864 pickle those too. */
865 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
867 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
868 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
870 if (!lto_symtab_encoder_encode_initializer_p (encoder,
871 vnode)
872 && (vnode->ctor_useable_for_folding_p ()
873 || POINTER_BOUNDS_P (vnode->decl)))
875 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
876 add_references (encoder, vnode);
881 /* Go over all the nodes again to include callees that are not in
882 SET. */
883 for (lsei = lsei_start_function_in_partition (encoder);
884 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
886 struct cgraph_node *node = lsei_cgraph_node (lsei);
887 for (edge = node->callees; edge; edge = edge->next_callee)
889 struct cgraph_node *callee = edge->callee;
890 if (!lto_symtab_encoder_in_partition_p (encoder, callee))
892 /* We should have moved all the inlines. */
893 gcc_assert (!callee->global.inlined_to);
894 add_node_to (encoder, callee, false);
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)
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);
908 if (!reachable_call_targets.add (cache_token))
910 for (i = 0; i < targets.length (); i++)
912 struct cgraph_node *callee = targets[i];
914 /* Adding an external declarations into the unit serves
915 no purpose and just increases its boundary. */
916 if (callee->definition
917 && !lto_symtab_encoder_in_partition_p
918 (encoder, callee))
920 gcc_assert (!callee->global.inlined_to);
921 add_node_to (encoder, callee, false);
927 lto_symtab_encoder_delete (in_encoder);
928 return encoder;
931 /* Output the part of the symtab in SET and VSET. */
933 void
934 output_symtab (void)
936 struct cgraph_node *node;
937 struct lto_simple_output_block *ob;
938 lto_symtab_encoder_iterator lsei;
939 int i, n_nodes;
940 lto_symtab_encoder_t encoder;
942 if (flag_wpa)
943 output_cgraph_opt_summary ();
945 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
947 output_profile_summary (ob);
949 /* An encoder for cgraph nodes should have been created by
950 ipa_write_summaries_1. */
951 gcc_assert (ob->decl_state->symtab_node_encoder);
952 encoder = ob->decl_state->symtab_node_encoder;
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. */
957 n_nodes = lto_symtab_encoder_size (encoder);
958 for (i = 0; i < n_nodes; i++)
960 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
961 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
962 lto_output_node (ob, cnode, encoder);
963 else
964 lto_output_varpool_node (ob, dyn_cast<varpool_node *> (node), encoder);
967 /* Go over the nodes in SET again to write edges. */
968 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
969 lsei_next_function_in_partition (&lsei))
971 node = lsei_cgraph_node (lsei);
972 output_outgoing_cgraph_edges (node->callees, ob, encoder);
973 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
976 streamer_write_uhwi_stream (ob->main_stream, 0);
978 lto_destroy_simple_output_block (ob);
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)
986 asm_nodes_output = true;
987 lto_output_toplevel_asms ();
990 output_refs (encoder);
993 /* Return identifier encoded in IB as a plain string. */
995 static tree
996 read_identifier (struct lto_input_block *ib)
998 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
999 tree id;
1001 if (ib->data[ib->p + len])
1002 lto_section_overrun (ib);
1003 if (!len)
1005 ib->p++;
1006 return NULL;
1008 id = get_identifier (ib->data + ib->p);
1009 ib->p += len + 1;
1010 return id;
1013 /* Return string encoded in IB, NULL if string is empty. */
1015 static const char *
1016 read_string (struct lto_input_block *ib)
1018 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1019 const char *str;
1021 if (ib->data[ib->p + len])
1022 lto_section_overrun (ib);
1023 if (!len)
1025 ib->p++;
1026 return NULL;
1028 str = ib->data + ib->p;
1029 ib->p += len + 1;
1030 return str;
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. */
1040 static void
1041 input_overwrite_node (struct lto_file_decl_data *file_data,
1042 struct cgraph_node *node,
1043 enum LTO_symtab_tags tag,
1044 struct bitpack_d *bp)
1046 node->aux = (void *) tag;
1047 node->lto_file_data = file_data;
1049 node->local.local = bp_unpack_value (bp, 1);
1050 node->externally_visible = bp_unpack_value (bp, 1);
1051 node->definition = bp_unpack_value (bp, 1);
1052 node->local.versionable = bp_unpack_value (bp, 1);
1053 node->local.can_change_signature = bp_unpack_value (bp, 1);
1054 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
1055 node->force_output = bp_unpack_value (bp, 1);
1056 node->forced_by_abi = bp_unpack_value (bp, 1);
1057 node->unique_name = bp_unpack_value (bp, 1);
1058 node->body_removed = bp_unpack_value (bp, 1);
1059 node->implicit_section = bp_unpack_value (bp, 1);
1060 node->address_taken = bp_unpack_value (bp, 1);
1061 node->used_from_other_partition = bp_unpack_value (bp, 1);
1062 node->lowered = bp_unpack_value (bp, 1);
1063 node->analyzed = tag == LTO_symtab_analyzed_node;
1064 node->in_other_partition = bp_unpack_value (bp, 1);
1065 if (node->in_other_partition
1066 /* Avoid updating decl when we are seeing just inline clone.
1067 When inlining function that has functions already inlined into it,
1068 we produce clones of inline clones.
1070 WPA partitioning might put each clone into different unit and
1071 we might end up streaming inline clone from other partition
1072 to support clone we are interested in. */
1073 && (!node->clone_of
1074 || node->clone_of->decl != node->decl))
1076 DECL_EXTERNAL (node->decl) = 1;
1077 TREE_STATIC (node->decl) = 0;
1079 node->alias = bp_unpack_value (bp, 1);
1080 node->weakref = bp_unpack_value (bp, 1);
1081 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
1082 node->only_called_at_startup = bp_unpack_value (bp, 1);
1083 node->only_called_at_exit = bp_unpack_value (bp, 1);
1084 node->tm_clone = bp_unpack_value (bp, 1);
1085 node->calls_comdat_local = bp_unpack_value (bp, 1);
1086 node->thunk.thunk_p = bp_unpack_value (bp, 1);
1087 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
1088 LDPR_NUM_KNOWN);
1089 node->instrumentation_clone = bp_unpack_value (bp, 1);
1090 gcc_assert (flag_ltrans
1091 || (!node->in_other_partition
1092 && !node->used_from_other_partition));
1095 /* Return string alias is alias of. */
1097 static tree
1098 get_alias_symbol (tree decl)
1100 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1101 return get_identifier (TREE_STRING_POINTER
1102 (TREE_VALUE (TREE_VALUE (alias))));
1105 /* Read a node from input_block IB. TAG is the node's tag just read.
1106 Return the node read or overwriten. */
1108 static struct cgraph_node *
1109 input_node (struct lto_file_decl_data *file_data,
1110 struct lto_input_block *ib,
1111 enum LTO_symtab_tags tag,
1112 vec<symtab_node *> nodes)
1114 gcc::pass_manager *passes = g->get_passes ();
1115 tree fn_decl;
1116 struct cgraph_node *node;
1117 struct bitpack_d bp;
1118 unsigned decl_index;
1119 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1120 int clone_ref;
1121 int order;
1122 int i, count;
1123 tree group;
1124 const char *section;
1126 order = streamer_read_hwi (ib) + order_base;
1127 clone_ref = streamer_read_hwi (ib);
1129 decl_index = streamer_read_uhwi (ib);
1130 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1132 if (clone_ref != LCC_NOT_FOUND)
1134 node = dyn_cast<cgraph_node *> (nodes[clone_ref])->create_clone (fn_decl,
1135 0, CGRAPH_FREQ_BASE, false,
1136 vNULL, false, NULL, NULL);
1138 else
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. */
1143 node = cgraph_node::create_empty ();
1144 node->decl = fn_decl;
1145 node->register_symbol ();
1148 node->order = order;
1149 if (order >= symtab_order)
1150 symtab_order = order + 1;
1152 node->count = streamer_read_gcov_count (ib);
1153 node->count_materialization_scale = streamer_read_hwi (ib);
1155 count = streamer_read_hwi (ib);
1156 node->ipa_transforms_to_apply = vNULL;
1157 for (i = 0; i < count; i++)
1159 opt_pass *pass;
1160 int pid = streamer_read_hwi (ib);
1162 gcc_assert (pid < passes->passes_by_id_size);
1163 pass = passes->passes_by_id[pid];
1164 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *) pass);
1167 if (tag == LTO_symtab_analyzed_node)
1168 ref = streamer_read_hwi (ib);
1170 group = read_identifier (ib);
1171 if (group)
1172 ref2 = streamer_read_hwi (ib);
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. */
1178 if (node->aux && !DECL_BUILT_IN (node->decl))
1179 internal_error ("bytecode stream: found multiple instances of cgraph "
1180 "node with uid %d", node->uid);
1182 node->tp_first_run = streamer_read_uhwi (ib);
1184 bp = streamer_read_bitpack (ib);
1186 input_overwrite_node (file_data, node, tag, &bp);
1188 /* Store a reference for now, and fix up later to be a pointer. */
1189 node->global.inlined_to = (cgraph_node *) (intptr_t) ref;
1191 if (group)
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;
1197 else
1198 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1199 section = read_string (ib);
1200 if (section)
1201 node->set_section_for_node (section);
1203 if (node->thunk.thunk_p)
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);
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);
1213 node->thunk.add_pointer_bounds_args = (type & 8);
1215 if (node->alias && !node->analyzed && node->weakref)
1216 node->alias_target = get_alias_symbol (node->decl);
1217 node->profile_id = streamer_read_hwi (ib);
1218 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1219 node->set_init_priority (streamer_read_hwi (ib));
1220 if (DECL_STATIC_DESTRUCTOR (node->decl))
1221 node->set_fini_priority (streamer_read_hwi (ib));
1223 if (node->instrumentation_clone)
1225 decl_index = streamer_read_uhwi (ib);
1226 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1227 node->orig_decl = fn_decl;
1230 return node;
1233 /* Read a node from input_block IB. TAG is the node's tag just read.
1234 Return the node read or overwriten. */
1236 static varpool_node *
1237 input_varpool_node (struct lto_file_decl_data *file_data,
1238 struct lto_input_block *ib)
1240 int decl_index;
1241 tree var_decl;
1242 varpool_node *node;
1243 struct bitpack_d bp;
1244 int ref = LCC_NOT_FOUND;
1245 int order;
1246 tree group;
1247 const char *section;
1249 order = streamer_read_hwi (ib) + order_base;
1250 decl_index = streamer_read_uhwi (ib);
1251 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1253 /* Declaration of functions can be already merged with a declaration
1254 from other input file. We keep cgraph unmerged until after streaming
1255 of ipa passes is done. Alays forcingly create a fresh node. */
1256 node = varpool_node::create_empty ();
1257 node->decl = var_decl;
1258 node->register_symbol ();
1260 node->order = order;
1261 if (order >= symtab_order)
1262 symtab_order = order + 1;
1263 node->lto_file_data = file_data;
1265 bp = streamer_read_bitpack (ib);
1266 node->externally_visible = bp_unpack_value (&bp, 1);
1267 node->force_output = bp_unpack_value (&bp, 1);
1268 node->forced_by_abi = bp_unpack_value (&bp, 1);
1269 node->unique_name = bp_unpack_value (&bp, 1);
1270 node->body_removed = bp_unpack_value (&bp, 1);
1271 node->implicit_section = bp_unpack_value (&bp, 1);
1272 node->writeonly = bp_unpack_value (&bp, 1);
1273 node->definition = bp_unpack_value (&bp, 1);
1274 node->alias = bp_unpack_value (&bp, 1);
1275 node->weakref = bp_unpack_value (&bp, 1);
1276 node->analyzed = bp_unpack_value (&bp, 1);
1277 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1278 node->in_other_partition = bp_unpack_value (&bp, 1);
1279 if (node->in_other_partition)
1281 DECL_EXTERNAL (node->decl) = 1;
1282 TREE_STATIC (node->decl) = 0;
1284 if (node->alias && !node->analyzed && node->weakref)
1285 node->alias_target = get_alias_symbol (node->decl);
1286 node->tls_model = (enum tls_model)bp_unpack_value (&bp, 3);
1287 node->used_by_single_function = (enum tls_model)bp_unpack_value (&bp, 1);
1288 node->need_bounds_init = bp_unpack_value (&bp, 1);
1289 group = read_identifier (ib);
1290 if (group)
1292 node->set_comdat_group (group);
1293 ref = streamer_read_hwi (ib);
1294 /* Store a reference for now, and fix up later to be a pointer. */
1295 node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1297 else
1298 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1299 section = read_string (ib);
1300 if (section)
1301 node->set_section_for_node (section);
1302 node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1303 LDPR_NUM_KNOWN);
1304 gcc_assert (flag_ltrans
1305 || (!node->in_other_partition
1306 && !node->used_from_other_partition));
1308 return node;
1311 /* Read a node from input_block IB. TAG is the node's tag just read.
1312 Return the node read or overwriten. */
1314 static void
1315 input_ref (struct lto_input_block *ib,
1316 symtab_node *referring_node,
1317 vec<symtab_node *> nodes)
1319 symtab_node *node = NULL;
1320 struct bitpack_d bp;
1321 enum ipa_ref_use use;
1322 bool speculative;
1323 struct ipa_ref *ref;
1325 bp = streamer_read_bitpack (ib);
1326 use = (enum ipa_ref_use) bp_unpack_value (&bp, 3);
1327 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1328 node = nodes[streamer_read_hwi (ib)];
1329 ref = referring_node->add_reference (node, use);
1330 ref->speculative = speculative;
1331 if (is_a <cgraph_node *> (referring_node))
1332 ref->lto_stmt_uid = streamer_read_hwi (ib);
1335 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1336 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1337 edge being read is indirect (in the sense that it has
1338 indirect_unknown_callee set). */
1340 static void
1341 input_edge (struct lto_input_block *ib, vec<symtab_node *> nodes,
1342 bool indirect)
1344 struct cgraph_node *caller, *callee;
1345 struct cgraph_edge *edge;
1346 unsigned int stmt_id;
1347 gcov_type count;
1348 int freq;
1349 cgraph_inline_failed_t inline_failed;
1350 struct bitpack_d bp;
1351 int ecf_flags = 0;
1353 caller = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1354 if (caller == NULL || caller->decl == NULL_TREE)
1355 internal_error ("bytecode stream: no caller found while reading edge");
1357 if (!indirect)
1359 callee = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1360 if (callee == NULL || callee->decl == NULL_TREE)
1361 internal_error ("bytecode stream: no callee found while reading edge");
1363 else
1364 callee = NULL;
1366 count = streamer_read_gcov_count (ib);
1368 bp = streamer_read_bitpack (ib);
1369 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_t, CIF_N_REASONS);
1370 stmt_id = bp_unpack_var_len_unsigned (&bp);
1371 freq = (int) bp_unpack_var_len_unsigned (&bp);
1373 if (indirect)
1374 edge = caller->create_indirect_edge (NULL, 0, count, freq);
1375 else
1376 edge = caller->create_edge (callee, NULL, count, freq);
1378 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1379 edge->speculative = bp_unpack_value (&bp, 1);
1380 edge->lto_stmt_uid = stmt_id;
1381 edge->inline_failed = inline_failed;
1382 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1383 edge->can_throw_external = bp_unpack_value (&bp, 1);
1384 if (indirect)
1386 if (bp_unpack_value (&bp, 1))
1387 ecf_flags |= ECF_CONST;
1388 if (bp_unpack_value (&bp, 1))
1389 ecf_flags |= ECF_PURE;
1390 if (bp_unpack_value (&bp, 1))
1391 ecf_flags |= ECF_NORETURN;
1392 if (bp_unpack_value (&bp, 1))
1393 ecf_flags |= ECF_MALLOC;
1394 if (bp_unpack_value (&bp, 1))
1395 ecf_flags |= ECF_NOTHROW;
1396 if (bp_unpack_value (&bp, 1))
1397 ecf_flags |= ECF_RETURNS_TWICE;
1398 edge->indirect_info->ecf_flags = ecf_flags;
1399 edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1400 if (edge->indirect_info->common_target_id)
1401 edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1406 /* Read a cgraph from IB using the info in FILE_DATA. */
1408 static vec<symtab_node *>
1409 input_cgraph_1 (struct lto_file_decl_data *file_data,
1410 struct lto_input_block *ib)
1412 enum LTO_symtab_tags tag;
1413 vec<symtab_node *> nodes = vNULL;
1414 symtab_node *node;
1415 unsigned i;
1417 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1418 order_base = symtab_order;
1419 while (tag)
1421 if (tag == LTO_symtab_edge)
1422 input_edge (ib, nodes, false);
1423 else if (tag == LTO_symtab_indirect_edge)
1424 input_edge (ib, nodes, true);
1425 else if (tag == LTO_symtab_variable)
1427 node = input_varpool_node (file_data, ib);
1428 nodes.safe_push (node);
1429 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1431 else
1433 node = input_node (file_data, ib, tag, nodes);
1434 if (node == NULL || node->decl == NULL_TREE)
1435 internal_error ("bytecode stream: found empty cgraph node");
1436 nodes.safe_push (node);
1437 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1440 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1443 lto_input_toplevel_asms (file_data, order_base);
1445 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1446 #ifdef ENABLE_CHECKING
1447 FOR_EACH_VEC_ELT (nodes, i, node)
1448 gcc_assert (node->aux || !is_a <cgraph_node *> (node));
1449 #endif
1450 FOR_EACH_VEC_ELT (nodes, i, node)
1452 int ref;
1453 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1455 ref = (int) (intptr_t) cnode->global.inlined_to;
1457 /* We share declaration of builtins, so we may read same node twice. */
1458 if (!node->aux)
1459 continue;
1460 node->aux = NULL;
1462 /* Fixup inlined_to from reference to pointer. */
1463 if (ref != LCC_NOT_FOUND)
1464 dyn_cast<cgraph_node *> (node)->global.inlined_to
1465 = dyn_cast<cgraph_node *> (nodes[ref]);
1466 else
1467 cnode->global.inlined_to = NULL;
1469 /* Compute instrumented_version. */
1470 if (cnode->instrumentation_clone)
1472 gcc_assert (cnode->orig_decl);
1474 cnode->instrumented_version = cgraph_node::get (cnode->orig_decl);
1475 if (cnode->instrumented_version)
1476 cnode->instrumented_version->instrumented_version = cnode;
1478 /* Restore decl names reference. */
1479 if (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (cnode->decl))
1480 && !TREE_CHAIN (DECL_ASSEMBLER_NAME (cnode->decl)))
1481 TREE_CHAIN (DECL_ASSEMBLER_NAME (cnode->decl))
1482 = DECL_ASSEMBLER_NAME (cnode->orig_decl);
1486 ref = (int) (intptr_t) node->same_comdat_group;
1488 /* Fixup same_comdat_group from reference to pointer. */
1489 if (ref != LCC_NOT_FOUND)
1490 node->same_comdat_group = nodes[ref];
1491 else
1492 node->same_comdat_group = NULL;
1494 FOR_EACH_VEC_ELT (nodes, i, node)
1495 node->aux = is_a <cgraph_node *> (node) ? (void *)1 : NULL;
1496 return nodes;
1499 /* Input ipa_refs. */
1501 static void
1502 input_refs (struct lto_input_block *ib,
1503 vec<symtab_node *> nodes)
1505 int count;
1506 int idx;
1507 while (true)
1509 symtab_node *node;
1510 count = streamer_read_uhwi (ib);
1511 if (!count)
1512 break;
1513 idx = streamer_read_uhwi (ib);
1514 node = nodes[idx];
1515 while (count)
1517 input_ref (ib, node, nodes);
1518 count--;
1524 static struct gcov_ctr_summary lto_gcov_summary;
1526 /* Input profile_info from IB. */
1527 static void
1528 input_profile_summary (struct lto_input_block *ib,
1529 struct lto_file_decl_data *file_data)
1531 unsigned h_ix;
1532 struct bitpack_d bp;
1533 unsigned int runs = streamer_read_uhwi (ib);
1534 if (runs)
1536 file_data->profile_info.runs = runs;
1537 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1538 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1540 memset (file_data->profile_info.histogram, 0,
1541 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1542 /* Input the bitpack of non-zero histogram indices. */
1543 bp = streamer_read_bitpack (ib);
1544 /* Read in and unpack the full bitpack, flagging non-zero
1545 histogram entries by setting the num_counters non-zero. */
1546 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1548 file_data->profile_info.histogram[h_ix].num_counters
1549 = bp_unpack_value (&bp, 1);
1551 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1553 if (!file_data->profile_info.histogram[h_ix].num_counters)
1554 continue;
1556 file_data->profile_info.histogram[h_ix].num_counters
1557 = streamer_read_gcov_count (ib);
1558 file_data->profile_info.histogram[h_ix].min_value
1559 = streamer_read_gcov_count (ib);
1560 file_data->profile_info.histogram[h_ix].cum_value
1561 = streamer_read_gcov_count (ib);
1563 /* IPA-profile computes hot bb threshold based on cumulated
1564 whole program profile. We need to stream it down to ltrans. */
1565 if (flag_ltrans)
1566 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1571 /* Rescale profile summaries to the same number of runs in the whole unit. */
1573 static void
1574 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1576 struct lto_file_decl_data *file_data;
1577 unsigned int j, h_ix;
1578 gcov_unsigned_t max_runs = 0;
1579 struct cgraph_node *node;
1580 struct cgraph_edge *edge;
1581 gcov_type saved_sum_all = 0;
1582 gcov_ctr_summary *saved_profile_info = 0;
1583 int saved_scale = 0;
1585 /* Find unit with maximal number of runs. If we ever get serious about
1586 roundoff errors, we might also consider computing smallest common
1587 multiply. */
1588 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1589 if (max_runs < file_data->profile_info.runs)
1590 max_runs = file_data->profile_info.runs;
1592 if (!max_runs)
1593 return;
1595 /* Simple overflow check. We probably don't need to support that many train
1596 runs. Such a large value probably imply data corruption anyway. */
1597 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1599 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1600 INT_MAX / REG_BR_PROB_BASE);
1601 return;
1604 profile_info = &lto_gcov_summary;
1605 lto_gcov_summary.runs = max_runs;
1606 lto_gcov_summary.sum_max = 0;
1607 memset (lto_gcov_summary.histogram, 0,
1608 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1610 /* Rescale all units to the maximal number of runs.
1611 sum_max can not be easily merged, as we have no idea what files come from
1612 the same run. We do not use the info anyway, so leave it 0. */
1613 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1614 if (file_data->profile_info.runs)
1616 int scale = GCOV_COMPUTE_SCALE (max_runs,
1617 file_data->profile_info.runs);
1618 lto_gcov_summary.sum_max
1619 = MAX (lto_gcov_summary.sum_max,
1620 apply_scale (file_data->profile_info.sum_max, scale));
1621 lto_gcov_summary.sum_all
1622 = MAX (lto_gcov_summary.sum_all,
1623 apply_scale (file_data->profile_info.sum_all, scale));
1624 /* Save a pointer to the profile_info with the largest
1625 scaled sum_all and the scale for use in merging the
1626 histogram. */
1627 if (!saved_profile_info
1628 || lto_gcov_summary.sum_all > saved_sum_all)
1630 saved_profile_info = &file_data->profile_info;
1631 saved_sum_all = lto_gcov_summary.sum_all;
1632 saved_scale = scale;
1636 gcc_assert (saved_profile_info);
1638 /* Scale up the histogram from the profile that had the largest
1639 scaled sum_all above. */
1640 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1642 /* Scale up the min value as we did the corresponding sum_all
1643 above. Use that to find the new histogram index. */
1644 gcov_type scaled_min
1645 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1646 saved_scale);
1647 /* The new index may be shared with another scaled histogram entry,
1648 so we need to account for a non-zero histogram entry at new_ix. */
1649 unsigned new_ix = gcov_histo_index (scaled_min);
1650 lto_gcov_summary.histogram[new_ix].min_value
1651 = (lto_gcov_summary.histogram[new_ix].num_counters
1652 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1653 : scaled_min);
1654 /* Some of the scaled counter values would ostensibly need to be placed
1655 into different (larger) histogram buckets, but we keep things simple
1656 here and place the scaled cumulative counter value in the bucket
1657 corresponding to the scaled minimum counter value. */
1658 lto_gcov_summary.histogram[new_ix].cum_value
1659 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1660 saved_scale);
1661 lto_gcov_summary.histogram[new_ix].num_counters
1662 += saved_profile_info->histogram[h_ix].num_counters;
1665 /* Watch roundoff errors. */
1666 if (lto_gcov_summary.sum_max < max_runs)
1667 lto_gcov_summary.sum_max = max_runs;
1669 /* If merging already happent at WPA time, we are done. */
1670 if (flag_ltrans)
1671 return;
1673 /* Now compute count_materialization_scale of each node.
1674 During LTRANS we already have values of count_materialization_scale
1675 computed, so just update them. */
1676 FOR_EACH_FUNCTION (node)
1677 if (node->lto_file_data
1678 && node->lto_file_data->profile_info.runs)
1680 int scale;
1682 scale = RDIV (node->count_materialization_scale * max_runs,
1683 node->lto_file_data->profile_info.runs);
1684 node->count_materialization_scale = scale;
1685 if (scale < 0)
1686 fatal_error ("Profile information in %s corrupted",
1687 file_data->file_name);
1689 if (scale == REG_BR_PROB_BASE)
1690 continue;
1691 for (edge = node->callees; edge; edge = edge->next_callee)
1692 edge->count = apply_scale (edge->count, scale);
1693 node->count = apply_scale (node->count, scale);
1697 /* Input and merge the symtab from each of the .o files passed to
1698 lto1. */
1700 void
1701 input_symtab (void)
1703 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1704 struct lto_file_decl_data *file_data;
1705 unsigned int j = 0;
1706 struct cgraph_node *node;
1708 while ((file_data = file_data_vec[j++]))
1710 const char *data;
1711 size_t len;
1712 struct lto_input_block *ib;
1713 vec<symtab_node *> nodes;
1715 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1716 &data, &len);
1717 if (!ib)
1718 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1719 input_profile_summary (ib, file_data);
1720 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1721 nodes = input_cgraph_1 (file_data, ib);
1722 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1723 ib, data, len);
1725 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1726 &data, &len);
1727 if (!ib)
1728 fatal_error ("cannot find LTO section refs in %s",
1729 file_data->file_name);
1730 input_refs (ib, nodes);
1731 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1732 ib, data, len);
1733 if (flag_ltrans)
1734 input_cgraph_opt_summary (nodes);
1735 nodes.release ();
1738 merge_profile_summaries (file_data_vec);
1739 get_working_sets ();
1742 /* Clear out the aux field that was used to store enough state to
1743 tell which nodes should be overwritten. */
1744 FOR_EACH_FUNCTION (node)
1746 /* Some nodes may have been created by cgraph_node. This
1747 happens when the callgraph contains nested functions. If the
1748 node for the parent function was never emitted to the gimple
1749 file, cgraph_node will create a node for it when setting the
1750 context of the nested function. */
1751 if (node->lto_file_data)
1752 node->aux = NULL;
1756 /* True when we need optimization summary for NODE. */
1758 static int
1759 output_cgraph_opt_summary_p (struct cgraph_node *node)
1761 return (node->clone_of
1762 && (node->clone.tree_map
1763 || node->clone.args_to_skip
1764 || node->clone.combined_args_to_skip));
1767 /* Output optimization summary for EDGE to OB. */
1768 static void
1769 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1770 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1774 /* Output optimization summary for NODE to OB. */
1776 static void
1777 output_node_opt_summary (struct output_block *ob,
1778 struct cgraph_node *node,
1779 lto_symtab_encoder_t encoder)
1781 unsigned int index;
1782 bitmap_iterator bi;
1783 struct ipa_replace_map *map;
1784 struct bitpack_d bp;
1785 int i;
1786 struct cgraph_edge *e;
1788 if (node->clone.args_to_skip)
1790 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1791 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1792 streamer_write_uhwi (ob, index);
1794 else
1795 streamer_write_uhwi (ob, 0);
1796 if (node->clone.combined_args_to_skip)
1798 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1799 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1800 streamer_write_uhwi (ob, index);
1802 else
1803 streamer_write_uhwi (ob, 0);
1804 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1805 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1807 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1808 mechanism to store function local declarations into summaries. */
1809 gcc_assert (!map->old_tree);
1810 streamer_write_uhwi (ob, map->parm_num);
1811 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1812 stream_write_tree (ob, map->new_tree, true);
1813 bp = bitpack_create (ob->main_stream);
1814 bp_pack_value (&bp, map->replace_p, 1);
1815 bp_pack_value (&bp, map->ref_p, 1);
1816 streamer_write_bitpack (&bp);
1819 if (lto_symtab_encoder_in_partition_p (encoder, node))
1821 for (e = node->callees; e; e = e->next_callee)
1822 output_edge_opt_summary (ob, e);
1823 for (e = node->indirect_calls; e; e = e->next_callee)
1824 output_edge_opt_summary (ob, e);
1828 /* Output optimization summaries stored in callgraph.
1829 At the moment it is the clone info structure. */
1831 static void
1832 output_cgraph_opt_summary (void)
1834 int i, n_nodes;
1835 lto_symtab_encoder_t encoder;
1836 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1837 unsigned count = 0;
1839 ob->symbol = NULL;
1840 encoder = ob->decl_state->symtab_node_encoder;
1841 n_nodes = lto_symtab_encoder_size (encoder);
1842 for (i = 0; i < n_nodes; i++)
1844 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1845 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1846 if (cnode && output_cgraph_opt_summary_p (cnode))
1847 count++;
1849 streamer_write_uhwi (ob, count);
1850 for (i = 0; i < n_nodes; i++)
1852 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1853 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1854 if (cnode && output_cgraph_opt_summary_p (cnode))
1856 streamer_write_uhwi (ob, i);
1857 output_node_opt_summary (ob, cnode, encoder);
1860 produce_asm (ob, NULL);
1861 destroy_output_block (ob);
1864 /* Input optimisation summary of EDGE. */
1866 static void
1867 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
1868 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
1872 /* Input optimisation summary of NODE. */
1874 static void
1875 input_node_opt_summary (struct cgraph_node *node,
1876 struct lto_input_block *ib_main,
1877 struct data_in *data_in)
1879 int i;
1880 int count;
1881 int bit;
1882 struct bitpack_d bp;
1883 struct cgraph_edge *e;
1885 count = streamer_read_uhwi (ib_main);
1886 if (count)
1887 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1888 for (i = 0; i < count; i++)
1890 bit = streamer_read_uhwi (ib_main);
1891 bitmap_set_bit (node->clone.args_to_skip, bit);
1893 count = streamer_read_uhwi (ib_main);
1894 if (count)
1895 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1896 for (i = 0; i < count; i++)
1898 bit = streamer_read_uhwi (ib_main);
1899 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1901 count = streamer_read_uhwi (ib_main);
1902 for (i = 0; i < count; i++)
1904 struct ipa_replace_map *map = ggc_alloc<ipa_replace_map> ();
1906 vec_safe_push (node->clone.tree_map, map);
1907 map->parm_num = streamer_read_uhwi (ib_main);
1908 map->old_tree = NULL;
1909 map->new_tree = stream_read_tree (ib_main, data_in);
1910 bp = streamer_read_bitpack (ib_main);
1911 map->replace_p = bp_unpack_value (&bp, 1);
1912 map->ref_p = bp_unpack_value (&bp, 1);
1914 for (e = node->callees; e; e = e->next_callee)
1915 input_edge_opt_summary (e, ib_main);
1916 for (e = node->indirect_calls; e; e = e->next_callee)
1917 input_edge_opt_summary (e, ib_main);
1920 /* Read section in file FILE_DATA of length LEN with data DATA. */
1922 static void
1923 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1924 const char *data, size_t len,
1925 vec<symtab_node *> nodes)
1927 const struct lto_function_header *header =
1928 (const struct lto_function_header *) data;
1929 const int cfg_offset = sizeof (struct lto_function_header);
1930 const int main_offset = cfg_offset + header->cfg_size;
1931 const int string_offset = main_offset + header->main_size;
1932 struct data_in *data_in;
1933 struct lto_input_block ib_main;
1934 unsigned int i;
1935 unsigned int count;
1937 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1938 header->main_size);
1940 data_in =
1941 lto_data_in_create (file_data, (const char *) data + string_offset,
1942 header->string_size, vNULL);
1943 count = streamer_read_uhwi (&ib_main);
1945 for (i = 0; i < count; i++)
1947 int ref = streamer_read_uhwi (&ib_main);
1948 input_node_opt_summary (dyn_cast<cgraph_node *> (nodes[ref]),
1949 &ib_main, data_in);
1951 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1952 len);
1953 lto_data_in_delete (data_in);
1956 /* Input optimization summary of cgraph. */
1958 static void
1959 input_cgraph_opt_summary (vec<symtab_node *> nodes)
1961 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1962 struct lto_file_decl_data *file_data;
1963 unsigned int j = 0;
1965 while ((file_data = file_data_vec[j++]))
1967 size_t len;
1968 const char *data =
1969 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1970 &len);
1972 if (data)
1973 input_cgraph_opt_section (file_data, data, len, nodes);