* doc/invoke.texi (AVR Options): Document __AVR_ARCH__.
[official-gcc.git] / gcc / lto-cgraph.c
blobb23f192e3bd6042e346c3c74a58cf05359b63790
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"
49 static void output_cgraph_opt_summary (void);
50 static void input_cgraph_opt_summary (VEC (symtab_node, heap) * nodes);
52 /* Number of LDPR values known to GCC. */
53 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
55 /* All node orders are ofsetted by ORDER_BASE. */
56 static int order_base;
58 /* Cgraph streaming is organized as set of record whose type
59 is indicated by a tag. */
60 enum LTO_symtab_tags
62 /* Must leave 0 for the stopper. */
64 /* Cgraph node without body available. */
65 LTO_symtab_unavail_node = 1,
66 /* Cgraph node with function body. */
67 LTO_symtab_analyzed_node,
68 /* Cgraph edges. */
69 LTO_symtab_edge,
70 LTO_symtab_indirect_edge,
71 LTO_symtab_variable,
72 LTO_symtab_last_tag
75 /* Create a new symtab encoder.
76 if FOR_INPUT, the encoder allocate only datastructures needed
77 to read the symtab. */
79 lto_symtab_encoder_t
80 lto_symtab_encoder_new (bool for_input)
82 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
84 if (!for_input)
85 encoder->map = pointer_map_create ();
86 encoder->nodes = NULL;
87 return encoder;
91 /* Delete ENCODER and its components. */
93 void
94 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
96 VEC_free (lto_encoder_entry, heap, encoder->nodes);
97 if (encoder->map)
98 pointer_map_destroy (encoder->map);
99 free (encoder);
103 /* Return the existing reference number of NODE in the symtab encoder in
104 output block OB. Assign a new reference if this is the first time
105 NODE is encoded. */
108 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
109 symtab_node node)
111 int ref;
112 void **slot;
114 if (!encoder->map)
116 lto_encoder_entry entry = {node, false, false, false};
118 ref = VEC_length (lto_encoder_entry, encoder->nodes);
119 VEC_safe_push (lto_encoder_entry, heap, encoder->nodes, entry);
120 return ref;
123 slot = pointer_map_contains (encoder->map, node);
124 if (!slot || !*slot)
126 lto_encoder_entry entry = {node, false, false, false};
127 ref = VEC_length (lto_encoder_entry, encoder->nodes);
128 if (!slot)
129 slot = pointer_map_insert (encoder->map, node);
130 *slot = (void *) (intptr_t) (ref + 1);
131 VEC_safe_push (lto_encoder_entry, heap, encoder->nodes, entry);
133 else
134 ref = (size_t) *slot - 1;
136 return ref;
139 /* Remove NODE from encoder. */
141 bool
142 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
143 symtab_node node)
145 void **slot, **last_slot;
146 int index;
147 lto_encoder_entry last_node;
149 slot = pointer_map_contains (encoder->map, node);
150 if (slot == NULL || !*slot)
151 return false;
153 index = (size_t) *slot - 1;
154 gcc_checking_assert (VEC_index (lto_encoder_entry,
155 encoder->nodes, index).node
156 == node);
158 /* Remove from vector. We do this by swapping node with the last element
159 of the vector. */
160 last_node = VEC_pop (lto_encoder_entry, encoder->nodes);
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 VEC_replace (lto_encoder_entry, encoder->nodes, index,
169 last_node);
172 /* Remove element from hash table. */
173 *slot = NULL;
174 return true;
178 /* Return TRUE if we should encode initializer of NODE (if any). */
180 bool
181 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
182 struct cgraph_node *node)
184 int index = lto_symtab_encoder_lookup (encoder, (symtab_node)node);
185 return VEC_index (lto_encoder_entry, encoder->nodes, index).body;
188 /* Return TRUE if we should encode body of NODE (if any). */
190 static void
191 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
192 struct cgraph_node *node)
194 int index = lto_symtab_encoder_encode (encoder, (symtab_node)node);
195 gcc_checking_assert (VEC_index (lto_encoder_entry, encoder->nodes,
196 index).node == (symtab_node)node);
197 VEC_index (lto_encoder_entry, encoder->nodes, index).body = true;
200 /* Return TRUE if we should encode initializer of NODE (if any). */
202 bool
203 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
204 struct varpool_node *node)
206 int index = lto_symtab_encoder_lookup (encoder, (symtab_node)node);
207 if (index == LCC_NOT_FOUND)
208 return false;
209 return VEC_index (lto_encoder_entry, encoder->nodes, index).initializer;
212 /* Return TRUE if we should encode initializer of NODE (if any). */
214 static void
215 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
216 struct varpool_node *node)
218 int index = lto_symtab_encoder_lookup (encoder, (symtab_node)node);
219 VEC_index (lto_encoder_entry, encoder->nodes, index).initializer = true;
222 /* Return TRUE if we should encode initializer of NODE (if any). */
224 bool
225 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
226 symtab_node node)
228 int index = lto_symtab_encoder_lookup (encoder, (symtab_node)node);
229 if (index == LCC_NOT_FOUND)
230 return false;
231 return VEC_index (lto_encoder_entry, encoder->nodes, index).in_partition;
234 /* Return TRUE if we should encode body of NODE (if any). */
236 void
237 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
238 symtab_node node)
240 int index = lto_symtab_encoder_encode (encoder, (symtab_node)node);
241 VEC_index (lto_encoder_entry, encoder->nodes, index).in_partition = true;
244 /* Output the cgraph EDGE to OB using ENCODER. */
246 static void
247 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
248 lto_symtab_encoder_t encoder)
250 unsigned int uid;
251 intptr_t ref;
252 struct bitpack_d bp;
254 if (edge->indirect_unknown_callee)
255 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
256 LTO_symtab_indirect_edge);
257 else
258 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
259 LTO_symtab_edge);
261 ref = lto_symtab_encoder_lookup (encoder, (symtab_node)edge->caller);
262 gcc_assert (ref != LCC_NOT_FOUND);
263 streamer_write_hwi_stream (ob->main_stream, ref);
265 if (!edge->indirect_unknown_callee)
267 ref = lto_symtab_encoder_lookup (encoder, (symtab_node)edge->callee);
268 gcc_assert (ref != LCC_NOT_FOUND);
269 streamer_write_hwi_stream (ob->main_stream, ref);
272 streamer_write_hwi_stream (ob->main_stream, edge->count);
274 bp = bitpack_create (ob->main_stream);
275 uid = (!gimple_has_body_p (edge->caller->symbol.decl)
276 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt));
277 bp_pack_enum (&bp, cgraph_inline_failed_enum,
278 CIF_N_REASONS, edge->inline_failed);
279 bp_pack_var_len_unsigned (&bp, uid);
280 bp_pack_var_len_unsigned (&bp, edge->frequency);
281 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
282 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
283 bp_pack_value (&bp, edge->can_throw_external, 1);
284 if (edge->indirect_unknown_callee)
286 int flags = edge->indirect_info->ecf_flags;
287 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
288 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
289 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
290 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
291 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
292 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
293 /* Flags that should not appear on indirect calls. */
294 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
295 | ECF_MAY_BE_ALLOCA
296 | ECF_SIBCALL
297 | ECF_LEAF
298 | ECF_NOVOPS)));
300 streamer_write_bitpack (&bp);
303 /* Return if LIST contain references from other partitions. */
305 bool
306 referenced_from_other_partition_p (struct ipa_ref_list *list, lto_symtab_encoder_t encoder)
308 int i;
309 struct ipa_ref *ref;
310 for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
312 if (ref->referring->symbol.in_other_partition
313 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
314 return true;
316 return false;
319 /* Return true when node is reachable from other partition. */
321 bool
322 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
324 struct cgraph_edge *e;
325 if (!node->analyzed)
326 return false;
327 if (node->global.inlined_to)
328 return false;
329 for (e = node->callers; e; e = e->next_caller)
330 if (e->caller->symbol.in_other_partition
331 || !lto_symtab_encoder_in_partition_p (encoder, (symtab_node)e->caller))
332 return true;
333 return false;
336 /* Return if LIST contain references from other partitions. */
338 bool
339 referenced_from_this_partition_p (struct ipa_ref_list *list,
340 lto_symtab_encoder_t encoder)
342 int i;
343 struct ipa_ref *ref;
344 for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
345 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
346 return true;
347 return false;
350 /* Return true when node is reachable from other partition. */
352 bool
353 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
355 struct cgraph_edge *e;
356 for (e = node->callers; e; e = e->next_caller)
357 if (lto_symtab_encoder_in_partition_p (encoder, (symtab_node)e->caller))
358 return true;
359 return false;
362 /* Output the cgraph NODE to OB. ENCODER is used to find the
363 reference number of NODE->inlined_to. SET is the set of nodes we
364 are writing to the current file. If NODE is not in SET, then NODE
365 is a boundary of a cgraph_node_set and we pretend NODE just has a
366 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
367 that have had their callgraph node written so far. This is used to
368 determine if NODE is a clone of a previously written node. */
370 static void
371 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
372 lto_symtab_encoder_t encoder)
374 unsigned int tag;
375 struct bitpack_d bp;
376 bool boundary_p;
377 intptr_t ref;
378 bool in_other_partition = false;
379 struct cgraph_node *clone_of;
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 if (tag == LTO_symtab_analyzed_node)
437 if (node->global.inlined_to)
439 ref = lto_symtab_encoder_lookup (encoder, (symtab_node)node->global.inlined_to);
440 gcc_assert (ref != LCC_NOT_FOUND);
442 else
443 ref = LCC_NOT_FOUND;
445 streamer_write_hwi_stream (ob->main_stream, ref);
448 if (node->symbol.same_comdat_group && !boundary_p)
450 ref = lto_symtab_encoder_lookup (encoder,
451 node->symbol.same_comdat_group);
452 gcc_assert (ref != LCC_NOT_FOUND);
454 else
455 ref = LCC_NOT_FOUND;
456 streamer_write_hwi_stream (ob->main_stream, ref);
458 bp = bitpack_create (ob->main_stream);
459 bp_pack_value (&bp, node->local.local, 1);
460 bp_pack_value (&bp, node->symbol.externally_visible, 1);
461 bp_pack_value (&bp, node->local.finalized, 1);
462 bp_pack_value (&bp, node->local.versionable, 1);
463 bp_pack_value (&bp, node->local.can_change_signature, 1);
464 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
465 bp_pack_value (&bp, node->symbol.force_output, 1);
466 bp_pack_value (&bp, node->symbol.address_taken, 1);
467 bp_pack_value (&bp, node->abstract_and_needed, 1);
468 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
469 && !DECL_EXTERNAL (node->symbol.decl)
470 && !DECL_COMDAT (node->symbol.decl)
471 && (reachable_from_other_partition_p (node, encoder)
472 || referenced_from_other_partition_p (&node->symbol.ref_list,
473 encoder)), 1);
474 bp_pack_value (&bp, node->lowered, 1);
475 bp_pack_value (&bp, in_other_partition, 1);
476 /* Real aliases in a boundary become non-aliases. However we still stream
477 alias info on weakrefs.
478 TODO: We lose a bit of information here - when we know that variable is
479 defined in other unit, we may use the info on aliases to resolve
480 symbol1 != symbol2 type tests that we can do only for locally defined objects
481 otherwise. */
482 bp_pack_value (&bp, node->alias && (!boundary_p || DECL_EXTERNAL (node->symbol.decl)), 1);
483 bp_pack_value (&bp, node->frequency, 2);
484 bp_pack_value (&bp, node->only_called_at_startup, 1);
485 bp_pack_value (&bp, node->only_called_at_exit, 1);
486 bp_pack_value (&bp, node->tm_clone, 1);
487 bp_pack_value (&bp, node->thunk.thunk_p && !boundary_p, 1);
488 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
489 LDPR_NUM_KNOWN, node->symbol.resolution);
490 streamer_write_bitpack (&bp);
492 if (node->thunk.thunk_p && !boundary_p)
494 streamer_write_uhwi_stream
495 (ob->main_stream,
496 1 + (node->thunk.this_adjusting != 0) * 2
497 + (node->thunk.virtual_offset_p != 0) * 4);
498 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
499 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
501 if ((node->alias || node->thunk.thunk_p)
502 && (!boundary_p || (node->alias && DECL_EXTERNAL (node->symbol.decl))))
504 streamer_write_hwi_in_range (ob->main_stream, 0, 1,
505 node->thunk.alias != NULL);
506 if (node->thunk.alias != NULL)
507 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
508 node->thunk.alias);
512 /* Output the varpool NODE to OB.
513 If NODE is not in SET, then NODE is a boundary. */
515 static void
516 lto_output_varpool_node (struct lto_simple_output_block *ob, struct varpool_node *node,
517 lto_symtab_encoder_t encoder)
519 bool boundary_p = (node->analyzed
520 && !lto_symtab_encoder_in_partition_p (encoder, (symtab_node)node));
521 struct bitpack_d bp;
522 int ref;
524 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
525 LTO_symtab_variable);
526 streamer_write_hwi_stream (ob->main_stream, node->symbol.order);
527 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->symbol.decl);
528 bp = bitpack_create (ob->main_stream);
529 bp_pack_value (&bp, node->symbol.externally_visible, 1);
530 bp_pack_value (&bp, node->symbol.force_output, 1);
531 bp_pack_value (&bp, node->finalized, 1);
532 bp_pack_value (&bp, node->alias, 1);
533 bp_pack_value (&bp, node->alias_of != NULL, 1);
534 gcc_assert (node->finalized || !node->analyzed);
535 /* Constant pool initializers can be de-unified into individual ltrans units.
536 FIXME: Alternatively at -Os we may want to avoid generating for them the local
537 labels and share them across LTRANS partitions. */
538 if (DECL_IN_CONSTANT_POOL (node->symbol.decl)
539 && !DECL_EXTERNAL (node->symbol.decl)
540 && !DECL_COMDAT (node->symbol.decl))
542 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
543 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
545 else
547 bp_pack_value (&bp, node->analyzed
548 && referenced_from_other_partition_p (&node->symbol.ref_list,
549 encoder), 1);
550 bp_pack_value (&bp, boundary_p && !DECL_EXTERNAL (node->symbol.decl), 1);
551 /* in_other_partition. */
553 streamer_write_bitpack (&bp);
554 if (node->alias_of)
555 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->alias_of);
556 if (node->symbol.same_comdat_group && !boundary_p)
558 ref = lto_symtab_encoder_lookup (encoder,
559 node->symbol.same_comdat_group);
560 gcc_assert (ref != LCC_NOT_FOUND);
562 else
563 ref = LCC_NOT_FOUND;
564 streamer_write_hwi_stream (ob->main_stream, ref);
565 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
566 LDPR_NUM_KNOWN, node->symbol.resolution);
569 /* Output the varpool NODE to OB.
570 If NODE is not in SET, then NODE is a boundary. */
572 static void
573 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
574 lto_symtab_encoder_t encoder)
576 struct bitpack_d bp;
577 int nref;
579 bp = bitpack_create (ob->main_stream);
580 bp_pack_value (&bp, ref->use, 2);
581 streamer_write_bitpack (&bp);
582 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
583 gcc_assert (nref != LCC_NOT_FOUND);
584 streamer_write_hwi_stream (ob->main_stream, nref);
587 /* Stream out profile_summary to OB. */
589 static void
590 output_profile_summary (struct lto_simple_output_block *ob)
592 if (profile_info)
594 /* We do not output num, sum_all and run_max, they are not used by
595 GCC profile feedback and they are difficult to merge from multiple
596 units. */
597 gcc_assert (profile_info->runs);
598 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
599 streamer_write_uhwi_stream (ob->main_stream, profile_info->sum_max);
601 else
602 streamer_write_uhwi_stream (ob->main_stream, 0);
605 /* Output all callees or indirect outgoing edges. EDGE must be the first such
606 edge. */
608 static void
609 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
610 struct lto_simple_output_block *ob,
611 lto_symtab_encoder_t encoder)
613 if (!edge)
614 return;
616 /* Output edges in backward direction, so the reconstructed callgraph match
617 and it is easy to associate call sites in the IPA pass summaries. */
618 while (edge->next_callee)
619 edge = edge->next_callee;
620 for (; edge; edge = edge->prev_callee)
621 lto_output_edge (ob, edge, encoder);
624 /* Output the part of the cgraph in SET. */
626 static void
627 output_refs (lto_symtab_encoder_t encoder)
629 lto_symtab_encoder_iterator lsei;
630 struct lto_simple_output_block *ob;
631 int count;
632 struct ipa_ref *ref;
633 int i;
635 ob = lto_create_simple_output_block (LTO_section_refs);
637 for (lsei = lsei_start_in_partition (encoder); !lsei_end_p (lsei);
638 lsei_next_in_partition (&lsei))
640 symtab_node node = lsei_node (lsei);
642 count = ipa_ref_list_nreferences (&node->symbol.ref_list);
643 if (count)
645 streamer_write_uhwi_stream (ob->main_stream, count);
646 streamer_write_uhwi_stream (ob->main_stream,
647 lto_symtab_encoder_lookup (encoder, node));
648 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list,
649 i, ref); i++)
650 lto_output_ref (ob, ref, encoder);
654 streamer_write_uhwi_stream (ob->main_stream, 0);
656 lto_destroy_simple_output_block (ob);
659 /* Add NODE into encoder as well as nodes it is cloned from.
660 Do it in a way so clones appear first. */
662 static void
663 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
664 bool include_body)
666 if (node->clone_of)
667 add_node_to (encoder, node->clone_of, include_body);
668 else if (include_body)
669 lto_set_symtab_encoder_encode_body (encoder, node);
670 lto_symtab_encoder_encode (encoder, (symtab_node)node);
673 /* Add all references in LIST to encoders. */
675 static void
676 add_references (lto_symtab_encoder_t encoder,
677 struct ipa_ref_list *list)
679 int i;
680 struct ipa_ref *ref;
681 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
682 if (symtab_function_p (ref->referred))
683 add_node_to (encoder, ipa_ref_node (ref), false);
684 else
685 lto_symtab_encoder_encode (encoder, ref->referred);
688 /* Find all symbols we want to stream into given partition and insert them
689 to encoders.
691 The function actually replaces IN_ENCODER by new one. The reason is that
692 streaming code needs clone's origin to be streamed before clone. This
693 means that we need to insert the nodes in specific order. This order is
694 ignored by the partitioning logic earlier. */
696 lto_symtab_encoder_t
697 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
699 struct cgraph_node *node;
700 struct cgraph_edge *edge;
701 int i;
702 lto_symtab_encoder_t encoder;
703 lto_symtab_encoder_iterator lsei;
705 encoder = lto_symtab_encoder_new (false);
707 /* Go over all entries in the IN_ENCODER and duplicate them to
708 ENCODER. At the same time insert masters of clones so
709 every master appears before clone. */
710 for (lsei = lsei_start_function_in_partition (in_encoder);
711 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
713 node = lsei_cgraph_node (lsei);
714 add_node_to (encoder, node, true);
715 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)node);
716 add_references (encoder, &node->symbol.ref_list);
718 for (lsei = lsei_start_variable_in_partition (in_encoder);
719 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
721 struct varpool_node *vnode = lsei_varpool_node (lsei);
722 gcc_assert (!vnode->alias || vnode->alias_of);
723 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)vnode);
724 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
725 add_references (encoder, &vnode->symbol.ref_list);
727 /* Pickle in also the initializer of all referenced readonly variables
728 to help folding. Constant pool variables are not shared, so we must
729 pickle those too. */
730 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
732 symtab_node node = lto_symtab_encoder_deref (encoder, i);
733 if (symtab_variable_p (node))
735 struct varpool_node *vnode = varpool (node);
736 if (DECL_INITIAL (vnode->symbol.decl)
737 && !lto_symtab_encoder_encode_initializer_p (encoder,
738 vnode)
739 && const_value_known_p (vnode->symbol.decl))
741 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
742 add_references (encoder, &vnode->symbol.ref_list);
747 /* Go over all the nodes again to include callees that are not in
748 SET. */
749 for (lsei = lsei_start_function_in_partition (encoder);
750 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
752 node = lsei_cgraph_node (lsei);
753 for (edge = node->callees; edge; edge = edge->next_callee)
755 struct cgraph_node *callee = edge->callee;
756 if (!lto_symtab_encoder_in_partition_p (encoder, (symtab_node)callee))
758 /* We should have moved all the inlines. */
759 gcc_assert (!callee->global.inlined_to);
760 add_node_to (encoder, callee, false);
764 lto_symtab_encoder_delete (in_encoder);
765 return encoder;
768 /* Output the part of the symtab in SET and VSET. */
770 void
771 output_symtab (void)
773 struct cgraph_node *node;
774 struct lto_simple_output_block *ob;
775 lto_symtab_encoder_iterator lsei;
776 int i, n_nodes;
777 lto_symtab_encoder_t encoder;
778 static bool asm_nodes_output = false;
780 if (flag_wpa)
781 output_cgraph_opt_summary ();
783 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
785 output_profile_summary (ob);
787 /* An encoder for cgraph nodes should have been created by
788 ipa_write_summaries_1. */
789 gcc_assert (ob->decl_state->symtab_node_encoder);
790 encoder = ob->decl_state->symtab_node_encoder;
792 /* Write out the nodes. We must first output a node and then its clones,
793 otherwise at a time reading back the node there would be nothing to clone
794 from. */
795 n_nodes = lto_symtab_encoder_size (encoder);
796 for (i = 0; i < n_nodes; i++)
798 symtab_node node = lto_symtab_encoder_deref (encoder, i);
799 if (symtab_function_p (node))
800 lto_output_node (ob, cgraph (node), encoder);
801 else
802 lto_output_varpool_node (ob, varpool (node), encoder);
806 /* Go over the nodes in SET again to write edges. */
807 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
808 lsei_next_function_in_partition (&lsei))
810 node = lsei_cgraph_node (lsei);
811 output_outgoing_cgraph_edges (node->callees, ob, encoder);
812 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
815 streamer_write_uhwi_stream (ob->main_stream, 0);
817 lto_destroy_simple_output_block (ob);
819 /* Emit toplevel asms.
820 When doing WPA we must output every asm just once. Since we do not partition asm
821 nodes at all, output them to first output. This is kind of hack, but should work
822 well. */
823 if (!asm_nodes_output)
825 asm_nodes_output = true;
826 lto_output_toplevel_asms ();
829 output_refs (encoder);
832 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
833 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
834 NODE or to replace the values in it, for instance because the first
835 time we saw it, the function body was not available but now it
836 is. BP is a bitpack with all the bitflags for NODE read from the
837 stream. */
839 static void
840 input_overwrite_node (struct lto_file_decl_data *file_data,
841 struct cgraph_node *node,
842 enum LTO_symtab_tags tag,
843 struct bitpack_d *bp)
845 node->symbol.aux = (void *) tag;
846 node->symbol.lto_file_data = file_data;
848 node->local.local = bp_unpack_value (bp, 1);
849 node->symbol.externally_visible = bp_unpack_value (bp, 1);
850 node->local.finalized = bp_unpack_value (bp, 1);
851 node->local.versionable = bp_unpack_value (bp, 1);
852 node->local.can_change_signature = bp_unpack_value (bp, 1);
853 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
854 node->symbol.force_output = bp_unpack_value (bp, 1);
855 node->symbol.address_taken = bp_unpack_value (bp, 1);
856 node->abstract_and_needed = bp_unpack_value (bp, 1);
857 node->symbol.used_from_other_partition = bp_unpack_value (bp, 1);
858 node->lowered = bp_unpack_value (bp, 1);
859 node->analyzed = tag == LTO_symtab_analyzed_node;
860 node->symbol.in_other_partition = bp_unpack_value (bp, 1);
861 if (node->symbol.in_other_partition
862 /* Avoid updating decl when we are seeing just inline clone.
863 When inlining function that has functions already inlined into it,
864 we produce clones of inline clones.
866 WPA partitioning might put each clone into different unit and
867 we might end up streaming inline clone from other partition
868 to support clone we are interested in. */
869 && (!node->clone_of
870 || node->clone_of->symbol.decl != node->symbol.decl))
872 DECL_EXTERNAL (node->symbol.decl) = 1;
873 TREE_STATIC (node->symbol.decl) = 0;
875 node->alias = bp_unpack_value (bp, 1);
876 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
877 node->only_called_at_startup = bp_unpack_value (bp, 1);
878 node->only_called_at_exit = bp_unpack_value (bp, 1);
879 node->tm_clone = bp_unpack_value (bp, 1);
880 node->thunk.thunk_p = bp_unpack_value (bp, 1);
881 node->symbol.resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
882 LDPR_NUM_KNOWN);
885 /* Read a node from input_block IB. TAG is the node's tag just read.
886 Return the node read or overwriten. */
888 static struct cgraph_node *
889 input_node (struct lto_file_decl_data *file_data,
890 struct lto_input_block *ib,
891 enum LTO_symtab_tags tag,
892 VEC(symtab_node, heap) *nodes)
894 tree fn_decl;
895 struct cgraph_node *node;
896 struct bitpack_d bp;
897 unsigned decl_index;
898 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
899 int clone_ref;
900 int order;
902 order = streamer_read_hwi (ib) + order_base;
903 clone_ref = streamer_read_hwi (ib);
905 decl_index = streamer_read_uhwi (ib);
906 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
908 if (clone_ref != LCC_NOT_FOUND)
910 node = cgraph_clone_node (cgraph (VEC_index (symtab_node, nodes, clone_ref)), fn_decl,
911 0, CGRAPH_FREQ_BASE, false, NULL, false);
913 else
914 node = cgraph_get_create_node (fn_decl);
916 node->symbol.order = order;
917 if (order >= symtab_order)
918 symtab_order = order + 1;
920 node->count = streamer_read_hwi (ib);
921 node->count_materialization_scale = streamer_read_hwi (ib);
923 if (tag == LTO_symtab_analyzed_node)
924 ref = streamer_read_hwi (ib);
926 ref2 = streamer_read_hwi (ib);
928 /* Make sure that we have not read this node before. Nodes that
929 have already been read will have their tag stored in the 'aux'
930 field. Since built-in functions can be referenced in multiple
931 functions, they are expected to be read more than once. */
932 if (node->symbol.aux && !DECL_BUILT_IN (node->symbol.decl))
933 internal_error ("bytecode stream: found multiple instances of cgraph "
934 "node %d", node->uid);
936 bp = streamer_read_bitpack (ib);
937 input_overwrite_node (file_data, node, tag, &bp);
939 /* Store a reference for now, and fix up later to be a pointer. */
940 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
942 /* Store a reference for now, and fix up later to be a pointer. */
943 node->symbol.same_comdat_group = (symtab_node) (intptr_t) ref2;
945 if (node->thunk.thunk_p)
947 int type = streamer_read_uhwi (ib);
948 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
949 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
951 node->thunk.fixed_offset = fixed_offset;
952 node->thunk.this_adjusting = (type & 2);
953 node->thunk.virtual_value = virtual_value;
954 node->thunk.virtual_offset_p = (type & 4);
956 if (node->thunk.thunk_p || node->alias)
958 if (streamer_read_hwi_in_range (ib, "alias nonzero flag", 0, 1))
960 decl_index = streamer_read_uhwi (ib);
961 node->thunk.alias = lto_file_decl_data_get_fn_decl (file_data,
962 decl_index);
965 return node;
968 /* Read a node from input_block IB. TAG is the node's tag just read.
969 Return the node read or overwriten. */
971 static struct varpool_node *
972 input_varpool_node (struct lto_file_decl_data *file_data,
973 struct lto_input_block *ib)
975 int decl_index;
976 tree var_decl;
977 struct varpool_node *node;
978 struct bitpack_d bp;
979 int ref = LCC_NOT_FOUND;
980 bool non_null_aliasof;
981 int order;
983 order = streamer_read_hwi (ib) + order_base;
984 decl_index = streamer_read_uhwi (ib);
985 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
986 node = varpool_node (var_decl);
987 node->symbol.order = order;
988 if (order >= symtab_order)
989 symtab_order = order + 1;
990 node->symbol.lto_file_data = file_data;
992 bp = streamer_read_bitpack (ib);
993 node->symbol.externally_visible = bp_unpack_value (&bp, 1);
994 node->symbol.force_output = bp_unpack_value (&bp, 1);
995 node->finalized = bp_unpack_value (&bp, 1);
996 node->alias = bp_unpack_value (&bp, 1);
997 non_null_aliasof = bp_unpack_value (&bp, 1);
998 node->symbol.used_from_other_partition = bp_unpack_value (&bp, 1);
999 node->symbol.in_other_partition = bp_unpack_value (&bp, 1);
1000 node->analyzed = (node->finalized && (!node->alias || !node->symbol.in_other_partition));
1001 if (node->symbol.in_other_partition)
1003 DECL_EXTERNAL (node->symbol.decl) = 1;
1004 TREE_STATIC (node->symbol.decl) = 0;
1006 if (non_null_aliasof)
1008 decl_index = streamer_read_uhwi (ib);
1009 node->alias_of = lto_file_decl_data_get_var_decl (file_data, decl_index);
1011 ref = streamer_read_hwi (ib);
1012 /* Store a reference for now, and fix up later to be a pointer. */
1013 node->symbol.same_comdat_group = (symtab_node) (intptr_t) ref;
1014 node->symbol.resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1015 LDPR_NUM_KNOWN);
1017 return node;
1020 /* Read a node from input_block IB. TAG is the node's tag just read.
1021 Return the node read or overwriten. */
1023 static void
1024 input_ref (struct lto_input_block *ib,
1025 symtab_node referring_node,
1026 VEC(symtab_node, heap) *nodes)
1028 symtab_node node = NULL;
1029 struct bitpack_d bp;
1030 enum ipa_ref_use use;
1032 bp = streamer_read_bitpack (ib);
1033 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1034 node = VEC_index (symtab_node, nodes, streamer_read_hwi (ib));
1035 ipa_record_reference (referring_node, node, use, NULL);
1038 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1039 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1040 edge being read is indirect (in the sense that it has
1041 indirect_unknown_callee set). */
1043 static void
1044 input_edge (struct lto_input_block *ib, VEC(symtab_node, heap) *nodes,
1045 bool indirect)
1047 struct cgraph_node *caller, *callee;
1048 struct cgraph_edge *edge;
1049 unsigned int stmt_id;
1050 gcov_type count;
1051 int freq;
1052 cgraph_inline_failed_t inline_failed;
1053 struct bitpack_d bp;
1054 int ecf_flags = 0;
1056 caller = cgraph (VEC_index (symtab_node, nodes, streamer_read_hwi (ib)));
1057 if (caller == NULL || caller->symbol.decl == NULL_TREE)
1058 internal_error ("bytecode stream: no caller found while reading edge");
1060 if (!indirect)
1062 callee = cgraph (VEC_index (symtab_node, nodes, streamer_read_hwi (ib)));
1063 if (callee == NULL || callee->symbol.decl == NULL_TREE)
1064 internal_error ("bytecode stream: no callee found while reading edge");
1066 else
1067 callee = NULL;
1069 count = (gcov_type) streamer_read_hwi (ib);
1071 bp = streamer_read_bitpack (ib);
1072 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_enum, CIF_N_REASONS);
1073 stmt_id = bp_unpack_var_len_unsigned (&bp);
1074 freq = (int) bp_unpack_var_len_unsigned (&bp);
1076 if (indirect)
1077 edge = cgraph_create_indirect_edge (caller, NULL, 0, count, freq);
1078 else
1079 edge = cgraph_create_edge (caller, callee, NULL, count, freq);
1081 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1082 edge->lto_stmt_uid = stmt_id;
1083 edge->inline_failed = inline_failed;
1084 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1085 edge->can_throw_external = bp_unpack_value (&bp, 1);
1086 if (indirect)
1088 if (bp_unpack_value (&bp, 1))
1089 ecf_flags |= ECF_CONST;
1090 if (bp_unpack_value (&bp, 1))
1091 ecf_flags |= ECF_PURE;
1092 if (bp_unpack_value (&bp, 1))
1093 ecf_flags |= ECF_NORETURN;
1094 if (bp_unpack_value (&bp, 1))
1095 ecf_flags |= ECF_MALLOC;
1096 if (bp_unpack_value (&bp, 1))
1097 ecf_flags |= ECF_NOTHROW;
1098 if (bp_unpack_value (&bp, 1))
1099 ecf_flags |= ECF_RETURNS_TWICE;
1100 edge->indirect_info->ecf_flags = ecf_flags;
1105 /* Read a cgraph from IB using the info in FILE_DATA. */
1107 static VEC(symtab_node, heap) *
1108 input_cgraph_1 (struct lto_file_decl_data *file_data,
1109 struct lto_input_block *ib)
1111 enum LTO_symtab_tags tag;
1112 VEC(symtab_node, heap) *nodes = NULL;
1113 symtab_node node;
1114 unsigned i;
1116 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1117 order_base = symtab_order;
1118 while (tag)
1120 if (tag == LTO_symtab_edge)
1121 input_edge (ib, nodes, false);
1122 else if (tag == LTO_symtab_indirect_edge)
1123 input_edge (ib, nodes, true);
1124 else if (tag == LTO_symtab_variable)
1126 node = (symtab_node)input_varpool_node (file_data, ib);
1127 VEC_safe_push (symtab_node, heap, nodes, node);
1128 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1130 else
1132 node = (symtab_node)input_node (file_data, ib, tag, nodes);
1133 if (node == NULL || node->symbol.decl == NULL_TREE)
1134 internal_error ("bytecode stream: found empty cgraph node");
1135 VEC_safe_push (symtab_node, heap, nodes, node);
1136 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1139 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1142 lto_input_toplevel_asms (file_data, order_base);
1144 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1145 #ifdef ENABLE_CHECKING
1146 FOR_EACH_VEC_ELT (symtab_node, nodes, i, node)
1147 gcc_assert (node->symbol.aux || !symtab_function_p (node));
1148 #endif
1149 FOR_EACH_VEC_ELT (symtab_node, nodes, i, node)
1151 int ref;
1152 if (symtab_function_p (node))
1154 ref = (int) (intptr_t) cgraph (node)->global.inlined_to;
1156 /* We share declaration of builtins, so we may read same node twice. */
1157 if (!node->symbol.aux)
1158 continue;
1159 node->symbol.aux = NULL;
1161 /* Fixup inlined_to from reference to pointer. */
1162 if (ref != LCC_NOT_FOUND)
1163 cgraph (node)->global.inlined_to = cgraph (VEC_index (symtab_node, nodes, ref));
1164 else
1165 cgraph (node)->global.inlined_to = NULL;
1168 ref = (int) (intptr_t) node->symbol.same_comdat_group;
1170 /* Fixup same_comdat_group from reference to pointer. */
1171 if (ref != LCC_NOT_FOUND)
1172 node->symbol.same_comdat_group = VEC_index (symtab_node, nodes, ref);
1173 else
1174 node->symbol.same_comdat_group = NULL;
1176 FOR_EACH_VEC_ELT (symtab_node, nodes, i, node)
1177 node->symbol.aux = symtab_function_p (node) ? (void *)1 : NULL;
1178 return nodes;
1181 /* Input ipa_refs. */
1183 static void
1184 input_refs (struct lto_input_block *ib,
1185 VEC(symtab_node, heap) *nodes)
1187 int count;
1188 int idx;
1189 while (true)
1191 symtab_node node;
1192 count = streamer_read_uhwi (ib);
1193 if (!count)
1194 break;
1195 idx = streamer_read_uhwi (ib);
1196 node = VEC_index (symtab_node, nodes, idx);
1197 while (count)
1199 input_ref (ib, node, nodes);
1200 count--;
1206 static struct gcov_ctr_summary lto_gcov_summary;
1208 /* Input profile_info from IB. */
1209 static void
1210 input_profile_summary (struct lto_input_block *ib,
1211 struct lto_file_decl_data *file_data)
1213 unsigned int runs = streamer_read_uhwi (ib);
1214 if (runs)
1216 file_data->profile_info.runs = runs;
1217 file_data->profile_info.sum_max = streamer_read_uhwi (ib);
1222 /* Rescale profile summaries to the same number of runs in the whole unit. */
1224 static void
1225 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1227 struct lto_file_decl_data *file_data;
1228 unsigned int j;
1229 gcov_unsigned_t max_runs = 0;
1230 struct cgraph_node *node;
1231 struct cgraph_edge *edge;
1233 /* Find unit with maximal number of runs. If we ever get serious about
1234 roundoff errors, we might also consider computing smallest common
1235 multiply. */
1236 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1237 if (max_runs < file_data->profile_info.runs)
1238 max_runs = file_data->profile_info.runs;
1240 if (!max_runs)
1241 return;
1243 /* Simple overflow check. We probably don't need to support that many train
1244 runs. Such a large value probably imply data corruption anyway. */
1245 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1247 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1248 INT_MAX / REG_BR_PROB_BASE);
1249 return;
1252 profile_info = &lto_gcov_summary;
1253 lto_gcov_summary.runs = max_runs;
1254 lto_gcov_summary.sum_max = 0;
1256 /* Rescale all units to the maximal number of runs.
1257 sum_max can not be easily merged, as we have no idea what files come from
1258 the same run. We do not use the info anyway, so leave it 0. */
1259 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1260 if (file_data->profile_info.runs)
1262 int scale = ((REG_BR_PROB_BASE * max_runs
1263 + file_data->profile_info.runs / 2)
1264 / file_data->profile_info.runs);
1265 lto_gcov_summary.sum_max = MAX (lto_gcov_summary.sum_max,
1266 (file_data->profile_info.sum_max
1267 * scale
1268 + REG_BR_PROB_BASE / 2)
1269 / REG_BR_PROB_BASE);
1272 /* Watch roundoff errors. */
1273 if (lto_gcov_summary.sum_max < max_runs)
1274 lto_gcov_summary.sum_max = max_runs;
1276 /* If merging already happent at WPA time, we are done. */
1277 if (flag_ltrans)
1278 return;
1280 /* Now compute count_materialization_scale of each node.
1281 During LTRANS we already have values of count_materialization_scale
1282 computed, so just update them. */
1283 FOR_EACH_FUNCTION (node)
1284 if (node->symbol.lto_file_data
1285 && node->symbol.lto_file_data->profile_info.runs)
1287 int scale;
1289 scale =
1290 ((node->count_materialization_scale * max_runs
1291 + node->symbol.lto_file_data->profile_info.runs / 2)
1292 / node->symbol.lto_file_data->profile_info.runs);
1293 node->count_materialization_scale = scale;
1294 if (scale < 0)
1295 fatal_error ("Profile information in %s corrupted",
1296 file_data->file_name);
1298 if (scale == REG_BR_PROB_BASE)
1299 continue;
1300 for (edge = node->callees; edge; edge = edge->next_callee)
1301 edge->count = ((edge->count * scale + REG_BR_PROB_BASE / 2)
1302 / REG_BR_PROB_BASE);
1303 node->count = ((node->count * scale + REG_BR_PROB_BASE / 2)
1304 / REG_BR_PROB_BASE);
1308 /* Input and merge the symtab from each of the .o files passed to
1309 lto1. */
1311 void
1312 input_symtab (void)
1314 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1315 struct lto_file_decl_data *file_data;
1316 unsigned int j = 0;
1317 struct cgraph_node *node;
1319 cgraph_state = CGRAPH_STATE_IPA_SSA;
1321 while ((file_data = file_data_vec[j++]))
1323 const char *data;
1324 size_t len;
1325 struct lto_input_block *ib;
1326 VEC(symtab_node, heap) *nodes;
1328 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1329 &data, &len);
1330 if (!ib)
1331 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1332 input_profile_summary (ib, file_data);
1333 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1334 nodes = input_cgraph_1 (file_data, ib);
1335 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1336 ib, data, len);
1338 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1339 &data, &len);
1340 if (!ib)
1341 fatal_error("cannot find LTO section refs in %s", file_data->file_name);
1342 input_refs (ib, nodes);
1343 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1344 ib, data, len);
1345 if (flag_ltrans)
1346 input_cgraph_opt_summary (nodes);
1347 VEC_free (symtab_node, heap, nodes);
1350 merge_profile_summaries (file_data_vec);
1352 /* Clear out the aux field that was used to store enough state to
1353 tell which nodes should be overwritten. */
1354 FOR_EACH_FUNCTION (node)
1356 /* Some nodes may have been created by cgraph_node. This
1357 happens when the callgraph contains nested functions. If the
1358 node for the parent function was never emitted to the gimple
1359 file, cgraph_node will create a node for it when setting the
1360 context of the nested function. */
1361 if (node->symbol.lto_file_data)
1362 node->symbol.aux = NULL;
1366 /* True when we need optimization summary for NODE. */
1368 static int
1369 output_cgraph_opt_summary_p (struct cgraph_node *node)
1371 return (node->clone_of
1372 && (node->clone.tree_map
1373 || node->clone.args_to_skip
1374 || node->clone.combined_args_to_skip));
1377 /* Output optimization summary for EDGE to OB. */
1378 static void
1379 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1380 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1384 /* Output optimization summary for NODE to OB. */
1386 static void
1387 output_node_opt_summary (struct output_block *ob,
1388 struct cgraph_node *node,
1389 lto_symtab_encoder_t encoder)
1391 unsigned int index;
1392 bitmap_iterator bi;
1393 struct ipa_replace_map *map;
1394 struct bitpack_d bp;
1395 int i;
1396 struct cgraph_edge *e;
1398 if (node->clone.args_to_skip)
1400 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1401 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1402 streamer_write_uhwi (ob, index);
1404 else
1405 streamer_write_uhwi (ob, 0);
1406 if (node->clone.combined_args_to_skip)
1408 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1409 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1410 streamer_write_uhwi (ob, index);
1412 else
1413 streamer_write_uhwi (ob, 0);
1414 streamer_write_uhwi (ob, VEC_length (ipa_replace_map_p,
1415 node->clone.tree_map));
1416 FOR_EACH_VEC_ELT (ipa_replace_map_p, node->clone.tree_map, i, map)
1418 int parm_num;
1419 tree parm;
1421 for (parm_num = 0, parm = DECL_ARGUMENTS (node->symbol.decl); parm;
1422 parm = DECL_CHAIN (parm), parm_num++)
1423 if (map->old_tree == parm)
1424 break;
1425 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1426 mechanism to store function local declarations into summaries. */
1427 gcc_assert (parm);
1428 streamer_write_uhwi (ob, parm_num);
1429 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1430 stream_write_tree (ob, map->new_tree, true);
1431 bp = bitpack_create (ob->main_stream);
1432 bp_pack_value (&bp, map->replace_p, 1);
1433 bp_pack_value (&bp, map->ref_p, 1);
1434 streamer_write_bitpack (&bp);
1437 if (lto_symtab_encoder_in_partition_p (encoder, (symtab_node) node))
1439 for (e = node->callees; e; e = e->next_callee)
1440 output_edge_opt_summary (ob, e);
1441 for (e = node->indirect_calls; e; e = e->next_callee)
1442 output_edge_opt_summary (ob, e);
1446 /* Output optimization summaries stored in callgraph.
1447 At the moment it is the clone info structure. */
1449 static void
1450 output_cgraph_opt_summary (void)
1452 symtab_node node;
1453 int i, n_nodes;
1454 lto_symtab_encoder_t encoder;
1455 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1456 unsigned count = 0;
1458 ob->cgraph_node = NULL;
1459 encoder = ob->decl_state->symtab_node_encoder;
1460 n_nodes = lto_symtab_encoder_size (encoder);
1461 for (i = 0; i < n_nodes; i++)
1462 if (symtab_function_p (node = lto_symtab_encoder_deref (encoder, i))
1463 && output_cgraph_opt_summary_p (cgraph (node)))
1464 count++;
1465 streamer_write_uhwi (ob, count);
1466 for (i = 0; i < n_nodes; i++)
1468 node = lto_symtab_encoder_deref (encoder, i);
1469 if (symtab_function_p (node)
1470 && output_cgraph_opt_summary_p (cgraph (node)))
1472 streamer_write_uhwi (ob, i);
1473 output_node_opt_summary (ob, cgraph (node), encoder);
1476 produce_asm (ob, NULL);
1477 destroy_output_block (ob);
1480 /* Input optimisation summary of EDGE. */
1482 static void
1483 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
1484 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
1488 /* Input optimisation summary of NODE. */
1490 static void
1491 input_node_opt_summary (struct cgraph_node *node,
1492 struct lto_input_block *ib_main,
1493 struct data_in *data_in)
1495 int i;
1496 int count;
1497 int bit;
1498 struct bitpack_d bp;
1499 struct cgraph_edge *e;
1501 count = streamer_read_uhwi (ib_main);
1502 if (count)
1503 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1504 for (i = 0; i < count; i++)
1506 bit = streamer_read_uhwi (ib_main);
1507 bitmap_set_bit (node->clone.args_to_skip, bit);
1509 count = streamer_read_uhwi (ib_main);
1510 if (count)
1511 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1512 for (i = 0; i < count; i++)
1514 bit = streamer_read_uhwi (ib_main);
1515 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1517 count = streamer_read_uhwi (ib_main);
1518 for (i = 0; i < count; i++)
1520 struct ipa_replace_map *map = ggc_alloc_ipa_replace_map ();
1522 VEC_safe_push (ipa_replace_map_p, gc, node->clone.tree_map, map);
1523 map->parm_num = streamer_read_uhwi (ib_main);
1524 map->old_tree = NULL;
1525 map->new_tree = stream_read_tree (ib_main, data_in);
1526 bp = streamer_read_bitpack (ib_main);
1527 map->replace_p = bp_unpack_value (&bp, 1);
1528 map->ref_p = bp_unpack_value (&bp, 1);
1530 for (e = node->callees; e; e = e->next_callee)
1531 input_edge_opt_summary (e, ib_main);
1532 for (e = node->indirect_calls; e; e = e->next_callee)
1533 input_edge_opt_summary (e, ib_main);
1536 /* Read section in file FILE_DATA of length LEN with data DATA. */
1538 static void
1539 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1540 const char *data, size_t len, VEC (symtab_node,
1541 heap) * nodes)
1543 const struct lto_function_header *header =
1544 (const struct lto_function_header *) data;
1545 const int cfg_offset = sizeof (struct lto_function_header);
1546 const int main_offset = cfg_offset + header->cfg_size;
1547 const int string_offset = main_offset + header->main_size;
1548 struct data_in *data_in;
1549 struct lto_input_block ib_main;
1550 unsigned int i;
1551 unsigned int count;
1553 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1554 header->main_size);
1556 data_in =
1557 lto_data_in_create (file_data, (const char *) data + string_offset,
1558 header->string_size, NULL);
1559 count = streamer_read_uhwi (&ib_main);
1561 for (i = 0; i < count; i++)
1563 int ref = streamer_read_uhwi (&ib_main);
1564 input_node_opt_summary (cgraph (VEC_index (symtab_node, nodes, ref)),
1565 &ib_main, data_in);
1567 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1568 len);
1569 lto_data_in_delete (data_in);
1572 /* Input optimization summary of cgraph. */
1574 static void
1575 input_cgraph_opt_summary (VEC (symtab_node, heap) * nodes)
1577 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1578 struct lto_file_decl_data *file_data;
1579 unsigned int j = 0;
1581 while ((file_data = file_data_vec[j++]))
1583 size_t len;
1584 const char *data =
1585 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1586 &len);
1588 if (data)
1589 input_cgraph_opt_section (file_data, data, len, nodes);