2014-07-29 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / lto-cgraph.c
blobb1fcf67a02584af6ef66f258208c42149662d215
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 /* True when asm nodes has been output. */
57 bool asm_nodes_output = false;
59 static void output_cgraph_opt_summary (void);
60 static void input_cgraph_opt_summary (vec<symtab_node *> nodes);
62 /* Number of LDPR values known to GCC. */
63 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
65 /* All node orders are ofsetted by ORDER_BASE. */
66 static int order_base;
68 /* Cgraph streaming is organized as set of record whose type
69 is indicated by a tag. */
70 enum LTO_symtab_tags
72 /* Must leave 0 for the stopper. */
74 /* Cgraph node without body available. */
75 LTO_symtab_unavail_node = 1,
76 /* Cgraph node with function body. */
77 LTO_symtab_analyzed_node,
78 /* Cgraph edges. */
79 LTO_symtab_edge,
80 LTO_symtab_indirect_edge,
81 LTO_symtab_variable,
82 LTO_symtab_last_tag
85 /* Create a new symtab encoder.
86 if FOR_INPUT, the encoder allocate only datastructures needed
87 to read the symtab. */
89 lto_symtab_encoder_t
90 lto_symtab_encoder_new (bool for_input)
92 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
94 if (!for_input)
95 encoder->map = pointer_map_create ();
96 encoder->nodes.create (0);
97 return encoder;
101 /* Delete ENCODER and its components. */
103 void
104 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
106 encoder->nodes.release ();
107 if (encoder->map)
108 pointer_map_destroy (encoder->map);
109 free (encoder);
113 /* Return the existing reference number of NODE in the symtab encoder in
114 output block OB. Assign a new reference if this is the first time
115 NODE is encoded. */
118 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
119 symtab_node *node)
121 int ref;
122 void **slot;
124 if (!encoder->map)
126 lto_encoder_entry entry = {node, false, false, false};
128 ref = encoder->nodes.length ();
129 encoder->nodes.safe_push (entry);
130 return ref;
133 slot = pointer_map_contains (encoder->map, node);
134 if (!slot || !*slot)
136 lto_encoder_entry entry = {node, false, false, false};
137 ref = encoder->nodes.length ();
138 if (!slot)
139 slot = pointer_map_insert (encoder->map, node);
140 *slot = (void *) (intptr_t) (ref + 1);
141 encoder->nodes.safe_push (entry);
143 else
144 ref = (size_t) *slot - 1;
146 return ref;
149 /* Remove NODE from encoder. */
151 bool
152 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
153 symtab_node *node)
155 void **slot, **last_slot;
156 int index;
157 lto_encoder_entry last_node;
159 slot = pointer_map_contains (encoder->map, node);
160 if (slot == NULL || !*slot)
161 return false;
163 index = (size_t) *slot - 1;
164 gcc_checking_assert (encoder->nodes[index].node == node);
166 /* Remove from vector. We do this by swapping node with the last element
167 of the vector. */
168 last_node = encoder->nodes.pop ();
169 if (last_node.node != node)
171 last_slot = pointer_map_contains (encoder->map, last_node.node);
172 gcc_checking_assert (last_slot && *last_slot);
173 *last_slot = (void *)(size_t) (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 *slot = NULL;
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 if (edge->indirect_unknown_callee)
293 int flags = edge->indirect_info->ecf_flags;
294 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
295 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
296 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
297 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
298 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
299 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
300 /* Flags that should not appear on indirect calls. */
301 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
302 | ECF_MAY_BE_ALLOCA
303 | ECF_SIBCALL
304 | ECF_LEAF
305 | ECF_NOVOPS)));
307 streamer_write_bitpack (&bp);
308 if (edge->indirect_unknown_callee)
310 streamer_write_hwi_stream (ob->main_stream,
311 edge->indirect_info->common_target_id);
312 if (edge->indirect_info->common_target_id)
313 streamer_write_hwi_stream
314 (ob->main_stream, edge->indirect_info->common_target_probability);
318 /* Return if NODE contain references from other partitions. */
320 bool
321 referenced_from_other_partition_p (symtab_node *node, lto_symtab_encoder_t encoder)
323 int i;
324 struct ipa_ref *ref = NULL;
326 for (i = 0; node->iterate_referring (i, ref); i++)
328 if (ref->referring->in_other_partition
329 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
330 return true;
332 return false;
335 /* Return true when node is reachable from other partition. */
337 bool
338 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
340 struct cgraph_edge *e;
341 if (!node->definition)
342 return false;
343 if (node->global.inlined_to)
344 return false;
345 for (e = node->callers; e; e = e->next_caller)
346 if (e->caller->in_other_partition
347 || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
348 return true;
349 return false;
352 /* Return if NODE contain references from other partitions. */
354 bool
355 referenced_from_this_partition_p (symtab_node *node,
356 lto_symtab_encoder_t encoder)
358 int i;
359 struct ipa_ref *ref = NULL;
361 for (i = 0; node->iterate_referring (i, ref); i++)
362 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
363 return true;
364 return false;
367 /* Return true when node is reachable from other partition. */
369 bool
370 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
372 struct cgraph_edge *e;
373 for (e = node->callers; e; e = e->next_caller)
374 if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
375 return true;
376 return false;
379 /* Output the cgraph NODE to OB. ENCODER is used to find the
380 reference number of NODE->inlined_to. SET is the set of nodes we
381 are writing to the current file. If NODE is not in SET, then NODE
382 is a boundary of a cgraph_node_set and we pretend NODE just has a
383 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
384 that have had their callgraph node written so far. This is used to
385 determine if NODE is a clone of a previously written node. */
387 static void
388 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
389 lto_symtab_encoder_t encoder)
391 unsigned int tag;
392 struct bitpack_d bp;
393 bool boundary_p;
394 intptr_t ref;
395 bool in_other_partition = false;
396 struct cgraph_node *clone_of, *ultimate_clone_of;
397 ipa_opt_pass_d *pass;
398 int i;
399 bool alias_p;
400 const char *comdat;
401 const char *section;
402 tree group;
404 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
406 if (node->analyzed && !boundary_p)
407 tag = LTO_symtab_analyzed_node;
408 else
409 tag = LTO_symtab_unavail_node;
411 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
412 tag);
413 streamer_write_hwi_stream (ob->main_stream, node->order);
415 /* In WPA mode, we only output part of the call-graph. Also, we
416 fake cgraph node attributes. There are two cases that we care.
418 Boundary nodes: There are nodes that are not part of SET but are
419 called from within SET. We artificially make them look like
420 externally visible nodes with no function body.
422 Cherry-picked nodes: These are nodes we pulled from other
423 translation units into SET during IPA-inlining. We make them as
424 local static nodes to prevent clashes with other local statics. */
425 if (boundary_p && node->analyzed
426 && node->get_partitioning_class () == SYMBOL_PARTITION)
428 /* Inline clones can not be part of boundary.
429 gcc_assert (!node->global.inlined_to);
431 FIXME: At the moment they can be, when partition contains an inline
432 clone that is clone of inline clone from outside partition. We can
433 reshape the clone tree and make other tree to be the root, but it
434 needs a bit extra work and will be promplty done by cgraph_remove_node
435 after reading back. */
436 in_other_partition = 1;
439 clone_of = node->clone_of;
440 while (clone_of
441 && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
442 if (clone_of->prev_sibling_clone)
443 clone_of = clone_of->prev_sibling_clone;
444 else
445 clone_of = clone_of->clone_of;
447 /* See if body of the master function is output. If not, we are seeing only
448 an declaration and we do not need to pass down clone tree. */
449 ultimate_clone_of = clone_of;
450 while (ultimate_clone_of && ultimate_clone_of->clone_of)
451 ultimate_clone_of = ultimate_clone_of->clone_of;
453 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
454 clone_of = NULL;
456 if (tag == LTO_symtab_analyzed_node)
457 gcc_assert (clone_of || !node->clone_of);
458 if (!clone_of)
459 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
460 else
461 streamer_write_hwi_stream (ob->main_stream, ref);
464 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
465 streamer_write_gcov_count_stream (ob->main_stream, node->count);
466 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
468 streamer_write_hwi_stream (ob->main_stream,
469 node->ipa_transforms_to_apply.length ());
470 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
471 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
473 if (tag == LTO_symtab_analyzed_node)
475 if (node->global.inlined_to)
477 ref = lto_symtab_encoder_lookup (encoder, node->global.inlined_to);
478 gcc_assert (ref != LCC_NOT_FOUND);
480 else
481 ref = LCC_NOT_FOUND;
483 streamer_write_hwi_stream (ob->main_stream, ref);
486 group = node->get_comdat_group ();
487 if (group)
488 comdat = IDENTIFIER_POINTER (group);
489 else
490 comdat = "";
491 lto_output_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
493 if (group)
495 if (node->same_comdat_group && !boundary_p)
497 ref = lto_symtab_encoder_lookup (encoder,
498 node->same_comdat_group);
499 gcc_assert (ref != LCC_NOT_FOUND);
501 else
502 ref = LCC_NOT_FOUND;
503 streamer_write_hwi_stream (ob->main_stream, ref);
506 section = node->get_section ();
507 if (!section)
508 section = "";
510 streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
512 bp = bitpack_create (ob->main_stream);
513 bp_pack_value (&bp, node->local.local, 1);
514 bp_pack_value (&bp, node->externally_visible, 1);
515 bp_pack_value (&bp, node->definition, 1);
516 bp_pack_value (&bp, node->local.versionable, 1);
517 bp_pack_value (&bp, node->local.can_change_signature, 1);
518 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
519 bp_pack_value (&bp, node->force_output, 1);
520 bp_pack_value (&bp, node->forced_by_abi, 1);
521 bp_pack_value (&bp, node->unique_name, 1);
522 bp_pack_value (&bp, node->body_removed, 1);
523 bp_pack_value (&bp, node->implicit_section, 1);
524 bp_pack_value (&bp, node->address_taken, 1);
525 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
526 && node->get_partitioning_class () == SYMBOL_PARTITION
527 && (reachable_from_other_partition_p (node, encoder)
528 || referenced_from_other_partition_p (node, encoder)), 1);
529 bp_pack_value (&bp, node->lowered, 1);
530 bp_pack_value (&bp, in_other_partition, 1);
531 /* Real aliases in a boundary become non-aliases. However we still stream
532 alias info on weakrefs.
533 TODO: We lose a bit of information here - when we know that variable is
534 defined in other unit, we may use the info on aliases to resolve
535 symbol1 != symbol2 type tests that we can do only for locally defined objects
536 otherwise. */
537 alias_p = node->alias && (!boundary_p || node->weakref);
538 bp_pack_value (&bp, alias_p, 1);
539 bp_pack_value (&bp, node->weakref, 1);
540 bp_pack_value (&bp, node->frequency, 2);
541 bp_pack_value (&bp, node->only_called_at_startup, 1);
542 bp_pack_value (&bp, node->only_called_at_exit, 1);
543 bp_pack_value (&bp, node->tm_clone, 1);
544 bp_pack_value (&bp, node->calls_comdat_local, 1);
545 bp_pack_value (&bp, node->thunk.thunk_p && !boundary_p, 1);
546 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
547 LDPR_NUM_KNOWN, node->resolution);
548 streamer_write_bitpack (&bp);
549 lto_output_data_stream (ob->main_stream, section, strlen (section) + 1);
551 if (node->thunk.thunk_p && !boundary_p)
553 streamer_write_uhwi_stream
554 (ob->main_stream,
555 1 + (node->thunk.this_adjusting != 0) * 2
556 + (node->thunk.virtual_offset_p != 0) * 4);
557 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
558 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
560 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
561 if (DECL_STATIC_CONSTRUCTOR (node->decl))
562 streamer_write_hwi_stream (ob->main_stream, node->get_init_priority ());
563 if (DECL_STATIC_DESTRUCTOR (node->decl))
564 streamer_write_hwi_stream (ob->main_stream, node->get_fini_priority ());
567 /* Output the varpool NODE to OB.
568 If NODE is not in SET, then NODE is a boundary. */
570 static void
571 lto_output_varpool_node (struct lto_simple_output_block *ob, varpool_node *node,
572 lto_symtab_encoder_t encoder)
574 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
575 struct bitpack_d bp;
576 int ref;
577 bool alias_p;
578 const char *comdat;
579 const char *section;
580 tree group;
582 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
583 LTO_symtab_variable);
584 streamer_write_hwi_stream (ob->main_stream, node->order);
585 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
586 bp = bitpack_create (ob->main_stream);
587 bp_pack_value (&bp, node->externally_visible, 1);
588 bp_pack_value (&bp, node->force_output, 1);
589 bp_pack_value (&bp, node->forced_by_abi, 1);
590 bp_pack_value (&bp, node->unique_name, 1);
591 bp_pack_value (&bp, node->body_removed, 1);
592 bp_pack_value (&bp, node->implicit_section, 1);
593 bp_pack_value (&bp, node->writeonly, 1);
594 bp_pack_value (&bp, node->definition, 1);
595 alias_p = node->alias && (!boundary_p || node->weakref);
596 bp_pack_value (&bp, alias_p, 1);
597 bp_pack_value (&bp, node->weakref, 1);
598 bp_pack_value (&bp, node->analyzed && !boundary_p, 1);
599 gcc_assert (node->definition || !node->analyzed);
600 /* Constant pool initializers can be de-unified into individual ltrans units.
601 FIXME: Alternatively at -Os we may want to avoid generating for them the local
602 labels and share them across LTRANS partitions. */
603 if (node->get_partitioning_class () != SYMBOL_PARTITION)
605 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
606 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
608 else
610 bp_pack_value (&bp, node->definition
611 && referenced_from_other_partition_p (node, encoder), 1);
612 bp_pack_value (&bp, node->analyzed
613 && boundary_p && !DECL_EXTERNAL (node->decl), 1);
614 /* in_other_partition. */
616 bp_pack_value (&bp, node->tls_model, 3);
617 bp_pack_value (&bp, node->used_by_single_function, 1);
618 streamer_write_bitpack (&bp);
620 group = node->get_comdat_group ();
621 if (group)
622 comdat = IDENTIFIER_POINTER (group);
623 else
624 comdat = "";
625 lto_output_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
627 if (group)
629 if (node->same_comdat_group && !boundary_p)
631 ref = lto_symtab_encoder_lookup (encoder,
632 node->same_comdat_group);
633 gcc_assert (ref != LCC_NOT_FOUND);
635 else
636 ref = LCC_NOT_FOUND;
637 streamer_write_hwi_stream (ob->main_stream, ref);
640 section = node->get_section ();
641 if (!section)
642 section = "";
643 lto_output_data_stream (ob->main_stream, section, strlen (section) + 1);
645 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
646 LDPR_NUM_KNOWN, node->resolution);
649 /* Output the varpool NODE to OB.
650 If NODE is not in SET, then NODE is a boundary. */
652 static void
653 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
654 lto_symtab_encoder_t encoder)
656 struct bitpack_d bp;
657 int nref;
658 int uid = ref->lto_stmt_uid;
659 struct cgraph_node *node;
661 bp = bitpack_create (ob->main_stream);
662 bp_pack_value (&bp, ref->use, 2);
663 bp_pack_value (&bp, ref->speculative, 1);
664 streamer_write_bitpack (&bp);
665 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
666 gcc_assert (nref != LCC_NOT_FOUND);
667 streamer_write_hwi_stream (ob->main_stream, nref);
669 node = dyn_cast <cgraph_node *> (ref->referring);
670 if (node)
672 if (ref->stmt)
673 uid = gimple_uid (ref->stmt) + 1;
674 streamer_write_hwi_stream (ob->main_stream, uid);
678 /* Stream out profile_summary to OB. */
680 static void
681 output_profile_summary (struct lto_simple_output_block *ob)
683 unsigned h_ix;
684 struct bitpack_d bp;
686 if (profile_info)
688 /* We do not output num and run_max, they are not used by
689 GCC profile feedback and they are difficult to merge from multiple
690 units. */
691 gcc_assert (profile_info->runs);
692 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
693 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
695 /* sum_all is needed for computing the working set with the
696 histogram. */
697 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
699 /* Create and output a bitpack of non-zero histogram entries indices. */
700 bp = bitpack_create (ob->main_stream);
701 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
702 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
703 streamer_write_bitpack (&bp);
704 /* Now stream out only those non-zero entries. */
705 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
707 if (!profile_info->histogram[h_ix].num_counters)
708 continue;
709 streamer_write_gcov_count_stream (ob->main_stream,
710 profile_info->histogram[h_ix].num_counters);
711 streamer_write_gcov_count_stream (ob->main_stream,
712 profile_info->histogram[h_ix].min_value);
713 streamer_write_gcov_count_stream (ob->main_stream,
714 profile_info->histogram[h_ix].cum_value);
716 /* IPA-profile computes hot bb threshold based on cumulated
717 whole program profile. We need to stream it down to ltrans. */
718 if (flag_wpa)
719 streamer_write_gcov_count_stream (ob->main_stream,
720 get_hot_bb_threshold ());
722 else
723 streamer_write_uhwi_stream (ob->main_stream, 0);
726 /* Output all callees or indirect outgoing edges. EDGE must be the first such
727 edge. */
729 static void
730 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
731 struct lto_simple_output_block *ob,
732 lto_symtab_encoder_t encoder)
734 if (!edge)
735 return;
737 /* Output edges in backward direction, so the reconstructed callgraph match
738 and it is easy to associate call sites in the IPA pass summaries. */
739 while (edge->next_callee)
740 edge = edge->next_callee;
741 for (; edge; edge = edge->prev_callee)
742 lto_output_edge (ob, edge, encoder);
745 /* Output the part of the cgraph in SET. */
747 static void
748 output_refs (lto_symtab_encoder_t encoder)
750 lto_symtab_encoder_iterator lsei;
751 struct lto_simple_output_block *ob;
752 int count;
753 struct ipa_ref *ref;
754 int i;
756 ob = lto_create_simple_output_block (LTO_section_refs);
758 for (lsei = lsei_start_in_partition (encoder); !lsei_end_p (lsei);
759 lsei_next_in_partition (&lsei))
761 symtab_node *node = lsei_node (lsei);
763 count = node->ref_list.nreferences ();
764 if (count)
766 streamer_write_gcov_count_stream (ob->main_stream, count);
767 streamer_write_uhwi_stream (ob->main_stream,
768 lto_symtab_encoder_lookup (encoder, node));
769 for (i = 0; node->iterate_reference (i, ref); i++)
770 lto_output_ref (ob, ref, encoder);
774 streamer_write_uhwi_stream (ob->main_stream, 0);
776 lto_destroy_simple_output_block (ob);
779 /* Add NODE into encoder as well as nodes it is cloned from.
780 Do it in a way so clones appear first. */
782 static void
783 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
784 bool include_body)
786 if (node->clone_of)
787 add_node_to (encoder, node->clone_of, include_body);
788 else if (include_body)
789 lto_set_symtab_encoder_encode_body (encoder, node);
790 lto_symtab_encoder_encode (encoder, node);
793 /* Add all references in NODE to encoders. */
795 static void
796 add_references (lto_symtab_encoder_t encoder, symtab_node *node)
798 int i;
799 struct ipa_ref *ref = NULL;
800 for (i = 0; node->iterate_reference (i, ref); i++)
801 if (is_a <cgraph_node *> (ref->referred))
802 add_node_to (encoder, dyn_cast <cgraph_node *> (ref->referred), false);
803 else
804 lto_symtab_encoder_encode (encoder, ref->referred);
807 /* Find all symbols we want to stream into given partition and insert them
808 to encoders.
810 The function actually replaces IN_ENCODER by new one. The reason is that
811 streaming code needs clone's origin to be streamed before clone. This
812 means that we need to insert the nodes in specific order. This order is
813 ignored by the partitioning logic earlier. */
815 lto_symtab_encoder_t
816 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
818 struct cgraph_edge *edge;
819 int i;
820 lto_symtab_encoder_t encoder;
821 lto_symtab_encoder_iterator lsei;
822 struct pointer_set_t *reachable_call_targets = pointer_set_create ();
824 encoder = lto_symtab_encoder_new (false);
826 /* Go over all entries in the IN_ENCODER and duplicate them to
827 ENCODER. At the same time insert masters of clones so
828 every master appears before clone. */
829 for (lsei = lsei_start_function_in_partition (in_encoder);
830 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
832 struct cgraph_node *node = lsei_cgraph_node (lsei);
833 add_node_to (encoder, node, true);
834 lto_set_symtab_encoder_in_partition (encoder, node);
835 add_references (encoder, node);
836 /* For proper debug info, we need to ship the origins, too. */
837 if (DECL_ABSTRACT_ORIGIN (node->decl))
839 struct cgraph_node *origin_node
840 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node->decl));
841 add_node_to (encoder, origin_node, true);
844 for (lsei = lsei_start_variable_in_partition (in_encoder);
845 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
847 varpool_node *vnode = lsei_varpool_node (lsei);
849 lto_set_symtab_encoder_in_partition (encoder, vnode);
850 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
851 add_references (encoder, vnode);
852 /* For proper debug info, we need to ship the origins, too. */
853 if (DECL_ABSTRACT_ORIGIN (vnode->decl))
855 varpool_node *origin_node
856 = varpool_node::get (DECL_ABSTRACT_ORIGIN (vnode->decl));
857 lto_set_symtab_encoder_in_partition (encoder, origin_node);
860 /* Pickle in also the initializer of all referenced readonly variables
861 to help folding. Constant pool variables are not shared, so we must
862 pickle those too. */
863 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
865 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
866 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
868 if (!lto_symtab_encoder_encode_initializer_p (encoder,
869 vnode)
870 && vnode->ctor_useable_for_folding_p ())
872 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
873 add_references (encoder, vnode);
878 /* Go over all the nodes again to include callees that are not in
879 SET. */
880 for (lsei = lsei_start_function_in_partition (encoder);
881 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
883 struct cgraph_node *node = lsei_cgraph_node (lsei);
884 for (edge = node->callees; edge; edge = edge->next_callee)
886 struct cgraph_node *callee = edge->callee;
887 if (!lto_symtab_encoder_in_partition_p (encoder, callee))
889 /* We should have moved all the inlines. */
890 gcc_assert (!callee->global.inlined_to);
891 add_node_to (encoder, callee, false);
894 /* Add all possible targets for late devirtualization. */
895 if (flag_devirtualize)
896 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
897 if (edge->indirect_info->polymorphic)
899 unsigned int i;
900 void *cache_token;
901 bool final;
902 vec <cgraph_node *>targets
903 = possible_polymorphic_call_targets
904 (edge, &final, &cache_token);
905 if (!pointer_set_insert (reachable_call_targets,
906 cache_token))
908 for (i = 0; i < targets.length (); i++)
910 struct cgraph_node *callee = targets[i];
912 /* Adding an external declarations into the unit serves
913 no purpose and just increases its boundary. */
914 if (callee->definition
915 && !lto_symtab_encoder_in_partition_p
916 (encoder, callee))
918 gcc_assert (!callee->global.inlined_to);
919 add_node_to (encoder, callee, false);
925 lto_symtab_encoder_delete (in_encoder);
926 pointer_set_destroy (reachable_call_targets);
927 return encoder;
930 /* Output the part of the symtab in SET and VSET. */
932 void
933 output_symtab (void)
935 struct cgraph_node *node;
936 struct lto_simple_output_block *ob;
937 lto_symtab_encoder_iterator lsei;
938 int i, n_nodes;
939 lto_symtab_encoder_t encoder;
941 if (flag_wpa)
942 output_cgraph_opt_summary ();
944 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
946 output_profile_summary (ob);
948 /* An encoder for cgraph nodes should have been created by
949 ipa_write_summaries_1. */
950 gcc_assert (ob->decl_state->symtab_node_encoder);
951 encoder = ob->decl_state->symtab_node_encoder;
953 /* Write out the nodes. We must first output a node and then its clones,
954 otherwise at a time reading back the node there would be nothing to clone
955 from. */
956 n_nodes = lto_symtab_encoder_size (encoder);
957 for (i = 0; i < n_nodes; i++)
959 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
960 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
961 lto_output_node (ob, cnode, encoder);
962 else
963 lto_output_varpool_node (ob, dyn_cast<varpool_node *> (node), encoder);
966 /* Go over the nodes in SET again to write edges. */
967 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
968 lsei_next_function_in_partition (&lsei))
970 node = lsei_cgraph_node (lsei);
971 output_outgoing_cgraph_edges (node->callees, ob, encoder);
972 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
975 streamer_write_uhwi_stream (ob->main_stream, 0);
977 lto_destroy_simple_output_block (ob);
979 /* Emit toplevel asms.
980 When doing WPA we must output every asm just once. Since we do not partition asm
981 nodes at all, output them to first output. This is kind of hack, but should work
982 well. */
983 if (!asm_nodes_output)
985 asm_nodes_output = true;
986 lto_output_toplevel_asms ();
989 output_refs (encoder);
992 /* Return identifier encoded in IB as a plain string. */
994 static tree
995 read_identifier (struct lto_input_block *ib)
997 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
998 tree id;
1000 if (ib->data[ib->p + len])
1001 lto_section_overrun (ib);
1002 if (!len)
1004 ib->p++;
1005 return NULL;
1007 id = get_identifier (ib->data + ib->p);
1008 ib->p += len + 1;
1009 return id;
1012 /* Return string encoded in IB, NULL if string is empty. */
1014 static const char *
1015 read_string (struct lto_input_block *ib)
1017 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1018 const char *str;
1020 if (ib->data[ib->p + len])
1021 lto_section_overrun (ib);
1022 if (!len)
1024 ib->p++;
1025 return NULL;
1027 str = ib->data + ib->p;
1028 ib->p += len + 1;
1029 return str;
1032 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
1033 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
1034 NODE or to replace the values in it, for instance because the first
1035 time we saw it, the function body was not available but now it
1036 is. BP is a bitpack with all the bitflags for NODE read from the
1037 stream. */
1039 static void
1040 input_overwrite_node (struct lto_file_decl_data *file_data,
1041 struct cgraph_node *node,
1042 enum LTO_symtab_tags tag,
1043 struct bitpack_d *bp)
1045 node->aux = (void *) tag;
1046 node->lto_file_data = file_data;
1048 node->local.local = bp_unpack_value (bp, 1);
1049 node->externally_visible = bp_unpack_value (bp, 1);
1050 node->definition = bp_unpack_value (bp, 1);
1051 node->local.versionable = bp_unpack_value (bp, 1);
1052 node->local.can_change_signature = bp_unpack_value (bp, 1);
1053 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
1054 node->force_output = bp_unpack_value (bp, 1);
1055 node->forced_by_abi = bp_unpack_value (bp, 1);
1056 node->unique_name = bp_unpack_value (bp, 1);
1057 node->body_removed = bp_unpack_value (bp, 1);
1058 node->implicit_section = bp_unpack_value (bp, 1);
1059 node->address_taken = bp_unpack_value (bp, 1);
1060 node->used_from_other_partition = bp_unpack_value (bp, 1);
1061 node->lowered = bp_unpack_value (bp, 1);
1062 node->analyzed = tag == LTO_symtab_analyzed_node;
1063 node->in_other_partition = bp_unpack_value (bp, 1);
1064 if (node->in_other_partition
1065 /* Avoid updating decl when we are seeing just inline clone.
1066 When inlining function that has functions already inlined into it,
1067 we produce clones of inline clones.
1069 WPA partitioning might put each clone into different unit and
1070 we might end up streaming inline clone from other partition
1071 to support clone we are interested in. */
1072 && (!node->clone_of
1073 || node->clone_of->decl != node->decl))
1075 DECL_EXTERNAL (node->decl) = 1;
1076 TREE_STATIC (node->decl) = 0;
1078 node->alias = bp_unpack_value (bp, 1);
1079 node->weakref = bp_unpack_value (bp, 1);
1080 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
1081 node->only_called_at_startup = bp_unpack_value (bp, 1);
1082 node->only_called_at_exit = bp_unpack_value (bp, 1);
1083 node->tm_clone = bp_unpack_value (bp, 1);
1084 node->calls_comdat_local = bp_unpack_value (bp, 1);
1085 node->thunk.thunk_p = bp_unpack_value (bp, 1);
1086 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
1087 LDPR_NUM_KNOWN);
1088 gcc_assert (flag_ltrans
1089 || (!node->in_other_partition
1090 && !node->used_from_other_partition));
1093 /* Return string alias is alias of. */
1095 static tree
1096 get_alias_symbol (tree decl)
1098 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1099 return get_identifier (TREE_STRING_POINTER
1100 (TREE_VALUE (TREE_VALUE (alias))));
1103 /* Read a node from input_block IB. TAG is the node's tag just read.
1104 Return the node read or overwriten. */
1106 static struct cgraph_node *
1107 input_node (struct lto_file_decl_data *file_data,
1108 struct lto_input_block *ib,
1109 enum LTO_symtab_tags tag,
1110 vec<symtab_node *> nodes)
1112 gcc::pass_manager *passes = g->get_passes ();
1113 tree fn_decl;
1114 struct cgraph_node *node;
1115 struct bitpack_d bp;
1116 unsigned decl_index;
1117 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1118 int clone_ref;
1119 int order;
1120 int i, count;
1121 tree group;
1122 const char *section;
1124 order = streamer_read_hwi (ib) + order_base;
1125 clone_ref = streamer_read_hwi (ib);
1127 decl_index = streamer_read_uhwi (ib);
1128 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1130 if (clone_ref != LCC_NOT_FOUND)
1132 node = dyn_cast<cgraph_node *> (nodes[clone_ref])->create_clone (fn_decl,
1133 0, CGRAPH_FREQ_BASE, false,
1134 vNULL, false, NULL, NULL);
1136 else
1138 /* Declaration of functions can be already merged with a declaration
1139 from other input file. We keep cgraph unmerged until after streaming
1140 of ipa passes is done. Alays forcingly create a fresh node. */
1141 node = cgraph_node::create_empty ();
1142 node->decl = fn_decl;
1143 node->register_symbol ();
1146 node->order = order;
1147 if (order >= symtab_order)
1148 symtab_order = order + 1;
1150 node->count = streamer_read_gcov_count (ib);
1151 node->count_materialization_scale = streamer_read_hwi (ib);
1153 count = streamer_read_hwi (ib);
1154 node->ipa_transforms_to_apply = vNULL;
1155 for (i = 0; i < count; i++)
1157 opt_pass *pass;
1158 int pid = streamer_read_hwi (ib);
1160 gcc_assert (pid < passes->passes_by_id_size);
1161 pass = passes->passes_by_id[pid];
1162 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *) pass);
1165 if (tag == LTO_symtab_analyzed_node)
1166 ref = streamer_read_hwi (ib);
1168 group = read_identifier (ib);
1169 if (group)
1170 ref2 = streamer_read_hwi (ib);
1172 /* Make sure that we have not read this node before. Nodes that
1173 have already been read will have their tag stored in the 'aux'
1174 field. Since built-in functions can be referenced in multiple
1175 functions, they are expected to be read more than once. */
1176 if (node->aux && !DECL_BUILT_IN (node->decl))
1177 internal_error ("bytecode stream: found multiple instances of cgraph "
1178 "node with uid %d", node->uid);
1180 node->tp_first_run = streamer_read_uhwi (ib);
1182 bp = streamer_read_bitpack (ib);
1184 input_overwrite_node (file_data, node, tag, &bp);
1186 /* Store a reference for now, and fix up later to be a pointer. */
1187 node->global.inlined_to = (cgraph_node *) (intptr_t) ref;
1189 if (group)
1191 node->set_comdat_group (group);
1192 /* Store a reference for now, and fix up later to be a pointer. */
1193 node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
1195 else
1196 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1197 section = read_string (ib);
1198 if (section)
1199 node->set_section_for_node (section);
1201 if (node->thunk.thunk_p)
1203 int type = streamer_read_uhwi (ib);
1204 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1205 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1207 node->thunk.fixed_offset = fixed_offset;
1208 node->thunk.this_adjusting = (type & 2);
1209 node->thunk.virtual_value = virtual_value;
1210 node->thunk.virtual_offset_p = (type & 4);
1212 if (node->alias && !node->analyzed && node->weakref)
1213 node->alias_target = get_alias_symbol (node->decl);
1214 node->profile_id = streamer_read_hwi (ib);
1215 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1216 node->set_init_priority (streamer_read_hwi (ib));
1217 if (DECL_STATIC_DESTRUCTOR (node->decl))
1218 node->set_fini_priority (streamer_read_hwi (ib));
1219 return node;
1222 /* Read a node from input_block IB. TAG is the node's tag just read.
1223 Return the node read or overwriten. */
1225 static varpool_node *
1226 input_varpool_node (struct lto_file_decl_data *file_data,
1227 struct lto_input_block *ib)
1229 int decl_index;
1230 tree var_decl;
1231 varpool_node *node;
1232 struct bitpack_d bp;
1233 int ref = LCC_NOT_FOUND;
1234 int order;
1235 tree group;
1236 const char *section;
1238 order = streamer_read_hwi (ib) + order_base;
1239 decl_index = streamer_read_uhwi (ib);
1240 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1242 /* Declaration of functions can be already merged with a declaration
1243 from other input file. We keep cgraph unmerged until after streaming
1244 of ipa passes is done. Alays forcingly create a fresh node. */
1245 node = varpool_node::create_empty ();
1246 node->decl = var_decl;
1247 node->register_symbol ();
1249 node->order = order;
1250 if (order >= symtab_order)
1251 symtab_order = order + 1;
1252 node->lto_file_data = file_data;
1254 bp = streamer_read_bitpack (ib);
1255 node->externally_visible = bp_unpack_value (&bp, 1);
1256 node->force_output = bp_unpack_value (&bp, 1);
1257 node->forced_by_abi = bp_unpack_value (&bp, 1);
1258 node->unique_name = bp_unpack_value (&bp, 1);
1259 node->body_removed = bp_unpack_value (&bp, 1);
1260 node->implicit_section = bp_unpack_value (&bp, 1);
1261 node->writeonly = bp_unpack_value (&bp, 1);
1262 node->definition = bp_unpack_value (&bp, 1);
1263 node->alias = bp_unpack_value (&bp, 1);
1264 node->weakref = bp_unpack_value (&bp, 1);
1265 node->analyzed = bp_unpack_value (&bp, 1);
1266 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1267 node->in_other_partition = bp_unpack_value (&bp, 1);
1268 if (node->in_other_partition)
1270 DECL_EXTERNAL (node->decl) = 1;
1271 TREE_STATIC (node->decl) = 0;
1273 if (node->alias && !node->analyzed && node->weakref)
1274 node->alias_target = get_alias_symbol (node->decl);
1275 node->tls_model = (enum tls_model)bp_unpack_value (&bp, 3);
1276 node->used_by_single_function = (enum tls_model)bp_unpack_value (&bp, 1);
1277 group = read_identifier (ib);
1278 if (group)
1280 node->set_comdat_group (group);
1281 ref = streamer_read_hwi (ib);
1282 /* Store a reference for now, and fix up later to be a pointer. */
1283 node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1285 else
1286 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1287 section = read_string (ib);
1288 if (section)
1289 node->set_section_for_node (section);
1290 node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1291 LDPR_NUM_KNOWN);
1292 gcc_assert (flag_ltrans
1293 || (!node->in_other_partition
1294 && !node->used_from_other_partition));
1296 return node;
1299 /* Read a node from input_block IB. TAG is the node's tag just read.
1300 Return the node read or overwriten. */
1302 static void
1303 input_ref (struct lto_input_block *ib,
1304 symtab_node *referring_node,
1305 vec<symtab_node *> nodes)
1307 symtab_node *node = NULL;
1308 struct bitpack_d bp;
1309 enum ipa_ref_use use;
1310 bool speculative;
1311 struct ipa_ref *ref;
1313 bp = streamer_read_bitpack (ib);
1314 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1315 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1316 node = nodes[streamer_read_hwi (ib)];
1317 ref = referring_node->add_reference (node, use);
1318 ref->speculative = speculative;
1319 if (is_a <cgraph_node *> (referring_node))
1320 ref->lto_stmt_uid = streamer_read_hwi (ib);
1323 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1324 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1325 edge being read is indirect (in the sense that it has
1326 indirect_unknown_callee set). */
1328 static void
1329 input_edge (struct lto_input_block *ib, vec<symtab_node *> nodes,
1330 bool indirect)
1332 struct cgraph_node *caller, *callee;
1333 struct cgraph_edge *edge;
1334 unsigned int stmt_id;
1335 gcov_type count;
1336 int freq;
1337 cgraph_inline_failed_t inline_failed;
1338 struct bitpack_d bp;
1339 int ecf_flags = 0;
1341 caller = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1342 if (caller == NULL || caller->decl == NULL_TREE)
1343 internal_error ("bytecode stream: no caller found while reading edge");
1345 if (!indirect)
1347 callee = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1348 if (callee == NULL || callee->decl == NULL_TREE)
1349 internal_error ("bytecode stream: no callee found while reading edge");
1351 else
1352 callee = NULL;
1354 count = streamer_read_gcov_count (ib);
1356 bp = streamer_read_bitpack (ib);
1357 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_t, CIF_N_REASONS);
1358 stmt_id = bp_unpack_var_len_unsigned (&bp);
1359 freq = (int) bp_unpack_var_len_unsigned (&bp);
1361 if (indirect)
1362 edge = caller->create_indirect_edge (NULL, 0, count, freq);
1363 else
1364 edge = caller->create_edge (callee, NULL, count, freq);
1366 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1367 edge->speculative = bp_unpack_value (&bp, 1);
1368 edge->lto_stmt_uid = stmt_id;
1369 edge->inline_failed = inline_failed;
1370 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1371 edge->can_throw_external = bp_unpack_value (&bp, 1);
1372 if (indirect)
1374 if (bp_unpack_value (&bp, 1))
1375 ecf_flags |= ECF_CONST;
1376 if (bp_unpack_value (&bp, 1))
1377 ecf_flags |= ECF_PURE;
1378 if (bp_unpack_value (&bp, 1))
1379 ecf_flags |= ECF_NORETURN;
1380 if (bp_unpack_value (&bp, 1))
1381 ecf_flags |= ECF_MALLOC;
1382 if (bp_unpack_value (&bp, 1))
1383 ecf_flags |= ECF_NOTHROW;
1384 if (bp_unpack_value (&bp, 1))
1385 ecf_flags |= ECF_RETURNS_TWICE;
1386 edge->indirect_info->ecf_flags = ecf_flags;
1387 edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1388 if (edge->indirect_info->common_target_id)
1389 edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1394 /* Read a cgraph from IB using the info in FILE_DATA. */
1396 static vec<symtab_node *>
1397 input_cgraph_1 (struct lto_file_decl_data *file_data,
1398 struct lto_input_block *ib)
1400 enum LTO_symtab_tags tag;
1401 vec<symtab_node *> nodes = vNULL;
1402 symtab_node *node;
1403 unsigned i;
1405 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1406 order_base = symtab_order;
1407 while (tag)
1409 if (tag == LTO_symtab_edge)
1410 input_edge (ib, nodes, false);
1411 else if (tag == LTO_symtab_indirect_edge)
1412 input_edge (ib, nodes, true);
1413 else if (tag == LTO_symtab_variable)
1415 node = input_varpool_node (file_data, ib);
1416 nodes.safe_push (node);
1417 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1419 else
1421 node = input_node (file_data, ib, tag, nodes);
1422 if (node == NULL || node->decl == NULL_TREE)
1423 internal_error ("bytecode stream: found empty cgraph node");
1424 nodes.safe_push (node);
1425 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1428 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1431 lto_input_toplevel_asms (file_data, order_base);
1433 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1434 #ifdef ENABLE_CHECKING
1435 FOR_EACH_VEC_ELT (nodes, i, node)
1436 gcc_assert (node->aux || !is_a <cgraph_node *> (node));
1437 #endif
1438 FOR_EACH_VEC_ELT (nodes, i, node)
1440 int ref;
1441 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1443 ref = (int) (intptr_t) cnode->global.inlined_to;
1445 /* We share declaration of builtins, so we may read same node twice. */
1446 if (!node->aux)
1447 continue;
1448 node->aux = NULL;
1450 /* Fixup inlined_to from reference to pointer. */
1451 if (ref != LCC_NOT_FOUND)
1452 dyn_cast<cgraph_node *> (node)->global.inlined_to
1453 = dyn_cast<cgraph_node *> (nodes[ref]);
1454 else
1455 cnode->global.inlined_to = NULL;
1458 ref = (int) (intptr_t) node->same_comdat_group;
1460 /* Fixup same_comdat_group from reference to pointer. */
1461 if (ref != LCC_NOT_FOUND)
1462 node->same_comdat_group = nodes[ref];
1463 else
1464 node->same_comdat_group = NULL;
1466 FOR_EACH_VEC_ELT (nodes, i, node)
1467 node->aux = is_a <cgraph_node *> (node) ? (void *)1 : NULL;
1468 return nodes;
1471 /* Input ipa_refs. */
1473 static void
1474 input_refs (struct lto_input_block *ib,
1475 vec<symtab_node *> nodes)
1477 int count;
1478 int idx;
1479 while (true)
1481 symtab_node *node;
1482 count = streamer_read_uhwi (ib);
1483 if (!count)
1484 break;
1485 idx = streamer_read_uhwi (ib);
1486 node = nodes[idx];
1487 while (count)
1489 input_ref (ib, node, nodes);
1490 count--;
1496 static struct gcov_ctr_summary lto_gcov_summary;
1498 /* Input profile_info from IB. */
1499 static void
1500 input_profile_summary (struct lto_input_block *ib,
1501 struct lto_file_decl_data *file_data)
1503 unsigned h_ix;
1504 struct bitpack_d bp;
1505 unsigned int runs = streamer_read_uhwi (ib);
1506 if (runs)
1508 file_data->profile_info.runs = runs;
1509 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1510 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1512 memset (file_data->profile_info.histogram, 0,
1513 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1514 /* Input the bitpack of non-zero histogram indices. */
1515 bp = streamer_read_bitpack (ib);
1516 /* Read in and unpack the full bitpack, flagging non-zero
1517 histogram entries by setting the num_counters non-zero. */
1518 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1520 file_data->profile_info.histogram[h_ix].num_counters
1521 = bp_unpack_value (&bp, 1);
1523 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1525 if (!file_data->profile_info.histogram[h_ix].num_counters)
1526 continue;
1528 file_data->profile_info.histogram[h_ix].num_counters
1529 = streamer_read_gcov_count (ib);
1530 file_data->profile_info.histogram[h_ix].min_value
1531 = streamer_read_gcov_count (ib);
1532 file_data->profile_info.histogram[h_ix].cum_value
1533 = streamer_read_gcov_count (ib);
1535 /* IPA-profile computes hot bb threshold based on cumulated
1536 whole program profile. We need to stream it down to ltrans. */
1537 if (flag_ltrans)
1538 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1543 /* Rescale profile summaries to the same number of runs in the whole unit. */
1545 static void
1546 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1548 struct lto_file_decl_data *file_data;
1549 unsigned int j, h_ix;
1550 gcov_unsigned_t max_runs = 0;
1551 struct cgraph_node *node;
1552 struct cgraph_edge *edge;
1553 gcov_type saved_sum_all = 0;
1554 gcov_ctr_summary *saved_profile_info = 0;
1555 int saved_scale = 0;
1557 /* Find unit with maximal number of runs. If we ever get serious about
1558 roundoff errors, we might also consider computing smallest common
1559 multiply. */
1560 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1561 if (max_runs < file_data->profile_info.runs)
1562 max_runs = file_data->profile_info.runs;
1564 if (!max_runs)
1565 return;
1567 /* Simple overflow check. We probably don't need to support that many train
1568 runs. Such a large value probably imply data corruption anyway. */
1569 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1571 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1572 INT_MAX / REG_BR_PROB_BASE);
1573 return;
1576 profile_info = &lto_gcov_summary;
1577 lto_gcov_summary.runs = max_runs;
1578 lto_gcov_summary.sum_max = 0;
1579 memset (lto_gcov_summary.histogram, 0,
1580 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1582 /* Rescale all units to the maximal number of runs.
1583 sum_max can not be easily merged, as we have no idea what files come from
1584 the same run. We do not use the info anyway, so leave it 0. */
1585 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1586 if (file_data->profile_info.runs)
1588 int scale = GCOV_COMPUTE_SCALE (max_runs,
1589 file_data->profile_info.runs);
1590 lto_gcov_summary.sum_max
1591 = MAX (lto_gcov_summary.sum_max,
1592 apply_scale (file_data->profile_info.sum_max, scale));
1593 lto_gcov_summary.sum_all
1594 = MAX (lto_gcov_summary.sum_all,
1595 apply_scale (file_data->profile_info.sum_all, scale));
1596 /* Save a pointer to the profile_info with the largest
1597 scaled sum_all and the scale for use in merging the
1598 histogram. */
1599 if (!saved_profile_info
1600 || lto_gcov_summary.sum_all > saved_sum_all)
1602 saved_profile_info = &file_data->profile_info;
1603 saved_sum_all = lto_gcov_summary.sum_all;
1604 saved_scale = scale;
1608 gcc_assert (saved_profile_info);
1610 /* Scale up the histogram from the profile that had the largest
1611 scaled sum_all above. */
1612 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1614 /* Scale up the min value as we did the corresponding sum_all
1615 above. Use that to find the new histogram index. */
1616 gcov_type scaled_min
1617 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1618 saved_scale);
1619 /* The new index may be shared with another scaled histogram entry,
1620 so we need to account for a non-zero histogram entry at new_ix. */
1621 unsigned new_ix = gcov_histo_index (scaled_min);
1622 lto_gcov_summary.histogram[new_ix].min_value
1623 = (lto_gcov_summary.histogram[new_ix].num_counters
1624 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1625 : scaled_min);
1626 /* Some of the scaled counter values would ostensibly need to be placed
1627 into different (larger) histogram buckets, but we keep things simple
1628 here and place the scaled cumulative counter value in the bucket
1629 corresponding to the scaled minimum counter value. */
1630 lto_gcov_summary.histogram[new_ix].cum_value
1631 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1632 saved_scale);
1633 lto_gcov_summary.histogram[new_ix].num_counters
1634 += saved_profile_info->histogram[h_ix].num_counters;
1637 /* Watch roundoff errors. */
1638 if (lto_gcov_summary.sum_max < max_runs)
1639 lto_gcov_summary.sum_max = max_runs;
1641 /* If merging already happent at WPA time, we are done. */
1642 if (flag_ltrans)
1643 return;
1645 /* Now compute count_materialization_scale of each node.
1646 During LTRANS we already have values of count_materialization_scale
1647 computed, so just update them. */
1648 FOR_EACH_FUNCTION (node)
1649 if (node->lto_file_data
1650 && node->lto_file_data->profile_info.runs)
1652 int scale;
1654 scale = RDIV (node->count_materialization_scale * max_runs,
1655 node->lto_file_data->profile_info.runs);
1656 node->count_materialization_scale = scale;
1657 if (scale < 0)
1658 fatal_error ("Profile information in %s corrupted",
1659 file_data->file_name);
1661 if (scale == REG_BR_PROB_BASE)
1662 continue;
1663 for (edge = node->callees; edge; edge = edge->next_callee)
1664 edge->count = apply_scale (edge->count, scale);
1665 node->count = apply_scale (node->count, scale);
1669 /* Input and merge the symtab from each of the .o files passed to
1670 lto1. */
1672 void
1673 input_symtab (void)
1675 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1676 struct lto_file_decl_data *file_data;
1677 unsigned int j = 0;
1678 struct cgraph_node *node;
1680 while ((file_data = file_data_vec[j++]))
1682 const char *data;
1683 size_t len;
1684 struct lto_input_block *ib;
1685 vec<symtab_node *> nodes;
1687 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1688 &data, &len);
1689 if (!ib)
1690 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1691 input_profile_summary (ib, file_data);
1692 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1693 nodes = input_cgraph_1 (file_data, ib);
1694 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1695 ib, data, len);
1697 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1698 &data, &len);
1699 if (!ib)
1700 fatal_error ("cannot find LTO section refs in %s",
1701 file_data->file_name);
1702 input_refs (ib, nodes);
1703 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1704 ib, data, len);
1705 if (flag_ltrans)
1706 input_cgraph_opt_summary (nodes);
1707 nodes.release ();
1710 merge_profile_summaries (file_data_vec);
1711 get_working_sets ();
1714 /* Clear out the aux field that was used to store enough state to
1715 tell which nodes should be overwritten. */
1716 FOR_EACH_FUNCTION (node)
1718 /* Some nodes may have been created by cgraph_node. This
1719 happens when the callgraph contains nested functions. If the
1720 node for the parent function was never emitted to the gimple
1721 file, cgraph_node will create a node for it when setting the
1722 context of the nested function. */
1723 if (node->lto_file_data)
1724 node->aux = NULL;
1728 /* True when we need optimization summary for NODE. */
1730 static int
1731 output_cgraph_opt_summary_p (struct cgraph_node *node)
1733 return (node->clone_of
1734 && (node->clone.tree_map
1735 || node->clone.args_to_skip
1736 || node->clone.combined_args_to_skip));
1739 /* Output optimization summary for EDGE to OB. */
1740 static void
1741 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1742 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1746 /* Output optimization summary for NODE to OB. */
1748 static void
1749 output_node_opt_summary (struct output_block *ob,
1750 struct cgraph_node *node,
1751 lto_symtab_encoder_t encoder)
1753 unsigned int index;
1754 bitmap_iterator bi;
1755 struct ipa_replace_map *map;
1756 struct bitpack_d bp;
1757 int i;
1758 struct cgraph_edge *e;
1760 if (node->clone.args_to_skip)
1762 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1763 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1764 streamer_write_uhwi (ob, index);
1766 else
1767 streamer_write_uhwi (ob, 0);
1768 if (node->clone.combined_args_to_skip)
1770 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1771 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1772 streamer_write_uhwi (ob, index);
1774 else
1775 streamer_write_uhwi (ob, 0);
1776 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1777 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1779 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1780 mechanism to store function local declarations into summaries. */
1781 gcc_assert (!map->old_tree);
1782 streamer_write_uhwi (ob, map->parm_num);
1783 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1784 stream_write_tree (ob, map->new_tree, true);
1785 bp = bitpack_create (ob->main_stream);
1786 bp_pack_value (&bp, map->replace_p, 1);
1787 bp_pack_value (&bp, map->ref_p, 1);
1788 streamer_write_bitpack (&bp);
1791 if (lto_symtab_encoder_in_partition_p (encoder, node))
1793 for (e = node->callees; e; e = e->next_callee)
1794 output_edge_opt_summary (ob, e);
1795 for (e = node->indirect_calls; e; e = e->next_callee)
1796 output_edge_opt_summary (ob, e);
1800 /* Output optimization summaries stored in callgraph.
1801 At the moment it is the clone info structure. */
1803 static void
1804 output_cgraph_opt_summary (void)
1806 int i, n_nodes;
1807 lto_symtab_encoder_t encoder;
1808 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1809 unsigned count = 0;
1811 ob->symbol = NULL;
1812 encoder = ob->decl_state->symtab_node_encoder;
1813 n_nodes = lto_symtab_encoder_size (encoder);
1814 for (i = 0; i < n_nodes; i++)
1816 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1817 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1818 if (cnode && output_cgraph_opt_summary_p (cnode))
1819 count++;
1821 streamer_write_uhwi (ob, count);
1822 for (i = 0; i < n_nodes; i++)
1824 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1825 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1826 if (cnode && output_cgraph_opt_summary_p (cnode))
1828 streamer_write_uhwi (ob, i);
1829 output_node_opt_summary (ob, cnode, encoder);
1832 produce_asm (ob, NULL);
1833 destroy_output_block (ob);
1836 /* Input optimisation summary of EDGE. */
1838 static void
1839 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
1840 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
1844 /* Input optimisation summary of NODE. */
1846 static void
1847 input_node_opt_summary (struct cgraph_node *node,
1848 struct lto_input_block *ib_main,
1849 struct data_in *data_in)
1851 int i;
1852 int count;
1853 int bit;
1854 struct bitpack_d bp;
1855 struct cgraph_edge *e;
1857 count = streamer_read_uhwi (ib_main);
1858 if (count)
1859 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1860 for (i = 0; i < count; i++)
1862 bit = streamer_read_uhwi (ib_main);
1863 bitmap_set_bit (node->clone.args_to_skip, bit);
1865 count = streamer_read_uhwi (ib_main);
1866 if (count)
1867 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1868 for (i = 0; i < count; i++)
1870 bit = streamer_read_uhwi (ib_main);
1871 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1873 count = streamer_read_uhwi (ib_main);
1874 for (i = 0; i < count; i++)
1876 struct ipa_replace_map *map = ggc_alloc<ipa_replace_map> ();
1878 vec_safe_push (node->clone.tree_map, map);
1879 map->parm_num = streamer_read_uhwi (ib_main);
1880 map->old_tree = NULL;
1881 map->new_tree = stream_read_tree (ib_main, data_in);
1882 bp = streamer_read_bitpack (ib_main);
1883 map->replace_p = bp_unpack_value (&bp, 1);
1884 map->ref_p = bp_unpack_value (&bp, 1);
1886 for (e = node->callees; e; e = e->next_callee)
1887 input_edge_opt_summary (e, ib_main);
1888 for (e = node->indirect_calls; e; e = e->next_callee)
1889 input_edge_opt_summary (e, ib_main);
1892 /* Read section in file FILE_DATA of length LEN with data DATA. */
1894 static void
1895 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1896 const char *data, size_t len,
1897 vec<symtab_node *> nodes)
1899 const struct lto_function_header *header =
1900 (const struct lto_function_header *) data;
1901 const int cfg_offset = sizeof (struct lto_function_header);
1902 const int main_offset = cfg_offset + header->cfg_size;
1903 const int string_offset = main_offset + header->main_size;
1904 struct data_in *data_in;
1905 struct lto_input_block ib_main;
1906 unsigned int i;
1907 unsigned int count;
1909 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1910 header->main_size);
1912 data_in =
1913 lto_data_in_create (file_data, (const char *) data + string_offset,
1914 header->string_size, vNULL);
1915 count = streamer_read_uhwi (&ib_main);
1917 for (i = 0; i < count; i++)
1919 int ref = streamer_read_uhwi (&ib_main);
1920 input_node_opt_summary (dyn_cast<cgraph_node *> (nodes[ref]),
1921 &ib_main, data_in);
1923 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1924 len);
1925 lto_data_in_delete (data_in);
1928 /* Input optimization summary of cgraph. */
1930 static void
1931 input_cgraph_opt_summary (vec<symtab_node *> nodes)
1933 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1934 struct lto_file_decl_data *file_data;
1935 unsigned int j = 0;
1937 while ((file_data = file_data_vec[j++]))
1939 size_t len;
1940 const char *data =
1941 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1942 &len);
1944 if (data)
1945 input_cgraph_opt_section (file_data, data, len, nodes);