* config/rx/rx.c (add_vector_labels): New.
[official-gcc.git] / gcc / lto-cgraph.c
blob4f8d5b701ff9a34d0b7029f8ed3621fda93b3f23
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 "langhooks.h"
41 #include "bitmap.h"
42 #include "function.h"
43 #include "diagnostic-core.h"
44 #include "except.h"
45 #include "timevar.h"
46 #include "lto-streamer.h"
47 #include "data-streamer.h"
48 #include "tree-streamer.h"
49 #include "gcov-io.h"
50 #include "tree-pass.h"
51 #include "profile.h"
52 #include "context.h"
53 #include "pass_manager.h"
54 #include "ipa-utils.h"
56 /* True when asm nodes has been output. */
57 bool asm_nodes_output = false;
59 static void output_cgraph_opt_summary (void);
60 static void input_cgraph_opt_summary (vec<symtab_node *> nodes);
62 /* Number of LDPR values known to GCC. */
63 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
65 /* All node orders are ofsetted by ORDER_BASE. */
66 static int order_base;
68 /* Cgraph streaming is organized as set of record whose type
69 is indicated by a tag. */
70 enum LTO_symtab_tags
72 /* Must leave 0 for the stopper. */
74 /* Cgraph node without body available. */
75 LTO_symtab_unavail_node = 1,
76 /* Cgraph node with function body. */
77 LTO_symtab_analyzed_node,
78 /* Cgraph edges. */
79 LTO_symtab_edge,
80 LTO_symtab_indirect_edge,
81 LTO_symtab_variable,
82 LTO_symtab_last_tag
85 /* Create a new symtab encoder.
86 if FOR_INPUT, the encoder allocate only datastructures needed
87 to read the symtab. */
89 lto_symtab_encoder_t
90 lto_symtab_encoder_new (bool for_input)
92 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
94 if (!for_input)
95 encoder->map = pointer_map_create ();
96 encoder->nodes.create (0);
97 return encoder;
101 /* Delete ENCODER and its components. */
103 void
104 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
106 encoder->nodes.release ();
107 if (encoder->map)
108 pointer_map_destroy (encoder->map);
109 free (encoder);
113 /* Return the existing reference number of NODE in the symtab encoder in
114 output block OB. Assign a new reference if this is the first time
115 NODE is encoded. */
118 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
119 symtab_node *node)
121 int ref;
122 void **slot;
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 slot = pointer_map_contains (encoder->map, node);
134 if (!slot || !*slot)
136 lto_encoder_entry entry = {node, false, false, false};
137 ref = encoder->nodes.length ();
138 if (!slot)
139 slot = pointer_map_insert (encoder->map, node);
140 *slot = (void *) (intptr_t) (ref + 1);
141 encoder->nodes.safe_push (entry);
143 else
144 ref = (size_t) *slot - 1;
146 return ref;
149 /* Remove NODE from encoder. */
151 bool
152 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
153 symtab_node *node)
155 void **slot, **last_slot;
156 int index;
157 lto_encoder_entry last_node;
159 slot = pointer_map_contains (encoder->map, node);
160 if (slot == NULL || !*slot)
161 return false;
163 index = (size_t) *slot - 1;
164 gcc_checking_assert (encoder->nodes[index].node == node);
166 /* Remove from vector. We do this by swapping node with the last element
167 of the vector. */
168 last_node = encoder->nodes.pop ();
169 if (last_node.node != node)
171 last_slot = pointer_map_contains (encoder->map, last_node.node);
172 gcc_checking_assert (last_slot && *last_slot);
173 *last_slot = (void *)(size_t) (index + 1);
175 /* Move the last element to the original spot of NODE. */
176 encoder->nodes[index] = last_node;
179 /* Remove element from hash table. */
180 *slot = NULL;
181 return true;
185 /* Return TRUE if we should encode initializer of NODE (if any). */
187 bool
188 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
189 struct cgraph_node *node)
191 int index = lto_symtab_encoder_lookup (encoder, node);
192 return encoder->nodes[index].body;
195 /* Return TRUE if we should encode body of NODE (if any). */
197 static void
198 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
199 struct cgraph_node *node)
201 int index = lto_symtab_encoder_encode (encoder, node);
202 gcc_checking_assert (encoder->nodes[index].node == node);
203 encoder->nodes[index].body = true;
206 /* Return TRUE if we should encode initializer of NODE (if any). */
208 bool
209 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
210 varpool_node *node)
212 int index = lto_symtab_encoder_lookup (encoder, node);
213 if (index == LCC_NOT_FOUND)
214 return false;
215 return encoder->nodes[index].initializer;
218 /* Return TRUE if we should encode initializer of NODE (if any). */
220 static void
221 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
222 varpool_node *node)
224 int index = lto_symtab_encoder_lookup (encoder, node);
225 encoder->nodes[index].initializer = true;
228 /* Return TRUE if we should encode initializer of NODE (if any). */
230 bool
231 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
232 symtab_node *node)
234 int index = lto_symtab_encoder_lookup (encoder, node);
235 if (index == LCC_NOT_FOUND)
236 return false;
237 return encoder->nodes[index].in_partition;
240 /* Return TRUE if we should encode body of NODE (if any). */
242 void
243 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
244 symtab_node *node)
246 int index = lto_symtab_encoder_encode (encoder, node);
247 encoder->nodes[index].in_partition = true;
250 /* Output the cgraph EDGE to OB using ENCODER. */
252 static void
253 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
254 lto_symtab_encoder_t encoder)
256 unsigned int uid;
257 intptr_t ref;
258 struct bitpack_d bp;
260 if (edge->indirect_unknown_callee)
261 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
262 LTO_symtab_indirect_edge);
263 else
264 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
265 LTO_symtab_edge);
267 ref = lto_symtab_encoder_lookup (encoder, edge->caller);
268 gcc_assert (ref != LCC_NOT_FOUND);
269 streamer_write_hwi_stream (ob->main_stream, ref);
271 if (!edge->indirect_unknown_callee)
273 ref = lto_symtab_encoder_lookup (encoder, edge->callee);
274 gcc_assert (ref != LCC_NOT_FOUND);
275 streamer_write_hwi_stream (ob->main_stream, ref);
278 streamer_write_gcov_count_stream (ob->main_stream, edge->count);
280 bp = bitpack_create (ob->main_stream);
281 uid = (!gimple_has_body_p (edge->caller->decl)
282 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt) + 1);
283 bp_pack_enum (&bp, cgraph_inline_failed_t,
284 CIF_N_REASONS, edge->inline_failed);
285 bp_pack_var_len_unsigned (&bp, uid);
286 bp_pack_var_len_unsigned (&bp, edge->frequency);
287 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
288 bp_pack_value (&bp, edge->speculative, 1);
289 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
290 bp_pack_value (&bp, edge->can_throw_external, 1);
291 if (edge->indirect_unknown_callee)
293 int flags = edge->indirect_info->ecf_flags;
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);
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
304 | ECF_LEAF
305 | ECF_NOVOPS)));
307 streamer_write_bitpack (&bp);
308 if (edge->indirect_unknown_callee)
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);
318 /* Return if LIST contain references from other partitions. */
320 bool
321 referenced_from_other_partition_p (struct ipa_ref_list *list, lto_symtab_encoder_t encoder)
323 int i;
324 struct ipa_ref *ref;
325 for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
327 if (ref->referring->in_other_partition
328 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
329 return true;
331 return false;
334 /* Return true when node is reachable from other partition. */
336 bool
337 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
339 struct cgraph_edge *e;
340 if (!node->definition)
341 return false;
342 if (node->global.inlined_to)
343 return false;
344 for (e = node->callers; e; e = e->next_caller)
345 if (e->caller->in_other_partition
346 || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
347 return true;
348 return false;
351 /* Return if LIST contain references from other partitions. */
353 bool
354 referenced_from_this_partition_p (struct ipa_ref_list *list,
355 lto_symtab_encoder_t encoder)
357 int i;
358 struct ipa_ref *ref;
359 for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
360 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
361 return true;
362 return false;
365 /* Return true when node is reachable from other partition. */
367 bool
368 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
370 struct cgraph_edge *e;
371 for (e = node->callers; e; e = e->next_caller)
372 if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
373 return true;
374 return false;
377 /* Output the cgraph NODE to OB. ENCODER is used to find the
378 reference number of NODE->inlined_to. SET is the set of nodes we
379 are writing to the current file. If NODE is not in SET, then NODE
380 is a boundary of a cgraph_node_set and we pretend NODE just has a
381 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
382 that have had their callgraph node written so far. This is used to
383 determine if NODE is a clone of a previously written node. */
385 static void
386 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
387 lto_symtab_encoder_t encoder)
389 unsigned int tag;
390 struct bitpack_d bp;
391 bool boundary_p;
392 intptr_t ref;
393 bool in_other_partition = false;
394 struct cgraph_node *clone_of, *ultimate_clone_of;
395 ipa_opt_pass_d *pass;
396 int i;
397 bool alias_p;
398 const char *comdat;
399 tree group;
401 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
403 if (node->analyzed && !boundary_p)
404 tag = LTO_symtab_analyzed_node;
405 else
406 tag = LTO_symtab_unavail_node;
408 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
409 tag);
410 streamer_write_hwi_stream (ob->main_stream, node->order);
412 /* In WPA mode, we only output part of the call-graph. Also, we
413 fake cgraph node attributes. There are two cases that we care.
415 Boundary nodes: There are nodes that are not part of SET but are
416 called from within SET. We artificially make them look like
417 externally visible nodes with no function body.
419 Cherry-picked nodes: These are nodes we pulled from other
420 translation units into SET during IPA-inlining. We make them as
421 local static nodes to prevent clashes with other local statics. */
422 if (boundary_p && node->analyzed
423 && symtab_get_symbol_partitioning_class (node) == SYMBOL_PARTITION)
425 /* Inline clones can not be part of boundary.
426 gcc_assert (!node->global.inlined_to);
428 FIXME: At the moment they can be, when partition contains an inline
429 clone that is clone of inline clone from outside partition. We can
430 reshape the clone tree and make other tree to be the root, but it
431 needs a bit extra work and will be promplty done by cgraph_remove_node
432 after reading back. */
433 in_other_partition = 1;
436 clone_of = node->clone_of;
437 while (clone_of
438 && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
439 if (clone_of->prev_sibling_clone)
440 clone_of = clone_of->prev_sibling_clone;
441 else
442 clone_of = clone_of->clone_of;
444 /* See if body of the master function is output. If not, we are seeing only
445 an declaration and we do not need to pass down clone tree. */
446 ultimate_clone_of = clone_of;
447 while (ultimate_clone_of && ultimate_clone_of->clone_of)
448 ultimate_clone_of = ultimate_clone_of->clone_of;
450 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
451 clone_of = NULL;
453 if (tag == LTO_symtab_analyzed_node)
454 gcc_assert (clone_of || !node->clone_of);
455 if (!clone_of)
456 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
457 else
458 streamer_write_hwi_stream (ob->main_stream, ref);
461 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
462 streamer_write_gcov_count_stream (ob->main_stream, node->count);
463 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
465 streamer_write_hwi_stream (ob->main_stream,
466 node->ipa_transforms_to_apply.length ());
467 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
468 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
470 if (tag == LTO_symtab_analyzed_node)
472 if (node->global.inlined_to)
474 ref = lto_symtab_encoder_lookup (encoder, node->global.inlined_to);
475 gcc_assert (ref != LCC_NOT_FOUND);
477 else
478 ref = LCC_NOT_FOUND;
480 streamer_write_hwi_stream (ob->main_stream, ref);
483 group = node->get_comdat_group ();
484 if (group)
485 comdat = IDENTIFIER_POINTER (group);
486 else
487 comdat = "";
488 lto_output_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 streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
504 bp = bitpack_create (ob->main_stream);
505 bp_pack_value (&bp, node->local.local, 1);
506 bp_pack_value (&bp, node->externally_visible, 1);
507 bp_pack_value (&bp, node->definition, 1);
508 bp_pack_value (&bp, node->local.versionable, 1);
509 bp_pack_value (&bp, node->local.can_change_signature, 1);
510 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
511 bp_pack_value (&bp, node->force_output, 1);
512 bp_pack_value (&bp, node->forced_by_abi, 1);
513 bp_pack_value (&bp, node->unique_name, 1);
514 bp_pack_value (&bp, node->body_removed, 1);
515 bp_pack_value (&bp, node->address_taken, 1);
516 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
517 && symtab_get_symbol_partitioning_class (node) == SYMBOL_PARTITION
518 && (reachable_from_other_partition_p (node, encoder)
519 || referenced_from_other_partition_p (&node->ref_list,
520 encoder)), 1);
521 bp_pack_value (&bp, node->lowered, 1);
522 bp_pack_value (&bp, in_other_partition, 1);
523 /* Real aliases in a boundary become non-aliases. However we still stream
524 alias info on weakrefs.
525 TODO: We lose a bit of information here - when we know that variable is
526 defined in other unit, we may use the info on aliases to resolve
527 symbol1 != symbol2 type tests that we can do only for locally defined objects
528 otherwise. */
529 alias_p = node->alias && (!boundary_p || node->weakref);
530 bp_pack_value (&bp, alias_p, 1);
531 bp_pack_value (&bp, node->weakref, 1);
532 bp_pack_value (&bp, node->frequency, 2);
533 bp_pack_value (&bp, node->only_called_at_startup, 1);
534 bp_pack_value (&bp, node->only_called_at_exit, 1);
535 bp_pack_value (&bp, node->tm_clone, 1);
536 bp_pack_value (&bp, node->calls_comdat_local, 1);
537 bp_pack_value (&bp, node->thunk.thunk_p && !boundary_p, 1);
538 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
539 LDPR_NUM_KNOWN, node->resolution);
540 streamer_write_bitpack (&bp);
542 if (node->thunk.thunk_p && !boundary_p)
544 streamer_write_uhwi_stream
545 (ob->main_stream,
546 1 + (node->thunk.this_adjusting != 0) * 2
547 + (node->thunk.virtual_offset_p != 0) * 4);
548 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
549 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
551 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
554 /* Output the varpool NODE to OB.
555 If NODE is not in SET, then NODE is a boundary. */
557 static void
558 lto_output_varpool_node (struct lto_simple_output_block *ob, varpool_node *node,
559 lto_symtab_encoder_t encoder)
561 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
562 struct bitpack_d bp;
563 int ref;
564 bool alias_p;
565 const char *comdat;
566 tree group;
568 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
569 LTO_symtab_variable);
570 streamer_write_hwi_stream (ob->main_stream, node->order);
571 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
572 bp = bitpack_create (ob->main_stream);
573 bp_pack_value (&bp, node->externally_visible, 1);
574 bp_pack_value (&bp, node->force_output, 1);
575 bp_pack_value (&bp, node->forced_by_abi, 1);
576 bp_pack_value (&bp, node->unique_name, 1);
577 bp_pack_value (&bp, node->body_removed, 1);
578 bp_pack_value (&bp, node->writeonly, 1);
579 bp_pack_value (&bp, node->definition, 1);
580 alias_p = node->alias && (!boundary_p || node->weakref);
581 bp_pack_value (&bp, alias_p, 1);
582 bp_pack_value (&bp, node->weakref, 1);
583 bp_pack_value (&bp, node->analyzed && !boundary_p, 1);
584 gcc_assert (node->definition || !node->analyzed);
585 /* Constant pool initializers can be de-unified into individual ltrans units.
586 FIXME: Alternatively at -Os we may want to avoid generating for them the local
587 labels and share them across LTRANS partitions. */
588 if (symtab_get_symbol_partitioning_class (node) != SYMBOL_PARTITION)
590 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
591 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
593 else
595 bp_pack_value (&bp, node->definition
596 && referenced_from_other_partition_p (&node->ref_list,
597 encoder), 1);
598 bp_pack_value (&bp, node->analyzed
599 && boundary_p && !DECL_EXTERNAL (node->decl), 1);
600 /* in_other_partition. */
602 streamer_write_bitpack (&bp);
603 group = node->get_comdat_group ();
604 if (group)
605 comdat = IDENTIFIER_POINTER (group);
606 else
607 comdat = "";
608 lto_output_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
609 if (group)
611 if (node->same_comdat_group && !boundary_p)
613 ref = lto_symtab_encoder_lookup (encoder,
614 node->same_comdat_group);
615 gcc_assert (ref != LCC_NOT_FOUND);
617 else
618 ref = LCC_NOT_FOUND;
619 streamer_write_hwi_stream (ob->main_stream, ref);
621 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
622 LDPR_NUM_KNOWN, node->resolution);
625 /* Output the varpool NODE to OB.
626 If NODE is not in SET, then NODE is a boundary. */
628 static void
629 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
630 lto_symtab_encoder_t encoder)
632 struct bitpack_d bp;
633 int nref;
634 int uid = ref->lto_stmt_uid;
635 struct cgraph_node *node;
637 bp = bitpack_create (ob->main_stream);
638 bp_pack_value (&bp, ref->use, 2);
639 bp_pack_value (&bp, ref->speculative, 1);
640 streamer_write_bitpack (&bp);
641 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
642 gcc_assert (nref != LCC_NOT_FOUND);
643 streamer_write_hwi_stream (ob->main_stream, nref);
645 node = dyn_cast <cgraph_node *> (ref->referring);
646 if (node)
648 if (ref->stmt)
649 uid = gimple_uid (ref->stmt) + 1;
650 streamer_write_hwi_stream (ob->main_stream, uid);
654 /* Stream out profile_summary to OB. */
656 static void
657 output_profile_summary (struct lto_simple_output_block *ob)
659 unsigned h_ix;
660 struct bitpack_d bp;
662 if (profile_info)
664 /* We do not output num and run_max, they are not used by
665 GCC profile feedback and they are difficult to merge from multiple
666 units. */
667 gcc_assert (profile_info->runs);
668 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
669 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
671 /* sum_all is needed for computing the working set with the
672 histogram. */
673 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
675 /* Create and output a bitpack of non-zero histogram entries indices. */
676 bp = bitpack_create (ob->main_stream);
677 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
678 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
679 streamer_write_bitpack (&bp);
680 /* Now stream out only those non-zero entries. */
681 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
683 if (!profile_info->histogram[h_ix].num_counters)
684 continue;
685 streamer_write_gcov_count_stream (ob->main_stream,
686 profile_info->histogram[h_ix].num_counters);
687 streamer_write_gcov_count_stream (ob->main_stream,
688 profile_info->histogram[h_ix].min_value);
689 streamer_write_gcov_count_stream (ob->main_stream,
690 profile_info->histogram[h_ix].cum_value);
692 /* IPA-profile computes hot bb threshold based on cumulated
693 whole program profile. We need to stream it down to ltrans. */
694 if (flag_wpa)
695 streamer_write_gcov_count_stream (ob->main_stream,
696 get_hot_bb_threshold ());
698 else
699 streamer_write_uhwi_stream (ob->main_stream, 0);
702 /* Output all callees or indirect outgoing edges. EDGE must be the first such
703 edge. */
705 static void
706 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
707 struct lto_simple_output_block *ob,
708 lto_symtab_encoder_t encoder)
710 if (!edge)
711 return;
713 /* Output edges in backward direction, so the reconstructed callgraph match
714 and it is easy to associate call sites in the IPA pass summaries. */
715 while (edge->next_callee)
716 edge = edge->next_callee;
717 for (; edge; edge = edge->prev_callee)
718 lto_output_edge (ob, edge, encoder);
721 /* Output the part of the cgraph in SET. */
723 static void
724 output_refs (lto_symtab_encoder_t encoder)
726 lto_symtab_encoder_iterator lsei;
727 struct lto_simple_output_block *ob;
728 int count;
729 struct ipa_ref *ref;
730 int i;
732 ob = lto_create_simple_output_block (LTO_section_refs);
734 for (lsei = lsei_start_in_partition (encoder); !lsei_end_p (lsei);
735 lsei_next_in_partition (&lsei))
737 symtab_node *node = lsei_node (lsei);
739 count = ipa_ref_list_nreferences (&node->ref_list);
740 if (count)
742 streamer_write_gcov_count_stream (ob->main_stream, count);
743 streamer_write_uhwi_stream (ob->main_stream,
744 lto_symtab_encoder_lookup (encoder, node));
745 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list,
746 i, ref); i++)
747 lto_output_ref (ob, ref, encoder);
751 streamer_write_uhwi_stream (ob->main_stream, 0);
753 lto_destroy_simple_output_block (ob);
756 /* Add NODE into encoder as well as nodes it is cloned from.
757 Do it in a way so clones appear first. */
759 static void
760 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
761 bool include_body)
763 if (node->clone_of)
764 add_node_to (encoder, node->clone_of, include_body);
765 else if (include_body)
766 lto_set_symtab_encoder_encode_body (encoder, node);
767 lto_symtab_encoder_encode (encoder, node);
770 /* Add all references in LIST to encoders. */
772 static void
773 add_references (lto_symtab_encoder_t encoder,
774 struct ipa_ref_list *list)
776 int i;
777 struct ipa_ref *ref;
778 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
779 if (is_a <cgraph_node *> (ref->referred))
780 add_node_to (encoder, ipa_ref_node (ref), false);
781 else
782 lto_symtab_encoder_encode (encoder, ref->referred);
785 /* Find all symbols we want to stream into given partition and insert them
786 to encoders.
788 The function actually replaces IN_ENCODER by new one. The reason is that
789 streaming code needs clone's origin to be streamed before clone. This
790 means that we need to insert the nodes in specific order. This order is
791 ignored by the partitioning logic earlier. */
793 lto_symtab_encoder_t
794 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
796 struct cgraph_edge *edge;
797 int i;
798 lto_symtab_encoder_t encoder;
799 lto_symtab_encoder_iterator lsei;
800 struct pointer_set_t *reachable_call_targets = pointer_set_create ();
802 encoder = lto_symtab_encoder_new (false);
804 /* Go over all entries in the IN_ENCODER and duplicate them to
805 ENCODER. At the same time insert masters of clones so
806 every master appears before clone. */
807 for (lsei = lsei_start_function_in_partition (in_encoder);
808 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
810 struct cgraph_node *node = lsei_cgraph_node (lsei);
811 add_node_to (encoder, node, true);
812 lto_set_symtab_encoder_in_partition (encoder, node);
813 add_references (encoder, &node->ref_list);
814 /* For proper debug info, we need to ship the origins, too. */
815 if (DECL_ABSTRACT_ORIGIN (node->decl))
817 struct cgraph_node *origin_node
818 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (node->decl));
819 add_node_to (encoder, origin_node, true);
822 for (lsei = lsei_start_variable_in_partition (in_encoder);
823 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
825 varpool_node *vnode = lsei_varpool_node (lsei);
827 lto_set_symtab_encoder_in_partition (encoder, vnode);
828 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
829 add_references (encoder, &vnode->ref_list);
830 /* For proper debug info, we need to ship the origins, too. */
831 if (DECL_ABSTRACT_ORIGIN (vnode->decl))
833 varpool_node *origin_node
834 = varpool_get_node (DECL_ABSTRACT_ORIGIN (vnode->decl));
835 lto_set_symtab_encoder_in_partition (encoder, origin_node);
838 /* Pickle in also the initializer of all referenced readonly variables
839 to help folding. Constant pool variables are not shared, so we must
840 pickle those too. */
841 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
843 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
844 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
846 if (!lto_symtab_encoder_encode_initializer_p (encoder,
847 vnode)
848 && ctor_for_folding (vnode->decl) != error_mark_node)
850 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
851 add_references (encoder, &vnode->ref_list);
856 /* Go over all the nodes again to include callees that are not in
857 SET. */
858 for (lsei = lsei_start_function_in_partition (encoder);
859 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
861 struct cgraph_node *node = lsei_cgraph_node (lsei);
862 for (edge = node->callees; edge; edge = edge->next_callee)
864 struct cgraph_node *callee = edge->callee;
865 if (!lto_symtab_encoder_in_partition_p (encoder, callee))
867 /* We should have moved all the inlines. */
868 gcc_assert (!callee->global.inlined_to);
869 add_node_to (encoder, callee, false);
872 /* Add all possible targets for late devirtualization. */
873 if (flag_devirtualize)
874 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
875 if (edge->indirect_info->polymorphic)
877 unsigned int i;
878 void *cache_token;
879 bool final;
880 vec <cgraph_node *>targets
881 = possible_polymorphic_call_targets
882 (edge, &final, &cache_token);
883 if (!pointer_set_insert (reachable_call_targets,
884 cache_token))
886 for (i = 0; i < targets.length (); i++)
888 struct cgraph_node *callee = targets[i];
890 /* Adding an external declarations into the unit serves
891 no purpose and just increases its boundary. */
892 if (callee->definition
893 && !lto_symtab_encoder_in_partition_p
894 (encoder, callee))
896 gcc_assert (!callee->global.inlined_to);
897 add_node_to (encoder, callee, false);
903 lto_symtab_encoder_delete (in_encoder);
904 pointer_set_destroy (reachable_call_targets);
905 return encoder;
908 /* Output the part of the symtab in SET and VSET. */
910 void
911 output_symtab (void)
913 struct cgraph_node *node;
914 struct lto_simple_output_block *ob;
915 lto_symtab_encoder_iterator lsei;
916 int i, n_nodes;
917 lto_symtab_encoder_t encoder;
919 if (flag_wpa)
920 output_cgraph_opt_summary ();
922 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
924 output_profile_summary (ob);
926 /* An encoder for cgraph nodes should have been created by
927 ipa_write_summaries_1. */
928 gcc_assert (ob->decl_state->symtab_node_encoder);
929 encoder = ob->decl_state->symtab_node_encoder;
931 /* Write out the nodes. We must first output a node and then its clones,
932 otherwise at a time reading back the node there would be nothing to clone
933 from. */
934 n_nodes = lto_symtab_encoder_size (encoder);
935 for (i = 0; i < n_nodes; i++)
937 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
938 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
939 lto_output_node (ob, cnode, encoder);
940 else
941 lto_output_varpool_node (ob, varpool (node), encoder);
945 /* Go over the nodes in SET again to write edges. */
946 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
947 lsei_next_function_in_partition (&lsei))
949 node = lsei_cgraph_node (lsei);
950 output_outgoing_cgraph_edges (node->callees, ob, encoder);
951 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
954 streamer_write_uhwi_stream (ob->main_stream, 0);
956 lto_destroy_simple_output_block (ob);
958 /* Emit toplevel asms.
959 When doing WPA we must output every asm just once. Since we do not partition asm
960 nodes at all, output them to first output. This is kind of hack, but should work
961 well. */
962 if (!asm_nodes_output)
964 asm_nodes_output = true;
965 lto_output_toplevel_asms ();
968 output_refs (encoder);
971 /* Return COMDAT_GROUP encoded in IB as a plain string. */
973 static tree
974 read_comdat_group (struct lto_input_block *ib)
976 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
977 tree group;
979 if (ib->data[ib->p + len])
980 lto_section_overrun (ib);
981 if (!len)
983 ib->p++;
984 return NULL;
986 group = get_identifier (ib->data + ib->p);
987 ib->p += len + 1;
988 return group;
991 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
992 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
993 NODE or to replace the values in it, for instance because the first
994 time we saw it, the function body was not available but now it
995 is. BP is a bitpack with all the bitflags for NODE read from the
996 stream. */
998 static void
999 input_overwrite_node (struct lto_file_decl_data *file_data,
1000 struct cgraph_node *node,
1001 enum LTO_symtab_tags tag,
1002 struct bitpack_d *bp)
1004 node->aux = (void *) tag;
1005 node->lto_file_data = file_data;
1007 node->local.local = bp_unpack_value (bp, 1);
1008 node->externally_visible = bp_unpack_value (bp, 1);
1009 node->definition = bp_unpack_value (bp, 1);
1010 node->local.versionable = bp_unpack_value (bp, 1);
1011 node->local.can_change_signature = bp_unpack_value (bp, 1);
1012 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
1013 node->force_output = bp_unpack_value (bp, 1);
1014 node->forced_by_abi = bp_unpack_value (bp, 1);
1015 node->unique_name = bp_unpack_value (bp, 1);
1016 node->body_removed = bp_unpack_value (bp, 1);
1017 node->address_taken = bp_unpack_value (bp, 1);
1018 node->used_from_other_partition = bp_unpack_value (bp, 1);
1019 node->lowered = bp_unpack_value (bp, 1);
1020 node->analyzed = tag == LTO_symtab_analyzed_node;
1021 node->in_other_partition = bp_unpack_value (bp, 1);
1022 if (node->in_other_partition
1023 /* Avoid updating decl when we are seeing just inline clone.
1024 When inlining function that has functions already inlined into it,
1025 we produce clones of inline clones.
1027 WPA partitioning might put each clone into different unit and
1028 we might end up streaming inline clone from other partition
1029 to support clone we are interested in. */
1030 && (!node->clone_of
1031 || node->clone_of->decl != node->decl))
1033 DECL_EXTERNAL (node->decl) = 1;
1034 TREE_STATIC (node->decl) = 0;
1036 node->alias = bp_unpack_value (bp, 1);
1037 node->weakref = bp_unpack_value (bp, 1);
1038 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
1039 node->only_called_at_startup = bp_unpack_value (bp, 1);
1040 node->only_called_at_exit = bp_unpack_value (bp, 1);
1041 node->tm_clone = bp_unpack_value (bp, 1);
1042 node->calls_comdat_local = bp_unpack_value (bp, 1);
1043 node->thunk.thunk_p = bp_unpack_value (bp, 1);
1044 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
1045 LDPR_NUM_KNOWN);
1046 gcc_assert (flag_ltrans
1047 || (!node->in_other_partition
1048 && !node->used_from_other_partition));
1051 /* Return string alias is alias of. */
1053 static tree
1054 get_alias_symbol (tree decl)
1056 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1057 return get_identifier (TREE_STRING_POINTER
1058 (TREE_VALUE (TREE_VALUE (alias))));
1061 /* Read a node from input_block IB. TAG is the node's tag just read.
1062 Return the node read or overwriten. */
1064 static struct cgraph_node *
1065 input_node (struct lto_file_decl_data *file_data,
1066 struct lto_input_block *ib,
1067 enum LTO_symtab_tags tag,
1068 vec<symtab_node *> nodes)
1070 gcc::pass_manager *passes = g->get_passes ();
1071 tree fn_decl;
1072 struct cgraph_node *node;
1073 struct bitpack_d bp;
1074 unsigned decl_index;
1075 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1076 int clone_ref;
1077 int order;
1078 int i, count;
1079 tree group;
1081 order = streamer_read_hwi (ib) + order_base;
1082 clone_ref = streamer_read_hwi (ib);
1084 decl_index = streamer_read_uhwi (ib);
1085 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1087 if (clone_ref != LCC_NOT_FOUND)
1089 node = cgraph_clone_node (cgraph (nodes[clone_ref]), fn_decl,
1090 0, CGRAPH_FREQ_BASE, false,
1091 vNULL, false, NULL, NULL);
1093 else
1095 /* Declaration of functions can be already merged with a declaration
1096 from other input file. We keep cgraph unmerged until after streaming
1097 of ipa passes is done. Alays forcingly create a fresh node. */
1098 node = cgraph_create_empty_node ();
1099 node->decl = fn_decl;
1100 symtab_register_node (node);
1103 node->order = order;
1104 if (order >= symtab_order)
1105 symtab_order = order + 1;
1107 node->count = streamer_read_gcov_count (ib);
1108 node->count_materialization_scale = streamer_read_hwi (ib);
1110 count = streamer_read_hwi (ib);
1111 node->ipa_transforms_to_apply = vNULL;
1112 for (i = 0; i < count; i++)
1114 opt_pass *pass;
1115 int pid = streamer_read_hwi (ib);
1117 gcc_assert (pid < passes->passes_by_id_size);
1118 pass = passes->passes_by_id[pid];
1119 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *) pass);
1122 if (tag == LTO_symtab_analyzed_node)
1123 ref = streamer_read_hwi (ib);
1125 group = read_comdat_group (ib);
1126 if (group)
1127 ref2 = streamer_read_hwi (ib);
1129 /* Make sure that we have not read this node before. Nodes that
1130 have already been read will have their tag stored in the 'aux'
1131 field. Since built-in functions can be referenced in multiple
1132 functions, they are expected to be read more than once. */
1133 if (node->aux && !DECL_BUILT_IN (node->decl))
1134 internal_error ("bytecode stream: found multiple instances of cgraph "
1135 "node with uid %d", node->uid);
1137 node->tp_first_run = streamer_read_uhwi (ib);
1139 bp = streamer_read_bitpack (ib);
1141 input_overwrite_node (file_data, node, tag, &bp);
1143 /* Store a reference for now, and fix up later to be a pointer. */
1144 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
1146 if (group)
1148 node->set_comdat_group (group);
1149 /* Store a reference for now, and fix up later to be a pointer. */
1150 node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
1152 else
1153 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1155 if (node->thunk.thunk_p)
1157 int type = streamer_read_uhwi (ib);
1158 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1159 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1161 node->thunk.fixed_offset = fixed_offset;
1162 node->thunk.this_adjusting = (type & 2);
1163 node->thunk.virtual_value = virtual_value;
1164 node->thunk.virtual_offset_p = (type & 4);
1166 if (node->alias && !node->analyzed && node->weakref)
1167 node->alias_target = get_alias_symbol (node->decl);
1168 node->profile_id = streamer_read_hwi (ib);
1169 return node;
1172 /* Read a node from input_block IB. TAG is the node's tag just read.
1173 Return the node read or overwriten. */
1175 static varpool_node *
1176 input_varpool_node (struct lto_file_decl_data *file_data,
1177 struct lto_input_block *ib)
1179 int decl_index;
1180 tree var_decl;
1181 varpool_node *node;
1182 struct bitpack_d bp;
1183 int ref = LCC_NOT_FOUND;
1184 int order;
1185 tree group;
1187 order = streamer_read_hwi (ib) + order_base;
1188 decl_index = streamer_read_uhwi (ib);
1189 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1191 /* Declaration of functions can be already merged with a declaration
1192 from other input file. We keep cgraph unmerged until after streaming
1193 of ipa passes is done. Alays forcingly create a fresh node. */
1194 node = varpool_create_empty_node ();
1195 node->decl = var_decl;
1196 symtab_register_node (node);
1198 node->order = order;
1199 if (order >= symtab_order)
1200 symtab_order = order + 1;
1201 node->lto_file_data = file_data;
1203 bp = streamer_read_bitpack (ib);
1204 node->externally_visible = bp_unpack_value (&bp, 1);
1205 node->force_output = bp_unpack_value (&bp, 1);
1206 node->forced_by_abi = bp_unpack_value (&bp, 1);
1207 node->unique_name = bp_unpack_value (&bp, 1);
1208 node->body_removed = bp_unpack_value (&bp, 1);
1209 node->writeonly = bp_unpack_value (&bp, 1);
1210 node->definition = bp_unpack_value (&bp, 1);
1211 node->alias = bp_unpack_value (&bp, 1);
1212 node->weakref = bp_unpack_value (&bp, 1);
1213 node->analyzed = bp_unpack_value (&bp, 1);
1214 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1215 node->in_other_partition = bp_unpack_value (&bp, 1);
1216 if (node->in_other_partition)
1218 DECL_EXTERNAL (node->decl) = 1;
1219 TREE_STATIC (node->decl) = 0;
1221 if (node->alias && !node->analyzed && node->weakref)
1222 node->alias_target = get_alias_symbol (node->decl);
1223 group = read_comdat_group (ib);
1224 if (group)
1226 node->set_comdat_group (group);
1227 ref = streamer_read_hwi (ib);
1228 /* Store a reference for now, and fix up later to be a pointer. */
1229 node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1231 else
1232 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1233 node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1234 LDPR_NUM_KNOWN);
1235 gcc_assert (flag_ltrans
1236 || (!node->in_other_partition
1237 && !node->used_from_other_partition));
1239 return node;
1242 /* Read a node from input_block IB. TAG is the node's tag just read.
1243 Return the node read or overwriten. */
1245 static void
1246 input_ref (struct lto_input_block *ib,
1247 symtab_node *referring_node,
1248 vec<symtab_node *> nodes)
1250 symtab_node *node = NULL;
1251 struct bitpack_d bp;
1252 enum ipa_ref_use use;
1253 bool speculative;
1254 struct ipa_ref *ref;
1256 bp = streamer_read_bitpack (ib);
1257 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1258 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1259 node = nodes[streamer_read_hwi (ib)];
1260 ref = ipa_record_reference (referring_node, node, use, NULL);
1261 ref->speculative = speculative;
1262 if (is_a <cgraph_node *> (referring_node))
1263 ref->lto_stmt_uid = streamer_read_hwi (ib);
1266 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1267 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1268 edge being read is indirect (in the sense that it has
1269 indirect_unknown_callee set). */
1271 static void
1272 input_edge (struct lto_input_block *ib, vec<symtab_node *> nodes,
1273 bool indirect)
1275 struct cgraph_node *caller, *callee;
1276 struct cgraph_edge *edge;
1277 unsigned int stmt_id;
1278 gcov_type count;
1279 int freq;
1280 cgraph_inline_failed_t inline_failed;
1281 struct bitpack_d bp;
1282 int ecf_flags = 0;
1284 caller = cgraph (nodes[streamer_read_hwi (ib)]);
1285 if (caller == NULL || caller->decl == NULL_TREE)
1286 internal_error ("bytecode stream: no caller found while reading edge");
1288 if (!indirect)
1290 callee = cgraph (nodes[streamer_read_hwi (ib)]);
1291 if (callee == NULL || callee->decl == NULL_TREE)
1292 internal_error ("bytecode stream: no callee found while reading edge");
1294 else
1295 callee = NULL;
1297 count = streamer_read_gcov_count (ib);
1299 bp = streamer_read_bitpack (ib);
1300 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_t, CIF_N_REASONS);
1301 stmt_id = bp_unpack_var_len_unsigned (&bp);
1302 freq = (int) bp_unpack_var_len_unsigned (&bp);
1304 if (indirect)
1305 edge = cgraph_create_indirect_edge (caller, NULL, 0, count, freq);
1306 else
1307 edge = cgraph_create_edge (caller, callee, NULL, count, freq);
1309 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1310 edge->speculative = bp_unpack_value (&bp, 1);
1311 edge->lto_stmt_uid = stmt_id;
1312 edge->inline_failed = inline_failed;
1313 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1314 edge->can_throw_external = bp_unpack_value (&bp, 1);
1315 if (indirect)
1317 if (bp_unpack_value (&bp, 1))
1318 ecf_flags |= ECF_CONST;
1319 if (bp_unpack_value (&bp, 1))
1320 ecf_flags |= ECF_PURE;
1321 if (bp_unpack_value (&bp, 1))
1322 ecf_flags |= ECF_NORETURN;
1323 if (bp_unpack_value (&bp, 1))
1324 ecf_flags |= ECF_MALLOC;
1325 if (bp_unpack_value (&bp, 1))
1326 ecf_flags |= ECF_NOTHROW;
1327 if (bp_unpack_value (&bp, 1))
1328 ecf_flags |= ECF_RETURNS_TWICE;
1329 edge->indirect_info->ecf_flags = ecf_flags;
1330 edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1331 if (edge->indirect_info->common_target_id)
1332 edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1337 /* Read a cgraph from IB using the info in FILE_DATA. */
1339 static vec<symtab_node *>
1340 input_cgraph_1 (struct lto_file_decl_data *file_data,
1341 struct lto_input_block *ib)
1343 enum LTO_symtab_tags tag;
1344 vec<symtab_node *> nodes = vNULL;
1345 symtab_node *node;
1346 unsigned i;
1348 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1349 order_base = symtab_order;
1350 while (tag)
1352 if (tag == LTO_symtab_edge)
1353 input_edge (ib, nodes, false);
1354 else if (tag == LTO_symtab_indirect_edge)
1355 input_edge (ib, nodes, true);
1356 else if (tag == LTO_symtab_variable)
1358 node = input_varpool_node (file_data, ib);
1359 nodes.safe_push (node);
1360 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1362 else
1364 node = input_node (file_data, ib, tag, nodes);
1365 if (node == NULL || node->decl == NULL_TREE)
1366 internal_error ("bytecode stream: found empty cgraph node");
1367 nodes.safe_push (node);
1368 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1371 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1374 lto_input_toplevel_asms (file_data, order_base);
1376 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1377 #ifdef ENABLE_CHECKING
1378 FOR_EACH_VEC_ELT (nodes, i, node)
1379 gcc_assert (node->aux || !is_a <cgraph_node *> (node));
1380 #endif
1381 FOR_EACH_VEC_ELT (nodes, i, node)
1383 int ref;
1384 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1386 ref = (int) (intptr_t) cnode->global.inlined_to;
1388 /* We share declaration of builtins, so we may read same node twice. */
1389 if (!node->aux)
1390 continue;
1391 node->aux = NULL;
1393 /* Fixup inlined_to from reference to pointer. */
1394 if (ref != LCC_NOT_FOUND)
1395 cgraph (node)->global.inlined_to = cgraph (nodes[ref]);
1396 else
1397 cnode->global.inlined_to = NULL;
1400 ref = (int) (intptr_t) node->same_comdat_group;
1402 /* Fixup same_comdat_group from reference to pointer. */
1403 if (ref != LCC_NOT_FOUND)
1404 node->same_comdat_group = nodes[ref];
1405 else
1406 node->same_comdat_group = NULL;
1408 FOR_EACH_VEC_ELT (nodes, i, node)
1409 node->aux = is_a <cgraph_node *> (node) ? (void *)1 : NULL;
1410 return nodes;
1413 /* Input ipa_refs. */
1415 static void
1416 input_refs (struct lto_input_block *ib,
1417 vec<symtab_node *> nodes)
1419 int count;
1420 int idx;
1421 while (true)
1423 symtab_node *node;
1424 count = streamer_read_uhwi (ib);
1425 if (!count)
1426 break;
1427 idx = streamer_read_uhwi (ib);
1428 node = nodes[idx];
1429 while (count)
1431 input_ref (ib, node, nodes);
1432 count--;
1438 static struct gcov_ctr_summary lto_gcov_summary;
1440 /* Input profile_info from IB. */
1441 static void
1442 input_profile_summary (struct lto_input_block *ib,
1443 struct lto_file_decl_data *file_data)
1445 unsigned h_ix;
1446 struct bitpack_d bp;
1447 unsigned int runs = streamer_read_uhwi (ib);
1448 if (runs)
1450 file_data->profile_info.runs = runs;
1451 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1452 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1454 memset (file_data->profile_info.histogram, 0,
1455 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1456 /* Input the bitpack of non-zero histogram indices. */
1457 bp = streamer_read_bitpack (ib);
1458 /* Read in and unpack the full bitpack, flagging non-zero
1459 histogram entries by setting the num_counters non-zero. */
1460 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1462 file_data->profile_info.histogram[h_ix].num_counters
1463 = bp_unpack_value (&bp, 1);
1465 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1467 if (!file_data->profile_info.histogram[h_ix].num_counters)
1468 continue;
1470 file_data->profile_info.histogram[h_ix].num_counters
1471 = streamer_read_gcov_count (ib);
1472 file_data->profile_info.histogram[h_ix].min_value
1473 = streamer_read_gcov_count (ib);
1474 file_data->profile_info.histogram[h_ix].cum_value
1475 = streamer_read_gcov_count (ib);
1477 /* IPA-profile computes hot bb threshold based on cumulated
1478 whole program profile. We need to stream it down to ltrans. */
1479 if (flag_ltrans)
1480 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1485 /* Rescale profile summaries to the same number of runs in the whole unit. */
1487 static void
1488 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1490 struct lto_file_decl_data *file_data;
1491 unsigned int j, h_ix;
1492 gcov_unsigned_t max_runs = 0;
1493 struct cgraph_node *node;
1494 struct cgraph_edge *edge;
1495 gcov_type saved_sum_all = 0;
1496 gcov_ctr_summary *saved_profile_info = 0;
1497 int saved_scale = 0;
1499 /* Find unit with maximal number of runs. If we ever get serious about
1500 roundoff errors, we might also consider computing smallest common
1501 multiply. */
1502 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1503 if (max_runs < file_data->profile_info.runs)
1504 max_runs = file_data->profile_info.runs;
1506 if (!max_runs)
1507 return;
1509 /* Simple overflow check. We probably don't need to support that many train
1510 runs. Such a large value probably imply data corruption anyway. */
1511 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1513 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1514 INT_MAX / REG_BR_PROB_BASE);
1515 return;
1518 profile_info = &lto_gcov_summary;
1519 lto_gcov_summary.runs = max_runs;
1520 lto_gcov_summary.sum_max = 0;
1521 memset (lto_gcov_summary.histogram, 0,
1522 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1524 /* Rescale all units to the maximal number of runs.
1525 sum_max can not be easily merged, as we have no idea what files come from
1526 the same run. We do not use the info anyway, so leave it 0. */
1527 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1528 if (file_data->profile_info.runs)
1530 int scale = GCOV_COMPUTE_SCALE (max_runs,
1531 file_data->profile_info.runs);
1532 lto_gcov_summary.sum_max
1533 = MAX (lto_gcov_summary.sum_max,
1534 apply_scale (file_data->profile_info.sum_max, scale));
1535 lto_gcov_summary.sum_all
1536 = MAX (lto_gcov_summary.sum_all,
1537 apply_scale (file_data->profile_info.sum_all, scale));
1538 /* Save a pointer to the profile_info with the largest
1539 scaled sum_all and the scale for use in merging the
1540 histogram. */
1541 if (!saved_profile_info
1542 || lto_gcov_summary.sum_all > saved_sum_all)
1544 saved_profile_info = &file_data->profile_info;
1545 saved_sum_all = lto_gcov_summary.sum_all;
1546 saved_scale = scale;
1550 gcc_assert (saved_profile_info);
1552 /* Scale up the histogram from the profile that had the largest
1553 scaled sum_all above. */
1554 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1556 /* Scale up the min value as we did the corresponding sum_all
1557 above. Use that to find the new histogram index. */
1558 gcov_type scaled_min
1559 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1560 saved_scale);
1561 /* The new index may be shared with another scaled histogram entry,
1562 so we need to account for a non-zero histogram entry at new_ix. */
1563 unsigned new_ix = gcov_histo_index (scaled_min);
1564 lto_gcov_summary.histogram[new_ix].min_value
1565 = (lto_gcov_summary.histogram[new_ix].num_counters
1566 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1567 : scaled_min);
1568 /* Some of the scaled counter values would ostensibly need to be placed
1569 into different (larger) histogram buckets, but we keep things simple
1570 here and place the scaled cumulative counter value in the bucket
1571 corresponding to the scaled minimum counter value. */
1572 lto_gcov_summary.histogram[new_ix].cum_value
1573 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1574 saved_scale);
1575 lto_gcov_summary.histogram[new_ix].num_counters
1576 += saved_profile_info->histogram[h_ix].num_counters;
1579 /* Watch roundoff errors. */
1580 if (lto_gcov_summary.sum_max < max_runs)
1581 lto_gcov_summary.sum_max = max_runs;
1583 /* If merging already happent at WPA time, we are done. */
1584 if (flag_ltrans)
1585 return;
1587 /* Now compute count_materialization_scale of each node.
1588 During LTRANS we already have values of count_materialization_scale
1589 computed, so just update them. */
1590 FOR_EACH_FUNCTION (node)
1591 if (node->lto_file_data
1592 && node->lto_file_data->profile_info.runs)
1594 int scale;
1596 scale = RDIV (node->count_materialization_scale * max_runs,
1597 node->lto_file_data->profile_info.runs);
1598 node->count_materialization_scale = scale;
1599 if (scale < 0)
1600 fatal_error ("Profile information in %s corrupted",
1601 file_data->file_name);
1603 if (scale == REG_BR_PROB_BASE)
1604 continue;
1605 for (edge = node->callees; edge; edge = edge->next_callee)
1606 edge->count = apply_scale (edge->count, scale);
1607 node->count = apply_scale (node->count, scale);
1611 /* Input and merge the symtab from each of the .o files passed to
1612 lto1. */
1614 void
1615 input_symtab (void)
1617 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1618 struct lto_file_decl_data *file_data;
1619 unsigned int j = 0;
1620 struct cgraph_node *node;
1622 while ((file_data = file_data_vec[j++]))
1624 const char *data;
1625 size_t len;
1626 struct lto_input_block *ib;
1627 vec<symtab_node *> nodes;
1629 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1630 &data, &len);
1631 if (!ib)
1632 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1633 input_profile_summary (ib, file_data);
1634 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1635 nodes = input_cgraph_1 (file_data, ib);
1636 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1637 ib, data, len);
1639 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1640 &data, &len);
1641 if (!ib)
1642 fatal_error ("cannot find LTO section refs in %s",
1643 file_data->file_name);
1644 input_refs (ib, nodes);
1645 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1646 ib, data, len);
1647 if (flag_ltrans)
1648 input_cgraph_opt_summary (nodes);
1649 nodes.release ();
1652 merge_profile_summaries (file_data_vec);
1653 get_working_sets ();
1656 /* Clear out the aux field that was used to store enough state to
1657 tell which nodes should be overwritten. */
1658 FOR_EACH_FUNCTION (node)
1660 /* Some nodes may have been created by cgraph_node. This
1661 happens when the callgraph contains nested functions. If the
1662 node for the parent function was never emitted to the gimple
1663 file, cgraph_node will create a node for it when setting the
1664 context of the nested function. */
1665 if (node->lto_file_data)
1666 node->aux = NULL;
1670 /* True when we need optimization summary for NODE. */
1672 static int
1673 output_cgraph_opt_summary_p (struct cgraph_node *node)
1675 return (node->clone_of
1676 && (node->clone.tree_map
1677 || node->clone.args_to_skip
1678 || node->clone.combined_args_to_skip));
1681 /* Output optimization summary for EDGE to OB. */
1682 static void
1683 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1684 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1688 /* Output optimization summary for NODE to OB. */
1690 static void
1691 output_node_opt_summary (struct output_block *ob,
1692 struct cgraph_node *node,
1693 lto_symtab_encoder_t encoder)
1695 unsigned int index;
1696 bitmap_iterator bi;
1697 struct ipa_replace_map *map;
1698 struct bitpack_d bp;
1699 int i;
1700 struct cgraph_edge *e;
1702 if (node->clone.args_to_skip)
1704 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1705 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1706 streamer_write_uhwi (ob, index);
1708 else
1709 streamer_write_uhwi (ob, 0);
1710 if (node->clone.combined_args_to_skip)
1712 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1713 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1714 streamer_write_uhwi (ob, index);
1716 else
1717 streamer_write_uhwi (ob, 0);
1718 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1719 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1721 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1722 mechanism to store function local declarations into summaries. */
1723 gcc_assert (!map->old_tree);
1724 streamer_write_uhwi (ob, map->parm_num);
1725 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1726 stream_write_tree (ob, map->new_tree, true);
1727 bp = bitpack_create (ob->main_stream);
1728 bp_pack_value (&bp, map->replace_p, 1);
1729 bp_pack_value (&bp, map->ref_p, 1);
1730 streamer_write_bitpack (&bp);
1733 if (lto_symtab_encoder_in_partition_p (encoder, node))
1735 for (e = node->callees; e; e = e->next_callee)
1736 output_edge_opt_summary (ob, e);
1737 for (e = node->indirect_calls; e; e = e->next_callee)
1738 output_edge_opt_summary (ob, e);
1742 /* Output optimization summaries stored in callgraph.
1743 At the moment it is the clone info structure. */
1745 static void
1746 output_cgraph_opt_summary (void)
1748 int i, n_nodes;
1749 lto_symtab_encoder_t encoder;
1750 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1751 unsigned count = 0;
1753 ob->cgraph_node = NULL;
1754 encoder = ob->decl_state->symtab_node_encoder;
1755 n_nodes = lto_symtab_encoder_size (encoder);
1756 for (i = 0; i < n_nodes; i++)
1758 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1759 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1760 if (cnode && output_cgraph_opt_summary_p (cnode))
1761 count++;
1763 streamer_write_uhwi (ob, count);
1764 for (i = 0; i < n_nodes; i++)
1766 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1767 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1768 if (cnode && output_cgraph_opt_summary_p (cnode))
1770 streamer_write_uhwi (ob, i);
1771 output_node_opt_summary (ob, cnode, encoder);
1774 produce_asm (ob, NULL);
1775 destroy_output_block (ob);
1778 /* Input optimisation summary of EDGE. */
1780 static void
1781 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
1782 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
1786 /* Input optimisation summary of NODE. */
1788 static void
1789 input_node_opt_summary (struct cgraph_node *node,
1790 struct lto_input_block *ib_main,
1791 struct data_in *data_in)
1793 int i;
1794 int count;
1795 int bit;
1796 struct bitpack_d bp;
1797 struct cgraph_edge *e;
1799 count = streamer_read_uhwi (ib_main);
1800 if (count)
1801 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1802 for (i = 0; i < count; i++)
1804 bit = streamer_read_uhwi (ib_main);
1805 bitmap_set_bit (node->clone.args_to_skip, bit);
1807 count = streamer_read_uhwi (ib_main);
1808 if (count)
1809 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1810 for (i = 0; i < count; i++)
1812 bit = streamer_read_uhwi (ib_main);
1813 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1815 count = streamer_read_uhwi (ib_main);
1816 for (i = 0; i < count; i++)
1818 struct ipa_replace_map *map = ggc_alloc<ipa_replace_map> ();
1820 vec_safe_push (node->clone.tree_map, map);
1821 map->parm_num = streamer_read_uhwi (ib_main);
1822 map->old_tree = NULL;
1823 map->new_tree = stream_read_tree (ib_main, data_in);
1824 bp = streamer_read_bitpack (ib_main);
1825 map->replace_p = bp_unpack_value (&bp, 1);
1826 map->ref_p = bp_unpack_value (&bp, 1);
1828 for (e = node->callees; e; e = e->next_callee)
1829 input_edge_opt_summary (e, ib_main);
1830 for (e = node->indirect_calls; e; e = e->next_callee)
1831 input_edge_opt_summary (e, ib_main);
1834 /* Read section in file FILE_DATA of length LEN with data DATA. */
1836 static void
1837 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1838 const char *data, size_t len,
1839 vec<symtab_node *> nodes)
1841 const struct lto_function_header *header =
1842 (const struct lto_function_header *) data;
1843 const int cfg_offset = sizeof (struct lto_function_header);
1844 const int main_offset = cfg_offset + header->cfg_size;
1845 const int string_offset = main_offset + header->main_size;
1846 struct data_in *data_in;
1847 struct lto_input_block ib_main;
1848 unsigned int i;
1849 unsigned int count;
1851 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1852 header->main_size);
1854 data_in =
1855 lto_data_in_create (file_data, (const char *) data + string_offset,
1856 header->string_size, vNULL);
1857 count = streamer_read_uhwi (&ib_main);
1859 for (i = 0; i < count; i++)
1861 int ref = streamer_read_uhwi (&ib_main);
1862 input_node_opt_summary (cgraph (nodes[ref]),
1863 &ib_main, data_in);
1865 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1866 len);
1867 lto_data_in_delete (data_in);
1870 /* Input optimization summary of cgraph. */
1872 static void
1873 input_cgraph_opt_summary (vec<symtab_node *> nodes)
1875 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1876 struct lto_file_decl_data *file_data;
1877 unsigned int j = 0;
1879 while ((file_data = file_data_vec[j++]))
1881 size_t len;
1882 const char *data =
1883 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1884 &len);
1886 if (data)
1887 input_cgraph_opt_section (file_data, data, len, nodes);