2013-08-07 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / lto-cgraph.c
blob51dc705b28fc4b8c6a1f6fa4b6c552d51fce0f0d
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"
53 static void output_cgraph_opt_summary (void);
54 static void input_cgraph_opt_summary (vec<symtab_node> nodes);
56 /* Number of LDPR values known to GCC. */
57 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
59 /* All node orders are ofsetted by ORDER_BASE. */
60 static int order_base;
62 /* Cgraph streaming is organized as set of record whose type
63 is indicated by a tag. */
64 enum LTO_symtab_tags
66 /* Must leave 0 for the stopper. */
68 /* Cgraph node without body available. */
69 LTO_symtab_unavail_node = 1,
70 /* Cgraph node with function body. */
71 LTO_symtab_analyzed_node,
72 /* Cgraph edges. */
73 LTO_symtab_edge,
74 LTO_symtab_indirect_edge,
75 LTO_symtab_variable,
76 LTO_symtab_last_tag
79 /* Create a new symtab encoder.
80 if FOR_INPUT, the encoder allocate only datastructures needed
81 to read the symtab. */
83 lto_symtab_encoder_t
84 lto_symtab_encoder_new (bool for_input)
86 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
88 if (!for_input)
89 encoder->map = pointer_map_create ();
90 encoder->nodes.create (0);
91 return encoder;
95 /* Delete ENCODER and its components. */
97 void
98 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
100 encoder->nodes.release ();
101 if (encoder->map)
102 pointer_map_destroy (encoder->map);
103 free (encoder);
107 /* Return the existing reference number of NODE in the symtab encoder in
108 output block OB. Assign a new reference if this is the first time
109 NODE is encoded. */
112 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
113 symtab_node node)
115 int ref;
116 void **slot;
118 if (!encoder->map)
120 lto_encoder_entry entry = {node, false, false, false};
122 ref = encoder->nodes.length ();
123 encoder->nodes.safe_push (entry);
124 return ref;
127 slot = pointer_map_contains (encoder->map, node);
128 if (!slot || !*slot)
130 lto_encoder_entry entry = {node, false, false, false};
131 ref = encoder->nodes.length ();
132 if (!slot)
133 slot = pointer_map_insert (encoder->map, node);
134 *slot = (void *) (intptr_t) (ref + 1);
135 encoder->nodes.safe_push (entry);
137 else
138 ref = (size_t) *slot - 1;
140 return ref;
143 /* Remove NODE from encoder. */
145 bool
146 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
147 symtab_node node)
149 void **slot, **last_slot;
150 int index;
151 lto_encoder_entry last_node;
153 slot = pointer_map_contains (encoder->map, node);
154 if (slot == NULL || !*slot)
155 return false;
157 index = (size_t) *slot - 1;
158 gcc_checking_assert (encoder->nodes[index].node == node);
160 /* Remove from vector. We do this by swapping node with the last element
161 of the vector. */
162 last_node = encoder->nodes.pop ();
163 if (last_node.node != node)
165 last_slot = pointer_map_contains (encoder->map, last_node.node);
166 gcc_checking_assert (last_slot && *last_slot);
167 *last_slot = (void *)(size_t) (index + 1);
169 /* Move the last element to the original spot of NODE. */
170 encoder->nodes[index] = last_node;
173 /* Remove element from hash table. */
174 *slot = NULL;
175 return true;
179 /* Return TRUE if we should encode initializer of NODE (if any). */
181 bool
182 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
183 struct cgraph_node *node)
185 int index = lto_symtab_encoder_lookup (encoder, (symtab_node)node);
186 return encoder->nodes[index].body;
189 /* Return TRUE if we should encode body of NODE (if any). */
191 static void
192 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
193 struct cgraph_node *node)
195 int index = lto_symtab_encoder_encode (encoder, (symtab_node)node);
196 gcc_checking_assert (encoder->nodes[index].node == (symtab_node)node);
197 encoder->nodes[index].body = true;
200 /* Return TRUE if we should encode initializer of NODE (if any). */
202 bool
203 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
204 struct varpool_node *node)
206 int index = lto_symtab_encoder_lookup (encoder, (symtab_node)node);
207 if (index == LCC_NOT_FOUND)
208 return false;
209 return encoder->nodes[index].initializer;
212 /* Return TRUE if we should encode initializer of NODE (if any). */
214 static void
215 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
216 struct varpool_node *node)
218 int index = lto_symtab_encoder_lookup (encoder, (symtab_node)node);
219 encoder->nodes[index].initializer = true;
222 /* Return TRUE if we should encode initializer of NODE (if any). */
224 bool
225 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
226 symtab_node node)
228 int index = lto_symtab_encoder_lookup (encoder, (symtab_node)node);
229 if (index == LCC_NOT_FOUND)
230 return false;
231 return encoder->nodes[index].in_partition;
234 /* Return TRUE if we should encode body of NODE (if any). */
236 void
237 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
238 symtab_node node)
240 int index = lto_symtab_encoder_encode (encoder, (symtab_node)node);
241 encoder->nodes[index].in_partition = true;
244 /* Output the cgraph EDGE to OB using ENCODER. */
246 static void
247 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
248 lto_symtab_encoder_t encoder)
250 unsigned int uid;
251 intptr_t ref;
252 struct bitpack_d bp;
254 if (edge->indirect_unknown_callee)
255 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
256 LTO_symtab_indirect_edge);
257 else
258 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
259 LTO_symtab_edge);
261 ref = lto_symtab_encoder_lookup (encoder, (symtab_node)edge->caller);
262 gcc_assert (ref != LCC_NOT_FOUND);
263 streamer_write_hwi_stream (ob->main_stream, ref);
265 if (!edge->indirect_unknown_callee)
267 ref = lto_symtab_encoder_lookup (encoder, (symtab_node)edge->callee);
268 gcc_assert (ref != LCC_NOT_FOUND);
269 streamer_write_hwi_stream (ob->main_stream, ref);
272 streamer_write_gcov_count_stream (ob->main_stream, edge->count);
274 bp = bitpack_create (ob->main_stream);
275 uid = (!gimple_has_body_p (edge->caller->symbol.decl)
276 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt));
277 bp_pack_enum (&bp, cgraph_inline_failed_enum,
278 CIF_N_REASONS, edge->inline_failed);
279 bp_pack_var_len_unsigned (&bp, uid);
280 bp_pack_var_len_unsigned (&bp, edge->frequency);
281 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
282 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
283 bp_pack_value (&bp, edge->can_throw_external, 1);
284 if (edge->indirect_unknown_callee)
286 int flags = edge->indirect_info->ecf_flags;
287 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
288 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
289 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
290 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
291 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
292 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
293 /* Flags that should not appear on indirect calls. */
294 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
295 | ECF_MAY_BE_ALLOCA
296 | ECF_SIBCALL
297 | ECF_LEAF
298 | ECF_NOVOPS)));
300 streamer_write_bitpack (&bp);
303 /* Return if LIST contain references from other partitions. */
305 bool
306 referenced_from_other_partition_p (struct ipa_ref_list *list, lto_symtab_encoder_t encoder)
308 int i;
309 struct ipa_ref *ref;
310 for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
312 if (ref->referring->symbol.in_other_partition
313 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
314 return true;
316 return false;
319 /* Return true when node is reachable from other partition. */
321 bool
322 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
324 struct cgraph_edge *e;
325 if (!node->symbol.definition)
326 return false;
327 if (node->global.inlined_to)
328 return false;
329 for (e = node->callers; e; e = e->next_caller)
330 if (e->caller->symbol.in_other_partition
331 || !lto_symtab_encoder_in_partition_p (encoder, (symtab_node)e->caller))
332 return true;
333 return false;
336 /* Return if LIST contain references from other partitions. */
338 bool
339 referenced_from_this_partition_p (struct ipa_ref_list *list,
340 lto_symtab_encoder_t encoder)
342 int i;
343 struct ipa_ref *ref;
344 for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
345 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
346 return true;
347 return false;
350 /* Return true when node is reachable from other partition. */
352 bool
353 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
355 struct cgraph_edge *e;
356 for (e = node->callers; e; e = e->next_caller)
357 if (lto_symtab_encoder_in_partition_p (encoder, (symtab_node)e->caller))
358 return true;
359 return false;
362 /* Output the cgraph NODE to OB. ENCODER is used to find the
363 reference number of NODE->inlined_to. SET is the set of nodes we
364 are writing to the current file. If NODE is not in SET, then NODE
365 is a boundary of a cgraph_node_set and we pretend NODE just has a
366 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
367 that have had their callgraph node written so far. This is used to
368 determine if NODE is a clone of a previously written node. */
370 static void
371 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
372 lto_symtab_encoder_t encoder)
374 unsigned int tag;
375 struct bitpack_d bp;
376 bool boundary_p;
377 intptr_t ref;
378 bool in_other_partition = false;
379 struct cgraph_node *clone_of, *ultimate_clone_of;
380 struct ipa_opt_pass_d *pass;
381 int i;
382 bool alias_p;
384 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, (symtab_node)node);
386 if (node->symbol.analyzed && !boundary_p)
387 tag = LTO_symtab_analyzed_node;
388 else
389 tag = LTO_symtab_unavail_node;
391 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
392 tag);
393 streamer_write_hwi_stream (ob->main_stream, node->symbol.order);
395 /* In WPA mode, we only output part of the call-graph. Also, we
396 fake cgraph node attributes. There are two cases that we care.
398 Boundary nodes: There are nodes that are not part of SET but are
399 called from within SET. We artificially make them look like
400 externally visible nodes with no function body.
402 Cherry-picked nodes: These are nodes we pulled from other
403 translation units into SET during IPA-inlining. We make them as
404 local static nodes to prevent clashes with other local statics. */
405 if (boundary_p && node->symbol.analyzed && !DECL_EXTERNAL (node->symbol.decl))
407 /* Inline clones can not be part of boundary.
408 gcc_assert (!node->global.inlined_to);
410 FIXME: At the moment they can be, when partition contains an inline
411 clone that is clone of inline clone from outside partition. We can
412 reshape the clone tree and make other tree to be the root, but it
413 needs a bit extra work and will be promplty done by cgraph_remove_node
414 after reading back. */
415 in_other_partition = 1;
418 clone_of = node->clone_of;
419 while (clone_of
420 && (ref = lto_symtab_encoder_lookup (encoder, (symtab_node)clone_of)) == LCC_NOT_FOUND)
421 if (clone_of->prev_sibling_clone)
422 clone_of = clone_of->prev_sibling_clone;
423 else
424 clone_of = clone_of->clone_of;
426 /* See if body of the master function is output. If not, we are seeing only
427 an declaration and we do not need to pass down clone tree. */
428 ultimate_clone_of = clone_of;
429 while (ultimate_clone_of && ultimate_clone_of->clone_of)
430 ultimate_clone_of = ultimate_clone_of->clone_of;
432 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
433 clone_of = NULL;
435 if (tag == LTO_symtab_analyzed_node)
436 gcc_assert (clone_of || !node->clone_of);
437 if (!clone_of)
438 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
439 else
440 streamer_write_hwi_stream (ob->main_stream, ref);
443 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->symbol.decl);
444 streamer_write_gcov_count_stream (ob->main_stream, node->count);
445 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
447 streamer_write_hwi_stream (ob->main_stream,
448 node->ipa_transforms_to_apply.length ());
449 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
450 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
452 if (tag == LTO_symtab_analyzed_node)
454 if (node->global.inlined_to)
456 ref = lto_symtab_encoder_lookup (encoder, (symtab_node)node->global.inlined_to);
457 gcc_assert (ref != LCC_NOT_FOUND);
459 else
460 ref = LCC_NOT_FOUND;
462 streamer_write_hwi_stream (ob->main_stream, ref);
465 if (node->symbol.same_comdat_group && !boundary_p)
467 ref = lto_symtab_encoder_lookup (encoder,
468 node->symbol.same_comdat_group);
469 gcc_assert (ref != LCC_NOT_FOUND);
471 else
472 ref = LCC_NOT_FOUND;
473 streamer_write_hwi_stream (ob->main_stream, ref);
475 bp = bitpack_create (ob->main_stream);
476 bp_pack_value (&bp, node->local.local, 1);
477 bp_pack_value (&bp, node->symbol.externally_visible, 1);
478 bp_pack_value (&bp, node->symbol.definition, 1);
479 bp_pack_value (&bp, node->local.versionable, 1);
480 bp_pack_value (&bp, node->local.can_change_signature, 1);
481 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
482 bp_pack_value (&bp, node->symbol.force_output, 1);
483 bp_pack_value (&bp, node->symbol.forced_by_abi, 1);
484 bp_pack_value (&bp, node->symbol.unique_name, 1);
485 bp_pack_value (&bp, node->symbol.address_taken, 1);
486 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
487 && !DECL_EXTERNAL (node->symbol.decl)
488 && !DECL_COMDAT (node->symbol.decl)
489 && (reachable_from_other_partition_p (node, encoder)
490 || referenced_from_other_partition_p (&node->symbol.ref_list,
491 encoder)), 1);
492 bp_pack_value (&bp, node->lowered, 1);
493 bp_pack_value (&bp, in_other_partition, 1);
494 /* Real aliases in a boundary become non-aliases. However we still stream
495 alias info on weakrefs.
496 TODO: We lose a bit of information here - when we know that variable is
497 defined in other unit, we may use the info on aliases to resolve
498 symbol1 != symbol2 type tests that we can do only for locally defined objects
499 otherwise. */
500 alias_p = node->symbol.alias && (!boundary_p || node->symbol.weakref);
501 bp_pack_value (&bp, alias_p, 1);
502 bp_pack_value (&bp, node->symbol.weakref, 1);
503 bp_pack_value (&bp, node->frequency, 2);
504 bp_pack_value (&bp, node->only_called_at_startup, 1);
505 bp_pack_value (&bp, node->only_called_at_exit, 1);
506 bp_pack_value (&bp, node->tm_clone, 1);
507 bp_pack_value (&bp, node->thunk.thunk_p && !boundary_p, 1);
508 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
509 LDPR_NUM_KNOWN, node->symbol.resolution);
510 streamer_write_bitpack (&bp);
512 if (node->thunk.thunk_p && !boundary_p)
514 streamer_write_uhwi_stream
515 (ob->main_stream,
516 1 + (node->thunk.this_adjusting != 0) * 2
517 + (node->thunk.virtual_offset_p != 0) * 4);
518 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
519 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
523 /* Output the varpool NODE to OB.
524 If NODE is not in SET, then NODE is a boundary. */
526 static void
527 lto_output_varpool_node (struct lto_simple_output_block *ob, struct varpool_node *node,
528 lto_symtab_encoder_t encoder)
530 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, (symtab_node)node);
531 struct bitpack_d bp;
532 int ref;
533 bool alias_p;
535 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
536 LTO_symtab_variable);
537 streamer_write_hwi_stream (ob->main_stream, node->symbol.order);
538 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->symbol.decl);
539 bp = bitpack_create (ob->main_stream);
540 bp_pack_value (&bp, node->symbol.externally_visible, 1);
541 bp_pack_value (&bp, node->symbol.force_output, 1);
542 bp_pack_value (&bp, node->symbol.forced_by_abi, 1);
543 bp_pack_value (&bp, node->symbol.unique_name, 1);
544 bp_pack_value (&bp, node->symbol.definition, 1);
545 alias_p = node->symbol.alias && (!boundary_p || node->symbol.weakref);
546 bp_pack_value (&bp, alias_p, 1);
547 bp_pack_value (&bp, node->symbol.weakref, 1);
548 bp_pack_value (&bp, node->symbol.analyzed && !boundary_p, 1);
549 gcc_assert (node->symbol.definition || !node->symbol.analyzed);
550 /* Constant pool initializers can be de-unified into individual ltrans units.
551 FIXME: Alternatively at -Os we may want to avoid generating for them the local
552 labels and share them across LTRANS partitions. */
553 if (DECL_IN_CONSTANT_POOL (node->symbol.decl)
554 && !DECL_EXTERNAL (node->symbol.decl)
555 && !DECL_COMDAT (node->symbol.decl))
557 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
558 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
560 else
562 bp_pack_value (&bp, node->symbol.definition
563 && referenced_from_other_partition_p (&node->symbol.ref_list,
564 encoder), 1);
565 bp_pack_value (&bp, node->symbol.analyzed
566 && boundary_p && !DECL_EXTERNAL (node->symbol.decl), 1);
567 /* in_other_partition. */
569 streamer_write_bitpack (&bp);
570 if (node->symbol.same_comdat_group && !boundary_p)
572 ref = lto_symtab_encoder_lookup (encoder,
573 node->symbol.same_comdat_group);
574 gcc_assert (ref != LCC_NOT_FOUND);
576 else
577 ref = LCC_NOT_FOUND;
578 streamer_write_hwi_stream (ob->main_stream, ref);
579 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
580 LDPR_NUM_KNOWN, node->symbol.resolution);
583 /* Output the varpool NODE to OB.
584 If NODE is not in SET, then NODE is a boundary. */
586 static void
587 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
588 lto_symtab_encoder_t encoder)
590 struct bitpack_d bp;
591 int nref;
593 bp = bitpack_create (ob->main_stream);
594 bp_pack_value (&bp, ref->use, 2);
595 streamer_write_bitpack (&bp);
596 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
597 gcc_assert (nref != LCC_NOT_FOUND);
598 streamer_write_hwi_stream (ob->main_stream, nref);
601 /* Stream out profile_summary to OB. */
603 static void
604 output_profile_summary (struct lto_simple_output_block *ob)
606 unsigned h_ix;
607 struct bitpack_d bp;
609 if (profile_info)
611 /* We do not output num and run_max, they are not used by
612 GCC profile feedback and they are difficult to merge from multiple
613 units. */
614 gcc_assert (profile_info->runs);
615 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
616 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
618 /* sum_all is needed for computing the working set with the
619 histogram. */
620 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
622 /* Create and output a bitpack of non-zero histogram entries indices. */
623 bp = bitpack_create (ob->main_stream);
624 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
625 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
626 streamer_write_bitpack (&bp);
627 /* Now stream out only those non-zero entries. */
628 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
630 if (!profile_info->histogram[h_ix].num_counters)
631 continue;
632 streamer_write_gcov_count_stream (ob->main_stream,
633 profile_info->histogram[h_ix].num_counters);
634 streamer_write_gcov_count_stream (ob->main_stream,
635 profile_info->histogram[h_ix].min_value);
636 streamer_write_gcov_count_stream (ob->main_stream,
637 profile_info->histogram[h_ix].cum_value);
639 /* IPA-profile computes hot bb threshold based on cumulated
640 whole program profile. We need to stream it down to ltrans. */
641 if (flag_wpa)
642 streamer_write_gcov_count_stream (ob->main_stream,
643 get_hot_bb_threshold ());
645 else
646 streamer_write_uhwi_stream (ob->main_stream, 0);
649 /* Output all callees or indirect outgoing edges. EDGE must be the first such
650 edge. */
652 static void
653 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
654 struct lto_simple_output_block *ob,
655 lto_symtab_encoder_t encoder)
657 if (!edge)
658 return;
660 /* Output edges in backward direction, so the reconstructed callgraph match
661 and it is easy to associate call sites in the IPA pass summaries. */
662 while (edge->next_callee)
663 edge = edge->next_callee;
664 for (; edge; edge = edge->prev_callee)
665 lto_output_edge (ob, edge, encoder);
668 /* Output the part of the cgraph in SET. */
670 static void
671 output_refs (lto_symtab_encoder_t encoder)
673 lto_symtab_encoder_iterator lsei;
674 struct lto_simple_output_block *ob;
675 int count;
676 struct ipa_ref *ref;
677 int i;
679 ob = lto_create_simple_output_block (LTO_section_refs);
681 for (lsei = lsei_start_in_partition (encoder); !lsei_end_p (lsei);
682 lsei_next_in_partition (&lsei))
684 symtab_node node = lsei_node (lsei);
686 count = ipa_ref_list_nreferences (&node->symbol.ref_list);
687 if (count)
689 streamer_write_gcov_count_stream (ob->main_stream, count);
690 streamer_write_uhwi_stream (ob->main_stream,
691 lto_symtab_encoder_lookup (encoder, node));
692 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list,
693 i, ref); i++)
694 lto_output_ref (ob, ref, encoder);
698 streamer_write_uhwi_stream (ob->main_stream, 0);
700 lto_destroy_simple_output_block (ob);
703 /* Add NODE into encoder as well as nodes it is cloned from.
704 Do it in a way so clones appear first. */
706 static void
707 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
708 bool include_body)
710 if (node->clone_of)
711 add_node_to (encoder, node->clone_of, include_body);
712 else if (include_body)
713 lto_set_symtab_encoder_encode_body (encoder, node);
714 lto_symtab_encoder_encode (encoder, (symtab_node)node);
717 /* Add all references in LIST to encoders. */
719 static void
720 add_references (lto_symtab_encoder_t encoder,
721 struct ipa_ref_list *list)
723 int i;
724 struct ipa_ref *ref;
725 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
726 if (is_a <cgraph_node> (ref->referred))
727 add_node_to (encoder, ipa_ref_node (ref), false);
728 else
729 lto_symtab_encoder_encode (encoder, ref->referred);
732 /* Find all symbols we want to stream into given partition and insert them
733 to encoders.
735 The function actually replaces IN_ENCODER by new one. The reason is that
736 streaming code needs clone's origin to be streamed before clone. This
737 means that we need to insert the nodes in specific order. This order is
738 ignored by the partitioning logic earlier. */
740 lto_symtab_encoder_t
741 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
743 struct cgraph_node *node;
744 struct cgraph_edge *edge;
745 int i;
746 lto_symtab_encoder_t encoder;
747 lto_symtab_encoder_iterator lsei;
749 encoder = lto_symtab_encoder_new (false);
751 /* Go over all entries in the IN_ENCODER and duplicate them to
752 ENCODER. At the same time insert masters of clones so
753 every master appears before clone. */
754 for (lsei = lsei_start_function_in_partition (in_encoder);
755 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
757 node = lsei_cgraph_node (lsei);
758 add_node_to (encoder, node, true);
759 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)node);
760 add_references (encoder, &node->symbol.ref_list);
761 /* For proper debug info, we need to ship the origins, too. */
762 if (DECL_ABSTRACT_ORIGIN (node->symbol.decl))
764 struct cgraph_node *origin_node
765 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (node->symbol.decl));
766 add_node_to (encoder, origin_node, true);
769 for (lsei = lsei_start_variable_in_partition (in_encoder);
770 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
772 struct varpool_node *vnode = lsei_varpool_node (lsei);
774 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)vnode);
775 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
776 add_references (encoder, &vnode->symbol.ref_list);
777 /* For proper debug info, we need to ship the origins, too. */
778 if (DECL_ABSTRACT_ORIGIN (vnode->symbol.decl))
780 struct varpool_node *origin_node
781 = varpool_get_node (DECL_ABSTRACT_ORIGIN (node->symbol.decl));
782 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)origin_node);
785 /* Pickle in also the initializer of all referenced readonly variables
786 to help folding. Constant pool variables are not shared, so we must
787 pickle those too. */
788 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
790 symtab_node node = lto_symtab_encoder_deref (encoder, i);
791 if (varpool_node *vnode = dyn_cast <varpool_node> (node))
793 if (!lto_symtab_encoder_encode_initializer_p (encoder,
794 vnode)
795 && ctor_for_folding (vnode->symbol.decl) != error_mark_node)
797 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
798 add_references (encoder, &vnode->symbol.ref_list);
803 /* Go over all the nodes again to include callees that are not in
804 SET. */
805 for (lsei = lsei_start_function_in_partition (encoder);
806 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
808 node = lsei_cgraph_node (lsei);
809 for (edge = node->callees; edge; edge = edge->next_callee)
811 struct cgraph_node *callee = edge->callee;
812 if (!lto_symtab_encoder_in_partition_p (encoder, (symtab_node)callee))
814 /* We should have moved all the inlines. */
815 gcc_assert (!callee->global.inlined_to);
816 add_node_to (encoder, callee, false);
820 lto_symtab_encoder_delete (in_encoder);
821 return encoder;
824 /* Output the part of the symtab in SET and VSET. */
826 void
827 output_symtab (void)
829 struct cgraph_node *node;
830 struct lto_simple_output_block *ob;
831 lto_symtab_encoder_iterator lsei;
832 int i, n_nodes;
833 lto_symtab_encoder_t encoder;
834 static bool asm_nodes_output = false;
836 if (flag_wpa)
837 output_cgraph_opt_summary ();
839 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
841 output_profile_summary (ob);
843 /* An encoder for cgraph nodes should have been created by
844 ipa_write_summaries_1. */
845 gcc_assert (ob->decl_state->symtab_node_encoder);
846 encoder = ob->decl_state->symtab_node_encoder;
848 /* Write out the nodes. We must first output a node and then its clones,
849 otherwise at a time reading back the node there would be nothing to clone
850 from. */
851 n_nodes = lto_symtab_encoder_size (encoder);
852 for (i = 0; i < n_nodes; i++)
854 symtab_node node = lto_symtab_encoder_deref (encoder, i);
855 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
856 lto_output_node (ob, cnode, encoder);
857 else
858 lto_output_varpool_node (ob, varpool (node), encoder);
862 /* Go over the nodes in SET again to write edges. */
863 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
864 lsei_next_function_in_partition (&lsei))
866 node = lsei_cgraph_node (lsei);
867 output_outgoing_cgraph_edges (node->callees, ob, encoder);
868 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
871 streamer_write_uhwi_stream (ob->main_stream, 0);
873 lto_destroy_simple_output_block (ob);
875 /* Emit toplevel asms.
876 When doing WPA we must output every asm just once. Since we do not partition asm
877 nodes at all, output them to first output. This is kind of hack, but should work
878 well. */
879 if (!asm_nodes_output)
881 asm_nodes_output = true;
882 lto_output_toplevel_asms ();
885 output_refs (encoder);
888 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
889 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
890 NODE or to replace the values in it, for instance because the first
891 time we saw it, the function body was not available but now it
892 is. BP is a bitpack with all the bitflags for NODE read from the
893 stream. */
895 static void
896 input_overwrite_node (struct lto_file_decl_data *file_data,
897 struct cgraph_node *node,
898 enum LTO_symtab_tags tag,
899 struct bitpack_d *bp)
901 node->symbol.aux = (void *) tag;
902 node->symbol.lto_file_data = file_data;
904 node->local.local = bp_unpack_value (bp, 1);
905 node->symbol.externally_visible = bp_unpack_value (bp, 1);
906 node->symbol.definition = bp_unpack_value (bp, 1);
907 node->local.versionable = bp_unpack_value (bp, 1);
908 node->local.can_change_signature = bp_unpack_value (bp, 1);
909 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
910 node->symbol.force_output = bp_unpack_value (bp, 1);
911 node->symbol.forced_by_abi = bp_unpack_value (bp, 1);
912 node->symbol.unique_name = bp_unpack_value (bp, 1);
913 node->symbol.address_taken = bp_unpack_value (bp, 1);
914 node->symbol.used_from_other_partition = bp_unpack_value (bp, 1);
915 node->lowered = bp_unpack_value (bp, 1);
916 node->symbol.analyzed = tag == LTO_symtab_analyzed_node;
917 node->symbol.in_other_partition = bp_unpack_value (bp, 1);
918 if (node->symbol.in_other_partition
919 /* Avoid updating decl when we are seeing just inline clone.
920 When inlining function that has functions already inlined into it,
921 we produce clones of inline clones.
923 WPA partitioning might put each clone into different unit and
924 we might end up streaming inline clone from other partition
925 to support clone we are interested in. */
926 && (!node->clone_of
927 || node->clone_of->symbol.decl != node->symbol.decl))
929 DECL_EXTERNAL (node->symbol.decl) = 1;
930 TREE_STATIC (node->symbol.decl) = 0;
932 node->symbol.alias = bp_unpack_value (bp, 1);
933 node->symbol.weakref = bp_unpack_value (bp, 1);
934 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
935 node->only_called_at_startup = bp_unpack_value (bp, 1);
936 node->only_called_at_exit = bp_unpack_value (bp, 1);
937 node->tm_clone = bp_unpack_value (bp, 1);
938 node->thunk.thunk_p = bp_unpack_value (bp, 1);
939 node->symbol.resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
940 LDPR_NUM_KNOWN);
943 /* Return string alias is alias of. */
945 static tree
946 get_alias_symbol (tree decl)
948 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
949 return get_identifier (TREE_STRING_POINTER
950 (TREE_VALUE (TREE_VALUE (alias))));
953 /* Read a node from input_block IB. TAG is the node's tag just read.
954 Return the node read or overwriten. */
956 static struct cgraph_node *
957 input_node (struct lto_file_decl_data *file_data,
958 struct lto_input_block *ib,
959 enum LTO_symtab_tags tag,
960 vec<symtab_node> nodes)
962 gcc::pass_manager *passes = g->get_passes ();
963 tree fn_decl;
964 struct cgraph_node *node;
965 struct bitpack_d bp;
966 unsigned decl_index;
967 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
968 int clone_ref;
969 int order;
970 int i, count;
972 order = streamer_read_hwi (ib) + order_base;
973 clone_ref = streamer_read_hwi (ib);
975 decl_index = streamer_read_uhwi (ib);
976 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
978 if (clone_ref != LCC_NOT_FOUND)
980 node = cgraph_clone_node (cgraph (nodes[clone_ref]), fn_decl,
981 0, CGRAPH_FREQ_BASE, false,
982 vNULL, false, NULL);
984 else
986 /* Declaration of functions can be already merged with a declaration
987 from other input file. We keep cgraph unmerged until after streaming
988 of ipa passes is done. Alays forcingly create a fresh node. */
989 node = cgraph_create_empty_node ();
990 node->symbol.decl = fn_decl;
991 symtab_register_node ((symtab_node)node);
994 node->symbol.order = order;
995 if (order >= symtab_order)
996 symtab_order = order + 1;
998 node->count = streamer_read_gcov_count (ib);
999 node->count_materialization_scale = streamer_read_hwi (ib);
1001 count = streamer_read_hwi (ib);
1002 node->ipa_transforms_to_apply = vNULL;
1003 for (i = 0; i < count; i++)
1005 struct opt_pass *pass;
1006 int pid = streamer_read_hwi (ib);
1008 gcc_assert (pid < passes->passes_by_id_size);
1009 pass = passes->passes_by_id[pid];
1010 node->ipa_transforms_to_apply.safe_push ((struct ipa_opt_pass_d *) pass);
1013 if (tag == LTO_symtab_analyzed_node)
1014 ref = streamer_read_hwi (ib);
1016 ref2 = streamer_read_hwi (ib);
1018 /* Make sure that we have not read this node before. Nodes that
1019 have already been read will have their tag stored in the 'aux'
1020 field. Since built-in functions can be referenced in multiple
1021 functions, they are expected to be read more than once. */
1022 if (node->symbol.aux && !DECL_BUILT_IN (node->symbol.decl))
1023 internal_error ("bytecode stream: found multiple instances of cgraph "
1024 "node with uid %d", node->uid);
1026 bp = streamer_read_bitpack (ib);
1027 input_overwrite_node (file_data, node, tag, &bp);
1029 /* Store a reference for now, and fix up later to be a pointer. */
1030 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
1032 /* Store a reference for now, and fix up later to be a pointer. */
1033 node->symbol.same_comdat_group = (symtab_node) (intptr_t) ref2;
1035 if (node->thunk.thunk_p)
1037 int type = streamer_read_uhwi (ib);
1038 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1039 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1041 node->thunk.fixed_offset = fixed_offset;
1042 node->thunk.this_adjusting = (type & 2);
1043 node->thunk.virtual_value = virtual_value;
1044 node->thunk.virtual_offset_p = (type & 4);
1046 if (node->symbol.alias && !node->symbol.analyzed && node->symbol.weakref)
1047 node->symbol.alias_target = get_alias_symbol (node->symbol.decl);
1048 return node;
1051 /* Read a node from input_block IB. TAG is the node's tag just read.
1052 Return the node read or overwriten. */
1054 static struct varpool_node *
1055 input_varpool_node (struct lto_file_decl_data *file_data,
1056 struct lto_input_block *ib)
1058 int decl_index;
1059 tree var_decl;
1060 struct varpool_node *node;
1061 struct bitpack_d bp;
1062 int ref = LCC_NOT_FOUND;
1063 int order;
1065 order = streamer_read_hwi (ib) + order_base;
1066 decl_index = streamer_read_uhwi (ib);
1067 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1069 /* Declaration of functions can be already merged with a declaration
1070 from other input file. We keep cgraph unmerged until after streaming
1071 of ipa passes is done. Alays forcingly create a fresh node. */
1072 node = varpool_create_empty_node ();
1073 node->symbol.decl = var_decl;
1074 symtab_register_node ((symtab_node)node);
1076 node->symbol.order = order;
1077 if (order >= symtab_order)
1078 symtab_order = order + 1;
1079 node->symbol.lto_file_data = file_data;
1081 bp = streamer_read_bitpack (ib);
1082 node->symbol.externally_visible = bp_unpack_value (&bp, 1);
1083 node->symbol.force_output = bp_unpack_value (&bp, 1);
1084 node->symbol.forced_by_abi = bp_unpack_value (&bp, 1);
1085 node->symbol.unique_name = bp_unpack_value (&bp, 1);
1086 node->symbol.definition = bp_unpack_value (&bp, 1);
1087 node->symbol.alias = bp_unpack_value (&bp, 1);
1088 node->symbol.weakref = bp_unpack_value (&bp, 1);
1089 node->symbol.analyzed = bp_unpack_value (&bp, 1);
1090 node->symbol.used_from_other_partition = bp_unpack_value (&bp, 1);
1091 node->symbol.in_other_partition = bp_unpack_value (&bp, 1);
1092 if (node->symbol.in_other_partition)
1094 DECL_EXTERNAL (node->symbol.decl) = 1;
1095 TREE_STATIC (node->symbol.decl) = 0;
1097 if (node->symbol.alias && !node->symbol.analyzed && node->symbol.weakref)
1098 node->symbol.alias_target = get_alias_symbol (node->symbol.decl);
1099 ref = streamer_read_hwi (ib);
1100 /* Store a reference for now, and fix up later to be a pointer. */
1101 node->symbol.same_comdat_group = (symtab_node) (intptr_t) ref;
1102 node->symbol.resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1103 LDPR_NUM_KNOWN);
1105 return node;
1108 /* Read a node from input_block IB. TAG is the node's tag just read.
1109 Return the node read or overwriten. */
1111 static void
1112 input_ref (struct lto_input_block *ib,
1113 symtab_node referring_node,
1114 vec<symtab_node> nodes)
1116 symtab_node node = NULL;
1117 struct bitpack_d bp;
1118 enum ipa_ref_use use;
1120 bp = streamer_read_bitpack (ib);
1121 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1122 node = nodes[streamer_read_hwi (ib)];
1123 ipa_record_reference (referring_node, node, use, NULL);
1126 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1127 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1128 edge being read is indirect (in the sense that it has
1129 indirect_unknown_callee set). */
1131 static void
1132 input_edge (struct lto_input_block *ib, vec<symtab_node> nodes,
1133 bool indirect)
1135 struct cgraph_node *caller, *callee;
1136 struct cgraph_edge *edge;
1137 unsigned int stmt_id;
1138 gcov_type count;
1139 int freq;
1140 cgraph_inline_failed_t inline_failed;
1141 struct bitpack_d bp;
1142 int ecf_flags = 0;
1144 caller = cgraph (nodes[streamer_read_hwi (ib)]);
1145 if (caller == NULL || caller->symbol.decl == NULL_TREE)
1146 internal_error ("bytecode stream: no caller found while reading edge");
1148 if (!indirect)
1150 callee = cgraph (nodes[streamer_read_hwi (ib)]);
1151 if (callee == NULL || callee->symbol.decl == NULL_TREE)
1152 internal_error ("bytecode stream: no callee found while reading edge");
1154 else
1155 callee = NULL;
1157 count = streamer_read_gcov_count (ib);
1159 bp = streamer_read_bitpack (ib);
1160 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_enum, CIF_N_REASONS);
1161 stmt_id = bp_unpack_var_len_unsigned (&bp);
1162 freq = (int) bp_unpack_var_len_unsigned (&bp);
1164 if (indirect)
1165 edge = cgraph_create_indirect_edge (caller, NULL, 0, count, freq);
1166 else
1167 edge = cgraph_create_edge (caller, callee, NULL, count, freq);
1169 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1170 edge->lto_stmt_uid = stmt_id;
1171 edge->inline_failed = inline_failed;
1172 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1173 edge->can_throw_external = bp_unpack_value (&bp, 1);
1174 if (indirect)
1176 if (bp_unpack_value (&bp, 1))
1177 ecf_flags |= ECF_CONST;
1178 if (bp_unpack_value (&bp, 1))
1179 ecf_flags |= ECF_PURE;
1180 if (bp_unpack_value (&bp, 1))
1181 ecf_flags |= ECF_NORETURN;
1182 if (bp_unpack_value (&bp, 1))
1183 ecf_flags |= ECF_MALLOC;
1184 if (bp_unpack_value (&bp, 1))
1185 ecf_flags |= ECF_NOTHROW;
1186 if (bp_unpack_value (&bp, 1))
1187 ecf_flags |= ECF_RETURNS_TWICE;
1188 edge->indirect_info->ecf_flags = ecf_flags;
1193 /* Read a cgraph from IB using the info in FILE_DATA. */
1195 static vec<symtab_node>
1196 input_cgraph_1 (struct lto_file_decl_data *file_data,
1197 struct lto_input_block *ib)
1199 enum LTO_symtab_tags tag;
1200 vec<symtab_node> nodes = vNULL;
1201 symtab_node node;
1202 unsigned i;
1204 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1205 order_base = symtab_order;
1206 while (tag)
1208 if (tag == LTO_symtab_edge)
1209 input_edge (ib, nodes, false);
1210 else if (tag == LTO_symtab_indirect_edge)
1211 input_edge (ib, nodes, true);
1212 else if (tag == LTO_symtab_variable)
1214 node = (symtab_node)input_varpool_node (file_data, ib);
1215 nodes.safe_push (node);
1216 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1218 else
1220 node = (symtab_node)input_node (file_data, ib, tag, nodes);
1221 if (node == NULL || node->symbol.decl == NULL_TREE)
1222 internal_error ("bytecode stream: found empty cgraph node");
1223 nodes.safe_push (node);
1224 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1227 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1230 lto_input_toplevel_asms (file_data, order_base);
1232 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1233 #ifdef ENABLE_CHECKING
1234 FOR_EACH_VEC_ELT (nodes, i, node)
1235 gcc_assert (node->symbol.aux || !is_a <cgraph_node> (node));
1236 #endif
1237 FOR_EACH_VEC_ELT (nodes, i, node)
1239 int ref;
1240 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
1242 ref = (int) (intptr_t) cnode->global.inlined_to;
1244 /* We share declaration of builtins, so we may read same node twice. */
1245 if (!node->symbol.aux)
1246 continue;
1247 node->symbol.aux = NULL;
1249 /* Fixup inlined_to from reference to pointer. */
1250 if (ref != LCC_NOT_FOUND)
1251 cgraph (node)->global.inlined_to = cgraph (nodes[ref]);
1252 else
1253 cnode->global.inlined_to = NULL;
1256 ref = (int) (intptr_t) node->symbol.same_comdat_group;
1258 /* Fixup same_comdat_group from reference to pointer. */
1259 if (ref != LCC_NOT_FOUND)
1260 node->symbol.same_comdat_group = nodes[ref];
1261 else
1262 node->symbol.same_comdat_group = NULL;
1264 FOR_EACH_VEC_ELT (nodes, i, node)
1265 node->symbol.aux = is_a <cgraph_node> (node) ? (void *)1 : NULL;
1266 return nodes;
1269 /* Input ipa_refs. */
1271 static void
1272 input_refs (struct lto_input_block *ib,
1273 vec<symtab_node> nodes)
1275 int count;
1276 int idx;
1277 while (true)
1279 symtab_node node;
1280 count = streamer_read_uhwi (ib);
1281 if (!count)
1282 break;
1283 idx = streamer_read_uhwi (ib);
1284 node = nodes[idx];
1285 while (count)
1287 input_ref (ib, node, nodes);
1288 count--;
1294 static struct gcov_ctr_summary lto_gcov_summary;
1296 /* Input profile_info from IB. */
1297 static void
1298 input_profile_summary (struct lto_input_block *ib,
1299 struct lto_file_decl_data *file_data)
1301 unsigned h_ix;
1302 struct bitpack_d bp;
1303 unsigned int runs = streamer_read_uhwi (ib);
1304 if (runs)
1306 file_data->profile_info.runs = runs;
1307 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1308 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1310 memset (file_data->profile_info.histogram, 0,
1311 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1312 /* Input the bitpack of non-zero histogram indices. */
1313 bp = streamer_read_bitpack (ib);
1314 /* Read in and unpack the full bitpack, flagging non-zero
1315 histogram entries by setting the num_counters non-zero. */
1316 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1318 file_data->profile_info.histogram[h_ix].num_counters
1319 = bp_unpack_value (&bp, 1);
1321 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1323 if (!file_data->profile_info.histogram[h_ix].num_counters)
1324 continue;
1326 file_data->profile_info.histogram[h_ix].num_counters
1327 = streamer_read_gcov_count (ib);
1328 file_data->profile_info.histogram[h_ix].min_value
1329 = streamer_read_gcov_count (ib);
1330 file_data->profile_info.histogram[h_ix].cum_value
1331 = streamer_read_gcov_count (ib);
1333 /* IPA-profile computes hot bb threshold based on cumulated
1334 whole program profile. We need to stream it down to ltrans. */
1335 if (flag_ltrans)
1336 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1341 /* Rescale profile summaries to the same number of runs in the whole unit. */
1343 static void
1344 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1346 struct lto_file_decl_data *file_data;
1347 unsigned int j, h_ix;
1348 gcov_unsigned_t max_runs = 0;
1349 struct cgraph_node *node;
1350 struct cgraph_edge *edge;
1351 gcov_type saved_sum_all = 0;
1352 gcov_ctr_summary *saved_profile_info = 0;
1353 int saved_scale = 0;
1355 /* Find unit with maximal number of runs. If we ever get serious about
1356 roundoff errors, we might also consider computing smallest common
1357 multiply. */
1358 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1359 if (max_runs < file_data->profile_info.runs)
1360 max_runs = file_data->profile_info.runs;
1362 if (!max_runs)
1363 return;
1365 /* Simple overflow check. We probably don't need to support that many train
1366 runs. Such a large value probably imply data corruption anyway. */
1367 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1369 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1370 INT_MAX / REG_BR_PROB_BASE);
1371 return;
1374 profile_info = &lto_gcov_summary;
1375 lto_gcov_summary.runs = max_runs;
1376 lto_gcov_summary.sum_max = 0;
1377 memset (lto_gcov_summary.histogram, 0,
1378 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1380 /* Rescale all units to the maximal number of runs.
1381 sum_max can not be easily merged, as we have no idea what files come from
1382 the same run. We do not use the info anyway, so leave it 0. */
1383 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1384 if (file_data->profile_info.runs)
1386 int scale = GCOV_COMPUTE_SCALE (max_runs,
1387 file_data->profile_info.runs);
1388 lto_gcov_summary.sum_max
1389 = MAX (lto_gcov_summary.sum_max,
1390 apply_scale (file_data->profile_info.sum_max, scale));
1391 lto_gcov_summary.sum_all
1392 = MAX (lto_gcov_summary.sum_all,
1393 apply_scale (file_data->profile_info.sum_all, scale));
1394 /* Save a pointer to the profile_info with the largest
1395 scaled sum_all and the scale for use in merging the
1396 histogram. */
1397 if (!saved_profile_info
1398 || lto_gcov_summary.sum_all > saved_sum_all)
1400 saved_profile_info = &file_data->profile_info;
1401 saved_sum_all = lto_gcov_summary.sum_all;
1402 saved_scale = scale;
1406 gcc_assert (saved_profile_info);
1408 /* Scale up the histogram from the profile that had the largest
1409 scaled sum_all above. */
1410 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1412 /* Scale up the min value as we did the corresponding sum_all
1413 above. Use that to find the new histogram index. */
1414 gcov_type scaled_min
1415 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1416 saved_scale);
1417 /* The new index may be shared with another scaled histogram entry,
1418 so we need to account for a non-zero histogram entry at new_ix. */
1419 unsigned new_ix = gcov_histo_index (scaled_min);
1420 lto_gcov_summary.histogram[new_ix].min_value
1421 = (lto_gcov_summary.histogram[new_ix].num_counters
1422 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1423 : scaled_min);
1424 /* Some of the scaled counter values would ostensibly need to be placed
1425 into different (larger) histogram buckets, but we keep things simple
1426 here and place the scaled cumulative counter value in the bucket
1427 corresponding to the scaled minimum counter value. */
1428 lto_gcov_summary.histogram[new_ix].cum_value
1429 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1430 saved_scale);
1431 lto_gcov_summary.histogram[new_ix].num_counters
1432 += saved_profile_info->histogram[h_ix].num_counters;
1435 /* Watch roundoff errors. */
1436 if (lto_gcov_summary.sum_max < max_runs)
1437 lto_gcov_summary.sum_max = max_runs;
1439 /* If merging already happent at WPA time, we are done. */
1440 if (flag_ltrans)
1441 return;
1443 /* Now compute count_materialization_scale of each node.
1444 During LTRANS we already have values of count_materialization_scale
1445 computed, so just update them. */
1446 FOR_EACH_FUNCTION (node)
1447 if (node->symbol.lto_file_data
1448 && node->symbol.lto_file_data->profile_info.runs)
1450 int scale;
1452 scale = RDIV (node->count_materialization_scale * max_runs,
1453 node->symbol.lto_file_data->profile_info.runs);
1454 node->count_materialization_scale = scale;
1455 if (scale < 0)
1456 fatal_error ("Profile information in %s corrupted",
1457 file_data->file_name);
1459 if (scale == REG_BR_PROB_BASE)
1460 continue;
1461 for (edge = node->callees; edge; edge = edge->next_callee)
1462 edge->count = apply_scale (edge->count, scale);
1463 node->count = apply_scale (node->count, scale);
1467 /* Input and merge the symtab from each of the .o files passed to
1468 lto1. */
1470 void
1471 input_symtab (void)
1473 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1474 struct lto_file_decl_data *file_data;
1475 unsigned int j = 0;
1476 struct cgraph_node *node;
1478 while ((file_data = file_data_vec[j++]))
1480 const char *data;
1481 size_t len;
1482 struct lto_input_block *ib;
1483 vec<symtab_node> nodes;
1485 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1486 &data, &len);
1487 if (!ib)
1488 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1489 input_profile_summary (ib, file_data);
1490 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1491 nodes = input_cgraph_1 (file_data, ib);
1492 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1493 ib, data, len);
1495 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1496 &data, &len);
1497 if (!ib)
1498 fatal_error("cannot find LTO section refs in %s", file_data->file_name);
1499 input_refs (ib, nodes);
1500 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1501 ib, data, len);
1502 if (flag_ltrans)
1503 input_cgraph_opt_summary (nodes);
1504 nodes.release ();
1507 merge_profile_summaries (file_data_vec);
1508 get_working_sets ();
1511 /* Clear out the aux field that was used to store enough state to
1512 tell which nodes should be overwritten. */
1513 FOR_EACH_FUNCTION (node)
1515 /* Some nodes may have been created by cgraph_node. This
1516 happens when the callgraph contains nested functions. If the
1517 node for the parent function was never emitted to the gimple
1518 file, cgraph_node will create a node for it when setting the
1519 context of the nested function. */
1520 if (node->symbol.lto_file_data)
1521 node->symbol.aux = NULL;
1525 /* True when we need optimization summary for NODE. */
1527 static int
1528 output_cgraph_opt_summary_p (struct cgraph_node *node)
1530 return (node->clone_of
1531 && (node->clone.tree_map
1532 || node->clone.args_to_skip
1533 || node->clone.combined_args_to_skip));
1536 /* Output optimization summary for EDGE to OB. */
1537 static void
1538 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1539 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1543 /* Output optimization summary for NODE to OB. */
1545 static void
1546 output_node_opt_summary (struct output_block *ob,
1547 struct cgraph_node *node,
1548 lto_symtab_encoder_t encoder)
1550 unsigned int index;
1551 bitmap_iterator bi;
1552 struct ipa_replace_map *map;
1553 struct bitpack_d bp;
1554 int i;
1555 struct cgraph_edge *e;
1557 if (node->clone.args_to_skip)
1559 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1560 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1561 streamer_write_uhwi (ob, index);
1563 else
1564 streamer_write_uhwi (ob, 0);
1565 if (node->clone.combined_args_to_skip)
1567 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1568 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1569 streamer_write_uhwi (ob, index);
1571 else
1572 streamer_write_uhwi (ob, 0);
1573 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1574 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1576 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1577 mechanism to store function local declarations into summaries. */
1578 gcc_assert (!map->old_tree);
1579 streamer_write_uhwi (ob, map->parm_num);
1580 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1581 stream_write_tree (ob, map->new_tree, true);
1582 bp = bitpack_create (ob->main_stream);
1583 bp_pack_value (&bp, map->replace_p, 1);
1584 bp_pack_value (&bp, map->ref_p, 1);
1585 streamer_write_bitpack (&bp);
1588 if (lto_symtab_encoder_in_partition_p (encoder, (symtab_node) node))
1590 for (e = node->callees; e; e = e->next_callee)
1591 output_edge_opt_summary (ob, e);
1592 for (e = node->indirect_calls; e; e = e->next_callee)
1593 output_edge_opt_summary (ob, e);
1597 /* Output optimization summaries stored in callgraph.
1598 At the moment it is the clone info structure. */
1600 static void
1601 output_cgraph_opt_summary (void)
1603 int i, n_nodes;
1604 lto_symtab_encoder_t encoder;
1605 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1606 unsigned count = 0;
1608 ob->cgraph_node = NULL;
1609 encoder = ob->decl_state->symtab_node_encoder;
1610 n_nodes = lto_symtab_encoder_size (encoder);
1611 for (i = 0; i < n_nodes; i++)
1613 symtab_node node = lto_symtab_encoder_deref (encoder, i);
1614 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1615 if (cnode && output_cgraph_opt_summary_p (cnode))
1616 count++;
1618 streamer_write_uhwi (ob, count);
1619 for (i = 0; i < n_nodes; i++)
1621 symtab_node node = lto_symtab_encoder_deref (encoder, i);
1622 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1623 if (cnode && output_cgraph_opt_summary_p (cnode))
1625 streamer_write_uhwi (ob, i);
1626 output_node_opt_summary (ob, cnode, encoder);
1629 produce_asm (ob, NULL);
1630 destroy_output_block (ob);
1633 /* Input optimisation summary of EDGE. */
1635 static void
1636 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
1637 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
1641 /* Input optimisation summary of NODE. */
1643 static void
1644 input_node_opt_summary (struct cgraph_node *node,
1645 struct lto_input_block *ib_main,
1646 struct data_in *data_in)
1648 int i;
1649 int count;
1650 int bit;
1651 struct bitpack_d bp;
1652 struct cgraph_edge *e;
1654 count = streamer_read_uhwi (ib_main);
1655 if (count)
1656 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1657 for (i = 0; i < count; i++)
1659 bit = streamer_read_uhwi (ib_main);
1660 bitmap_set_bit (node->clone.args_to_skip, bit);
1662 count = streamer_read_uhwi (ib_main);
1663 if (count)
1664 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1665 for (i = 0; i < count; i++)
1667 bit = streamer_read_uhwi (ib_main);
1668 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1670 count = streamer_read_uhwi (ib_main);
1671 for (i = 0; i < count; i++)
1673 struct ipa_replace_map *map = ggc_alloc_ipa_replace_map ();
1675 vec_safe_push (node->clone.tree_map, map);
1676 map->parm_num = streamer_read_uhwi (ib_main);
1677 map->old_tree = NULL;
1678 map->new_tree = stream_read_tree (ib_main, data_in);
1679 bp = streamer_read_bitpack (ib_main);
1680 map->replace_p = bp_unpack_value (&bp, 1);
1681 map->ref_p = bp_unpack_value (&bp, 1);
1683 for (e = node->callees; e; e = e->next_callee)
1684 input_edge_opt_summary (e, ib_main);
1685 for (e = node->indirect_calls; e; e = e->next_callee)
1686 input_edge_opt_summary (e, ib_main);
1689 /* Read section in file FILE_DATA of length LEN with data DATA. */
1691 static void
1692 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1693 const char *data, size_t len,
1694 vec<symtab_node> nodes)
1696 const struct lto_function_header *header =
1697 (const struct lto_function_header *) data;
1698 const int cfg_offset = sizeof (struct lto_function_header);
1699 const int main_offset = cfg_offset + header->cfg_size;
1700 const int string_offset = main_offset + header->main_size;
1701 struct data_in *data_in;
1702 struct lto_input_block ib_main;
1703 unsigned int i;
1704 unsigned int count;
1706 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1707 header->main_size);
1709 data_in =
1710 lto_data_in_create (file_data, (const char *) data + string_offset,
1711 header->string_size, vNULL);
1712 count = streamer_read_uhwi (&ib_main);
1714 for (i = 0; i < count; i++)
1716 int ref = streamer_read_uhwi (&ib_main);
1717 input_node_opt_summary (cgraph (nodes[ref]),
1718 &ib_main, data_in);
1720 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1721 len);
1722 lto_data_in_delete (data_in);
1725 /* Input optimization summary of cgraph. */
1727 static void
1728 input_cgraph_opt_summary (vec<symtab_node> nodes)
1730 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1731 struct lto_file_decl_data *file_data;
1732 unsigned int j = 0;
1734 while ((file_data = file_data_vec[j++]))
1736 size_t len;
1737 const char *data =
1738 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1739 &len);
1741 if (data)
1742 input_cgraph_opt_section (file_data, data, len, nodes);