* varasm.c (bss_initializer_p): Remove static.
[official-gcc.git] / gcc / lto-cgraph.c
blob73a58f03206b11f1bb1563c657b39c59424f7a0a
1 /* Write and read the cgraph to the memory mapped representation of a
2 .o file.
4 Copyright 2009, 2010, 2011 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"
51 static void output_cgraph_opt_summary (void);
52 static void input_cgraph_opt_summary (vec<symtab_node> nodes);
54 /* Number of LDPR values known to GCC. */
55 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
57 /* All node orders are ofsetted by ORDER_BASE. */
58 static int order_base;
60 /* Cgraph streaming is organized as set of record whose type
61 is indicated by a tag. */
62 enum LTO_symtab_tags
64 /* Must leave 0 for the stopper. */
66 /* Cgraph node without body available. */
67 LTO_symtab_unavail_node = 1,
68 /* Cgraph node with function body. */
69 LTO_symtab_analyzed_node,
70 /* Cgraph edges. */
71 LTO_symtab_edge,
72 LTO_symtab_indirect_edge,
73 LTO_symtab_variable,
74 LTO_symtab_last_tag
77 /* Create a new symtab encoder.
78 if FOR_INPUT, the encoder allocate only datastructures needed
79 to read the symtab. */
81 lto_symtab_encoder_t
82 lto_symtab_encoder_new (bool for_input)
84 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
86 if (!for_input)
87 encoder->map = pointer_map_create ();
88 encoder->nodes.create (0);
89 return encoder;
93 /* Delete ENCODER and its components. */
95 void
96 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
98 encoder->nodes.release ();
99 if (encoder->map)
100 pointer_map_destroy (encoder->map);
101 free (encoder);
105 /* Return the existing reference number of NODE in the symtab encoder in
106 output block OB. Assign a new reference if this is the first time
107 NODE is encoded. */
110 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
111 symtab_node node)
113 int ref;
114 void **slot;
116 if (!encoder->map)
118 lto_encoder_entry entry = {node, false, false, false};
120 ref = encoder->nodes.length ();
121 encoder->nodes.safe_push (entry);
122 return ref;
125 slot = pointer_map_contains (encoder->map, node);
126 if (!slot || !*slot)
128 lto_encoder_entry entry = {node, false, false, false};
129 ref = encoder->nodes.length ();
130 if (!slot)
131 slot = pointer_map_insert (encoder->map, node);
132 *slot = (void *) (intptr_t) (ref + 1);
133 encoder->nodes.safe_push (entry);
135 else
136 ref = (size_t) *slot - 1;
138 return ref;
141 /* Remove NODE from encoder. */
143 bool
144 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
145 symtab_node node)
147 void **slot, **last_slot;
148 int index;
149 lto_encoder_entry last_node;
151 slot = pointer_map_contains (encoder->map, node);
152 if (slot == NULL || !*slot)
153 return false;
155 index = (size_t) *slot - 1;
156 gcc_checking_assert (encoder->nodes[index].node == node);
158 /* Remove from vector. We do this by swapping node with the last element
159 of the vector. */
160 last_node = encoder->nodes.pop ();
161 if (last_node.node != node)
163 last_slot = pointer_map_contains (encoder->map, last_node.node);
164 gcc_checking_assert (last_slot && *last_slot);
165 *last_slot = (void *)(size_t) (index + 1);
167 /* Move the last element to the original spot of NODE. */
168 encoder->nodes[index] = last_node;
171 /* Remove element from hash table. */
172 *slot = NULL;
173 return true;
177 /* Return TRUE if we should encode initializer of NODE (if any). */
179 bool
180 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
181 struct cgraph_node *node)
183 int index = lto_symtab_encoder_lookup (encoder, (symtab_node)node);
184 return encoder->nodes[index].body;
187 /* Return TRUE if we should encode body of NODE (if any). */
189 static void
190 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
191 struct cgraph_node *node)
193 int index = lto_symtab_encoder_encode (encoder, (symtab_node)node);
194 gcc_checking_assert (encoder->nodes[index].node == (symtab_node)node);
195 encoder->nodes[index].body = true;
198 /* Return TRUE if we should encode initializer of NODE (if any). */
200 bool
201 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
202 struct varpool_node *node)
204 int index = lto_symtab_encoder_lookup (encoder, (symtab_node)node);
205 if (index == LCC_NOT_FOUND)
206 return false;
207 return encoder->nodes[index].initializer;
210 /* Return TRUE if we should encode initializer of NODE (if any). */
212 static void
213 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
214 struct varpool_node *node)
216 int index = lto_symtab_encoder_lookup (encoder, (symtab_node)node);
217 encoder->nodes[index].initializer = true;
220 /* Return TRUE if we should encode initializer of NODE (if any). */
222 bool
223 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
224 symtab_node node)
226 int index = lto_symtab_encoder_lookup (encoder, (symtab_node)node);
227 if (index == LCC_NOT_FOUND)
228 return false;
229 return encoder->nodes[index].in_partition;
232 /* Return TRUE if we should encode body of NODE (if any). */
234 void
235 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
236 symtab_node node)
238 int index = lto_symtab_encoder_encode (encoder, (symtab_node)node);
239 encoder->nodes[index].in_partition = true;
242 /* Output the cgraph EDGE to OB using ENCODER. */
244 static void
245 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
246 lto_symtab_encoder_t encoder)
248 unsigned int uid;
249 intptr_t ref;
250 struct bitpack_d bp;
252 if (edge->indirect_unknown_callee)
253 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
254 LTO_symtab_indirect_edge);
255 else
256 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
257 LTO_symtab_edge);
259 ref = lto_symtab_encoder_lookup (encoder, (symtab_node)edge->caller);
260 gcc_assert (ref != LCC_NOT_FOUND);
261 streamer_write_hwi_stream (ob->main_stream, ref);
263 if (!edge->indirect_unknown_callee)
265 ref = lto_symtab_encoder_lookup (encoder, (symtab_node)edge->callee);
266 gcc_assert (ref != LCC_NOT_FOUND);
267 streamer_write_hwi_stream (ob->main_stream, ref);
270 streamer_write_hwi_stream (ob->main_stream, edge->count);
272 bp = bitpack_create (ob->main_stream);
273 uid = (!gimple_has_body_p (edge->caller->symbol.decl)
274 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt));
275 bp_pack_enum (&bp, cgraph_inline_failed_enum,
276 CIF_N_REASONS, edge->inline_failed);
277 bp_pack_var_len_unsigned (&bp, uid);
278 bp_pack_var_len_unsigned (&bp, edge->frequency);
279 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
280 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
281 bp_pack_value (&bp, edge->can_throw_external, 1);
282 if (edge->indirect_unknown_callee)
284 int flags = edge->indirect_info->ecf_flags;
285 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
286 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
287 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
288 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
289 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
290 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
291 /* Flags that should not appear on indirect calls. */
292 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
293 | ECF_MAY_BE_ALLOCA
294 | ECF_SIBCALL
295 | ECF_LEAF
296 | ECF_NOVOPS)));
298 streamer_write_bitpack (&bp);
301 /* Return if LIST contain references from other partitions. */
303 bool
304 referenced_from_other_partition_p (struct ipa_ref_list *list, lto_symtab_encoder_t encoder)
306 int i;
307 struct ipa_ref *ref;
308 for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
310 if (ref->referring->symbol.in_other_partition
311 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
312 return true;
314 return false;
317 /* Return true when node is reachable from other partition. */
319 bool
320 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
322 struct cgraph_edge *e;
323 if (!node->analyzed)
324 return false;
325 if (node->global.inlined_to)
326 return false;
327 for (e = node->callers; e; e = e->next_caller)
328 if (e->caller->symbol.in_other_partition
329 || !lto_symtab_encoder_in_partition_p (encoder, (symtab_node)e->caller))
330 return true;
331 return false;
334 /* Return if LIST contain references from other partitions. */
336 bool
337 referenced_from_this_partition_p (struct ipa_ref_list *list,
338 lto_symtab_encoder_t encoder)
340 int i;
341 struct ipa_ref *ref;
342 for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
343 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
344 return true;
345 return false;
348 /* Return true when node is reachable from other partition. */
350 bool
351 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
353 struct cgraph_edge *e;
354 for (e = node->callers; e; e = e->next_caller)
355 if (lto_symtab_encoder_in_partition_p (encoder, (symtab_node)e->caller))
356 return true;
357 return false;
360 /* Output the cgraph NODE to OB. ENCODER is used to find the
361 reference number of NODE->inlined_to. SET is the set of nodes we
362 are writing to the current file. If NODE is not in SET, then NODE
363 is a boundary of a cgraph_node_set and we pretend NODE just has a
364 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
365 that have had their callgraph node written so far. This is used to
366 determine if NODE is a clone of a previously written node. */
368 static void
369 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
370 lto_symtab_encoder_t encoder)
372 unsigned int tag;
373 struct bitpack_d bp;
374 bool boundary_p;
375 intptr_t ref;
376 bool in_other_partition = false;
377 struct cgraph_node *clone_of;
378 struct ipa_opt_pass_d *pass;
379 int i;
381 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, (symtab_node)node);
383 if (node->analyzed && !boundary_p)
384 tag = LTO_symtab_analyzed_node;
385 else
386 tag = LTO_symtab_unavail_node;
388 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
389 tag);
390 streamer_write_hwi_stream (ob->main_stream, node->symbol.order);
392 /* In WPA mode, we only output part of the call-graph. Also, we
393 fake cgraph node attributes. There are two cases that we care.
395 Boundary nodes: There are nodes that are not part of SET but are
396 called from within SET. We artificially make them look like
397 externally visible nodes with no function body.
399 Cherry-picked nodes: These are nodes we pulled from other
400 translation units into SET during IPA-inlining. We make them as
401 local static nodes to prevent clashes with other local statics. */
402 if (boundary_p && node->analyzed && !DECL_EXTERNAL (node->symbol.decl))
404 /* Inline clones can not be part of boundary.
405 gcc_assert (!node->global.inlined_to);
407 FIXME: At the moment they can be, when partition contains an inline
408 clone that is clone of inline clone from outside partition. We can
409 reshape the clone tree and make other tree to be the root, but it
410 needs a bit extra work and will be promplty done by cgraph_remove_node
411 after reading back. */
412 in_other_partition = 1;
415 clone_of = node->clone_of;
416 while (clone_of
417 && (ref = lto_symtab_encoder_lookup (encoder, (symtab_node)clone_of)) == LCC_NOT_FOUND)
418 if (clone_of->prev_sibling_clone)
419 clone_of = clone_of->prev_sibling_clone;
420 else
421 clone_of = clone_of->clone_of;
423 if (LTO_symtab_analyzed_node)
424 gcc_assert (clone_of || !node->clone_of);
425 if (!clone_of)
426 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
427 else
428 streamer_write_hwi_stream (ob->main_stream, ref);
431 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->symbol.decl);
432 streamer_write_hwi_stream (ob->main_stream, node->count);
433 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
435 streamer_write_hwi_stream (ob->main_stream,
436 node->ipa_transforms_to_apply.length ());
437 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
438 streamer_write_hwi_stream (ob->main_stream, pass->pass.static_pass_number);
440 if (tag == LTO_symtab_analyzed_node)
442 if (node->global.inlined_to)
444 ref = lto_symtab_encoder_lookup (encoder, (symtab_node)node->global.inlined_to);
445 gcc_assert (ref != LCC_NOT_FOUND);
447 else
448 ref = LCC_NOT_FOUND;
450 streamer_write_hwi_stream (ob->main_stream, ref);
453 if (node->symbol.same_comdat_group && !boundary_p)
455 ref = lto_symtab_encoder_lookup (encoder,
456 node->symbol.same_comdat_group);
457 gcc_assert (ref != LCC_NOT_FOUND);
459 else
460 ref = LCC_NOT_FOUND;
461 streamer_write_hwi_stream (ob->main_stream, ref);
463 bp = bitpack_create (ob->main_stream);
464 bp_pack_value (&bp, node->local.local, 1);
465 bp_pack_value (&bp, node->symbol.externally_visible, 1);
466 bp_pack_value (&bp, node->local.finalized, 1);
467 bp_pack_value (&bp, node->local.versionable, 1);
468 bp_pack_value (&bp, node->local.can_change_signature, 1);
469 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
470 bp_pack_value (&bp, node->symbol.force_output, 1);
471 bp_pack_value (&bp, node->symbol.address_taken, 1);
472 bp_pack_value (&bp, node->abstract_and_needed, 1);
473 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
474 && !DECL_EXTERNAL (node->symbol.decl)
475 && !DECL_COMDAT (node->symbol.decl)
476 && (reachable_from_other_partition_p (node, encoder)
477 || referenced_from_other_partition_p (&node->symbol.ref_list,
478 encoder)), 1);
479 bp_pack_value (&bp, node->lowered, 1);
480 bp_pack_value (&bp, in_other_partition, 1);
481 /* Real aliases in a boundary become non-aliases. However we still stream
482 alias info on weakrefs.
483 TODO: We lose a bit of information here - when we know that variable is
484 defined in other unit, we may use the info on aliases to resolve
485 symbol1 != symbol2 type tests that we can do only for locally defined objects
486 otherwise. */
487 bp_pack_value (&bp, node->alias && (!boundary_p || DECL_EXTERNAL (node->symbol.decl)), 1);
488 bp_pack_value (&bp, node->frequency, 2);
489 bp_pack_value (&bp, node->only_called_at_startup, 1);
490 bp_pack_value (&bp, node->only_called_at_exit, 1);
491 bp_pack_value (&bp, node->tm_clone, 1);
492 bp_pack_value (&bp, node->thunk.thunk_p && !boundary_p, 1);
493 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
494 LDPR_NUM_KNOWN, node->symbol.resolution);
495 streamer_write_bitpack (&bp);
497 if (node->thunk.thunk_p && !boundary_p)
499 streamer_write_uhwi_stream
500 (ob->main_stream,
501 1 + (node->thunk.this_adjusting != 0) * 2
502 + (node->thunk.virtual_offset_p != 0) * 4);
503 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
504 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
506 if ((node->alias || node->thunk.thunk_p)
507 && (!boundary_p || (node->alias && DECL_EXTERNAL (node->symbol.decl))))
509 streamer_write_hwi_in_range (ob->main_stream, 0, 1,
510 node->thunk.alias != NULL);
511 if (node->thunk.alias != NULL)
512 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
513 node->thunk.alias);
517 /* Output the varpool NODE to OB.
518 If NODE is not in SET, then NODE is a boundary. */
520 static void
521 lto_output_varpool_node (struct lto_simple_output_block *ob, struct varpool_node *node,
522 lto_symtab_encoder_t encoder)
524 bool boundary_p = (node->analyzed
525 && !lto_symtab_encoder_in_partition_p (encoder, (symtab_node)node));
526 struct bitpack_d bp;
527 int ref;
529 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
530 LTO_symtab_variable);
531 streamer_write_hwi_stream (ob->main_stream, node->symbol.order);
532 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->symbol.decl);
533 bp = bitpack_create (ob->main_stream);
534 bp_pack_value (&bp, node->symbol.externally_visible, 1);
535 bp_pack_value (&bp, node->symbol.force_output, 1);
536 bp_pack_value (&bp, node->finalized, 1);
537 bp_pack_value (&bp, node->alias, 1);
538 bp_pack_value (&bp, node->alias_of != NULL, 1);
539 gcc_assert (node->finalized || !node->analyzed);
540 /* Constant pool initializers can be de-unified into individual ltrans units.
541 FIXME: Alternatively at -Os we may want to avoid generating for them the local
542 labels and share them across LTRANS partitions. */
543 if (DECL_IN_CONSTANT_POOL (node->symbol.decl)
544 && !DECL_EXTERNAL (node->symbol.decl)
545 && !DECL_COMDAT (node->symbol.decl))
547 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
548 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
550 else
552 bp_pack_value (&bp, node->analyzed
553 && referenced_from_other_partition_p (&node->symbol.ref_list,
554 encoder), 1);
555 bp_pack_value (&bp, boundary_p && !DECL_EXTERNAL (node->symbol.decl), 1);
556 /* in_other_partition. */
558 streamer_write_bitpack (&bp);
559 if (node->alias_of)
560 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->alias_of);
561 if (node->symbol.same_comdat_group && !boundary_p)
563 ref = lto_symtab_encoder_lookup (encoder,
564 node->symbol.same_comdat_group);
565 gcc_assert (ref != LCC_NOT_FOUND);
567 else
568 ref = LCC_NOT_FOUND;
569 streamer_write_hwi_stream (ob->main_stream, ref);
570 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
571 LDPR_NUM_KNOWN, node->symbol.resolution);
574 /* Output the varpool NODE to OB.
575 If NODE is not in SET, then NODE is a boundary. */
577 static void
578 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
579 lto_symtab_encoder_t encoder)
581 struct bitpack_d bp;
582 int nref;
584 bp = bitpack_create (ob->main_stream);
585 bp_pack_value (&bp, ref->use, 2);
586 streamer_write_bitpack (&bp);
587 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
588 gcc_assert (nref != LCC_NOT_FOUND);
589 streamer_write_hwi_stream (ob->main_stream, nref);
592 /* Stream out profile_summary to OB. */
594 static void
595 output_profile_summary (struct lto_simple_output_block *ob)
597 unsigned h_ix;
598 struct bitpack_d bp;
600 if (profile_info)
602 /* We do not output num and run_max, they are not used by
603 GCC profile feedback and they are difficult to merge from multiple
604 units. */
605 gcc_assert (profile_info->runs);
606 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
607 streamer_write_uhwi_stream (ob->main_stream, profile_info->sum_max);
609 /* sum_all is needed for computing the working set with the
610 histogram. */
611 streamer_write_uhwi_stream (ob->main_stream, profile_info->sum_all);
613 /* Create and output a bitpack of non-zero histogram entries indices. */
614 bp = bitpack_create (ob->main_stream);
615 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
616 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
617 streamer_write_bitpack (&bp);
618 /* Now stream out only those non-zero entries. */
619 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
621 if (!profile_info->histogram[h_ix].num_counters)
622 continue;
623 streamer_write_uhwi_stream (ob->main_stream,
624 profile_info->histogram[h_ix].num_counters);
625 streamer_write_uhwi_stream (ob->main_stream,
626 profile_info->histogram[h_ix].min_value);
627 streamer_write_uhwi_stream (ob->main_stream,
628 profile_info->histogram[h_ix].cum_value);
631 else
632 streamer_write_uhwi_stream (ob->main_stream, 0);
635 /* Output all callees or indirect outgoing edges. EDGE must be the first such
636 edge. */
638 static void
639 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
640 struct lto_simple_output_block *ob,
641 lto_symtab_encoder_t encoder)
643 if (!edge)
644 return;
646 /* Output edges in backward direction, so the reconstructed callgraph match
647 and it is easy to associate call sites in the IPA pass summaries. */
648 while (edge->next_callee)
649 edge = edge->next_callee;
650 for (; edge; edge = edge->prev_callee)
651 lto_output_edge (ob, edge, encoder);
654 /* Output the part of the cgraph in SET. */
656 static void
657 output_refs (lto_symtab_encoder_t encoder)
659 lto_symtab_encoder_iterator lsei;
660 struct lto_simple_output_block *ob;
661 int count;
662 struct ipa_ref *ref;
663 int i;
665 ob = lto_create_simple_output_block (LTO_section_refs);
667 for (lsei = lsei_start_in_partition (encoder); !lsei_end_p (lsei);
668 lsei_next_in_partition (&lsei))
670 symtab_node node = lsei_node (lsei);
672 count = ipa_ref_list_nreferences (&node->symbol.ref_list);
673 if (count)
675 streamer_write_uhwi_stream (ob->main_stream, count);
676 streamer_write_uhwi_stream (ob->main_stream,
677 lto_symtab_encoder_lookup (encoder, node));
678 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list,
679 i, ref); i++)
680 lto_output_ref (ob, ref, encoder);
684 streamer_write_uhwi_stream (ob->main_stream, 0);
686 lto_destroy_simple_output_block (ob);
689 /* Add NODE into encoder as well as nodes it is cloned from.
690 Do it in a way so clones appear first. */
692 static void
693 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
694 bool include_body)
696 if (node->clone_of)
697 add_node_to (encoder, node->clone_of, include_body);
698 else if (include_body)
699 lto_set_symtab_encoder_encode_body (encoder, node);
700 lto_symtab_encoder_encode (encoder, (symtab_node)node);
703 /* Add all references in LIST to encoders. */
705 static void
706 add_references (lto_symtab_encoder_t encoder,
707 struct ipa_ref_list *list)
709 int i;
710 struct ipa_ref *ref;
711 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
712 if (is_a <cgraph_node> (ref->referred))
713 add_node_to (encoder, ipa_ref_node (ref), false);
714 else
715 lto_symtab_encoder_encode (encoder, ref->referred);
718 /* Find all symbols we want to stream into given partition and insert them
719 to encoders.
721 The function actually replaces IN_ENCODER by new one. The reason is that
722 streaming code needs clone's origin to be streamed before clone. This
723 means that we need to insert the nodes in specific order. This order is
724 ignored by the partitioning logic earlier. */
726 lto_symtab_encoder_t
727 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
729 struct cgraph_node *node;
730 struct cgraph_edge *edge;
731 int i;
732 lto_symtab_encoder_t encoder;
733 lto_symtab_encoder_iterator lsei;
735 encoder = lto_symtab_encoder_new (false);
737 /* Go over all entries in the IN_ENCODER and duplicate them to
738 ENCODER. At the same time insert masters of clones so
739 every master appears before clone. */
740 for (lsei = lsei_start_function_in_partition (in_encoder);
741 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
743 node = lsei_cgraph_node (lsei);
744 add_node_to (encoder, node, true);
745 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)node);
746 add_references (encoder, &node->symbol.ref_list);
748 for (lsei = lsei_start_variable_in_partition (in_encoder);
749 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
751 struct varpool_node *vnode = lsei_varpool_node (lsei);
752 gcc_assert (!vnode->alias || vnode->alias_of);
753 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)vnode);
754 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
755 add_references (encoder, &vnode->symbol.ref_list);
757 /* Pickle in also the initializer of all referenced readonly variables
758 to help folding. Constant pool variables are not shared, so we must
759 pickle those too. */
760 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
762 symtab_node node = lto_symtab_encoder_deref (encoder, i);
763 if (varpool_node *vnode = dyn_cast <varpool_node> (node))
765 if (DECL_INITIAL (vnode->symbol.decl)
766 && !lto_symtab_encoder_encode_initializer_p (encoder,
767 vnode)
768 && const_value_known_p (vnode->symbol.decl))
770 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
771 add_references (encoder, &vnode->symbol.ref_list);
776 /* Go over all the nodes again to include callees that are not in
777 SET. */
778 for (lsei = lsei_start_function_in_partition (encoder);
779 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
781 node = lsei_cgraph_node (lsei);
782 for (edge = node->callees; edge; edge = edge->next_callee)
784 struct cgraph_node *callee = edge->callee;
785 if (!lto_symtab_encoder_in_partition_p (encoder, (symtab_node)callee))
787 /* We should have moved all the inlines. */
788 gcc_assert (!callee->global.inlined_to);
789 add_node_to (encoder, callee, false);
793 lto_symtab_encoder_delete (in_encoder);
794 return encoder;
797 /* Output the part of the symtab in SET and VSET. */
799 void
800 output_symtab (void)
802 struct cgraph_node *node;
803 struct lto_simple_output_block *ob;
804 lto_symtab_encoder_iterator lsei;
805 int i, n_nodes;
806 lto_symtab_encoder_t encoder;
807 static bool asm_nodes_output = false;
809 if (flag_wpa)
810 output_cgraph_opt_summary ();
812 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
814 output_profile_summary (ob);
816 /* An encoder for cgraph nodes should have been created by
817 ipa_write_summaries_1. */
818 gcc_assert (ob->decl_state->symtab_node_encoder);
819 encoder = ob->decl_state->symtab_node_encoder;
821 /* Write out the nodes. We must first output a node and then its clones,
822 otherwise at a time reading back the node there would be nothing to clone
823 from. */
824 n_nodes = lto_symtab_encoder_size (encoder);
825 for (i = 0; i < n_nodes; i++)
827 symtab_node node = lto_symtab_encoder_deref (encoder, i);
828 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
829 lto_output_node (ob, cnode, encoder);
830 else
831 lto_output_varpool_node (ob, varpool (node), encoder);
835 /* Go over the nodes in SET again to write edges. */
836 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
837 lsei_next_function_in_partition (&lsei))
839 node = lsei_cgraph_node (lsei);
840 output_outgoing_cgraph_edges (node->callees, ob, encoder);
841 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
844 streamer_write_uhwi_stream (ob->main_stream, 0);
846 lto_destroy_simple_output_block (ob);
848 /* Emit toplevel asms.
849 When doing WPA we must output every asm just once. Since we do not partition asm
850 nodes at all, output them to first output. This is kind of hack, but should work
851 well. */
852 if (!asm_nodes_output)
854 asm_nodes_output = true;
855 lto_output_toplevel_asms ();
858 output_refs (encoder);
861 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
862 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
863 NODE or to replace the values in it, for instance because the first
864 time we saw it, the function body was not available but now it
865 is. BP is a bitpack with all the bitflags for NODE read from the
866 stream. */
868 static void
869 input_overwrite_node (struct lto_file_decl_data *file_data,
870 struct cgraph_node *node,
871 enum LTO_symtab_tags tag,
872 struct bitpack_d *bp)
874 node->symbol.aux = (void *) tag;
875 node->symbol.lto_file_data = file_data;
877 node->local.local = bp_unpack_value (bp, 1);
878 node->symbol.externally_visible = bp_unpack_value (bp, 1);
879 node->local.finalized = bp_unpack_value (bp, 1);
880 node->local.versionable = bp_unpack_value (bp, 1);
881 node->local.can_change_signature = bp_unpack_value (bp, 1);
882 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
883 node->symbol.force_output = bp_unpack_value (bp, 1);
884 node->symbol.address_taken = bp_unpack_value (bp, 1);
885 node->abstract_and_needed = bp_unpack_value (bp, 1);
886 node->symbol.used_from_other_partition = bp_unpack_value (bp, 1);
887 node->lowered = bp_unpack_value (bp, 1);
888 node->analyzed = tag == LTO_symtab_analyzed_node;
889 node->symbol.in_other_partition = bp_unpack_value (bp, 1);
890 if (node->symbol.in_other_partition
891 /* Avoid updating decl when we are seeing just inline clone.
892 When inlining function that has functions already inlined into it,
893 we produce clones of inline clones.
895 WPA partitioning might put each clone into different unit and
896 we might end up streaming inline clone from other partition
897 to support clone we are interested in. */
898 && (!node->clone_of
899 || node->clone_of->symbol.decl != node->symbol.decl))
901 DECL_EXTERNAL (node->symbol.decl) = 1;
902 TREE_STATIC (node->symbol.decl) = 0;
904 node->alias = bp_unpack_value (bp, 1);
905 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
906 node->only_called_at_startup = bp_unpack_value (bp, 1);
907 node->only_called_at_exit = bp_unpack_value (bp, 1);
908 node->tm_clone = bp_unpack_value (bp, 1);
909 node->thunk.thunk_p = bp_unpack_value (bp, 1);
910 node->symbol.resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
911 LDPR_NUM_KNOWN);
914 /* Read a node from input_block IB. TAG is the node's tag just read.
915 Return the node read or overwriten. */
917 static struct cgraph_node *
918 input_node (struct lto_file_decl_data *file_data,
919 struct lto_input_block *ib,
920 enum LTO_symtab_tags tag,
921 vec<symtab_node> nodes)
923 tree fn_decl;
924 struct cgraph_node *node;
925 struct bitpack_d bp;
926 unsigned decl_index;
927 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
928 int clone_ref;
929 int order;
930 int i, count;
932 order = streamer_read_hwi (ib) + order_base;
933 clone_ref = streamer_read_hwi (ib);
935 decl_index = streamer_read_uhwi (ib);
936 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
938 if (clone_ref != LCC_NOT_FOUND)
940 node = cgraph_clone_node (cgraph (nodes[clone_ref]), fn_decl,
941 0, CGRAPH_FREQ_BASE, false,
942 vNULL, false);
944 else
945 node = cgraph_get_create_node (fn_decl);
947 node->symbol.order = order;
948 if (order >= symtab_order)
949 symtab_order = order + 1;
951 node->count = streamer_read_hwi (ib);
952 node->count_materialization_scale = streamer_read_hwi (ib);
954 count = streamer_read_hwi (ib);
955 node->ipa_transforms_to_apply = vNULL;
956 for (i = 0; i < count; i++)
958 struct opt_pass *pass;
959 int pid = streamer_read_hwi (ib);
961 gcc_assert (pid < passes_by_id_size);
962 pass = passes_by_id[pid];
963 node->ipa_transforms_to_apply.safe_push ((struct ipa_opt_pass_d *) pass);
966 if (tag == LTO_symtab_analyzed_node)
967 ref = streamer_read_hwi (ib);
969 ref2 = streamer_read_hwi (ib);
971 /* Make sure that we have not read this node before. Nodes that
972 have already been read will have their tag stored in the 'aux'
973 field. Since built-in functions can be referenced in multiple
974 functions, they are expected to be read more than once. */
975 if (node->symbol.aux && !DECL_BUILT_IN (node->symbol.decl))
976 internal_error ("bytecode stream: found multiple instances of cgraph "
977 "node %d", node->uid);
979 bp = streamer_read_bitpack (ib);
980 input_overwrite_node (file_data, node, tag, &bp);
982 /* Store a reference for now, and fix up later to be a pointer. */
983 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
985 /* Store a reference for now, and fix up later to be a pointer. */
986 node->symbol.same_comdat_group = (symtab_node) (intptr_t) ref2;
988 if (node->thunk.thunk_p)
990 int type = streamer_read_uhwi (ib);
991 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
992 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
994 node->thunk.fixed_offset = fixed_offset;
995 node->thunk.this_adjusting = (type & 2);
996 node->thunk.virtual_value = virtual_value;
997 node->thunk.virtual_offset_p = (type & 4);
999 if (node->thunk.thunk_p || node->alias)
1001 if (streamer_read_hwi_in_range (ib, "alias nonzero flag", 0, 1))
1003 decl_index = streamer_read_uhwi (ib);
1004 node->thunk.alias = lto_file_decl_data_get_fn_decl (file_data,
1005 decl_index);
1008 return node;
1011 /* Read a node from input_block IB. TAG is the node's tag just read.
1012 Return the node read or overwriten. */
1014 static struct varpool_node *
1015 input_varpool_node (struct lto_file_decl_data *file_data,
1016 struct lto_input_block *ib)
1018 int decl_index;
1019 tree var_decl;
1020 struct varpool_node *node;
1021 struct bitpack_d bp;
1022 int ref = LCC_NOT_FOUND;
1023 bool non_null_aliasof;
1024 int order;
1026 order = streamer_read_hwi (ib) + order_base;
1027 decl_index = streamer_read_uhwi (ib);
1028 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1029 node = varpool_node_for_decl (var_decl);
1030 node->symbol.order = order;
1031 if (order >= symtab_order)
1032 symtab_order = order + 1;
1033 node->symbol.lto_file_data = file_data;
1035 bp = streamer_read_bitpack (ib);
1036 node->symbol.externally_visible = bp_unpack_value (&bp, 1);
1037 node->symbol.force_output = bp_unpack_value (&bp, 1);
1038 node->finalized = bp_unpack_value (&bp, 1);
1039 node->alias = bp_unpack_value (&bp, 1);
1040 non_null_aliasof = bp_unpack_value (&bp, 1);
1041 node->symbol.used_from_other_partition = bp_unpack_value (&bp, 1);
1042 node->symbol.in_other_partition = bp_unpack_value (&bp, 1);
1043 node->analyzed = (node->finalized && (!node->alias || !node->symbol.in_other_partition));
1044 if (node->symbol.in_other_partition)
1046 DECL_EXTERNAL (node->symbol.decl) = 1;
1047 TREE_STATIC (node->symbol.decl) = 0;
1049 if (non_null_aliasof)
1051 decl_index = streamer_read_uhwi (ib);
1052 node->alias_of = lto_file_decl_data_get_var_decl (file_data, decl_index);
1054 ref = streamer_read_hwi (ib);
1055 /* Store a reference for now, and fix up later to be a pointer. */
1056 node->symbol.same_comdat_group = (symtab_node) (intptr_t) ref;
1057 node->symbol.resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1058 LDPR_NUM_KNOWN);
1060 return node;
1063 /* Read a node from input_block IB. TAG is the node's tag just read.
1064 Return the node read or overwriten. */
1066 static void
1067 input_ref (struct lto_input_block *ib,
1068 symtab_node referring_node,
1069 vec<symtab_node> nodes)
1071 symtab_node node = NULL;
1072 struct bitpack_d bp;
1073 enum ipa_ref_use use;
1075 bp = streamer_read_bitpack (ib);
1076 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1077 node = nodes[streamer_read_hwi (ib)];
1078 ipa_record_reference (referring_node, node, use, NULL);
1081 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1082 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1083 edge being read is indirect (in the sense that it has
1084 indirect_unknown_callee set). */
1086 static void
1087 input_edge (struct lto_input_block *ib, vec<symtab_node> nodes,
1088 bool indirect)
1090 struct cgraph_node *caller, *callee;
1091 struct cgraph_edge *edge;
1092 unsigned int stmt_id;
1093 gcov_type count;
1094 int freq;
1095 cgraph_inline_failed_t inline_failed;
1096 struct bitpack_d bp;
1097 int ecf_flags = 0;
1099 caller = cgraph (nodes[streamer_read_hwi (ib)]);
1100 if (caller == NULL || caller->symbol.decl == NULL_TREE)
1101 internal_error ("bytecode stream: no caller found while reading edge");
1103 if (!indirect)
1105 callee = cgraph (nodes[streamer_read_hwi (ib)]);
1106 if (callee == NULL || callee->symbol.decl == NULL_TREE)
1107 internal_error ("bytecode stream: no callee found while reading edge");
1109 else
1110 callee = NULL;
1112 count = (gcov_type) streamer_read_hwi (ib);
1114 bp = streamer_read_bitpack (ib);
1115 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_enum, CIF_N_REASONS);
1116 stmt_id = bp_unpack_var_len_unsigned (&bp);
1117 freq = (int) bp_unpack_var_len_unsigned (&bp);
1119 if (indirect)
1120 edge = cgraph_create_indirect_edge (caller, NULL, 0, count, freq);
1121 else
1122 edge = cgraph_create_edge (caller, callee, NULL, count, freq);
1124 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1125 edge->lto_stmt_uid = stmt_id;
1126 edge->inline_failed = inline_failed;
1127 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1128 edge->can_throw_external = bp_unpack_value (&bp, 1);
1129 if (indirect)
1131 if (bp_unpack_value (&bp, 1))
1132 ecf_flags |= ECF_CONST;
1133 if (bp_unpack_value (&bp, 1))
1134 ecf_flags |= ECF_PURE;
1135 if (bp_unpack_value (&bp, 1))
1136 ecf_flags |= ECF_NORETURN;
1137 if (bp_unpack_value (&bp, 1))
1138 ecf_flags |= ECF_MALLOC;
1139 if (bp_unpack_value (&bp, 1))
1140 ecf_flags |= ECF_NOTHROW;
1141 if (bp_unpack_value (&bp, 1))
1142 ecf_flags |= ECF_RETURNS_TWICE;
1143 edge->indirect_info->ecf_flags = ecf_flags;
1148 /* Read a cgraph from IB using the info in FILE_DATA. */
1150 static vec<symtab_node>
1151 input_cgraph_1 (struct lto_file_decl_data *file_data,
1152 struct lto_input_block *ib)
1154 enum LTO_symtab_tags tag;
1155 vec<symtab_node> nodes = vNULL;
1156 symtab_node node;
1157 unsigned i;
1159 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1160 order_base = symtab_order;
1161 while (tag)
1163 if (tag == LTO_symtab_edge)
1164 input_edge (ib, nodes, false);
1165 else if (tag == LTO_symtab_indirect_edge)
1166 input_edge (ib, nodes, true);
1167 else if (tag == LTO_symtab_variable)
1169 node = (symtab_node)input_varpool_node (file_data, ib);
1170 nodes.safe_push (node);
1171 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1173 else
1175 node = (symtab_node)input_node (file_data, ib, tag, nodes);
1176 if (node == NULL || node->symbol.decl == NULL_TREE)
1177 internal_error ("bytecode stream: found empty cgraph node");
1178 nodes.safe_push (node);
1179 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1182 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1185 lto_input_toplevel_asms (file_data, order_base);
1187 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1188 #ifdef ENABLE_CHECKING
1189 FOR_EACH_VEC_ELT (nodes, i, node)
1190 gcc_assert (node->symbol.aux || !is_a <cgraph_node> (node));
1191 #endif
1192 FOR_EACH_VEC_ELT (nodes, i, node)
1194 int ref;
1195 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
1197 ref = (int) (intptr_t) cnode->global.inlined_to;
1199 /* We share declaration of builtins, so we may read same node twice. */
1200 if (!node->symbol.aux)
1201 continue;
1202 node->symbol.aux = NULL;
1204 /* Fixup inlined_to from reference to pointer. */
1205 if (ref != LCC_NOT_FOUND)
1206 cgraph (node)->global.inlined_to = cgraph (nodes[ref]);
1207 else
1208 cnode->global.inlined_to = NULL;
1211 ref = (int) (intptr_t) node->symbol.same_comdat_group;
1213 /* Fixup same_comdat_group from reference to pointer. */
1214 if (ref != LCC_NOT_FOUND)
1215 node->symbol.same_comdat_group = nodes[ref];
1216 else
1217 node->symbol.same_comdat_group = NULL;
1219 FOR_EACH_VEC_ELT (nodes, i, node)
1220 node->symbol.aux = is_a <cgraph_node> (node) ? (void *)1 : NULL;
1221 return nodes;
1224 /* Input ipa_refs. */
1226 static void
1227 input_refs (struct lto_input_block *ib,
1228 vec<symtab_node> nodes)
1230 int count;
1231 int idx;
1232 while (true)
1234 symtab_node node;
1235 count = streamer_read_uhwi (ib);
1236 if (!count)
1237 break;
1238 idx = streamer_read_uhwi (ib);
1239 node = nodes[idx];
1240 while (count)
1242 input_ref (ib, node, nodes);
1243 count--;
1249 static struct gcov_ctr_summary lto_gcov_summary;
1251 /* Input profile_info from IB. */
1252 static void
1253 input_profile_summary (struct lto_input_block *ib,
1254 struct lto_file_decl_data *file_data)
1256 unsigned h_ix;
1257 struct bitpack_d bp;
1258 unsigned int runs = streamer_read_uhwi (ib);
1259 if (runs)
1261 file_data->profile_info.runs = runs;
1262 file_data->profile_info.sum_max = streamer_read_uhwi (ib);
1263 file_data->profile_info.sum_all = streamer_read_uhwi (ib);
1265 memset (file_data->profile_info.histogram, 0,
1266 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1267 /* Input the bitpack of non-zero histogram indices. */
1268 bp = streamer_read_bitpack (ib);
1269 /* Read in and unpack the full bitpack, flagging non-zero
1270 histogram entries by setting the num_counters non-zero. */
1271 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1273 file_data->profile_info.histogram[h_ix].num_counters
1274 = bp_unpack_value (&bp, 1);
1276 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1278 if (!file_data->profile_info.histogram[h_ix].num_counters)
1279 continue;
1281 file_data->profile_info.histogram[h_ix].num_counters
1282 = streamer_read_uhwi (ib);
1283 file_data->profile_info.histogram[h_ix].min_value
1284 = streamer_read_uhwi (ib);
1285 file_data->profile_info.histogram[h_ix].cum_value
1286 = streamer_read_uhwi (ib);
1292 /* Rescale profile summaries to the same number of runs in the whole unit. */
1294 static void
1295 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1297 struct lto_file_decl_data *file_data;
1298 unsigned int j, h_ix;
1299 gcov_unsigned_t max_runs = 0;
1300 struct cgraph_node *node;
1301 struct cgraph_edge *edge;
1302 gcov_type saved_sum_all = 0;
1303 gcov_ctr_summary *saved_profile_info = 0;
1304 int saved_scale = 0;
1306 /* Find unit with maximal number of runs. If we ever get serious about
1307 roundoff errors, we might also consider computing smallest common
1308 multiply. */
1309 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1310 if (max_runs < file_data->profile_info.runs)
1311 max_runs = file_data->profile_info.runs;
1313 if (!max_runs)
1314 return;
1316 /* Simple overflow check. We probably don't need to support that many train
1317 runs. Such a large value probably imply data corruption anyway. */
1318 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1320 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1321 INT_MAX / REG_BR_PROB_BASE);
1322 return;
1325 profile_info = &lto_gcov_summary;
1326 lto_gcov_summary.runs = max_runs;
1327 lto_gcov_summary.sum_max = 0;
1328 memset (lto_gcov_summary.histogram, 0,
1329 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1331 /* Rescale all units to the maximal number of runs.
1332 sum_max can not be easily merged, as we have no idea what files come from
1333 the same run. We do not use the info anyway, so leave it 0. */
1334 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1335 if (file_data->profile_info.runs)
1337 int scale = RDIV (REG_BR_PROB_BASE * max_runs,
1338 file_data->profile_info.runs);
1339 lto_gcov_summary.sum_max = MAX (lto_gcov_summary.sum_max,
1340 RDIV (file_data->profile_info.sum_max
1341 * scale, REG_BR_PROB_BASE));
1342 lto_gcov_summary.sum_all = MAX (lto_gcov_summary.sum_all,
1343 RDIV (file_data->profile_info.sum_all
1344 * scale, REG_BR_PROB_BASE));
1345 /* Save a pointer to the profile_info with the largest
1346 scaled sum_all and the scale for use in merging the
1347 histogram. */
1348 if (!saved_profile_info
1349 || lto_gcov_summary.sum_all > saved_sum_all)
1351 saved_profile_info = &file_data->profile_info;
1352 saved_sum_all = lto_gcov_summary.sum_all;
1353 saved_scale = scale;
1357 gcc_assert (saved_profile_info);
1359 /* Scale up the histogram from the profile that had the largest
1360 scaled sum_all above. */
1361 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1363 /* Scale up the min value as we did the corresponding sum_all
1364 above. Use that to find the new histogram index. */
1365 int scaled_min = RDIV (saved_profile_info->histogram[h_ix].min_value
1366 * saved_scale, REG_BR_PROB_BASE);
1367 /* The new index may be shared with another scaled histogram entry,
1368 so we need to account for a non-zero histogram entry at new_ix. */
1369 unsigned new_ix = gcov_histo_index (scaled_min);
1370 lto_gcov_summary.histogram[new_ix].min_value
1371 = (lto_gcov_summary.histogram[new_ix].num_counters
1372 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1373 : scaled_min);
1374 /* Some of the scaled counter values would ostensibly need to be placed
1375 into different (larger) histogram buckets, but we keep things simple
1376 here and place the scaled cumulative counter value in the bucket
1377 corresponding to the scaled minimum counter value. */
1378 lto_gcov_summary.histogram[new_ix].cum_value
1379 += RDIV (saved_profile_info->histogram[h_ix].cum_value
1380 * saved_scale, REG_BR_PROB_BASE);
1381 lto_gcov_summary.histogram[new_ix].num_counters
1382 += saved_profile_info->histogram[h_ix].num_counters;
1385 /* Watch roundoff errors. */
1386 if (lto_gcov_summary.sum_max < max_runs)
1387 lto_gcov_summary.sum_max = max_runs;
1389 /* If merging already happent at WPA time, we are done. */
1390 if (flag_ltrans)
1391 return;
1393 /* Now compute count_materialization_scale of each node.
1394 During LTRANS we already have values of count_materialization_scale
1395 computed, so just update them. */
1396 FOR_EACH_FUNCTION (node)
1397 if (node->symbol.lto_file_data
1398 && node->symbol.lto_file_data->profile_info.runs)
1400 int scale;
1402 scale = RDIV (node->count_materialization_scale * max_runs,
1403 node->symbol.lto_file_data->profile_info.runs);
1404 node->count_materialization_scale = scale;
1405 if (scale < 0)
1406 fatal_error ("Profile information in %s corrupted",
1407 file_data->file_name);
1409 if (scale == REG_BR_PROB_BASE)
1410 continue;
1411 for (edge = node->callees; edge; edge = edge->next_callee)
1412 edge->count = RDIV (edge->count * scale, REG_BR_PROB_BASE);
1413 node->count = RDIV (node->count * scale, REG_BR_PROB_BASE);
1417 /* Input and merge the symtab from each of the .o files passed to
1418 lto1. */
1420 void
1421 input_symtab (void)
1423 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1424 struct lto_file_decl_data *file_data;
1425 unsigned int j = 0;
1426 struct cgraph_node *node;
1428 cgraph_state = CGRAPH_STATE_IPA_SSA;
1430 while ((file_data = file_data_vec[j++]))
1432 const char *data;
1433 size_t len;
1434 struct lto_input_block *ib;
1435 vec<symtab_node> nodes;
1437 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1438 &data, &len);
1439 if (!ib)
1440 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1441 input_profile_summary (ib, file_data);
1442 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1443 nodes = input_cgraph_1 (file_data, ib);
1444 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1445 ib, data, len);
1447 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1448 &data, &len);
1449 if (!ib)
1450 fatal_error("cannot find LTO section refs in %s", file_data->file_name);
1451 input_refs (ib, nodes);
1452 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1453 ib, data, len);
1454 if (flag_ltrans)
1455 input_cgraph_opt_summary (nodes);
1456 nodes.release ();
1459 merge_profile_summaries (file_data_vec);
1460 compute_working_sets ();
1463 /* Clear out the aux field that was used to store enough state to
1464 tell which nodes should be overwritten. */
1465 FOR_EACH_FUNCTION (node)
1467 /* Some nodes may have been created by cgraph_node. This
1468 happens when the callgraph contains nested functions. If the
1469 node for the parent function was never emitted to the gimple
1470 file, cgraph_node will create a node for it when setting the
1471 context of the nested function. */
1472 if (node->symbol.lto_file_data)
1473 node->symbol.aux = NULL;
1477 /* True when we need optimization summary for NODE. */
1479 static int
1480 output_cgraph_opt_summary_p (struct cgraph_node *node)
1482 return (node->clone_of
1483 && (node->clone.tree_map
1484 || node->clone.args_to_skip
1485 || node->clone.combined_args_to_skip));
1488 /* Output optimization summary for EDGE to OB. */
1489 static void
1490 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1491 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1495 /* Output optimization summary for NODE to OB. */
1497 static void
1498 output_node_opt_summary (struct output_block *ob,
1499 struct cgraph_node *node,
1500 lto_symtab_encoder_t encoder)
1502 unsigned int index;
1503 bitmap_iterator bi;
1504 struct ipa_replace_map *map;
1505 struct bitpack_d bp;
1506 int i;
1507 struct cgraph_edge *e;
1509 if (node->clone.args_to_skip)
1511 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1512 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1513 streamer_write_uhwi (ob, index);
1515 else
1516 streamer_write_uhwi (ob, 0);
1517 if (node->clone.combined_args_to_skip)
1519 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1520 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1521 streamer_write_uhwi (ob, index);
1523 else
1524 streamer_write_uhwi (ob, 0);
1525 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1526 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1528 int parm_num;
1529 tree parm;
1531 for (parm_num = 0, parm = DECL_ARGUMENTS (node->symbol.decl); parm;
1532 parm = DECL_CHAIN (parm), parm_num++)
1533 if (map->old_tree == parm)
1534 break;
1535 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1536 mechanism to store function local declarations into summaries. */
1537 gcc_assert (parm);
1538 streamer_write_uhwi (ob, parm_num);
1539 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1540 stream_write_tree (ob, map->new_tree, true);
1541 bp = bitpack_create (ob->main_stream);
1542 bp_pack_value (&bp, map->replace_p, 1);
1543 bp_pack_value (&bp, map->ref_p, 1);
1544 streamer_write_bitpack (&bp);
1547 if (lto_symtab_encoder_in_partition_p (encoder, (symtab_node) node))
1549 for (e = node->callees; e; e = e->next_callee)
1550 output_edge_opt_summary (ob, e);
1551 for (e = node->indirect_calls; e; e = e->next_callee)
1552 output_edge_opt_summary (ob, e);
1556 /* Output optimization summaries stored in callgraph.
1557 At the moment it is the clone info structure. */
1559 static void
1560 output_cgraph_opt_summary (void)
1562 int i, n_nodes;
1563 lto_symtab_encoder_t encoder;
1564 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1565 unsigned count = 0;
1567 ob->cgraph_node = NULL;
1568 encoder = ob->decl_state->symtab_node_encoder;
1569 n_nodes = lto_symtab_encoder_size (encoder);
1570 for (i = 0; i < n_nodes; i++)
1572 symtab_node node = lto_symtab_encoder_deref (encoder, i);
1573 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1574 if (cnode && output_cgraph_opt_summary_p (cnode))
1575 count++;
1577 streamer_write_uhwi (ob, count);
1578 for (i = 0; i < n_nodes; i++)
1580 symtab_node node = lto_symtab_encoder_deref (encoder, i);
1581 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1582 if (cnode && output_cgraph_opt_summary_p (cnode))
1584 streamer_write_uhwi (ob, i);
1585 output_node_opt_summary (ob, cnode, encoder);
1588 produce_asm (ob, NULL);
1589 destroy_output_block (ob);
1592 /* Input optimisation summary of EDGE. */
1594 static void
1595 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
1596 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
1600 /* Input optimisation summary of NODE. */
1602 static void
1603 input_node_opt_summary (struct cgraph_node *node,
1604 struct lto_input_block *ib_main,
1605 struct data_in *data_in)
1607 int i;
1608 int count;
1609 int bit;
1610 struct bitpack_d bp;
1611 struct cgraph_edge *e;
1613 count = streamer_read_uhwi (ib_main);
1614 if (count)
1615 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1616 for (i = 0; i < count; i++)
1618 bit = streamer_read_uhwi (ib_main);
1619 bitmap_set_bit (node->clone.args_to_skip, bit);
1621 count = streamer_read_uhwi (ib_main);
1622 if (count)
1623 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1624 for (i = 0; i < count; i++)
1626 bit = streamer_read_uhwi (ib_main);
1627 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1629 count = streamer_read_uhwi (ib_main);
1630 for (i = 0; i < count; i++)
1632 struct ipa_replace_map *map = ggc_alloc_ipa_replace_map ();
1634 vec_safe_push (node->clone.tree_map, map);
1635 map->parm_num = streamer_read_uhwi (ib_main);
1636 map->old_tree = NULL;
1637 map->new_tree = stream_read_tree (ib_main, data_in);
1638 bp = streamer_read_bitpack (ib_main);
1639 map->replace_p = bp_unpack_value (&bp, 1);
1640 map->ref_p = bp_unpack_value (&bp, 1);
1642 for (e = node->callees; e; e = e->next_callee)
1643 input_edge_opt_summary (e, ib_main);
1644 for (e = node->indirect_calls; e; e = e->next_callee)
1645 input_edge_opt_summary (e, ib_main);
1648 /* Read section in file FILE_DATA of length LEN with data DATA. */
1650 static void
1651 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1652 const char *data, size_t len,
1653 vec<symtab_node> nodes)
1655 const struct lto_function_header *header =
1656 (const struct lto_function_header *) data;
1657 const int cfg_offset = sizeof (struct lto_function_header);
1658 const int main_offset = cfg_offset + header->cfg_size;
1659 const int string_offset = main_offset + header->main_size;
1660 struct data_in *data_in;
1661 struct lto_input_block ib_main;
1662 unsigned int i;
1663 unsigned int count;
1665 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1666 header->main_size);
1668 data_in =
1669 lto_data_in_create (file_data, (const char *) data + string_offset,
1670 header->string_size, vNULL);
1671 count = streamer_read_uhwi (&ib_main);
1673 for (i = 0; i < count; i++)
1675 int ref = streamer_read_uhwi (&ib_main);
1676 input_node_opt_summary (cgraph (nodes[ref]),
1677 &ib_main, data_in);
1679 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1680 len);
1681 lto_data_in_delete (data_in);
1684 /* Input optimization summary of cgraph. */
1686 static void
1687 input_cgraph_opt_summary (vec<symtab_node> nodes)
1689 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1690 struct lto_file_decl_data *file_data;
1691 unsigned int j = 0;
1693 while ((file_data = file_data_vec[j++]))
1695 size_t len;
1696 const char *data =
1697 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1698 &len);
1700 if (data)
1701 input_cgraph_opt_section (file_data, data, len, nodes);