2013-11-21 Edward Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / lto-cgraph.c
blob0f1a1c5e9a045f3622ce9e532581f4350cbfa753
1 /* Write and read the cgraph to the memory mapped representation of a
2 .o file.
4 Copyright (C) 2009-2013 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 "gimple.h"
30 #include "expr.h"
31 #include "flags.h"
32 #include "params.h"
33 #include "input.h"
34 #include "hashtab.h"
35 #include "langhooks.h"
36 #include "basic-block.h"
37 #include "bitmap.h"
38 #include "function.h"
39 #include "ggc.h"
40 #include "diagnostic-core.h"
41 #include "except.h"
42 #include "vec.h"
43 #include "timevar.h"
44 #include "pointer-set.h"
45 #include "lto-streamer.h"
46 #include "data-streamer.h"
47 #include "tree-streamer.h"
48 #include "gcov-io.h"
49 #include "tree-pass.h"
50 #include "profile.h"
51 #include "context.h"
52 #include "pass_manager.h"
53 #include "ipa-utils.h"
55 static void output_cgraph_opt_summary (void);
56 static void input_cgraph_opt_summary (vec<symtab_node *> nodes);
58 /* Number of LDPR values known to GCC. */
59 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
61 /* All node orders are ofsetted by ORDER_BASE. */
62 static int order_base;
64 /* Cgraph streaming is organized as set of record whose type
65 is indicated by a tag. */
66 enum LTO_symtab_tags
68 /* Must leave 0 for the stopper. */
70 /* Cgraph node without body available. */
71 LTO_symtab_unavail_node = 1,
72 /* Cgraph node with function body. */
73 LTO_symtab_analyzed_node,
74 /* Cgraph edges. */
75 LTO_symtab_edge,
76 LTO_symtab_indirect_edge,
77 LTO_symtab_variable,
78 LTO_symtab_last_tag
81 /* Create a new symtab encoder.
82 if FOR_INPUT, the encoder allocate only datastructures needed
83 to read the symtab. */
85 lto_symtab_encoder_t
86 lto_symtab_encoder_new (bool for_input)
88 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
90 if (!for_input)
91 encoder->map = pointer_map_create ();
92 encoder->nodes.create (0);
93 return encoder;
97 /* Delete ENCODER and its components. */
99 void
100 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
102 encoder->nodes.release ();
103 if (encoder->map)
104 pointer_map_destroy (encoder->map);
105 free (encoder);
109 /* Return the existing reference number of NODE in the symtab encoder in
110 output block OB. Assign a new reference if this is the first time
111 NODE is encoded. */
114 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
115 symtab_node *node)
117 int ref;
118 void **slot;
120 if (!encoder->map)
122 lto_encoder_entry entry = {node, false, false, false};
124 ref = encoder->nodes.length ();
125 encoder->nodes.safe_push (entry);
126 return ref;
129 slot = pointer_map_contains (encoder->map, node);
130 if (!slot || !*slot)
132 lto_encoder_entry entry = {node, false, false, false};
133 ref = encoder->nodes.length ();
134 if (!slot)
135 slot = pointer_map_insert (encoder->map, node);
136 *slot = (void *) (intptr_t) (ref + 1);
137 encoder->nodes.safe_push (entry);
139 else
140 ref = (size_t) *slot - 1;
142 return ref;
145 /* Remove NODE from encoder. */
147 bool
148 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
149 symtab_node *node)
151 void **slot, **last_slot;
152 int index;
153 lto_encoder_entry last_node;
155 slot = pointer_map_contains (encoder->map, node);
156 if (slot == NULL || !*slot)
157 return false;
159 index = (size_t) *slot - 1;
160 gcc_checking_assert (encoder->nodes[index].node == node);
162 /* Remove from vector. We do this by swapping node with the last element
163 of the vector. */
164 last_node = encoder->nodes.pop ();
165 if (last_node.node != node)
167 last_slot = pointer_map_contains (encoder->map, last_node.node);
168 gcc_checking_assert (last_slot && *last_slot);
169 *last_slot = (void *)(size_t) (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 *slot = NULL;
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 struct 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 struct 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_enum,
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 LIST contain references from other partitions. */
316 bool
317 referenced_from_other_partition_p (struct ipa_ref_list *list, lto_symtab_encoder_t encoder)
319 int i;
320 struct ipa_ref *ref;
321 for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
323 if (ref->referring->in_other_partition
324 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
325 return true;
327 return false;
330 /* Return true when node is reachable from other partition. */
332 bool
333 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
335 struct cgraph_edge *e;
336 if (!node->definition)
337 return false;
338 if (node->global.inlined_to)
339 return false;
340 for (e = node->callers; e; e = e->next_caller)
341 if (e->caller->in_other_partition
342 || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
343 return true;
344 return false;
347 /* Return if LIST contain references from other partitions. */
349 bool
350 referenced_from_this_partition_p (struct ipa_ref_list *list,
351 lto_symtab_encoder_t encoder)
353 int i;
354 struct ipa_ref *ref;
355 for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
356 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
357 return true;
358 return false;
361 /* Return true when node is reachable from other partition. */
363 bool
364 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
366 struct cgraph_edge *e;
367 for (e = node->callers; e; e = e->next_caller)
368 if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
369 return true;
370 return false;
373 /* Output the cgraph NODE to OB. ENCODER is used to find the
374 reference number of NODE->inlined_to. SET is the set of nodes we
375 are writing to the current file. If NODE is not in SET, then NODE
376 is a boundary of a cgraph_node_set and we pretend NODE just has a
377 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
378 that have had their callgraph node written so far. This is used to
379 determine if NODE is a clone of a previously written node. */
381 static void
382 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
383 lto_symtab_encoder_t encoder)
385 unsigned int tag;
386 struct bitpack_d bp;
387 bool boundary_p;
388 intptr_t ref;
389 bool in_other_partition = false;
390 struct cgraph_node *clone_of, *ultimate_clone_of;
391 struct ipa_opt_pass_d *pass;
392 int i;
393 bool alias_p;
395 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
397 if (node->analyzed && !boundary_p)
398 tag = LTO_symtab_analyzed_node;
399 else
400 tag = LTO_symtab_unavail_node;
402 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
403 tag);
404 streamer_write_hwi_stream (ob->main_stream, node->order);
406 /* In WPA mode, we only output part of the call-graph. Also, we
407 fake cgraph node attributes. There are two cases that we care.
409 Boundary nodes: There are nodes that are not part of SET but are
410 called from within SET. We artificially make them look like
411 externally visible nodes with no function body.
413 Cherry-picked nodes: These are nodes we pulled from other
414 translation units into SET during IPA-inlining. We make them as
415 local static nodes to prevent clashes with other local statics. */
416 if (boundary_p && node->analyzed && !DECL_EXTERNAL (node->decl))
418 /* Inline clones can not be part of boundary.
419 gcc_assert (!node->global.inlined_to);
421 FIXME: At the moment they can be, when partition contains an inline
422 clone that is clone of inline clone from outside partition. We can
423 reshape the clone tree and make other tree to be the root, but it
424 needs a bit extra work and will be promplty done by cgraph_remove_node
425 after reading back. */
426 in_other_partition = 1;
429 clone_of = node->clone_of;
430 while (clone_of
431 && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
432 if (clone_of->prev_sibling_clone)
433 clone_of = clone_of->prev_sibling_clone;
434 else
435 clone_of = clone_of->clone_of;
437 /* See if body of the master function is output. If not, we are seeing only
438 an declaration and we do not need to pass down clone tree. */
439 ultimate_clone_of = clone_of;
440 while (ultimate_clone_of && ultimate_clone_of->clone_of)
441 ultimate_clone_of = ultimate_clone_of->clone_of;
443 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
444 clone_of = NULL;
446 if (tag == LTO_symtab_analyzed_node)
447 gcc_assert (clone_of || !node->clone_of);
448 if (!clone_of)
449 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
450 else
451 streamer_write_hwi_stream (ob->main_stream, ref);
454 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
455 streamer_write_gcov_count_stream (ob->main_stream, node->count);
456 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
458 streamer_write_hwi_stream (ob->main_stream,
459 node->ipa_transforms_to_apply.length ());
460 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
461 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
463 if (tag == LTO_symtab_analyzed_node)
465 if (node->global.inlined_to)
467 ref = lto_symtab_encoder_lookup (encoder, node->global.inlined_to);
468 gcc_assert (ref != LCC_NOT_FOUND);
470 else
471 ref = LCC_NOT_FOUND;
473 streamer_write_hwi_stream (ob->main_stream, ref);
476 if (node->same_comdat_group && !boundary_p)
478 ref = lto_symtab_encoder_lookup (encoder,
479 node->same_comdat_group);
480 gcc_assert (ref != LCC_NOT_FOUND);
482 else
483 ref = LCC_NOT_FOUND;
484 streamer_write_hwi_stream (ob->main_stream, ref);
486 streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
488 bp = bitpack_create (ob->main_stream);
489 bp_pack_value (&bp, node->local.local, 1);
490 bp_pack_value (&bp, node->externally_visible, 1);
491 bp_pack_value (&bp, node->definition, 1);
492 bp_pack_value (&bp, node->local.versionable, 1);
493 bp_pack_value (&bp, node->local.can_change_signature, 1);
494 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
495 bp_pack_value (&bp, node->force_output, 1);
496 bp_pack_value (&bp, node->forced_by_abi, 1);
497 bp_pack_value (&bp, node->unique_name, 1);
498 bp_pack_value (&bp, node->address_taken, 1);
499 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
500 && !DECL_EXTERNAL (node->decl)
501 && !DECL_COMDAT (node->decl)
502 && (reachable_from_other_partition_p (node, encoder)
503 || referenced_from_other_partition_p (&node->ref_list,
504 encoder)), 1);
505 bp_pack_value (&bp, node->lowered, 1);
506 bp_pack_value (&bp, in_other_partition, 1);
507 /* Real aliases in a boundary become non-aliases. However we still stream
508 alias info on weakrefs.
509 TODO: We lose a bit of information here - when we know that variable is
510 defined in other unit, we may use the info on aliases to resolve
511 symbol1 != symbol2 type tests that we can do only for locally defined objects
512 otherwise. */
513 alias_p = node->alias && (!boundary_p || node->weakref);
514 bp_pack_value (&bp, alias_p, 1);
515 bp_pack_value (&bp, node->weakref, 1);
516 bp_pack_value (&bp, node->frequency, 2);
517 bp_pack_value (&bp, node->only_called_at_startup, 1);
518 bp_pack_value (&bp, node->only_called_at_exit, 1);
519 bp_pack_value (&bp, node->tm_clone, 1);
520 bp_pack_value (&bp, node->thunk.thunk_p && !boundary_p, 1);
521 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
522 LDPR_NUM_KNOWN, node->resolution);
523 streamer_write_bitpack (&bp);
525 if (node->thunk.thunk_p && !boundary_p)
527 streamer_write_uhwi_stream
528 (ob->main_stream,
529 1 + (node->thunk.this_adjusting != 0) * 2
530 + (node->thunk.virtual_offset_p != 0) * 4);
531 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
532 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
534 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
537 /* Output the varpool NODE to OB.
538 If NODE is not in SET, then NODE is a boundary. */
540 static void
541 lto_output_varpool_node (struct lto_simple_output_block *ob, struct varpool_node *node,
542 lto_symtab_encoder_t encoder)
544 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
545 struct bitpack_d bp;
546 int ref;
547 bool alias_p;
549 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
550 LTO_symtab_variable);
551 streamer_write_hwi_stream (ob->main_stream, node->order);
552 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
553 bp = bitpack_create (ob->main_stream);
554 bp_pack_value (&bp, node->externally_visible, 1);
555 bp_pack_value (&bp, node->force_output, 1);
556 bp_pack_value (&bp, node->forced_by_abi, 1);
557 bp_pack_value (&bp, node->unique_name, 1);
558 bp_pack_value (&bp, node->definition, 1);
559 alias_p = node->alias && (!boundary_p || node->weakref);
560 bp_pack_value (&bp, alias_p, 1);
561 bp_pack_value (&bp, node->weakref, 1);
562 bp_pack_value (&bp, node->analyzed && !boundary_p, 1);
563 gcc_assert (node->definition || !node->analyzed);
564 /* Constant pool initializers can be de-unified into individual ltrans units.
565 FIXME: Alternatively at -Os we may want to avoid generating for them the local
566 labels and share them across LTRANS partitions. */
567 if (DECL_IN_CONSTANT_POOL (node->decl)
568 && !DECL_EXTERNAL (node->decl)
569 && !DECL_COMDAT (node->decl))
571 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
572 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
574 else
576 bp_pack_value (&bp, node->definition
577 && referenced_from_other_partition_p (&node->ref_list,
578 encoder), 1);
579 bp_pack_value (&bp, node->analyzed
580 && boundary_p && !DECL_EXTERNAL (node->decl), 1);
581 /* in_other_partition. */
583 bp_pack_value (&bp, node->need_bounds_init, 1);
584 streamer_write_bitpack (&bp);
585 if (node->same_comdat_group && !boundary_p)
587 ref = lto_symtab_encoder_lookup (encoder,
588 node->same_comdat_group);
589 gcc_assert (ref != LCC_NOT_FOUND);
591 else
592 ref = LCC_NOT_FOUND;
593 streamer_write_hwi_stream (ob->main_stream, ref);
594 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
595 LDPR_NUM_KNOWN, node->resolution);
598 /* Output the varpool NODE to OB.
599 If NODE is not in SET, then NODE is a boundary. */
601 static void
602 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
603 lto_symtab_encoder_t encoder)
605 struct bitpack_d bp;
606 int nref;
607 int uid = ref->lto_stmt_uid;
608 struct cgraph_node *node;
610 bp = bitpack_create (ob->main_stream);
611 bp_pack_value (&bp, ref->use, 2);
612 bp_pack_value (&bp, ref->speculative, 1);
613 streamer_write_bitpack (&bp);
614 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
615 gcc_assert (nref != LCC_NOT_FOUND);
616 streamer_write_hwi_stream (ob->main_stream, nref);
618 node = dyn_cast <cgraph_node> (ref->referring);
619 if (node)
621 if (ref->stmt)
622 uid = gimple_uid (ref->stmt) + 1;
623 streamer_write_hwi_stream (ob->main_stream, uid);
627 /* Stream out profile_summary to OB. */
629 static void
630 output_profile_summary (struct lto_simple_output_block *ob)
632 unsigned h_ix;
633 struct bitpack_d bp;
635 if (profile_info)
637 /* We do not output num and run_max, they are not used by
638 GCC profile feedback and they are difficult to merge from multiple
639 units. */
640 gcc_assert (profile_info->runs);
641 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
642 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
644 /* sum_all is needed for computing the working set with the
645 histogram. */
646 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
648 /* Create and output a bitpack of non-zero histogram entries indices. */
649 bp = bitpack_create (ob->main_stream);
650 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
651 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
652 streamer_write_bitpack (&bp);
653 /* Now stream out only those non-zero entries. */
654 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
656 if (!profile_info->histogram[h_ix].num_counters)
657 continue;
658 streamer_write_gcov_count_stream (ob->main_stream,
659 profile_info->histogram[h_ix].num_counters);
660 streamer_write_gcov_count_stream (ob->main_stream,
661 profile_info->histogram[h_ix].min_value);
662 streamer_write_gcov_count_stream (ob->main_stream,
663 profile_info->histogram[h_ix].cum_value);
665 /* IPA-profile computes hot bb threshold based on cumulated
666 whole program profile. We need to stream it down to ltrans. */
667 if (flag_wpa)
668 streamer_write_gcov_count_stream (ob->main_stream,
669 get_hot_bb_threshold ());
671 else
672 streamer_write_uhwi_stream (ob->main_stream, 0);
675 /* Output all callees or indirect outgoing edges. EDGE must be the first such
676 edge. */
678 static void
679 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
680 struct lto_simple_output_block *ob,
681 lto_symtab_encoder_t encoder)
683 if (!edge)
684 return;
686 /* Output edges in backward direction, so the reconstructed callgraph match
687 and it is easy to associate call sites in the IPA pass summaries. */
688 while (edge->next_callee)
689 edge = edge->next_callee;
690 for (; edge; edge = edge->prev_callee)
691 lto_output_edge (ob, edge, encoder);
694 /* Output the part of the cgraph in SET. */
696 static void
697 output_refs (lto_symtab_encoder_t encoder)
699 lto_symtab_encoder_iterator lsei;
700 struct lto_simple_output_block *ob;
701 int count;
702 struct ipa_ref *ref;
703 int i;
705 ob = lto_create_simple_output_block (LTO_section_refs);
707 for (lsei = lsei_start_in_partition (encoder); !lsei_end_p (lsei);
708 lsei_next_in_partition (&lsei))
710 symtab_node *node = lsei_node (lsei);
712 count = ipa_ref_list_nreferences (&node->ref_list);
713 if (count)
715 streamer_write_gcov_count_stream (ob->main_stream, count);
716 streamer_write_uhwi_stream (ob->main_stream,
717 lto_symtab_encoder_lookup (encoder, node));
718 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list,
719 i, ref); i++)
720 lto_output_ref (ob, ref, encoder);
724 streamer_write_uhwi_stream (ob->main_stream, 0);
726 lto_destroy_simple_output_block (ob);
729 /* Add NODE into encoder as well as nodes it is cloned from.
730 Do it in a way so clones appear first. */
732 static void
733 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
734 bool include_body)
736 if (node->clone_of)
737 add_node_to (encoder, node->clone_of, include_body);
738 else if (include_body)
739 lto_set_symtab_encoder_encode_body (encoder, node);
740 lto_symtab_encoder_encode (encoder, node);
743 /* Add all references in LIST to encoders. */
745 static void
746 add_references (lto_symtab_encoder_t encoder,
747 struct ipa_ref_list *list)
749 int i;
750 struct ipa_ref *ref;
751 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
752 if (is_a <cgraph_node> (ref->referred))
753 add_node_to (encoder, ipa_ref_node (ref), false);
754 else
755 lto_symtab_encoder_encode (encoder, ref->referred);
758 /* Find all symbols we want to stream into given partition and insert them
759 to encoders.
761 The function actually replaces IN_ENCODER by new one. The reason is that
762 streaming code needs clone's origin to be streamed before clone. This
763 means that we need to insert the nodes in specific order. This order is
764 ignored by the partitioning logic earlier. */
766 lto_symtab_encoder_t
767 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
769 struct cgraph_node *node;
770 struct cgraph_edge *edge;
771 int i;
772 lto_symtab_encoder_t encoder;
773 lto_symtab_encoder_iterator lsei;
774 struct pointer_set_t *reachable_call_targets = pointer_set_create ();
776 encoder = lto_symtab_encoder_new (false);
778 /* Go over all entries in the IN_ENCODER and duplicate them to
779 ENCODER. At the same time insert masters of clones so
780 every master appears before clone. */
781 for (lsei = lsei_start_function_in_partition (in_encoder);
782 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
784 node = lsei_cgraph_node (lsei);
785 add_node_to (encoder, node, true);
786 lto_set_symtab_encoder_in_partition (encoder, node);
787 add_references (encoder, &node->ref_list);
788 /* For proper debug info, we need to ship the origins, too. */
789 if (DECL_ABSTRACT_ORIGIN (node->decl))
791 struct cgraph_node *origin_node
792 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (node->decl));
793 add_node_to (encoder, origin_node, true);
796 for (lsei = lsei_start_variable_in_partition (in_encoder);
797 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
799 struct varpool_node *vnode = lsei_varpool_node (lsei);
801 lto_set_symtab_encoder_in_partition (encoder, vnode);
802 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
803 add_references (encoder, &vnode->ref_list);
804 /* For proper debug info, we need to ship the origins, too. */
805 if (DECL_ABSTRACT_ORIGIN (vnode->decl))
807 struct varpool_node *origin_node
808 = varpool_get_node (DECL_ABSTRACT_ORIGIN (node->decl));
809 lto_set_symtab_encoder_in_partition (encoder, origin_node);
812 /* Pickle in also the initializer of all referenced readonly variables
813 to help folding. Constant pool variables are not shared, so we must
814 pickle those too. */
815 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
817 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
818 if (varpool_node *vnode = dyn_cast <varpool_node> (node))
820 if (!lto_symtab_encoder_encode_initializer_p (encoder,
821 vnode)
822 && ctor_for_folding (vnode->decl) != error_mark_node)
824 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
825 add_references (encoder, &vnode->ref_list);
830 /* Go over all the nodes again to include callees that are not in
831 SET. */
832 for (lsei = lsei_start_function_in_partition (encoder);
833 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
835 node = lsei_cgraph_node (lsei);
836 for (edge = node->callees; edge; edge = edge->next_callee)
838 struct cgraph_node *callee = edge->callee;
839 if (!lto_symtab_encoder_in_partition_p (encoder, callee))
841 /* We should have moved all the inlines. */
842 gcc_assert (!callee->global.inlined_to);
843 add_node_to (encoder, callee, false);
846 /* Add all possible targets for late devirtualization. */
847 if (flag_devirtualize)
848 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
849 if (edge->indirect_info->polymorphic)
851 unsigned int i;
852 void *cache_token;
853 bool final;
854 vec <cgraph_node *>targets
855 = possible_polymorphic_call_targets
856 (edge, &final, &cache_token);
857 if (!pointer_set_insert (reachable_call_targets,
858 cache_token))
860 for (i = 0; i < targets.length (); i++)
862 struct cgraph_node *callee = targets[i];
864 /* Adding an external declarations into the unit serves
865 no purpose and just increases its boundary. */
866 if (callee->definition
867 && !lto_symtab_encoder_in_partition_p
868 (encoder, callee))
870 gcc_assert (!callee->global.inlined_to);
871 add_node_to (encoder, callee, false);
877 lto_symtab_encoder_delete (in_encoder);
878 pointer_set_destroy (reachable_call_targets);
879 return encoder;
882 /* Output the part of the symtab in SET and VSET. */
884 void
885 output_symtab (void)
887 struct cgraph_node *node;
888 struct lto_simple_output_block *ob;
889 lto_symtab_encoder_iterator lsei;
890 int i, n_nodes;
891 lto_symtab_encoder_t encoder;
892 static bool asm_nodes_output = false;
894 if (flag_wpa)
895 output_cgraph_opt_summary ();
897 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
899 output_profile_summary (ob);
901 /* An encoder for cgraph nodes should have been created by
902 ipa_write_summaries_1. */
903 gcc_assert (ob->decl_state->symtab_node_encoder);
904 encoder = ob->decl_state->symtab_node_encoder;
906 /* Write out the nodes. We must first output a node and then its clones,
907 otherwise at a time reading back the node there would be nothing to clone
908 from. */
909 n_nodes = lto_symtab_encoder_size (encoder);
910 for (i = 0; i < n_nodes; i++)
912 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
913 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
914 lto_output_node (ob, cnode, encoder);
915 else
916 lto_output_varpool_node (ob, varpool (node), encoder);
920 /* Go over the nodes in SET again to write edges. */
921 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
922 lsei_next_function_in_partition (&lsei))
924 node = lsei_cgraph_node (lsei);
925 output_outgoing_cgraph_edges (node->callees, ob, encoder);
926 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
929 streamer_write_uhwi_stream (ob->main_stream, 0);
931 lto_destroy_simple_output_block (ob);
933 /* Emit toplevel asms.
934 When doing WPA we must output every asm just once. Since we do not partition asm
935 nodes at all, output them to first output. This is kind of hack, but should work
936 well. */
937 if (!asm_nodes_output)
939 asm_nodes_output = true;
940 lto_output_toplevel_asms ();
943 output_refs (encoder);
946 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
947 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
948 NODE or to replace the values in it, for instance because the first
949 time we saw it, the function body was not available but now it
950 is. BP is a bitpack with all the bitflags for NODE read from the
951 stream. */
953 static void
954 input_overwrite_node (struct lto_file_decl_data *file_data,
955 struct cgraph_node *node,
956 enum LTO_symtab_tags tag,
957 struct bitpack_d *bp)
959 node->aux = (void *) tag;
960 node->lto_file_data = file_data;
962 node->local.local = bp_unpack_value (bp, 1);
963 node->externally_visible = bp_unpack_value (bp, 1);
964 node->definition = bp_unpack_value (bp, 1);
965 node->local.versionable = bp_unpack_value (bp, 1);
966 node->local.can_change_signature = bp_unpack_value (bp, 1);
967 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
968 node->force_output = bp_unpack_value (bp, 1);
969 node->forced_by_abi = bp_unpack_value (bp, 1);
970 node->unique_name = bp_unpack_value (bp, 1);
971 node->address_taken = bp_unpack_value (bp, 1);
972 node->used_from_other_partition = bp_unpack_value (bp, 1);
973 node->lowered = bp_unpack_value (bp, 1);
974 node->analyzed = tag == LTO_symtab_analyzed_node;
975 node->in_other_partition = bp_unpack_value (bp, 1);
976 if (node->in_other_partition
977 /* Avoid updating decl when we are seeing just inline clone.
978 When inlining function that has functions already inlined into it,
979 we produce clones of inline clones.
981 WPA partitioning might put each clone into different unit and
982 we might end up streaming inline clone from other partition
983 to support clone we are interested in. */
984 && (!node->clone_of
985 || node->clone_of->decl != node->decl))
987 DECL_EXTERNAL (node->decl) = 1;
988 TREE_STATIC (node->decl) = 0;
990 node->alias = bp_unpack_value (bp, 1);
991 node->weakref = bp_unpack_value (bp, 1);
992 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
993 node->only_called_at_startup = bp_unpack_value (bp, 1);
994 node->only_called_at_exit = bp_unpack_value (bp, 1);
995 node->tm_clone = bp_unpack_value (bp, 1);
996 node->thunk.thunk_p = bp_unpack_value (bp, 1);
997 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
998 LDPR_NUM_KNOWN);
1001 /* Return string alias is alias of. */
1003 static tree
1004 get_alias_symbol (tree decl)
1006 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1007 return get_identifier (TREE_STRING_POINTER
1008 (TREE_VALUE (TREE_VALUE (alias))));
1011 /* Read a node from input_block IB. TAG is the node's tag just read.
1012 Return the node read or overwriten. */
1014 static struct cgraph_node *
1015 input_node (struct lto_file_decl_data *file_data,
1016 struct lto_input_block *ib,
1017 enum LTO_symtab_tags tag,
1018 vec<symtab_node *> nodes)
1020 gcc::pass_manager *passes = g->get_passes ();
1021 tree fn_decl;
1022 struct cgraph_node *node;
1023 struct bitpack_d bp;
1024 unsigned decl_index;
1025 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1026 int clone_ref;
1027 int order;
1028 int i, count;
1030 order = streamer_read_hwi (ib) + order_base;
1031 clone_ref = streamer_read_hwi (ib);
1033 decl_index = streamer_read_uhwi (ib);
1034 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1036 if (clone_ref != LCC_NOT_FOUND)
1038 node = cgraph_clone_node (cgraph (nodes[clone_ref]), fn_decl,
1039 0, CGRAPH_FREQ_BASE, false,
1040 vNULL, false, NULL);
1042 else
1044 /* Declaration of functions can be already merged with a declaration
1045 from other input file. We keep cgraph unmerged until after streaming
1046 of ipa passes is done. Alays forcingly create a fresh node. */
1047 node = cgraph_create_empty_node ();
1048 node->decl = fn_decl;
1049 symtab_register_node (node);
1052 node->order = order;
1053 if (order >= symtab_order)
1054 symtab_order = order + 1;
1056 node->count = streamer_read_gcov_count (ib);
1057 node->count_materialization_scale = streamer_read_hwi (ib);
1059 count = streamer_read_hwi (ib);
1060 node->ipa_transforms_to_apply = vNULL;
1061 for (i = 0; i < count; i++)
1063 struct opt_pass *pass;
1064 int pid = streamer_read_hwi (ib);
1066 gcc_assert (pid < passes->passes_by_id_size);
1067 pass = passes->passes_by_id[pid];
1068 node->ipa_transforms_to_apply.safe_push ((struct ipa_opt_pass_d *) pass);
1071 if (tag == LTO_symtab_analyzed_node)
1072 ref = streamer_read_hwi (ib);
1074 ref2 = streamer_read_hwi (ib);
1076 /* Make sure that we have not read this node before. Nodes that
1077 have already been read will have their tag stored in the 'aux'
1078 field. Since built-in functions can be referenced in multiple
1079 functions, they are expected to be read more than once. */
1080 if (node->aux && !DECL_BUILT_IN (node->decl))
1081 internal_error ("bytecode stream: found multiple instances of cgraph "
1082 "node with uid %d", node->uid);
1084 node->tp_first_run = streamer_read_uhwi (ib);
1086 bp = streamer_read_bitpack (ib);
1088 input_overwrite_node (file_data, node, tag, &bp);
1090 /* Store a reference for now, and fix up later to be a pointer. */
1091 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
1093 /* Store a reference for now, and fix up later to be a pointer. */
1094 node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
1096 if (node->thunk.thunk_p)
1098 int type = streamer_read_uhwi (ib);
1099 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1100 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1102 node->thunk.fixed_offset = fixed_offset;
1103 node->thunk.this_adjusting = (type & 2);
1104 node->thunk.virtual_value = virtual_value;
1105 node->thunk.virtual_offset_p = (type & 4);
1107 if (node->alias && !node->analyzed && node->weakref)
1108 node->alias_target = get_alias_symbol (node->decl);
1109 node->profile_id = streamer_read_hwi (ib);
1110 return node;
1113 /* Read a node from input_block IB. TAG is the node's tag just read.
1114 Return the node read or overwriten. */
1116 static struct varpool_node *
1117 input_varpool_node (struct lto_file_decl_data *file_data,
1118 struct lto_input_block *ib)
1120 int decl_index;
1121 tree var_decl;
1122 struct varpool_node *node;
1123 struct bitpack_d bp;
1124 int ref = LCC_NOT_FOUND;
1125 int order;
1127 order = streamer_read_hwi (ib) + order_base;
1128 decl_index = streamer_read_uhwi (ib);
1129 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1131 /* Declaration of functions can be already merged with a declaration
1132 from other input file. We keep cgraph unmerged until after streaming
1133 of ipa passes is done. Alays forcingly create a fresh node. */
1134 node = varpool_create_empty_node ();
1135 node->decl = var_decl;
1136 symtab_register_node (node);
1138 node->order = order;
1139 if (order >= symtab_order)
1140 symtab_order = order + 1;
1141 node->lto_file_data = file_data;
1143 bp = streamer_read_bitpack (ib);
1144 node->externally_visible = bp_unpack_value (&bp, 1);
1145 node->force_output = bp_unpack_value (&bp, 1);
1146 node->forced_by_abi = bp_unpack_value (&bp, 1);
1147 node->unique_name = bp_unpack_value (&bp, 1);
1148 node->definition = bp_unpack_value (&bp, 1);
1149 node->alias = bp_unpack_value (&bp, 1);
1150 node->weakref = bp_unpack_value (&bp, 1);
1151 node->analyzed = bp_unpack_value (&bp, 1);
1152 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1153 node->in_other_partition = bp_unpack_value (&bp, 1);
1154 node->need_bounds_init = bp_unpack_value (&bp, 1);
1155 if (node->in_other_partition)
1157 DECL_EXTERNAL (node->decl) = 1;
1158 TREE_STATIC (node->decl) = 0;
1160 if (node->alias && !node->analyzed && node->weakref)
1161 node->alias_target = get_alias_symbol (node->decl);
1162 ref = streamer_read_hwi (ib);
1163 /* Store a reference for now, and fix up later to be a pointer. */
1164 node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1165 node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1166 LDPR_NUM_KNOWN);
1168 return node;
1171 /* Read a node from input_block IB. TAG is the node's tag just read.
1172 Return the node read or overwriten. */
1174 static void
1175 input_ref (struct lto_input_block *ib,
1176 symtab_node *referring_node,
1177 vec<symtab_node *> nodes)
1179 symtab_node *node = NULL;
1180 struct bitpack_d bp;
1181 enum ipa_ref_use use;
1182 bool speculative;
1183 struct ipa_ref *ref;
1185 bp = streamer_read_bitpack (ib);
1186 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1187 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1188 node = nodes[streamer_read_hwi (ib)];
1189 ref = ipa_record_reference (referring_node, node, use, NULL);
1190 ref->speculative = speculative;
1191 if (is_a <cgraph_node> (referring_node))
1192 ref->lto_stmt_uid = streamer_read_hwi (ib);
1195 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1196 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1197 edge being read is indirect (in the sense that it has
1198 indirect_unknown_callee set). */
1200 static void
1201 input_edge (struct lto_input_block *ib, vec<symtab_node *> nodes,
1202 bool indirect)
1204 struct cgraph_node *caller, *callee;
1205 struct cgraph_edge *edge;
1206 unsigned int stmt_id;
1207 gcov_type count;
1208 int freq;
1209 cgraph_inline_failed_t inline_failed;
1210 struct bitpack_d bp;
1211 int ecf_flags = 0;
1213 caller = cgraph (nodes[streamer_read_hwi (ib)]);
1214 if (caller == NULL || caller->decl == NULL_TREE)
1215 internal_error ("bytecode stream: no caller found while reading edge");
1217 if (!indirect)
1219 callee = cgraph (nodes[streamer_read_hwi (ib)]);
1220 if (callee == NULL || callee->decl == NULL_TREE)
1221 internal_error ("bytecode stream: no callee found while reading edge");
1223 else
1224 callee = NULL;
1226 count = streamer_read_gcov_count (ib);
1228 bp = streamer_read_bitpack (ib);
1229 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_enum, CIF_N_REASONS);
1230 stmt_id = bp_unpack_var_len_unsigned (&bp);
1231 freq = (int) bp_unpack_var_len_unsigned (&bp);
1233 if (indirect)
1234 edge = cgraph_create_indirect_edge (caller, NULL, 0, count, freq);
1235 else
1236 edge = cgraph_create_edge (caller, callee, NULL, count, freq);
1238 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1239 edge->speculative = bp_unpack_value (&bp, 1);
1240 edge->lto_stmt_uid = stmt_id;
1241 edge->inline_failed = inline_failed;
1242 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1243 edge->can_throw_external = bp_unpack_value (&bp, 1);
1244 if (indirect)
1246 if (bp_unpack_value (&bp, 1))
1247 ecf_flags |= ECF_CONST;
1248 if (bp_unpack_value (&bp, 1))
1249 ecf_flags |= ECF_PURE;
1250 if (bp_unpack_value (&bp, 1))
1251 ecf_flags |= ECF_NORETURN;
1252 if (bp_unpack_value (&bp, 1))
1253 ecf_flags |= ECF_MALLOC;
1254 if (bp_unpack_value (&bp, 1))
1255 ecf_flags |= ECF_NOTHROW;
1256 if (bp_unpack_value (&bp, 1))
1257 ecf_flags |= ECF_RETURNS_TWICE;
1258 edge->indirect_info->ecf_flags = ecf_flags;
1259 edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1260 if (edge->indirect_info->common_target_id)
1261 edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1266 /* Read a cgraph from IB using the info in FILE_DATA. */
1268 static vec<symtab_node *>
1269 input_cgraph_1 (struct lto_file_decl_data *file_data,
1270 struct lto_input_block *ib)
1272 enum LTO_symtab_tags tag;
1273 vec<symtab_node *> nodes = vNULL;
1274 symtab_node *node;
1275 unsigned i;
1277 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1278 order_base = symtab_order;
1279 while (tag)
1281 if (tag == LTO_symtab_edge)
1282 input_edge (ib, nodes, false);
1283 else if (tag == LTO_symtab_indirect_edge)
1284 input_edge (ib, nodes, true);
1285 else if (tag == LTO_symtab_variable)
1287 node = input_varpool_node (file_data, ib);
1288 nodes.safe_push (node);
1289 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1291 else
1293 node = input_node (file_data, ib, tag, nodes);
1294 if (node == NULL || node->decl == NULL_TREE)
1295 internal_error ("bytecode stream: found empty cgraph node");
1296 nodes.safe_push (node);
1297 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1300 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1303 lto_input_toplevel_asms (file_data, order_base);
1305 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1306 #ifdef ENABLE_CHECKING
1307 FOR_EACH_VEC_ELT (nodes, i, node)
1308 gcc_assert (node->aux || !is_a <cgraph_node> (node));
1309 #endif
1310 FOR_EACH_VEC_ELT (nodes, i, node)
1312 int ref;
1313 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
1315 ref = (int) (intptr_t) cnode->global.inlined_to;
1317 /* We share declaration of builtins, so we may read same node twice. */
1318 if (!node->aux)
1319 continue;
1320 node->aux = NULL;
1322 /* Fixup inlined_to from reference to pointer. */
1323 if (ref != LCC_NOT_FOUND)
1324 cgraph (node)->global.inlined_to = cgraph (nodes[ref]);
1325 else
1326 cnode->global.inlined_to = NULL;
1329 ref = (int) (intptr_t) node->same_comdat_group;
1331 /* Fixup same_comdat_group from reference to pointer. */
1332 if (ref != LCC_NOT_FOUND)
1333 node->same_comdat_group = nodes[ref];
1334 else
1335 node->same_comdat_group = NULL;
1337 FOR_EACH_VEC_ELT (nodes, i, node)
1338 node->aux = is_a <cgraph_node> (node) ? (void *)1 : NULL;
1339 return nodes;
1342 /* Input ipa_refs. */
1344 static void
1345 input_refs (struct lto_input_block *ib,
1346 vec<symtab_node *> nodes)
1348 int count;
1349 int idx;
1350 while (true)
1352 symtab_node *node;
1353 count = streamer_read_uhwi (ib);
1354 if (!count)
1355 break;
1356 idx = streamer_read_uhwi (ib);
1357 node = nodes[idx];
1358 while (count)
1360 input_ref (ib, node, nodes);
1361 count--;
1367 static struct gcov_ctr_summary lto_gcov_summary;
1369 /* Input profile_info from IB. */
1370 static void
1371 input_profile_summary (struct lto_input_block *ib,
1372 struct lto_file_decl_data *file_data)
1374 unsigned h_ix;
1375 struct bitpack_d bp;
1376 unsigned int runs = streamer_read_uhwi (ib);
1377 if (runs)
1379 file_data->profile_info.runs = runs;
1380 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1381 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1383 memset (file_data->profile_info.histogram, 0,
1384 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1385 /* Input the bitpack of non-zero histogram indices. */
1386 bp = streamer_read_bitpack (ib);
1387 /* Read in and unpack the full bitpack, flagging non-zero
1388 histogram entries by setting the num_counters non-zero. */
1389 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1391 file_data->profile_info.histogram[h_ix].num_counters
1392 = bp_unpack_value (&bp, 1);
1394 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1396 if (!file_data->profile_info.histogram[h_ix].num_counters)
1397 continue;
1399 file_data->profile_info.histogram[h_ix].num_counters
1400 = streamer_read_gcov_count (ib);
1401 file_data->profile_info.histogram[h_ix].min_value
1402 = streamer_read_gcov_count (ib);
1403 file_data->profile_info.histogram[h_ix].cum_value
1404 = streamer_read_gcov_count (ib);
1406 /* IPA-profile computes hot bb threshold based on cumulated
1407 whole program profile. We need to stream it down to ltrans. */
1408 if (flag_ltrans)
1409 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1414 /* Rescale profile summaries to the same number of runs in the whole unit. */
1416 static void
1417 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1419 struct lto_file_decl_data *file_data;
1420 unsigned int j, h_ix;
1421 gcov_unsigned_t max_runs = 0;
1422 struct cgraph_node *node;
1423 struct cgraph_edge *edge;
1424 gcov_type saved_sum_all = 0;
1425 gcov_ctr_summary *saved_profile_info = 0;
1426 int saved_scale = 0;
1428 /* Find unit with maximal number of runs. If we ever get serious about
1429 roundoff errors, we might also consider computing smallest common
1430 multiply. */
1431 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1432 if (max_runs < file_data->profile_info.runs)
1433 max_runs = file_data->profile_info.runs;
1435 if (!max_runs)
1436 return;
1438 /* Simple overflow check. We probably don't need to support that many train
1439 runs. Such a large value probably imply data corruption anyway. */
1440 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1442 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1443 INT_MAX / REG_BR_PROB_BASE);
1444 return;
1447 profile_info = &lto_gcov_summary;
1448 lto_gcov_summary.runs = max_runs;
1449 lto_gcov_summary.sum_max = 0;
1450 memset (lto_gcov_summary.histogram, 0,
1451 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1453 /* Rescale all units to the maximal number of runs.
1454 sum_max can not be easily merged, as we have no idea what files come from
1455 the same run. We do not use the info anyway, so leave it 0. */
1456 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1457 if (file_data->profile_info.runs)
1459 int scale = GCOV_COMPUTE_SCALE (max_runs,
1460 file_data->profile_info.runs);
1461 lto_gcov_summary.sum_max
1462 = MAX (lto_gcov_summary.sum_max,
1463 apply_scale (file_data->profile_info.sum_max, scale));
1464 lto_gcov_summary.sum_all
1465 = MAX (lto_gcov_summary.sum_all,
1466 apply_scale (file_data->profile_info.sum_all, scale));
1467 /* Save a pointer to the profile_info with the largest
1468 scaled sum_all and the scale for use in merging the
1469 histogram. */
1470 if (!saved_profile_info
1471 || lto_gcov_summary.sum_all > saved_sum_all)
1473 saved_profile_info = &file_data->profile_info;
1474 saved_sum_all = lto_gcov_summary.sum_all;
1475 saved_scale = scale;
1479 gcc_assert (saved_profile_info);
1481 /* Scale up the histogram from the profile that had the largest
1482 scaled sum_all above. */
1483 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1485 /* Scale up the min value as we did the corresponding sum_all
1486 above. Use that to find the new histogram index. */
1487 gcov_type scaled_min
1488 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1489 saved_scale);
1490 /* The new index may be shared with another scaled histogram entry,
1491 so we need to account for a non-zero histogram entry at new_ix. */
1492 unsigned new_ix = gcov_histo_index (scaled_min);
1493 lto_gcov_summary.histogram[new_ix].min_value
1494 = (lto_gcov_summary.histogram[new_ix].num_counters
1495 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1496 : scaled_min);
1497 /* Some of the scaled counter values would ostensibly need to be placed
1498 into different (larger) histogram buckets, but we keep things simple
1499 here and place the scaled cumulative counter value in the bucket
1500 corresponding to the scaled minimum counter value. */
1501 lto_gcov_summary.histogram[new_ix].cum_value
1502 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1503 saved_scale);
1504 lto_gcov_summary.histogram[new_ix].num_counters
1505 += saved_profile_info->histogram[h_ix].num_counters;
1508 /* Watch roundoff errors. */
1509 if (lto_gcov_summary.sum_max < max_runs)
1510 lto_gcov_summary.sum_max = max_runs;
1512 /* If merging already happent at WPA time, we are done. */
1513 if (flag_ltrans)
1514 return;
1516 /* Now compute count_materialization_scale of each node.
1517 During LTRANS we already have values of count_materialization_scale
1518 computed, so just update them. */
1519 FOR_EACH_FUNCTION (node)
1520 if (node->lto_file_data
1521 && node->lto_file_data->profile_info.runs)
1523 int scale;
1525 scale = RDIV (node->count_materialization_scale * max_runs,
1526 node->lto_file_data->profile_info.runs);
1527 node->count_materialization_scale = scale;
1528 if (scale < 0)
1529 fatal_error ("Profile information in %s corrupted",
1530 file_data->file_name);
1532 if (scale == REG_BR_PROB_BASE)
1533 continue;
1534 for (edge = node->callees; edge; edge = edge->next_callee)
1535 edge->count = apply_scale (edge->count, scale);
1536 node->count = apply_scale (node->count, scale);
1540 /* Input and merge the symtab from each of the .o files passed to
1541 lto1. */
1543 void
1544 input_symtab (void)
1546 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1547 struct lto_file_decl_data *file_data;
1548 unsigned int j = 0;
1549 struct cgraph_node *node;
1551 while ((file_data = file_data_vec[j++]))
1553 const char *data;
1554 size_t len;
1555 struct lto_input_block *ib;
1556 vec<symtab_node *> nodes;
1558 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1559 &data, &len);
1560 if (!ib)
1561 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1562 input_profile_summary (ib, file_data);
1563 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1564 nodes = input_cgraph_1 (file_data, ib);
1565 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1566 ib, data, len);
1568 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1569 &data, &len);
1570 if (!ib)
1571 fatal_error ("cannot find LTO section refs in %s",
1572 file_data->file_name);
1573 input_refs (ib, nodes);
1574 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1575 ib, data, len);
1576 if (flag_ltrans)
1577 input_cgraph_opt_summary (nodes);
1578 nodes.release ();
1581 merge_profile_summaries (file_data_vec);
1582 get_working_sets ();
1585 /* Clear out the aux field that was used to store enough state to
1586 tell which nodes should be overwritten. */
1587 FOR_EACH_FUNCTION (node)
1589 /* Some nodes may have been created by cgraph_node. This
1590 happens when the callgraph contains nested functions. If the
1591 node for the parent function was never emitted to the gimple
1592 file, cgraph_node will create a node for it when setting the
1593 context of the nested function. */
1594 if (node->lto_file_data)
1595 node->aux = NULL;
1599 /* True when we need optimization summary for NODE. */
1601 static int
1602 output_cgraph_opt_summary_p (struct cgraph_node *node)
1604 return (node->clone_of
1605 && (node->clone.tree_map
1606 || node->clone.args_to_skip
1607 || node->clone.combined_args_to_skip));
1610 /* Output optimization summary for EDGE to OB. */
1611 static void
1612 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1613 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1617 /* Output optimization summary for NODE to OB. */
1619 static void
1620 output_node_opt_summary (struct output_block *ob,
1621 struct cgraph_node *node,
1622 lto_symtab_encoder_t encoder)
1624 unsigned int index;
1625 bitmap_iterator bi;
1626 struct ipa_replace_map *map;
1627 struct bitpack_d bp;
1628 int i;
1629 struct cgraph_edge *e;
1631 if (node->clone.args_to_skip)
1633 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1634 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1635 streamer_write_uhwi (ob, index);
1637 else
1638 streamer_write_uhwi (ob, 0);
1639 if (node->clone.combined_args_to_skip)
1641 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1642 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1643 streamer_write_uhwi (ob, index);
1645 else
1646 streamer_write_uhwi (ob, 0);
1647 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1648 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1650 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1651 mechanism to store function local declarations into summaries. */
1652 gcc_assert (!map->old_tree);
1653 streamer_write_uhwi (ob, map->parm_num);
1654 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1655 stream_write_tree (ob, map->new_tree, true);
1656 bp = bitpack_create (ob->main_stream);
1657 bp_pack_value (&bp, map->replace_p, 1);
1658 bp_pack_value (&bp, map->ref_p, 1);
1659 streamer_write_bitpack (&bp);
1662 if (lto_symtab_encoder_in_partition_p (encoder, node))
1664 for (e = node->callees; e; e = e->next_callee)
1665 output_edge_opt_summary (ob, e);
1666 for (e = node->indirect_calls; e; e = e->next_callee)
1667 output_edge_opt_summary (ob, e);
1671 /* Output optimization summaries stored in callgraph.
1672 At the moment it is the clone info structure. */
1674 static void
1675 output_cgraph_opt_summary (void)
1677 int i, n_nodes;
1678 lto_symtab_encoder_t encoder;
1679 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1680 unsigned count = 0;
1682 ob->cgraph_node = NULL;
1683 encoder = ob->decl_state->symtab_node_encoder;
1684 n_nodes = lto_symtab_encoder_size (encoder);
1685 for (i = 0; i < n_nodes; i++)
1687 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1688 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1689 if (cnode && output_cgraph_opt_summary_p (cnode))
1690 count++;
1692 streamer_write_uhwi (ob, count);
1693 for (i = 0; i < n_nodes; i++)
1695 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1696 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1697 if (cnode && output_cgraph_opt_summary_p (cnode))
1699 streamer_write_uhwi (ob, i);
1700 output_node_opt_summary (ob, cnode, encoder);
1703 produce_asm (ob, NULL);
1704 destroy_output_block (ob);
1707 /* Input optimisation summary of EDGE. */
1709 static void
1710 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
1711 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
1715 /* Input optimisation summary of NODE. */
1717 static void
1718 input_node_opt_summary (struct cgraph_node *node,
1719 struct lto_input_block *ib_main,
1720 struct data_in *data_in)
1722 int i;
1723 int count;
1724 int bit;
1725 struct bitpack_d bp;
1726 struct cgraph_edge *e;
1728 count = streamer_read_uhwi (ib_main);
1729 if (count)
1730 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1731 for (i = 0; i < count; i++)
1733 bit = streamer_read_uhwi (ib_main);
1734 bitmap_set_bit (node->clone.args_to_skip, bit);
1736 count = streamer_read_uhwi (ib_main);
1737 if (count)
1738 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1739 for (i = 0; i < count; i++)
1741 bit = streamer_read_uhwi (ib_main);
1742 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1744 count = streamer_read_uhwi (ib_main);
1745 for (i = 0; i < count; i++)
1747 struct ipa_replace_map *map = ggc_alloc_ipa_replace_map ();
1749 vec_safe_push (node->clone.tree_map, map);
1750 map->parm_num = streamer_read_uhwi (ib_main);
1751 map->old_tree = NULL;
1752 map->new_tree = stream_read_tree (ib_main, data_in);
1753 bp = streamer_read_bitpack (ib_main);
1754 map->replace_p = bp_unpack_value (&bp, 1);
1755 map->ref_p = bp_unpack_value (&bp, 1);
1757 for (e = node->callees; e; e = e->next_callee)
1758 input_edge_opt_summary (e, ib_main);
1759 for (e = node->indirect_calls; e; e = e->next_callee)
1760 input_edge_opt_summary (e, ib_main);
1763 /* Read section in file FILE_DATA of length LEN with data DATA. */
1765 static void
1766 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1767 const char *data, size_t len,
1768 vec<symtab_node *> nodes)
1770 const struct lto_function_header *header =
1771 (const struct lto_function_header *) data;
1772 const int cfg_offset = sizeof (struct lto_function_header);
1773 const int main_offset = cfg_offset + header->cfg_size;
1774 const int string_offset = main_offset + header->main_size;
1775 struct data_in *data_in;
1776 struct lto_input_block ib_main;
1777 unsigned int i;
1778 unsigned int count;
1780 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1781 header->main_size);
1783 data_in =
1784 lto_data_in_create (file_data, (const char *) data + string_offset,
1785 header->string_size, vNULL);
1786 count = streamer_read_uhwi (&ib_main);
1788 for (i = 0; i < count; i++)
1790 int ref = streamer_read_uhwi (&ib_main);
1791 input_node_opt_summary (cgraph (nodes[ref]),
1792 &ib_main, data_in);
1794 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1795 len);
1796 lto_data_in_delete (data_in);
1799 /* Input optimization summary of cgraph. */
1801 static void
1802 input_cgraph_opt_summary (vec<symtab_node *> nodes)
1804 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1805 struct lto_file_decl_data *file_data;
1806 unsigned int j = 0;
1808 while ((file_data = file_data_vec[j++]))
1810 size_t len;
1811 const char *data =
1812 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1813 &len);
1815 if (data)
1816 input_cgraph_opt_section (file_data, data, len, nodes);