* lto-cgraph.c: Include ipa-utils.h.
[official-gcc.git] / gcc / lto-cgraph.c
blob4eb443bc8df3f373d8cdeeee2f439bde6ef28662
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 "expr.h"
29 #include "flags.h"
30 #include "params.h"
31 #include "input.h"
32 #include "hashtab.h"
33 #include "langhooks.h"
34 #include "basic-block.h"
35 #include "tree-flow.h"
36 #include "cgraph.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, (symtab_node)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, (symtab_node)node);
197 gcc_checking_assert (encoder->nodes[index].node == (symtab_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, (symtab_node)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, (symtab_node)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, (symtab_node)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, (symtab_node)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, (symtab_node)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, (symtab_node)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->symbol.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->symbol.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->symbol.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->symbol.in_other_partition
341 || !lto_symtab_encoder_in_partition_p (encoder, (symtab_node)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, (symtab_node)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, (symtab_node)node);
396 if (node->symbol.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->symbol.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->symbol.analyzed && !DECL_EXTERNAL (node->symbol.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, (symtab_node)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->symbol.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, (symtab_node)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->symbol.same_comdat_group && !boundary_p)
477 ref = lto_symtab_encoder_lookup (encoder,
478 node->symbol.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 bp = bitpack_create (ob->main_stream);
486 bp_pack_value (&bp, node->local.local, 1);
487 bp_pack_value (&bp, node->symbol.externally_visible, 1);
488 bp_pack_value (&bp, node->symbol.definition, 1);
489 bp_pack_value (&bp, node->local.versionable, 1);
490 bp_pack_value (&bp, node->local.can_change_signature, 1);
491 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
492 bp_pack_value (&bp, node->symbol.force_output, 1);
493 bp_pack_value (&bp, node->symbol.forced_by_abi, 1);
494 bp_pack_value (&bp, node->symbol.unique_name, 1);
495 bp_pack_value (&bp, node->symbol.address_taken, 1);
496 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
497 && !DECL_EXTERNAL (node->symbol.decl)
498 && !DECL_COMDAT (node->symbol.decl)
499 && (reachable_from_other_partition_p (node, encoder)
500 || referenced_from_other_partition_p (&node->symbol.ref_list,
501 encoder)), 1);
502 bp_pack_value (&bp, node->lowered, 1);
503 bp_pack_value (&bp, in_other_partition, 1);
504 /* Real aliases in a boundary become non-aliases. However we still stream
505 alias info on weakrefs.
506 TODO: We lose a bit of information here - when we know that variable is
507 defined in other unit, we may use the info on aliases to resolve
508 symbol1 != symbol2 type tests that we can do only for locally defined objects
509 otherwise. */
510 alias_p = node->symbol.alias && (!boundary_p || node->symbol.weakref);
511 bp_pack_value (&bp, alias_p, 1);
512 bp_pack_value (&bp, node->symbol.weakref, 1);
513 bp_pack_value (&bp, node->frequency, 2);
514 bp_pack_value (&bp, node->only_called_at_startup, 1);
515 bp_pack_value (&bp, node->only_called_at_exit, 1);
516 bp_pack_value (&bp, node->tm_clone, 1);
517 bp_pack_value (&bp, node->thunk.thunk_p && !boundary_p, 1);
518 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
519 LDPR_NUM_KNOWN, node->symbol.resolution);
520 streamer_write_bitpack (&bp);
522 if (node->thunk.thunk_p && !boundary_p)
524 streamer_write_uhwi_stream
525 (ob->main_stream,
526 1 + (node->thunk.this_adjusting != 0) * 2
527 + (node->thunk.virtual_offset_p != 0) * 4);
528 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
529 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
531 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
534 /* Output the varpool NODE to OB.
535 If NODE is not in SET, then NODE is a boundary. */
537 static void
538 lto_output_varpool_node (struct lto_simple_output_block *ob, struct varpool_node *node,
539 lto_symtab_encoder_t encoder)
541 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, (symtab_node)node);
542 struct bitpack_d bp;
543 int ref;
544 bool alias_p;
546 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
547 LTO_symtab_variable);
548 streamer_write_hwi_stream (ob->main_stream, node->symbol.order);
549 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->symbol.decl);
550 bp = bitpack_create (ob->main_stream);
551 bp_pack_value (&bp, node->symbol.externally_visible, 1);
552 bp_pack_value (&bp, node->symbol.force_output, 1);
553 bp_pack_value (&bp, node->symbol.forced_by_abi, 1);
554 bp_pack_value (&bp, node->symbol.unique_name, 1);
555 bp_pack_value (&bp, node->symbol.definition, 1);
556 alias_p = node->symbol.alias && (!boundary_p || node->symbol.weakref);
557 bp_pack_value (&bp, alias_p, 1);
558 bp_pack_value (&bp, node->symbol.weakref, 1);
559 bp_pack_value (&bp, node->symbol.analyzed && !boundary_p, 1);
560 gcc_assert (node->symbol.definition || !node->symbol.analyzed);
561 /* Constant pool initializers can be de-unified into individual ltrans units.
562 FIXME: Alternatively at -Os we may want to avoid generating for them the local
563 labels and share them across LTRANS partitions. */
564 if (DECL_IN_CONSTANT_POOL (node->symbol.decl)
565 && !DECL_EXTERNAL (node->symbol.decl)
566 && !DECL_COMDAT (node->symbol.decl))
568 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
569 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
571 else
573 bp_pack_value (&bp, node->symbol.definition
574 && referenced_from_other_partition_p (&node->symbol.ref_list,
575 encoder), 1);
576 bp_pack_value (&bp, node->symbol.analyzed
577 && boundary_p && !DECL_EXTERNAL (node->symbol.decl), 1);
578 /* in_other_partition. */
580 streamer_write_bitpack (&bp);
581 if (node->symbol.same_comdat_group && !boundary_p)
583 ref = lto_symtab_encoder_lookup (encoder,
584 node->symbol.same_comdat_group);
585 gcc_assert (ref != LCC_NOT_FOUND);
587 else
588 ref = LCC_NOT_FOUND;
589 streamer_write_hwi_stream (ob->main_stream, ref);
590 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
591 LDPR_NUM_KNOWN, node->symbol.resolution);
594 /* Output the varpool NODE to OB.
595 If NODE is not in SET, then NODE is a boundary. */
597 static void
598 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
599 lto_symtab_encoder_t encoder)
601 struct bitpack_d bp;
602 int nref;
603 int uid = ref->lto_stmt_uid;
604 struct cgraph_node *node;
606 bp = bitpack_create (ob->main_stream);
607 bp_pack_value (&bp, ref->use, 2);
608 bp_pack_value (&bp, ref->speculative, 1);
609 streamer_write_bitpack (&bp);
610 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
611 gcc_assert (nref != LCC_NOT_FOUND);
612 streamer_write_hwi_stream (ob->main_stream, nref);
614 node = dyn_cast <cgraph_node> (ref->referring);
615 if (node)
617 if (ref->stmt)
618 uid = gimple_uid (ref->stmt) + 1;
619 streamer_write_hwi_stream (ob->main_stream, uid);
623 /* Stream out profile_summary to OB. */
625 static void
626 output_profile_summary (struct lto_simple_output_block *ob)
628 unsigned h_ix;
629 struct bitpack_d bp;
631 if (profile_info)
633 /* We do not output num and run_max, they are not used by
634 GCC profile feedback and they are difficult to merge from multiple
635 units. */
636 gcc_assert (profile_info->runs);
637 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
638 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
640 /* sum_all is needed for computing the working set with the
641 histogram. */
642 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
644 /* Create and output a bitpack of non-zero histogram entries indices. */
645 bp = bitpack_create (ob->main_stream);
646 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
647 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
648 streamer_write_bitpack (&bp);
649 /* Now stream out only those non-zero entries. */
650 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
652 if (!profile_info->histogram[h_ix].num_counters)
653 continue;
654 streamer_write_gcov_count_stream (ob->main_stream,
655 profile_info->histogram[h_ix].num_counters);
656 streamer_write_gcov_count_stream (ob->main_stream,
657 profile_info->histogram[h_ix].min_value);
658 streamer_write_gcov_count_stream (ob->main_stream,
659 profile_info->histogram[h_ix].cum_value);
661 /* IPA-profile computes hot bb threshold based on cumulated
662 whole program profile. We need to stream it down to ltrans. */
663 if (flag_wpa)
664 streamer_write_gcov_count_stream (ob->main_stream,
665 get_hot_bb_threshold ());
667 else
668 streamer_write_uhwi_stream (ob->main_stream, 0);
671 /* Output all callees or indirect outgoing edges. EDGE must be the first such
672 edge. */
674 static void
675 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
676 struct lto_simple_output_block *ob,
677 lto_symtab_encoder_t encoder)
679 if (!edge)
680 return;
682 /* Output edges in backward direction, so the reconstructed callgraph match
683 and it is easy to associate call sites in the IPA pass summaries. */
684 while (edge->next_callee)
685 edge = edge->next_callee;
686 for (; edge; edge = edge->prev_callee)
687 lto_output_edge (ob, edge, encoder);
690 /* Output the part of the cgraph in SET. */
692 static void
693 output_refs (lto_symtab_encoder_t encoder)
695 lto_symtab_encoder_iterator lsei;
696 struct lto_simple_output_block *ob;
697 int count;
698 struct ipa_ref *ref;
699 int i;
701 ob = lto_create_simple_output_block (LTO_section_refs);
703 for (lsei = lsei_start_in_partition (encoder); !lsei_end_p (lsei);
704 lsei_next_in_partition (&lsei))
706 symtab_node node = lsei_node (lsei);
708 count = ipa_ref_list_nreferences (&node->symbol.ref_list);
709 if (count)
711 streamer_write_gcov_count_stream (ob->main_stream, count);
712 streamer_write_uhwi_stream (ob->main_stream,
713 lto_symtab_encoder_lookup (encoder, node));
714 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list,
715 i, ref); i++)
716 lto_output_ref (ob, ref, encoder);
720 streamer_write_uhwi_stream (ob->main_stream, 0);
722 lto_destroy_simple_output_block (ob);
725 /* Add NODE into encoder as well as nodes it is cloned from.
726 Do it in a way so clones appear first. */
728 static void
729 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
730 bool include_body)
732 if (node->clone_of)
733 add_node_to (encoder, node->clone_of, include_body);
734 else if (include_body)
735 lto_set_symtab_encoder_encode_body (encoder, node);
736 lto_symtab_encoder_encode (encoder, (symtab_node)node);
739 /* Add all references in LIST to encoders. */
741 static void
742 add_references (lto_symtab_encoder_t encoder,
743 struct ipa_ref_list *list)
745 int i;
746 struct ipa_ref *ref;
747 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
748 if (is_a <cgraph_node> (ref->referred))
749 add_node_to (encoder, ipa_ref_node (ref), false);
750 else
751 lto_symtab_encoder_encode (encoder, ref->referred);
754 /* Find all symbols we want to stream into given partition and insert them
755 to encoders.
757 The function actually replaces IN_ENCODER by new one. The reason is that
758 streaming code needs clone's origin to be streamed before clone. This
759 means that we need to insert the nodes in specific order. This order is
760 ignored by the partitioning logic earlier. */
762 lto_symtab_encoder_t
763 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
765 struct cgraph_node *node;
766 struct cgraph_edge *edge;
767 int i;
768 lto_symtab_encoder_t encoder;
769 lto_symtab_encoder_iterator lsei;
770 struct pointer_set_t *reachable_call_targets = pointer_set_create ();
772 encoder = lto_symtab_encoder_new (false);
774 /* Go over all entries in the IN_ENCODER and duplicate them to
775 ENCODER. At the same time insert masters of clones so
776 every master appears before clone. */
777 for (lsei = lsei_start_function_in_partition (in_encoder);
778 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
780 node = lsei_cgraph_node (lsei);
781 add_node_to (encoder, node, true);
782 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)node);
783 add_references (encoder, &node->symbol.ref_list);
784 /* For proper debug info, we need to ship the origins, too. */
785 if (DECL_ABSTRACT_ORIGIN (node->symbol.decl))
787 struct cgraph_node *origin_node
788 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (node->symbol.decl));
789 add_node_to (encoder, origin_node, true);
792 for (lsei = lsei_start_variable_in_partition (in_encoder);
793 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
795 struct varpool_node *vnode = lsei_varpool_node (lsei);
797 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)vnode);
798 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
799 add_references (encoder, &vnode->symbol.ref_list);
800 /* For proper debug info, we need to ship the origins, too. */
801 if (DECL_ABSTRACT_ORIGIN (vnode->symbol.decl))
803 struct varpool_node *origin_node
804 = varpool_get_node (DECL_ABSTRACT_ORIGIN (node->symbol.decl));
805 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)origin_node);
808 /* Pickle in also the initializer of all referenced readonly variables
809 to help folding. Constant pool variables are not shared, so we must
810 pickle those too. */
811 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
813 symtab_node node = lto_symtab_encoder_deref (encoder, i);
814 if (varpool_node *vnode = dyn_cast <varpool_node> (node))
816 if (!lto_symtab_encoder_encode_initializer_p (encoder,
817 vnode)
818 && ctor_for_folding (vnode->symbol.decl) != error_mark_node)
820 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
821 add_references (encoder, &vnode->symbol.ref_list);
826 /* Go over all the nodes again to include callees that are not in
827 SET. */
828 for (lsei = lsei_start_function_in_partition (encoder);
829 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
831 node = lsei_cgraph_node (lsei);
832 for (edge = node->callees; edge; edge = edge->next_callee)
834 struct cgraph_node *callee = edge->callee;
835 if (!lto_symtab_encoder_in_partition_p (encoder, (symtab_node)callee))
837 /* We should have moved all the inlines. */
838 gcc_assert (!callee->global.inlined_to);
839 add_node_to (encoder, callee, false);
842 /* Add all possible targets for late devirtualization. */
843 if (flag_devirtualize)
844 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
845 if (edge->indirect_info->polymorphic)
847 unsigned int i;
848 void *cache_token;
849 bool final;
850 vec <cgraph_node *>targets
851 = possible_polymorphic_call_targets
852 (edge, &final, &cache_token);
853 if (!pointer_set_insert (reachable_call_targets,
854 cache_token))
856 for (i = 0; i < targets.length(); i++)
858 struct cgraph_node *callee = targets[i];
860 /* Adding an external declarations into the unit serves
861 no purpose and just increases its boundary. */
862 if (callee->symbol.definition
863 && !lto_symtab_encoder_in_partition_p
864 (encoder, (symtab_node)callee))
866 gcc_assert (!callee->global.inlined_to);
867 add_node_to (encoder, callee, false);
873 lto_symtab_encoder_delete (in_encoder);
874 pointer_set_destroy (reachable_call_targets);
875 return encoder;
878 /* Output the part of the symtab in SET and VSET. */
880 void
881 output_symtab (void)
883 struct cgraph_node *node;
884 struct lto_simple_output_block *ob;
885 lto_symtab_encoder_iterator lsei;
886 int i, n_nodes;
887 lto_symtab_encoder_t encoder;
888 static bool asm_nodes_output = false;
890 if (flag_wpa)
891 output_cgraph_opt_summary ();
893 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
895 output_profile_summary (ob);
897 /* An encoder for cgraph nodes should have been created by
898 ipa_write_summaries_1. */
899 gcc_assert (ob->decl_state->symtab_node_encoder);
900 encoder = ob->decl_state->symtab_node_encoder;
902 /* Write out the nodes. We must first output a node and then its clones,
903 otherwise at a time reading back the node there would be nothing to clone
904 from. */
905 n_nodes = lto_symtab_encoder_size (encoder);
906 for (i = 0; i < n_nodes; i++)
908 symtab_node node = lto_symtab_encoder_deref (encoder, i);
909 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
910 lto_output_node (ob, cnode, encoder);
911 else
912 lto_output_varpool_node (ob, varpool (node), encoder);
916 /* Go over the nodes in SET again to write edges. */
917 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
918 lsei_next_function_in_partition (&lsei))
920 node = lsei_cgraph_node (lsei);
921 output_outgoing_cgraph_edges (node->callees, ob, encoder);
922 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
925 streamer_write_uhwi_stream (ob->main_stream, 0);
927 lto_destroy_simple_output_block (ob);
929 /* Emit toplevel asms.
930 When doing WPA we must output every asm just once. Since we do not partition asm
931 nodes at all, output them to first output. This is kind of hack, but should work
932 well. */
933 if (!asm_nodes_output)
935 asm_nodes_output = true;
936 lto_output_toplevel_asms ();
939 output_refs (encoder);
942 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
943 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
944 NODE or to replace the values in it, for instance because the first
945 time we saw it, the function body was not available but now it
946 is. BP is a bitpack with all the bitflags for NODE read from the
947 stream. */
949 static void
950 input_overwrite_node (struct lto_file_decl_data *file_data,
951 struct cgraph_node *node,
952 enum LTO_symtab_tags tag,
953 struct bitpack_d *bp)
955 node->symbol.aux = (void *) tag;
956 node->symbol.lto_file_data = file_data;
958 node->local.local = bp_unpack_value (bp, 1);
959 node->symbol.externally_visible = bp_unpack_value (bp, 1);
960 node->symbol.definition = bp_unpack_value (bp, 1);
961 node->local.versionable = bp_unpack_value (bp, 1);
962 node->local.can_change_signature = bp_unpack_value (bp, 1);
963 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
964 node->symbol.force_output = bp_unpack_value (bp, 1);
965 node->symbol.forced_by_abi = bp_unpack_value (bp, 1);
966 node->symbol.unique_name = bp_unpack_value (bp, 1);
967 node->symbol.address_taken = bp_unpack_value (bp, 1);
968 node->symbol.used_from_other_partition = bp_unpack_value (bp, 1);
969 node->lowered = bp_unpack_value (bp, 1);
970 node->symbol.analyzed = tag == LTO_symtab_analyzed_node;
971 node->symbol.in_other_partition = bp_unpack_value (bp, 1);
972 if (node->symbol.in_other_partition
973 /* Avoid updating decl when we are seeing just inline clone.
974 When inlining function that has functions already inlined into it,
975 we produce clones of inline clones.
977 WPA partitioning might put each clone into different unit and
978 we might end up streaming inline clone from other partition
979 to support clone we are interested in. */
980 && (!node->clone_of
981 || node->clone_of->symbol.decl != node->symbol.decl))
983 DECL_EXTERNAL (node->symbol.decl) = 1;
984 TREE_STATIC (node->symbol.decl) = 0;
986 node->symbol.alias = bp_unpack_value (bp, 1);
987 node->symbol.weakref = bp_unpack_value (bp, 1);
988 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
989 node->only_called_at_startup = bp_unpack_value (bp, 1);
990 node->only_called_at_exit = bp_unpack_value (bp, 1);
991 node->tm_clone = bp_unpack_value (bp, 1);
992 node->thunk.thunk_p = bp_unpack_value (bp, 1);
993 node->symbol.resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
994 LDPR_NUM_KNOWN);
997 /* Return string alias is alias of. */
999 static tree
1000 get_alias_symbol (tree decl)
1002 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1003 return get_identifier (TREE_STRING_POINTER
1004 (TREE_VALUE (TREE_VALUE (alias))));
1007 /* Read a node from input_block IB. TAG is the node's tag just read.
1008 Return the node read or overwriten. */
1010 static struct cgraph_node *
1011 input_node (struct lto_file_decl_data *file_data,
1012 struct lto_input_block *ib,
1013 enum LTO_symtab_tags tag,
1014 vec<symtab_node> nodes)
1016 gcc::pass_manager *passes = g->get_passes ();
1017 tree fn_decl;
1018 struct cgraph_node *node;
1019 struct bitpack_d bp;
1020 unsigned decl_index;
1021 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1022 int clone_ref;
1023 int order;
1024 int i, count;
1026 order = streamer_read_hwi (ib) + order_base;
1027 clone_ref = streamer_read_hwi (ib);
1029 decl_index = streamer_read_uhwi (ib);
1030 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1032 if (clone_ref != LCC_NOT_FOUND)
1034 node = cgraph_clone_node (cgraph (nodes[clone_ref]), fn_decl,
1035 0, CGRAPH_FREQ_BASE, false,
1036 vNULL, false, NULL);
1038 else
1040 /* Declaration of functions can be already merged with a declaration
1041 from other input file. We keep cgraph unmerged until after streaming
1042 of ipa passes is done. Alays forcingly create a fresh node. */
1043 node = cgraph_create_empty_node ();
1044 node->symbol.decl = fn_decl;
1045 symtab_register_node ((symtab_node)node);
1048 node->symbol.order = order;
1049 if (order >= symtab_order)
1050 symtab_order = order + 1;
1052 node->count = streamer_read_gcov_count (ib);
1053 node->count_materialization_scale = streamer_read_hwi (ib);
1055 count = streamer_read_hwi (ib);
1056 node->ipa_transforms_to_apply = vNULL;
1057 for (i = 0; i < count; i++)
1059 struct opt_pass *pass;
1060 int pid = streamer_read_hwi (ib);
1062 gcc_assert (pid < passes->passes_by_id_size);
1063 pass = passes->passes_by_id[pid];
1064 node->ipa_transforms_to_apply.safe_push ((struct ipa_opt_pass_d *) pass);
1067 if (tag == LTO_symtab_analyzed_node)
1068 ref = streamer_read_hwi (ib);
1070 ref2 = streamer_read_hwi (ib);
1072 /* Make sure that we have not read this node before. Nodes that
1073 have already been read will have their tag stored in the 'aux'
1074 field. Since built-in functions can be referenced in multiple
1075 functions, they are expected to be read more than once. */
1076 if (node->symbol.aux && !DECL_BUILT_IN (node->symbol.decl))
1077 internal_error ("bytecode stream: found multiple instances of cgraph "
1078 "node with uid %d", node->uid);
1080 bp = streamer_read_bitpack (ib);
1081 input_overwrite_node (file_data, node, tag, &bp);
1083 /* Store a reference for now, and fix up later to be a pointer. */
1084 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
1086 /* Store a reference for now, and fix up later to be a pointer. */
1087 node->symbol.same_comdat_group = (symtab_node) (intptr_t) ref2;
1089 if (node->thunk.thunk_p)
1091 int type = streamer_read_uhwi (ib);
1092 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1093 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1095 node->thunk.fixed_offset = fixed_offset;
1096 node->thunk.this_adjusting = (type & 2);
1097 node->thunk.virtual_value = virtual_value;
1098 node->thunk.virtual_offset_p = (type & 4);
1100 if (node->symbol.alias && !node->symbol.analyzed && node->symbol.weakref)
1101 node->symbol.alias_target = get_alias_symbol (node->symbol.decl);
1102 node->profile_id = streamer_read_hwi (ib);
1103 return node;
1106 /* Read a node from input_block IB. TAG is the node's tag just read.
1107 Return the node read or overwriten. */
1109 static struct varpool_node *
1110 input_varpool_node (struct lto_file_decl_data *file_data,
1111 struct lto_input_block *ib)
1113 int decl_index;
1114 tree var_decl;
1115 struct varpool_node *node;
1116 struct bitpack_d bp;
1117 int ref = LCC_NOT_FOUND;
1118 int order;
1120 order = streamer_read_hwi (ib) + order_base;
1121 decl_index = streamer_read_uhwi (ib);
1122 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1124 /* Declaration of functions can be already merged with a declaration
1125 from other input file. We keep cgraph unmerged until after streaming
1126 of ipa passes is done. Alays forcingly create a fresh node. */
1127 node = varpool_create_empty_node ();
1128 node->symbol.decl = var_decl;
1129 symtab_register_node ((symtab_node)node);
1131 node->symbol.order = order;
1132 if (order >= symtab_order)
1133 symtab_order = order + 1;
1134 node->symbol.lto_file_data = file_data;
1136 bp = streamer_read_bitpack (ib);
1137 node->symbol.externally_visible = bp_unpack_value (&bp, 1);
1138 node->symbol.force_output = bp_unpack_value (&bp, 1);
1139 node->symbol.forced_by_abi = bp_unpack_value (&bp, 1);
1140 node->symbol.unique_name = bp_unpack_value (&bp, 1);
1141 node->symbol.definition = bp_unpack_value (&bp, 1);
1142 node->symbol.alias = bp_unpack_value (&bp, 1);
1143 node->symbol.weakref = bp_unpack_value (&bp, 1);
1144 node->symbol.analyzed = bp_unpack_value (&bp, 1);
1145 node->symbol.used_from_other_partition = bp_unpack_value (&bp, 1);
1146 node->symbol.in_other_partition = bp_unpack_value (&bp, 1);
1147 if (node->symbol.in_other_partition)
1149 DECL_EXTERNAL (node->symbol.decl) = 1;
1150 TREE_STATIC (node->symbol.decl) = 0;
1152 if (node->symbol.alias && !node->symbol.analyzed && node->symbol.weakref)
1153 node->symbol.alias_target = get_alias_symbol (node->symbol.decl);
1154 ref = streamer_read_hwi (ib);
1155 /* Store a reference for now, and fix up later to be a pointer. */
1156 node->symbol.same_comdat_group = (symtab_node) (intptr_t) ref;
1157 node->symbol.resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1158 LDPR_NUM_KNOWN);
1160 return node;
1163 /* Read a node from input_block IB. TAG is the node's tag just read.
1164 Return the node read or overwriten. */
1166 static void
1167 input_ref (struct lto_input_block *ib,
1168 symtab_node referring_node,
1169 vec<symtab_node> nodes)
1171 symtab_node node = NULL;
1172 struct bitpack_d bp;
1173 enum ipa_ref_use use;
1174 bool speculative;
1175 struct ipa_ref *ref;
1177 bp = streamer_read_bitpack (ib);
1178 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1179 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1180 node = nodes[streamer_read_hwi (ib)];
1181 ref = ipa_record_reference (referring_node, node, use, NULL);
1182 ref->speculative = speculative;
1183 if (is_a <cgraph_node> (referring_node))
1184 ref->lto_stmt_uid = streamer_read_hwi (ib);
1187 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1188 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1189 edge being read is indirect (in the sense that it has
1190 indirect_unknown_callee set). */
1192 static void
1193 input_edge (struct lto_input_block *ib, vec<symtab_node> nodes,
1194 bool indirect)
1196 struct cgraph_node *caller, *callee;
1197 struct cgraph_edge *edge;
1198 unsigned int stmt_id;
1199 gcov_type count;
1200 int freq;
1201 cgraph_inline_failed_t inline_failed;
1202 struct bitpack_d bp;
1203 int ecf_flags = 0;
1205 caller = cgraph (nodes[streamer_read_hwi (ib)]);
1206 if (caller == NULL || caller->symbol.decl == NULL_TREE)
1207 internal_error ("bytecode stream: no caller found while reading edge");
1209 if (!indirect)
1211 callee = cgraph (nodes[streamer_read_hwi (ib)]);
1212 if (callee == NULL || callee->symbol.decl == NULL_TREE)
1213 internal_error ("bytecode stream: no callee found while reading edge");
1215 else
1216 callee = NULL;
1218 count = streamer_read_gcov_count (ib);
1220 bp = streamer_read_bitpack (ib);
1221 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_enum, CIF_N_REASONS);
1222 stmt_id = bp_unpack_var_len_unsigned (&bp);
1223 freq = (int) bp_unpack_var_len_unsigned (&bp);
1225 if (indirect)
1226 edge = cgraph_create_indirect_edge (caller, NULL, 0, count, freq);
1227 else
1228 edge = cgraph_create_edge (caller, callee, NULL, count, freq);
1230 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1231 edge->speculative = bp_unpack_value (&bp, 1);
1232 edge->lto_stmt_uid = stmt_id;
1233 edge->inline_failed = inline_failed;
1234 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1235 edge->can_throw_external = bp_unpack_value (&bp, 1);
1236 if (indirect)
1238 if (bp_unpack_value (&bp, 1))
1239 ecf_flags |= ECF_CONST;
1240 if (bp_unpack_value (&bp, 1))
1241 ecf_flags |= ECF_PURE;
1242 if (bp_unpack_value (&bp, 1))
1243 ecf_flags |= ECF_NORETURN;
1244 if (bp_unpack_value (&bp, 1))
1245 ecf_flags |= ECF_MALLOC;
1246 if (bp_unpack_value (&bp, 1))
1247 ecf_flags |= ECF_NOTHROW;
1248 if (bp_unpack_value (&bp, 1))
1249 ecf_flags |= ECF_RETURNS_TWICE;
1250 edge->indirect_info->ecf_flags = ecf_flags;
1251 edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1252 if (edge->indirect_info->common_target_id)
1253 edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1258 /* Read a cgraph from IB using the info in FILE_DATA. */
1260 static vec<symtab_node>
1261 input_cgraph_1 (struct lto_file_decl_data *file_data,
1262 struct lto_input_block *ib)
1264 enum LTO_symtab_tags tag;
1265 vec<symtab_node> nodes = vNULL;
1266 symtab_node node;
1267 unsigned i;
1269 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1270 order_base = symtab_order;
1271 while (tag)
1273 if (tag == LTO_symtab_edge)
1274 input_edge (ib, nodes, false);
1275 else if (tag == LTO_symtab_indirect_edge)
1276 input_edge (ib, nodes, true);
1277 else if (tag == LTO_symtab_variable)
1279 node = (symtab_node)input_varpool_node (file_data, ib);
1280 nodes.safe_push (node);
1281 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1283 else
1285 node = (symtab_node)input_node (file_data, ib, tag, nodes);
1286 if (node == NULL || node->symbol.decl == NULL_TREE)
1287 internal_error ("bytecode stream: found empty cgraph node");
1288 nodes.safe_push (node);
1289 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1292 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1295 lto_input_toplevel_asms (file_data, order_base);
1297 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1298 #ifdef ENABLE_CHECKING
1299 FOR_EACH_VEC_ELT (nodes, i, node)
1300 gcc_assert (node->symbol.aux || !is_a <cgraph_node> (node));
1301 #endif
1302 FOR_EACH_VEC_ELT (nodes, i, node)
1304 int ref;
1305 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
1307 ref = (int) (intptr_t) cnode->global.inlined_to;
1309 /* We share declaration of builtins, so we may read same node twice. */
1310 if (!node->symbol.aux)
1311 continue;
1312 node->symbol.aux = NULL;
1314 /* Fixup inlined_to from reference to pointer. */
1315 if (ref != LCC_NOT_FOUND)
1316 cgraph (node)->global.inlined_to = cgraph (nodes[ref]);
1317 else
1318 cnode->global.inlined_to = NULL;
1321 ref = (int) (intptr_t) node->symbol.same_comdat_group;
1323 /* Fixup same_comdat_group from reference to pointer. */
1324 if (ref != LCC_NOT_FOUND)
1325 node->symbol.same_comdat_group = nodes[ref];
1326 else
1327 node->symbol.same_comdat_group = NULL;
1329 FOR_EACH_VEC_ELT (nodes, i, node)
1330 node->symbol.aux = is_a <cgraph_node> (node) ? (void *)1 : NULL;
1331 return nodes;
1334 /* Input ipa_refs. */
1336 static void
1337 input_refs (struct lto_input_block *ib,
1338 vec<symtab_node> nodes)
1340 int count;
1341 int idx;
1342 while (true)
1344 symtab_node node;
1345 count = streamer_read_uhwi (ib);
1346 if (!count)
1347 break;
1348 idx = streamer_read_uhwi (ib);
1349 node = nodes[idx];
1350 while (count)
1352 input_ref (ib, node, nodes);
1353 count--;
1359 static struct gcov_ctr_summary lto_gcov_summary;
1361 /* Input profile_info from IB. */
1362 static void
1363 input_profile_summary (struct lto_input_block *ib,
1364 struct lto_file_decl_data *file_data)
1366 unsigned h_ix;
1367 struct bitpack_d bp;
1368 unsigned int runs = streamer_read_uhwi (ib);
1369 if (runs)
1371 file_data->profile_info.runs = runs;
1372 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1373 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1375 memset (file_data->profile_info.histogram, 0,
1376 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1377 /* Input the bitpack of non-zero histogram indices. */
1378 bp = streamer_read_bitpack (ib);
1379 /* Read in and unpack the full bitpack, flagging non-zero
1380 histogram entries by setting the num_counters non-zero. */
1381 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1383 file_data->profile_info.histogram[h_ix].num_counters
1384 = bp_unpack_value (&bp, 1);
1386 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1388 if (!file_data->profile_info.histogram[h_ix].num_counters)
1389 continue;
1391 file_data->profile_info.histogram[h_ix].num_counters
1392 = streamer_read_gcov_count (ib);
1393 file_data->profile_info.histogram[h_ix].min_value
1394 = streamer_read_gcov_count (ib);
1395 file_data->profile_info.histogram[h_ix].cum_value
1396 = streamer_read_gcov_count (ib);
1398 /* IPA-profile computes hot bb threshold based on cumulated
1399 whole program profile. We need to stream it down to ltrans. */
1400 if (flag_ltrans)
1401 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1406 /* Rescale profile summaries to the same number of runs in the whole unit. */
1408 static void
1409 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1411 struct lto_file_decl_data *file_data;
1412 unsigned int j, h_ix;
1413 gcov_unsigned_t max_runs = 0;
1414 struct cgraph_node *node;
1415 struct cgraph_edge *edge;
1416 gcov_type saved_sum_all = 0;
1417 gcov_ctr_summary *saved_profile_info = 0;
1418 int saved_scale = 0;
1420 /* Find unit with maximal number of runs. If we ever get serious about
1421 roundoff errors, we might also consider computing smallest common
1422 multiply. */
1423 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1424 if (max_runs < file_data->profile_info.runs)
1425 max_runs = file_data->profile_info.runs;
1427 if (!max_runs)
1428 return;
1430 /* Simple overflow check. We probably don't need to support that many train
1431 runs. Such a large value probably imply data corruption anyway. */
1432 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1434 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1435 INT_MAX / REG_BR_PROB_BASE);
1436 return;
1439 profile_info = &lto_gcov_summary;
1440 lto_gcov_summary.runs = max_runs;
1441 lto_gcov_summary.sum_max = 0;
1442 memset (lto_gcov_summary.histogram, 0,
1443 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1445 /* Rescale all units to the maximal number of runs.
1446 sum_max can not be easily merged, as we have no idea what files come from
1447 the same run. We do not use the info anyway, so leave it 0. */
1448 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1449 if (file_data->profile_info.runs)
1451 int scale = GCOV_COMPUTE_SCALE (max_runs,
1452 file_data->profile_info.runs);
1453 lto_gcov_summary.sum_max
1454 = MAX (lto_gcov_summary.sum_max,
1455 apply_scale (file_data->profile_info.sum_max, scale));
1456 lto_gcov_summary.sum_all
1457 = MAX (lto_gcov_summary.sum_all,
1458 apply_scale (file_data->profile_info.sum_all, scale));
1459 /* Save a pointer to the profile_info with the largest
1460 scaled sum_all and the scale for use in merging the
1461 histogram. */
1462 if (!saved_profile_info
1463 || lto_gcov_summary.sum_all > saved_sum_all)
1465 saved_profile_info = &file_data->profile_info;
1466 saved_sum_all = lto_gcov_summary.sum_all;
1467 saved_scale = scale;
1471 gcc_assert (saved_profile_info);
1473 /* Scale up the histogram from the profile that had the largest
1474 scaled sum_all above. */
1475 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1477 /* Scale up the min value as we did the corresponding sum_all
1478 above. Use that to find the new histogram index. */
1479 gcov_type scaled_min
1480 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1481 saved_scale);
1482 /* The new index may be shared with another scaled histogram entry,
1483 so we need to account for a non-zero histogram entry at new_ix. */
1484 unsigned new_ix = gcov_histo_index (scaled_min);
1485 lto_gcov_summary.histogram[new_ix].min_value
1486 = (lto_gcov_summary.histogram[new_ix].num_counters
1487 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1488 : scaled_min);
1489 /* Some of the scaled counter values would ostensibly need to be placed
1490 into different (larger) histogram buckets, but we keep things simple
1491 here and place the scaled cumulative counter value in the bucket
1492 corresponding to the scaled minimum counter value. */
1493 lto_gcov_summary.histogram[new_ix].cum_value
1494 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1495 saved_scale);
1496 lto_gcov_summary.histogram[new_ix].num_counters
1497 += saved_profile_info->histogram[h_ix].num_counters;
1500 /* Watch roundoff errors. */
1501 if (lto_gcov_summary.sum_max < max_runs)
1502 lto_gcov_summary.sum_max = max_runs;
1504 /* If merging already happent at WPA time, we are done. */
1505 if (flag_ltrans)
1506 return;
1508 /* Now compute count_materialization_scale of each node.
1509 During LTRANS we already have values of count_materialization_scale
1510 computed, so just update them. */
1511 FOR_EACH_FUNCTION (node)
1512 if (node->symbol.lto_file_data
1513 && node->symbol.lto_file_data->profile_info.runs)
1515 int scale;
1517 scale = RDIV (node->count_materialization_scale * max_runs,
1518 node->symbol.lto_file_data->profile_info.runs);
1519 node->count_materialization_scale = scale;
1520 if (scale < 0)
1521 fatal_error ("Profile information in %s corrupted",
1522 file_data->file_name);
1524 if (scale == REG_BR_PROB_BASE)
1525 continue;
1526 for (edge = node->callees; edge; edge = edge->next_callee)
1527 edge->count = apply_scale (edge->count, scale);
1528 node->count = apply_scale (node->count, scale);
1532 /* Input and merge the symtab from each of the .o files passed to
1533 lto1. */
1535 void
1536 input_symtab (void)
1538 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1539 struct lto_file_decl_data *file_data;
1540 unsigned int j = 0;
1541 struct cgraph_node *node;
1543 while ((file_data = file_data_vec[j++]))
1545 const char *data;
1546 size_t len;
1547 struct lto_input_block *ib;
1548 vec<symtab_node> nodes;
1550 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1551 &data, &len);
1552 if (!ib)
1553 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1554 input_profile_summary (ib, file_data);
1555 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1556 nodes = input_cgraph_1 (file_data, ib);
1557 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1558 ib, data, len);
1560 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1561 &data, &len);
1562 if (!ib)
1563 fatal_error("cannot find LTO section refs in %s", file_data->file_name);
1564 input_refs (ib, nodes);
1565 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1566 ib, data, len);
1567 if (flag_ltrans)
1568 input_cgraph_opt_summary (nodes);
1569 nodes.release ();
1572 merge_profile_summaries (file_data_vec);
1573 get_working_sets ();
1576 /* Clear out the aux field that was used to store enough state to
1577 tell which nodes should be overwritten. */
1578 FOR_EACH_FUNCTION (node)
1580 /* Some nodes may have been created by cgraph_node. This
1581 happens when the callgraph contains nested functions. If the
1582 node for the parent function was never emitted to the gimple
1583 file, cgraph_node will create a node for it when setting the
1584 context of the nested function. */
1585 if (node->symbol.lto_file_data)
1586 node->symbol.aux = NULL;
1590 /* True when we need optimization summary for NODE. */
1592 static int
1593 output_cgraph_opt_summary_p (struct cgraph_node *node)
1595 return (node->clone_of
1596 && (node->clone.tree_map
1597 || node->clone.args_to_skip
1598 || node->clone.combined_args_to_skip));
1601 /* Output optimization summary for EDGE to OB. */
1602 static void
1603 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1604 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1608 /* Output optimization summary for NODE to OB. */
1610 static void
1611 output_node_opt_summary (struct output_block *ob,
1612 struct cgraph_node *node,
1613 lto_symtab_encoder_t encoder)
1615 unsigned int index;
1616 bitmap_iterator bi;
1617 struct ipa_replace_map *map;
1618 struct bitpack_d bp;
1619 int i;
1620 struct cgraph_edge *e;
1622 if (node->clone.args_to_skip)
1624 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1625 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1626 streamer_write_uhwi (ob, index);
1628 else
1629 streamer_write_uhwi (ob, 0);
1630 if (node->clone.combined_args_to_skip)
1632 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1633 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1634 streamer_write_uhwi (ob, index);
1636 else
1637 streamer_write_uhwi (ob, 0);
1638 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1639 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1641 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1642 mechanism to store function local declarations into summaries. */
1643 gcc_assert (!map->old_tree);
1644 streamer_write_uhwi (ob, map->parm_num);
1645 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1646 stream_write_tree (ob, map->new_tree, true);
1647 bp = bitpack_create (ob->main_stream);
1648 bp_pack_value (&bp, map->replace_p, 1);
1649 bp_pack_value (&bp, map->ref_p, 1);
1650 streamer_write_bitpack (&bp);
1653 if (lto_symtab_encoder_in_partition_p (encoder, (symtab_node) node))
1655 for (e = node->callees; e; e = e->next_callee)
1656 output_edge_opt_summary (ob, e);
1657 for (e = node->indirect_calls; e; e = e->next_callee)
1658 output_edge_opt_summary (ob, e);
1662 /* Output optimization summaries stored in callgraph.
1663 At the moment it is the clone info structure. */
1665 static void
1666 output_cgraph_opt_summary (void)
1668 int i, n_nodes;
1669 lto_symtab_encoder_t encoder;
1670 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1671 unsigned count = 0;
1673 ob->cgraph_node = NULL;
1674 encoder = ob->decl_state->symtab_node_encoder;
1675 n_nodes = lto_symtab_encoder_size (encoder);
1676 for (i = 0; i < n_nodes; i++)
1678 symtab_node node = lto_symtab_encoder_deref (encoder, i);
1679 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1680 if (cnode && output_cgraph_opt_summary_p (cnode))
1681 count++;
1683 streamer_write_uhwi (ob, count);
1684 for (i = 0; i < n_nodes; i++)
1686 symtab_node node = lto_symtab_encoder_deref (encoder, i);
1687 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1688 if (cnode && output_cgraph_opt_summary_p (cnode))
1690 streamer_write_uhwi (ob, i);
1691 output_node_opt_summary (ob, cnode, encoder);
1694 produce_asm (ob, NULL);
1695 destroy_output_block (ob);
1698 /* Input optimisation summary of EDGE. */
1700 static void
1701 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
1702 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
1706 /* Input optimisation summary of NODE. */
1708 static void
1709 input_node_opt_summary (struct cgraph_node *node,
1710 struct lto_input_block *ib_main,
1711 struct data_in *data_in)
1713 int i;
1714 int count;
1715 int bit;
1716 struct bitpack_d bp;
1717 struct cgraph_edge *e;
1719 count = streamer_read_uhwi (ib_main);
1720 if (count)
1721 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1722 for (i = 0; i < count; i++)
1724 bit = streamer_read_uhwi (ib_main);
1725 bitmap_set_bit (node->clone.args_to_skip, bit);
1727 count = streamer_read_uhwi (ib_main);
1728 if (count)
1729 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1730 for (i = 0; i < count; i++)
1732 bit = streamer_read_uhwi (ib_main);
1733 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1735 count = streamer_read_uhwi (ib_main);
1736 for (i = 0; i < count; i++)
1738 struct ipa_replace_map *map = ggc_alloc_ipa_replace_map ();
1740 vec_safe_push (node->clone.tree_map, map);
1741 map->parm_num = streamer_read_uhwi (ib_main);
1742 map->old_tree = NULL;
1743 map->new_tree = stream_read_tree (ib_main, data_in);
1744 bp = streamer_read_bitpack (ib_main);
1745 map->replace_p = bp_unpack_value (&bp, 1);
1746 map->ref_p = bp_unpack_value (&bp, 1);
1748 for (e = node->callees; e; e = e->next_callee)
1749 input_edge_opt_summary (e, ib_main);
1750 for (e = node->indirect_calls; e; e = e->next_callee)
1751 input_edge_opt_summary (e, ib_main);
1754 /* Read section in file FILE_DATA of length LEN with data DATA. */
1756 static void
1757 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1758 const char *data, size_t len,
1759 vec<symtab_node> nodes)
1761 const struct lto_function_header *header =
1762 (const struct lto_function_header *) data;
1763 const int cfg_offset = sizeof (struct lto_function_header);
1764 const int main_offset = cfg_offset + header->cfg_size;
1765 const int string_offset = main_offset + header->main_size;
1766 struct data_in *data_in;
1767 struct lto_input_block ib_main;
1768 unsigned int i;
1769 unsigned int count;
1771 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1772 header->main_size);
1774 data_in =
1775 lto_data_in_create (file_data, (const char *) data + string_offset,
1776 header->string_size, vNULL);
1777 count = streamer_read_uhwi (&ib_main);
1779 for (i = 0; i < count; i++)
1781 int ref = streamer_read_uhwi (&ib_main);
1782 input_node_opt_summary (cgraph (nodes[ref]),
1783 &ib_main, data_in);
1785 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1786 len);
1787 lto_data_in_delete (data_in);
1790 /* Input optimization summary of cgraph. */
1792 static void
1793 input_cgraph_opt_summary (vec<symtab_node> nodes)
1795 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1796 struct lto_file_decl_data *file_data;
1797 unsigned int j = 0;
1799 while ((file_data = file_data_vec[j++]))
1801 size_t len;
1802 const char *data =
1803 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1804 &len);
1806 if (data)
1807 input_cgraph_opt_section (file_data, data, len, nodes);