2014-01-17 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / lto-cgraph.c
blob5c774b8ee81ef99ea46c324b198c5b4ebd620652
1 /* Write and read the cgraph to the memory mapped representation of a
2 .o file.
4 Copyright (C) 2009-2014 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 "stringpool.h"
29 #include "basic-block.h"
30 #include "tree-ssa-alias.h"
31 #include "internal-fn.h"
32 #include "gimple-expr.h"
33 #include "is-a.h"
34 #include "gimple.h"
35 #include "expr.h"
36 #include "flags.h"
37 #include "params.h"
38 #include "input.h"
39 #include "hashtab.h"
40 #include "langhooks.h"
41 #include "bitmap.h"
42 #include "function.h"
43 #include "diagnostic-core.h"
44 #include "except.h"
45 #include "timevar.h"
46 #include "lto-streamer.h"
47 #include "data-streamer.h"
48 #include "tree-streamer.h"
49 #include "gcov-io.h"
50 #include "tree-pass.h"
51 #include "profile.h"
52 #include "context.h"
53 #include "pass_manager.h"
54 #include "ipa-utils.h"
56 static void output_cgraph_opt_summary (void);
57 static void input_cgraph_opt_summary (vec<symtab_node *> nodes);
59 /* Number of LDPR values known to GCC. */
60 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
62 /* All node orders are ofsetted by ORDER_BASE. */
63 static int order_base;
65 /* Cgraph streaming is organized as set of record whose type
66 is indicated by a tag. */
67 enum LTO_symtab_tags
69 /* Must leave 0 for the stopper. */
71 /* Cgraph node without body available. */
72 LTO_symtab_unavail_node = 1,
73 /* Cgraph node with function body. */
74 LTO_symtab_analyzed_node,
75 /* Cgraph edges. */
76 LTO_symtab_edge,
77 LTO_symtab_indirect_edge,
78 LTO_symtab_variable,
79 LTO_symtab_last_tag
82 /* Create a new symtab encoder.
83 if FOR_INPUT, the encoder allocate only datastructures needed
84 to read the symtab. */
86 lto_symtab_encoder_t
87 lto_symtab_encoder_new (bool for_input)
89 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
91 if (!for_input)
92 encoder->map = pointer_map_create ();
93 encoder->nodes.create (0);
94 return encoder;
98 /* Delete ENCODER and its components. */
100 void
101 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
103 encoder->nodes.release ();
104 if (encoder->map)
105 pointer_map_destroy (encoder->map);
106 free (encoder);
110 /* Return the existing reference number of NODE in the symtab encoder in
111 output block OB. Assign a new reference if this is the first time
112 NODE is encoded. */
115 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
116 symtab_node *node)
118 int ref;
119 void **slot;
121 if (!encoder->map)
123 lto_encoder_entry entry = {node, false, false, false};
125 ref = encoder->nodes.length ();
126 encoder->nodes.safe_push (entry);
127 return ref;
130 slot = pointer_map_contains (encoder->map, node);
131 if (!slot || !*slot)
133 lto_encoder_entry entry = {node, false, false, false};
134 ref = encoder->nodes.length ();
135 if (!slot)
136 slot = pointer_map_insert (encoder->map, node);
137 *slot = (void *) (intptr_t) (ref + 1);
138 encoder->nodes.safe_push (entry);
140 else
141 ref = (size_t) *slot - 1;
143 return ref;
146 /* Remove NODE from encoder. */
148 bool
149 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
150 symtab_node *node)
152 void **slot, **last_slot;
153 int index;
154 lto_encoder_entry last_node;
156 slot = pointer_map_contains (encoder->map, node);
157 if (slot == NULL || !*slot)
158 return false;
160 index = (size_t) *slot - 1;
161 gcc_checking_assert (encoder->nodes[index].node == node);
163 /* Remove from vector. We do this by swapping node with the last element
164 of the vector. */
165 last_node = encoder->nodes.pop ();
166 if (last_node.node != node)
168 last_slot = pointer_map_contains (encoder->map, last_node.node);
169 gcc_checking_assert (last_slot && *last_slot);
170 *last_slot = (void *)(size_t) (index + 1);
172 /* Move the last element to the original spot of NODE. */
173 encoder->nodes[index] = last_node;
176 /* Remove element from hash table. */
177 *slot = NULL;
178 return true;
182 /* Return TRUE if we should encode initializer of NODE (if any). */
184 bool
185 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
186 struct cgraph_node *node)
188 int index = lto_symtab_encoder_lookup (encoder, node);
189 return encoder->nodes[index].body;
192 /* Return TRUE if we should encode body of NODE (if any). */
194 static void
195 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
196 struct cgraph_node *node)
198 int index = lto_symtab_encoder_encode (encoder, node);
199 gcc_checking_assert (encoder->nodes[index].node == node);
200 encoder->nodes[index].body = true;
203 /* Return TRUE if we should encode initializer of NODE (if any). */
205 bool
206 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
207 varpool_node *node)
209 int index = lto_symtab_encoder_lookup (encoder, node);
210 if (index == LCC_NOT_FOUND)
211 return false;
212 return encoder->nodes[index].initializer;
215 /* Return TRUE if we should encode initializer of NODE (if any). */
217 static void
218 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
219 varpool_node *node)
221 int index = lto_symtab_encoder_lookup (encoder, node);
222 encoder->nodes[index].initializer = true;
225 /* Return TRUE if we should encode initializer of NODE (if any). */
227 bool
228 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
229 symtab_node *node)
231 int index = lto_symtab_encoder_lookup (encoder, node);
232 if (index == LCC_NOT_FOUND)
233 return false;
234 return encoder->nodes[index].in_partition;
237 /* Return TRUE if we should encode body of NODE (if any). */
239 void
240 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
241 symtab_node *node)
243 int index = lto_symtab_encoder_encode (encoder, node);
244 encoder->nodes[index].in_partition = true;
247 /* Output the cgraph EDGE to OB using ENCODER. */
249 static void
250 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
251 lto_symtab_encoder_t encoder)
253 unsigned int uid;
254 intptr_t ref;
255 struct bitpack_d bp;
257 if (edge->indirect_unknown_callee)
258 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
259 LTO_symtab_indirect_edge);
260 else
261 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
262 LTO_symtab_edge);
264 ref = lto_symtab_encoder_lookup (encoder, edge->caller);
265 gcc_assert (ref != LCC_NOT_FOUND);
266 streamer_write_hwi_stream (ob->main_stream, ref);
268 if (!edge->indirect_unknown_callee)
270 ref = lto_symtab_encoder_lookup (encoder, edge->callee);
271 gcc_assert (ref != LCC_NOT_FOUND);
272 streamer_write_hwi_stream (ob->main_stream, ref);
275 streamer_write_gcov_count_stream (ob->main_stream, edge->count);
277 bp = bitpack_create (ob->main_stream);
278 uid = (!gimple_has_body_p (edge->caller->decl)
279 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt) + 1);
280 bp_pack_enum (&bp, cgraph_inline_failed_t,
281 CIF_N_REASONS, edge->inline_failed);
282 bp_pack_var_len_unsigned (&bp, uid);
283 bp_pack_var_len_unsigned (&bp, edge->frequency);
284 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
285 bp_pack_value (&bp, edge->speculative, 1);
286 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
287 bp_pack_value (&bp, edge->can_throw_external, 1);
288 if (edge->indirect_unknown_callee)
290 int flags = edge->indirect_info->ecf_flags;
291 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
292 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
293 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
294 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
295 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
296 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
297 /* Flags that should not appear on indirect calls. */
298 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
299 | ECF_MAY_BE_ALLOCA
300 | ECF_SIBCALL
301 | ECF_LEAF
302 | ECF_NOVOPS)));
304 streamer_write_bitpack (&bp);
305 if (edge->indirect_unknown_callee)
307 streamer_write_hwi_stream (ob->main_stream,
308 edge->indirect_info->common_target_id);
309 if (edge->indirect_info->common_target_id)
310 streamer_write_hwi_stream
311 (ob->main_stream, edge->indirect_info->common_target_probability);
315 /* Return if LIST contain references from other partitions. */
317 bool
318 referenced_from_other_partition_p (struct ipa_ref_list *list, lto_symtab_encoder_t encoder)
320 int i;
321 struct ipa_ref *ref;
322 for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
324 if (ref->referring->in_other_partition
325 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
326 return true;
328 return false;
331 /* Return true when node is reachable from other partition. */
333 bool
334 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
336 struct cgraph_edge *e;
337 if (!node->definition)
338 return false;
339 if (node->global.inlined_to)
340 return false;
341 for (e = node->callers; e; e = e->next_caller)
342 if (e->caller->in_other_partition
343 || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
344 return true;
345 return false;
348 /* Return if LIST contain references from other partitions. */
350 bool
351 referenced_from_this_partition_p (struct ipa_ref_list *list,
352 lto_symtab_encoder_t encoder)
354 int i;
355 struct ipa_ref *ref;
356 for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
357 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
358 return true;
359 return false;
362 /* Return true when node is reachable from other partition. */
364 bool
365 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
367 struct cgraph_edge *e;
368 for (e = node->callers; e; e = e->next_caller)
369 if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
370 return true;
371 return false;
374 /* Output the cgraph NODE to OB. ENCODER is used to find the
375 reference number of NODE->inlined_to. SET is the set of nodes we
376 are writing to the current file. If NODE is not in SET, then NODE
377 is a boundary of a cgraph_node_set and we pretend NODE just has a
378 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
379 that have had their callgraph node written so far. This is used to
380 determine if NODE is a clone of a previously written node. */
382 static void
383 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
384 lto_symtab_encoder_t encoder)
386 unsigned int tag;
387 struct bitpack_d bp;
388 bool boundary_p;
389 intptr_t ref;
390 bool in_other_partition = false;
391 struct cgraph_node *clone_of, *ultimate_clone_of;
392 ipa_opt_pass_d *pass;
393 int i;
394 bool alias_p;
396 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
398 if (node->analyzed && !boundary_p)
399 tag = LTO_symtab_analyzed_node;
400 else
401 tag = LTO_symtab_unavail_node;
403 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
404 tag);
405 streamer_write_hwi_stream (ob->main_stream, node->order);
407 /* In WPA mode, we only output part of the call-graph. Also, we
408 fake cgraph node attributes. There are two cases that we care.
410 Boundary nodes: There are nodes that are not part of SET but are
411 called from within SET. We artificially make them look like
412 externally visible nodes with no function body.
414 Cherry-picked nodes: These are nodes we pulled from other
415 translation units into SET during IPA-inlining. We make them as
416 local static nodes to prevent clashes with other local statics. */
417 if (boundary_p && node->analyzed && !DECL_EXTERNAL (node->decl))
419 /* Inline clones can not be part of boundary.
420 gcc_assert (!node->global.inlined_to);
422 FIXME: At the moment they can be, when partition contains an inline
423 clone that is clone of inline clone from outside partition. We can
424 reshape the clone tree and make other tree to be the root, but it
425 needs a bit extra work and will be promplty done by cgraph_remove_node
426 after reading back. */
427 in_other_partition = 1;
430 clone_of = node->clone_of;
431 while (clone_of
432 && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
433 if (clone_of->prev_sibling_clone)
434 clone_of = clone_of->prev_sibling_clone;
435 else
436 clone_of = clone_of->clone_of;
438 /* See if body of the master function is output. If not, we are seeing only
439 an declaration and we do not need to pass down clone tree. */
440 ultimate_clone_of = clone_of;
441 while (ultimate_clone_of && ultimate_clone_of->clone_of)
442 ultimate_clone_of = ultimate_clone_of->clone_of;
444 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
445 clone_of = NULL;
447 if (tag == LTO_symtab_analyzed_node)
448 gcc_assert (clone_of || !node->clone_of);
449 if (!clone_of)
450 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
451 else
452 streamer_write_hwi_stream (ob->main_stream, ref);
455 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
456 streamer_write_gcov_count_stream (ob->main_stream, node->count);
457 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
459 streamer_write_hwi_stream (ob->main_stream,
460 node->ipa_transforms_to_apply.length ());
461 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
462 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
464 if (tag == LTO_symtab_analyzed_node)
466 if (node->global.inlined_to)
468 ref = lto_symtab_encoder_lookup (encoder, node->global.inlined_to);
469 gcc_assert (ref != LCC_NOT_FOUND);
471 else
472 ref = LCC_NOT_FOUND;
474 streamer_write_hwi_stream (ob->main_stream, ref);
477 if (node->same_comdat_group && !boundary_p)
479 ref = lto_symtab_encoder_lookup (encoder,
480 node->same_comdat_group);
481 gcc_assert (ref != LCC_NOT_FOUND);
483 else
484 ref = LCC_NOT_FOUND;
485 streamer_write_hwi_stream (ob->main_stream, ref);
487 streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
489 bp = bitpack_create (ob->main_stream);
490 bp_pack_value (&bp, node->local.local, 1);
491 bp_pack_value (&bp, node->externally_visible, 1);
492 bp_pack_value (&bp, node->definition, 1);
493 bp_pack_value (&bp, node->local.versionable, 1);
494 bp_pack_value (&bp, node->local.can_change_signature, 1);
495 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
496 bp_pack_value (&bp, node->force_output, 1);
497 bp_pack_value (&bp, node->forced_by_abi, 1);
498 bp_pack_value (&bp, node->unique_name, 1);
499 bp_pack_value (&bp, node->address_taken, 1);
500 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
501 && !DECL_EXTERNAL (node->decl)
502 && !DECL_COMDAT (node->decl)
503 && (reachable_from_other_partition_p (node, encoder)
504 || referenced_from_other_partition_p (&node->ref_list,
505 encoder)), 1);
506 bp_pack_value (&bp, node->lowered, 1);
507 bp_pack_value (&bp, in_other_partition, 1);
508 /* Real aliases in a boundary become non-aliases. However we still stream
509 alias info on weakrefs.
510 TODO: We lose a bit of information here - when we know that variable is
511 defined in other unit, we may use the info on aliases to resolve
512 symbol1 != symbol2 type tests that we can do only for locally defined objects
513 otherwise. */
514 alias_p = node->alias && (!boundary_p || node->weakref);
515 bp_pack_value (&bp, alias_p, 1);
516 bp_pack_value (&bp, node->weakref, 1);
517 bp_pack_value (&bp, node->frequency, 2);
518 bp_pack_value (&bp, node->only_called_at_startup, 1);
519 bp_pack_value (&bp, node->only_called_at_exit, 1);
520 bp_pack_value (&bp, node->tm_clone, 1);
521 bp_pack_value (&bp, node->calls_comdat_local, 1);
522 bp_pack_value (&bp, node->thunk.thunk_p && !boundary_p, 1);
523 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
524 LDPR_NUM_KNOWN, node->resolution);
525 streamer_write_bitpack (&bp);
527 if (node->thunk.thunk_p && !boundary_p)
529 streamer_write_uhwi_stream
530 (ob->main_stream,
531 1 + (node->thunk.this_adjusting != 0) * 2
532 + (node->thunk.virtual_offset_p != 0) * 4);
533 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
534 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
536 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
539 /* Output the varpool NODE to OB.
540 If NODE is not in SET, then NODE is a boundary. */
542 static void
543 lto_output_varpool_node (struct lto_simple_output_block *ob, varpool_node *node,
544 lto_symtab_encoder_t encoder)
546 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
547 struct bitpack_d bp;
548 int ref;
549 bool alias_p;
551 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
552 LTO_symtab_variable);
553 streamer_write_hwi_stream (ob->main_stream, node->order);
554 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
555 bp = bitpack_create (ob->main_stream);
556 bp_pack_value (&bp, node->externally_visible, 1);
557 bp_pack_value (&bp, node->force_output, 1);
558 bp_pack_value (&bp, node->forced_by_abi, 1);
559 bp_pack_value (&bp, node->unique_name, 1);
560 bp_pack_value (&bp, node->definition, 1);
561 alias_p = node->alias && (!boundary_p || node->weakref);
562 bp_pack_value (&bp, alias_p, 1);
563 bp_pack_value (&bp, node->weakref, 1);
564 bp_pack_value (&bp, node->analyzed && !boundary_p, 1);
565 gcc_assert (node->definition || !node->analyzed);
566 /* Constant pool initializers can be de-unified into individual ltrans units.
567 FIXME: Alternatively at -Os we may want to avoid generating for them the local
568 labels and share them across LTRANS partitions. */
569 if (DECL_IN_CONSTANT_POOL (node->decl)
570 && !DECL_EXTERNAL (node->decl)
571 && !DECL_COMDAT (node->decl))
573 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
574 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
576 else
578 bp_pack_value (&bp, node->definition
579 && referenced_from_other_partition_p (&node->ref_list,
580 encoder), 1);
581 bp_pack_value (&bp, node->analyzed
582 && boundary_p && !DECL_EXTERNAL (node->decl), 1);
583 /* in_other_partition. */
585 streamer_write_bitpack (&bp);
586 if (node->same_comdat_group && !boundary_p)
588 ref = lto_symtab_encoder_lookup (encoder,
589 node->same_comdat_group);
590 gcc_assert (ref != LCC_NOT_FOUND);
592 else
593 ref = LCC_NOT_FOUND;
594 streamer_write_hwi_stream (ob->main_stream, ref);
595 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
596 LDPR_NUM_KNOWN, node->resolution);
599 /* Output the varpool NODE to OB.
600 If NODE is not in SET, then NODE is a boundary. */
602 static void
603 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
604 lto_symtab_encoder_t encoder)
606 struct bitpack_d bp;
607 int nref;
608 int uid = ref->lto_stmt_uid;
609 struct cgraph_node *node;
611 bp = bitpack_create (ob->main_stream);
612 bp_pack_value (&bp, ref->use, 2);
613 bp_pack_value (&bp, ref->speculative, 1);
614 streamer_write_bitpack (&bp);
615 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
616 gcc_assert (nref != LCC_NOT_FOUND);
617 streamer_write_hwi_stream (ob->main_stream, nref);
619 node = dyn_cast <cgraph_node> (ref->referring);
620 if (node)
622 if (ref->stmt)
623 uid = gimple_uid (ref->stmt) + 1;
624 streamer_write_hwi_stream (ob->main_stream, uid);
628 /* Stream out profile_summary to OB. */
630 static void
631 output_profile_summary (struct lto_simple_output_block *ob)
633 unsigned h_ix;
634 struct bitpack_d bp;
636 if (profile_info)
638 /* We do not output num and run_max, they are not used by
639 GCC profile feedback and they are difficult to merge from multiple
640 units. */
641 gcc_assert (profile_info->runs);
642 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
643 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
645 /* sum_all is needed for computing the working set with the
646 histogram. */
647 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
649 /* Create and output a bitpack of non-zero histogram entries indices. */
650 bp = bitpack_create (ob->main_stream);
651 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
652 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
653 streamer_write_bitpack (&bp);
654 /* Now stream out only those non-zero entries. */
655 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
657 if (!profile_info->histogram[h_ix].num_counters)
658 continue;
659 streamer_write_gcov_count_stream (ob->main_stream,
660 profile_info->histogram[h_ix].num_counters);
661 streamer_write_gcov_count_stream (ob->main_stream,
662 profile_info->histogram[h_ix].min_value);
663 streamer_write_gcov_count_stream (ob->main_stream,
664 profile_info->histogram[h_ix].cum_value);
666 /* IPA-profile computes hot bb threshold based on cumulated
667 whole program profile. We need to stream it down to ltrans. */
668 if (flag_wpa)
669 streamer_write_gcov_count_stream (ob->main_stream,
670 get_hot_bb_threshold ());
672 else
673 streamer_write_uhwi_stream (ob->main_stream, 0);
676 /* Output all callees or indirect outgoing edges. EDGE must be the first such
677 edge. */
679 static void
680 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
681 struct lto_simple_output_block *ob,
682 lto_symtab_encoder_t encoder)
684 if (!edge)
685 return;
687 /* Output edges in backward direction, so the reconstructed callgraph match
688 and it is easy to associate call sites in the IPA pass summaries. */
689 while (edge->next_callee)
690 edge = edge->next_callee;
691 for (; edge; edge = edge->prev_callee)
692 lto_output_edge (ob, edge, encoder);
695 /* Output the part of the cgraph in SET. */
697 static void
698 output_refs (lto_symtab_encoder_t encoder)
700 lto_symtab_encoder_iterator lsei;
701 struct lto_simple_output_block *ob;
702 int count;
703 struct ipa_ref *ref;
704 int i;
706 ob = lto_create_simple_output_block (LTO_section_refs);
708 for (lsei = lsei_start_in_partition (encoder); !lsei_end_p (lsei);
709 lsei_next_in_partition (&lsei))
711 symtab_node *node = lsei_node (lsei);
713 count = ipa_ref_list_nreferences (&node->ref_list);
714 if (count)
716 streamer_write_gcov_count_stream (ob->main_stream, count);
717 streamer_write_uhwi_stream (ob->main_stream,
718 lto_symtab_encoder_lookup (encoder, node));
719 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list,
720 i, ref); i++)
721 lto_output_ref (ob, ref, encoder);
725 streamer_write_uhwi_stream (ob->main_stream, 0);
727 lto_destroy_simple_output_block (ob);
730 /* Add NODE into encoder as well as nodes it is cloned from.
731 Do it in a way so clones appear first. */
733 static void
734 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
735 bool include_body)
737 if (node->clone_of)
738 add_node_to (encoder, node->clone_of, include_body);
739 else if (include_body)
740 lto_set_symtab_encoder_encode_body (encoder, node);
741 lto_symtab_encoder_encode (encoder, node);
744 /* Add all references in LIST to encoders. */
746 static void
747 add_references (lto_symtab_encoder_t encoder,
748 struct ipa_ref_list *list)
750 int i;
751 struct ipa_ref *ref;
752 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
753 if (is_a <cgraph_node> (ref->referred))
754 add_node_to (encoder, ipa_ref_node (ref), false);
755 else
756 lto_symtab_encoder_encode (encoder, ref->referred);
759 /* Find all symbols we want to stream into given partition and insert them
760 to encoders.
762 The function actually replaces IN_ENCODER by new one. The reason is that
763 streaming code needs clone's origin to be streamed before clone. This
764 means that we need to insert the nodes in specific order. This order is
765 ignored by the partitioning logic earlier. */
767 lto_symtab_encoder_t
768 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
770 struct cgraph_node *node;
771 struct cgraph_edge *edge;
772 int i;
773 lto_symtab_encoder_t encoder;
774 lto_symtab_encoder_iterator lsei;
775 struct pointer_set_t *reachable_call_targets = pointer_set_create ();
777 encoder = lto_symtab_encoder_new (false);
779 /* Go over all entries in the IN_ENCODER and duplicate them to
780 ENCODER. At the same time insert masters of clones so
781 every master appears before clone. */
782 for (lsei = lsei_start_function_in_partition (in_encoder);
783 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
785 node = lsei_cgraph_node (lsei);
786 add_node_to (encoder, node, true);
787 lto_set_symtab_encoder_in_partition (encoder, node);
788 add_references (encoder, &node->ref_list);
789 /* For proper debug info, we need to ship the origins, too. */
790 if (DECL_ABSTRACT_ORIGIN (node->decl))
792 struct cgraph_node *origin_node
793 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (node->decl));
794 add_node_to (encoder, origin_node, true);
797 for (lsei = lsei_start_variable_in_partition (in_encoder);
798 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
800 varpool_node *vnode = lsei_varpool_node (lsei);
802 lto_set_symtab_encoder_in_partition (encoder, vnode);
803 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
804 add_references (encoder, &vnode->ref_list);
805 /* For proper debug info, we need to ship the origins, too. */
806 if (DECL_ABSTRACT_ORIGIN (vnode->decl))
808 varpool_node *origin_node
809 = varpool_get_node (DECL_ABSTRACT_ORIGIN (node->decl));
810 lto_set_symtab_encoder_in_partition (encoder, origin_node);
813 /* Pickle in also the initializer of all referenced readonly variables
814 to help folding. Constant pool variables are not shared, so we must
815 pickle those too. */
816 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
818 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
819 if (varpool_node *vnode = dyn_cast <varpool_node> (node))
821 if (!lto_symtab_encoder_encode_initializer_p (encoder,
822 vnode)
823 && ctor_for_folding (vnode->decl) != error_mark_node)
825 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
826 add_references (encoder, &vnode->ref_list);
831 /* Go over all the nodes again to include callees that are not in
832 SET. */
833 for (lsei = lsei_start_function_in_partition (encoder);
834 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
836 node = lsei_cgraph_node (lsei);
837 for (edge = node->callees; edge; edge = edge->next_callee)
839 struct cgraph_node *callee = edge->callee;
840 if (!lto_symtab_encoder_in_partition_p (encoder, callee))
842 /* We should have moved all the inlines. */
843 gcc_assert (!callee->global.inlined_to);
844 add_node_to (encoder, callee, false);
847 /* Add all possible targets for late devirtualization. */
848 if (flag_devirtualize)
849 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
850 if (edge->indirect_info->polymorphic)
852 unsigned int i;
853 void *cache_token;
854 bool final;
855 vec <cgraph_node *>targets
856 = possible_polymorphic_call_targets
857 (edge, &final, &cache_token);
858 if (!pointer_set_insert (reachable_call_targets,
859 cache_token))
861 for (i = 0; i < targets.length (); i++)
863 struct cgraph_node *callee = targets[i];
865 /* Adding an external declarations into the unit serves
866 no purpose and just increases its boundary. */
867 if (callee->definition
868 && !lto_symtab_encoder_in_partition_p
869 (encoder, callee))
871 gcc_assert (!callee->global.inlined_to);
872 add_node_to (encoder, callee, false);
878 lto_symtab_encoder_delete (in_encoder);
879 pointer_set_destroy (reachable_call_targets);
880 return encoder;
883 /* Output the part of the symtab in SET and VSET. */
885 void
886 output_symtab (void)
888 struct cgraph_node *node;
889 struct lto_simple_output_block *ob;
890 lto_symtab_encoder_iterator lsei;
891 int i, n_nodes;
892 lto_symtab_encoder_t encoder;
893 static bool asm_nodes_output = false;
895 if (flag_wpa)
896 output_cgraph_opt_summary ();
898 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
900 output_profile_summary (ob);
902 /* An encoder for cgraph nodes should have been created by
903 ipa_write_summaries_1. */
904 gcc_assert (ob->decl_state->symtab_node_encoder);
905 encoder = ob->decl_state->symtab_node_encoder;
907 /* Write out the nodes. We must first output a node and then its clones,
908 otherwise at a time reading back the node there would be nothing to clone
909 from. */
910 n_nodes = lto_symtab_encoder_size (encoder);
911 for (i = 0; i < n_nodes; i++)
913 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
914 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
915 lto_output_node (ob, cnode, encoder);
916 else
917 lto_output_varpool_node (ob, varpool (node), encoder);
921 /* Go over the nodes in SET again to write edges. */
922 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
923 lsei_next_function_in_partition (&lsei))
925 node = lsei_cgraph_node (lsei);
926 output_outgoing_cgraph_edges (node->callees, ob, encoder);
927 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
930 streamer_write_uhwi_stream (ob->main_stream, 0);
932 lto_destroy_simple_output_block (ob);
934 /* Emit toplevel asms.
935 When doing WPA we must output every asm just once. Since we do not partition asm
936 nodes at all, output them to first output. This is kind of hack, but should work
937 well. */
938 if (!asm_nodes_output)
940 asm_nodes_output = true;
941 lto_output_toplevel_asms ();
944 output_refs (encoder);
947 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
948 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
949 NODE or to replace the values in it, for instance because the first
950 time we saw it, the function body was not available but now it
951 is. BP is a bitpack with all the bitflags for NODE read from the
952 stream. */
954 static void
955 input_overwrite_node (struct lto_file_decl_data *file_data,
956 struct cgraph_node *node,
957 enum LTO_symtab_tags tag,
958 struct bitpack_d *bp)
960 node->aux = (void *) tag;
961 node->lto_file_data = file_data;
963 node->local.local = bp_unpack_value (bp, 1);
964 node->externally_visible = bp_unpack_value (bp, 1);
965 node->definition = bp_unpack_value (bp, 1);
966 node->local.versionable = bp_unpack_value (bp, 1);
967 node->local.can_change_signature = bp_unpack_value (bp, 1);
968 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
969 node->force_output = bp_unpack_value (bp, 1);
970 node->forced_by_abi = bp_unpack_value (bp, 1);
971 node->unique_name = bp_unpack_value (bp, 1);
972 node->address_taken = bp_unpack_value (bp, 1);
973 node->used_from_other_partition = bp_unpack_value (bp, 1);
974 node->lowered = bp_unpack_value (bp, 1);
975 node->analyzed = tag == LTO_symtab_analyzed_node;
976 node->in_other_partition = bp_unpack_value (bp, 1);
977 if (node->in_other_partition
978 /* Avoid updating decl when we are seeing just inline clone.
979 When inlining function that has functions already inlined into it,
980 we produce clones of inline clones.
982 WPA partitioning might put each clone into different unit and
983 we might end up streaming inline clone from other partition
984 to support clone we are interested in. */
985 && (!node->clone_of
986 || node->clone_of->decl != node->decl))
988 DECL_EXTERNAL (node->decl) = 1;
989 TREE_STATIC (node->decl) = 0;
991 node->alias = bp_unpack_value (bp, 1);
992 node->weakref = bp_unpack_value (bp, 1);
993 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
994 node->only_called_at_startup = bp_unpack_value (bp, 1);
995 node->only_called_at_exit = bp_unpack_value (bp, 1);
996 node->tm_clone = bp_unpack_value (bp, 1);
997 node->calls_comdat_local = bp_unpack_value (bp, 1);
998 node->thunk.thunk_p = bp_unpack_value (bp, 1);
999 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
1000 LDPR_NUM_KNOWN);
1003 /* Return string alias is alias of. */
1005 static tree
1006 get_alias_symbol (tree decl)
1008 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1009 return get_identifier (TREE_STRING_POINTER
1010 (TREE_VALUE (TREE_VALUE (alias))));
1013 /* Read a node from input_block IB. TAG is the node's tag just read.
1014 Return the node read or overwriten. */
1016 static struct cgraph_node *
1017 input_node (struct lto_file_decl_data *file_data,
1018 struct lto_input_block *ib,
1019 enum LTO_symtab_tags tag,
1020 vec<symtab_node *> nodes)
1022 gcc::pass_manager *passes = g->get_passes ();
1023 tree fn_decl;
1024 struct cgraph_node *node;
1025 struct bitpack_d bp;
1026 unsigned decl_index;
1027 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1028 int clone_ref;
1029 int order;
1030 int i, count;
1032 order = streamer_read_hwi (ib) + order_base;
1033 clone_ref = streamer_read_hwi (ib);
1035 decl_index = streamer_read_uhwi (ib);
1036 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1038 if (clone_ref != LCC_NOT_FOUND)
1040 node = cgraph_clone_node (cgraph (nodes[clone_ref]), fn_decl,
1041 0, CGRAPH_FREQ_BASE, false,
1042 vNULL, false, NULL);
1044 else
1046 /* Declaration of functions can be already merged with a declaration
1047 from other input file. We keep cgraph unmerged until after streaming
1048 of ipa passes is done. Alays forcingly create a fresh node. */
1049 node = cgraph_create_empty_node ();
1050 node->decl = fn_decl;
1051 symtab_register_node (node);
1054 node->order = order;
1055 if (order >= symtab_order)
1056 symtab_order = order + 1;
1058 node->count = streamer_read_gcov_count (ib);
1059 node->count_materialization_scale = streamer_read_hwi (ib);
1061 count = streamer_read_hwi (ib);
1062 node->ipa_transforms_to_apply = vNULL;
1063 for (i = 0; i < count; i++)
1065 opt_pass *pass;
1066 int pid = streamer_read_hwi (ib);
1068 gcc_assert (pid < passes->passes_by_id_size);
1069 pass = passes->passes_by_id[pid];
1070 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *) pass);
1073 if (tag == LTO_symtab_analyzed_node)
1074 ref = streamer_read_hwi (ib);
1076 ref2 = streamer_read_hwi (ib);
1078 /* Make sure that we have not read this node before. Nodes that
1079 have already been read will have their tag stored in the 'aux'
1080 field. Since built-in functions can be referenced in multiple
1081 functions, they are expected to be read more than once. */
1082 if (node->aux && !DECL_BUILT_IN (node->decl))
1083 internal_error ("bytecode stream: found multiple instances of cgraph "
1084 "node with uid %d", node->uid);
1086 node->tp_first_run = streamer_read_uhwi (ib);
1088 bp = streamer_read_bitpack (ib);
1090 input_overwrite_node (file_data, node, tag, &bp);
1092 /* Store a reference for now, and fix up later to be a pointer. */
1093 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
1095 /* Store a reference for now, and fix up later to be a pointer. */
1096 node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
1098 if (node->thunk.thunk_p)
1100 int type = streamer_read_uhwi (ib);
1101 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1102 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1104 node->thunk.fixed_offset = fixed_offset;
1105 node->thunk.this_adjusting = (type & 2);
1106 node->thunk.virtual_value = virtual_value;
1107 node->thunk.virtual_offset_p = (type & 4);
1109 if (node->alias && !node->analyzed && node->weakref)
1110 node->alias_target = get_alias_symbol (node->decl);
1111 node->profile_id = streamer_read_hwi (ib);
1112 return node;
1115 /* Read a node from input_block IB. TAG is the node's tag just read.
1116 Return the node read or overwriten. */
1118 static varpool_node *
1119 input_varpool_node (struct lto_file_decl_data *file_data,
1120 struct lto_input_block *ib)
1122 int decl_index;
1123 tree var_decl;
1124 varpool_node *node;
1125 struct bitpack_d bp;
1126 int ref = LCC_NOT_FOUND;
1127 int order;
1129 order = streamer_read_hwi (ib) + order_base;
1130 decl_index = streamer_read_uhwi (ib);
1131 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1133 /* Declaration of functions can be already merged with a declaration
1134 from other input file. We keep cgraph unmerged until after streaming
1135 of ipa passes is done. Alays forcingly create a fresh node. */
1136 node = varpool_create_empty_node ();
1137 node->decl = var_decl;
1138 symtab_register_node (node);
1140 node->order = order;
1141 if (order >= symtab_order)
1142 symtab_order = order + 1;
1143 node->lto_file_data = file_data;
1145 bp = streamer_read_bitpack (ib);
1146 node->externally_visible = bp_unpack_value (&bp, 1);
1147 node->force_output = bp_unpack_value (&bp, 1);
1148 node->forced_by_abi = bp_unpack_value (&bp, 1);
1149 node->unique_name = bp_unpack_value (&bp, 1);
1150 node->definition = bp_unpack_value (&bp, 1);
1151 node->alias = bp_unpack_value (&bp, 1);
1152 node->weakref = bp_unpack_value (&bp, 1);
1153 node->analyzed = bp_unpack_value (&bp, 1);
1154 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1155 node->in_other_partition = bp_unpack_value (&bp, 1);
1156 if (node->in_other_partition)
1158 DECL_EXTERNAL (node->decl) = 1;
1159 TREE_STATIC (node->decl) = 0;
1161 if (node->alias && !node->analyzed && node->weakref)
1162 node->alias_target = get_alias_symbol (node->decl);
1163 ref = streamer_read_hwi (ib);
1164 /* Store a reference for now, and fix up later to be a pointer. */
1165 node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1166 node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1167 LDPR_NUM_KNOWN);
1169 return node;
1172 /* Read a node from input_block IB. TAG is the node's tag just read.
1173 Return the node read or overwriten. */
1175 static void
1176 input_ref (struct lto_input_block *ib,
1177 symtab_node *referring_node,
1178 vec<symtab_node *> nodes)
1180 symtab_node *node = NULL;
1181 struct bitpack_d bp;
1182 enum ipa_ref_use use;
1183 bool speculative;
1184 struct ipa_ref *ref;
1186 bp = streamer_read_bitpack (ib);
1187 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1188 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1189 node = nodes[streamer_read_hwi (ib)];
1190 ref = ipa_record_reference (referring_node, node, use, NULL);
1191 ref->speculative = speculative;
1192 if (is_a <cgraph_node> (referring_node))
1193 ref->lto_stmt_uid = streamer_read_hwi (ib);
1196 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1197 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1198 edge being read is indirect (in the sense that it has
1199 indirect_unknown_callee set). */
1201 static void
1202 input_edge (struct lto_input_block *ib, vec<symtab_node *> nodes,
1203 bool indirect)
1205 struct cgraph_node *caller, *callee;
1206 struct cgraph_edge *edge;
1207 unsigned int stmt_id;
1208 gcov_type count;
1209 int freq;
1210 cgraph_inline_failed_t inline_failed;
1211 struct bitpack_d bp;
1212 int ecf_flags = 0;
1214 caller = cgraph (nodes[streamer_read_hwi (ib)]);
1215 if (caller == NULL || caller->decl == NULL_TREE)
1216 internal_error ("bytecode stream: no caller found while reading edge");
1218 if (!indirect)
1220 callee = cgraph (nodes[streamer_read_hwi (ib)]);
1221 if (callee == NULL || callee->decl == NULL_TREE)
1222 internal_error ("bytecode stream: no callee found while reading edge");
1224 else
1225 callee = NULL;
1227 count = streamer_read_gcov_count (ib);
1229 bp = streamer_read_bitpack (ib);
1230 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_t, CIF_N_REASONS);
1231 stmt_id = bp_unpack_var_len_unsigned (&bp);
1232 freq = (int) bp_unpack_var_len_unsigned (&bp);
1234 if (indirect)
1235 edge = cgraph_create_indirect_edge (caller, NULL, 0, count, freq);
1236 else
1237 edge = cgraph_create_edge (caller, callee, NULL, count, freq);
1239 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1240 edge->speculative = bp_unpack_value (&bp, 1);
1241 edge->lto_stmt_uid = stmt_id;
1242 edge->inline_failed = inline_failed;
1243 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1244 edge->can_throw_external = bp_unpack_value (&bp, 1);
1245 if (indirect)
1247 if (bp_unpack_value (&bp, 1))
1248 ecf_flags |= ECF_CONST;
1249 if (bp_unpack_value (&bp, 1))
1250 ecf_flags |= ECF_PURE;
1251 if (bp_unpack_value (&bp, 1))
1252 ecf_flags |= ECF_NORETURN;
1253 if (bp_unpack_value (&bp, 1))
1254 ecf_flags |= ECF_MALLOC;
1255 if (bp_unpack_value (&bp, 1))
1256 ecf_flags |= ECF_NOTHROW;
1257 if (bp_unpack_value (&bp, 1))
1258 ecf_flags |= ECF_RETURNS_TWICE;
1259 edge->indirect_info->ecf_flags = ecf_flags;
1260 edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1261 if (edge->indirect_info->common_target_id)
1262 edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1267 /* Read a cgraph from IB using the info in FILE_DATA. */
1269 static vec<symtab_node *>
1270 input_cgraph_1 (struct lto_file_decl_data *file_data,
1271 struct lto_input_block *ib)
1273 enum LTO_symtab_tags tag;
1274 vec<symtab_node *> nodes = vNULL;
1275 symtab_node *node;
1276 unsigned i;
1278 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1279 order_base = symtab_order;
1280 while (tag)
1282 if (tag == LTO_symtab_edge)
1283 input_edge (ib, nodes, false);
1284 else if (tag == LTO_symtab_indirect_edge)
1285 input_edge (ib, nodes, true);
1286 else if (tag == LTO_symtab_variable)
1288 node = input_varpool_node (file_data, ib);
1289 nodes.safe_push (node);
1290 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1292 else
1294 node = input_node (file_data, ib, tag, nodes);
1295 if (node == NULL || node->decl == NULL_TREE)
1296 internal_error ("bytecode stream: found empty cgraph node");
1297 nodes.safe_push (node);
1298 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1301 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1304 lto_input_toplevel_asms (file_data, order_base);
1306 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1307 #ifdef ENABLE_CHECKING
1308 FOR_EACH_VEC_ELT (nodes, i, node)
1309 gcc_assert (node->aux || !is_a <cgraph_node> (node));
1310 #endif
1311 FOR_EACH_VEC_ELT (nodes, i, node)
1313 int ref;
1314 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
1316 ref = (int) (intptr_t) cnode->global.inlined_to;
1318 /* We share declaration of builtins, so we may read same node twice. */
1319 if (!node->aux)
1320 continue;
1321 node->aux = NULL;
1323 /* Fixup inlined_to from reference to pointer. */
1324 if (ref != LCC_NOT_FOUND)
1325 cgraph (node)->global.inlined_to = cgraph (nodes[ref]);
1326 else
1327 cnode->global.inlined_to = NULL;
1330 ref = (int) (intptr_t) node->same_comdat_group;
1332 /* Fixup same_comdat_group from reference to pointer. */
1333 if (ref != LCC_NOT_FOUND)
1334 node->same_comdat_group = nodes[ref];
1335 else
1336 node->same_comdat_group = NULL;
1338 FOR_EACH_VEC_ELT (nodes, i, node)
1339 node->aux = is_a <cgraph_node> (node) ? (void *)1 : NULL;
1340 return nodes;
1343 /* Input ipa_refs. */
1345 static void
1346 input_refs (struct lto_input_block *ib,
1347 vec<symtab_node *> nodes)
1349 int count;
1350 int idx;
1351 while (true)
1353 symtab_node *node;
1354 count = streamer_read_uhwi (ib);
1355 if (!count)
1356 break;
1357 idx = streamer_read_uhwi (ib);
1358 node = nodes[idx];
1359 while (count)
1361 input_ref (ib, node, nodes);
1362 count--;
1368 static struct gcov_ctr_summary lto_gcov_summary;
1370 /* Input profile_info from IB. */
1371 static void
1372 input_profile_summary (struct lto_input_block *ib,
1373 struct lto_file_decl_data *file_data)
1375 unsigned h_ix;
1376 struct bitpack_d bp;
1377 unsigned int runs = streamer_read_uhwi (ib);
1378 if (runs)
1380 file_data->profile_info.runs = runs;
1381 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1382 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1384 memset (file_data->profile_info.histogram, 0,
1385 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1386 /* Input the bitpack of non-zero histogram indices. */
1387 bp = streamer_read_bitpack (ib);
1388 /* Read in and unpack the full bitpack, flagging non-zero
1389 histogram entries by setting the num_counters non-zero. */
1390 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1392 file_data->profile_info.histogram[h_ix].num_counters
1393 = bp_unpack_value (&bp, 1);
1395 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1397 if (!file_data->profile_info.histogram[h_ix].num_counters)
1398 continue;
1400 file_data->profile_info.histogram[h_ix].num_counters
1401 = streamer_read_gcov_count (ib);
1402 file_data->profile_info.histogram[h_ix].min_value
1403 = streamer_read_gcov_count (ib);
1404 file_data->profile_info.histogram[h_ix].cum_value
1405 = streamer_read_gcov_count (ib);
1407 /* IPA-profile computes hot bb threshold based on cumulated
1408 whole program profile. We need to stream it down to ltrans. */
1409 if (flag_ltrans)
1410 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1415 /* Rescale profile summaries to the same number of runs in the whole unit. */
1417 static void
1418 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1420 struct lto_file_decl_data *file_data;
1421 unsigned int j, h_ix;
1422 gcov_unsigned_t max_runs = 0;
1423 struct cgraph_node *node;
1424 struct cgraph_edge *edge;
1425 gcov_type saved_sum_all = 0;
1426 gcov_ctr_summary *saved_profile_info = 0;
1427 int saved_scale = 0;
1429 /* Find unit with maximal number of runs. If we ever get serious about
1430 roundoff errors, we might also consider computing smallest common
1431 multiply. */
1432 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1433 if (max_runs < file_data->profile_info.runs)
1434 max_runs = file_data->profile_info.runs;
1436 if (!max_runs)
1437 return;
1439 /* Simple overflow check. We probably don't need to support that many train
1440 runs. Such a large value probably imply data corruption anyway. */
1441 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1443 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1444 INT_MAX / REG_BR_PROB_BASE);
1445 return;
1448 profile_info = &lto_gcov_summary;
1449 lto_gcov_summary.runs = max_runs;
1450 lto_gcov_summary.sum_max = 0;
1451 memset (lto_gcov_summary.histogram, 0,
1452 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1454 /* Rescale all units to the maximal number of runs.
1455 sum_max can not be easily merged, as we have no idea what files come from
1456 the same run. We do not use the info anyway, so leave it 0. */
1457 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1458 if (file_data->profile_info.runs)
1460 int scale = GCOV_COMPUTE_SCALE (max_runs,
1461 file_data->profile_info.runs);
1462 lto_gcov_summary.sum_max
1463 = MAX (lto_gcov_summary.sum_max,
1464 apply_scale (file_data->profile_info.sum_max, scale));
1465 lto_gcov_summary.sum_all
1466 = MAX (lto_gcov_summary.sum_all,
1467 apply_scale (file_data->profile_info.sum_all, scale));
1468 /* Save a pointer to the profile_info with the largest
1469 scaled sum_all and the scale for use in merging the
1470 histogram. */
1471 if (!saved_profile_info
1472 || lto_gcov_summary.sum_all > saved_sum_all)
1474 saved_profile_info = &file_data->profile_info;
1475 saved_sum_all = lto_gcov_summary.sum_all;
1476 saved_scale = scale;
1480 gcc_assert (saved_profile_info);
1482 /* Scale up the histogram from the profile that had the largest
1483 scaled sum_all above. */
1484 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1486 /* Scale up the min value as we did the corresponding sum_all
1487 above. Use that to find the new histogram index. */
1488 gcov_type scaled_min
1489 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1490 saved_scale);
1491 /* The new index may be shared with another scaled histogram entry,
1492 so we need to account for a non-zero histogram entry at new_ix. */
1493 unsigned new_ix = gcov_histo_index (scaled_min);
1494 lto_gcov_summary.histogram[new_ix].min_value
1495 = (lto_gcov_summary.histogram[new_ix].num_counters
1496 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1497 : scaled_min);
1498 /* Some of the scaled counter values would ostensibly need to be placed
1499 into different (larger) histogram buckets, but we keep things simple
1500 here and place the scaled cumulative counter value in the bucket
1501 corresponding to the scaled minimum counter value. */
1502 lto_gcov_summary.histogram[new_ix].cum_value
1503 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1504 saved_scale);
1505 lto_gcov_summary.histogram[new_ix].num_counters
1506 += saved_profile_info->histogram[h_ix].num_counters;
1509 /* Watch roundoff errors. */
1510 if (lto_gcov_summary.sum_max < max_runs)
1511 lto_gcov_summary.sum_max = max_runs;
1513 /* If merging already happent at WPA time, we are done. */
1514 if (flag_ltrans)
1515 return;
1517 /* Now compute count_materialization_scale of each node.
1518 During LTRANS we already have values of count_materialization_scale
1519 computed, so just update them. */
1520 FOR_EACH_FUNCTION (node)
1521 if (node->lto_file_data
1522 && node->lto_file_data->profile_info.runs)
1524 int scale;
1526 scale = RDIV (node->count_materialization_scale * max_runs,
1527 node->lto_file_data->profile_info.runs);
1528 node->count_materialization_scale = scale;
1529 if (scale < 0)
1530 fatal_error ("Profile information in %s corrupted",
1531 file_data->file_name);
1533 if (scale == REG_BR_PROB_BASE)
1534 continue;
1535 for (edge = node->callees; edge; edge = edge->next_callee)
1536 edge->count = apply_scale (edge->count, scale);
1537 node->count = apply_scale (node->count, scale);
1541 /* Input and merge the symtab from each of the .o files passed to
1542 lto1. */
1544 void
1545 input_symtab (void)
1547 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1548 struct lto_file_decl_data *file_data;
1549 unsigned int j = 0;
1550 struct cgraph_node *node;
1552 while ((file_data = file_data_vec[j++]))
1554 const char *data;
1555 size_t len;
1556 struct lto_input_block *ib;
1557 vec<symtab_node *> nodes;
1559 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1560 &data, &len);
1561 if (!ib)
1562 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1563 input_profile_summary (ib, file_data);
1564 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1565 nodes = input_cgraph_1 (file_data, ib);
1566 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1567 ib, data, len);
1569 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1570 &data, &len);
1571 if (!ib)
1572 fatal_error ("cannot find LTO section refs in %s",
1573 file_data->file_name);
1574 input_refs (ib, nodes);
1575 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1576 ib, data, len);
1577 if (flag_ltrans)
1578 input_cgraph_opt_summary (nodes);
1579 nodes.release ();
1582 merge_profile_summaries (file_data_vec);
1583 get_working_sets ();
1586 /* Clear out the aux field that was used to store enough state to
1587 tell which nodes should be overwritten. */
1588 FOR_EACH_FUNCTION (node)
1590 /* Some nodes may have been created by cgraph_node. This
1591 happens when the callgraph contains nested functions. If the
1592 node for the parent function was never emitted to the gimple
1593 file, cgraph_node will create a node for it when setting the
1594 context of the nested function. */
1595 if (node->lto_file_data)
1596 node->aux = NULL;
1600 /* True when we need optimization summary for NODE. */
1602 static int
1603 output_cgraph_opt_summary_p (struct cgraph_node *node)
1605 return (node->clone_of
1606 && (node->clone.tree_map
1607 || node->clone.args_to_skip
1608 || node->clone.combined_args_to_skip));
1611 /* Output optimization summary for EDGE to OB. */
1612 static void
1613 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1614 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1618 /* Output optimization summary for NODE to OB. */
1620 static void
1621 output_node_opt_summary (struct output_block *ob,
1622 struct cgraph_node *node,
1623 lto_symtab_encoder_t encoder)
1625 unsigned int index;
1626 bitmap_iterator bi;
1627 struct ipa_replace_map *map;
1628 struct bitpack_d bp;
1629 int i;
1630 struct cgraph_edge *e;
1632 if (node->clone.args_to_skip)
1634 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1635 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1636 streamer_write_uhwi (ob, index);
1638 else
1639 streamer_write_uhwi (ob, 0);
1640 if (node->clone.combined_args_to_skip)
1642 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1643 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1644 streamer_write_uhwi (ob, index);
1646 else
1647 streamer_write_uhwi (ob, 0);
1648 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1649 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1651 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1652 mechanism to store function local declarations into summaries. */
1653 gcc_assert (!map->old_tree);
1654 streamer_write_uhwi (ob, map->parm_num);
1655 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1656 stream_write_tree (ob, map->new_tree, true);
1657 bp = bitpack_create (ob->main_stream);
1658 bp_pack_value (&bp, map->replace_p, 1);
1659 bp_pack_value (&bp, map->ref_p, 1);
1660 streamer_write_bitpack (&bp);
1663 if (lto_symtab_encoder_in_partition_p (encoder, node))
1665 for (e = node->callees; e; e = e->next_callee)
1666 output_edge_opt_summary (ob, e);
1667 for (e = node->indirect_calls; e; e = e->next_callee)
1668 output_edge_opt_summary (ob, e);
1672 /* Output optimization summaries stored in callgraph.
1673 At the moment it is the clone info structure. */
1675 static void
1676 output_cgraph_opt_summary (void)
1678 int i, n_nodes;
1679 lto_symtab_encoder_t encoder;
1680 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1681 unsigned count = 0;
1683 ob->cgraph_node = NULL;
1684 encoder = ob->decl_state->symtab_node_encoder;
1685 n_nodes = lto_symtab_encoder_size (encoder);
1686 for (i = 0; i < n_nodes; i++)
1688 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1689 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1690 if (cnode && output_cgraph_opt_summary_p (cnode))
1691 count++;
1693 streamer_write_uhwi (ob, count);
1694 for (i = 0; i < n_nodes; i++)
1696 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1697 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1698 if (cnode && output_cgraph_opt_summary_p (cnode))
1700 streamer_write_uhwi (ob, i);
1701 output_node_opt_summary (ob, cnode, encoder);
1704 produce_asm (ob, NULL);
1705 destroy_output_block (ob);
1708 /* Input optimisation summary of EDGE. */
1710 static void
1711 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
1712 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
1716 /* Input optimisation summary of NODE. */
1718 static void
1719 input_node_opt_summary (struct cgraph_node *node,
1720 struct lto_input_block *ib_main,
1721 struct data_in *data_in)
1723 int i;
1724 int count;
1725 int bit;
1726 struct bitpack_d bp;
1727 struct cgraph_edge *e;
1729 count = streamer_read_uhwi (ib_main);
1730 if (count)
1731 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1732 for (i = 0; i < count; i++)
1734 bit = streamer_read_uhwi (ib_main);
1735 bitmap_set_bit (node->clone.args_to_skip, bit);
1737 count = streamer_read_uhwi (ib_main);
1738 if (count)
1739 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1740 for (i = 0; i < count; i++)
1742 bit = streamer_read_uhwi (ib_main);
1743 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1745 count = streamer_read_uhwi (ib_main);
1746 for (i = 0; i < count; i++)
1748 struct ipa_replace_map *map = ggc_alloc_ipa_replace_map ();
1750 vec_safe_push (node->clone.tree_map, map);
1751 map->parm_num = streamer_read_uhwi (ib_main);
1752 map->old_tree = NULL;
1753 map->new_tree = stream_read_tree (ib_main, data_in);
1754 bp = streamer_read_bitpack (ib_main);
1755 map->replace_p = bp_unpack_value (&bp, 1);
1756 map->ref_p = bp_unpack_value (&bp, 1);
1758 for (e = node->callees; e; e = e->next_callee)
1759 input_edge_opt_summary (e, ib_main);
1760 for (e = node->indirect_calls; e; e = e->next_callee)
1761 input_edge_opt_summary (e, ib_main);
1764 /* Read section in file FILE_DATA of length LEN with data DATA. */
1766 static void
1767 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1768 const char *data, size_t len,
1769 vec<symtab_node *> nodes)
1771 const struct lto_function_header *header =
1772 (const struct lto_function_header *) data;
1773 const int cfg_offset = sizeof (struct lto_function_header);
1774 const int main_offset = cfg_offset + header->cfg_size;
1775 const int string_offset = main_offset + header->main_size;
1776 struct data_in *data_in;
1777 struct lto_input_block ib_main;
1778 unsigned int i;
1779 unsigned int count;
1781 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1782 header->main_size);
1784 data_in =
1785 lto_data_in_create (file_data, (const char *) data + string_offset,
1786 header->string_size, vNULL);
1787 count = streamer_read_uhwi (&ib_main);
1789 for (i = 0; i < count; i++)
1791 int ref = streamer_read_uhwi (&ib_main);
1792 input_node_opt_summary (cgraph (nodes[ref]),
1793 &ib_main, data_in);
1795 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1796 len);
1797 lto_data_in_delete (data_in);
1800 /* Input optimization summary of cgraph. */
1802 static void
1803 input_cgraph_opt_summary (vec<symtab_node *> nodes)
1805 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1806 struct lto_file_decl_data *file_data;
1807 unsigned int j = 0;
1809 while ((file_data = file_data_vec[j++]))
1811 size_t len;
1812 const char *data =
1813 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1814 &len);
1816 if (data)
1817 input_cgraph_opt_section (file_data, data, len, nodes);