Merge trunk version 193672 into gupc branch.
[official-gcc.git] / gcc / lto-cgraph.c
blob7d936efa2c57c17f84f103eb4c0fdd17d5f35a96
1 /* Write and read the cgraph to the memory mapped representation of a
2 .o file.
4 Copyright 2009, 2010, 2011 Free Software Foundation, Inc.
5 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "expr.h"
29 #include "flags.h"
30 #include "params.h"
31 #include "input.h"
32 #include "hashtab.h"
33 #include "langhooks.h"
34 #include "basic-block.h"
35 #include "tree-flow.h"
36 #include "cgraph.h"
37 #include "function.h"
38 #include "ggc.h"
39 #include "diagnostic-core.h"
40 #include "except.h"
41 #include "vec.h"
42 #include "timevar.h"
43 #include "pointer-set.h"
44 #include "lto-streamer.h"
45 #include "data-streamer.h"
46 #include "tree-streamer.h"
47 #include "gcov-io.h"
48 #include "tree-pass.h"
50 static void output_cgraph_opt_summary (void);
51 static void input_cgraph_opt_summary (vec<symtab_node> nodes);
53 /* Number of LDPR values known to GCC. */
54 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
56 /* All node orders are ofsetted by ORDER_BASE. */
57 static int order_base;
59 /* Cgraph streaming is organized as set of record whose type
60 is indicated by a tag. */
61 enum LTO_symtab_tags
63 /* Must leave 0 for the stopper. */
65 /* Cgraph node without body available. */
66 LTO_symtab_unavail_node = 1,
67 /* Cgraph node with function body. */
68 LTO_symtab_analyzed_node,
69 /* Cgraph edges. */
70 LTO_symtab_edge,
71 LTO_symtab_indirect_edge,
72 LTO_symtab_variable,
73 LTO_symtab_last_tag
76 /* Create a new symtab encoder.
77 if FOR_INPUT, the encoder allocate only datastructures needed
78 to read the symtab. */
80 lto_symtab_encoder_t
81 lto_symtab_encoder_new (bool for_input)
83 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
85 if (!for_input)
86 encoder->map = pointer_map_create ();
87 encoder->nodes.create (0);
88 return encoder;
92 /* Delete ENCODER and its components. */
94 void
95 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
97 encoder->nodes.release ();
98 if (encoder->map)
99 pointer_map_destroy (encoder->map);
100 free (encoder);
104 /* Return the existing reference number of NODE in the symtab encoder in
105 output block OB. Assign a new reference if this is the first time
106 NODE is encoded. */
109 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
110 symtab_node node)
112 int ref;
113 void **slot;
115 if (!encoder->map)
117 lto_encoder_entry entry = {node, false, false, false};
119 ref = encoder->nodes.length ();
120 encoder->nodes.safe_push (entry);
121 return ref;
124 slot = pointer_map_contains (encoder->map, node);
125 if (!slot || !*slot)
127 lto_encoder_entry entry = {node, false, false, false};
128 ref = encoder->nodes.length ();
129 if (!slot)
130 slot = pointer_map_insert (encoder->map, node);
131 *slot = (void *) (intptr_t) (ref + 1);
132 encoder->nodes.safe_push (entry);
134 else
135 ref = (size_t) *slot - 1;
137 return ref;
140 /* Remove NODE from encoder. */
142 bool
143 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
144 symtab_node node)
146 void **slot, **last_slot;
147 int index;
148 lto_encoder_entry last_node;
150 slot = pointer_map_contains (encoder->map, node);
151 if (slot == NULL || !*slot)
152 return false;
154 index = (size_t) *slot - 1;
155 gcc_checking_assert (encoder->nodes[index].node == node);
157 /* Remove from vector. We do this by swapping node with the last element
158 of the vector. */
159 last_node = encoder->nodes.pop ();
160 if (last_node.node != node)
162 last_slot = pointer_map_contains (encoder->map, last_node.node);
163 gcc_checking_assert (last_slot && *last_slot);
164 *last_slot = (void *)(size_t) (index + 1);
166 /* Move the last element to the original spot of NODE. */
167 encoder->nodes[index] = last_node;
170 /* Remove element from hash table. */
171 *slot = NULL;
172 return true;
176 /* Return TRUE if we should encode initializer of NODE (if any). */
178 bool
179 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
180 struct cgraph_node *node)
182 int index = lto_symtab_encoder_lookup (encoder, (symtab_node)node);
183 return encoder->nodes[index].body;
186 /* Return TRUE if we should encode body of NODE (if any). */
188 static void
189 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
190 struct cgraph_node *node)
192 int index = lto_symtab_encoder_encode (encoder, (symtab_node)node);
193 gcc_checking_assert (encoder->nodes[index].node == (symtab_node)node);
194 encoder->nodes[index].body = true;
197 /* Return TRUE if we should encode initializer of NODE (if any). */
199 bool
200 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
201 struct varpool_node *node)
203 int index = lto_symtab_encoder_lookup (encoder, (symtab_node)node);
204 if (index == LCC_NOT_FOUND)
205 return false;
206 return encoder->nodes[index].initializer;
209 /* Return TRUE if we should encode initializer of NODE (if any). */
211 static void
212 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
213 struct varpool_node *node)
215 int index = lto_symtab_encoder_lookup (encoder, (symtab_node)node);
216 encoder->nodes[index].initializer = true;
219 /* Return TRUE if we should encode initializer of NODE (if any). */
221 bool
222 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
223 symtab_node node)
225 int index = lto_symtab_encoder_lookup (encoder, (symtab_node)node);
226 if (index == LCC_NOT_FOUND)
227 return false;
228 return encoder->nodes[index].in_partition;
231 /* Return TRUE if we should encode body of NODE (if any). */
233 void
234 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
235 symtab_node node)
237 int index = lto_symtab_encoder_encode (encoder, (symtab_node)node);
238 encoder->nodes[index].in_partition = true;
241 /* Output the cgraph EDGE to OB using ENCODER. */
243 static void
244 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
245 lto_symtab_encoder_t encoder)
247 unsigned int uid;
248 intptr_t ref;
249 struct bitpack_d bp;
251 if (edge->indirect_unknown_callee)
252 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
253 LTO_symtab_indirect_edge);
254 else
255 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
256 LTO_symtab_edge);
258 ref = lto_symtab_encoder_lookup (encoder, (symtab_node)edge->caller);
259 gcc_assert (ref != LCC_NOT_FOUND);
260 streamer_write_hwi_stream (ob->main_stream, ref);
262 if (!edge->indirect_unknown_callee)
264 ref = lto_symtab_encoder_lookup (encoder, (symtab_node)edge->callee);
265 gcc_assert (ref != LCC_NOT_FOUND);
266 streamer_write_hwi_stream (ob->main_stream, ref);
269 streamer_write_hwi_stream (ob->main_stream, edge->count);
271 bp = bitpack_create (ob->main_stream);
272 uid = (!gimple_has_body_p (edge->caller->symbol.decl)
273 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt));
274 bp_pack_enum (&bp, cgraph_inline_failed_enum,
275 CIF_N_REASONS, edge->inline_failed);
276 bp_pack_var_len_unsigned (&bp, uid);
277 bp_pack_var_len_unsigned (&bp, edge->frequency);
278 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
279 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
280 bp_pack_value (&bp, edge->can_throw_external, 1);
281 if (edge->indirect_unknown_callee)
283 int flags = edge->indirect_info->ecf_flags;
284 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
285 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
286 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
287 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
288 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
289 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
290 /* Flags that should not appear on indirect calls. */
291 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
292 | ECF_MAY_BE_ALLOCA
293 | ECF_SIBCALL
294 | ECF_LEAF
295 | ECF_NOVOPS)));
297 streamer_write_bitpack (&bp);
300 /* Return if LIST contain references from other partitions. */
302 bool
303 referenced_from_other_partition_p (struct ipa_ref_list *list, lto_symtab_encoder_t encoder)
305 int i;
306 struct ipa_ref *ref;
307 for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
309 if (ref->referring->symbol.in_other_partition
310 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
311 return true;
313 return false;
316 /* Return true when node is reachable from other partition. */
318 bool
319 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
321 struct cgraph_edge *e;
322 if (!node->analyzed)
323 return false;
324 if (node->global.inlined_to)
325 return false;
326 for (e = node->callers; e; e = e->next_caller)
327 if (e->caller->symbol.in_other_partition
328 || !lto_symtab_encoder_in_partition_p (encoder, (symtab_node)e->caller))
329 return true;
330 return false;
333 /* Return if LIST contain references from other partitions. */
335 bool
336 referenced_from_this_partition_p (struct ipa_ref_list *list,
337 lto_symtab_encoder_t encoder)
339 int i;
340 struct ipa_ref *ref;
341 for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
342 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
343 return true;
344 return false;
347 /* Return true when node is reachable from other partition. */
349 bool
350 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
352 struct cgraph_edge *e;
353 for (e = node->callers; e; e = e->next_caller)
354 if (lto_symtab_encoder_in_partition_p (encoder, (symtab_node)e->caller))
355 return true;
356 return false;
359 /* Output the cgraph NODE to OB. ENCODER is used to find the
360 reference number of NODE->inlined_to. SET is the set of nodes we
361 are writing to the current file. If NODE is not in SET, then NODE
362 is a boundary of a cgraph_node_set and we pretend NODE just has a
363 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
364 that have had their callgraph node written so far. This is used to
365 determine if NODE is a clone of a previously written node. */
367 static void
368 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
369 lto_symtab_encoder_t encoder)
371 unsigned int tag;
372 struct bitpack_d bp;
373 bool boundary_p;
374 intptr_t ref;
375 bool in_other_partition = false;
376 struct cgraph_node *clone_of;
377 struct ipa_opt_pass_d *pass;
378 int i;
380 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, (symtab_node)node);
382 if (node->analyzed && !boundary_p)
383 tag = LTO_symtab_analyzed_node;
384 else
385 tag = LTO_symtab_unavail_node;
387 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
388 tag);
389 streamer_write_hwi_stream (ob->main_stream, node->symbol.order);
391 /* In WPA mode, we only output part of the call-graph. Also, we
392 fake cgraph node attributes. There are two cases that we care.
394 Boundary nodes: There are nodes that are not part of SET but are
395 called from within SET. We artificially make them look like
396 externally visible nodes with no function body.
398 Cherry-picked nodes: These are nodes we pulled from other
399 translation units into SET during IPA-inlining. We make them as
400 local static nodes to prevent clashes with other local statics. */
401 if (boundary_p && node->analyzed && !DECL_EXTERNAL (node->symbol.decl))
403 /* Inline clones can not be part of boundary.
404 gcc_assert (!node->global.inlined_to);
406 FIXME: At the moment they can be, when partition contains an inline
407 clone that is clone of inline clone from outside partition. We can
408 reshape the clone tree and make other tree to be the root, but it
409 needs a bit extra work and will be promplty done by cgraph_remove_node
410 after reading back. */
411 in_other_partition = 1;
414 clone_of = node->clone_of;
415 while (clone_of
416 && (ref = lto_symtab_encoder_lookup (encoder, (symtab_node)clone_of)) == LCC_NOT_FOUND)
417 if (clone_of->prev_sibling_clone)
418 clone_of = clone_of->prev_sibling_clone;
419 else
420 clone_of = clone_of->clone_of;
422 if (LTO_symtab_analyzed_node)
423 gcc_assert (clone_of || !node->clone_of);
424 if (!clone_of)
425 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
426 else
427 streamer_write_hwi_stream (ob->main_stream, ref);
430 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->symbol.decl);
431 streamer_write_hwi_stream (ob->main_stream, node->count);
432 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
434 streamer_write_hwi_stream (ob->main_stream,
435 node->ipa_transforms_to_apply.length ());
436 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
437 streamer_write_hwi_stream (ob->main_stream, pass->pass.static_pass_number);
439 if (tag == LTO_symtab_analyzed_node)
441 if (node->global.inlined_to)
443 ref = lto_symtab_encoder_lookup (encoder, (symtab_node)node->global.inlined_to);
444 gcc_assert (ref != LCC_NOT_FOUND);
446 else
447 ref = LCC_NOT_FOUND;
449 streamer_write_hwi_stream (ob->main_stream, ref);
452 if (node->symbol.same_comdat_group && !boundary_p)
454 ref = lto_symtab_encoder_lookup (encoder,
455 node->symbol.same_comdat_group);
456 gcc_assert (ref != LCC_NOT_FOUND);
458 else
459 ref = LCC_NOT_FOUND;
460 streamer_write_hwi_stream (ob->main_stream, ref);
462 bp = bitpack_create (ob->main_stream);
463 bp_pack_value (&bp, node->local.local, 1);
464 bp_pack_value (&bp, node->symbol.externally_visible, 1);
465 bp_pack_value (&bp, node->local.finalized, 1);
466 bp_pack_value (&bp, node->local.versionable, 1);
467 bp_pack_value (&bp, node->local.can_change_signature, 1);
468 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
469 bp_pack_value (&bp, node->symbol.force_output, 1);
470 bp_pack_value (&bp, node->symbol.address_taken, 1);
471 bp_pack_value (&bp, node->abstract_and_needed, 1);
472 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
473 && !DECL_EXTERNAL (node->symbol.decl)
474 && !DECL_COMDAT (node->symbol.decl)
475 && (reachable_from_other_partition_p (node, encoder)
476 || referenced_from_other_partition_p (&node->symbol.ref_list,
477 encoder)), 1);
478 bp_pack_value (&bp, node->lowered, 1);
479 bp_pack_value (&bp, in_other_partition, 1);
480 /* Real aliases in a boundary become non-aliases. However we still stream
481 alias info on weakrefs.
482 TODO: We lose a bit of information here - when we know that variable is
483 defined in other unit, we may use the info on aliases to resolve
484 symbol1 != symbol2 type tests that we can do only for locally defined objects
485 otherwise. */
486 bp_pack_value (&bp, node->alias && (!boundary_p || DECL_EXTERNAL (node->symbol.decl)), 1);
487 bp_pack_value (&bp, node->frequency, 2);
488 bp_pack_value (&bp, node->only_called_at_startup, 1);
489 bp_pack_value (&bp, node->only_called_at_exit, 1);
490 bp_pack_value (&bp, node->tm_clone, 1);
491 bp_pack_value (&bp, node->thunk.thunk_p && !boundary_p, 1);
492 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
493 LDPR_NUM_KNOWN, node->symbol.resolution);
494 streamer_write_bitpack (&bp);
496 if (node->thunk.thunk_p && !boundary_p)
498 streamer_write_uhwi_stream
499 (ob->main_stream,
500 1 + (node->thunk.this_adjusting != 0) * 2
501 + (node->thunk.virtual_offset_p != 0) * 4);
502 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
503 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
505 if ((node->alias || node->thunk.thunk_p)
506 && (!boundary_p || (node->alias && DECL_EXTERNAL (node->symbol.decl))))
508 streamer_write_hwi_in_range (ob->main_stream, 0, 1,
509 node->thunk.alias != NULL);
510 if (node->thunk.alias != NULL)
511 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
512 node->thunk.alias);
516 /* Output the varpool NODE to OB.
517 If NODE is not in SET, then NODE is a boundary. */
519 static void
520 lto_output_varpool_node (struct lto_simple_output_block *ob, struct varpool_node *node,
521 lto_symtab_encoder_t encoder)
523 bool boundary_p = (node->analyzed
524 && !lto_symtab_encoder_in_partition_p (encoder, (symtab_node)node));
525 struct bitpack_d bp;
526 int ref;
528 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
529 LTO_symtab_variable);
530 streamer_write_hwi_stream (ob->main_stream, node->symbol.order);
531 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->symbol.decl);
532 bp = bitpack_create (ob->main_stream);
533 bp_pack_value (&bp, node->symbol.externally_visible, 1);
534 bp_pack_value (&bp, node->symbol.force_output, 1);
535 bp_pack_value (&bp, node->finalized, 1);
536 bp_pack_value (&bp, node->alias, 1);
537 bp_pack_value (&bp, node->alias_of != NULL, 1);
538 gcc_assert (node->finalized || !node->analyzed);
539 /* Constant pool initializers can be de-unified into individual ltrans units.
540 FIXME: Alternatively at -Os we may want to avoid generating for them the local
541 labels and share them across LTRANS partitions. */
542 if (DECL_IN_CONSTANT_POOL (node->symbol.decl)
543 && !DECL_EXTERNAL (node->symbol.decl)
544 && !DECL_COMDAT (node->symbol.decl))
546 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
547 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
549 else
551 bp_pack_value (&bp, node->analyzed
552 && referenced_from_other_partition_p (&node->symbol.ref_list,
553 encoder), 1);
554 bp_pack_value (&bp, boundary_p && !DECL_EXTERNAL (node->symbol.decl), 1);
555 /* in_other_partition. */
557 streamer_write_bitpack (&bp);
558 if (node->alias_of)
559 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->alias_of);
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 if (profile_info)
598 /* We do not output num, sum_all and run_max, they are not used by
599 GCC profile feedback and they are difficult to merge from multiple
600 units. */
601 gcc_assert (profile_info->runs);
602 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
603 streamer_write_uhwi_stream (ob->main_stream, profile_info->sum_max);
605 else
606 streamer_write_uhwi_stream (ob->main_stream, 0);
609 /* Output all callees or indirect outgoing edges. EDGE must be the first such
610 edge. */
612 static void
613 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
614 struct lto_simple_output_block *ob,
615 lto_symtab_encoder_t encoder)
617 if (!edge)
618 return;
620 /* Output edges in backward direction, so the reconstructed callgraph match
621 and it is easy to associate call sites in the IPA pass summaries. */
622 while (edge->next_callee)
623 edge = edge->next_callee;
624 for (; edge; edge = edge->prev_callee)
625 lto_output_edge (ob, edge, encoder);
628 /* Output the part of the cgraph in SET. */
630 static void
631 output_refs (lto_symtab_encoder_t encoder)
633 lto_symtab_encoder_iterator lsei;
634 struct lto_simple_output_block *ob;
635 int count;
636 struct ipa_ref *ref;
637 int i;
639 ob = lto_create_simple_output_block (LTO_section_refs);
641 for (lsei = lsei_start_in_partition (encoder); !lsei_end_p (lsei);
642 lsei_next_in_partition (&lsei))
644 symtab_node node = lsei_node (lsei);
646 count = ipa_ref_list_nreferences (&node->symbol.ref_list);
647 if (count)
649 streamer_write_uhwi_stream (ob->main_stream, count);
650 streamer_write_uhwi_stream (ob->main_stream,
651 lto_symtab_encoder_lookup (encoder, node));
652 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list,
653 i, ref); i++)
654 lto_output_ref (ob, ref, encoder);
658 streamer_write_uhwi_stream (ob->main_stream, 0);
660 lto_destroy_simple_output_block (ob);
663 /* Add NODE into encoder as well as nodes it is cloned from.
664 Do it in a way so clones appear first. */
666 static void
667 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
668 bool include_body)
670 if (node->clone_of)
671 add_node_to (encoder, node->clone_of, include_body);
672 else if (include_body)
673 lto_set_symtab_encoder_encode_body (encoder, node);
674 lto_symtab_encoder_encode (encoder, (symtab_node)node);
677 /* Add all references in LIST to encoders. */
679 static void
680 add_references (lto_symtab_encoder_t encoder,
681 struct ipa_ref_list *list)
683 int i;
684 struct ipa_ref *ref;
685 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
686 if (is_a <cgraph_node> (ref->referred))
687 add_node_to (encoder, ipa_ref_node (ref), false);
688 else
689 lto_symtab_encoder_encode (encoder, ref->referred);
692 /* Find all symbols we want to stream into given partition and insert them
693 to encoders.
695 The function actually replaces IN_ENCODER by new one. The reason is that
696 streaming code needs clone's origin to be streamed before clone. This
697 means that we need to insert the nodes in specific order. This order is
698 ignored by the partitioning logic earlier. */
700 lto_symtab_encoder_t
701 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
703 struct cgraph_node *node;
704 struct cgraph_edge *edge;
705 int i;
706 lto_symtab_encoder_t encoder;
707 lto_symtab_encoder_iterator lsei;
709 encoder = lto_symtab_encoder_new (false);
711 /* Go over all entries in the IN_ENCODER and duplicate them to
712 ENCODER. At the same time insert masters of clones so
713 every master appears before clone. */
714 for (lsei = lsei_start_function_in_partition (in_encoder);
715 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
717 node = lsei_cgraph_node (lsei);
718 add_node_to (encoder, node, true);
719 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)node);
720 add_references (encoder, &node->symbol.ref_list);
722 for (lsei = lsei_start_variable_in_partition (in_encoder);
723 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
725 struct varpool_node *vnode = lsei_varpool_node (lsei);
726 gcc_assert (!vnode->alias || vnode->alias_of);
727 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)vnode);
728 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
729 add_references (encoder, &vnode->symbol.ref_list);
731 /* Pickle in also the initializer of all referenced readonly variables
732 to help folding. Constant pool variables are not shared, so we must
733 pickle those too. */
734 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
736 symtab_node node = lto_symtab_encoder_deref (encoder, i);
737 if (varpool_node *vnode = dyn_cast <varpool_node> (node))
739 if (DECL_INITIAL (vnode->symbol.decl)
740 && !lto_symtab_encoder_encode_initializer_p (encoder,
741 vnode)
742 && const_value_known_p (vnode->symbol.decl))
744 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
745 add_references (encoder, &vnode->symbol.ref_list);
750 /* Go over all the nodes again to include callees that are not in
751 SET. */
752 for (lsei = lsei_start_function_in_partition (encoder);
753 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
755 node = lsei_cgraph_node (lsei);
756 for (edge = node->callees; edge; edge = edge->next_callee)
758 struct cgraph_node *callee = edge->callee;
759 if (!lto_symtab_encoder_in_partition_p (encoder, (symtab_node)callee))
761 /* We should have moved all the inlines. */
762 gcc_assert (!callee->global.inlined_to);
763 add_node_to (encoder, callee, false);
767 lto_symtab_encoder_delete (in_encoder);
768 return encoder;
771 /* Output the part of the symtab in SET and VSET. */
773 void
774 output_symtab (void)
776 struct cgraph_node *node;
777 struct lto_simple_output_block *ob;
778 lto_symtab_encoder_iterator lsei;
779 int i, n_nodes;
780 lto_symtab_encoder_t encoder;
781 static bool asm_nodes_output = false;
783 if (flag_wpa)
784 output_cgraph_opt_summary ();
786 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
788 output_profile_summary (ob);
790 /* An encoder for cgraph nodes should have been created by
791 ipa_write_summaries_1. */
792 gcc_assert (ob->decl_state->symtab_node_encoder);
793 encoder = ob->decl_state->symtab_node_encoder;
795 /* Write out the nodes. We must first output a node and then its clones,
796 otherwise at a time reading back the node there would be nothing to clone
797 from. */
798 n_nodes = lto_symtab_encoder_size (encoder);
799 for (i = 0; i < n_nodes; i++)
801 symtab_node node = lto_symtab_encoder_deref (encoder, i);
802 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
803 lto_output_node (ob, cnode, encoder);
804 else
805 lto_output_varpool_node (ob, varpool (node), encoder);
809 /* Go over the nodes in SET again to write edges. */
810 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
811 lsei_next_function_in_partition (&lsei))
813 node = lsei_cgraph_node (lsei);
814 output_outgoing_cgraph_edges (node->callees, ob, encoder);
815 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
818 streamer_write_uhwi_stream (ob->main_stream, 0);
820 lto_destroy_simple_output_block (ob);
822 /* Emit toplevel asms.
823 When doing WPA we must output every asm just once. Since we do not partition asm
824 nodes at all, output them to first output. This is kind of hack, but should work
825 well. */
826 if (!asm_nodes_output)
828 asm_nodes_output = true;
829 lto_output_toplevel_asms ();
832 output_refs (encoder);
835 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
836 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
837 NODE or to replace the values in it, for instance because the first
838 time we saw it, the function body was not available but now it
839 is. BP is a bitpack with all the bitflags for NODE read from the
840 stream. */
842 static void
843 input_overwrite_node (struct lto_file_decl_data *file_data,
844 struct cgraph_node *node,
845 enum LTO_symtab_tags tag,
846 struct bitpack_d *bp)
848 node->symbol.aux = (void *) tag;
849 node->symbol.lto_file_data = file_data;
851 node->local.local = bp_unpack_value (bp, 1);
852 node->symbol.externally_visible = bp_unpack_value (bp, 1);
853 node->local.finalized = bp_unpack_value (bp, 1);
854 node->local.versionable = bp_unpack_value (bp, 1);
855 node->local.can_change_signature = bp_unpack_value (bp, 1);
856 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
857 node->symbol.force_output = bp_unpack_value (bp, 1);
858 node->symbol.address_taken = bp_unpack_value (bp, 1);
859 node->abstract_and_needed = bp_unpack_value (bp, 1);
860 node->symbol.used_from_other_partition = bp_unpack_value (bp, 1);
861 node->lowered = bp_unpack_value (bp, 1);
862 node->analyzed = tag == LTO_symtab_analyzed_node;
863 node->symbol.in_other_partition = bp_unpack_value (bp, 1);
864 if (node->symbol.in_other_partition
865 /* Avoid updating decl when we are seeing just inline clone.
866 When inlining function that has functions already inlined into it,
867 we produce clones of inline clones.
869 WPA partitioning might put each clone into different unit and
870 we might end up streaming inline clone from other partition
871 to support clone we are interested in. */
872 && (!node->clone_of
873 || node->clone_of->symbol.decl != node->symbol.decl))
875 DECL_EXTERNAL (node->symbol.decl) = 1;
876 TREE_STATIC (node->symbol.decl) = 0;
878 node->alias = bp_unpack_value (bp, 1);
879 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
880 node->only_called_at_startup = bp_unpack_value (bp, 1);
881 node->only_called_at_exit = bp_unpack_value (bp, 1);
882 node->tm_clone = bp_unpack_value (bp, 1);
883 node->thunk.thunk_p = bp_unpack_value (bp, 1);
884 node->symbol.resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
885 LDPR_NUM_KNOWN);
888 /* Read a node from input_block IB. TAG is the node's tag just read.
889 Return the node read or overwriten. */
891 static struct cgraph_node *
892 input_node (struct lto_file_decl_data *file_data,
893 struct lto_input_block *ib,
894 enum LTO_symtab_tags tag,
895 vec<symtab_node> nodes)
897 tree fn_decl;
898 struct cgraph_node *node;
899 struct bitpack_d bp;
900 unsigned decl_index;
901 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
902 int clone_ref;
903 int order;
904 int i, count;
906 order = streamer_read_hwi (ib) + order_base;
907 clone_ref = streamer_read_hwi (ib);
909 decl_index = streamer_read_uhwi (ib);
910 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
912 if (clone_ref != LCC_NOT_FOUND)
914 node = cgraph_clone_node (cgraph (nodes[clone_ref]), fn_decl,
915 0, CGRAPH_FREQ_BASE, false,
916 vec<cgraph_edge_p>(), false);
918 else
919 node = cgraph_get_create_node (fn_decl);
921 node->symbol.order = order;
922 if (order >= symtab_order)
923 symtab_order = order + 1;
925 node->count = streamer_read_hwi (ib);
926 node->count_materialization_scale = streamer_read_hwi (ib);
928 count = streamer_read_hwi (ib);
929 node->ipa_transforms_to_apply = vec<ipa_opt_pass>();
930 for (i = 0; i < count; i++)
932 struct opt_pass *pass;
933 int pid = streamer_read_hwi (ib);
935 gcc_assert (pid < passes_by_id_size);
936 pass = passes_by_id[pid];
937 node->ipa_transforms_to_apply.safe_push ((struct ipa_opt_pass_d *) pass);
940 if (tag == LTO_symtab_analyzed_node)
941 ref = streamer_read_hwi (ib);
943 ref2 = streamer_read_hwi (ib);
945 /* Make sure that we have not read this node before. Nodes that
946 have already been read will have their tag stored in the 'aux'
947 field. Since built-in functions can be referenced in multiple
948 functions, they are expected to be read more than once. */
949 if (node->symbol.aux && !DECL_BUILT_IN (node->symbol.decl))
950 internal_error ("bytecode stream: found multiple instances of cgraph "
951 "node %d", node->uid);
953 bp = streamer_read_bitpack (ib);
954 input_overwrite_node (file_data, node, tag, &bp);
956 /* Store a reference for now, and fix up later to be a pointer. */
957 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
959 /* Store a reference for now, and fix up later to be a pointer. */
960 node->symbol.same_comdat_group = (symtab_node) (intptr_t) ref2;
962 if (node->thunk.thunk_p)
964 int type = streamer_read_uhwi (ib);
965 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
966 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
968 node->thunk.fixed_offset = fixed_offset;
969 node->thunk.this_adjusting = (type & 2);
970 node->thunk.virtual_value = virtual_value;
971 node->thunk.virtual_offset_p = (type & 4);
973 if (node->thunk.thunk_p || node->alias)
975 if (streamer_read_hwi_in_range (ib, "alias nonzero flag", 0, 1))
977 decl_index = streamer_read_uhwi (ib);
978 node->thunk.alias = lto_file_decl_data_get_fn_decl (file_data,
979 decl_index);
982 return node;
985 /* Read a node from input_block IB. TAG is the node's tag just read.
986 Return the node read or overwriten. */
988 static struct varpool_node *
989 input_varpool_node (struct lto_file_decl_data *file_data,
990 struct lto_input_block *ib)
992 int decl_index;
993 tree var_decl;
994 struct varpool_node *node;
995 struct bitpack_d bp;
996 int ref = LCC_NOT_FOUND;
997 bool non_null_aliasof;
998 int order;
1000 order = streamer_read_hwi (ib) + order_base;
1001 decl_index = streamer_read_uhwi (ib);
1002 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1003 node = varpool_node_for_decl (var_decl);
1004 node->symbol.order = order;
1005 if (order >= symtab_order)
1006 symtab_order = order + 1;
1007 node->symbol.lto_file_data = file_data;
1009 bp = streamer_read_bitpack (ib);
1010 node->symbol.externally_visible = bp_unpack_value (&bp, 1);
1011 node->symbol.force_output = bp_unpack_value (&bp, 1);
1012 node->finalized = bp_unpack_value (&bp, 1);
1013 node->alias = bp_unpack_value (&bp, 1);
1014 non_null_aliasof = bp_unpack_value (&bp, 1);
1015 node->symbol.used_from_other_partition = bp_unpack_value (&bp, 1);
1016 node->symbol.in_other_partition = bp_unpack_value (&bp, 1);
1017 node->analyzed = (node->finalized && (!node->alias || !node->symbol.in_other_partition));
1018 if (node->symbol.in_other_partition)
1020 DECL_EXTERNAL (node->symbol.decl) = 1;
1021 TREE_STATIC (node->symbol.decl) = 0;
1023 if (non_null_aliasof)
1025 decl_index = streamer_read_uhwi (ib);
1026 node->alias_of = lto_file_decl_data_get_var_decl (file_data, decl_index);
1028 ref = streamer_read_hwi (ib);
1029 /* Store a reference for now, and fix up later to be a pointer. */
1030 node->symbol.same_comdat_group = (symtab_node) (intptr_t) ref;
1031 node->symbol.resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1032 LDPR_NUM_KNOWN);
1034 return node;
1037 /* Read a node from input_block IB. TAG is the node's tag just read.
1038 Return the node read or overwriten. */
1040 static void
1041 input_ref (struct lto_input_block *ib,
1042 symtab_node referring_node,
1043 vec<symtab_node> nodes)
1045 symtab_node node = NULL;
1046 struct bitpack_d bp;
1047 enum ipa_ref_use use;
1049 bp = streamer_read_bitpack (ib);
1050 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1051 node = nodes[streamer_read_hwi (ib)];
1052 ipa_record_reference (referring_node, node, use, NULL);
1055 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1056 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1057 edge being read is indirect (in the sense that it has
1058 indirect_unknown_callee set). */
1060 static void
1061 input_edge (struct lto_input_block *ib, vec<symtab_node> nodes,
1062 bool indirect)
1064 struct cgraph_node *caller, *callee;
1065 struct cgraph_edge *edge;
1066 unsigned int stmt_id;
1067 gcov_type count;
1068 int freq;
1069 cgraph_inline_failed_t inline_failed;
1070 struct bitpack_d bp;
1071 int ecf_flags = 0;
1073 caller = cgraph (nodes[streamer_read_hwi (ib)]);
1074 if (caller == NULL || caller->symbol.decl == NULL_TREE)
1075 internal_error ("bytecode stream: no caller found while reading edge");
1077 if (!indirect)
1079 callee = cgraph (nodes[streamer_read_hwi (ib)]);
1080 if (callee == NULL || callee->symbol.decl == NULL_TREE)
1081 internal_error ("bytecode stream: no callee found while reading edge");
1083 else
1084 callee = NULL;
1086 count = (gcov_type) streamer_read_hwi (ib);
1088 bp = streamer_read_bitpack (ib);
1089 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_enum, CIF_N_REASONS);
1090 stmt_id = bp_unpack_var_len_unsigned (&bp);
1091 freq = (int) bp_unpack_var_len_unsigned (&bp);
1093 if (indirect)
1094 edge = cgraph_create_indirect_edge (caller, NULL, 0, count, freq);
1095 else
1096 edge = cgraph_create_edge (caller, callee, NULL, count, freq);
1098 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1099 edge->lto_stmt_uid = stmt_id;
1100 edge->inline_failed = inline_failed;
1101 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1102 edge->can_throw_external = bp_unpack_value (&bp, 1);
1103 if (indirect)
1105 if (bp_unpack_value (&bp, 1))
1106 ecf_flags |= ECF_CONST;
1107 if (bp_unpack_value (&bp, 1))
1108 ecf_flags |= ECF_PURE;
1109 if (bp_unpack_value (&bp, 1))
1110 ecf_flags |= ECF_NORETURN;
1111 if (bp_unpack_value (&bp, 1))
1112 ecf_flags |= ECF_MALLOC;
1113 if (bp_unpack_value (&bp, 1))
1114 ecf_flags |= ECF_NOTHROW;
1115 if (bp_unpack_value (&bp, 1))
1116 ecf_flags |= ECF_RETURNS_TWICE;
1117 edge->indirect_info->ecf_flags = ecf_flags;
1122 /* Read a cgraph from IB using the info in FILE_DATA. */
1124 static vec<symtab_node>
1125 input_cgraph_1 (struct lto_file_decl_data *file_data,
1126 struct lto_input_block *ib)
1128 enum LTO_symtab_tags tag;
1129 vec<symtab_node> nodes = vec<symtab_node>();
1130 symtab_node node;
1131 unsigned i;
1133 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1134 order_base = symtab_order;
1135 while (tag)
1137 if (tag == LTO_symtab_edge)
1138 input_edge (ib, nodes, false);
1139 else if (tag == LTO_symtab_indirect_edge)
1140 input_edge (ib, nodes, true);
1141 else if (tag == LTO_symtab_variable)
1143 node = (symtab_node)input_varpool_node (file_data, ib);
1144 nodes.safe_push (node);
1145 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1147 else
1149 node = (symtab_node)input_node (file_data, ib, tag, nodes);
1150 if (node == NULL || node->symbol.decl == NULL_TREE)
1151 internal_error ("bytecode stream: found empty cgraph node");
1152 nodes.safe_push (node);
1153 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1156 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1159 lto_input_toplevel_asms (file_data, order_base);
1161 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1162 #ifdef ENABLE_CHECKING
1163 FOR_EACH_VEC_ELT (nodes, i, node)
1164 gcc_assert (node->symbol.aux || !is_a <cgraph_node> (node));
1165 #endif
1166 FOR_EACH_VEC_ELT (nodes, i, node)
1168 int ref;
1169 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
1171 ref = (int) (intptr_t) cnode->global.inlined_to;
1173 /* We share declaration of builtins, so we may read same node twice. */
1174 if (!node->symbol.aux)
1175 continue;
1176 node->symbol.aux = NULL;
1178 /* Fixup inlined_to from reference to pointer. */
1179 if (ref != LCC_NOT_FOUND)
1180 cgraph (node)->global.inlined_to = cgraph (nodes[ref]);
1181 else
1182 cnode->global.inlined_to = NULL;
1185 ref = (int) (intptr_t) node->symbol.same_comdat_group;
1187 /* Fixup same_comdat_group from reference to pointer. */
1188 if (ref != LCC_NOT_FOUND)
1189 node->symbol.same_comdat_group = nodes[ref];
1190 else
1191 node->symbol.same_comdat_group = NULL;
1193 FOR_EACH_VEC_ELT (nodes, i, node)
1194 node->symbol.aux = is_a <cgraph_node> (node) ? (void *)1 : NULL;
1195 return nodes;
1198 /* Input ipa_refs. */
1200 static void
1201 input_refs (struct lto_input_block *ib,
1202 vec<symtab_node> nodes)
1204 int count;
1205 int idx;
1206 while (true)
1208 symtab_node node;
1209 count = streamer_read_uhwi (ib);
1210 if (!count)
1211 break;
1212 idx = streamer_read_uhwi (ib);
1213 node = nodes[idx];
1214 while (count)
1216 input_ref (ib, node, nodes);
1217 count--;
1223 static struct gcov_ctr_summary lto_gcov_summary;
1225 /* Input profile_info from IB. */
1226 static void
1227 input_profile_summary (struct lto_input_block *ib,
1228 struct lto_file_decl_data *file_data)
1230 unsigned int runs = streamer_read_uhwi (ib);
1231 if (runs)
1233 file_data->profile_info.runs = runs;
1234 file_data->profile_info.sum_max = streamer_read_uhwi (ib);
1239 /* Rescale profile summaries to the same number of runs in the whole unit. */
1241 static void
1242 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1244 struct lto_file_decl_data *file_data;
1245 unsigned int j;
1246 gcov_unsigned_t max_runs = 0;
1247 struct cgraph_node *node;
1248 struct cgraph_edge *edge;
1250 /* Find unit with maximal number of runs. If we ever get serious about
1251 roundoff errors, we might also consider computing smallest common
1252 multiply. */
1253 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1254 if (max_runs < file_data->profile_info.runs)
1255 max_runs = file_data->profile_info.runs;
1257 if (!max_runs)
1258 return;
1260 /* Simple overflow check. We probably don't need to support that many train
1261 runs. Such a large value probably imply data corruption anyway. */
1262 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1264 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1265 INT_MAX / REG_BR_PROB_BASE);
1266 return;
1269 profile_info = &lto_gcov_summary;
1270 lto_gcov_summary.runs = max_runs;
1271 lto_gcov_summary.sum_max = 0;
1273 /* Rescale all units to the maximal number of runs.
1274 sum_max can not be easily merged, as we have no idea what files come from
1275 the same run. We do not use the info anyway, so leave it 0. */
1276 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1277 if (file_data->profile_info.runs)
1279 int scale = ((REG_BR_PROB_BASE * max_runs
1280 + file_data->profile_info.runs / 2)
1281 / file_data->profile_info.runs);
1282 lto_gcov_summary.sum_max = MAX (lto_gcov_summary.sum_max,
1283 (file_data->profile_info.sum_max
1284 * scale
1285 + REG_BR_PROB_BASE / 2)
1286 / REG_BR_PROB_BASE);
1289 /* Watch roundoff errors. */
1290 if (lto_gcov_summary.sum_max < max_runs)
1291 lto_gcov_summary.sum_max = max_runs;
1293 /* If merging already happent at WPA time, we are done. */
1294 if (flag_ltrans)
1295 return;
1297 /* Now compute count_materialization_scale of each node.
1298 During LTRANS we already have values of count_materialization_scale
1299 computed, so just update them. */
1300 FOR_EACH_FUNCTION (node)
1301 if (node->symbol.lto_file_data
1302 && node->symbol.lto_file_data->profile_info.runs)
1304 int scale;
1306 scale =
1307 ((node->count_materialization_scale * max_runs
1308 + node->symbol.lto_file_data->profile_info.runs / 2)
1309 / node->symbol.lto_file_data->profile_info.runs);
1310 node->count_materialization_scale = scale;
1311 if (scale < 0)
1312 fatal_error ("Profile information in %s corrupted",
1313 file_data->file_name);
1315 if (scale == REG_BR_PROB_BASE)
1316 continue;
1317 for (edge = node->callees; edge; edge = edge->next_callee)
1318 edge->count = ((edge->count * scale + REG_BR_PROB_BASE / 2)
1319 / REG_BR_PROB_BASE);
1320 node->count = ((node->count * scale + REG_BR_PROB_BASE / 2)
1321 / REG_BR_PROB_BASE);
1325 /* Input and merge the symtab from each of the .o files passed to
1326 lto1. */
1328 void
1329 input_symtab (void)
1331 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1332 struct lto_file_decl_data *file_data;
1333 unsigned int j = 0;
1334 struct cgraph_node *node;
1336 cgraph_state = CGRAPH_STATE_IPA_SSA;
1338 while ((file_data = file_data_vec[j++]))
1340 const char *data;
1341 size_t len;
1342 struct lto_input_block *ib;
1343 vec<symtab_node> nodes;
1345 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1346 &data, &len);
1347 if (!ib)
1348 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1349 input_profile_summary (ib, file_data);
1350 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1351 nodes = input_cgraph_1 (file_data, ib);
1352 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1353 ib, data, len);
1355 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1356 &data, &len);
1357 if (!ib)
1358 fatal_error("cannot find LTO section refs in %s", file_data->file_name);
1359 input_refs (ib, nodes);
1360 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1361 ib, data, len);
1362 if (flag_ltrans)
1363 input_cgraph_opt_summary (nodes);
1364 nodes.release ();
1367 merge_profile_summaries (file_data_vec);
1369 /* Clear out the aux field that was used to store enough state to
1370 tell which nodes should be overwritten. */
1371 FOR_EACH_FUNCTION (node)
1373 /* Some nodes may have been created by cgraph_node. This
1374 happens when the callgraph contains nested functions. If the
1375 node for the parent function was never emitted to the gimple
1376 file, cgraph_node will create a node for it when setting the
1377 context of the nested function. */
1378 if (node->symbol.lto_file_data)
1379 node->symbol.aux = NULL;
1383 /* True when we need optimization summary for NODE. */
1385 static int
1386 output_cgraph_opt_summary_p (struct cgraph_node *node)
1388 return (node->clone_of
1389 && (node->clone.tree_map
1390 || node->clone.args_to_skip
1391 || node->clone.combined_args_to_skip));
1394 /* Output optimization summary for EDGE to OB. */
1395 static void
1396 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1397 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1401 /* Output optimization summary for NODE to OB. */
1403 static void
1404 output_node_opt_summary (struct output_block *ob,
1405 struct cgraph_node *node,
1406 lto_symtab_encoder_t encoder)
1408 unsigned int index;
1409 bitmap_iterator bi;
1410 struct ipa_replace_map *map;
1411 struct bitpack_d bp;
1412 int i;
1413 struct cgraph_edge *e;
1415 if (node->clone.args_to_skip)
1417 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1418 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1419 streamer_write_uhwi (ob, index);
1421 else
1422 streamer_write_uhwi (ob, 0);
1423 if (node->clone.combined_args_to_skip)
1425 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1426 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1427 streamer_write_uhwi (ob, index);
1429 else
1430 streamer_write_uhwi (ob, 0);
1431 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1432 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1434 int parm_num;
1435 tree parm;
1437 for (parm_num = 0, parm = DECL_ARGUMENTS (node->symbol.decl); parm;
1438 parm = DECL_CHAIN (parm), parm_num++)
1439 if (map->old_tree == parm)
1440 break;
1441 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1442 mechanism to store function local declarations into summaries. */
1443 gcc_assert (parm);
1444 streamer_write_uhwi (ob, parm_num);
1445 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1446 stream_write_tree (ob, map->new_tree, true);
1447 bp = bitpack_create (ob->main_stream);
1448 bp_pack_value (&bp, map->replace_p, 1);
1449 bp_pack_value (&bp, map->ref_p, 1);
1450 streamer_write_bitpack (&bp);
1453 if (lto_symtab_encoder_in_partition_p (encoder, (symtab_node) node))
1455 for (e = node->callees; e; e = e->next_callee)
1456 output_edge_opt_summary (ob, e);
1457 for (e = node->indirect_calls; e; e = e->next_callee)
1458 output_edge_opt_summary (ob, e);
1462 /* Output optimization summaries stored in callgraph.
1463 At the moment it is the clone info structure. */
1465 static void
1466 output_cgraph_opt_summary (void)
1468 int i, n_nodes;
1469 lto_symtab_encoder_t encoder;
1470 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1471 unsigned count = 0;
1473 ob->cgraph_node = NULL;
1474 encoder = ob->decl_state->symtab_node_encoder;
1475 n_nodes = lto_symtab_encoder_size (encoder);
1476 for (i = 0; i < n_nodes; i++)
1478 symtab_node node = lto_symtab_encoder_deref (encoder, i);
1479 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1480 if (cnode && output_cgraph_opt_summary_p (cnode))
1481 count++;
1483 streamer_write_uhwi (ob, count);
1484 for (i = 0; i < n_nodes; i++)
1486 symtab_node node = lto_symtab_encoder_deref (encoder, i);
1487 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1488 if (cnode && output_cgraph_opt_summary_p (cnode))
1490 streamer_write_uhwi (ob, i);
1491 output_node_opt_summary (ob, cnode, encoder);
1494 produce_asm (ob, NULL);
1495 destroy_output_block (ob);
1498 /* Input optimisation summary of EDGE. */
1500 static void
1501 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
1502 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
1506 /* Input optimisation summary of NODE. */
1508 static void
1509 input_node_opt_summary (struct cgraph_node *node,
1510 struct lto_input_block *ib_main,
1511 struct data_in *data_in)
1513 int i;
1514 int count;
1515 int bit;
1516 struct bitpack_d bp;
1517 struct cgraph_edge *e;
1519 count = streamer_read_uhwi (ib_main);
1520 if (count)
1521 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1522 for (i = 0; i < count; i++)
1524 bit = streamer_read_uhwi (ib_main);
1525 bitmap_set_bit (node->clone.args_to_skip, bit);
1527 count = streamer_read_uhwi (ib_main);
1528 if (count)
1529 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1530 for (i = 0; i < count; i++)
1532 bit = streamer_read_uhwi (ib_main);
1533 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1535 count = streamer_read_uhwi (ib_main);
1536 for (i = 0; i < count; i++)
1538 struct ipa_replace_map *map = ggc_alloc_ipa_replace_map ();
1540 vec_safe_push (node->clone.tree_map, map);
1541 map->parm_num = streamer_read_uhwi (ib_main);
1542 map->old_tree = NULL;
1543 map->new_tree = stream_read_tree (ib_main, data_in);
1544 bp = streamer_read_bitpack (ib_main);
1545 map->replace_p = bp_unpack_value (&bp, 1);
1546 map->ref_p = bp_unpack_value (&bp, 1);
1548 for (e = node->callees; e; e = e->next_callee)
1549 input_edge_opt_summary (e, ib_main);
1550 for (e = node->indirect_calls; e; e = e->next_callee)
1551 input_edge_opt_summary (e, ib_main);
1554 /* Read section in file FILE_DATA of length LEN with data DATA. */
1556 static void
1557 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1558 const char *data, size_t len,
1559 vec<symtab_node> nodes)
1561 const struct lto_function_header *header =
1562 (const struct lto_function_header *) data;
1563 const int cfg_offset = sizeof (struct lto_function_header);
1564 const int main_offset = cfg_offset + header->cfg_size;
1565 const int string_offset = main_offset + header->main_size;
1566 struct data_in *data_in;
1567 struct lto_input_block ib_main;
1568 unsigned int i;
1569 unsigned int count;
1571 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1572 header->main_size);
1574 data_in =
1575 lto_data_in_create (file_data, (const char *) data + string_offset,
1576 header->string_size,
1577 vec<ld_plugin_symbol_resolution_t>());
1578 count = streamer_read_uhwi (&ib_main);
1580 for (i = 0; i < count; i++)
1582 int ref = streamer_read_uhwi (&ib_main);
1583 input_node_opt_summary (cgraph (nodes[ref]),
1584 &ib_main, data_in);
1586 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1587 len);
1588 lto_data_in_delete (data_in);
1591 /* Input optimization summary of cgraph. */
1593 static void
1594 input_cgraph_opt_summary (vec<symtab_node> nodes)
1596 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1597 struct lto_file_decl_data *file_data;
1598 unsigned int j = 0;
1600 while ((file_data = file_data_vec[j++]))
1602 size_t len;
1603 const char *data =
1604 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1605 &len);
1607 if (data)
1608 input_cgraph_opt_section (file_data, data, len, nodes);