gcc/
[official-gcc.git] / gcc / lto-cgraph.c
blob3071f0c0c86bd1c692f47e1abc282e3b90eccef7
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 "predict.h"
30 #include "vec.h"
31 #include "hashtab.h"
32 #include "hash-set.h"
33 #include "machmode.h"
34 #include "hard-reg-set.h"
35 #include "input.h"
36 #include "function.h"
37 #include "basic-block.h"
38 #include "tree-ssa-alias.h"
39 #include "internal-fn.h"
40 #include "gimple-expr.h"
41 #include "is-a.h"
42 #include "gimple.h"
43 #include "expr.h"
44 #include "flags.h"
45 #include "params.h"
46 #include "langhooks.h"
47 #include "bitmap.h"
48 #include "diagnostic-core.h"
49 #include "except.h"
50 #include "timevar.h"
51 #include "lto-streamer.h"
52 #include "data-streamer.h"
53 #include "tree-streamer.h"
54 #include "gcov-io.h"
55 #include "tree-pass.h"
56 #include "profile.h"
57 #include "context.h"
58 #include "pass_manager.h"
59 #include "ipa-utils.h"
61 /* True when asm nodes has been output. */
62 bool asm_nodes_output = false;
64 static void output_cgraph_opt_summary (void);
65 static void input_cgraph_opt_summary (vec<symtab_node *> nodes);
67 /* Number of LDPR values known to GCC. */
68 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
70 /* All node orders are ofsetted by ORDER_BASE. */
71 static int order_base;
73 /* Cgraph streaming is organized as set of record whose type
74 is indicated by a tag. */
75 enum LTO_symtab_tags
77 /* Must leave 0 for the stopper. */
79 /* Cgraph node without body available. */
80 LTO_symtab_unavail_node = 1,
81 /* Cgraph node with function body. */
82 LTO_symtab_analyzed_node,
83 /* Cgraph edges. */
84 LTO_symtab_edge,
85 LTO_symtab_indirect_edge,
86 LTO_symtab_variable,
87 LTO_symtab_last_tag
90 /* Create a new symtab encoder.
91 if FOR_INPUT, the encoder allocate only datastructures needed
92 to read the symtab. */
94 lto_symtab_encoder_t
95 lto_symtab_encoder_new (bool for_input)
97 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
99 if (!for_input)
100 encoder->map = new hash_map<symtab_node *, size_t>;
101 encoder->nodes.create (0);
102 return encoder;
106 /* Delete ENCODER and its components. */
108 void
109 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
111 encoder->nodes.release ();
112 if (encoder->map)
113 delete encoder->map;
114 free (encoder);
118 /* Return the existing reference number of NODE in the symtab encoder in
119 output block OB. Assign a new reference if this is the first time
120 NODE is encoded. */
123 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
124 symtab_node *node)
126 int ref;
128 if (!encoder->map)
130 lto_encoder_entry entry = {node, false, false, false};
132 ref = encoder->nodes.length ();
133 encoder->nodes.safe_push (entry);
134 return ref;
137 size_t *slot = encoder->map->get (node);
138 if (!slot || !*slot)
140 lto_encoder_entry entry = {node, false, false, false};
141 ref = encoder->nodes.length ();
142 if (!slot)
143 encoder->map->put (node, ref + 1);
144 encoder->nodes.safe_push (entry);
146 else
147 ref = *slot - 1;
149 return ref;
152 /* Remove NODE from encoder. */
154 bool
155 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
156 symtab_node *node)
158 int index;
159 lto_encoder_entry last_node;
161 size_t *slot = encoder->map->get (node);
162 if (slot == NULL || !*slot)
163 return false;
165 index = *slot - 1;
166 gcc_checking_assert (encoder->nodes[index].node == node);
168 /* Remove from vector. We do this by swapping node with the last element
169 of the vector. */
170 last_node = encoder->nodes.pop ();
171 if (last_node.node != node)
173 gcc_assert (encoder->map->put (last_node.node, index + 1));
175 /* Move the last element to the original spot of NODE. */
176 encoder->nodes[index] = last_node;
179 /* Remove element from hash table. */
180 encoder->map->remove (node);
181 return true;
185 /* Return TRUE if we should encode initializer of NODE (if any). */
187 bool
188 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
189 struct cgraph_node *node)
191 int index = lto_symtab_encoder_lookup (encoder, node);
192 return encoder->nodes[index].body;
195 /* Return TRUE if we should encode body of NODE (if any). */
197 static void
198 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
199 struct cgraph_node *node)
201 int index = lto_symtab_encoder_encode (encoder, node);
202 gcc_checking_assert (encoder->nodes[index].node == node);
203 encoder->nodes[index].body = true;
206 /* Return TRUE if we should encode initializer of NODE (if any). */
208 bool
209 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
210 varpool_node *node)
212 int index = lto_symtab_encoder_lookup (encoder, node);
213 if (index == LCC_NOT_FOUND)
214 return false;
215 return encoder->nodes[index].initializer;
218 /* Return TRUE if we should encode initializer of NODE (if any). */
220 static void
221 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
222 varpool_node *node)
224 int index = lto_symtab_encoder_lookup (encoder, node);
225 encoder->nodes[index].initializer = true;
228 /* Return TRUE if we should encode initializer of NODE (if any). */
230 bool
231 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
232 symtab_node *node)
234 int index = lto_symtab_encoder_lookup (encoder, node);
235 if (index == LCC_NOT_FOUND)
236 return false;
237 return encoder->nodes[index].in_partition;
240 /* Return TRUE if we should encode body of NODE (if any). */
242 void
243 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
244 symtab_node *node)
246 int index = lto_symtab_encoder_encode (encoder, node);
247 encoder->nodes[index].in_partition = true;
250 /* Output the cgraph EDGE to OB using ENCODER. */
252 static void
253 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
254 lto_symtab_encoder_t encoder)
256 unsigned int uid;
257 intptr_t ref;
258 struct bitpack_d bp;
260 if (edge->indirect_unknown_callee)
261 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
262 LTO_symtab_indirect_edge);
263 else
264 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
265 LTO_symtab_edge);
267 ref = lto_symtab_encoder_lookup (encoder, edge->caller);
268 gcc_assert (ref != LCC_NOT_FOUND);
269 streamer_write_hwi_stream (ob->main_stream, ref);
271 if (!edge->indirect_unknown_callee)
273 ref = lto_symtab_encoder_lookup (encoder, edge->callee);
274 gcc_assert (ref != LCC_NOT_FOUND);
275 streamer_write_hwi_stream (ob->main_stream, ref);
278 streamer_write_gcov_count_stream (ob->main_stream, edge->count);
280 bp = bitpack_create (ob->main_stream);
281 uid = (!gimple_has_body_p (edge->caller->decl)
282 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt) + 1);
283 bp_pack_enum (&bp, cgraph_inline_failed_t,
284 CIF_N_REASONS, edge->inline_failed);
285 bp_pack_var_len_unsigned (&bp, uid);
286 bp_pack_var_len_unsigned (&bp, edge->frequency);
287 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
288 bp_pack_value (&bp, edge->speculative, 1);
289 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
290 bp_pack_value (&bp, edge->can_throw_external, 1);
291 bp_pack_value (&bp, edge->in_polymorphic_cdtor, 1);
292 if (edge->indirect_unknown_callee)
294 int flags = edge->indirect_info->ecf_flags;
295 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
296 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
297 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
298 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
299 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
300 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
301 /* Flags that should not appear on indirect calls. */
302 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
303 | ECF_MAY_BE_ALLOCA
304 | ECF_SIBCALL
305 | ECF_LEAF
306 | ECF_NOVOPS)));
308 streamer_write_bitpack (&bp);
309 if (edge->indirect_unknown_callee)
311 streamer_write_hwi_stream (ob->main_stream,
312 edge->indirect_info->common_target_id);
313 if (edge->indirect_info->common_target_id)
314 streamer_write_hwi_stream
315 (ob->main_stream, edge->indirect_info->common_target_probability);
319 /* Return if NODE contain references from other partitions. */
321 bool
322 referenced_from_other_partition_p (symtab_node *node, lto_symtab_encoder_t encoder)
324 int i;
325 struct ipa_ref *ref = NULL;
327 for (i = 0; node->iterate_referring (i, ref); i++)
329 if (ref->referring->in_other_partition
330 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
331 return true;
333 return false;
336 /* Return true when node is reachable from other partition. */
338 bool
339 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
341 struct cgraph_edge *e;
342 if (!node->definition)
343 return false;
344 if (node->global.inlined_to)
345 return false;
346 for (e = node->callers; e; e = e->next_caller)
347 if (e->caller->in_other_partition
348 || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
349 return true;
350 return false;
353 /* Return if NODE contain references from other partitions. */
355 bool
356 referenced_from_this_partition_p (symtab_node *node,
357 lto_symtab_encoder_t encoder)
359 int i;
360 struct ipa_ref *ref = NULL;
362 for (i = 0; node->iterate_referring (i, ref); i++)
363 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
364 return true;
365 return false;
368 /* Return true when node is reachable from other partition. */
370 bool
371 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
373 struct cgraph_edge *e;
374 for (e = node->callers; e; e = e->next_caller)
375 if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
376 return true;
377 return false;
380 /* Output the cgraph NODE to OB. ENCODER is used to find the
381 reference number of NODE->inlined_to. SET is the set of nodes we
382 are writing to the current file. If NODE is not in SET, then NODE
383 is a boundary of a cgraph_node_set and we pretend NODE just has a
384 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
385 that have had their callgraph node written so far. This is used to
386 determine if NODE is a clone of a previously written node. */
388 static void
389 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
390 lto_symtab_encoder_t encoder)
392 unsigned int tag;
393 struct bitpack_d bp;
394 bool boundary_p;
395 intptr_t ref;
396 bool in_other_partition = false;
397 struct cgraph_node *clone_of, *ultimate_clone_of;
398 ipa_opt_pass_d *pass;
399 int i;
400 bool alias_p;
401 const char *comdat;
402 const char *section;
403 tree group;
405 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
407 if (node->analyzed && !boundary_p)
408 tag = LTO_symtab_analyzed_node;
409 else
410 tag = LTO_symtab_unavail_node;
412 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
413 tag);
414 streamer_write_hwi_stream (ob->main_stream, node->order);
416 /* In WPA mode, we only output part of the call-graph. Also, we
417 fake cgraph node attributes. There are two cases that we care.
419 Boundary nodes: There are nodes that are not part of SET but are
420 called from within SET. We artificially make them look like
421 externally visible nodes with no function body.
423 Cherry-picked nodes: These are nodes we pulled from other
424 translation units into SET during IPA-inlining. We make them as
425 local static nodes to prevent clashes with other local statics. */
426 if (boundary_p && node->analyzed
427 && node->get_partitioning_class () == SYMBOL_PARTITION)
429 /* Inline clones can not be part of boundary.
430 gcc_assert (!node->global.inlined_to);
432 FIXME: At the moment they can be, when partition contains an inline
433 clone that is clone of inline clone from outside partition. We can
434 reshape the clone tree and make other tree to be the root, but it
435 needs a bit extra work and will be promplty done by cgraph_remove_node
436 after reading back. */
437 in_other_partition = 1;
440 clone_of = node->clone_of;
441 while (clone_of
442 && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
443 if (clone_of->prev_sibling_clone)
444 clone_of = clone_of->prev_sibling_clone;
445 else
446 clone_of = clone_of->clone_of;
448 /* See if body of the master function is output. If not, we are seeing only
449 an declaration and we do not need to pass down clone tree. */
450 ultimate_clone_of = clone_of;
451 while (ultimate_clone_of && ultimate_clone_of->clone_of)
452 ultimate_clone_of = ultimate_clone_of->clone_of;
454 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
455 clone_of = NULL;
457 if (tag == LTO_symtab_analyzed_node)
458 gcc_assert (clone_of || !node->clone_of);
459 if (!clone_of)
460 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
461 else
462 streamer_write_hwi_stream (ob->main_stream, ref);
465 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
466 streamer_write_gcov_count_stream (ob->main_stream, node->count);
467 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
469 streamer_write_hwi_stream (ob->main_stream,
470 node->ipa_transforms_to_apply.length ());
471 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
472 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
474 if (tag == LTO_symtab_analyzed_node)
476 if (node->global.inlined_to)
478 ref = lto_symtab_encoder_lookup (encoder, node->global.inlined_to);
479 gcc_assert (ref != LCC_NOT_FOUND);
481 else
482 ref = LCC_NOT_FOUND;
484 streamer_write_hwi_stream (ob->main_stream, ref);
487 group = node->get_comdat_group ();
488 if (group)
489 comdat = IDENTIFIER_POINTER (group);
490 else
491 comdat = "";
492 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
494 if (group)
496 if (node->same_comdat_group && !boundary_p)
498 ref = lto_symtab_encoder_lookup (encoder,
499 node->same_comdat_group);
500 gcc_assert (ref != LCC_NOT_FOUND);
502 else
503 ref = LCC_NOT_FOUND;
504 streamer_write_hwi_stream (ob->main_stream, ref);
507 section = node->get_section ();
508 if (!section)
509 section = "";
511 streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
513 bp = bitpack_create (ob->main_stream);
514 bp_pack_value (&bp, node->local.local, 1);
515 bp_pack_value (&bp, node->externally_visible, 1);
516 bp_pack_value (&bp, node->no_reorder, 1);
517 bp_pack_value (&bp, node->definition, 1);
518 bp_pack_value (&bp, node->local.versionable, 1);
519 bp_pack_value (&bp, node->local.can_change_signature, 1);
520 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
521 bp_pack_value (&bp, node->force_output, 1);
522 bp_pack_value (&bp, node->forced_by_abi, 1);
523 bp_pack_value (&bp, node->unique_name, 1);
524 bp_pack_value (&bp, node->body_removed, 1);
525 bp_pack_value (&bp, node->implicit_section, 1);
526 bp_pack_value (&bp, node->address_taken, 1);
527 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
528 && node->get_partitioning_class () == SYMBOL_PARTITION
529 && (reachable_from_other_partition_p (node, encoder)
530 || referenced_from_other_partition_p (node, encoder)), 1);
531 bp_pack_value (&bp, node->lowered, 1);
532 bp_pack_value (&bp, in_other_partition, 1);
533 /* Real aliases in a boundary become non-aliases. However we still stream
534 alias info on weakrefs.
535 TODO: We lose a bit of information here - when we know that variable is
536 defined in other unit, we may use the info on aliases to resolve
537 symbol1 != symbol2 type tests that we can do only for locally defined objects
538 otherwise. */
539 alias_p = node->alias && (!boundary_p || node->weakref);
540 bp_pack_value (&bp, alias_p, 1);
541 bp_pack_value (&bp, node->weakref, 1);
542 bp_pack_value (&bp, node->frequency, 2);
543 bp_pack_value (&bp, node->only_called_at_startup, 1);
544 bp_pack_value (&bp, node->only_called_at_exit, 1);
545 bp_pack_value (&bp, node->tm_clone, 1);
546 bp_pack_value (&bp, node->calls_comdat_local, 1);
547 bp_pack_value (&bp, node->icf_merged, 1);
548 bp_pack_value (&bp, node->thunk.thunk_p && !boundary_p, 1);
549 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
550 LDPR_NUM_KNOWN, node->resolution);
551 streamer_write_bitpack (&bp);
552 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
554 if (node->thunk.thunk_p && !boundary_p)
556 streamer_write_uhwi_stream
557 (ob->main_stream,
558 1 + (node->thunk.this_adjusting != 0) * 2
559 + (node->thunk.virtual_offset_p != 0) * 4);
560 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
561 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
563 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
564 if (DECL_STATIC_CONSTRUCTOR (node->decl))
565 streamer_write_hwi_stream (ob->main_stream, node->get_init_priority ());
566 if (DECL_STATIC_DESTRUCTOR (node->decl))
567 streamer_write_hwi_stream (ob->main_stream, node->get_fini_priority ());
570 /* Output the varpool NODE to OB.
571 If NODE is not in SET, then NODE is a boundary. */
573 static void
574 lto_output_varpool_node (struct lto_simple_output_block *ob, varpool_node *node,
575 lto_symtab_encoder_t encoder)
577 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
578 struct bitpack_d bp;
579 int ref;
580 bool alias_p;
581 const char *comdat;
582 const char *section;
583 tree group;
585 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
586 LTO_symtab_variable);
587 streamer_write_hwi_stream (ob->main_stream, node->order);
588 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
589 bp = bitpack_create (ob->main_stream);
590 bp_pack_value (&bp, node->externally_visible, 1);
591 bp_pack_value (&bp, node->no_reorder, 1);
592 bp_pack_value (&bp, node->force_output, 1);
593 bp_pack_value (&bp, node->forced_by_abi, 1);
594 bp_pack_value (&bp, node->unique_name, 1);
595 bp_pack_value (&bp, node->body_removed, 1);
596 bp_pack_value (&bp, node->implicit_section, 1);
597 bp_pack_value (&bp, node->writeonly, 1);
598 bp_pack_value (&bp, node->definition, 1);
599 alias_p = node->alias && (!boundary_p || node->weakref);
600 bp_pack_value (&bp, alias_p, 1);
601 bp_pack_value (&bp, node->weakref, 1);
602 bp_pack_value (&bp, node->analyzed && !boundary_p, 1);
603 gcc_assert (node->definition || !node->analyzed);
604 /* Constant pool initializers can be de-unified into individual ltrans units.
605 FIXME: Alternatively at -Os we may want to avoid generating for them the local
606 labels and share them across LTRANS partitions. */
607 if (node->get_partitioning_class () != SYMBOL_PARTITION)
609 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
610 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
612 else
614 bp_pack_value (&bp, node->definition
615 && referenced_from_other_partition_p (node, encoder), 1);
616 bp_pack_value (&bp, node->analyzed
617 && boundary_p && !DECL_EXTERNAL (node->decl), 1);
618 /* in_other_partition. */
620 bp_pack_value (&bp, node->tls_model, 3);
621 bp_pack_value (&bp, node->used_by_single_function, 1);
622 streamer_write_bitpack (&bp);
624 group = node->get_comdat_group ();
625 if (group)
626 comdat = IDENTIFIER_POINTER (group);
627 else
628 comdat = "";
629 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
631 if (group)
633 if (node->same_comdat_group && !boundary_p)
635 ref = lto_symtab_encoder_lookup (encoder,
636 node->same_comdat_group);
637 gcc_assert (ref != LCC_NOT_FOUND);
639 else
640 ref = LCC_NOT_FOUND;
641 streamer_write_hwi_stream (ob->main_stream, ref);
644 section = node->get_section ();
645 if (!section)
646 section = "";
647 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
649 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
650 LDPR_NUM_KNOWN, node->resolution);
653 /* Output the varpool NODE to OB.
654 If NODE is not in SET, then NODE is a boundary. */
656 static void
657 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
658 lto_symtab_encoder_t encoder)
660 struct bitpack_d bp;
661 int nref;
662 int uid = ref->lto_stmt_uid;
663 struct cgraph_node *node;
665 bp = bitpack_create (ob->main_stream);
666 bp_pack_value (&bp, ref->use, 2);
667 bp_pack_value (&bp, ref->speculative, 1);
668 streamer_write_bitpack (&bp);
669 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
670 gcc_assert (nref != LCC_NOT_FOUND);
671 streamer_write_hwi_stream (ob->main_stream, nref);
673 node = dyn_cast <cgraph_node *> (ref->referring);
674 if (node)
676 if (ref->stmt)
677 uid = gimple_uid (ref->stmt) + 1;
678 streamer_write_hwi_stream (ob->main_stream, uid);
682 /* Stream out profile_summary to OB. */
684 static void
685 output_profile_summary (struct lto_simple_output_block *ob)
687 unsigned h_ix;
688 struct bitpack_d bp;
690 if (profile_info)
692 /* We do not output num and run_max, they are not used by
693 GCC profile feedback and they are difficult to merge from multiple
694 units. */
695 gcc_assert (profile_info->runs);
696 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
697 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
699 /* sum_all is needed for computing the working set with the
700 histogram. */
701 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
703 /* Create and output a bitpack of non-zero histogram entries indices. */
704 bp = bitpack_create (ob->main_stream);
705 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
706 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
707 streamer_write_bitpack (&bp);
708 /* Now stream out only those non-zero entries. */
709 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
711 if (!profile_info->histogram[h_ix].num_counters)
712 continue;
713 streamer_write_gcov_count_stream (ob->main_stream,
714 profile_info->histogram[h_ix].num_counters);
715 streamer_write_gcov_count_stream (ob->main_stream,
716 profile_info->histogram[h_ix].min_value);
717 streamer_write_gcov_count_stream (ob->main_stream,
718 profile_info->histogram[h_ix].cum_value);
720 /* IPA-profile computes hot bb threshold based on cumulated
721 whole program profile. We need to stream it down to ltrans. */
722 if (flag_wpa)
723 streamer_write_gcov_count_stream (ob->main_stream,
724 get_hot_bb_threshold ());
726 else
727 streamer_write_uhwi_stream (ob->main_stream, 0);
730 /* Output all callees or indirect outgoing edges. EDGE must be the first such
731 edge. */
733 static void
734 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
735 struct lto_simple_output_block *ob,
736 lto_symtab_encoder_t encoder)
738 if (!edge)
739 return;
741 /* Output edges in backward direction, so the reconstructed callgraph match
742 and it is easy to associate call sites in the IPA pass summaries. */
743 while (edge->next_callee)
744 edge = edge->next_callee;
745 for (; edge; edge = edge->prev_callee)
746 lto_output_edge (ob, edge, encoder);
749 /* Output the part of the cgraph in SET. */
751 static void
752 output_refs (lto_symtab_encoder_t encoder)
754 lto_symtab_encoder_iterator lsei;
755 struct lto_simple_output_block *ob;
756 int count;
757 struct ipa_ref *ref;
758 int i;
760 ob = lto_create_simple_output_block (LTO_section_refs);
762 for (lsei = lsei_start_in_partition (encoder); !lsei_end_p (lsei);
763 lsei_next_in_partition (&lsei))
765 symtab_node *node = lsei_node (lsei);
767 count = node->ref_list.nreferences ();
768 if (count)
770 streamer_write_gcov_count_stream (ob->main_stream, count);
771 streamer_write_uhwi_stream (ob->main_stream,
772 lto_symtab_encoder_lookup (encoder, node));
773 for (i = 0; node->iterate_reference (i, ref); i++)
774 lto_output_ref (ob, ref, encoder);
778 streamer_write_uhwi_stream (ob->main_stream, 0);
780 lto_destroy_simple_output_block (ob);
783 /* Add NODE into encoder as well as nodes it is cloned from.
784 Do it in a way so clones appear first. */
786 static void
787 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
788 bool include_body)
790 if (node->clone_of)
791 add_node_to (encoder, node->clone_of, include_body);
792 else if (include_body)
793 lto_set_symtab_encoder_encode_body (encoder, node);
794 lto_symtab_encoder_encode (encoder, node);
797 /* Add all references in NODE to encoders. */
799 static void
800 create_references (lto_symtab_encoder_t encoder, symtab_node *node)
802 int i;
803 struct ipa_ref *ref = NULL;
804 for (i = 0; node->iterate_reference (i, ref); i++)
805 if (is_a <cgraph_node *> (ref->referred))
806 add_node_to (encoder, dyn_cast <cgraph_node *> (ref->referred), false);
807 else
808 lto_symtab_encoder_encode (encoder, ref->referred);
811 /* Find all symbols we want to stream into given partition and insert them
812 to encoders.
814 The function actually replaces IN_ENCODER by new one. The reason is that
815 streaming code needs clone's origin to be streamed before clone. This
816 means that we need to insert the nodes in specific order. This order is
817 ignored by the partitioning logic earlier. */
819 lto_symtab_encoder_t
820 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
822 struct cgraph_edge *edge;
823 int i;
824 lto_symtab_encoder_t encoder;
825 lto_symtab_encoder_iterator lsei;
826 hash_set<void *> reachable_call_targets;
828 encoder = lto_symtab_encoder_new (false);
830 /* Go over all entries in the IN_ENCODER and duplicate them to
831 ENCODER. At the same time insert masters of clones so
832 every master appears before clone. */
833 for (lsei = lsei_start_function_in_partition (in_encoder);
834 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
836 struct cgraph_node *node = lsei_cgraph_node (lsei);
837 add_node_to (encoder, node, true);
838 lto_set_symtab_encoder_in_partition (encoder, node);
839 create_references (encoder, node);
840 /* For proper debug info, we need to ship the origins, too. */
841 if (DECL_ABSTRACT_ORIGIN (node->decl))
843 struct cgraph_node *origin_node
844 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node->decl));
845 add_node_to (encoder, origin_node, true);
848 for (lsei = lsei_start_variable_in_partition (in_encoder);
849 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
851 varpool_node *vnode = lsei_varpool_node (lsei);
853 lto_set_symtab_encoder_in_partition (encoder, vnode);
854 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
855 create_references (encoder, vnode);
856 /* For proper debug info, we need to ship the origins, too. */
857 if (DECL_ABSTRACT_ORIGIN (vnode->decl))
859 varpool_node *origin_node
860 = varpool_node::get (DECL_ABSTRACT_ORIGIN (vnode->decl));
861 lto_set_symtab_encoder_in_partition (encoder, origin_node);
864 /* Pickle in also the initializer of all referenced readonly variables
865 to help folding. Constant pool variables are not shared, so we must
866 pickle those too. */
867 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
869 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
870 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
872 if (!lto_symtab_encoder_encode_initializer_p (encoder,
873 vnode)
874 && vnode->ctor_useable_for_folding_p ())
876 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
877 create_references (encoder, vnode);
882 /* Go over all the nodes again to include callees that are not in
883 SET. */
884 for (lsei = lsei_start_function_in_partition (encoder);
885 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
887 struct cgraph_node *node = lsei_cgraph_node (lsei);
888 for (edge = node->callees; edge; edge = edge->next_callee)
890 struct cgraph_node *callee = edge->callee;
891 if (!lto_symtab_encoder_in_partition_p (encoder, callee))
893 /* We should have moved all the inlines. */
894 gcc_assert (!callee->global.inlined_to);
895 add_node_to (encoder, callee, false);
898 /* Add all possible targets for late devirtualization. */
899 if (flag_devirtualize)
900 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
901 if (edge->indirect_info->polymorphic)
903 unsigned int i;
904 void *cache_token;
905 bool final;
906 vec <cgraph_node *>targets
907 = possible_polymorphic_call_targets
908 (edge, &final, &cache_token);
909 if (!reachable_call_targets.add (cache_token))
911 for (i = 0; i < targets.length (); i++)
913 struct cgraph_node *callee = targets[i];
915 /* Adding an external declarations into the unit serves
916 no purpose and just increases its boundary. */
917 if (callee->definition
918 && !lto_symtab_encoder_in_partition_p
919 (encoder, callee))
921 gcc_assert (!callee->global.inlined_to);
922 add_node_to (encoder, callee, false);
928 lto_symtab_encoder_delete (in_encoder);
929 return encoder;
932 /* Output the part of the symtab in SET and VSET. */
934 void
935 output_symtab (void)
937 struct cgraph_node *node;
938 struct lto_simple_output_block *ob;
939 lto_symtab_encoder_iterator lsei;
940 int i, n_nodes;
941 lto_symtab_encoder_t encoder;
943 if (flag_wpa)
944 output_cgraph_opt_summary ();
946 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
948 output_profile_summary (ob);
950 /* An encoder for cgraph nodes should have been created by
951 ipa_write_summaries_1. */
952 gcc_assert (ob->decl_state->symtab_node_encoder);
953 encoder = ob->decl_state->symtab_node_encoder;
955 /* Write out the nodes. We must first output a node and then its clones,
956 otherwise at a time reading back the node there would be nothing to clone
957 from. */
958 n_nodes = lto_symtab_encoder_size (encoder);
959 for (i = 0; i < n_nodes; i++)
961 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
962 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
963 lto_output_node (ob, cnode, encoder);
964 else
965 lto_output_varpool_node (ob, dyn_cast<varpool_node *> (node), encoder);
968 /* Go over the nodes in SET again to write edges. */
969 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
970 lsei_next_function_in_partition (&lsei))
972 node = lsei_cgraph_node (lsei);
973 output_outgoing_cgraph_edges (node->callees, ob, encoder);
974 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
977 streamer_write_uhwi_stream (ob->main_stream, 0);
979 lto_destroy_simple_output_block (ob);
981 /* Emit toplevel asms.
982 When doing WPA we must output every asm just once. Since we do not partition asm
983 nodes at all, output them to first output. This is kind of hack, but should work
984 well. */
985 if (!asm_nodes_output)
987 asm_nodes_output = true;
988 lto_output_toplevel_asms ();
991 output_refs (encoder);
994 /* Return identifier encoded in IB as a plain string. */
996 static tree
997 read_identifier (struct lto_input_block *ib)
999 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1000 tree id;
1002 if (ib->data[ib->p + len])
1003 lto_section_overrun (ib);
1004 if (!len)
1006 ib->p++;
1007 return NULL;
1009 id = get_identifier (ib->data + ib->p);
1010 ib->p += len + 1;
1011 return id;
1014 /* Return string encoded in IB, NULL if string is empty. */
1016 static const char *
1017 read_string (struct lto_input_block *ib)
1019 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1020 const char *str;
1022 if (ib->data[ib->p + len])
1023 lto_section_overrun (ib);
1024 if (!len)
1026 ib->p++;
1027 return NULL;
1029 str = ib->data + ib->p;
1030 ib->p += len + 1;
1031 return str;
1034 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
1035 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
1036 NODE or to replace the values in it, for instance because the first
1037 time we saw it, the function body was not available but now it
1038 is. BP is a bitpack with all the bitflags for NODE read from the
1039 stream. */
1041 static void
1042 input_overwrite_node (struct lto_file_decl_data *file_data,
1043 struct cgraph_node *node,
1044 enum LTO_symtab_tags tag,
1045 struct bitpack_d *bp)
1047 node->aux = (void *) tag;
1048 node->lto_file_data = file_data;
1050 node->local.local = bp_unpack_value (bp, 1);
1051 node->externally_visible = bp_unpack_value (bp, 1);
1052 node->no_reorder = bp_unpack_value (bp, 1);
1053 node->definition = bp_unpack_value (bp, 1);
1054 node->local.versionable = bp_unpack_value (bp, 1);
1055 node->local.can_change_signature = bp_unpack_value (bp, 1);
1056 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
1057 node->force_output = bp_unpack_value (bp, 1);
1058 node->forced_by_abi = bp_unpack_value (bp, 1);
1059 node->unique_name = bp_unpack_value (bp, 1);
1060 node->body_removed = bp_unpack_value (bp, 1);
1061 node->implicit_section = bp_unpack_value (bp, 1);
1062 node->address_taken = bp_unpack_value (bp, 1);
1063 node->used_from_other_partition = bp_unpack_value (bp, 1);
1064 node->lowered = bp_unpack_value (bp, 1);
1065 node->analyzed = tag == LTO_symtab_analyzed_node;
1066 node->in_other_partition = bp_unpack_value (bp, 1);
1067 if (node->in_other_partition
1068 /* Avoid updating decl when we are seeing just inline clone.
1069 When inlining function that has functions already inlined into it,
1070 we produce clones of inline clones.
1072 WPA partitioning might put each clone into different unit and
1073 we might end up streaming inline clone from other partition
1074 to support clone we are interested in. */
1075 && (!node->clone_of
1076 || node->clone_of->decl != node->decl))
1078 DECL_EXTERNAL (node->decl) = 1;
1079 TREE_STATIC (node->decl) = 0;
1081 node->alias = bp_unpack_value (bp, 1);
1082 node->weakref = bp_unpack_value (bp, 1);
1083 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
1084 node->only_called_at_startup = bp_unpack_value (bp, 1);
1085 node->only_called_at_exit = bp_unpack_value (bp, 1);
1086 node->tm_clone = bp_unpack_value (bp, 1);
1087 node->calls_comdat_local = bp_unpack_value (bp, 1);
1088 node->icf_merged = bp_unpack_value (bp, 1);
1089 node->thunk.thunk_p = bp_unpack_value (bp, 1);
1090 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
1091 LDPR_NUM_KNOWN);
1092 gcc_assert (flag_ltrans
1093 || (!node->in_other_partition
1094 && !node->used_from_other_partition));
1097 /* Return string alias is alias of. */
1099 static tree
1100 get_alias_symbol (tree decl)
1102 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1103 return get_identifier (TREE_STRING_POINTER
1104 (TREE_VALUE (TREE_VALUE (alias))));
1107 /* Read a node from input_block IB. TAG is the node's tag just read.
1108 Return the node read or overwriten. */
1110 static struct cgraph_node *
1111 input_node (struct lto_file_decl_data *file_data,
1112 struct lto_input_block *ib,
1113 enum LTO_symtab_tags tag,
1114 vec<symtab_node *> nodes)
1116 gcc::pass_manager *passes = g->get_passes ();
1117 tree fn_decl;
1118 struct cgraph_node *node;
1119 struct bitpack_d bp;
1120 unsigned decl_index;
1121 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1122 int clone_ref;
1123 int order;
1124 int i, count;
1125 tree group;
1126 const char *section;
1127 order = streamer_read_hwi (ib) + order_base;
1128 clone_ref = streamer_read_hwi (ib);
1130 decl_index = streamer_read_uhwi (ib);
1131 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1133 if (clone_ref != LCC_NOT_FOUND)
1135 node = dyn_cast<cgraph_node *> (nodes[clone_ref])->create_clone (fn_decl,
1136 0, CGRAPH_FREQ_BASE, false,
1137 vNULL, false, NULL, NULL);
1139 else
1141 /* Declaration of functions can be already merged with a declaration
1142 from other input file. We keep cgraph unmerged until after streaming
1143 of ipa passes is done. Alays forcingly create a fresh node. */
1144 node = symtab->create_empty ();
1145 node->decl = fn_decl;
1146 node->register_symbol ();
1149 node->order = order;
1150 if (order >= symtab->order)
1151 symtab->order = order + 1;
1153 node->count = streamer_read_gcov_count (ib);
1154 node->count_materialization_scale = streamer_read_hwi (ib);
1156 count = streamer_read_hwi (ib);
1157 node->ipa_transforms_to_apply = vNULL;
1158 for (i = 0; i < count; i++)
1160 opt_pass *pass;
1161 int pid = streamer_read_hwi (ib);
1163 gcc_assert (pid < passes->passes_by_id_size);
1164 pass = passes->passes_by_id[pid];
1165 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *) pass);
1168 if (tag == LTO_symtab_analyzed_node)
1169 ref = streamer_read_hwi (ib);
1171 group = read_identifier (ib);
1172 if (group)
1173 ref2 = streamer_read_hwi (ib);
1175 /* Make sure that we have not read this node before. Nodes that
1176 have already been read will have their tag stored in the 'aux'
1177 field. Since built-in functions can be referenced in multiple
1178 functions, they are expected to be read more than once. */
1179 if (node->aux && !DECL_BUILT_IN (node->decl))
1180 internal_error ("bytecode stream: found multiple instances of cgraph "
1181 "node with uid %d", node->uid);
1183 node->tp_first_run = streamer_read_uhwi (ib);
1185 bp = streamer_read_bitpack (ib);
1187 input_overwrite_node (file_data, node, tag, &bp);
1189 /* Store a reference for now, and fix up later to be a pointer. */
1190 node->global.inlined_to = (cgraph_node *) (intptr_t) ref;
1192 if (group)
1194 node->set_comdat_group (group);
1195 /* Store a reference for now, and fix up later to be a pointer. */
1196 node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
1198 else
1199 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1200 section = read_string (ib);
1201 if (section)
1202 node->set_section_for_node (section);
1204 if (node->thunk.thunk_p)
1206 int type = streamer_read_uhwi (ib);
1207 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1208 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1210 node->thunk.fixed_offset = fixed_offset;
1211 node->thunk.this_adjusting = (type & 2);
1212 node->thunk.virtual_value = virtual_value;
1213 node->thunk.virtual_offset_p = (type & 4);
1215 if (node->alias && !node->analyzed && node->weakref)
1216 node->alias_target = get_alias_symbol (node->decl);
1217 node->profile_id = streamer_read_hwi (ib);
1218 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1219 node->set_init_priority (streamer_read_hwi (ib));
1220 if (DECL_STATIC_DESTRUCTOR (node->decl))
1221 node->set_fini_priority (streamer_read_hwi (ib));
1222 return node;
1225 /* Read a node from input_block IB. TAG is the node's tag just read.
1226 Return the node read or overwriten. */
1228 static varpool_node *
1229 input_varpool_node (struct lto_file_decl_data *file_data,
1230 struct lto_input_block *ib)
1232 int decl_index;
1233 tree var_decl;
1234 varpool_node *node;
1235 struct bitpack_d bp;
1236 int ref = LCC_NOT_FOUND;
1237 int order;
1238 tree group;
1239 const char *section;
1241 order = streamer_read_hwi (ib) + order_base;
1242 decl_index = streamer_read_uhwi (ib);
1243 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1245 /* Declaration of functions can be already merged with a declaration
1246 from other input file. We keep cgraph unmerged until after streaming
1247 of ipa passes is done. Alays forcingly create a fresh node. */
1248 node = varpool_node::create_empty ();
1249 node->decl = var_decl;
1250 node->register_symbol ();
1252 node->order = order;
1253 if (order >= symtab->order)
1254 symtab->order = order + 1;
1255 node->lto_file_data = file_data;
1257 bp = streamer_read_bitpack (ib);
1258 node->externally_visible = bp_unpack_value (&bp, 1);
1259 node->no_reorder = bp_unpack_value (&bp, 1);
1260 node->force_output = bp_unpack_value (&bp, 1);
1261 node->forced_by_abi = bp_unpack_value (&bp, 1);
1262 node->unique_name = bp_unpack_value (&bp, 1);
1263 node->body_removed = bp_unpack_value (&bp, 1);
1264 node->implicit_section = bp_unpack_value (&bp, 1);
1265 node->writeonly = bp_unpack_value (&bp, 1);
1266 node->definition = bp_unpack_value (&bp, 1);
1267 node->alias = bp_unpack_value (&bp, 1);
1268 node->weakref = bp_unpack_value (&bp, 1);
1269 node->analyzed = bp_unpack_value (&bp, 1);
1270 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1271 node->in_other_partition = bp_unpack_value (&bp, 1);
1272 if (node->in_other_partition)
1274 DECL_EXTERNAL (node->decl) = 1;
1275 TREE_STATIC (node->decl) = 0;
1277 if (node->alias && !node->analyzed && node->weakref)
1278 node->alias_target = get_alias_symbol (node->decl);
1279 node->tls_model = (enum tls_model)bp_unpack_value (&bp, 3);
1280 node->used_by_single_function = (enum tls_model)bp_unpack_value (&bp, 1);
1281 group = read_identifier (ib);
1282 if (group)
1284 node->set_comdat_group (group);
1285 ref = streamer_read_hwi (ib);
1286 /* Store a reference for now, and fix up later to be a pointer. */
1287 node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1289 else
1290 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1291 section = read_string (ib);
1292 if (section)
1293 node->set_section_for_node (section);
1294 node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1295 LDPR_NUM_KNOWN);
1296 gcc_assert (flag_ltrans
1297 || (!node->in_other_partition
1298 && !node->used_from_other_partition));
1300 return node;
1303 /* Read a node from input_block IB. TAG is the node's tag just read.
1304 Return the node read or overwriten. */
1306 static void
1307 input_ref (struct lto_input_block *ib,
1308 symtab_node *referring_node,
1309 vec<symtab_node *> nodes)
1311 symtab_node *node = NULL;
1312 struct bitpack_d bp;
1313 enum ipa_ref_use use;
1314 bool speculative;
1315 struct ipa_ref *ref;
1317 bp = streamer_read_bitpack (ib);
1318 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1319 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1320 node = nodes[streamer_read_hwi (ib)];
1321 ref = referring_node->create_reference (node, use);
1322 ref->speculative = speculative;
1323 if (is_a <cgraph_node *> (referring_node))
1324 ref->lto_stmt_uid = streamer_read_hwi (ib);
1327 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1328 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1329 edge being read is indirect (in the sense that it has
1330 indirect_unknown_callee set). */
1332 static void
1333 input_edge (struct lto_input_block *ib, vec<symtab_node *> nodes,
1334 bool indirect)
1336 struct cgraph_node *caller, *callee;
1337 struct cgraph_edge *edge;
1338 unsigned int stmt_id;
1339 gcov_type count;
1340 int freq;
1341 cgraph_inline_failed_t inline_failed;
1342 struct bitpack_d bp;
1343 int ecf_flags = 0;
1345 caller = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1346 if (caller == NULL || caller->decl == NULL_TREE)
1347 internal_error ("bytecode stream: no caller found while reading edge");
1349 if (!indirect)
1351 callee = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1352 if (callee == NULL || callee->decl == NULL_TREE)
1353 internal_error ("bytecode stream: no callee found while reading edge");
1355 else
1356 callee = NULL;
1358 count = streamer_read_gcov_count (ib);
1360 bp = streamer_read_bitpack (ib);
1361 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_t, CIF_N_REASONS);
1362 stmt_id = bp_unpack_var_len_unsigned (&bp);
1363 freq = (int) bp_unpack_var_len_unsigned (&bp);
1365 if (indirect)
1366 edge = caller->create_indirect_edge (NULL, 0, count, freq);
1367 else
1368 edge = caller->create_edge (callee, NULL, count, freq);
1370 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1371 edge->speculative = bp_unpack_value (&bp, 1);
1372 edge->lto_stmt_uid = stmt_id;
1373 edge->inline_failed = inline_failed;
1374 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1375 edge->can_throw_external = bp_unpack_value (&bp, 1);
1376 edge->in_polymorphic_cdtor = bp_unpack_value (&bp, 1);
1377 if (indirect)
1379 if (bp_unpack_value (&bp, 1))
1380 ecf_flags |= ECF_CONST;
1381 if (bp_unpack_value (&bp, 1))
1382 ecf_flags |= ECF_PURE;
1383 if (bp_unpack_value (&bp, 1))
1384 ecf_flags |= ECF_NORETURN;
1385 if (bp_unpack_value (&bp, 1))
1386 ecf_flags |= ECF_MALLOC;
1387 if (bp_unpack_value (&bp, 1))
1388 ecf_flags |= ECF_NOTHROW;
1389 if (bp_unpack_value (&bp, 1))
1390 ecf_flags |= ECF_RETURNS_TWICE;
1391 edge->indirect_info->ecf_flags = ecf_flags;
1392 edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1393 if (edge->indirect_info->common_target_id)
1394 edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1399 /* Read a cgraph from IB using the info in FILE_DATA. */
1401 static vec<symtab_node *>
1402 input_cgraph_1 (struct lto_file_decl_data *file_data,
1403 struct lto_input_block *ib)
1405 enum LTO_symtab_tags tag;
1406 vec<symtab_node *> nodes = vNULL;
1407 symtab_node *node;
1408 unsigned i;
1410 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1411 order_base = symtab->order;
1412 while (tag)
1414 if (tag == LTO_symtab_edge)
1415 input_edge (ib, nodes, false);
1416 else if (tag == LTO_symtab_indirect_edge)
1417 input_edge (ib, nodes, true);
1418 else if (tag == LTO_symtab_variable)
1420 node = input_varpool_node (file_data, ib);
1421 nodes.safe_push (node);
1422 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1424 else
1426 node = input_node (file_data, ib, tag, nodes);
1427 if (node == NULL || node->decl == NULL_TREE)
1428 internal_error ("bytecode stream: found empty cgraph node");
1429 nodes.safe_push (node);
1430 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1433 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1436 lto_input_toplevel_asms (file_data, order_base);
1438 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1439 #ifdef ENABLE_CHECKING
1440 FOR_EACH_VEC_ELT (nodes, i, node)
1441 gcc_assert (node->aux || !is_a <cgraph_node *> (node));
1442 #endif
1443 FOR_EACH_VEC_ELT (nodes, i, node)
1445 int ref;
1446 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1448 ref = (int) (intptr_t) cnode->global.inlined_to;
1450 /* We share declaration of builtins, so we may read same node twice. */
1451 if (!node->aux)
1452 continue;
1453 node->aux = NULL;
1455 /* Fixup inlined_to from reference to pointer. */
1456 if (ref != LCC_NOT_FOUND)
1457 dyn_cast<cgraph_node *> (node)->global.inlined_to
1458 = dyn_cast<cgraph_node *> (nodes[ref]);
1459 else
1460 cnode->global.inlined_to = NULL;
1463 ref = (int) (intptr_t) node->same_comdat_group;
1465 /* Fixup same_comdat_group from reference to pointer. */
1466 if (ref != LCC_NOT_FOUND)
1467 node->same_comdat_group = nodes[ref];
1468 else
1469 node->same_comdat_group = NULL;
1471 FOR_EACH_VEC_ELT (nodes, i, node)
1472 node->aux = is_a <cgraph_node *> (node) ? (void *)1 : NULL;
1473 return nodes;
1476 /* Input ipa_refs. */
1478 static void
1479 input_refs (struct lto_input_block *ib,
1480 vec<symtab_node *> nodes)
1482 int count;
1483 int idx;
1484 while (true)
1486 symtab_node *node;
1487 count = streamer_read_uhwi (ib);
1488 if (!count)
1489 break;
1490 idx = streamer_read_uhwi (ib);
1491 node = nodes[idx];
1492 while (count)
1494 input_ref (ib, node, nodes);
1495 count--;
1501 static struct gcov_ctr_summary lto_gcov_summary;
1503 /* Input profile_info from IB. */
1504 static void
1505 input_profile_summary (struct lto_input_block *ib,
1506 struct lto_file_decl_data *file_data)
1508 unsigned h_ix;
1509 struct bitpack_d bp;
1510 unsigned int runs = streamer_read_uhwi (ib);
1511 if (runs)
1513 file_data->profile_info.runs = runs;
1514 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1515 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1517 memset (file_data->profile_info.histogram, 0,
1518 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1519 /* Input the bitpack of non-zero histogram indices. */
1520 bp = streamer_read_bitpack (ib);
1521 /* Read in and unpack the full bitpack, flagging non-zero
1522 histogram entries by setting the num_counters non-zero. */
1523 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1525 file_data->profile_info.histogram[h_ix].num_counters
1526 = bp_unpack_value (&bp, 1);
1528 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1530 if (!file_data->profile_info.histogram[h_ix].num_counters)
1531 continue;
1533 file_data->profile_info.histogram[h_ix].num_counters
1534 = streamer_read_gcov_count (ib);
1535 file_data->profile_info.histogram[h_ix].min_value
1536 = streamer_read_gcov_count (ib);
1537 file_data->profile_info.histogram[h_ix].cum_value
1538 = streamer_read_gcov_count (ib);
1540 /* IPA-profile computes hot bb threshold based on cumulated
1541 whole program profile. We need to stream it down to ltrans. */
1542 if (flag_ltrans)
1543 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1548 /* Rescale profile summaries to the same number of runs in the whole unit. */
1550 static void
1551 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1553 struct lto_file_decl_data *file_data;
1554 unsigned int j, h_ix;
1555 gcov_unsigned_t max_runs = 0;
1556 struct cgraph_node *node;
1557 struct cgraph_edge *edge;
1558 gcov_type saved_sum_all = 0;
1559 gcov_ctr_summary *saved_profile_info = 0;
1560 int saved_scale = 0;
1562 /* Find unit with maximal number of runs. If we ever get serious about
1563 roundoff errors, we might also consider computing smallest common
1564 multiply. */
1565 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1566 if (max_runs < file_data->profile_info.runs)
1567 max_runs = file_data->profile_info.runs;
1569 if (!max_runs)
1570 return;
1572 /* Simple overflow check. We probably don't need to support that many train
1573 runs. Such a large value probably imply data corruption anyway. */
1574 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1576 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1577 INT_MAX / REG_BR_PROB_BASE);
1578 return;
1581 profile_info = &lto_gcov_summary;
1582 lto_gcov_summary.runs = max_runs;
1583 lto_gcov_summary.sum_max = 0;
1584 memset (lto_gcov_summary.histogram, 0,
1585 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1587 /* Rescale all units to the maximal number of runs.
1588 sum_max can not be easily merged, as we have no idea what files come from
1589 the same run. We do not use the info anyway, so leave it 0. */
1590 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1591 if (file_data->profile_info.runs)
1593 int scale = GCOV_COMPUTE_SCALE (max_runs,
1594 file_data->profile_info.runs);
1595 lto_gcov_summary.sum_max
1596 = MAX (lto_gcov_summary.sum_max,
1597 apply_scale (file_data->profile_info.sum_max, scale));
1598 lto_gcov_summary.sum_all
1599 = MAX (lto_gcov_summary.sum_all,
1600 apply_scale (file_data->profile_info.sum_all, scale));
1601 /* Save a pointer to the profile_info with the largest
1602 scaled sum_all and the scale for use in merging the
1603 histogram. */
1604 if (!saved_profile_info
1605 || lto_gcov_summary.sum_all > saved_sum_all)
1607 saved_profile_info = &file_data->profile_info;
1608 saved_sum_all = lto_gcov_summary.sum_all;
1609 saved_scale = scale;
1613 gcc_assert (saved_profile_info);
1615 /* Scale up the histogram from the profile that had the largest
1616 scaled sum_all above. */
1617 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1619 /* Scale up the min value as we did the corresponding sum_all
1620 above. Use that to find the new histogram index. */
1621 gcov_type scaled_min
1622 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1623 saved_scale);
1624 /* The new index may be shared with another scaled histogram entry,
1625 so we need to account for a non-zero histogram entry at new_ix. */
1626 unsigned new_ix = gcov_histo_index (scaled_min);
1627 lto_gcov_summary.histogram[new_ix].min_value
1628 = (lto_gcov_summary.histogram[new_ix].num_counters
1629 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1630 : scaled_min);
1631 /* Some of the scaled counter values would ostensibly need to be placed
1632 into different (larger) histogram buckets, but we keep things simple
1633 here and place the scaled cumulative counter value in the bucket
1634 corresponding to the scaled minimum counter value. */
1635 lto_gcov_summary.histogram[new_ix].cum_value
1636 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1637 saved_scale);
1638 lto_gcov_summary.histogram[new_ix].num_counters
1639 += saved_profile_info->histogram[h_ix].num_counters;
1642 /* Watch roundoff errors. */
1643 if (lto_gcov_summary.sum_max < max_runs)
1644 lto_gcov_summary.sum_max = max_runs;
1646 /* If merging already happent at WPA time, we are done. */
1647 if (flag_ltrans)
1648 return;
1650 /* Now compute count_materialization_scale of each node.
1651 During LTRANS we already have values of count_materialization_scale
1652 computed, so just update them. */
1653 FOR_EACH_FUNCTION (node)
1654 if (node->lto_file_data
1655 && node->lto_file_data->profile_info.runs)
1657 int scale;
1659 scale = RDIV (node->count_materialization_scale * max_runs,
1660 node->lto_file_data->profile_info.runs);
1661 node->count_materialization_scale = scale;
1662 if (scale < 0)
1663 fatal_error ("Profile information in %s corrupted",
1664 file_data->file_name);
1666 if (scale == REG_BR_PROB_BASE)
1667 continue;
1668 for (edge = node->callees; edge; edge = edge->next_callee)
1669 edge->count = apply_scale (edge->count, scale);
1670 node->count = apply_scale (node->count, scale);
1674 /* Input and merge the symtab from each of the .o files passed to
1675 lto1. */
1677 void
1678 input_symtab (void)
1680 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1681 struct lto_file_decl_data *file_data;
1682 unsigned int j = 0;
1683 struct cgraph_node *node;
1685 while ((file_data = file_data_vec[j++]))
1687 const char *data;
1688 size_t len;
1689 struct lto_input_block *ib;
1690 vec<symtab_node *> nodes;
1692 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1693 &data, &len);
1694 if (!ib)
1695 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1696 input_profile_summary (ib, file_data);
1697 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1698 nodes = input_cgraph_1 (file_data, ib);
1699 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1700 ib, data, len);
1702 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1703 &data, &len);
1704 if (!ib)
1705 fatal_error ("cannot find LTO section refs in %s",
1706 file_data->file_name);
1707 input_refs (ib, nodes);
1708 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1709 ib, data, len);
1710 if (flag_ltrans)
1711 input_cgraph_opt_summary (nodes);
1712 nodes.release ();
1715 merge_profile_summaries (file_data_vec);
1716 get_working_sets ();
1719 /* Clear out the aux field that was used to store enough state to
1720 tell which nodes should be overwritten. */
1721 FOR_EACH_FUNCTION (node)
1723 /* Some nodes may have been created by cgraph_node. This
1724 happens when the callgraph contains nested functions. If the
1725 node for the parent function was never emitted to the gimple
1726 file, cgraph_node will create a node for it when setting the
1727 context of the nested function. */
1728 if (node->lto_file_data)
1729 node->aux = NULL;
1733 /* True when we need optimization summary for NODE. */
1735 static int
1736 output_cgraph_opt_summary_p (struct cgraph_node *node)
1738 return (node->clone_of
1739 && (node->clone.tree_map
1740 || node->clone.args_to_skip
1741 || node->clone.combined_args_to_skip));
1744 /* Output optimization summary for EDGE to OB. */
1745 static void
1746 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1747 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1751 /* Output optimization summary for NODE to OB. */
1753 static void
1754 output_node_opt_summary (struct output_block *ob,
1755 struct cgraph_node *node,
1756 lto_symtab_encoder_t encoder)
1758 unsigned int index;
1759 bitmap_iterator bi;
1760 struct ipa_replace_map *map;
1761 struct bitpack_d bp;
1762 int i;
1763 struct cgraph_edge *e;
1765 if (node->clone.args_to_skip)
1767 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1768 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1769 streamer_write_uhwi (ob, index);
1771 else
1772 streamer_write_uhwi (ob, 0);
1773 if (node->clone.combined_args_to_skip)
1775 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1776 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1777 streamer_write_uhwi (ob, index);
1779 else
1780 streamer_write_uhwi (ob, 0);
1781 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1782 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1784 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1785 mechanism to store function local declarations into summaries. */
1786 gcc_assert (!map->old_tree);
1787 streamer_write_uhwi (ob, map->parm_num);
1788 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1789 stream_write_tree (ob, map->new_tree, true);
1790 bp = bitpack_create (ob->main_stream);
1791 bp_pack_value (&bp, map->replace_p, 1);
1792 bp_pack_value (&bp, map->ref_p, 1);
1793 streamer_write_bitpack (&bp);
1796 if (lto_symtab_encoder_in_partition_p (encoder, node))
1798 for (e = node->callees; e; e = e->next_callee)
1799 output_edge_opt_summary (ob, e);
1800 for (e = node->indirect_calls; e; e = e->next_callee)
1801 output_edge_opt_summary (ob, e);
1805 /* Output optimization summaries stored in callgraph.
1806 At the moment it is the clone info structure. */
1808 static void
1809 output_cgraph_opt_summary (void)
1811 int i, n_nodes;
1812 lto_symtab_encoder_t encoder;
1813 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1814 unsigned count = 0;
1816 ob->symbol = NULL;
1817 encoder = ob->decl_state->symtab_node_encoder;
1818 n_nodes = lto_symtab_encoder_size (encoder);
1819 for (i = 0; i < n_nodes; i++)
1821 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1822 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1823 if (cnode && output_cgraph_opt_summary_p (cnode))
1824 count++;
1826 streamer_write_uhwi (ob, count);
1827 for (i = 0; i < n_nodes; i++)
1829 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1830 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1831 if (cnode && output_cgraph_opt_summary_p (cnode))
1833 streamer_write_uhwi (ob, i);
1834 output_node_opt_summary (ob, cnode, encoder);
1837 produce_asm (ob, NULL);
1838 destroy_output_block (ob);
1841 /* Input optimisation summary of EDGE. */
1843 static void
1844 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
1845 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
1849 /* Input optimisation summary of NODE. */
1851 static void
1852 input_node_opt_summary (struct cgraph_node *node,
1853 struct lto_input_block *ib_main,
1854 struct data_in *data_in)
1856 int i;
1857 int count;
1858 int bit;
1859 struct bitpack_d bp;
1860 struct cgraph_edge *e;
1862 count = streamer_read_uhwi (ib_main);
1863 if (count)
1864 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1865 for (i = 0; i < count; i++)
1867 bit = streamer_read_uhwi (ib_main);
1868 bitmap_set_bit (node->clone.args_to_skip, bit);
1870 count = streamer_read_uhwi (ib_main);
1871 if (count)
1872 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1873 for (i = 0; i < count; i++)
1875 bit = streamer_read_uhwi (ib_main);
1876 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1878 count = streamer_read_uhwi (ib_main);
1879 for (i = 0; i < count; i++)
1881 struct ipa_replace_map *map = ggc_alloc<ipa_replace_map> ();
1883 vec_safe_push (node->clone.tree_map, map);
1884 map->parm_num = streamer_read_uhwi (ib_main);
1885 map->old_tree = NULL;
1886 map->new_tree = stream_read_tree (ib_main, data_in);
1887 bp = streamer_read_bitpack (ib_main);
1888 map->replace_p = bp_unpack_value (&bp, 1);
1889 map->ref_p = bp_unpack_value (&bp, 1);
1891 for (e = node->callees; e; e = e->next_callee)
1892 input_edge_opt_summary (e, ib_main);
1893 for (e = node->indirect_calls; e; e = e->next_callee)
1894 input_edge_opt_summary (e, ib_main);
1897 /* Read section in file FILE_DATA of length LEN with data DATA. */
1899 static void
1900 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1901 const char *data, size_t len,
1902 vec<symtab_node *> nodes)
1904 const struct lto_function_header *header =
1905 (const struct lto_function_header *) data;
1906 const int cfg_offset = sizeof (struct lto_function_header);
1907 const int main_offset = cfg_offset + header->cfg_size;
1908 const int string_offset = main_offset + header->main_size;
1909 struct data_in *data_in;
1910 unsigned int i;
1911 unsigned int count;
1913 lto_input_block ib_main ((const char *) data + main_offset,
1914 header->main_size);
1916 data_in =
1917 lto_data_in_create (file_data, (const char *) data + string_offset,
1918 header->string_size, vNULL);
1919 count = streamer_read_uhwi (&ib_main);
1921 for (i = 0; i < count; i++)
1923 int ref = streamer_read_uhwi (&ib_main);
1924 input_node_opt_summary (dyn_cast<cgraph_node *> (nodes[ref]),
1925 &ib_main, data_in);
1927 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1928 len);
1929 lto_data_in_delete (data_in);
1932 /* Input optimization summary of cgraph. */
1934 static void
1935 input_cgraph_opt_summary (vec<symtab_node *> nodes)
1937 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1938 struct lto_file_decl_data *file_data;
1939 unsigned int j = 0;
1941 while ((file_data = file_data_vec[j++]))
1943 size_t len;
1944 const char *data =
1945 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1946 &len);
1948 if (data)
1949 input_cgraph_opt_section (file_data, data, len, nodes);