Merge trunk version 201119 into gupc branch.
[official-gcc.git] / gcc / lto-cgraph.c
blob4a287f65463f9f7888c591ce04257411403a18e6
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"
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_gcov_count_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->symbol.definition)
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;
380 bool alias_p;
382 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, (symtab_node)node);
384 if (node->symbol.analyzed && !boundary_p)
385 tag = LTO_symtab_analyzed_node;
386 else
387 tag = LTO_symtab_unavail_node;
389 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
390 tag);
391 streamer_write_hwi_stream (ob->main_stream, node->symbol.order);
393 /* In WPA mode, we only output part of the call-graph. Also, we
394 fake cgraph node attributes. There are two cases that we care.
396 Boundary nodes: There are nodes that are not part of SET but are
397 called from within SET. We artificially make them look like
398 externally visible nodes with no function body.
400 Cherry-picked nodes: These are nodes we pulled from other
401 translation units into SET during IPA-inlining. We make them as
402 local static nodes to prevent clashes with other local statics. */
403 if (boundary_p && node->symbol.analyzed && !DECL_EXTERNAL (node->symbol.decl))
405 /* Inline clones can not be part of boundary.
406 gcc_assert (!node->global.inlined_to);
408 FIXME: At the moment they can be, when partition contains an inline
409 clone that is clone of inline clone from outside partition. We can
410 reshape the clone tree and make other tree to be the root, but it
411 needs a bit extra work and will be promplty done by cgraph_remove_node
412 after reading back. */
413 in_other_partition = 1;
416 clone_of = node->clone_of;
417 while (clone_of
418 && (ref = lto_symtab_encoder_lookup (encoder, (symtab_node)clone_of)) == LCC_NOT_FOUND)
419 if (clone_of->prev_sibling_clone)
420 clone_of = clone_of->prev_sibling_clone;
421 else
422 clone_of = clone_of->clone_of;
424 if (LTO_symtab_analyzed_node)
425 gcc_assert (clone_of || !node->clone_of);
426 if (!clone_of)
427 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
428 else
429 streamer_write_hwi_stream (ob->main_stream, ref);
432 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->symbol.decl);
433 streamer_write_gcov_count_stream (ob->main_stream, node->count);
434 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
436 streamer_write_hwi_stream (ob->main_stream,
437 node->ipa_transforms_to_apply.length ());
438 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
439 streamer_write_hwi_stream (ob->main_stream, pass->pass.static_pass_number);
441 if (tag == LTO_symtab_analyzed_node)
443 if (node->global.inlined_to)
445 ref = lto_symtab_encoder_lookup (encoder, (symtab_node)node->global.inlined_to);
446 gcc_assert (ref != LCC_NOT_FOUND);
448 else
449 ref = LCC_NOT_FOUND;
451 streamer_write_hwi_stream (ob->main_stream, ref);
454 if (node->symbol.same_comdat_group && !boundary_p)
456 ref = lto_symtab_encoder_lookup (encoder,
457 node->symbol.same_comdat_group);
458 gcc_assert (ref != LCC_NOT_FOUND);
460 else
461 ref = LCC_NOT_FOUND;
462 streamer_write_hwi_stream (ob->main_stream, ref);
464 bp = bitpack_create (ob->main_stream);
465 bp_pack_value (&bp, node->local.local, 1);
466 bp_pack_value (&bp, node->symbol.externally_visible, 1);
467 bp_pack_value (&bp, node->symbol.definition, 1);
468 bp_pack_value (&bp, node->local.versionable, 1);
469 bp_pack_value (&bp, node->local.can_change_signature, 1);
470 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
471 bp_pack_value (&bp, node->symbol.force_output, 1);
472 bp_pack_value (&bp, node->symbol.forced_by_abi, 1);
473 bp_pack_value (&bp, node->symbol.unique_name, 1);
474 bp_pack_value (&bp, node->symbol.address_taken, 1);
475 bp_pack_value (&bp, node->abstract_and_needed, 1);
476 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
477 && !DECL_EXTERNAL (node->symbol.decl)
478 && !DECL_COMDAT (node->symbol.decl)
479 && (reachable_from_other_partition_p (node, encoder)
480 || referenced_from_other_partition_p (&node->symbol.ref_list,
481 encoder)), 1);
482 bp_pack_value (&bp, node->lowered, 1);
483 bp_pack_value (&bp, in_other_partition, 1);
484 /* Real aliases in a boundary become non-aliases. However we still stream
485 alias info on weakrefs.
486 TODO: We lose a bit of information here - when we know that variable is
487 defined in other unit, we may use the info on aliases to resolve
488 symbol1 != symbol2 type tests that we can do only for locally defined objects
489 otherwise. */
490 alias_p = node->symbol.alias && (!boundary_p || node->symbol.weakref);
491 bp_pack_value (&bp, alias_p, 1);
492 bp_pack_value (&bp, node->symbol.weakref, 1);
493 bp_pack_value (&bp, node->frequency, 2);
494 bp_pack_value (&bp, node->only_called_at_startup, 1);
495 bp_pack_value (&bp, node->only_called_at_exit, 1);
496 bp_pack_value (&bp, node->tm_clone, 1);
497 bp_pack_value (&bp, node->thunk.thunk_p && !boundary_p, 1);
498 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
499 LDPR_NUM_KNOWN, node->symbol.resolution);
500 streamer_write_bitpack (&bp);
502 if (node->thunk.thunk_p && !boundary_p)
504 streamer_write_uhwi_stream
505 (ob->main_stream,
506 1 + (node->thunk.this_adjusting != 0) * 2
507 + (node->thunk.virtual_offset_p != 0) * 4);
508 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
509 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
513 /* Output the varpool NODE to OB.
514 If NODE is not in SET, then NODE is a boundary. */
516 static void
517 lto_output_varpool_node (struct lto_simple_output_block *ob, struct varpool_node *node,
518 lto_symtab_encoder_t encoder)
520 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, (symtab_node)node);
521 struct bitpack_d bp;
522 int ref;
523 bool alias_p;
525 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
526 LTO_symtab_variable);
527 streamer_write_hwi_stream (ob->main_stream, node->symbol.order);
528 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->symbol.decl);
529 bp = bitpack_create (ob->main_stream);
530 bp_pack_value (&bp, node->symbol.externally_visible, 1);
531 bp_pack_value (&bp, node->symbol.force_output, 1);
532 bp_pack_value (&bp, node->symbol.forced_by_abi, 1);
533 bp_pack_value (&bp, node->symbol.unique_name, 1);
534 bp_pack_value (&bp, node->symbol.definition, 1);
535 alias_p = node->symbol.alias && (!boundary_p || node->symbol.weakref);
536 bp_pack_value (&bp, alias_p, 1);
537 bp_pack_value (&bp, node->symbol.weakref, 1);
538 bp_pack_value (&bp, node->symbol.analyzed && !boundary_p, 1);
539 gcc_assert (node->symbol.definition || !node->symbol.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->symbol.definition
553 && referenced_from_other_partition_p (&node->symbol.ref_list,
554 encoder), 1);
555 bp_pack_value (&bp, node->symbol.analyzed
556 && boundary_p && !DECL_EXTERNAL (node->symbol.decl), 1);
557 /* in_other_partition. */
559 streamer_write_bitpack (&bp);
560 if (node->symbol.same_comdat_group && !boundary_p)
562 ref = lto_symtab_encoder_lookup (encoder,
563 node->symbol.same_comdat_group);
564 gcc_assert (ref != LCC_NOT_FOUND);
566 else
567 ref = LCC_NOT_FOUND;
568 streamer_write_hwi_stream (ob->main_stream, ref);
569 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
570 LDPR_NUM_KNOWN, node->symbol.resolution);
573 /* Output the varpool NODE to OB.
574 If NODE is not in SET, then NODE is a boundary. */
576 static void
577 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
578 lto_symtab_encoder_t encoder)
580 struct bitpack_d bp;
581 int nref;
583 bp = bitpack_create (ob->main_stream);
584 bp_pack_value (&bp, ref->use, 2);
585 streamer_write_bitpack (&bp);
586 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
587 gcc_assert (nref != LCC_NOT_FOUND);
588 streamer_write_hwi_stream (ob->main_stream, nref);
591 /* Stream out profile_summary to OB. */
593 static void
594 output_profile_summary (struct lto_simple_output_block *ob)
596 unsigned h_ix;
597 struct bitpack_d bp;
599 if (profile_info)
601 /* We do not output num and run_max, they are not used by
602 GCC profile feedback and they are difficult to merge from multiple
603 units. */
604 gcc_assert (profile_info->runs);
605 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
606 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
608 /* sum_all is needed for computing the working set with the
609 histogram. */
610 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
612 /* Create and output a bitpack of non-zero histogram entries indices. */
613 bp = bitpack_create (ob->main_stream);
614 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
615 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
616 streamer_write_bitpack (&bp);
617 /* Now stream out only those non-zero entries. */
618 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
620 if (!profile_info->histogram[h_ix].num_counters)
621 continue;
622 streamer_write_gcov_count_stream (ob->main_stream,
623 profile_info->histogram[h_ix].num_counters);
624 streamer_write_gcov_count_stream (ob->main_stream,
625 profile_info->histogram[h_ix].min_value);
626 streamer_write_gcov_count_stream (ob->main_stream,
627 profile_info->histogram[h_ix].cum_value);
629 /* IPA-profile computes hot bb threshold based on cumulated
630 whole program profile. We need to stream it down to ltrans. */
631 if (flag_wpa)
632 streamer_write_gcov_count_stream (ob->main_stream,
633 get_hot_bb_threshold ());
635 else
636 streamer_write_uhwi_stream (ob->main_stream, 0);
639 /* Output all callees or indirect outgoing edges. EDGE must be the first such
640 edge. */
642 static void
643 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
644 struct lto_simple_output_block *ob,
645 lto_symtab_encoder_t encoder)
647 if (!edge)
648 return;
650 /* Output edges in backward direction, so the reconstructed callgraph match
651 and it is easy to associate call sites in the IPA pass summaries. */
652 while (edge->next_callee)
653 edge = edge->next_callee;
654 for (; edge; edge = edge->prev_callee)
655 lto_output_edge (ob, edge, encoder);
658 /* Output the part of the cgraph in SET. */
660 static void
661 output_refs (lto_symtab_encoder_t encoder)
663 lto_symtab_encoder_iterator lsei;
664 struct lto_simple_output_block *ob;
665 int count;
666 struct ipa_ref *ref;
667 int i;
669 ob = lto_create_simple_output_block (LTO_section_refs);
671 for (lsei = lsei_start_in_partition (encoder); !lsei_end_p (lsei);
672 lsei_next_in_partition (&lsei))
674 symtab_node node = lsei_node (lsei);
676 count = ipa_ref_list_nreferences (&node->symbol.ref_list);
677 if (count)
679 streamer_write_gcov_count_stream (ob->main_stream, count);
680 streamer_write_uhwi_stream (ob->main_stream,
681 lto_symtab_encoder_lookup (encoder, node));
682 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list,
683 i, ref); i++)
684 lto_output_ref (ob, ref, encoder);
688 streamer_write_uhwi_stream (ob->main_stream, 0);
690 lto_destroy_simple_output_block (ob);
693 /* Add NODE into encoder as well as nodes it is cloned from.
694 Do it in a way so clones appear first. */
696 static void
697 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
698 bool include_body)
700 if (node->clone_of)
701 add_node_to (encoder, node->clone_of, include_body);
702 else if (include_body)
703 lto_set_symtab_encoder_encode_body (encoder, node);
704 lto_symtab_encoder_encode (encoder, (symtab_node)node);
707 /* Add all references in LIST to encoders. */
709 static void
710 add_references (lto_symtab_encoder_t encoder,
711 struct ipa_ref_list *list)
713 int i;
714 struct ipa_ref *ref;
715 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
716 if (is_a <cgraph_node> (ref->referred))
717 add_node_to (encoder, ipa_ref_node (ref), false);
718 else
719 lto_symtab_encoder_encode (encoder, ref->referred);
722 /* Find all symbols we want to stream into given partition and insert them
723 to encoders.
725 The function actually replaces IN_ENCODER by new one. The reason is that
726 streaming code needs clone's origin to be streamed before clone. This
727 means that we need to insert the nodes in specific order. This order is
728 ignored by the partitioning logic earlier. */
730 lto_symtab_encoder_t
731 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
733 struct cgraph_node *node;
734 struct cgraph_edge *edge;
735 int i;
736 lto_symtab_encoder_t encoder;
737 lto_symtab_encoder_iterator lsei;
739 encoder = lto_symtab_encoder_new (false);
741 /* Go over all entries in the IN_ENCODER and duplicate them to
742 ENCODER. At the same time insert masters of clones so
743 every master appears before clone. */
744 for (lsei = lsei_start_function_in_partition (in_encoder);
745 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
747 node = lsei_cgraph_node (lsei);
748 add_node_to (encoder, node, true);
749 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)node);
750 add_references (encoder, &node->symbol.ref_list);
752 for (lsei = lsei_start_variable_in_partition (in_encoder);
753 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
755 struct varpool_node *vnode = lsei_varpool_node (lsei);
757 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)vnode);
758 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
759 add_references (encoder, &vnode->symbol.ref_list);
761 /* Pickle in also the initializer of all referenced readonly variables
762 to help folding. Constant pool variables are not shared, so we must
763 pickle those too. */
764 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
766 symtab_node node = lto_symtab_encoder_deref (encoder, i);
767 if (varpool_node *vnode = dyn_cast <varpool_node> (node))
769 if (!lto_symtab_encoder_encode_initializer_p (encoder,
770 vnode)
771 && ctor_for_folding (vnode->symbol.decl) != error_mark_node)
773 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
774 add_references (encoder, &vnode->symbol.ref_list);
779 /* Go over all the nodes again to include callees that are not in
780 SET. */
781 for (lsei = lsei_start_function_in_partition (encoder);
782 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
784 node = lsei_cgraph_node (lsei);
785 for (edge = node->callees; edge; edge = edge->next_callee)
787 struct cgraph_node *callee = edge->callee;
788 if (!lto_symtab_encoder_in_partition_p (encoder, (symtab_node)callee))
790 /* We should have moved all the inlines. */
791 gcc_assert (!callee->global.inlined_to);
792 add_node_to (encoder, callee, false);
796 lto_symtab_encoder_delete (in_encoder);
797 return encoder;
800 /* Output the part of the symtab in SET and VSET. */
802 void
803 output_symtab (void)
805 struct cgraph_node *node;
806 struct lto_simple_output_block *ob;
807 lto_symtab_encoder_iterator lsei;
808 int i, n_nodes;
809 lto_symtab_encoder_t encoder;
810 static bool asm_nodes_output = false;
812 if (flag_wpa)
813 output_cgraph_opt_summary ();
815 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
817 output_profile_summary (ob);
819 /* An encoder for cgraph nodes should have been created by
820 ipa_write_summaries_1. */
821 gcc_assert (ob->decl_state->symtab_node_encoder);
822 encoder = ob->decl_state->symtab_node_encoder;
824 /* Write out the nodes. We must first output a node and then its clones,
825 otherwise at a time reading back the node there would be nothing to clone
826 from. */
827 n_nodes = lto_symtab_encoder_size (encoder);
828 for (i = 0; i < n_nodes; i++)
830 symtab_node node = lto_symtab_encoder_deref (encoder, i);
831 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
832 lto_output_node (ob, cnode, encoder);
833 else
834 lto_output_varpool_node (ob, varpool (node), encoder);
838 /* Go over the nodes in SET again to write edges. */
839 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
840 lsei_next_function_in_partition (&lsei))
842 node = lsei_cgraph_node (lsei);
843 output_outgoing_cgraph_edges (node->callees, ob, encoder);
844 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
847 streamer_write_uhwi_stream (ob->main_stream, 0);
849 lto_destroy_simple_output_block (ob);
851 /* Emit toplevel asms.
852 When doing WPA we must output every asm just once. Since we do not partition asm
853 nodes at all, output them to first output. This is kind of hack, but should work
854 well. */
855 if (!asm_nodes_output)
857 asm_nodes_output = true;
858 lto_output_toplevel_asms ();
861 output_refs (encoder);
864 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
865 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
866 NODE or to replace the values in it, for instance because the first
867 time we saw it, the function body was not available but now it
868 is. BP is a bitpack with all the bitflags for NODE read from the
869 stream. */
871 static void
872 input_overwrite_node (struct lto_file_decl_data *file_data,
873 struct cgraph_node *node,
874 enum LTO_symtab_tags tag,
875 struct bitpack_d *bp)
877 node->symbol.aux = (void *) tag;
878 node->symbol.lto_file_data = file_data;
880 node->local.local = bp_unpack_value (bp, 1);
881 node->symbol.externally_visible = bp_unpack_value (bp, 1);
882 node->symbol.definition = bp_unpack_value (bp, 1);
883 node->local.versionable = bp_unpack_value (bp, 1);
884 node->local.can_change_signature = bp_unpack_value (bp, 1);
885 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
886 node->symbol.force_output = bp_unpack_value (bp, 1);
887 node->symbol.forced_by_abi = bp_unpack_value (bp, 1);
888 node->symbol.unique_name = bp_unpack_value (bp, 1);
889 node->symbol.address_taken = bp_unpack_value (bp, 1);
890 node->abstract_and_needed = bp_unpack_value (bp, 1);
891 node->symbol.used_from_other_partition = bp_unpack_value (bp, 1);
892 node->lowered = bp_unpack_value (bp, 1);
893 node->symbol.analyzed = tag == LTO_symtab_analyzed_node;
894 node->symbol.in_other_partition = bp_unpack_value (bp, 1);
895 if (node->symbol.in_other_partition
896 /* Avoid updating decl when we are seeing just inline clone.
897 When inlining function that has functions already inlined into it,
898 we produce clones of inline clones.
900 WPA partitioning might put each clone into different unit and
901 we might end up streaming inline clone from other partition
902 to support clone we are interested in. */
903 && (!node->clone_of
904 || node->clone_of->symbol.decl != node->symbol.decl))
906 DECL_EXTERNAL (node->symbol.decl) = 1;
907 TREE_STATIC (node->symbol.decl) = 0;
909 node->symbol.alias = bp_unpack_value (bp, 1);
910 node->symbol.weakref = bp_unpack_value (bp, 1);
911 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
912 node->only_called_at_startup = bp_unpack_value (bp, 1);
913 node->only_called_at_exit = bp_unpack_value (bp, 1);
914 node->tm_clone = bp_unpack_value (bp, 1);
915 node->thunk.thunk_p = bp_unpack_value (bp, 1);
916 node->symbol.resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
917 LDPR_NUM_KNOWN);
920 /* Return string alias is alias of. */
922 static tree
923 get_alias_symbol (tree decl)
925 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
926 return get_identifier (TREE_STRING_POINTER
927 (TREE_VALUE (TREE_VALUE (alias))));
930 /* Read a node from input_block IB. TAG is the node's tag just read.
931 Return the node read or overwriten. */
933 static struct cgraph_node *
934 input_node (struct lto_file_decl_data *file_data,
935 struct lto_input_block *ib,
936 enum LTO_symtab_tags tag,
937 vec<symtab_node> nodes)
939 tree fn_decl;
940 struct cgraph_node *node;
941 struct bitpack_d bp;
942 unsigned decl_index;
943 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
944 int clone_ref;
945 int order;
946 int i, count;
948 order = streamer_read_hwi (ib) + order_base;
949 clone_ref = streamer_read_hwi (ib);
951 decl_index = streamer_read_uhwi (ib);
952 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
954 if (clone_ref != LCC_NOT_FOUND)
956 node = cgraph_clone_node (cgraph (nodes[clone_ref]), fn_decl,
957 0, CGRAPH_FREQ_BASE, false,
958 vNULL, false, NULL);
960 else
962 /* Declaration of functions can be already merged with a declaration
963 from other input file. We keep cgraph unmerged until after streaming
964 of ipa passes is done. Alays forcingly create a fresh node. */
965 node = cgraph_create_empty_node ();
966 node->symbol.decl = fn_decl;
967 symtab_register_node ((symtab_node)node);
970 node->symbol.order = order;
971 if (order >= symtab_order)
972 symtab_order = order + 1;
974 node->count = streamer_read_gcov_count (ib);
975 node->count_materialization_scale = streamer_read_hwi (ib);
977 count = streamer_read_hwi (ib);
978 node->ipa_transforms_to_apply = vNULL;
979 for (i = 0; i < count; i++)
981 struct opt_pass *pass;
982 int pid = streamer_read_hwi (ib);
984 gcc_assert (pid < passes_by_id_size);
985 pass = passes_by_id[pid];
986 node->ipa_transforms_to_apply.safe_push ((struct ipa_opt_pass_d *) pass);
989 if (tag == LTO_symtab_analyzed_node)
990 ref = streamer_read_hwi (ib);
992 ref2 = streamer_read_hwi (ib);
994 /* Make sure that we have not read this node before. Nodes that
995 have already been read will have their tag stored in the 'aux'
996 field. Since built-in functions can be referenced in multiple
997 functions, they are expected to be read more than once. */
998 if (node->symbol.aux && !DECL_BUILT_IN (node->symbol.decl))
999 internal_error ("bytecode stream: found multiple instances of cgraph "
1000 "node with uid %d", node->uid);
1002 bp = streamer_read_bitpack (ib);
1003 input_overwrite_node (file_data, node, tag, &bp);
1005 /* Store a reference for now, and fix up later to be a pointer. */
1006 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
1008 /* Store a reference for now, and fix up later to be a pointer. */
1009 node->symbol.same_comdat_group = (symtab_node) (intptr_t) ref2;
1011 if (node->thunk.thunk_p)
1013 int type = streamer_read_uhwi (ib);
1014 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1015 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1017 node->thunk.fixed_offset = fixed_offset;
1018 node->thunk.this_adjusting = (type & 2);
1019 node->thunk.virtual_value = virtual_value;
1020 node->thunk.virtual_offset_p = (type & 4);
1022 if (node->symbol.alias && !node->symbol.analyzed && node->symbol.weakref)
1023 node->symbol.alias_target = get_alias_symbol (node->symbol.decl);
1024 return node;
1027 /* Read a node from input_block IB. TAG is the node's tag just read.
1028 Return the node read or overwriten. */
1030 static struct varpool_node *
1031 input_varpool_node (struct lto_file_decl_data *file_data,
1032 struct lto_input_block *ib)
1034 int decl_index;
1035 tree var_decl;
1036 struct varpool_node *node;
1037 struct bitpack_d bp;
1038 int ref = LCC_NOT_FOUND;
1039 int order;
1041 order = streamer_read_hwi (ib) + order_base;
1042 decl_index = streamer_read_uhwi (ib);
1043 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1045 /* Declaration of functions can be already merged with a declaration
1046 from other input file. We keep cgraph unmerged until after streaming
1047 of ipa passes is done. Alays forcingly create a fresh node. */
1048 node = varpool_create_empty_node ();
1049 node->symbol.decl = var_decl;
1050 symtab_register_node ((symtab_node)node);
1052 node->symbol.order = order;
1053 if (order >= symtab_order)
1054 symtab_order = order + 1;
1055 node->symbol.lto_file_data = file_data;
1057 bp = streamer_read_bitpack (ib);
1058 node->symbol.externally_visible = bp_unpack_value (&bp, 1);
1059 node->symbol.force_output = bp_unpack_value (&bp, 1);
1060 node->symbol.forced_by_abi = bp_unpack_value (&bp, 1);
1061 node->symbol.unique_name = bp_unpack_value (&bp, 1);
1062 node->symbol.definition = bp_unpack_value (&bp, 1);
1063 node->symbol.alias = bp_unpack_value (&bp, 1);
1064 node->symbol.weakref = bp_unpack_value (&bp, 1);
1065 node->symbol.analyzed = bp_unpack_value (&bp, 1);
1066 node->symbol.used_from_other_partition = bp_unpack_value (&bp, 1);
1067 node->symbol.in_other_partition = bp_unpack_value (&bp, 1);
1068 if (node->symbol.in_other_partition)
1070 DECL_EXTERNAL (node->symbol.decl) = 1;
1071 TREE_STATIC (node->symbol.decl) = 0;
1073 if (node->symbol.alias && !node->symbol.analyzed && node->symbol.weakref)
1074 node->symbol.alias_target = get_alias_symbol (node->symbol.decl);
1075 ref = streamer_read_hwi (ib);
1076 /* Store a reference for now, and fix up later to be a pointer. */
1077 node->symbol.same_comdat_group = (symtab_node) (intptr_t) ref;
1078 node->symbol.resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1079 LDPR_NUM_KNOWN);
1081 return node;
1084 /* Read a node from input_block IB. TAG is the node's tag just read.
1085 Return the node read or overwriten. */
1087 static void
1088 input_ref (struct lto_input_block *ib,
1089 symtab_node referring_node,
1090 vec<symtab_node> nodes)
1092 symtab_node node = NULL;
1093 struct bitpack_d bp;
1094 enum ipa_ref_use use;
1096 bp = streamer_read_bitpack (ib);
1097 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1098 node = nodes[streamer_read_hwi (ib)];
1099 ipa_record_reference (referring_node, node, use, NULL);
1102 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1103 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1104 edge being read is indirect (in the sense that it has
1105 indirect_unknown_callee set). */
1107 static void
1108 input_edge (struct lto_input_block *ib, vec<symtab_node> nodes,
1109 bool indirect)
1111 struct cgraph_node *caller, *callee;
1112 struct cgraph_edge *edge;
1113 unsigned int stmt_id;
1114 gcov_type count;
1115 int freq;
1116 cgraph_inline_failed_t inline_failed;
1117 struct bitpack_d bp;
1118 int ecf_flags = 0;
1120 caller = cgraph (nodes[streamer_read_hwi (ib)]);
1121 if (caller == NULL || caller->symbol.decl == NULL_TREE)
1122 internal_error ("bytecode stream: no caller found while reading edge");
1124 if (!indirect)
1126 callee = cgraph (nodes[streamer_read_hwi (ib)]);
1127 if (callee == NULL || callee->symbol.decl == NULL_TREE)
1128 internal_error ("bytecode stream: no callee found while reading edge");
1130 else
1131 callee = NULL;
1133 count = streamer_read_gcov_count (ib);
1135 bp = streamer_read_bitpack (ib);
1136 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_enum, CIF_N_REASONS);
1137 stmt_id = bp_unpack_var_len_unsigned (&bp);
1138 freq = (int) bp_unpack_var_len_unsigned (&bp);
1140 if (indirect)
1141 edge = cgraph_create_indirect_edge (caller, NULL, 0, count, freq);
1142 else
1143 edge = cgraph_create_edge (caller, callee, NULL, count, freq);
1145 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1146 edge->lto_stmt_uid = stmt_id;
1147 edge->inline_failed = inline_failed;
1148 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1149 edge->can_throw_external = bp_unpack_value (&bp, 1);
1150 if (indirect)
1152 if (bp_unpack_value (&bp, 1))
1153 ecf_flags |= ECF_CONST;
1154 if (bp_unpack_value (&bp, 1))
1155 ecf_flags |= ECF_PURE;
1156 if (bp_unpack_value (&bp, 1))
1157 ecf_flags |= ECF_NORETURN;
1158 if (bp_unpack_value (&bp, 1))
1159 ecf_flags |= ECF_MALLOC;
1160 if (bp_unpack_value (&bp, 1))
1161 ecf_flags |= ECF_NOTHROW;
1162 if (bp_unpack_value (&bp, 1))
1163 ecf_flags |= ECF_RETURNS_TWICE;
1164 edge->indirect_info->ecf_flags = ecf_flags;
1169 /* Read a cgraph from IB using the info in FILE_DATA. */
1171 static vec<symtab_node>
1172 input_cgraph_1 (struct lto_file_decl_data *file_data,
1173 struct lto_input_block *ib)
1175 enum LTO_symtab_tags tag;
1176 vec<symtab_node> nodes = vNULL;
1177 symtab_node node;
1178 unsigned i;
1180 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1181 order_base = symtab_order;
1182 while (tag)
1184 if (tag == LTO_symtab_edge)
1185 input_edge (ib, nodes, false);
1186 else if (tag == LTO_symtab_indirect_edge)
1187 input_edge (ib, nodes, true);
1188 else if (tag == LTO_symtab_variable)
1190 node = (symtab_node)input_varpool_node (file_data, ib);
1191 nodes.safe_push (node);
1192 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1194 else
1196 node = (symtab_node)input_node (file_data, ib, tag, nodes);
1197 if (node == NULL || node->symbol.decl == NULL_TREE)
1198 internal_error ("bytecode stream: found empty cgraph node");
1199 nodes.safe_push (node);
1200 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1203 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1206 lto_input_toplevel_asms (file_data, order_base);
1208 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1209 #ifdef ENABLE_CHECKING
1210 FOR_EACH_VEC_ELT (nodes, i, node)
1211 gcc_assert (node->symbol.aux || !is_a <cgraph_node> (node));
1212 #endif
1213 FOR_EACH_VEC_ELT (nodes, i, node)
1215 int ref;
1216 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
1218 ref = (int) (intptr_t) cnode->global.inlined_to;
1220 /* We share declaration of builtins, so we may read same node twice. */
1221 if (!node->symbol.aux)
1222 continue;
1223 node->symbol.aux = NULL;
1225 /* Fixup inlined_to from reference to pointer. */
1226 if (ref != LCC_NOT_FOUND)
1227 cgraph (node)->global.inlined_to = cgraph (nodes[ref]);
1228 else
1229 cnode->global.inlined_to = NULL;
1232 ref = (int) (intptr_t) node->symbol.same_comdat_group;
1234 /* Fixup same_comdat_group from reference to pointer. */
1235 if (ref != LCC_NOT_FOUND)
1236 node->symbol.same_comdat_group = nodes[ref];
1237 else
1238 node->symbol.same_comdat_group = NULL;
1240 FOR_EACH_VEC_ELT (nodes, i, node)
1241 node->symbol.aux = is_a <cgraph_node> (node) ? (void *)1 : NULL;
1242 return nodes;
1245 /* Input ipa_refs. */
1247 static void
1248 input_refs (struct lto_input_block *ib,
1249 vec<symtab_node> nodes)
1251 int count;
1252 int idx;
1253 while (true)
1255 symtab_node node;
1256 count = streamer_read_uhwi (ib);
1257 if (!count)
1258 break;
1259 idx = streamer_read_uhwi (ib);
1260 node = nodes[idx];
1261 while (count)
1263 input_ref (ib, node, nodes);
1264 count--;
1270 static struct gcov_ctr_summary lto_gcov_summary;
1272 /* Input profile_info from IB. */
1273 static void
1274 input_profile_summary (struct lto_input_block *ib,
1275 struct lto_file_decl_data *file_data)
1277 unsigned h_ix;
1278 struct bitpack_d bp;
1279 unsigned int runs = streamer_read_uhwi (ib);
1280 if (runs)
1282 file_data->profile_info.runs = runs;
1283 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1284 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1286 memset (file_data->profile_info.histogram, 0,
1287 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1288 /* Input the bitpack of non-zero histogram indices. */
1289 bp = streamer_read_bitpack (ib);
1290 /* Read in and unpack the full bitpack, flagging non-zero
1291 histogram entries by setting the num_counters non-zero. */
1292 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1294 file_data->profile_info.histogram[h_ix].num_counters
1295 = bp_unpack_value (&bp, 1);
1297 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1299 if (!file_data->profile_info.histogram[h_ix].num_counters)
1300 continue;
1302 file_data->profile_info.histogram[h_ix].num_counters
1303 = streamer_read_gcov_count (ib);
1304 file_data->profile_info.histogram[h_ix].min_value
1305 = streamer_read_gcov_count (ib);
1306 file_data->profile_info.histogram[h_ix].cum_value
1307 = streamer_read_gcov_count (ib);
1309 /* IPA-profile computes hot bb threshold based on cumulated
1310 whole program profile. We need to stream it down to ltrans. */
1311 if (flag_ltrans)
1312 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1317 /* Rescale profile summaries to the same number of runs in the whole unit. */
1319 static void
1320 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1322 struct lto_file_decl_data *file_data;
1323 unsigned int j, h_ix;
1324 gcov_unsigned_t max_runs = 0;
1325 struct cgraph_node *node;
1326 struct cgraph_edge *edge;
1327 gcov_type saved_sum_all = 0;
1328 gcov_ctr_summary *saved_profile_info = 0;
1329 int saved_scale = 0;
1331 /* Find unit with maximal number of runs. If we ever get serious about
1332 roundoff errors, we might also consider computing smallest common
1333 multiply. */
1334 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1335 if (max_runs < file_data->profile_info.runs)
1336 max_runs = file_data->profile_info.runs;
1338 if (!max_runs)
1339 return;
1341 /* Simple overflow check. We probably don't need to support that many train
1342 runs. Such a large value probably imply data corruption anyway. */
1343 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1345 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1346 INT_MAX / REG_BR_PROB_BASE);
1347 return;
1350 profile_info = &lto_gcov_summary;
1351 lto_gcov_summary.runs = max_runs;
1352 lto_gcov_summary.sum_max = 0;
1353 memset (lto_gcov_summary.histogram, 0,
1354 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1356 /* Rescale all units to the maximal number of runs.
1357 sum_max can not be easily merged, as we have no idea what files come from
1358 the same run. We do not use the info anyway, so leave it 0. */
1359 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1360 if (file_data->profile_info.runs)
1362 int scale = GCOV_COMPUTE_SCALE (max_runs,
1363 file_data->profile_info.runs);
1364 lto_gcov_summary.sum_max
1365 = MAX (lto_gcov_summary.sum_max,
1366 apply_scale (file_data->profile_info.sum_max, scale));
1367 lto_gcov_summary.sum_all
1368 = MAX (lto_gcov_summary.sum_all,
1369 apply_scale (file_data->profile_info.sum_all, scale));
1370 /* Save a pointer to the profile_info with the largest
1371 scaled sum_all and the scale for use in merging the
1372 histogram. */
1373 if (!saved_profile_info
1374 || lto_gcov_summary.sum_all > saved_sum_all)
1376 saved_profile_info = &file_data->profile_info;
1377 saved_sum_all = lto_gcov_summary.sum_all;
1378 saved_scale = scale;
1382 gcc_assert (saved_profile_info);
1384 /* Scale up the histogram from the profile that had the largest
1385 scaled sum_all above. */
1386 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1388 /* Scale up the min value as we did the corresponding sum_all
1389 above. Use that to find the new histogram index. */
1390 gcov_type scaled_min
1391 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1392 saved_scale);
1393 /* The new index may be shared with another scaled histogram entry,
1394 so we need to account for a non-zero histogram entry at new_ix. */
1395 unsigned new_ix = gcov_histo_index (scaled_min);
1396 lto_gcov_summary.histogram[new_ix].min_value
1397 = (lto_gcov_summary.histogram[new_ix].num_counters
1398 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1399 : scaled_min);
1400 /* Some of the scaled counter values would ostensibly need to be placed
1401 into different (larger) histogram buckets, but we keep things simple
1402 here and place the scaled cumulative counter value in the bucket
1403 corresponding to the scaled minimum counter value. */
1404 lto_gcov_summary.histogram[new_ix].cum_value
1405 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1406 saved_scale);
1407 lto_gcov_summary.histogram[new_ix].num_counters
1408 += saved_profile_info->histogram[h_ix].num_counters;
1411 /* Watch roundoff errors. */
1412 if (lto_gcov_summary.sum_max < max_runs)
1413 lto_gcov_summary.sum_max = max_runs;
1415 /* If merging already happent at WPA time, we are done. */
1416 if (flag_ltrans)
1417 return;
1419 /* Now compute count_materialization_scale of each node.
1420 During LTRANS we already have values of count_materialization_scale
1421 computed, so just update them. */
1422 FOR_EACH_FUNCTION (node)
1423 if (node->symbol.lto_file_data
1424 && node->symbol.lto_file_data->profile_info.runs)
1426 int scale;
1428 scale = RDIV (node->count_materialization_scale * max_runs,
1429 node->symbol.lto_file_data->profile_info.runs);
1430 node->count_materialization_scale = scale;
1431 if (scale < 0)
1432 fatal_error ("Profile information in %s corrupted",
1433 file_data->file_name);
1435 if (scale == REG_BR_PROB_BASE)
1436 continue;
1437 for (edge = node->callees; edge; edge = edge->next_callee)
1438 edge->count = apply_scale (edge->count, scale);
1439 node->count = apply_scale (node->count, scale);
1443 /* Input and merge the symtab from each of the .o files passed to
1444 lto1. */
1446 void
1447 input_symtab (void)
1449 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1450 struct lto_file_decl_data *file_data;
1451 unsigned int j = 0;
1452 struct cgraph_node *node;
1454 while ((file_data = file_data_vec[j++]))
1456 const char *data;
1457 size_t len;
1458 struct lto_input_block *ib;
1459 vec<symtab_node> nodes;
1461 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1462 &data, &len);
1463 if (!ib)
1464 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1465 input_profile_summary (ib, file_data);
1466 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1467 nodes = input_cgraph_1 (file_data, ib);
1468 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1469 ib, data, len);
1471 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1472 &data, &len);
1473 if (!ib)
1474 fatal_error("cannot find LTO section refs in %s", file_data->file_name);
1475 input_refs (ib, nodes);
1476 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1477 ib, data, len);
1478 if (flag_ltrans)
1479 input_cgraph_opt_summary (nodes);
1480 nodes.release ();
1483 merge_profile_summaries (file_data_vec);
1484 get_working_sets ();
1487 /* Clear out the aux field that was used to store enough state to
1488 tell which nodes should be overwritten. */
1489 FOR_EACH_FUNCTION (node)
1491 /* Some nodes may have been created by cgraph_node. This
1492 happens when the callgraph contains nested functions. If the
1493 node for the parent function was never emitted to the gimple
1494 file, cgraph_node will create a node for it when setting the
1495 context of the nested function. */
1496 if (node->symbol.lto_file_data)
1497 node->symbol.aux = NULL;
1501 /* True when we need optimization summary for NODE. */
1503 static int
1504 output_cgraph_opt_summary_p (struct cgraph_node *node)
1506 return (node->clone_of
1507 && (node->clone.tree_map
1508 || node->clone.args_to_skip
1509 || node->clone.combined_args_to_skip));
1512 /* Output optimization summary for EDGE to OB. */
1513 static void
1514 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1515 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1519 /* Output optimization summary for NODE to OB. */
1521 static void
1522 output_node_opt_summary (struct output_block *ob,
1523 struct cgraph_node *node,
1524 lto_symtab_encoder_t encoder)
1526 unsigned int index;
1527 bitmap_iterator bi;
1528 struct ipa_replace_map *map;
1529 struct bitpack_d bp;
1530 int i;
1531 struct cgraph_edge *e;
1533 if (node->clone.args_to_skip)
1535 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1536 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1537 streamer_write_uhwi (ob, index);
1539 else
1540 streamer_write_uhwi (ob, 0);
1541 if (node->clone.combined_args_to_skip)
1543 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1544 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1545 streamer_write_uhwi (ob, index);
1547 else
1548 streamer_write_uhwi (ob, 0);
1549 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1550 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1552 int parm_num;
1553 tree parm;
1555 for (parm_num = 0, parm = DECL_ARGUMENTS (node->symbol.decl); parm;
1556 parm = DECL_CHAIN (parm), parm_num++)
1557 if (map->old_tree == parm)
1558 break;
1559 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1560 mechanism to store function local declarations into summaries. */
1561 gcc_assert (parm);
1562 streamer_write_uhwi (ob, parm_num);
1563 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1564 stream_write_tree (ob, map->new_tree, true);
1565 bp = bitpack_create (ob->main_stream);
1566 bp_pack_value (&bp, map->replace_p, 1);
1567 bp_pack_value (&bp, map->ref_p, 1);
1568 streamer_write_bitpack (&bp);
1571 if (lto_symtab_encoder_in_partition_p (encoder, (symtab_node) node))
1573 for (e = node->callees; e; e = e->next_callee)
1574 output_edge_opt_summary (ob, e);
1575 for (e = node->indirect_calls; e; e = e->next_callee)
1576 output_edge_opt_summary (ob, e);
1580 /* Output optimization summaries stored in callgraph.
1581 At the moment it is the clone info structure. */
1583 static void
1584 output_cgraph_opt_summary (void)
1586 int i, n_nodes;
1587 lto_symtab_encoder_t encoder;
1588 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1589 unsigned count = 0;
1591 ob->cgraph_node = NULL;
1592 encoder = ob->decl_state->symtab_node_encoder;
1593 n_nodes = lto_symtab_encoder_size (encoder);
1594 for (i = 0; i < n_nodes; i++)
1596 symtab_node node = lto_symtab_encoder_deref (encoder, i);
1597 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1598 if (cnode && output_cgraph_opt_summary_p (cnode))
1599 count++;
1601 streamer_write_uhwi (ob, count);
1602 for (i = 0; i < n_nodes; i++)
1604 symtab_node node = lto_symtab_encoder_deref (encoder, i);
1605 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1606 if (cnode && output_cgraph_opt_summary_p (cnode))
1608 streamer_write_uhwi (ob, i);
1609 output_node_opt_summary (ob, cnode, encoder);
1612 produce_asm (ob, NULL);
1613 destroy_output_block (ob);
1616 /* Input optimisation summary of EDGE. */
1618 static void
1619 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
1620 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
1624 /* Input optimisation summary of NODE. */
1626 static void
1627 input_node_opt_summary (struct cgraph_node *node,
1628 struct lto_input_block *ib_main,
1629 struct data_in *data_in)
1631 int i;
1632 int count;
1633 int bit;
1634 struct bitpack_d bp;
1635 struct cgraph_edge *e;
1637 count = streamer_read_uhwi (ib_main);
1638 if (count)
1639 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1640 for (i = 0; i < count; i++)
1642 bit = streamer_read_uhwi (ib_main);
1643 bitmap_set_bit (node->clone.args_to_skip, bit);
1645 count = streamer_read_uhwi (ib_main);
1646 if (count)
1647 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1648 for (i = 0; i < count; i++)
1650 bit = streamer_read_uhwi (ib_main);
1651 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1653 count = streamer_read_uhwi (ib_main);
1654 for (i = 0; i < count; i++)
1656 struct ipa_replace_map *map = ggc_alloc_ipa_replace_map ();
1658 vec_safe_push (node->clone.tree_map, map);
1659 map->parm_num = streamer_read_uhwi (ib_main);
1660 map->old_tree = NULL;
1661 map->new_tree = stream_read_tree (ib_main, data_in);
1662 bp = streamer_read_bitpack (ib_main);
1663 map->replace_p = bp_unpack_value (&bp, 1);
1664 map->ref_p = bp_unpack_value (&bp, 1);
1666 for (e = node->callees; e; e = e->next_callee)
1667 input_edge_opt_summary (e, ib_main);
1668 for (e = node->indirect_calls; e; e = e->next_callee)
1669 input_edge_opt_summary (e, ib_main);
1672 /* Read section in file FILE_DATA of length LEN with data DATA. */
1674 static void
1675 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1676 const char *data, size_t len,
1677 vec<symtab_node> nodes)
1679 const struct lto_function_header *header =
1680 (const struct lto_function_header *) data;
1681 const int cfg_offset = sizeof (struct lto_function_header);
1682 const int main_offset = cfg_offset + header->cfg_size;
1683 const int string_offset = main_offset + header->main_size;
1684 struct data_in *data_in;
1685 struct lto_input_block ib_main;
1686 unsigned int i;
1687 unsigned int count;
1689 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1690 header->main_size);
1692 data_in =
1693 lto_data_in_create (file_data, (const char *) data + string_offset,
1694 header->string_size, vNULL);
1695 count = streamer_read_uhwi (&ib_main);
1697 for (i = 0; i < count; i++)
1699 int ref = streamer_read_uhwi (&ib_main);
1700 input_node_opt_summary (cgraph (nodes[ref]),
1701 &ib_main, data_in);
1703 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1704 len);
1705 lto_data_in_delete (data_in);
1708 /* Input optimization summary of cgraph. */
1710 static void
1711 input_cgraph_opt_summary (vec<symtab_node> nodes)
1713 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1714 struct lto_file_decl_data *file_data;
1715 unsigned int j = 0;
1717 while ((file_data = file_data_vec[j++]))
1719 size_t len;
1720 const char *data =
1721 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1722 &len);
1724 if (data)
1725 input_cgraph_opt_section (file_data, data, len, nodes);