Just enumerate all GF_OMP_FOR_KIND_* and GF_OMP_TARGET_KIND_*.
[official-gcc.git] / gcc / lto-cgraph.c
blob544f04b4275cbfbf9eda12afafe6bdbec769c096
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 /* Ignore not needed nodes. */
247 if (!node->need_dump)
248 return;
249 int index = lto_symtab_encoder_encode (encoder, node);
250 encoder->nodes[index].in_partition = true;
253 /* Output the cgraph EDGE to OB using ENCODER. */
255 static void
256 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
257 lto_symtab_encoder_t encoder)
259 unsigned int uid;
260 intptr_t ref;
261 struct bitpack_d bp;
263 if (edge->indirect_unknown_callee)
264 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
265 LTO_symtab_indirect_edge);
266 else
267 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
268 LTO_symtab_edge);
270 ref = lto_symtab_encoder_lookup (encoder, edge->caller);
271 gcc_assert (ref != LCC_NOT_FOUND);
272 streamer_write_hwi_stream (ob->main_stream, ref);
274 if (!edge->indirect_unknown_callee)
276 ref = lto_symtab_encoder_lookup (encoder, edge->callee);
277 gcc_assert (ref != LCC_NOT_FOUND);
278 streamer_write_hwi_stream (ob->main_stream, ref);
281 streamer_write_gcov_count_stream (ob->main_stream, edge->count);
283 bp = bitpack_create (ob->main_stream);
284 uid = (!gimple_has_body_p (edge->caller->decl)
285 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt) + 1);
286 bp_pack_enum (&bp, cgraph_inline_failed_t,
287 CIF_N_REASONS, edge->inline_failed);
288 bp_pack_var_len_unsigned (&bp, uid);
289 bp_pack_var_len_unsigned (&bp, edge->frequency);
290 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
291 bp_pack_value (&bp, edge->speculative, 1);
292 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
293 bp_pack_value (&bp, edge->can_throw_external, 1);
294 if (edge->indirect_unknown_callee)
296 int flags = edge->indirect_info->ecf_flags;
297 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
298 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
299 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
300 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
301 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
302 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
303 /* Flags that should not appear on indirect calls. */
304 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
305 | ECF_MAY_BE_ALLOCA
306 | ECF_SIBCALL
307 | ECF_LEAF
308 | ECF_NOVOPS)));
310 streamer_write_bitpack (&bp);
311 if (edge->indirect_unknown_callee)
313 streamer_write_hwi_stream (ob->main_stream,
314 edge->indirect_info->common_target_id);
315 if (edge->indirect_info->common_target_id)
316 streamer_write_hwi_stream
317 (ob->main_stream, edge->indirect_info->common_target_probability);
321 /* Return if LIST contain references from other partitions. */
323 bool
324 referenced_from_other_partition_p (struct ipa_ref_list *list, lto_symtab_encoder_t encoder)
326 int i;
327 struct ipa_ref *ref;
328 for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
330 if (ref->referring->in_other_partition
331 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
332 return true;
334 return false;
337 /* Return true when node is reachable from other partition. */
339 bool
340 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
342 struct cgraph_edge *e;
343 if (!node->definition)
344 return false;
345 if (node->global.inlined_to)
346 return false;
347 for (e = node->callers; e; e = e->next_caller)
348 if (e->caller->in_other_partition
349 || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
350 return true;
351 return false;
354 /* Return if LIST contain references from other partitions. */
356 bool
357 referenced_from_this_partition_p (struct ipa_ref_list *list,
358 lto_symtab_encoder_t encoder)
360 int i;
361 struct ipa_ref *ref;
362 for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
363 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
364 return true;
365 return false;
368 /* Return true when node is reachable from other partition. */
370 bool
371 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
373 struct cgraph_edge *e;
374 for (e = node->callers; e; e = e->next_caller)
375 if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
376 return true;
377 return false;
380 /* Output the cgraph NODE to OB. ENCODER is used to find the
381 reference number of NODE->inlined_to. SET is the set of nodes we
382 are writing to the current file. If NODE is not in SET, then NODE
383 is a boundary of a cgraph_node_set and we pretend NODE just has a
384 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
385 that have had their callgraph node written so far. This is used to
386 determine if NODE is a clone of a previously written node. */
388 static void
389 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
390 lto_symtab_encoder_t encoder)
392 unsigned int tag;
393 struct bitpack_d bp;
394 bool boundary_p;
395 intptr_t ref;
396 bool in_other_partition = false;
397 struct cgraph_node *clone_of, *ultimate_clone_of;
398 ipa_opt_pass_d *pass;
399 int i;
400 bool alias_p;
402 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
404 if (node->analyzed && !boundary_p)
405 tag = LTO_symtab_analyzed_node;
406 else
407 tag = LTO_symtab_unavail_node;
409 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
410 tag);
411 streamer_write_hwi_stream (ob->main_stream, node->order);
413 /* In WPA mode, we only output part of the call-graph. Also, we
414 fake cgraph node attributes. There are two cases that we care.
416 Boundary nodes: There are nodes that are not part of SET but are
417 called from within SET. We artificially make them look like
418 externally visible nodes with no function body.
420 Cherry-picked nodes: These are nodes we pulled from other
421 translation units into SET during IPA-inlining. We make them as
422 local static nodes to prevent clashes with other local statics. */
423 if (boundary_p && node->analyzed
424 && symtab_get_symbol_partitioning_class (node) == SYMBOL_PARTITION)
426 /* Inline clones can not be part of boundary.
427 gcc_assert (!node->global.inlined_to);
429 FIXME: At the moment they can be, when partition contains an inline
430 clone that is clone of inline clone from outside partition. We can
431 reshape the clone tree and make other tree to be the root, but it
432 needs a bit extra work and will be promplty done by cgraph_remove_node
433 after reading back. */
434 in_other_partition = 1;
437 clone_of = node->clone_of;
438 while (clone_of
439 && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
440 if (clone_of->prev_sibling_clone)
441 clone_of = clone_of->prev_sibling_clone;
442 else
443 clone_of = clone_of->clone_of;
445 /* See if body of the master function is output. If not, we are seeing only
446 an declaration and we do not need to pass down clone tree. */
447 ultimate_clone_of = clone_of;
448 while (ultimate_clone_of && ultimate_clone_of->clone_of)
449 ultimate_clone_of = ultimate_clone_of->clone_of;
451 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
452 clone_of = NULL;
454 if (tag == LTO_symtab_analyzed_node)
455 gcc_assert (clone_of || !node->clone_of);
456 if (!clone_of)
457 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
458 else
459 streamer_write_hwi_stream (ob->main_stream, ref);
462 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
463 streamer_write_gcov_count_stream (ob->main_stream, node->count);
464 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
466 streamer_write_hwi_stream (ob->main_stream,
467 node->ipa_transforms_to_apply.length ());
468 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
469 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
471 if (tag == LTO_symtab_analyzed_node)
473 if (node->global.inlined_to)
475 ref = lto_symtab_encoder_lookup (encoder, node->global.inlined_to);
476 gcc_assert (ref != LCC_NOT_FOUND);
478 else
479 ref = LCC_NOT_FOUND;
481 streamer_write_hwi_stream (ob->main_stream, ref);
484 if (node->same_comdat_group && !boundary_p)
486 ref = lto_symtab_encoder_lookup (encoder,
487 node->same_comdat_group);
488 gcc_assert (ref != LCC_NOT_FOUND);
490 else
491 ref = LCC_NOT_FOUND;
492 streamer_write_hwi_stream (ob->main_stream, ref);
494 streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
496 bp = bitpack_create (ob->main_stream);
497 bp_pack_value (&bp, node->local.local, 1);
498 bp_pack_value (&bp, node->externally_visible, 1);
499 bp_pack_value (&bp, node->definition, 1);
500 bp_pack_value (&bp, node->local.versionable, 1);
501 bp_pack_value (&bp, node->local.can_change_signature, 1);
502 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
503 bp_pack_value (&bp, node->force_output, 1);
504 bp_pack_value (&bp, node->forced_by_abi, 1);
505 bp_pack_value (&bp, node->unique_name, 1);
506 bp_pack_value (&bp, node->address_taken, 1);
507 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
508 && symtab_get_symbol_partitioning_class (node) == SYMBOL_PARTITION
509 && (reachable_from_other_partition_p (node, encoder)
510 || referenced_from_other_partition_p (&node->ref_list,
511 encoder)), 1);
512 bp_pack_value (&bp, node->lowered, 1);
513 bp_pack_value (&bp, in_other_partition, 1);
514 /* Real aliases in a boundary become non-aliases. However we still stream
515 alias info on weakrefs.
516 TODO: We lose a bit of information here - when we know that variable is
517 defined in other unit, we may use the info on aliases to resolve
518 symbol1 != symbol2 type tests that we can do only for locally defined objects
519 otherwise. */
520 alias_p = node->alias && (!boundary_p || node->weakref);
521 bp_pack_value (&bp, alias_p, 1);
522 bp_pack_value (&bp, node->weakref, 1);
523 bp_pack_value (&bp, node->frequency, 2);
524 bp_pack_value (&bp, node->only_called_at_startup, 1);
525 bp_pack_value (&bp, node->only_called_at_exit, 1);
526 bp_pack_value (&bp, node->tm_clone, 1);
527 bp_pack_value (&bp, node->calls_comdat_local, 1);
528 bp_pack_value (&bp, node->thunk.thunk_p && !boundary_p, 1);
529 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
530 LDPR_NUM_KNOWN, node->resolution);
531 streamer_write_bitpack (&bp);
533 if (node->thunk.thunk_p && !boundary_p)
535 streamer_write_uhwi_stream
536 (ob->main_stream,
537 1 + (node->thunk.this_adjusting != 0) * 2
538 + (node->thunk.virtual_offset_p != 0) * 4);
539 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
540 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
542 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
545 /* Output the varpool NODE to OB.
546 If NODE is not in SET, then NODE is a boundary. */
548 static void
549 lto_output_varpool_node (struct lto_simple_output_block *ob, varpool_node *node,
550 lto_symtab_encoder_t encoder)
552 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
553 struct bitpack_d bp;
554 int ref;
555 bool alias_p;
557 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
558 LTO_symtab_variable);
559 streamer_write_hwi_stream (ob->main_stream, node->order);
560 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
561 bp = bitpack_create (ob->main_stream);
562 bp_pack_value (&bp, node->externally_visible, 1);
563 bp_pack_value (&bp, node->force_output, 1);
564 bp_pack_value (&bp, node->forced_by_abi, 1);
565 bp_pack_value (&bp, node->unique_name, 1);
566 bp_pack_value (&bp, node->definition, 1);
567 alias_p = node->alias && (!boundary_p || node->weakref);
568 bp_pack_value (&bp, alias_p, 1);
569 bp_pack_value (&bp, node->weakref, 1);
570 bp_pack_value (&bp, node->analyzed && !boundary_p, 1);
571 gcc_assert (node->definition || !node->analyzed);
572 /* Constant pool initializers can be de-unified into individual ltrans units.
573 FIXME: Alternatively at -Os we may want to avoid generating for them the local
574 labels and share them across LTRANS partitions. */
575 if (symtab_get_symbol_partitioning_class (node) != SYMBOL_PARTITION)
577 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
578 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
580 else
582 bp_pack_value (&bp, node->definition
583 && referenced_from_other_partition_p (&node->ref_list,
584 encoder), 1);
585 bp_pack_value (&bp, node->analyzed
586 && boundary_p && !DECL_EXTERNAL (node->decl), 1);
587 /* in_other_partition. */
589 streamer_write_bitpack (&bp);
590 if (node->same_comdat_group && !boundary_p)
592 ref = lto_symtab_encoder_lookup (encoder,
593 node->same_comdat_group);
594 gcc_assert (ref != LCC_NOT_FOUND);
596 else
597 ref = LCC_NOT_FOUND;
598 streamer_write_hwi_stream (ob->main_stream, ref);
599 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
600 LDPR_NUM_KNOWN, node->resolution);
603 /* Output the varpool NODE to OB.
604 If NODE is not in SET, then NODE is a boundary. */
606 static void
607 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
608 lto_symtab_encoder_t encoder)
610 struct bitpack_d bp;
611 int nref;
612 int uid = ref->lto_stmt_uid;
613 struct cgraph_node *node;
615 bp = bitpack_create (ob->main_stream);
616 bp_pack_value (&bp, ref->use, 2);
617 bp_pack_value (&bp, ref->speculative, 1);
618 streamer_write_bitpack (&bp);
619 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
620 gcc_assert (nref != LCC_NOT_FOUND);
621 streamer_write_hwi_stream (ob->main_stream, nref);
623 node = dyn_cast <cgraph_node> (ref->referring);
624 if (node)
626 if (ref->stmt)
627 uid = gimple_uid (ref->stmt) + 1;
628 streamer_write_hwi_stream (ob->main_stream, uid);
632 /* Stream out profile_summary to OB. */
634 static void
635 output_profile_summary (struct lto_simple_output_block *ob)
637 unsigned h_ix;
638 struct bitpack_d bp;
640 if (profile_info)
642 /* We do not output num and run_max, they are not used by
643 GCC profile feedback and they are difficult to merge from multiple
644 units. */
645 gcc_assert (profile_info->runs);
646 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
647 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
649 /* sum_all is needed for computing the working set with the
650 histogram. */
651 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
653 /* Create and output a bitpack of non-zero histogram entries indices. */
654 bp = bitpack_create (ob->main_stream);
655 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
656 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
657 streamer_write_bitpack (&bp);
658 /* Now stream out only those non-zero entries. */
659 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
661 if (!profile_info->histogram[h_ix].num_counters)
662 continue;
663 streamer_write_gcov_count_stream (ob->main_stream,
664 profile_info->histogram[h_ix].num_counters);
665 streamer_write_gcov_count_stream (ob->main_stream,
666 profile_info->histogram[h_ix].min_value);
667 streamer_write_gcov_count_stream (ob->main_stream,
668 profile_info->histogram[h_ix].cum_value);
670 /* IPA-profile computes hot bb threshold based on cumulated
671 whole program profile. We need to stream it down to ltrans. */
672 if (flag_wpa)
673 streamer_write_gcov_count_stream (ob->main_stream,
674 get_hot_bb_threshold ());
676 else
677 streamer_write_uhwi_stream (ob->main_stream, 0);
680 /* Output all callees or indirect outgoing edges. EDGE must be the first such
681 edge. */
683 static void
684 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
685 struct lto_simple_output_block *ob,
686 lto_symtab_encoder_t encoder)
688 if (!edge)
689 return;
691 /* Output edges in backward direction, so the reconstructed callgraph match
692 and it is easy to associate call sites in the IPA pass summaries. */
693 while (edge->next_callee)
694 edge = edge->next_callee;
695 for (; edge; edge = edge->prev_callee)
696 lto_output_edge (ob, edge, encoder);
699 /* Output the part of the cgraph in SET. */
701 static void
702 output_refs (lto_symtab_encoder_t encoder)
704 lto_symtab_encoder_iterator lsei;
705 struct lto_simple_output_block *ob;
706 int count;
707 struct ipa_ref *ref;
708 int i;
710 ob = lto_create_simple_output_block (LTO_section_refs);
712 for (lsei = lsei_start_in_partition (encoder); !lsei_end_p (lsei);
713 lsei_next_in_partition (&lsei))
715 symtab_node *node = lsei_node (lsei);
717 count = ipa_ref_list_nreferences (&node->ref_list);
718 if (count)
720 streamer_write_gcov_count_stream (ob->main_stream, count);
721 streamer_write_uhwi_stream (ob->main_stream,
722 lto_symtab_encoder_lookup (encoder, node));
723 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list,
724 i, ref); i++)
725 lto_output_ref (ob, ref, encoder);
729 streamer_write_uhwi_stream (ob->main_stream, 0);
731 lto_destroy_simple_output_block (ob);
734 /* Add NODE into encoder as well as nodes it is cloned from.
735 Do it in a way so clones appear first. */
737 static void
738 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
739 bool include_body)
741 if (node->clone_of)
742 add_node_to (encoder, node->clone_of, include_body);
743 else if (include_body)
744 lto_set_symtab_encoder_encode_body (encoder, node);
745 lto_symtab_encoder_encode (encoder, node);
748 /* Add all references in LIST to encoders. */
750 static void
751 add_references (lto_symtab_encoder_t encoder,
752 struct ipa_ref_list *list)
754 int i;
755 struct ipa_ref *ref;
756 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
757 if (is_a <cgraph_node> (ref->referred))
758 add_node_to (encoder, ipa_ref_node (ref), false);
759 else
760 lto_symtab_encoder_encode (encoder, ref->referred);
763 /* Select what needs to be dumped. In lto case dump everything.
764 In omp target case only dump stuff makrked with attribute. */
765 void
766 select_what_to_dump (bool is_omp)
768 struct symtab_node *snode;
769 FOR_EACH_SYMBOL(snode)
770 snode->need_dump = !is_omp || lookup_attribute ("omp declare target",
771 DECL_ATTRIBUTES (snode->decl));
774 /* Find all symbols we want to stream into given partition and insert them
775 to encoders.
777 The function actually replaces IN_ENCODER by new one. The reason is that
778 streaming code needs clone's origin to be streamed before clone. This
779 means that we need to insert the nodes in specific order. This order is
780 ignored by the partitioning logic earlier. */
782 lto_symtab_encoder_t
783 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
785 struct cgraph_node *node;
786 struct cgraph_edge *edge;
787 int i;
788 lto_symtab_encoder_t encoder;
789 lto_symtab_encoder_iterator lsei;
790 struct pointer_set_t *reachable_call_targets = pointer_set_create ();
792 encoder = lto_symtab_encoder_new (false);
794 /* Go over all entries in the IN_ENCODER and duplicate them to
795 ENCODER. At the same time insert masters of clones so
796 every master appears before clone. */
797 for (lsei = lsei_start_function_in_partition (in_encoder);
798 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
800 node = lsei_cgraph_node (lsei);
801 add_node_to (encoder, node, true);
802 lto_set_symtab_encoder_in_partition (encoder, node);
803 add_references (encoder, &node->ref_list);
804 /* For proper debug info, we need to ship the origins, too. */
805 if (DECL_ABSTRACT_ORIGIN (node->decl))
807 struct cgraph_node *origin_node
808 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (node->decl));
809 add_node_to (encoder, origin_node, true);
812 for (lsei = lsei_start_variable_in_partition (in_encoder);
813 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
815 varpool_node *vnode = lsei_varpool_node (lsei);
817 lto_set_symtab_encoder_in_partition (encoder, vnode);
818 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
819 add_references (encoder, &vnode->ref_list);
820 /* For proper debug info, we need to ship the origins, too. */
821 if (DECL_ABSTRACT_ORIGIN (vnode->decl))
823 varpool_node *origin_node
824 = varpool_get_node (DECL_ABSTRACT_ORIGIN (node->decl));
825 lto_set_symtab_encoder_in_partition (encoder, origin_node);
828 /* Pickle in also the initializer of all referenced readonly variables
829 to help folding. Constant pool variables are not shared, so we must
830 pickle those too. */
831 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
833 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
834 if (varpool_node *vnode = dyn_cast <varpool_node> (node))
836 if (!lto_symtab_encoder_encode_initializer_p (encoder,
837 vnode)
838 && ctor_for_folding (vnode->decl) != error_mark_node)
840 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
841 add_references (encoder, &vnode->ref_list);
846 /* Go over all the nodes again to include callees that are not in
847 SET. */
848 for (lsei = lsei_start_function_in_partition (encoder);
849 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
851 node = lsei_cgraph_node (lsei);
852 for (edge = node->callees; edge; edge = edge->next_callee)
854 struct cgraph_node *callee = edge->callee;
855 if (!lto_symtab_encoder_in_partition_p (encoder, callee))
857 /* We should have moved all the inlines. */
858 gcc_assert (!callee->global.inlined_to);
859 add_node_to (encoder, callee, false);
862 /* Add all possible targets for late devirtualization. */
863 if (flag_devirtualize)
864 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
865 if (edge->indirect_info->polymorphic)
867 unsigned int i;
868 void *cache_token;
869 bool final;
870 vec <cgraph_node *>targets
871 = possible_polymorphic_call_targets
872 (edge, &final, &cache_token);
873 if (!pointer_set_insert (reachable_call_targets,
874 cache_token))
876 for (i = 0; i < targets.length (); i++)
878 struct cgraph_node *callee = targets[i];
880 /* Adding an external declarations into the unit serves
881 no purpose and just increases its boundary. */
882 if (callee->definition
883 && !lto_symtab_encoder_in_partition_p
884 (encoder, callee))
886 gcc_assert (!callee->global.inlined_to);
887 add_node_to (encoder, callee, false);
893 lto_symtab_encoder_delete (in_encoder);
894 pointer_set_destroy (reachable_call_targets);
895 return encoder;
898 /* Output the part of the symtab in SET and VSET. */
900 void
901 output_symtab (void)
903 struct cgraph_node *node;
904 struct lto_simple_output_block *ob;
905 lto_symtab_encoder_iterator lsei;
906 int i, n_nodes;
907 lto_symtab_encoder_t encoder;
909 if (flag_wpa)
910 output_cgraph_opt_summary ();
912 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
914 output_profile_summary (ob);
916 /* An encoder for cgraph nodes should have been created by
917 ipa_write_summaries_1. */
918 gcc_assert (ob->decl_state->symtab_node_encoder);
919 encoder = ob->decl_state->symtab_node_encoder;
921 /* Write out the nodes. We must first output a node and then its clones,
922 otherwise at a time reading back the node there would be nothing to clone
923 from. */
924 n_nodes = lto_symtab_encoder_size (encoder);
925 for (i = 0; i < n_nodes; i++)
927 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
928 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
929 lto_output_node (ob, cnode, encoder);
930 else
931 lto_output_varpool_node (ob, varpool (node), encoder);
935 /* Go over the nodes in SET again to write edges. */
936 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
937 lsei_next_function_in_partition (&lsei))
939 node = lsei_cgraph_node (lsei);
940 output_outgoing_cgraph_edges (node->callees, ob, encoder);
941 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
944 streamer_write_uhwi_stream (ob->main_stream, 0);
946 lto_destroy_simple_output_block (ob);
948 /* Emit toplevel asms.
949 When doing WPA we must output every asm just once. Since we do not partition asm
950 nodes at all, output them to first output. This is kind of hack, but should work
951 well. */
952 if (!asm_nodes_output)
954 asm_nodes_output = true;
955 lto_output_toplevel_asms ();
958 output_refs (encoder);
961 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
962 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
963 NODE or to replace the values in it, for instance because the first
964 time we saw it, the function body was not available but now it
965 is. BP is a bitpack with all the bitflags for NODE read from the
966 stream. */
968 static void
969 input_overwrite_node (struct lto_file_decl_data *file_data,
970 struct cgraph_node *node,
971 enum LTO_symtab_tags tag,
972 struct bitpack_d *bp)
974 node->aux = (void *) tag;
975 node->lto_file_data = file_data;
977 node->local.local = bp_unpack_value (bp, 1);
978 node->externally_visible = bp_unpack_value (bp, 1);
979 node->definition = bp_unpack_value (bp, 1);
980 node->local.versionable = bp_unpack_value (bp, 1);
981 node->local.can_change_signature = bp_unpack_value (bp, 1);
982 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
983 node->force_output = bp_unpack_value (bp, 1);
984 node->forced_by_abi = bp_unpack_value (bp, 1);
985 node->unique_name = bp_unpack_value (bp, 1);
986 node->address_taken = bp_unpack_value (bp, 1);
987 node->used_from_other_partition = bp_unpack_value (bp, 1);
988 node->lowered = bp_unpack_value (bp, 1);
989 node->analyzed = tag == LTO_symtab_analyzed_node;
990 node->in_other_partition = bp_unpack_value (bp, 1);
991 if (node->in_other_partition
992 /* Avoid updating decl when we are seeing just inline clone.
993 When inlining function that has functions already inlined into it,
994 we produce clones of inline clones.
996 WPA partitioning might put each clone into different unit and
997 we might end up streaming inline clone from other partition
998 to support clone we are interested in. */
999 && (!node->clone_of
1000 || node->clone_of->decl != node->decl))
1002 DECL_EXTERNAL (node->decl) = 1;
1003 TREE_STATIC (node->decl) = 0;
1005 node->alias = bp_unpack_value (bp, 1);
1006 node->weakref = bp_unpack_value (bp, 1);
1007 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
1008 node->only_called_at_startup = bp_unpack_value (bp, 1);
1009 node->only_called_at_exit = bp_unpack_value (bp, 1);
1010 node->tm_clone = bp_unpack_value (bp, 1);
1011 node->calls_comdat_local = bp_unpack_value (bp, 1);
1012 node->thunk.thunk_p = bp_unpack_value (bp, 1);
1013 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
1014 LDPR_NUM_KNOWN);
1017 /* Return string alias is alias of. */
1019 static tree
1020 get_alias_symbol (tree decl)
1022 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1023 return get_identifier (TREE_STRING_POINTER
1024 (TREE_VALUE (TREE_VALUE (alias))));
1027 /* Read a node from input_block IB. TAG is the node's tag just read.
1028 Return the node read or overwriten. */
1030 static struct cgraph_node *
1031 input_node (struct lto_file_decl_data *file_data,
1032 struct lto_input_block *ib,
1033 enum LTO_symtab_tags tag,
1034 vec<symtab_node *> nodes)
1036 gcc::pass_manager *passes = g->get_passes ();
1037 tree fn_decl;
1038 struct cgraph_node *node;
1039 struct bitpack_d bp;
1040 unsigned decl_index;
1041 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1042 int clone_ref;
1043 int order;
1044 int i, count;
1046 order = streamer_read_hwi (ib) + order_base;
1047 clone_ref = streamer_read_hwi (ib);
1049 decl_index = streamer_read_uhwi (ib);
1050 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1052 if (clone_ref != LCC_NOT_FOUND)
1054 node = cgraph_clone_node (cgraph (nodes[clone_ref]), fn_decl,
1055 0, CGRAPH_FREQ_BASE, false,
1056 vNULL, false, NULL);
1058 else
1060 /* Declaration of functions can be already merged with a declaration
1061 from other input file. We keep cgraph unmerged until after streaming
1062 of ipa passes is done. Alays forcingly create a fresh node. */
1063 node = cgraph_create_empty_node ();
1064 node->decl = fn_decl;
1065 symtab_register_node (node);
1068 node->order = order;
1069 if (order >= symtab_order)
1070 symtab_order = order + 1;
1072 node->count = streamer_read_gcov_count (ib);
1073 node->count_materialization_scale = streamer_read_hwi (ib);
1075 count = streamer_read_hwi (ib);
1076 node->ipa_transforms_to_apply = vNULL;
1077 for (i = 0; i < count; i++)
1079 opt_pass *pass;
1080 int pid = streamer_read_hwi (ib);
1082 gcc_assert (pid < passes->passes_by_id_size);
1083 pass = passes->passes_by_id[pid];
1084 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *) pass);
1087 if (tag == LTO_symtab_analyzed_node)
1088 ref = streamer_read_hwi (ib);
1090 ref2 = streamer_read_hwi (ib);
1092 /* Make sure that we have not read this node before. Nodes that
1093 have already been read will have their tag stored in the 'aux'
1094 field. Since built-in functions can be referenced in multiple
1095 functions, they are expected to be read more than once. */
1096 if (node->aux && !DECL_BUILT_IN (node->decl))
1097 internal_error ("bytecode stream: found multiple instances of cgraph "
1098 "node with uid %d", node->uid);
1100 node->tp_first_run = streamer_read_uhwi (ib);
1102 bp = streamer_read_bitpack (ib);
1104 input_overwrite_node (file_data, node, tag, &bp);
1106 /* Store a reference for now, and fix up later to be a pointer. */
1107 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
1109 /* Store a reference for now, and fix up later to be a pointer. */
1110 node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
1112 if (node->thunk.thunk_p)
1114 int type = streamer_read_uhwi (ib);
1115 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1116 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1118 node->thunk.fixed_offset = fixed_offset;
1119 node->thunk.this_adjusting = (type & 2);
1120 node->thunk.virtual_value = virtual_value;
1121 node->thunk.virtual_offset_p = (type & 4);
1123 if (node->alias && !node->analyzed && node->weakref)
1124 node->alias_target = get_alias_symbol (node->decl);
1125 node->profile_id = streamer_read_hwi (ib);
1126 return node;
1129 /* Read a node from input_block IB. TAG is the node's tag just read.
1130 Return the node read or overwriten. */
1132 static varpool_node *
1133 input_varpool_node (struct lto_file_decl_data *file_data,
1134 struct lto_input_block *ib)
1136 int decl_index;
1137 tree var_decl;
1138 varpool_node *node;
1139 struct bitpack_d bp;
1140 int ref = LCC_NOT_FOUND;
1141 int order;
1143 order = streamer_read_hwi (ib) + order_base;
1144 decl_index = streamer_read_uhwi (ib);
1145 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1147 /* Declaration of functions can be already merged with a declaration
1148 from other input file. We keep cgraph unmerged until after streaming
1149 of ipa passes is done. Alays forcingly create a fresh node. */
1150 node = varpool_create_empty_node ();
1151 node->decl = var_decl;
1152 symtab_register_node (node);
1154 node->order = order;
1155 if (order >= symtab_order)
1156 symtab_order = order + 1;
1157 node->lto_file_data = file_data;
1159 bp = streamer_read_bitpack (ib);
1160 node->externally_visible = bp_unpack_value (&bp, 1);
1161 node->force_output = bp_unpack_value (&bp, 1);
1162 node->forced_by_abi = bp_unpack_value (&bp, 1);
1163 node->unique_name = bp_unpack_value (&bp, 1);
1164 node->definition = bp_unpack_value (&bp, 1);
1165 node->alias = bp_unpack_value (&bp, 1);
1166 node->weakref = bp_unpack_value (&bp, 1);
1167 node->analyzed = bp_unpack_value (&bp, 1);
1168 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1169 node->in_other_partition = bp_unpack_value (&bp, 1);
1170 if (node->in_other_partition)
1172 DECL_EXTERNAL (node->decl) = 1;
1173 TREE_STATIC (node->decl) = 0;
1175 if (node->alias && !node->analyzed && node->weakref)
1176 node->alias_target = get_alias_symbol (node->decl);
1177 ref = streamer_read_hwi (ib);
1178 /* Store a reference for now, and fix up later to be a pointer. */
1179 node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1180 node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1181 LDPR_NUM_KNOWN);
1183 return node;
1186 /* Read a node from input_block IB. TAG is the node's tag just read.
1187 Return the node read or overwriten. */
1189 static void
1190 input_ref (struct lto_input_block *ib,
1191 symtab_node *referring_node,
1192 vec<symtab_node *> nodes)
1194 symtab_node *node = NULL;
1195 struct bitpack_d bp;
1196 enum ipa_ref_use use;
1197 bool speculative;
1198 struct ipa_ref *ref;
1200 bp = streamer_read_bitpack (ib);
1201 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1202 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1203 node = nodes[streamer_read_hwi (ib)];
1204 ref = ipa_record_reference (referring_node, node, use, NULL);
1205 ref->speculative = speculative;
1206 if (is_a <cgraph_node> (referring_node))
1207 ref->lto_stmt_uid = streamer_read_hwi (ib);
1210 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1211 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1212 edge being read is indirect (in the sense that it has
1213 indirect_unknown_callee set). */
1215 static void
1216 input_edge (struct lto_input_block *ib, vec<symtab_node *> nodes,
1217 bool indirect)
1219 struct cgraph_node *caller, *callee;
1220 struct cgraph_edge *edge;
1221 unsigned int stmt_id;
1222 gcov_type count;
1223 int freq;
1224 cgraph_inline_failed_t inline_failed;
1225 struct bitpack_d bp;
1226 int ecf_flags = 0;
1228 caller = cgraph (nodes[streamer_read_hwi (ib)]);
1229 if (caller == NULL || caller->decl == NULL_TREE)
1230 internal_error ("bytecode stream: no caller found while reading edge");
1232 if (!indirect)
1234 callee = cgraph (nodes[streamer_read_hwi (ib)]);
1235 if (callee == NULL || callee->decl == NULL_TREE)
1236 internal_error ("bytecode stream: no callee found while reading edge");
1238 else
1239 callee = NULL;
1241 count = streamer_read_gcov_count (ib);
1243 bp = streamer_read_bitpack (ib);
1244 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_t, CIF_N_REASONS);
1245 stmt_id = bp_unpack_var_len_unsigned (&bp);
1246 freq = (int) bp_unpack_var_len_unsigned (&bp);
1248 if (indirect)
1249 edge = cgraph_create_indirect_edge (caller, NULL, 0, count, freq);
1250 else
1251 edge = cgraph_create_edge (caller, callee, NULL, count, freq);
1253 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1254 edge->speculative = bp_unpack_value (&bp, 1);
1255 edge->lto_stmt_uid = stmt_id;
1256 edge->inline_failed = inline_failed;
1257 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1258 edge->can_throw_external = bp_unpack_value (&bp, 1);
1259 if (indirect)
1261 if (bp_unpack_value (&bp, 1))
1262 ecf_flags |= ECF_CONST;
1263 if (bp_unpack_value (&bp, 1))
1264 ecf_flags |= ECF_PURE;
1265 if (bp_unpack_value (&bp, 1))
1266 ecf_flags |= ECF_NORETURN;
1267 if (bp_unpack_value (&bp, 1))
1268 ecf_flags |= ECF_MALLOC;
1269 if (bp_unpack_value (&bp, 1))
1270 ecf_flags |= ECF_NOTHROW;
1271 if (bp_unpack_value (&bp, 1))
1272 ecf_flags |= ECF_RETURNS_TWICE;
1273 edge->indirect_info->ecf_flags = ecf_flags;
1274 edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1275 if (edge->indirect_info->common_target_id)
1276 edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1281 /* Read a cgraph from IB using the info in FILE_DATA. */
1283 static vec<symtab_node *>
1284 input_cgraph_1 (struct lto_file_decl_data *file_data,
1285 struct lto_input_block *ib)
1287 enum LTO_symtab_tags tag;
1288 vec<symtab_node *> nodes = vNULL;
1289 symtab_node *node;
1290 unsigned i;
1292 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1293 order_base = symtab_order;
1294 while (tag)
1296 if (tag == LTO_symtab_edge)
1297 input_edge (ib, nodes, false);
1298 else if (tag == LTO_symtab_indirect_edge)
1299 input_edge (ib, nodes, true);
1300 else if (tag == LTO_symtab_variable)
1302 node = input_varpool_node (file_data, ib);
1303 nodes.safe_push (node);
1304 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1306 else
1308 node = input_node (file_data, ib, tag, nodes);
1309 if (node == NULL || node->decl == NULL_TREE)
1310 internal_error ("bytecode stream: found empty cgraph node");
1311 nodes.safe_push (node);
1312 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1315 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1318 lto_input_toplevel_asms (file_data, order_base);
1320 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1321 #ifdef ENABLE_CHECKING
1322 FOR_EACH_VEC_ELT (nodes, i, node)
1323 gcc_assert (node->aux || !is_a <cgraph_node> (node));
1324 #endif
1325 FOR_EACH_VEC_ELT (nodes, i, node)
1327 int ref;
1328 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
1330 ref = (int) (intptr_t) cnode->global.inlined_to;
1332 /* We share declaration of builtins, so we may read same node twice. */
1333 if (!node->aux)
1334 continue;
1335 node->aux = NULL;
1337 /* Fixup inlined_to from reference to pointer. */
1338 if (ref != LCC_NOT_FOUND)
1339 cgraph (node)->global.inlined_to = cgraph (nodes[ref]);
1340 else
1341 cnode->global.inlined_to = NULL;
1344 ref = (int) (intptr_t) node->same_comdat_group;
1346 /* Fixup same_comdat_group from reference to pointer. */
1347 if (ref != LCC_NOT_FOUND)
1348 node->same_comdat_group = nodes[ref];
1349 else
1350 node->same_comdat_group = NULL;
1352 FOR_EACH_VEC_ELT (nodes, i, node)
1353 node->aux = is_a <cgraph_node> (node) ? (void *)1 : NULL;
1354 return nodes;
1357 /* Input ipa_refs. */
1359 static void
1360 input_refs (struct lto_input_block *ib,
1361 vec<symtab_node *> nodes)
1363 int count;
1364 int idx;
1365 while (true)
1367 symtab_node *node;
1368 count = streamer_read_uhwi (ib);
1369 if (!count)
1370 break;
1371 idx = streamer_read_uhwi (ib);
1372 node = nodes[idx];
1373 while (count)
1375 input_ref (ib, node, nodes);
1376 count--;
1382 static struct gcov_ctr_summary lto_gcov_summary;
1384 /* Input profile_info from IB. */
1385 static void
1386 input_profile_summary (struct lto_input_block *ib,
1387 struct lto_file_decl_data *file_data)
1389 unsigned h_ix;
1390 struct bitpack_d bp;
1391 unsigned int runs = streamer_read_uhwi (ib);
1392 if (runs)
1394 file_data->profile_info.runs = runs;
1395 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1396 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1398 memset (file_data->profile_info.histogram, 0,
1399 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1400 /* Input the bitpack of non-zero histogram indices. */
1401 bp = streamer_read_bitpack (ib);
1402 /* Read in and unpack the full bitpack, flagging non-zero
1403 histogram entries by setting the num_counters non-zero. */
1404 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1406 file_data->profile_info.histogram[h_ix].num_counters
1407 = bp_unpack_value (&bp, 1);
1409 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1411 if (!file_data->profile_info.histogram[h_ix].num_counters)
1412 continue;
1414 file_data->profile_info.histogram[h_ix].num_counters
1415 = streamer_read_gcov_count (ib);
1416 file_data->profile_info.histogram[h_ix].min_value
1417 = streamer_read_gcov_count (ib);
1418 file_data->profile_info.histogram[h_ix].cum_value
1419 = streamer_read_gcov_count (ib);
1421 /* IPA-profile computes hot bb threshold based on cumulated
1422 whole program profile. We need to stream it down to ltrans. */
1423 if (flag_ltrans)
1424 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1429 /* Rescale profile summaries to the same number of runs in the whole unit. */
1431 static void
1432 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1434 struct lto_file_decl_data *file_data;
1435 unsigned int j, h_ix;
1436 gcov_unsigned_t max_runs = 0;
1437 struct cgraph_node *node;
1438 struct cgraph_edge *edge;
1439 gcov_type saved_sum_all = 0;
1440 gcov_ctr_summary *saved_profile_info = 0;
1441 int saved_scale = 0;
1443 /* Find unit with maximal number of runs. If we ever get serious about
1444 roundoff errors, we might also consider computing smallest common
1445 multiply. */
1446 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1447 if (max_runs < file_data->profile_info.runs)
1448 max_runs = file_data->profile_info.runs;
1450 if (!max_runs)
1451 return;
1453 /* Simple overflow check. We probably don't need to support that many train
1454 runs. Such a large value probably imply data corruption anyway. */
1455 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1457 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1458 INT_MAX / REG_BR_PROB_BASE);
1459 return;
1462 profile_info = &lto_gcov_summary;
1463 lto_gcov_summary.runs = max_runs;
1464 lto_gcov_summary.sum_max = 0;
1465 memset (lto_gcov_summary.histogram, 0,
1466 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1468 /* Rescale all units to the maximal number of runs.
1469 sum_max can not be easily merged, as we have no idea what files come from
1470 the same run. We do not use the info anyway, so leave it 0. */
1471 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1472 if (file_data->profile_info.runs)
1474 int scale = GCOV_COMPUTE_SCALE (max_runs,
1475 file_data->profile_info.runs);
1476 lto_gcov_summary.sum_max
1477 = MAX (lto_gcov_summary.sum_max,
1478 apply_scale (file_data->profile_info.sum_max, scale));
1479 lto_gcov_summary.sum_all
1480 = MAX (lto_gcov_summary.sum_all,
1481 apply_scale (file_data->profile_info.sum_all, scale));
1482 /* Save a pointer to the profile_info with the largest
1483 scaled sum_all and the scale for use in merging the
1484 histogram. */
1485 if (!saved_profile_info
1486 || lto_gcov_summary.sum_all > saved_sum_all)
1488 saved_profile_info = &file_data->profile_info;
1489 saved_sum_all = lto_gcov_summary.sum_all;
1490 saved_scale = scale;
1494 gcc_assert (saved_profile_info);
1496 /* Scale up the histogram from the profile that had the largest
1497 scaled sum_all above. */
1498 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1500 /* Scale up the min value as we did the corresponding sum_all
1501 above. Use that to find the new histogram index. */
1502 gcov_type scaled_min
1503 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1504 saved_scale);
1505 /* The new index may be shared with another scaled histogram entry,
1506 so we need to account for a non-zero histogram entry at new_ix. */
1507 unsigned new_ix = gcov_histo_index (scaled_min);
1508 lto_gcov_summary.histogram[new_ix].min_value
1509 = (lto_gcov_summary.histogram[new_ix].num_counters
1510 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1511 : scaled_min);
1512 /* Some of the scaled counter values would ostensibly need to be placed
1513 into different (larger) histogram buckets, but we keep things simple
1514 here and place the scaled cumulative counter value in the bucket
1515 corresponding to the scaled minimum counter value. */
1516 lto_gcov_summary.histogram[new_ix].cum_value
1517 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1518 saved_scale);
1519 lto_gcov_summary.histogram[new_ix].num_counters
1520 += saved_profile_info->histogram[h_ix].num_counters;
1523 /* Watch roundoff errors. */
1524 if (lto_gcov_summary.sum_max < max_runs)
1525 lto_gcov_summary.sum_max = max_runs;
1527 /* If merging already happent at WPA time, we are done. */
1528 if (flag_ltrans)
1529 return;
1531 /* Now compute count_materialization_scale of each node.
1532 During LTRANS we already have values of count_materialization_scale
1533 computed, so just update them. */
1534 FOR_EACH_FUNCTION (node)
1535 if (node->lto_file_data
1536 && node->lto_file_data->profile_info.runs)
1538 int scale;
1540 scale = RDIV (node->count_materialization_scale * max_runs,
1541 node->lto_file_data->profile_info.runs);
1542 node->count_materialization_scale = scale;
1543 if (scale < 0)
1544 fatal_error ("Profile information in %s corrupted",
1545 file_data->file_name);
1547 if (scale == REG_BR_PROB_BASE)
1548 continue;
1549 for (edge = node->callees; edge; edge = edge->next_callee)
1550 edge->count = apply_scale (edge->count, scale);
1551 node->count = apply_scale (node->count, scale);
1555 /* Input and merge the symtab from each of the .o files passed to
1556 lto1. */
1558 void
1559 input_symtab (void)
1561 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1562 struct lto_file_decl_data *file_data;
1563 unsigned int j = 0;
1564 struct cgraph_node *node;
1566 while ((file_data = file_data_vec[j++]))
1568 const char *data;
1569 size_t len;
1570 struct lto_input_block *ib;
1571 vec<symtab_node *> nodes;
1573 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1574 &data, &len);
1575 if (!ib)
1576 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1577 input_profile_summary (ib, file_data);
1578 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1579 nodes = input_cgraph_1 (file_data, ib);
1580 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1581 ib, data, len);
1583 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1584 &data, &len);
1585 if (!ib)
1586 fatal_error ("cannot find LTO section refs in %s",
1587 file_data->file_name);
1588 input_refs (ib, nodes);
1589 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1590 ib, data, len);
1591 if (flag_ltrans)
1592 input_cgraph_opt_summary (nodes);
1593 nodes.release ();
1596 merge_profile_summaries (file_data_vec);
1597 get_working_sets ();
1600 /* Clear out the aux field that was used to store enough state to
1601 tell which nodes should be overwritten. */
1602 FOR_EACH_FUNCTION (node)
1604 /* Some nodes may have been created by cgraph_node. This
1605 happens when the callgraph contains nested functions. If the
1606 node for the parent function was never emitted to the gimple
1607 file, cgraph_node will create a node for it when setting the
1608 context of the nested function. */
1609 if (node->lto_file_data)
1610 node->aux = NULL;
1614 /* True when we need optimization summary for NODE. */
1616 static int
1617 output_cgraph_opt_summary_p (struct cgraph_node *node)
1619 return (node->clone_of
1620 && (node->clone.tree_map
1621 || node->clone.args_to_skip
1622 || node->clone.combined_args_to_skip));
1625 /* Output optimization summary for EDGE to OB. */
1626 static void
1627 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1628 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1632 /* Output optimization summary for NODE to OB. */
1634 static void
1635 output_node_opt_summary (struct output_block *ob,
1636 struct cgraph_node *node,
1637 lto_symtab_encoder_t encoder)
1639 unsigned int index;
1640 bitmap_iterator bi;
1641 struct ipa_replace_map *map;
1642 struct bitpack_d bp;
1643 int i;
1644 struct cgraph_edge *e;
1646 if (node->clone.args_to_skip)
1648 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1649 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1650 streamer_write_uhwi (ob, index);
1652 else
1653 streamer_write_uhwi (ob, 0);
1654 if (node->clone.combined_args_to_skip)
1656 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1657 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1658 streamer_write_uhwi (ob, index);
1660 else
1661 streamer_write_uhwi (ob, 0);
1662 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1663 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1665 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1666 mechanism to store function local declarations into summaries. */
1667 gcc_assert (!map->old_tree);
1668 streamer_write_uhwi (ob, map->parm_num);
1669 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1670 stream_write_tree (ob, map->new_tree, true);
1671 bp = bitpack_create (ob->main_stream);
1672 bp_pack_value (&bp, map->replace_p, 1);
1673 bp_pack_value (&bp, map->ref_p, 1);
1674 streamer_write_bitpack (&bp);
1677 if (lto_symtab_encoder_in_partition_p (encoder, node))
1679 for (e = node->callees; e; e = e->next_callee)
1680 output_edge_opt_summary (ob, e);
1681 for (e = node->indirect_calls; e; e = e->next_callee)
1682 output_edge_opt_summary (ob, e);
1686 /* Output optimization summaries stored in callgraph.
1687 At the moment it is the clone info structure. */
1689 static void
1690 output_cgraph_opt_summary (void)
1692 int i, n_nodes;
1693 lto_symtab_encoder_t encoder;
1694 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1695 unsigned count = 0;
1697 ob->cgraph_node = NULL;
1698 encoder = ob->decl_state->symtab_node_encoder;
1699 n_nodes = lto_symtab_encoder_size (encoder);
1700 for (i = 0; i < n_nodes; i++)
1702 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1703 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1704 if (cnode && output_cgraph_opt_summary_p (cnode))
1705 count++;
1707 streamer_write_uhwi (ob, count);
1708 for (i = 0; i < n_nodes; i++)
1710 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1711 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1712 if (cnode && output_cgraph_opt_summary_p (cnode))
1714 streamer_write_uhwi (ob, i);
1715 output_node_opt_summary (ob, cnode, encoder);
1718 produce_asm (ob, NULL);
1719 destroy_output_block (ob);
1722 /* Input optimisation summary of EDGE. */
1724 static void
1725 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
1726 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
1730 /* Input optimisation summary of NODE. */
1732 static void
1733 input_node_opt_summary (struct cgraph_node *node,
1734 struct lto_input_block *ib_main,
1735 struct data_in *data_in)
1737 int i;
1738 int count;
1739 int bit;
1740 struct bitpack_d bp;
1741 struct cgraph_edge *e;
1743 count = streamer_read_uhwi (ib_main);
1744 if (count)
1745 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1746 for (i = 0; i < count; i++)
1748 bit = streamer_read_uhwi (ib_main);
1749 bitmap_set_bit (node->clone.args_to_skip, bit);
1751 count = streamer_read_uhwi (ib_main);
1752 if (count)
1753 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1754 for (i = 0; i < count; i++)
1756 bit = streamer_read_uhwi (ib_main);
1757 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1759 count = streamer_read_uhwi (ib_main);
1760 for (i = 0; i < count; i++)
1762 struct ipa_replace_map *map = ggc_alloc_ipa_replace_map ();
1764 vec_safe_push (node->clone.tree_map, map);
1765 map->parm_num = streamer_read_uhwi (ib_main);
1766 map->old_tree = NULL;
1767 map->new_tree = stream_read_tree (ib_main, data_in);
1768 bp = streamer_read_bitpack (ib_main);
1769 map->replace_p = bp_unpack_value (&bp, 1);
1770 map->ref_p = bp_unpack_value (&bp, 1);
1772 for (e = node->callees; e; e = e->next_callee)
1773 input_edge_opt_summary (e, ib_main);
1774 for (e = node->indirect_calls; e; e = e->next_callee)
1775 input_edge_opt_summary (e, ib_main);
1778 /* Read section in file FILE_DATA of length LEN with data DATA. */
1780 static void
1781 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1782 const char *data, size_t len,
1783 vec<symtab_node *> nodes)
1785 const struct lto_function_header *header =
1786 (const struct lto_function_header *) data;
1787 const int cfg_offset = sizeof (struct lto_function_header);
1788 const int main_offset = cfg_offset + header->cfg_size;
1789 const int string_offset = main_offset + header->main_size;
1790 struct data_in *data_in;
1791 struct lto_input_block ib_main;
1792 unsigned int i;
1793 unsigned int count;
1795 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1796 header->main_size);
1798 data_in =
1799 lto_data_in_create (file_data, (const char *) data + string_offset,
1800 header->string_size, vNULL);
1801 count = streamer_read_uhwi (&ib_main);
1803 for (i = 0; i < count; i++)
1805 int ref = streamer_read_uhwi (&ib_main);
1806 input_node_opt_summary (cgraph (nodes[ref]),
1807 &ib_main, data_in);
1809 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1810 len);
1811 lto_data_in_delete (data_in);
1814 /* Input optimization summary of cgraph. */
1816 static void
1817 input_cgraph_opt_summary (vec<symtab_node *> nodes)
1819 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1820 struct lto_file_decl_data *file_data;
1821 unsigned int j = 0;
1823 while ((file_data = file_data_vec[j++]))
1825 size_t len;
1826 const char *data =
1827 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1828 &len);
1830 if (data)
1831 input_cgraph_opt_section (file_data, data, len, nodes);