* gcc.dg/c11-complex-1.c: Use dg-add-options ieee.
[official-gcc.git] / gcc / lto-cgraph.c
blob99dbf96b7a56ce19be3ddbe53caadac52aba9ce9
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 "gimple.h"
29 #include "expr.h"
30 #include "flags.h"
31 #include "params.h"
32 #include "input.h"
33 #include "hashtab.h"
34 #include "langhooks.h"
35 #include "basic-block.h"
36 #include "bitmap.h"
37 #include "function.h"
38 #include "ggc.h"
39 #include "diagnostic-core.h"
40 #include "except.h"
41 #include "vec.h"
42 #include "timevar.h"
43 #include "pointer-set.h"
44 #include "lto-streamer.h"
45 #include "data-streamer.h"
46 #include "tree-streamer.h"
47 #include "gcov-io.h"
48 #include "tree-pass.h"
49 #include "profile.h"
50 #include "context.h"
51 #include "pass_manager.h"
52 #include "ipa-utils.h"
54 static void output_cgraph_opt_summary (void);
55 static void input_cgraph_opt_summary (vec<symtab_node *> nodes);
57 /* Number of LDPR values known to GCC. */
58 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
60 /* All node orders are ofsetted by ORDER_BASE. */
61 static int order_base;
63 /* Cgraph streaming is organized as set of record whose type
64 is indicated by a tag. */
65 enum LTO_symtab_tags
67 /* Must leave 0 for the stopper. */
69 /* Cgraph node without body available. */
70 LTO_symtab_unavail_node = 1,
71 /* Cgraph node with function body. */
72 LTO_symtab_analyzed_node,
73 /* Cgraph edges. */
74 LTO_symtab_edge,
75 LTO_symtab_indirect_edge,
76 LTO_symtab_variable,
77 LTO_symtab_last_tag
80 /* Create a new symtab encoder.
81 if FOR_INPUT, the encoder allocate only datastructures needed
82 to read the symtab. */
84 lto_symtab_encoder_t
85 lto_symtab_encoder_new (bool for_input)
87 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
89 if (!for_input)
90 encoder->map = pointer_map_create ();
91 encoder->nodes.create (0);
92 return encoder;
96 /* Delete ENCODER and its components. */
98 void
99 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
101 encoder->nodes.release ();
102 if (encoder->map)
103 pointer_map_destroy (encoder->map);
104 free (encoder);
108 /* Return the existing reference number of NODE in the symtab encoder in
109 output block OB. Assign a new reference if this is the first time
110 NODE is encoded. */
113 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
114 symtab_node *node)
116 int ref;
117 void **slot;
119 if (!encoder->map)
121 lto_encoder_entry entry = {node, false, false, false};
123 ref = encoder->nodes.length ();
124 encoder->nodes.safe_push (entry);
125 return ref;
128 slot = pointer_map_contains (encoder->map, node);
129 if (!slot || !*slot)
131 lto_encoder_entry entry = {node, false, false, false};
132 ref = encoder->nodes.length ();
133 if (!slot)
134 slot = pointer_map_insert (encoder->map, node);
135 *slot = (void *) (intptr_t) (ref + 1);
136 encoder->nodes.safe_push (entry);
138 else
139 ref = (size_t) *slot - 1;
141 return ref;
144 /* Remove NODE from encoder. */
146 bool
147 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
148 symtab_node *node)
150 void **slot, **last_slot;
151 int index;
152 lto_encoder_entry last_node;
154 slot = pointer_map_contains (encoder->map, node);
155 if (slot == NULL || !*slot)
156 return false;
158 index = (size_t) *slot - 1;
159 gcc_checking_assert (encoder->nodes[index].node == node);
161 /* Remove from vector. We do this by swapping node with the last element
162 of the vector. */
163 last_node = encoder->nodes.pop ();
164 if (last_node.node != node)
166 last_slot = pointer_map_contains (encoder->map, last_node.node);
167 gcc_checking_assert (last_slot && *last_slot);
168 *last_slot = (void *)(size_t) (index + 1);
170 /* Move the last element to the original spot of NODE. */
171 encoder->nodes[index] = last_node;
174 /* Remove element from hash table. */
175 *slot = NULL;
176 return true;
180 /* Return TRUE if we should encode initializer of NODE (if any). */
182 bool
183 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
184 struct cgraph_node *node)
186 int index = lto_symtab_encoder_lookup (encoder, node);
187 return encoder->nodes[index].body;
190 /* Return TRUE if we should encode body of NODE (if any). */
192 static void
193 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
194 struct cgraph_node *node)
196 int index = lto_symtab_encoder_encode (encoder, node);
197 gcc_checking_assert (encoder->nodes[index].node == node);
198 encoder->nodes[index].body = true;
201 /* Return TRUE if we should encode initializer of NODE (if any). */
203 bool
204 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
205 struct varpool_node *node)
207 int index = lto_symtab_encoder_lookup (encoder, node);
208 if (index == LCC_NOT_FOUND)
209 return false;
210 return encoder->nodes[index].initializer;
213 /* Return TRUE if we should encode initializer of NODE (if any). */
215 static void
216 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
217 struct varpool_node *node)
219 int index = lto_symtab_encoder_lookup (encoder, node);
220 encoder->nodes[index].initializer = true;
223 /* Return TRUE if we should encode initializer of NODE (if any). */
225 bool
226 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
227 symtab_node *node)
229 int index = lto_symtab_encoder_lookup (encoder, node);
230 if (index == LCC_NOT_FOUND)
231 return false;
232 return encoder->nodes[index].in_partition;
235 /* Return TRUE if we should encode body of NODE (if any). */
237 void
238 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
239 symtab_node *node)
241 int index = lto_symtab_encoder_encode (encoder, node);
242 encoder->nodes[index].in_partition = true;
245 /* Output the cgraph EDGE to OB using ENCODER. */
247 static void
248 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
249 lto_symtab_encoder_t encoder)
251 unsigned int uid;
252 intptr_t ref;
253 struct bitpack_d bp;
255 if (edge->indirect_unknown_callee)
256 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
257 LTO_symtab_indirect_edge);
258 else
259 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
260 LTO_symtab_edge);
262 ref = lto_symtab_encoder_lookup (encoder, edge->caller);
263 gcc_assert (ref != LCC_NOT_FOUND);
264 streamer_write_hwi_stream (ob->main_stream, ref);
266 if (!edge->indirect_unknown_callee)
268 ref = lto_symtab_encoder_lookup (encoder, edge->callee);
269 gcc_assert (ref != LCC_NOT_FOUND);
270 streamer_write_hwi_stream (ob->main_stream, ref);
273 streamer_write_gcov_count_stream (ob->main_stream, edge->count);
275 bp = bitpack_create (ob->main_stream);
276 uid = (!gimple_has_body_p (edge->caller->decl)
277 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt) + 1);
278 bp_pack_enum (&bp, cgraph_inline_failed_enum,
279 CIF_N_REASONS, edge->inline_failed);
280 bp_pack_var_len_unsigned (&bp, uid);
281 bp_pack_var_len_unsigned (&bp, edge->frequency);
282 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
283 bp_pack_value (&bp, edge->speculative, 1);
284 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
285 bp_pack_value (&bp, edge->can_throw_external, 1);
286 if (edge->indirect_unknown_callee)
288 int flags = edge->indirect_info->ecf_flags;
289 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
290 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
291 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
292 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
293 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
294 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
295 /* Flags that should not appear on indirect calls. */
296 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
297 | ECF_MAY_BE_ALLOCA
298 | ECF_SIBCALL
299 | ECF_LEAF
300 | ECF_NOVOPS)));
302 streamer_write_bitpack (&bp);
303 if (edge->indirect_unknown_callee)
305 streamer_write_hwi_stream (ob->main_stream,
306 edge->indirect_info->common_target_id);
307 if (edge->indirect_info->common_target_id)
308 streamer_write_hwi_stream
309 (ob->main_stream, edge->indirect_info->common_target_probability);
313 /* Return if LIST contain references from other partitions. */
315 bool
316 referenced_from_other_partition_p (struct ipa_ref_list *list, lto_symtab_encoder_t encoder)
318 int i;
319 struct ipa_ref *ref;
320 for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
322 if (ref->referring->in_other_partition
323 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
324 return true;
326 return false;
329 /* Return true when node is reachable from other partition. */
331 bool
332 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
334 struct cgraph_edge *e;
335 if (!node->definition)
336 return false;
337 if (node->global.inlined_to)
338 return false;
339 for (e = node->callers; e; e = e->next_caller)
340 if (e->caller->in_other_partition
341 || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
342 return true;
343 return false;
346 /* Return if LIST contain references from other partitions. */
348 bool
349 referenced_from_this_partition_p (struct ipa_ref_list *list,
350 lto_symtab_encoder_t encoder)
352 int i;
353 struct ipa_ref *ref;
354 for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
355 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
356 return true;
357 return false;
360 /* Return true when node is reachable from other partition. */
362 bool
363 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
365 struct cgraph_edge *e;
366 for (e = node->callers; e; e = e->next_caller)
367 if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
368 return true;
369 return false;
372 /* Output the cgraph NODE to OB. ENCODER is used to find the
373 reference number of NODE->inlined_to. SET is the set of nodes we
374 are writing to the current file. If NODE is not in SET, then NODE
375 is a boundary of a cgraph_node_set and we pretend NODE just has a
376 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
377 that have had their callgraph node written so far. This is used to
378 determine if NODE is a clone of a previously written node. */
380 static void
381 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
382 lto_symtab_encoder_t encoder)
384 unsigned int tag;
385 struct bitpack_d bp;
386 bool boundary_p;
387 intptr_t ref;
388 bool in_other_partition = false;
389 struct cgraph_node *clone_of, *ultimate_clone_of;
390 struct ipa_opt_pass_d *pass;
391 int i;
392 bool alias_p;
394 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
396 if (node->analyzed && !boundary_p)
397 tag = LTO_symtab_analyzed_node;
398 else
399 tag = LTO_symtab_unavail_node;
401 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
402 tag);
403 streamer_write_hwi_stream (ob->main_stream, node->order);
405 /* In WPA mode, we only output part of the call-graph. Also, we
406 fake cgraph node attributes. There are two cases that we care.
408 Boundary nodes: There are nodes that are not part of SET but are
409 called from within SET. We artificially make them look like
410 externally visible nodes with no function body.
412 Cherry-picked nodes: These are nodes we pulled from other
413 translation units into SET during IPA-inlining. We make them as
414 local static nodes to prevent clashes with other local statics. */
415 if (boundary_p && node->analyzed && !DECL_EXTERNAL (node->decl))
417 /* Inline clones can not be part of boundary.
418 gcc_assert (!node->global.inlined_to);
420 FIXME: At the moment they can be, when partition contains an inline
421 clone that is clone of inline clone from outside partition. We can
422 reshape the clone tree and make other tree to be the root, but it
423 needs a bit extra work and will be promplty done by cgraph_remove_node
424 after reading back. */
425 in_other_partition = 1;
428 clone_of = node->clone_of;
429 while (clone_of
430 && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
431 if (clone_of->prev_sibling_clone)
432 clone_of = clone_of->prev_sibling_clone;
433 else
434 clone_of = clone_of->clone_of;
436 /* See if body of the master function is output. If not, we are seeing only
437 an declaration and we do not need to pass down clone tree. */
438 ultimate_clone_of = clone_of;
439 while (ultimate_clone_of && ultimate_clone_of->clone_of)
440 ultimate_clone_of = ultimate_clone_of->clone_of;
442 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
443 clone_of = NULL;
445 if (tag == LTO_symtab_analyzed_node)
446 gcc_assert (clone_of || !node->clone_of);
447 if (!clone_of)
448 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
449 else
450 streamer_write_hwi_stream (ob->main_stream, ref);
453 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
454 streamer_write_gcov_count_stream (ob->main_stream, node->count);
455 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
457 streamer_write_hwi_stream (ob->main_stream,
458 node->ipa_transforms_to_apply.length ());
459 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
460 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
462 if (tag == LTO_symtab_analyzed_node)
464 if (node->global.inlined_to)
466 ref = lto_symtab_encoder_lookup (encoder, node->global.inlined_to);
467 gcc_assert (ref != LCC_NOT_FOUND);
469 else
470 ref = LCC_NOT_FOUND;
472 streamer_write_hwi_stream (ob->main_stream, ref);
475 if (node->same_comdat_group && !boundary_p)
477 ref = lto_symtab_encoder_lookup (encoder,
478 node->same_comdat_group);
479 gcc_assert (ref != LCC_NOT_FOUND);
481 else
482 ref = LCC_NOT_FOUND;
483 streamer_write_hwi_stream (ob->main_stream, ref);
485 streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
487 bp = bitpack_create (ob->main_stream);
488 bp_pack_value (&bp, node->local.local, 1);
489 bp_pack_value (&bp, node->externally_visible, 1);
490 bp_pack_value (&bp, node->definition, 1);
491 bp_pack_value (&bp, node->local.versionable, 1);
492 bp_pack_value (&bp, node->local.can_change_signature, 1);
493 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
494 bp_pack_value (&bp, node->force_output, 1);
495 bp_pack_value (&bp, node->forced_by_abi, 1);
496 bp_pack_value (&bp, node->unique_name, 1);
497 bp_pack_value (&bp, node->address_taken, 1);
498 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
499 && !DECL_EXTERNAL (node->decl)
500 && !DECL_COMDAT (node->decl)
501 && (reachable_from_other_partition_p (node, encoder)
502 || referenced_from_other_partition_p (&node->ref_list,
503 encoder)), 1);
504 bp_pack_value (&bp, node->lowered, 1);
505 bp_pack_value (&bp, in_other_partition, 1);
506 /* Real aliases in a boundary become non-aliases. However we still stream
507 alias info on weakrefs.
508 TODO: We lose a bit of information here - when we know that variable is
509 defined in other unit, we may use the info on aliases to resolve
510 symbol1 != symbol2 type tests that we can do only for locally defined objects
511 otherwise. */
512 alias_p = node->alias && (!boundary_p || node->weakref);
513 bp_pack_value (&bp, alias_p, 1);
514 bp_pack_value (&bp, node->weakref, 1);
515 bp_pack_value (&bp, node->frequency, 2);
516 bp_pack_value (&bp, node->only_called_at_startup, 1);
517 bp_pack_value (&bp, node->only_called_at_exit, 1);
518 bp_pack_value (&bp, node->tm_clone, 1);
519 bp_pack_value (&bp, node->thunk.thunk_p && !boundary_p, 1);
520 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
521 LDPR_NUM_KNOWN, node->resolution);
522 streamer_write_bitpack (&bp);
524 if (node->thunk.thunk_p && !boundary_p)
526 streamer_write_uhwi_stream
527 (ob->main_stream,
528 1 + (node->thunk.this_adjusting != 0) * 2
529 + (node->thunk.virtual_offset_p != 0) * 4);
530 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
531 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
533 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
536 /* Output the varpool NODE to OB.
537 If NODE is not in SET, then NODE is a boundary. */
539 static void
540 lto_output_varpool_node (struct lto_simple_output_block *ob, struct varpool_node *node,
541 lto_symtab_encoder_t encoder)
543 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
544 struct bitpack_d bp;
545 int ref;
546 bool alias_p;
548 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
549 LTO_symtab_variable);
550 streamer_write_hwi_stream (ob->main_stream, node->order);
551 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
552 bp = bitpack_create (ob->main_stream);
553 bp_pack_value (&bp, node->externally_visible, 1);
554 bp_pack_value (&bp, node->force_output, 1);
555 bp_pack_value (&bp, node->forced_by_abi, 1);
556 bp_pack_value (&bp, node->unique_name, 1);
557 bp_pack_value (&bp, node->definition, 1);
558 alias_p = node->alias && (!boundary_p || node->weakref);
559 bp_pack_value (&bp, alias_p, 1);
560 bp_pack_value (&bp, node->weakref, 1);
561 bp_pack_value (&bp, node->analyzed && !boundary_p, 1);
562 gcc_assert (node->definition || !node->analyzed);
563 /* Constant pool initializers can be de-unified into individual ltrans units.
564 FIXME: Alternatively at -Os we may want to avoid generating for them the local
565 labels and share them across LTRANS partitions. */
566 if (DECL_IN_CONSTANT_POOL (node->decl)
567 && !DECL_EXTERNAL (node->decl)
568 && !DECL_COMDAT (node->decl))
570 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
571 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
573 else
575 bp_pack_value (&bp, node->definition
576 && referenced_from_other_partition_p (&node->ref_list,
577 encoder), 1);
578 bp_pack_value (&bp, node->analyzed
579 && boundary_p && !DECL_EXTERNAL (node->decl), 1);
580 /* in_other_partition. */
582 streamer_write_bitpack (&bp);
583 if (node->same_comdat_group && !boundary_p)
585 ref = lto_symtab_encoder_lookup (encoder,
586 node->same_comdat_group);
587 gcc_assert (ref != LCC_NOT_FOUND);
589 else
590 ref = LCC_NOT_FOUND;
591 streamer_write_hwi_stream (ob->main_stream, ref);
592 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
593 LDPR_NUM_KNOWN, node->resolution);
596 /* Output the varpool NODE to OB.
597 If NODE is not in SET, then NODE is a boundary. */
599 static void
600 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
601 lto_symtab_encoder_t encoder)
603 struct bitpack_d bp;
604 int nref;
605 int uid = ref->lto_stmt_uid;
606 struct cgraph_node *node;
608 bp = bitpack_create (ob->main_stream);
609 bp_pack_value (&bp, ref->use, 2);
610 bp_pack_value (&bp, ref->speculative, 1);
611 streamer_write_bitpack (&bp);
612 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
613 gcc_assert (nref != LCC_NOT_FOUND);
614 streamer_write_hwi_stream (ob->main_stream, nref);
616 node = dyn_cast <cgraph_node> (ref->referring);
617 if (node)
619 if (ref->stmt)
620 uid = gimple_uid (ref->stmt) + 1;
621 streamer_write_hwi_stream (ob->main_stream, uid);
625 /* Stream out profile_summary to OB. */
627 static void
628 output_profile_summary (struct lto_simple_output_block *ob)
630 unsigned h_ix;
631 struct bitpack_d bp;
633 if (profile_info)
635 /* We do not output num and run_max, they are not used by
636 GCC profile feedback and they are difficult to merge from multiple
637 units. */
638 gcc_assert (profile_info->runs);
639 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
640 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
642 /* sum_all is needed for computing the working set with the
643 histogram. */
644 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
646 /* Create and output a bitpack of non-zero histogram entries indices. */
647 bp = bitpack_create (ob->main_stream);
648 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
649 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
650 streamer_write_bitpack (&bp);
651 /* Now stream out only those non-zero entries. */
652 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
654 if (!profile_info->histogram[h_ix].num_counters)
655 continue;
656 streamer_write_gcov_count_stream (ob->main_stream,
657 profile_info->histogram[h_ix].num_counters);
658 streamer_write_gcov_count_stream (ob->main_stream,
659 profile_info->histogram[h_ix].min_value);
660 streamer_write_gcov_count_stream (ob->main_stream,
661 profile_info->histogram[h_ix].cum_value);
663 /* IPA-profile computes hot bb threshold based on cumulated
664 whole program profile. We need to stream it down to ltrans. */
665 if (flag_wpa)
666 streamer_write_gcov_count_stream (ob->main_stream,
667 get_hot_bb_threshold ());
669 else
670 streamer_write_uhwi_stream (ob->main_stream, 0);
673 /* Output all callees or indirect outgoing edges. EDGE must be the first such
674 edge. */
676 static void
677 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
678 struct lto_simple_output_block *ob,
679 lto_symtab_encoder_t encoder)
681 if (!edge)
682 return;
684 /* Output edges in backward direction, so the reconstructed callgraph match
685 and it is easy to associate call sites in the IPA pass summaries. */
686 while (edge->next_callee)
687 edge = edge->next_callee;
688 for (; edge; edge = edge->prev_callee)
689 lto_output_edge (ob, edge, encoder);
692 /* Output the part of the cgraph in SET. */
694 static void
695 output_refs (lto_symtab_encoder_t encoder)
697 lto_symtab_encoder_iterator lsei;
698 struct lto_simple_output_block *ob;
699 int count;
700 struct ipa_ref *ref;
701 int i;
703 ob = lto_create_simple_output_block (LTO_section_refs);
705 for (lsei = lsei_start_in_partition (encoder); !lsei_end_p (lsei);
706 lsei_next_in_partition (&lsei))
708 symtab_node *node = lsei_node (lsei);
710 count = ipa_ref_list_nreferences (&node->ref_list);
711 if (count)
713 streamer_write_gcov_count_stream (ob->main_stream, count);
714 streamer_write_uhwi_stream (ob->main_stream,
715 lto_symtab_encoder_lookup (encoder, node));
716 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list,
717 i, ref); i++)
718 lto_output_ref (ob, ref, encoder);
722 streamer_write_uhwi_stream (ob->main_stream, 0);
724 lto_destroy_simple_output_block (ob);
727 /* Add NODE into encoder as well as nodes it is cloned from.
728 Do it in a way so clones appear first. */
730 static void
731 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
732 bool include_body)
734 if (node->clone_of)
735 add_node_to (encoder, node->clone_of, include_body);
736 else if (include_body)
737 lto_set_symtab_encoder_encode_body (encoder, node);
738 lto_symtab_encoder_encode (encoder, node);
741 /* Add all references in LIST to encoders. */
743 static void
744 add_references (lto_symtab_encoder_t encoder,
745 struct ipa_ref_list *list)
747 int i;
748 struct ipa_ref *ref;
749 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
750 if (is_a <cgraph_node> (ref->referred))
751 add_node_to (encoder, ipa_ref_node (ref), false);
752 else
753 lto_symtab_encoder_encode (encoder, ref->referred);
756 /* Find all symbols we want to stream into given partition and insert them
757 to encoders.
759 The function actually replaces IN_ENCODER by new one. The reason is that
760 streaming code needs clone's origin to be streamed before clone. This
761 means that we need to insert the nodes in specific order. This order is
762 ignored by the partitioning logic earlier. */
764 lto_symtab_encoder_t
765 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
767 struct cgraph_node *node;
768 struct cgraph_edge *edge;
769 int i;
770 lto_symtab_encoder_t encoder;
771 lto_symtab_encoder_iterator lsei;
772 struct pointer_set_t *reachable_call_targets = pointer_set_create ();
774 encoder = lto_symtab_encoder_new (false);
776 /* Go over all entries in the IN_ENCODER and duplicate them to
777 ENCODER. At the same time insert masters of clones so
778 every master appears before clone. */
779 for (lsei = lsei_start_function_in_partition (in_encoder);
780 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
782 node = lsei_cgraph_node (lsei);
783 add_node_to (encoder, node, true);
784 lto_set_symtab_encoder_in_partition (encoder, node);
785 add_references (encoder, &node->ref_list);
786 /* For proper debug info, we need to ship the origins, too. */
787 if (DECL_ABSTRACT_ORIGIN (node->decl))
789 struct cgraph_node *origin_node
790 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (node->decl));
791 add_node_to (encoder, origin_node, true);
794 for (lsei = lsei_start_variable_in_partition (in_encoder);
795 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
797 struct varpool_node *vnode = lsei_varpool_node (lsei);
799 lto_set_symtab_encoder_in_partition (encoder, vnode);
800 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
801 add_references (encoder, &vnode->ref_list);
802 /* For proper debug info, we need to ship the origins, too. */
803 if (DECL_ABSTRACT_ORIGIN (vnode->decl))
805 struct varpool_node *origin_node
806 = varpool_get_node (DECL_ABSTRACT_ORIGIN (node->decl));
807 lto_set_symtab_encoder_in_partition (encoder, origin_node);
810 /* Pickle in also the initializer of all referenced readonly variables
811 to help folding. Constant pool variables are not shared, so we must
812 pickle those too. */
813 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
815 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
816 if (varpool_node *vnode = dyn_cast <varpool_node> (node))
818 if (!lto_symtab_encoder_encode_initializer_p (encoder,
819 vnode)
820 && ctor_for_folding (vnode->decl) != error_mark_node)
822 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
823 add_references (encoder, &vnode->ref_list);
828 /* Go over all the nodes again to include callees that are not in
829 SET. */
830 for (lsei = lsei_start_function_in_partition (encoder);
831 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
833 node = lsei_cgraph_node (lsei);
834 for (edge = node->callees; edge; edge = edge->next_callee)
836 struct cgraph_node *callee = edge->callee;
837 if (!lto_symtab_encoder_in_partition_p (encoder, callee))
839 /* We should have moved all the inlines. */
840 gcc_assert (!callee->global.inlined_to);
841 add_node_to (encoder, callee, false);
844 /* Add all possible targets for late devirtualization. */
845 if (flag_devirtualize)
846 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
847 if (edge->indirect_info->polymorphic)
849 unsigned int i;
850 void *cache_token;
851 bool final;
852 vec <cgraph_node *>targets
853 = possible_polymorphic_call_targets
854 (edge, &final, &cache_token);
855 if (!pointer_set_insert (reachable_call_targets,
856 cache_token))
858 for (i = 0; i < targets.length (); i++)
860 struct cgraph_node *callee = targets[i];
862 /* Adding an external declarations into the unit serves
863 no purpose and just increases its boundary. */
864 if (callee->definition
865 && !lto_symtab_encoder_in_partition_p
866 (encoder, callee))
868 gcc_assert (!callee->global.inlined_to);
869 add_node_to (encoder, callee, false);
875 lto_symtab_encoder_delete (in_encoder);
876 pointer_set_destroy (reachable_call_targets);
877 return encoder;
880 /* Output the part of the symtab in SET and VSET. */
882 void
883 output_symtab (void)
885 struct cgraph_node *node;
886 struct lto_simple_output_block *ob;
887 lto_symtab_encoder_iterator lsei;
888 int i, n_nodes;
889 lto_symtab_encoder_t encoder;
890 static bool asm_nodes_output = false;
892 if (flag_wpa)
893 output_cgraph_opt_summary ();
895 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
897 output_profile_summary (ob);
899 /* An encoder for cgraph nodes should have been created by
900 ipa_write_summaries_1. */
901 gcc_assert (ob->decl_state->symtab_node_encoder);
902 encoder = ob->decl_state->symtab_node_encoder;
904 /* Write out the nodes. We must first output a node and then its clones,
905 otherwise at a time reading back the node there would be nothing to clone
906 from. */
907 n_nodes = lto_symtab_encoder_size (encoder);
908 for (i = 0; i < n_nodes; i++)
910 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
911 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
912 lto_output_node (ob, cnode, encoder);
913 else
914 lto_output_varpool_node (ob, varpool (node), encoder);
918 /* Go over the nodes in SET again to write edges. */
919 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
920 lsei_next_function_in_partition (&lsei))
922 node = lsei_cgraph_node (lsei);
923 output_outgoing_cgraph_edges (node->callees, ob, encoder);
924 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
927 streamer_write_uhwi_stream (ob->main_stream, 0);
929 lto_destroy_simple_output_block (ob);
931 /* Emit toplevel asms.
932 When doing WPA we must output every asm just once. Since we do not partition asm
933 nodes at all, output them to first output. This is kind of hack, but should work
934 well. */
935 if (!asm_nodes_output)
937 asm_nodes_output = true;
938 lto_output_toplevel_asms ();
941 output_refs (encoder);
944 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
945 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
946 NODE or to replace the values in it, for instance because the first
947 time we saw it, the function body was not available but now it
948 is. BP is a bitpack with all the bitflags for NODE read from the
949 stream. */
951 static void
952 input_overwrite_node (struct lto_file_decl_data *file_data,
953 struct cgraph_node *node,
954 enum LTO_symtab_tags tag,
955 struct bitpack_d *bp)
957 node->aux = (void *) tag;
958 node->lto_file_data = file_data;
960 node->local.local = bp_unpack_value (bp, 1);
961 node->externally_visible = bp_unpack_value (bp, 1);
962 node->definition = bp_unpack_value (bp, 1);
963 node->local.versionable = bp_unpack_value (bp, 1);
964 node->local.can_change_signature = bp_unpack_value (bp, 1);
965 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
966 node->force_output = bp_unpack_value (bp, 1);
967 node->forced_by_abi = bp_unpack_value (bp, 1);
968 node->unique_name = bp_unpack_value (bp, 1);
969 node->address_taken = bp_unpack_value (bp, 1);
970 node->used_from_other_partition = bp_unpack_value (bp, 1);
971 node->lowered = bp_unpack_value (bp, 1);
972 node->analyzed = tag == LTO_symtab_analyzed_node;
973 node->in_other_partition = bp_unpack_value (bp, 1);
974 if (node->in_other_partition
975 /* Avoid updating decl when we are seeing just inline clone.
976 When inlining function that has functions already inlined into it,
977 we produce clones of inline clones.
979 WPA partitioning might put each clone into different unit and
980 we might end up streaming inline clone from other partition
981 to support clone we are interested in. */
982 && (!node->clone_of
983 || node->clone_of->decl != node->decl))
985 DECL_EXTERNAL (node->decl) = 1;
986 TREE_STATIC (node->decl) = 0;
988 node->alias = bp_unpack_value (bp, 1);
989 node->weakref = bp_unpack_value (bp, 1);
990 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
991 node->only_called_at_startup = bp_unpack_value (bp, 1);
992 node->only_called_at_exit = bp_unpack_value (bp, 1);
993 node->tm_clone = bp_unpack_value (bp, 1);
994 node->thunk.thunk_p = bp_unpack_value (bp, 1);
995 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
996 LDPR_NUM_KNOWN);
999 /* Return string alias is alias of. */
1001 static tree
1002 get_alias_symbol (tree decl)
1004 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1005 return get_identifier (TREE_STRING_POINTER
1006 (TREE_VALUE (TREE_VALUE (alias))));
1009 /* Read a node from input_block IB. TAG is the node's tag just read.
1010 Return the node read or overwriten. */
1012 static struct cgraph_node *
1013 input_node (struct lto_file_decl_data *file_data,
1014 struct lto_input_block *ib,
1015 enum LTO_symtab_tags tag,
1016 vec<symtab_node *> nodes)
1018 gcc::pass_manager *passes = g->get_passes ();
1019 tree fn_decl;
1020 struct cgraph_node *node;
1021 struct bitpack_d bp;
1022 unsigned decl_index;
1023 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1024 int clone_ref;
1025 int order;
1026 int i, count;
1028 order = streamer_read_hwi (ib) + order_base;
1029 clone_ref = streamer_read_hwi (ib);
1031 decl_index = streamer_read_uhwi (ib);
1032 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1034 if (clone_ref != LCC_NOT_FOUND)
1036 node = cgraph_clone_node (cgraph (nodes[clone_ref]), fn_decl,
1037 0, CGRAPH_FREQ_BASE, false,
1038 vNULL, false, NULL);
1040 else
1042 /* Declaration of functions can be already merged with a declaration
1043 from other input file. We keep cgraph unmerged until after streaming
1044 of ipa passes is done. Alays forcingly create a fresh node. */
1045 node = cgraph_create_empty_node ();
1046 node->decl = fn_decl;
1047 symtab_register_node (node);
1050 node->order = order;
1051 if (order >= symtab_order)
1052 symtab_order = order + 1;
1054 node->count = streamer_read_gcov_count (ib);
1055 node->count_materialization_scale = streamer_read_hwi (ib);
1057 count = streamer_read_hwi (ib);
1058 node->ipa_transforms_to_apply = vNULL;
1059 for (i = 0; i < count; i++)
1061 struct opt_pass *pass;
1062 int pid = streamer_read_hwi (ib);
1064 gcc_assert (pid < passes->passes_by_id_size);
1065 pass = passes->passes_by_id[pid];
1066 node->ipa_transforms_to_apply.safe_push ((struct ipa_opt_pass_d *) pass);
1069 if (tag == LTO_symtab_analyzed_node)
1070 ref = streamer_read_hwi (ib);
1072 ref2 = streamer_read_hwi (ib);
1074 /* Make sure that we have not read this node before. Nodes that
1075 have already been read will have their tag stored in the 'aux'
1076 field. Since built-in functions can be referenced in multiple
1077 functions, they are expected to be read more than once. */
1078 if (node->aux && !DECL_BUILT_IN (node->decl))
1079 internal_error ("bytecode stream: found multiple instances of cgraph "
1080 "node with uid %d", node->uid);
1082 node->tp_first_run = streamer_read_uhwi (ib);
1084 bp = streamer_read_bitpack (ib);
1086 input_overwrite_node (file_data, node, tag, &bp);
1088 /* Store a reference for now, and fix up later to be a pointer. */
1089 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
1091 /* Store a reference for now, and fix up later to be a pointer. */
1092 node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
1094 if (node->thunk.thunk_p)
1096 int type = streamer_read_uhwi (ib);
1097 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1098 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1100 node->thunk.fixed_offset = fixed_offset;
1101 node->thunk.this_adjusting = (type & 2);
1102 node->thunk.virtual_value = virtual_value;
1103 node->thunk.virtual_offset_p = (type & 4);
1105 if (node->alias && !node->analyzed && node->weakref)
1106 node->alias_target = get_alias_symbol (node->decl);
1107 node->profile_id = streamer_read_hwi (ib);
1108 return node;
1111 /* Read a node from input_block IB. TAG is the node's tag just read.
1112 Return the node read or overwriten. */
1114 static struct varpool_node *
1115 input_varpool_node (struct lto_file_decl_data *file_data,
1116 struct lto_input_block *ib)
1118 int decl_index;
1119 tree var_decl;
1120 struct varpool_node *node;
1121 struct bitpack_d bp;
1122 int ref = LCC_NOT_FOUND;
1123 int order;
1125 order = streamer_read_hwi (ib) + order_base;
1126 decl_index = streamer_read_uhwi (ib);
1127 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1129 /* Declaration of functions can be already merged with a declaration
1130 from other input file. We keep cgraph unmerged until after streaming
1131 of ipa passes is done. Alays forcingly create a fresh node. */
1132 node = varpool_create_empty_node ();
1133 node->decl = var_decl;
1134 symtab_register_node (node);
1136 node->order = order;
1137 if (order >= symtab_order)
1138 symtab_order = order + 1;
1139 node->lto_file_data = file_data;
1141 bp = streamer_read_bitpack (ib);
1142 node->externally_visible = bp_unpack_value (&bp, 1);
1143 node->force_output = bp_unpack_value (&bp, 1);
1144 node->forced_by_abi = bp_unpack_value (&bp, 1);
1145 node->unique_name = bp_unpack_value (&bp, 1);
1146 node->definition = bp_unpack_value (&bp, 1);
1147 node->alias = bp_unpack_value (&bp, 1);
1148 node->weakref = bp_unpack_value (&bp, 1);
1149 node->analyzed = bp_unpack_value (&bp, 1);
1150 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1151 node->in_other_partition = bp_unpack_value (&bp, 1);
1152 if (node->in_other_partition)
1154 DECL_EXTERNAL (node->decl) = 1;
1155 TREE_STATIC (node->decl) = 0;
1157 if (node->alias && !node->analyzed && node->weakref)
1158 node->alias_target = get_alias_symbol (node->decl);
1159 ref = streamer_read_hwi (ib);
1160 /* Store a reference for now, and fix up later to be a pointer. */
1161 node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1162 node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1163 LDPR_NUM_KNOWN);
1165 return node;
1168 /* Read a node from input_block IB. TAG is the node's tag just read.
1169 Return the node read or overwriten. */
1171 static void
1172 input_ref (struct lto_input_block *ib,
1173 symtab_node *referring_node,
1174 vec<symtab_node *> nodes)
1176 symtab_node *node = NULL;
1177 struct bitpack_d bp;
1178 enum ipa_ref_use use;
1179 bool speculative;
1180 struct ipa_ref *ref;
1182 bp = streamer_read_bitpack (ib);
1183 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1184 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1185 node = nodes[streamer_read_hwi (ib)];
1186 ref = ipa_record_reference (referring_node, node, use, NULL);
1187 ref->speculative = speculative;
1188 if (is_a <cgraph_node> (referring_node))
1189 ref->lto_stmt_uid = streamer_read_hwi (ib);
1192 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1193 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1194 edge being read is indirect (in the sense that it has
1195 indirect_unknown_callee set). */
1197 static void
1198 input_edge (struct lto_input_block *ib, vec<symtab_node *> nodes,
1199 bool indirect)
1201 struct cgraph_node *caller, *callee;
1202 struct cgraph_edge *edge;
1203 unsigned int stmt_id;
1204 gcov_type count;
1205 int freq;
1206 cgraph_inline_failed_t inline_failed;
1207 struct bitpack_d bp;
1208 int ecf_flags = 0;
1210 caller = cgraph (nodes[streamer_read_hwi (ib)]);
1211 if (caller == NULL || caller->decl == NULL_TREE)
1212 internal_error ("bytecode stream: no caller found while reading edge");
1214 if (!indirect)
1216 callee = cgraph (nodes[streamer_read_hwi (ib)]);
1217 if (callee == NULL || callee->decl == NULL_TREE)
1218 internal_error ("bytecode stream: no callee found while reading edge");
1220 else
1221 callee = NULL;
1223 count = streamer_read_gcov_count (ib);
1225 bp = streamer_read_bitpack (ib);
1226 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_enum, CIF_N_REASONS);
1227 stmt_id = bp_unpack_var_len_unsigned (&bp);
1228 freq = (int) bp_unpack_var_len_unsigned (&bp);
1230 if (indirect)
1231 edge = cgraph_create_indirect_edge (caller, NULL, 0, count, freq);
1232 else
1233 edge = cgraph_create_edge (caller, callee, NULL, count, freq);
1235 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1236 edge->speculative = bp_unpack_value (&bp, 1);
1237 edge->lto_stmt_uid = stmt_id;
1238 edge->inline_failed = inline_failed;
1239 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1240 edge->can_throw_external = bp_unpack_value (&bp, 1);
1241 if (indirect)
1243 if (bp_unpack_value (&bp, 1))
1244 ecf_flags |= ECF_CONST;
1245 if (bp_unpack_value (&bp, 1))
1246 ecf_flags |= ECF_PURE;
1247 if (bp_unpack_value (&bp, 1))
1248 ecf_flags |= ECF_NORETURN;
1249 if (bp_unpack_value (&bp, 1))
1250 ecf_flags |= ECF_MALLOC;
1251 if (bp_unpack_value (&bp, 1))
1252 ecf_flags |= ECF_NOTHROW;
1253 if (bp_unpack_value (&bp, 1))
1254 ecf_flags |= ECF_RETURNS_TWICE;
1255 edge->indirect_info->ecf_flags = ecf_flags;
1256 edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1257 if (edge->indirect_info->common_target_id)
1258 edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1263 /* Read a cgraph from IB using the info in FILE_DATA. */
1265 static vec<symtab_node *>
1266 input_cgraph_1 (struct lto_file_decl_data *file_data,
1267 struct lto_input_block *ib)
1269 enum LTO_symtab_tags tag;
1270 vec<symtab_node *> nodes = vNULL;
1271 symtab_node *node;
1272 unsigned i;
1274 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1275 order_base = symtab_order;
1276 while (tag)
1278 if (tag == LTO_symtab_edge)
1279 input_edge (ib, nodes, false);
1280 else if (tag == LTO_symtab_indirect_edge)
1281 input_edge (ib, nodes, true);
1282 else if (tag == LTO_symtab_variable)
1284 node = input_varpool_node (file_data, ib);
1285 nodes.safe_push (node);
1286 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1288 else
1290 node = input_node (file_data, ib, tag, nodes);
1291 if (node == NULL || node->decl == NULL_TREE)
1292 internal_error ("bytecode stream: found empty cgraph node");
1293 nodes.safe_push (node);
1294 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1297 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1300 lto_input_toplevel_asms (file_data, order_base);
1302 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1303 #ifdef ENABLE_CHECKING
1304 FOR_EACH_VEC_ELT (nodes, i, node)
1305 gcc_assert (node->aux || !is_a <cgraph_node> (node));
1306 #endif
1307 FOR_EACH_VEC_ELT (nodes, i, node)
1309 int ref;
1310 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
1312 ref = (int) (intptr_t) cnode->global.inlined_to;
1314 /* We share declaration of builtins, so we may read same node twice. */
1315 if (!node->aux)
1316 continue;
1317 node->aux = NULL;
1319 /* Fixup inlined_to from reference to pointer. */
1320 if (ref != LCC_NOT_FOUND)
1321 cgraph (node)->global.inlined_to = cgraph (nodes[ref]);
1322 else
1323 cnode->global.inlined_to = NULL;
1326 ref = (int) (intptr_t) node->same_comdat_group;
1328 /* Fixup same_comdat_group from reference to pointer. */
1329 if (ref != LCC_NOT_FOUND)
1330 node->same_comdat_group = nodes[ref];
1331 else
1332 node->same_comdat_group = NULL;
1334 FOR_EACH_VEC_ELT (nodes, i, node)
1335 node->aux = is_a <cgraph_node> (node) ? (void *)1 : NULL;
1336 return nodes;
1339 /* Input ipa_refs. */
1341 static void
1342 input_refs (struct lto_input_block *ib,
1343 vec<symtab_node *> nodes)
1345 int count;
1346 int idx;
1347 while (true)
1349 symtab_node *node;
1350 count = streamer_read_uhwi (ib);
1351 if (!count)
1352 break;
1353 idx = streamer_read_uhwi (ib);
1354 node = nodes[idx];
1355 while (count)
1357 input_ref (ib, node, nodes);
1358 count--;
1364 static struct gcov_ctr_summary lto_gcov_summary;
1366 /* Input profile_info from IB. */
1367 static void
1368 input_profile_summary (struct lto_input_block *ib,
1369 struct lto_file_decl_data *file_data)
1371 unsigned h_ix;
1372 struct bitpack_d bp;
1373 unsigned int runs = streamer_read_uhwi (ib);
1374 if (runs)
1376 file_data->profile_info.runs = runs;
1377 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1378 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1380 memset (file_data->profile_info.histogram, 0,
1381 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1382 /* Input the bitpack of non-zero histogram indices. */
1383 bp = streamer_read_bitpack (ib);
1384 /* Read in and unpack the full bitpack, flagging non-zero
1385 histogram entries by setting the num_counters non-zero. */
1386 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1388 file_data->profile_info.histogram[h_ix].num_counters
1389 = bp_unpack_value (&bp, 1);
1391 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1393 if (!file_data->profile_info.histogram[h_ix].num_counters)
1394 continue;
1396 file_data->profile_info.histogram[h_ix].num_counters
1397 = streamer_read_gcov_count (ib);
1398 file_data->profile_info.histogram[h_ix].min_value
1399 = streamer_read_gcov_count (ib);
1400 file_data->profile_info.histogram[h_ix].cum_value
1401 = streamer_read_gcov_count (ib);
1403 /* IPA-profile computes hot bb threshold based on cumulated
1404 whole program profile. We need to stream it down to ltrans. */
1405 if (flag_ltrans)
1406 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1411 /* Rescale profile summaries to the same number of runs in the whole unit. */
1413 static void
1414 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1416 struct lto_file_decl_data *file_data;
1417 unsigned int j, h_ix;
1418 gcov_unsigned_t max_runs = 0;
1419 struct cgraph_node *node;
1420 struct cgraph_edge *edge;
1421 gcov_type saved_sum_all = 0;
1422 gcov_ctr_summary *saved_profile_info = 0;
1423 int saved_scale = 0;
1425 /* Find unit with maximal number of runs. If we ever get serious about
1426 roundoff errors, we might also consider computing smallest common
1427 multiply. */
1428 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1429 if (max_runs < file_data->profile_info.runs)
1430 max_runs = file_data->profile_info.runs;
1432 if (!max_runs)
1433 return;
1435 /* Simple overflow check. We probably don't need to support that many train
1436 runs. Such a large value probably imply data corruption anyway. */
1437 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1439 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1440 INT_MAX / REG_BR_PROB_BASE);
1441 return;
1444 profile_info = &lto_gcov_summary;
1445 lto_gcov_summary.runs = max_runs;
1446 lto_gcov_summary.sum_max = 0;
1447 memset (lto_gcov_summary.histogram, 0,
1448 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1450 /* Rescale all units to the maximal number of runs.
1451 sum_max can not be easily merged, as we have no idea what files come from
1452 the same run. We do not use the info anyway, so leave it 0. */
1453 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1454 if (file_data->profile_info.runs)
1456 int scale = GCOV_COMPUTE_SCALE (max_runs,
1457 file_data->profile_info.runs);
1458 lto_gcov_summary.sum_max
1459 = MAX (lto_gcov_summary.sum_max,
1460 apply_scale (file_data->profile_info.sum_max, scale));
1461 lto_gcov_summary.sum_all
1462 = MAX (lto_gcov_summary.sum_all,
1463 apply_scale (file_data->profile_info.sum_all, scale));
1464 /* Save a pointer to the profile_info with the largest
1465 scaled sum_all and the scale for use in merging the
1466 histogram. */
1467 if (!saved_profile_info
1468 || lto_gcov_summary.sum_all > saved_sum_all)
1470 saved_profile_info = &file_data->profile_info;
1471 saved_sum_all = lto_gcov_summary.sum_all;
1472 saved_scale = scale;
1476 gcc_assert (saved_profile_info);
1478 /* Scale up the histogram from the profile that had the largest
1479 scaled sum_all above. */
1480 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1482 /* Scale up the min value as we did the corresponding sum_all
1483 above. Use that to find the new histogram index. */
1484 gcov_type scaled_min
1485 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1486 saved_scale);
1487 /* The new index may be shared with another scaled histogram entry,
1488 so we need to account for a non-zero histogram entry at new_ix. */
1489 unsigned new_ix = gcov_histo_index (scaled_min);
1490 lto_gcov_summary.histogram[new_ix].min_value
1491 = (lto_gcov_summary.histogram[new_ix].num_counters
1492 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1493 : scaled_min);
1494 /* Some of the scaled counter values would ostensibly need to be placed
1495 into different (larger) histogram buckets, but we keep things simple
1496 here and place the scaled cumulative counter value in the bucket
1497 corresponding to the scaled minimum counter value. */
1498 lto_gcov_summary.histogram[new_ix].cum_value
1499 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1500 saved_scale);
1501 lto_gcov_summary.histogram[new_ix].num_counters
1502 += saved_profile_info->histogram[h_ix].num_counters;
1505 /* Watch roundoff errors. */
1506 if (lto_gcov_summary.sum_max < max_runs)
1507 lto_gcov_summary.sum_max = max_runs;
1509 /* If merging already happent at WPA time, we are done. */
1510 if (flag_ltrans)
1511 return;
1513 /* Now compute count_materialization_scale of each node.
1514 During LTRANS we already have values of count_materialization_scale
1515 computed, so just update them. */
1516 FOR_EACH_FUNCTION (node)
1517 if (node->lto_file_data
1518 && node->lto_file_data->profile_info.runs)
1520 int scale;
1522 scale = RDIV (node->count_materialization_scale * max_runs,
1523 node->lto_file_data->profile_info.runs);
1524 node->count_materialization_scale = scale;
1525 if (scale < 0)
1526 fatal_error ("Profile information in %s corrupted",
1527 file_data->file_name);
1529 if (scale == REG_BR_PROB_BASE)
1530 continue;
1531 for (edge = node->callees; edge; edge = edge->next_callee)
1532 edge->count = apply_scale (edge->count, scale);
1533 node->count = apply_scale (node->count, scale);
1537 /* Input and merge the symtab from each of the .o files passed to
1538 lto1. */
1540 void
1541 input_symtab (void)
1543 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1544 struct lto_file_decl_data *file_data;
1545 unsigned int j = 0;
1546 struct cgraph_node *node;
1548 while ((file_data = file_data_vec[j++]))
1550 const char *data;
1551 size_t len;
1552 struct lto_input_block *ib;
1553 vec<symtab_node *> nodes;
1555 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1556 &data, &len);
1557 if (!ib)
1558 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1559 input_profile_summary (ib, file_data);
1560 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1561 nodes = input_cgraph_1 (file_data, ib);
1562 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1563 ib, data, len);
1565 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1566 &data, &len);
1567 if (!ib)
1568 fatal_error ("cannot find LTO section refs in %s",
1569 file_data->file_name);
1570 input_refs (ib, nodes);
1571 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1572 ib, data, len);
1573 if (flag_ltrans)
1574 input_cgraph_opt_summary (nodes);
1575 nodes.release ();
1578 merge_profile_summaries (file_data_vec);
1579 get_working_sets ();
1582 /* Clear out the aux field that was used to store enough state to
1583 tell which nodes should be overwritten. */
1584 FOR_EACH_FUNCTION (node)
1586 /* Some nodes may have been created by cgraph_node. This
1587 happens when the callgraph contains nested functions. If the
1588 node for the parent function was never emitted to the gimple
1589 file, cgraph_node will create a node for it when setting the
1590 context of the nested function. */
1591 if (node->lto_file_data)
1592 node->aux = NULL;
1596 /* True when we need optimization summary for NODE. */
1598 static int
1599 output_cgraph_opt_summary_p (struct cgraph_node *node)
1601 return (node->clone_of
1602 && (node->clone.tree_map
1603 || node->clone.args_to_skip
1604 || node->clone.combined_args_to_skip));
1607 /* Output optimization summary for EDGE to OB. */
1608 static void
1609 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1610 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1614 /* Output optimization summary for NODE to OB. */
1616 static void
1617 output_node_opt_summary (struct output_block *ob,
1618 struct cgraph_node *node,
1619 lto_symtab_encoder_t encoder)
1621 unsigned int index;
1622 bitmap_iterator bi;
1623 struct ipa_replace_map *map;
1624 struct bitpack_d bp;
1625 int i;
1626 struct cgraph_edge *e;
1628 if (node->clone.args_to_skip)
1630 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1631 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1632 streamer_write_uhwi (ob, index);
1634 else
1635 streamer_write_uhwi (ob, 0);
1636 if (node->clone.combined_args_to_skip)
1638 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1639 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1640 streamer_write_uhwi (ob, index);
1642 else
1643 streamer_write_uhwi (ob, 0);
1644 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1645 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1647 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1648 mechanism to store function local declarations into summaries. */
1649 gcc_assert (!map->old_tree);
1650 streamer_write_uhwi (ob, map->parm_num);
1651 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1652 stream_write_tree (ob, map->new_tree, true);
1653 bp = bitpack_create (ob->main_stream);
1654 bp_pack_value (&bp, map->replace_p, 1);
1655 bp_pack_value (&bp, map->ref_p, 1);
1656 streamer_write_bitpack (&bp);
1659 if (lto_symtab_encoder_in_partition_p (encoder, node))
1661 for (e = node->callees; e; e = e->next_callee)
1662 output_edge_opt_summary (ob, e);
1663 for (e = node->indirect_calls; e; e = e->next_callee)
1664 output_edge_opt_summary (ob, e);
1668 /* Output optimization summaries stored in callgraph.
1669 At the moment it is the clone info structure. */
1671 static void
1672 output_cgraph_opt_summary (void)
1674 int i, n_nodes;
1675 lto_symtab_encoder_t encoder;
1676 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1677 unsigned count = 0;
1679 ob->cgraph_node = NULL;
1680 encoder = ob->decl_state->symtab_node_encoder;
1681 n_nodes = lto_symtab_encoder_size (encoder);
1682 for (i = 0; i < n_nodes; i++)
1684 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1685 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1686 if (cnode && output_cgraph_opt_summary_p (cnode))
1687 count++;
1689 streamer_write_uhwi (ob, count);
1690 for (i = 0; i < n_nodes; i++)
1692 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1693 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1694 if (cnode && output_cgraph_opt_summary_p (cnode))
1696 streamer_write_uhwi (ob, i);
1697 output_node_opt_summary (ob, cnode, encoder);
1700 produce_asm (ob, NULL);
1701 destroy_output_block (ob);
1704 /* Input optimisation summary of EDGE. */
1706 static void
1707 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
1708 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
1712 /* Input optimisation summary of NODE. */
1714 static void
1715 input_node_opt_summary (struct cgraph_node *node,
1716 struct lto_input_block *ib_main,
1717 struct data_in *data_in)
1719 int i;
1720 int count;
1721 int bit;
1722 struct bitpack_d bp;
1723 struct cgraph_edge *e;
1725 count = streamer_read_uhwi (ib_main);
1726 if (count)
1727 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1728 for (i = 0; i < count; i++)
1730 bit = streamer_read_uhwi (ib_main);
1731 bitmap_set_bit (node->clone.args_to_skip, bit);
1733 count = streamer_read_uhwi (ib_main);
1734 if (count)
1735 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1736 for (i = 0; i < count; i++)
1738 bit = streamer_read_uhwi (ib_main);
1739 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1741 count = streamer_read_uhwi (ib_main);
1742 for (i = 0; i < count; i++)
1744 struct ipa_replace_map *map = ggc_alloc_ipa_replace_map ();
1746 vec_safe_push (node->clone.tree_map, map);
1747 map->parm_num = streamer_read_uhwi (ib_main);
1748 map->old_tree = NULL;
1749 map->new_tree = stream_read_tree (ib_main, data_in);
1750 bp = streamer_read_bitpack (ib_main);
1751 map->replace_p = bp_unpack_value (&bp, 1);
1752 map->ref_p = bp_unpack_value (&bp, 1);
1754 for (e = node->callees; e; e = e->next_callee)
1755 input_edge_opt_summary (e, ib_main);
1756 for (e = node->indirect_calls; e; e = e->next_callee)
1757 input_edge_opt_summary (e, ib_main);
1760 /* Read section in file FILE_DATA of length LEN with data DATA. */
1762 static void
1763 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1764 const char *data, size_t len,
1765 vec<symtab_node *> nodes)
1767 const struct lto_function_header *header =
1768 (const struct lto_function_header *) data;
1769 const int cfg_offset = sizeof (struct lto_function_header);
1770 const int main_offset = cfg_offset + header->cfg_size;
1771 const int string_offset = main_offset + header->main_size;
1772 struct data_in *data_in;
1773 struct lto_input_block ib_main;
1774 unsigned int i;
1775 unsigned int count;
1777 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1778 header->main_size);
1780 data_in =
1781 lto_data_in_create (file_data, (const char *) data + string_offset,
1782 header->string_size, vNULL);
1783 count = streamer_read_uhwi (&ib_main);
1785 for (i = 0; i < count; i++)
1787 int ref = streamer_read_uhwi (&ib_main);
1788 input_node_opt_summary (cgraph (nodes[ref]),
1789 &ib_main, data_in);
1791 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1792 len);
1793 lto_data_in_delete (data_in);
1796 /* Input optimization summary of cgraph. */
1798 static void
1799 input_cgraph_opt_summary (vec<symtab_node *> nodes)
1801 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1802 struct lto_file_decl_data *file_data;
1803 unsigned int j = 0;
1805 while ((file_data = file_data_vec[j++]))
1807 size_t len;
1808 const char *data =
1809 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1810 &len);
1812 if (data)
1813 input_cgraph_opt_section (file_data, data, len, nodes);