Optimize powerpc*-*-linux* e500 hardfp/soft-fp use.
[official-gcc.git] / gcc / lto-cgraph.c
blob28ec341f5654aa84f87f0a77773e43dd3c574dc2
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 "hash-map.h"
52 #include "plugin-api.h"
53 #include "ipa-ref.h"
54 #include "cgraph.h"
55 #include "lto-streamer.h"
56 #include "data-streamer.h"
57 #include "tree-streamer.h"
58 #include "gcov-io.h"
59 #include "tree-pass.h"
60 #include "profile.h"
61 #include "context.h"
62 #include "pass_manager.h"
63 #include "ipa-utils.h"
65 /* True when asm nodes has been output. */
66 bool asm_nodes_output = false;
68 static void output_cgraph_opt_summary (void);
69 static void input_cgraph_opt_summary (vec<symtab_node *> nodes);
71 /* Number of LDPR values known to GCC. */
72 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
74 /* All node orders are ofsetted by ORDER_BASE. */
75 static int order_base;
77 /* Cgraph streaming is organized as set of record whose type
78 is indicated by a tag. */
79 enum LTO_symtab_tags
81 /* Must leave 0 for the stopper. */
83 /* Cgraph node without body available. */
84 LTO_symtab_unavail_node = 1,
85 /* Cgraph node with function body. */
86 LTO_symtab_analyzed_node,
87 /* Cgraph edges. */
88 LTO_symtab_edge,
89 LTO_symtab_indirect_edge,
90 LTO_symtab_variable,
91 LTO_symtab_last_tag
94 /* Create a new symtab encoder.
95 if FOR_INPUT, the encoder allocate only datastructures needed
96 to read the symtab. */
98 lto_symtab_encoder_t
99 lto_symtab_encoder_new (bool for_input)
101 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
103 if (!for_input)
104 encoder->map = new hash_map<symtab_node *, size_t>;
105 encoder->nodes.create (0);
106 return encoder;
110 /* Delete ENCODER and its components. */
112 void
113 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
115 encoder->nodes.release ();
116 if (encoder->map)
117 delete encoder->map;
118 free (encoder);
122 /* Return the existing reference number of NODE in the symtab encoder in
123 output block OB. Assign a new reference if this is the first time
124 NODE is encoded. */
127 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
128 symtab_node *node)
130 int ref;
132 if (!encoder->map)
134 lto_encoder_entry entry = {node, false, false, false};
136 ref = encoder->nodes.length ();
137 encoder->nodes.safe_push (entry);
138 return ref;
141 size_t *slot = encoder->map->get (node);
142 if (!slot || !*slot)
144 lto_encoder_entry entry = {node, false, false, false};
145 ref = encoder->nodes.length ();
146 if (!slot)
147 encoder->map->put (node, ref + 1);
148 encoder->nodes.safe_push (entry);
150 else
151 ref = *slot - 1;
153 return ref;
156 /* Remove NODE from encoder. */
158 bool
159 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
160 symtab_node *node)
162 int index;
163 lto_encoder_entry last_node;
165 size_t *slot = encoder->map->get (node);
166 if (slot == NULL || !*slot)
167 return false;
169 index = *slot - 1;
170 gcc_checking_assert (encoder->nodes[index].node == node);
172 /* Remove from vector. We do this by swapping node with the last element
173 of the vector. */
174 last_node = encoder->nodes.pop ();
175 if (last_node.node != node)
177 gcc_assert (encoder->map->put (last_node.node, index + 1));
179 /* Move the last element to the original spot of NODE. */
180 encoder->nodes[index] = last_node;
183 /* Remove element from hash table. */
184 encoder->map->remove (node);
185 return true;
189 /* Return TRUE if we should encode initializer of NODE (if any). */
191 bool
192 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
193 struct cgraph_node *node)
195 int index = lto_symtab_encoder_lookup (encoder, node);
196 return encoder->nodes[index].body;
199 /* Return TRUE if we should encode body of NODE (if any). */
201 static void
202 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
203 struct cgraph_node *node)
205 int index = lto_symtab_encoder_encode (encoder, node);
206 gcc_checking_assert (encoder->nodes[index].node == node);
207 encoder->nodes[index].body = true;
210 /* Return TRUE if we should encode initializer of NODE (if any). */
212 bool
213 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
214 varpool_node *node)
216 int index = lto_symtab_encoder_lookup (encoder, node);
217 if (index == LCC_NOT_FOUND)
218 return false;
219 return encoder->nodes[index].initializer;
222 /* Return TRUE if we should encode initializer of NODE (if any). */
224 static void
225 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
226 varpool_node *node)
228 int index = lto_symtab_encoder_lookup (encoder, node);
229 encoder->nodes[index].initializer = true;
232 /* Return TRUE if we should encode initializer of NODE (if any). */
234 bool
235 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
236 symtab_node *node)
238 int index = lto_symtab_encoder_lookup (encoder, node);
239 if (index == LCC_NOT_FOUND)
240 return false;
241 return encoder->nodes[index].in_partition;
244 /* Return TRUE if we should encode body of NODE (if any). */
246 void
247 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
248 symtab_node *node)
250 int index = lto_symtab_encoder_encode (encoder, node);
251 encoder->nodes[index].in_partition = true;
254 /* Output the cgraph EDGE to OB using ENCODER. */
256 static void
257 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
258 lto_symtab_encoder_t encoder)
260 unsigned int uid;
261 intptr_t ref;
262 struct bitpack_d bp;
264 if (edge->indirect_unknown_callee)
265 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
266 LTO_symtab_indirect_edge);
267 else
268 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
269 LTO_symtab_edge);
271 ref = lto_symtab_encoder_lookup (encoder, edge->caller);
272 gcc_assert (ref != LCC_NOT_FOUND);
273 streamer_write_hwi_stream (ob->main_stream, ref);
275 if (!edge->indirect_unknown_callee)
277 ref = lto_symtab_encoder_lookup (encoder, edge->callee);
278 gcc_assert (ref != LCC_NOT_FOUND);
279 streamer_write_hwi_stream (ob->main_stream, ref);
282 streamer_write_gcov_count_stream (ob->main_stream, edge->count);
284 bp = bitpack_create (ob->main_stream);
285 uid = (!gimple_has_body_p (edge->caller->decl)
286 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt) + 1);
287 bp_pack_enum (&bp, cgraph_inline_failed_t,
288 CIF_N_REASONS, edge->inline_failed);
289 bp_pack_var_len_unsigned (&bp, uid);
290 bp_pack_var_len_unsigned (&bp, edge->frequency);
291 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
292 bp_pack_value (&bp, edge->speculative, 1);
293 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
294 bp_pack_value (&bp, edge->can_throw_external, 1);
295 bp_pack_value (&bp, edge->in_polymorphic_cdtor, 1);
296 if (edge->indirect_unknown_callee)
298 int flags = edge->indirect_info->ecf_flags;
299 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
300 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
301 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
302 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
303 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
304 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
305 /* Flags that should not appear on indirect calls. */
306 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
307 | ECF_MAY_BE_ALLOCA
308 | ECF_SIBCALL
309 | ECF_LEAF
310 | ECF_NOVOPS)));
312 streamer_write_bitpack (&bp);
313 if (edge->indirect_unknown_callee)
315 streamer_write_hwi_stream (ob->main_stream,
316 edge->indirect_info->common_target_id);
317 if (edge->indirect_info->common_target_id)
318 streamer_write_hwi_stream
319 (ob->main_stream, edge->indirect_info->common_target_probability);
323 /* Return if NODE contain references from other partitions. */
325 bool
326 referenced_from_other_partition_p (symtab_node *node, lto_symtab_encoder_t encoder)
328 int i;
329 struct ipa_ref *ref = NULL;
331 for (i = 0; node->iterate_referring (i, ref); i++)
333 if (ref->referring->in_other_partition
334 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
335 return true;
337 return false;
340 /* Return true when node is reachable from other partition. */
342 bool
343 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
345 struct cgraph_edge *e;
346 if (!node->definition)
347 return false;
348 if (node->global.inlined_to)
349 return false;
350 for (e = node->callers; e; e = e->next_caller)
351 if (e->caller->in_other_partition
352 || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
353 return true;
354 return false;
357 /* Return if NODE contain references from other partitions. */
359 bool
360 referenced_from_this_partition_p (symtab_node *node,
361 lto_symtab_encoder_t encoder)
363 int i;
364 struct ipa_ref *ref = NULL;
366 for (i = 0; node->iterate_referring (i, ref); i++)
367 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
368 return true;
369 return false;
372 /* Return true when node is reachable from other partition. */
374 bool
375 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
377 struct cgraph_edge *e;
378 for (e = node->callers; e; e = e->next_caller)
379 if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
380 return true;
381 return false;
384 /* Output the cgraph NODE to OB. ENCODER is used to find the
385 reference number of NODE->inlined_to. SET is the set of nodes we
386 are writing to the current file. If NODE is not in SET, then NODE
387 is a boundary of a cgraph_node_set and we pretend NODE just has a
388 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
389 that have had their callgraph node written so far. This is used to
390 determine if NODE is a clone of a previously written node. */
392 static void
393 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
394 lto_symtab_encoder_t encoder)
396 unsigned int tag;
397 struct bitpack_d bp;
398 bool boundary_p;
399 intptr_t ref;
400 bool in_other_partition = false;
401 struct cgraph_node *clone_of, *ultimate_clone_of;
402 ipa_opt_pass_d *pass;
403 int i;
404 bool alias_p;
405 const char *comdat;
406 const char *section;
407 tree group;
409 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
411 if (node->analyzed && !boundary_p)
412 tag = LTO_symtab_analyzed_node;
413 else
414 tag = LTO_symtab_unavail_node;
416 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
417 tag);
418 streamer_write_hwi_stream (ob->main_stream, node->order);
420 /* In WPA mode, we only output part of the call-graph. Also, we
421 fake cgraph node attributes. There are two cases that we care.
423 Boundary nodes: There are nodes that are not part of SET but are
424 called from within SET. We artificially make them look like
425 externally visible nodes with no function body.
427 Cherry-picked nodes: These are nodes we pulled from other
428 translation units into SET during IPA-inlining. We make them as
429 local static nodes to prevent clashes with other local statics. */
430 if (boundary_p && node->analyzed
431 && node->get_partitioning_class () == SYMBOL_PARTITION)
433 /* Inline clones can not be part of boundary.
434 gcc_assert (!node->global.inlined_to);
436 FIXME: At the moment they can be, when partition contains an inline
437 clone that is clone of inline clone from outside partition. We can
438 reshape the clone tree and make other tree to be the root, but it
439 needs a bit extra work and will be promplty done by cgraph_remove_node
440 after reading back. */
441 in_other_partition = 1;
444 clone_of = node->clone_of;
445 while (clone_of
446 && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
447 if (clone_of->prev_sibling_clone)
448 clone_of = clone_of->prev_sibling_clone;
449 else
450 clone_of = clone_of->clone_of;
452 /* See if body of the master function is output. If not, we are seeing only
453 an declaration and we do not need to pass down clone tree. */
454 ultimate_clone_of = clone_of;
455 while (ultimate_clone_of && ultimate_clone_of->clone_of)
456 ultimate_clone_of = ultimate_clone_of->clone_of;
458 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
459 clone_of = NULL;
461 if (tag == LTO_symtab_analyzed_node)
462 gcc_assert (clone_of || !node->clone_of);
463 if (!clone_of)
464 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
465 else
466 streamer_write_hwi_stream (ob->main_stream, ref);
469 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
470 streamer_write_gcov_count_stream (ob->main_stream, node->count);
471 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
473 streamer_write_hwi_stream (ob->main_stream,
474 node->ipa_transforms_to_apply.length ());
475 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
476 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
478 if (tag == LTO_symtab_analyzed_node)
480 if (node->global.inlined_to)
482 ref = lto_symtab_encoder_lookup (encoder, node->global.inlined_to);
483 gcc_assert (ref != LCC_NOT_FOUND);
485 else
486 ref = LCC_NOT_FOUND;
488 streamer_write_hwi_stream (ob->main_stream, ref);
491 group = node->get_comdat_group ();
492 if (group)
493 comdat = IDENTIFIER_POINTER (group);
494 else
495 comdat = "";
496 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
498 if (group)
500 if (node->same_comdat_group && !boundary_p)
502 ref = lto_symtab_encoder_lookup (encoder,
503 node->same_comdat_group);
504 gcc_assert (ref != LCC_NOT_FOUND);
506 else
507 ref = LCC_NOT_FOUND;
508 streamer_write_hwi_stream (ob->main_stream, ref);
511 section = node->get_section ();
512 if (!section)
513 section = "";
515 streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
517 bp = bitpack_create (ob->main_stream);
518 bp_pack_value (&bp, node->local.local, 1);
519 bp_pack_value (&bp, node->externally_visible, 1);
520 bp_pack_value (&bp, node->no_reorder, 1);
521 bp_pack_value (&bp, node->definition, 1);
522 bp_pack_value (&bp, node->local.versionable, 1);
523 bp_pack_value (&bp, node->local.can_change_signature, 1);
524 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
525 bp_pack_value (&bp, node->force_output, 1);
526 bp_pack_value (&bp, node->forced_by_abi, 1);
527 bp_pack_value (&bp, node->unique_name, 1);
528 bp_pack_value (&bp, node->body_removed, 1);
529 bp_pack_value (&bp, node->implicit_section, 1);
530 bp_pack_value (&bp, node->address_taken, 1);
531 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
532 && node->get_partitioning_class () == SYMBOL_PARTITION
533 && (reachable_from_other_partition_p (node, encoder)
534 || referenced_from_other_partition_p (node, encoder)), 1);
535 bp_pack_value (&bp, node->lowered, 1);
536 bp_pack_value (&bp, in_other_partition, 1);
537 /* Real aliases in a boundary become non-aliases. However we still stream
538 alias info on weakrefs.
539 TODO: We lose a bit of information here - when we know that variable is
540 defined in other unit, we may use the info on aliases to resolve
541 symbol1 != symbol2 type tests that we can do only for locally defined objects
542 otherwise. */
543 alias_p = node->alias && (!boundary_p || node->weakref);
544 bp_pack_value (&bp, alias_p, 1);
545 bp_pack_value (&bp, node->weakref, 1);
546 bp_pack_value (&bp, node->frequency, 2);
547 bp_pack_value (&bp, node->only_called_at_startup, 1);
548 bp_pack_value (&bp, node->only_called_at_exit, 1);
549 bp_pack_value (&bp, node->tm_clone, 1);
550 bp_pack_value (&bp, node->calls_comdat_local, 1);
551 bp_pack_value (&bp, node->icf_merged, 1);
552 bp_pack_value (&bp, node->thunk.thunk_p && !boundary_p, 1);
553 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
554 LDPR_NUM_KNOWN, node->resolution);
555 streamer_write_bitpack (&bp);
556 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
558 if (node->thunk.thunk_p && !boundary_p)
560 streamer_write_uhwi_stream
561 (ob->main_stream,
562 1 + (node->thunk.this_adjusting != 0) * 2
563 + (node->thunk.virtual_offset_p != 0) * 4);
564 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
565 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
567 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
568 if (DECL_STATIC_CONSTRUCTOR (node->decl))
569 streamer_write_hwi_stream (ob->main_stream, node->get_init_priority ());
570 if (DECL_STATIC_DESTRUCTOR (node->decl))
571 streamer_write_hwi_stream (ob->main_stream, node->get_fini_priority ());
574 /* Output the varpool NODE to OB.
575 If NODE is not in SET, then NODE is a boundary. */
577 static void
578 lto_output_varpool_node (struct lto_simple_output_block *ob, varpool_node *node,
579 lto_symtab_encoder_t encoder)
581 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
582 struct bitpack_d bp;
583 int ref;
584 bool alias_p;
585 const char *comdat;
586 const char *section;
587 tree group;
589 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
590 LTO_symtab_variable);
591 streamer_write_hwi_stream (ob->main_stream, node->order);
592 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
593 bp = bitpack_create (ob->main_stream);
594 bp_pack_value (&bp, node->externally_visible, 1);
595 bp_pack_value (&bp, node->no_reorder, 1);
596 bp_pack_value (&bp, node->force_output, 1);
597 bp_pack_value (&bp, node->forced_by_abi, 1);
598 bp_pack_value (&bp, node->unique_name, 1);
599 bp_pack_value (&bp, node->body_removed, 1);
600 bp_pack_value (&bp, node->implicit_section, 1);
601 bp_pack_value (&bp, node->writeonly, 1);
602 bp_pack_value (&bp, node->definition, 1);
603 alias_p = node->alias && (!boundary_p || node->weakref);
604 bp_pack_value (&bp, alias_p, 1);
605 bp_pack_value (&bp, node->weakref, 1);
606 bp_pack_value (&bp, node->analyzed && !boundary_p, 1);
607 gcc_assert (node->definition || !node->analyzed);
608 /* Constant pool initializers can be de-unified into individual ltrans units.
609 FIXME: Alternatively at -Os we may want to avoid generating for them the local
610 labels and share them across LTRANS partitions. */
611 if (node->get_partitioning_class () != SYMBOL_PARTITION)
613 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
614 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
616 else
618 bp_pack_value (&bp, node->definition
619 && referenced_from_other_partition_p (node, encoder), 1);
620 bp_pack_value (&bp, node->analyzed
621 && boundary_p && !DECL_EXTERNAL (node->decl), 1);
622 /* in_other_partition. */
624 bp_pack_value (&bp, node->tls_model, 3);
625 bp_pack_value (&bp, node->used_by_single_function, 1);
626 streamer_write_bitpack (&bp);
628 group = node->get_comdat_group ();
629 if (group)
630 comdat = IDENTIFIER_POINTER (group);
631 else
632 comdat = "";
633 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
635 if (group)
637 if (node->same_comdat_group && !boundary_p)
639 ref = lto_symtab_encoder_lookup (encoder,
640 node->same_comdat_group);
641 gcc_assert (ref != LCC_NOT_FOUND);
643 else
644 ref = LCC_NOT_FOUND;
645 streamer_write_hwi_stream (ob->main_stream, ref);
648 section = node->get_section ();
649 if (!section)
650 section = "";
651 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
653 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
654 LDPR_NUM_KNOWN, node->resolution);
657 /* Output the varpool NODE to OB.
658 If NODE is not in SET, then NODE is a boundary. */
660 static void
661 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
662 lto_symtab_encoder_t encoder)
664 struct bitpack_d bp;
665 int nref;
666 int uid = ref->lto_stmt_uid;
667 struct cgraph_node *node;
669 bp = bitpack_create (ob->main_stream);
670 bp_pack_value (&bp, ref->use, 2);
671 bp_pack_value (&bp, ref->speculative, 1);
672 streamer_write_bitpack (&bp);
673 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
674 gcc_assert (nref != LCC_NOT_FOUND);
675 streamer_write_hwi_stream (ob->main_stream, nref);
677 node = dyn_cast <cgraph_node *> (ref->referring);
678 if (node)
680 if (ref->stmt)
681 uid = gimple_uid (ref->stmt) + 1;
682 streamer_write_hwi_stream (ob->main_stream, uid);
686 /* Stream out profile_summary to OB. */
688 static void
689 output_profile_summary (struct lto_simple_output_block *ob)
691 unsigned h_ix;
692 struct bitpack_d bp;
694 if (profile_info)
696 /* We do not output num and run_max, they are not used by
697 GCC profile feedback and they are difficult to merge from multiple
698 units. */
699 gcc_assert (profile_info->runs);
700 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
701 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
703 /* sum_all is needed for computing the working set with the
704 histogram. */
705 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
707 /* Create and output a bitpack of non-zero histogram entries indices. */
708 bp = bitpack_create (ob->main_stream);
709 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
710 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
711 streamer_write_bitpack (&bp);
712 /* Now stream out only those non-zero entries. */
713 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
715 if (!profile_info->histogram[h_ix].num_counters)
716 continue;
717 streamer_write_gcov_count_stream (ob->main_stream,
718 profile_info->histogram[h_ix].num_counters);
719 streamer_write_gcov_count_stream (ob->main_stream,
720 profile_info->histogram[h_ix].min_value);
721 streamer_write_gcov_count_stream (ob->main_stream,
722 profile_info->histogram[h_ix].cum_value);
724 /* IPA-profile computes hot bb threshold based on cumulated
725 whole program profile. We need to stream it down to ltrans. */
726 if (flag_wpa)
727 streamer_write_gcov_count_stream (ob->main_stream,
728 get_hot_bb_threshold ());
730 else
731 streamer_write_uhwi_stream (ob->main_stream, 0);
734 /* Output all callees or indirect outgoing edges. EDGE must be the first such
735 edge. */
737 static void
738 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
739 struct lto_simple_output_block *ob,
740 lto_symtab_encoder_t encoder)
742 if (!edge)
743 return;
745 /* Output edges in backward direction, so the reconstructed callgraph match
746 and it is easy to associate call sites in the IPA pass summaries. */
747 while (edge->next_callee)
748 edge = edge->next_callee;
749 for (; edge; edge = edge->prev_callee)
750 lto_output_edge (ob, edge, encoder);
753 /* Output the part of the cgraph in SET. */
755 static void
756 output_refs (lto_symtab_encoder_t encoder)
758 lto_symtab_encoder_iterator lsei;
759 struct lto_simple_output_block *ob;
760 int count;
761 struct ipa_ref *ref;
762 int i;
764 ob = lto_create_simple_output_block (LTO_section_refs);
766 for (lsei = lsei_start_in_partition (encoder); !lsei_end_p (lsei);
767 lsei_next_in_partition (&lsei))
769 symtab_node *node = lsei_node (lsei);
771 count = node->ref_list.nreferences ();
772 if (count)
774 streamer_write_gcov_count_stream (ob->main_stream, count);
775 streamer_write_uhwi_stream (ob->main_stream,
776 lto_symtab_encoder_lookup (encoder, node));
777 for (i = 0; node->iterate_reference (i, ref); i++)
778 lto_output_ref (ob, ref, encoder);
782 streamer_write_uhwi_stream (ob->main_stream, 0);
784 lto_destroy_simple_output_block (ob);
787 /* Add NODE into encoder as well as nodes it is cloned from.
788 Do it in a way so clones appear first. */
790 static void
791 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
792 bool include_body)
794 if (node->clone_of)
795 add_node_to (encoder, node->clone_of, include_body);
796 else if (include_body)
797 lto_set_symtab_encoder_encode_body (encoder, node);
798 lto_symtab_encoder_encode (encoder, node);
801 /* Add all references in NODE to encoders. */
803 static void
804 create_references (lto_symtab_encoder_t encoder, symtab_node *node)
806 int i;
807 struct ipa_ref *ref = NULL;
808 for (i = 0; node->iterate_reference (i, ref); i++)
809 if (is_a <cgraph_node *> (ref->referred))
810 add_node_to (encoder, dyn_cast <cgraph_node *> (ref->referred), false);
811 else
812 lto_symtab_encoder_encode (encoder, ref->referred);
815 /* Find all symbols we want to stream into given partition and insert them
816 to encoders.
818 The function actually replaces IN_ENCODER by new one. The reason is that
819 streaming code needs clone's origin to be streamed before clone. This
820 means that we need to insert the nodes in specific order. This order is
821 ignored by the partitioning logic earlier. */
823 lto_symtab_encoder_t
824 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
826 struct cgraph_edge *edge;
827 int i;
828 lto_symtab_encoder_t encoder;
829 lto_symtab_encoder_iterator lsei;
830 hash_set<void *> reachable_call_targets;
832 encoder = lto_symtab_encoder_new (false);
834 /* Go over all entries in the IN_ENCODER and duplicate them to
835 ENCODER. At the same time insert masters of clones so
836 every master appears before clone. */
837 for (lsei = lsei_start_function_in_partition (in_encoder);
838 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
840 struct cgraph_node *node = lsei_cgraph_node (lsei);
841 add_node_to (encoder, node, true);
842 lto_set_symtab_encoder_in_partition (encoder, node);
843 create_references (encoder, node);
844 /* For proper debug info, we need to ship the origins, too. */
845 if (DECL_ABSTRACT_ORIGIN (node->decl))
847 struct cgraph_node *origin_node
848 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node->decl));
849 add_node_to (encoder, origin_node, true);
852 for (lsei = lsei_start_variable_in_partition (in_encoder);
853 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
855 varpool_node *vnode = lsei_varpool_node (lsei);
857 lto_set_symtab_encoder_in_partition (encoder, vnode);
858 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
859 create_references (encoder, vnode);
860 /* For proper debug info, we need to ship the origins, too. */
861 if (DECL_ABSTRACT_ORIGIN (vnode->decl))
863 varpool_node *origin_node
864 = varpool_node::get (DECL_ABSTRACT_ORIGIN (vnode->decl));
865 lto_set_symtab_encoder_in_partition (encoder, origin_node);
868 /* Pickle in also the initializer of all referenced readonly variables
869 to help folding. Constant pool variables are not shared, so we must
870 pickle those too. */
871 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
873 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
874 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
876 if (!lto_symtab_encoder_encode_initializer_p (encoder,
877 vnode)
878 && vnode->ctor_useable_for_folding_p ())
880 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
881 create_references (encoder, vnode);
886 /* Go over all the nodes again to include callees that are not in
887 SET. */
888 for (lsei = lsei_start_function_in_partition (encoder);
889 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
891 struct cgraph_node *node = lsei_cgraph_node (lsei);
892 for (edge = node->callees; edge; edge = edge->next_callee)
894 struct cgraph_node *callee = edge->callee;
895 if (!lto_symtab_encoder_in_partition_p (encoder, callee))
897 /* We should have moved all the inlines. */
898 gcc_assert (!callee->global.inlined_to);
899 add_node_to (encoder, callee, false);
902 /* Add all possible targets for late devirtualization. */
903 if (flag_devirtualize)
904 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
905 if (edge->indirect_info->polymorphic)
907 unsigned int i;
908 void *cache_token;
909 bool final;
910 vec <cgraph_node *>targets
911 = possible_polymorphic_call_targets
912 (edge, &final, &cache_token);
913 if (!reachable_call_targets.add (cache_token))
915 for (i = 0; i < targets.length (); i++)
917 struct cgraph_node *callee = targets[i];
919 /* Adding an external declarations into the unit serves
920 no purpose and just increases its boundary. */
921 if (callee->definition
922 && !lto_symtab_encoder_in_partition_p
923 (encoder, callee))
925 gcc_assert (!callee->global.inlined_to);
926 add_node_to (encoder, callee, false);
932 lto_symtab_encoder_delete (in_encoder);
933 return encoder;
936 /* Output the part of the symtab in SET and VSET. */
938 void
939 output_symtab (void)
941 struct cgraph_node *node;
942 struct lto_simple_output_block *ob;
943 lto_symtab_encoder_iterator lsei;
944 int i, n_nodes;
945 lto_symtab_encoder_t encoder;
947 if (flag_wpa)
948 output_cgraph_opt_summary ();
950 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
952 output_profile_summary (ob);
954 /* An encoder for cgraph nodes should have been created by
955 ipa_write_summaries_1. */
956 gcc_assert (ob->decl_state->symtab_node_encoder);
957 encoder = ob->decl_state->symtab_node_encoder;
959 /* Write out the nodes. We must first output a node and then its clones,
960 otherwise at a time reading back the node there would be nothing to clone
961 from. */
962 n_nodes = lto_symtab_encoder_size (encoder);
963 for (i = 0; i < n_nodes; i++)
965 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
966 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
967 lto_output_node (ob, cnode, encoder);
968 else
969 lto_output_varpool_node (ob, dyn_cast<varpool_node *> (node), encoder);
972 /* Go over the nodes in SET again to write edges. */
973 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
974 lsei_next_function_in_partition (&lsei))
976 node = lsei_cgraph_node (lsei);
977 output_outgoing_cgraph_edges (node->callees, ob, encoder);
978 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
981 streamer_write_uhwi_stream (ob->main_stream, 0);
983 lto_destroy_simple_output_block (ob);
985 /* Emit toplevel asms.
986 When doing WPA we must output every asm just once. Since we do not partition asm
987 nodes at all, output them to first output. This is kind of hack, but should work
988 well. */
989 if (!asm_nodes_output)
991 asm_nodes_output = true;
992 lto_output_toplevel_asms ();
995 output_refs (encoder);
998 /* Return identifier encoded in IB as a plain string. */
1000 static tree
1001 read_identifier (struct lto_input_block *ib)
1003 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1004 tree id;
1006 if (ib->data[ib->p + len])
1007 lto_section_overrun (ib);
1008 if (!len)
1010 ib->p++;
1011 return NULL;
1013 id = get_identifier (ib->data + ib->p);
1014 ib->p += len + 1;
1015 return id;
1018 /* Return string encoded in IB, NULL if string is empty. */
1020 static const char *
1021 read_string (struct lto_input_block *ib)
1023 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1024 const char *str;
1026 if (ib->data[ib->p + len])
1027 lto_section_overrun (ib);
1028 if (!len)
1030 ib->p++;
1031 return NULL;
1033 str = ib->data + ib->p;
1034 ib->p += len + 1;
1035 return str;
1038 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
1039 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
1040 NODE or to replace the values in it, for instance because the first
1041 time we saw it, the function body was not available but now it
1042 is. BP is a bitpack with all the bitflags for NODE read from the
1043 stream. */
1045 static void
1046 input_overwrite_node (struct lto_file_decl_data *file_data,
1047 struct cgraph_node *node,
1048 enum LTO_symtab_tags tag,
1049 struct bitpack_d *bp)
1051 node->aux = (void *) tag;
1052 node->lto_file_data = file_data;
1054 node->local.local = bp_unpack_value (bp, 1);
1055 node->externally_visible = bp_unpack_value (bp, 1);
1056 node->no_reorder = bp_unpack_value (bp, 1);
1057 node->definition = bp_unpack_value (bp, 1);
1058 node->local.versionable = bp_unpack_value (bp, 1);
1059 node->local.can_change_signature = bp_unpack_value (bp, 1);
1060 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
1061 node->force_output = bp_unpack_value (bp, 1);
1062 node->forced_by_abi = bp_unpack_value (bp, 1);
1063 node->unique_name = bp_unpack_value (bp, 1);
1064 node->body_removed = bp_unpack_value (bp, 1);
1065 node->implicit_section = bp_unpack_value (bp, 1);
1066 node->address_taken = bp_unpack_value (bp, 1);
1067 node->used_from_other_partition = bp_unpack_value (bp, 1);
1068 node->lowered = bp_unpack_value (bp, 1);
1069 node->analyzed = tag == LTO_symtab_analyzed_node;
1070 node->in_other_partition = bp_unpack_value (bp, 1);
1071 if (node->in_other_partition
1072 /* Avoid updating decl when we are seeing just inline clone.
1073 When inlining function that has functions already inlined into it,
1074 we produce clones of inline clones.
1076 WPA partitioning might put each clone into different unit and
1077 we might end up streaming inline clone from other partition
1078 to support clone we are interested in. */
1079 && (!node->clone_of
1080 || node->clone_of->decl != node->decl))
1082 DECL_EXTERNAL (node->decl) = 1;
1083 TREE_STATIC (node->decl) = 0;
1085 node->alias = bp_unpack_value (bp, 1);
1086 node->weakref = bp_unpack_value (bp, 1);
1087 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
1088 node->only_called_at_startup = bp_unpack_value (bp, 1);
1089 node->only_called_at_exit = bp_unpack_value (bp, 1);
1090 node->tm_clone = bp_unpack_value (bp, 1);
1091 node->calls_comdat_local = bp_unpack_value (bp, 1);
1092 node->icf_merged = bp_unpack_value (bp, 1);
1093 node->thunk.thunk_p = bp_unpack_value (bp, 1);
1094 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
1095 LDPR_NUM_KNOWN);
1096 gcc_assert (flag_ltrans
1097 || (!node->in_other_partition
1098 && !node->used_from_other_partition));
1101 /* Return string alias is alias of. */
1103 static tree
1104 get_alias_symbol (tree decl)
1106 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1107 return get_identifier (TREE_STRING_POINTER
1108 (TREE_VALUE (TREE_VALUE (alias))));
1111 /* Read a node from input_block IB. TAG is the node's tag just read.
1112 Return the node read or overwriten. */
1114 static struct cgraph_node *
1115 input_node (struct lto_file_decl_data *file_data,
1116 struct lto_input_block *ib,
1117 enum LTO_symtab_tags tag,
1118 vec<symtab_node *> nodes)
1120 gcc::pass_manager *passes = g->get_passes ();
1121 tree fn_decl;
1122 struct cgraph_node *node;
1123 struct bitpack_d bp;
1124 unsigned decl_index;
1125 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1126 int clone_ref;
1127 int order;
1128 int i, count;
1129 tree group;
1130 const char *section;
1131 order = streamer_read_hwi (ib) + order_base;
1132 clone_ref = streamer_read_hwi (ib);
1134 decl_index = streamer_read_uhwi (ib);
1135 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1137 if (clone_ref != LCC_NOT_FOUND)
1139 node = dyn_cast<cgraph_node *> (nodes[clone_ref])->create_clone (fn_decl,
1140 0, CGRAPH_FREQ_BASE, false,
1141 vNULL, false, NULL, NULL);
1143 else
1145 /* Declaration of functions can be already merged with a declaration
1146 from other input file. We keep cgraph unmerged until after streaming
1147 of ipa passes is done. Alays forcingly create a fresh node. */
1148 node = symtab->create_empty ();
1149 node->decl = fn_decl;
1150 node->register_symbol ();
1153 node->order = order;
1154 if (order >= symtab->order)
1155 symtab->order = order + 1;
1157 node->count = streamer_read_gcov_count (ib);
1158 node->count_materialization_scale = streamer_read_hwi (ib);
1160 count = streamer_read_hwi (ib);
1161 node->ipa_transforms_to_apply = vNULL;
1162 for (i = 0; i < count; i++)
1164 opt_pass *pass;
1165 int pid = streamer_read_hwi (ib);
1167 gcc_assert (pid < passes->passes_by_id_size);
1168 pass = passes->passes_by_id[pid];
1169 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *) pass);
1172 if (tag == LTO_symtab_analyzed_node)
1173 ref = streamer_read_hwi (ib);
1175 group = read_identifier (ib);
1176 if (group)
1177 ref2 = streamer_read_hwi (ib);
1179 /* Make sure that we have not read this node before. Nodes that
1180 have already been read will have their tag stored in the 'aux'
1181 field. Since built-in functions can be referenced in multiple
1182 functions, they are expected to be read more than once. */
1183 if (node->aux && !DECL_BUILT_IN (node->decl))
1184 internal_error ("bytecode stream: found multiple instances of cgraph "
1185 "node with uid %d", node->uid);
1187 node->tp_first_run = streamer_read_uhwi (ib);
1189 bp = streamer_read_bitpack (ib);
1191 input_overwrite_node (file_data, node, tag, &bp);
1193 /* Store a reference for now, and fix up later to be a pointer. */
1194 node->global.inlined_to = (cgraph_node *) (intptr_t) ref;
1196 if (group)
1198 node->set_comdat_group (group);
1199 /* Store a reference for now, and fix up later to be a pointer. */
1200 node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
1202 else
1203 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1204 section = read_string (ib);
1205 if (section)
1206 node->set_section_for_node (section);
1208 if (node->thunk.thunk_p)
1210 int type = streamer_read_uhwi (ib);
1211 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1212 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1214 node->thunk.fixed_offset = fixed_offset;
1215 node->thunk.this_adjusting = (type & 2);
1216 node->thunk.virtual_value = virtual_value;
1217 node->thunk.virtual_offset_p = (type & 4);
1219 if (node->alias && !node->analyzed && node->weakref)
1220 node->alias_target = get_alias_symbol (node->decl);
1221 node->profile_id = streamer_read_hwi (ib);
1222 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1223 node->set_init_priority (streamer_read_hwi (ib));
1224 if (DECL_STATIC_DESTRUCTOR (node->decl))
1225 node->set_fini_priority (streamer_read_hwi (ib));
1226 return node;
1229 /* Read a node from input_block IB. TAG is the node's tag just read.
1230 Return the node read or overwriten. */
1232 static varpool_node *
1233 input_varpool_node (struct lto_file_decl_data *file_data,
1234 struct lto_input_block *ib)
1236 int decl_index;
1237 tree var_decl;
1238 varpool_node *node;
1239 struct bitpack_d bp;
1240 int ref = LCC_NOT_FOUND;
1241 int order;
1242 tree group;
1243 const char *section;
1245 order = streamer_read_hwi (ib) + order_base;
1246 decl_index = streamer_read_uhwi (ib);
1247 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1249 /* Declaration of functions can be already merged with a declaration
1250 from other input file. We keep cgraph unmerged until after streaming
1251 of ipa passes is done. Alays forcingly create a fresh node. */
1252 node = varpool_node::create_empty ();
1253 node->decl = var_decl;
1254 node->register_symbol ();
1256 node->order = order;
1257 if (order >= symtab->order)
1258 symtab->order = order + 1;
1259 node->lto_file_data = file_data;
1261 bp = streamer_read_bitpack (ib);
1262 node->externally_visible = bp_unpack_value (&bp, 1);
1263 node->no_reorder = bp_unpack_value (&bp, 1);
1264 node->force_output = bp_unpack_value (&bp, 1);
1265 node->forced_by_abi = bp_unpack_value (&bp, 1);
1266 node->unique_name = bp_unpack_value (&bp, 1);
1267 node->body_removed = bp_unpack_value (&bp, 1);
1268 node->implicit_section = bp_unpack_value (&bp, 1);
1269 node->writeonly = bp_unpack_value (&bp, 1);
1270 node->definition = bp_unpack_value (&bp, 1);
1271 node->alias = bp_unpack_value (&bp, 1);
1272 node->weakref = bp_unpack_value (&bp, 1);
1273 node->analyzed = bp_unpack_value (&bp, 1);
1274 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1275 node->in_other_partition = bp_unpack_value (&bp, 1);
1276 if (node->in_other_partition)
1278 DECL_EXTERNAL (node->decl) = 1;
1279 TREE_STATIC (node->decl) = 0;
1281 if (node->alias && !node->analyzed && node->weakref)
1282 node->alias_target = get_alias_symbol (node->decl);
1283 node->tls_model = (enum tls_model)bp_unpack_value (&bp, 3);
1284 node->used_by_single_function = (enum tls_model)bp_unpack_value (&bp, 1);
1285 group = read_identifier (ib);
1286 if (group)
1288 node->set_comdat_group (group);
1289 ref = streamer_read_hwi (ib);
1290 /* Store a reference for now, and fix up later to be a pointer. */
1291 node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1293 else
1294 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1295 section = read_string (ib);
1296 if (section)
1297 node->set_section_for_node (section);
1298 node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1299 LDPR_NUM_KNOWN);
1300 gcc_assert (flag_ltrans
1301 || (!node->in_other_partition
1302 && !node->used_from_other_partition));
1304 return node;
1307 /* Read a node from input_block IB. TAG is the node's tag just read.
1308 Return the node read or overwriten. */
1310 static void
1311 input_ref (struct lto_input_block *ib,
1312 symtab_node *referring_node,
1313 vec<symtab_node *> nodes)
1315 symtab_node *node = NULL;
1316 struct bitpack_d bp;
1317 enum ipa_ref_use use;
1318 bool speculative;
1319 struct ipa_ref *ref;
1321 bp = streamer_read_bitpack (ib);
1322 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1323 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1324 node = nodes[streamer_read_hwi (ib)];
1325 ref = referring_node->create_reference (node, use);
1326 ref->speculative = speculative;
1327 if (is_a <cgraph_node *> (referring_node))
1328 ref->lto_stmt_uid = streamer_read_hwi (ib);
1331 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1332 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1333 edge being read is indirect (in the sense that it has
1334 indirect_unknown_callee set). */
1336 static void
1337 input_edge (struct lto_input_block *ib, vec<symtab_node *> nodes,
1338 bool indirect)
1340 struct cgraph_node *caller, *callee;
1341 struct cgraph_edge *edge;
1342 unsigned int stmt_id;
1343 gcov_type count;
1344 int freq;
1345 cgraph_inline_failed_t inline_failed;
1346 struct bitpack_d bp;
1347 int ecf_flags = 0;
1349 caller = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1350 if (caller == NULL || caller->decl == NULL_TREE)
1351 internal_error ("bytecode stream: no caller found while reading edge");
1353 if (!indirect)
1355 callee = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1356 if (callee == NULL || callee->decl == NULL_TREE)
1357 internal_error ("bytecode stream: no callee found while reading edge");
1359 else
1360 callee = NULL;
1362 count = streamer_read_gcov_count (ib);
1364 bp = streamer_read_bitpack (ib);
1365 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_t, CIF_N_REASONS);
1366 stmt_id = bp_unpack_var_len_unsigned (&bp);
1367 freq = (int) bp_unpack_var_len_unsigned (&bp);
1369 if (indirect)
1370 edge = caller->create_indirect_edge (NULL, 0, count, freq);
1371 else
1372 edge = caller->create_edge (callee, NULL, count, freq);
1374 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1375 edge->speculative = bp_unpack_value (&bp, 1);
1376 edge->lto_stmt_uid = stmt_id;
1377 edge->inline_failed = inline_failed;
1378 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1379 edge->can_throw_external = bp_unpack_value (&bp, 1);
1380 edge->in_polymorphic_cdtor = bp_unpack_value (&bp, 1);
1381 if (indirect)
1383 if (bp_unpack_value (&bp, 1))
1384 ecf_flags |= ECF_CONST;
1385 if (bp_unpack_value (&bp, 1))
1386 ecf_flags |= ECF_PURE;
1387 if (bp_unpack_value (&bp, 1))
1388 ecf_flags |= ECF_NORETURN;
1389 if (bp_unpack_value (&bp, 1))
1390 ecf_flags |= ECF_MALLOC;
1391 if (bp_unpack_value (&bp, 1))
1392 ecf_flags |= ECF_NOTHROW;
1393 if (bp_unpack_value (&bp, 1))
1394 ecf_flags |= ECF_RETURNS_TWICE;
1395 edge->indirect_info->ecf_flags = ecf_flags;
1396 edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1397 if (edge->indirect_info->common_target_id)
1398 edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1403 /* Read a cgraph from IB using the info in FILE_DATA. */
1405 static vec<symtab_node *>
1406 input_cgraph_1 (struct lto_file_decl_data *file_data,
1407 struct lto_input_block *ib)
1409 enum LTO_symtab_tags tag;
1410 vec<symtab_node *> nodes = vNULL;
1411 symtab_node *node;
1412 unsigned i;
1414 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1415 order_base = symtab->order;
1416 while (tag)
1418 if (tag == LTO_symtab_edge)
1419 input_edge (ib, nodes, false);
1420 else if (tag == LTO_symtab_indirect_edge)
1421 input_edge (ib, nodes, true);
1422 else if (tag == LTO_symtab_variable)
1424 node = input_varpool_node (file_data, ib);
1425 nodes.safe_push (node);
1426 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1428 else
1430 node = input_node (file_data, ib, tag, nodes);
1431 if (node == NULL || node->decl == NULL_TREE)
1432 internal_error ("bytecode stream: found empty cgraph node");
1433 nodes.safe_push (node);
1434 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1437 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1440 lto_input_toplevel_asms (file_data, order_base);
1442 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1443 #ifdef ENABLE_CHECKING
1444 FOR_EACH_VEC_ELT (nodes, i, node)
1445 gcc_assert (node->aux || !is_a <cgraph_node *> (node));
1446 #endif
1447 FOR_EACH_VEC_ELT (nodes, i, node)
1449 int ref;
1450 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1452 ref = (int) (intptr_t) cnode->global.inlined_to;
1454 /* We share declaration of builtins, so we may read same node twice. */
1455 if (!node->aux)
1456 continue;
1457 node->aux = NULL;
1459 /* Fixup inlined_to from reference to pointer. */
1460 if (ref != LCC_NOT_FOUND)
1461 dyn_cast<cgraph_node *> (node)->global.inlined_to
1462 = dyn_cast<cgraph_node *> (nodes[ref]);
1463 else
1464 cnode->global.inlined_to = NULL;
1467 ref = (int) (intptr_t) node->same_comdat_group;
1469 /* Fixup same_comdat_group from reference to pointer. */
1470 if (ref != LCC_NOT_FOUND)
1471 node->same_comdat_group = nodes[ref];
1472 else
1473 node->same_comdat_group = NULL;
1475 FOR_EACH_VEC_ELT (nodes, i, node)
1476 node->aux = is_a <cgraph_node *> (node) ? (void *)1 : NULL;
1477 return nodes;
1480 /* Input ipa_refs. */
1482 static void
1483 input_refs (struct lto_input_block *ib,
1484 vec<symtab_node *> nodes)
1486 int count;
1487 int idx;
1488 while (true)
1490 symtab_node *node;
1491 count = streamer_read_uhwi (ib);
1492 if (!count)
1493 break;
1494 idx = streamer_read_uhwi (ib);
1495 node = nodes[idx];
1496 while (count)
1498 input_ref (ib, node, nodes);
1499 count--;
1505 static struct gcov_ctr_summary lto_gcov_summary;
1507 /* Input profile_info from IB. */
1508 static void
1509 input_profile_summary (struct lto_input_block *ib,
1510 struct lto_file_decl_data *file_data)
1512 unsigned h_ix;
1513 struct bitpack_d bp;
1514 unsigned int runs = streamer_read_uhwi (ib);
1515 if (runs)
1517 file_data->profile_info.runs = runs;
1518 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1519 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1521 memset (file_data->profile_info.histogram, 0,
1522 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1523 /* Input the bitpack of non-zero histogram indices. */
1524 bp = streamer_read_bitpack (ib);
1525 /* Read in and unpack the full bitpack, flagging non-zero
1526 histogram entries by setting the num_counters non-zero. */
1527 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1529 file_data->profile_info.histogram[h_ix].num_counters
1530 = bp_unpack_value (&bp, 1);
1532 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1534 if (!file_data->profile_info.histogram[h_ix].num_counters)
1535 continue;
1537 file_data->profile_info.histogram[h_ix].num_counters
1538 = streamer_read_gcov_count (ib);
1539 file_data->profile_info.histogram[h_ix].min_value
1540 = streamer_read_gcov_count (ib);
1541 file_data->profile_info.histogram[h_ix].cum_value
1542 = streamer_read_gcov_count (ib);
1544 /* IPA-profile computes hot bb threshold based on cumulated
1545 whole program profile. We need to stream it down to ltrans. */
1546 if (flag_ltrans)
1547 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1552 /* Rescale profile summaries to the same number of runs in the whole unit. */
1554 static void
1555 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1557 struct lto_file_decl_data *file_data;
1558 unsigned int j, h_ix;
1559 gcov_unsigned_t max_runs = 0;
1560 struct cgraph_node *node;
1561 struct cgraph_edge *edge;
1562 gcov_type saved_sum_all = 0;
1563 gcov_ctr_summary *saved_profile_info = 0;
1564 int saved_scale = 0;
1566 /* Find unit with maximal number of runs. If we ever get serious about
1567 roundoff errors, we might also consider computing smallest common
1568 multiply. */
1569 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1570 if (max_runs < file_data->profile_info.runs)
1571 max_runs = file_data->profile_info.runs;
1573 if (!max_runs)
1574 return;
1576 /* Simple overflow check. We probably don't need to support that many train
1577 runs. Such a large value probably imply data corruption anyway. */
1578 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1580 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1581 INT_MAX / REG_BR_PROB_BASE);
1582 return;
1585 profile_info = &lto_gcov_summary;
1586 lto_gcov_summary.runs = max_runs;
1587 lto_gcov_summary.sum_max = 0;
1588 memset (lto_gcov_summary.histogram, 0,
1589 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1591 /* Rescale all units to the maximal number of runs.
1592 sum_max can not be easily merged, as we have no idea what files come from
1593 the same run. We do not use the info anyway, so leave it 0. */
1594 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1595 if (file_data->profile_info.runs)
1597 int scale = GCOV_COMPUTE_SCALE (max_runs,
1598 file_data->profile_info.runs);
1599 lto_gcov_summary.sum_max
1600 = MAX (lto_gcov_summary.sum_max,
1601 apply_scale (file_data->profile_info.sum_max, scale));
1602 lto_gcov_summary.sum_all
1603 = MAX (lto_gcov_summary.sum_all,
1604 apply_scale (file_data->profile_info.sum_all, scale));
1605 /* Save a pointer to the profile_info with the largest
1606 scaled sum_all and the scale for use in merging the
1607 histogram. */
1608 if (!saved_profile_info
1609 || lto_gcov_summary.sum_all > saved_sum_all)
1611 saved_profile_info = &file_data->profile_info;
1612 saved_sum_all = lto_gcov_summary.sum_all;
1613 saved_scale = scale;
1617 gcc_assert (saved_profile_info);
1619 /* Scale up the histogram from the profile that had the largest
1620 scaled sum_all above. */
1621 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1623 /* Scale up the min value as we did the corresponding sum_all
1624 above. Use that to find the new histogram index. */
1625 gcov_type scaled_min
1626 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1627 saved_scale);
1628 /* The new index may be shared with another scaled histogram entry,
1629 so we need to account for a non-zero histogram entry at new_ix. */
1630 unsigned new_ix = gcov_histo_index (scaled_min);
1631 lto_gcov_summary.histogram[new_ix].min_value
1632 = (lto_gcov_summary.histogram[new_ix].num_counters
1633 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1634 : scaled_min);
1635 /* Some of the scaled counter values would ostensibly need to be placed
1636 into different (larger) histogram buckets, but we keep things simple
1637 here and place the scaled cumulative counter value in the bucket
1638 corresponding to the scaled minimum counter value. */
1639 lto_gcov_summary.histogram[new_ix].cum_value
1640 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1641 saved_scale);
1642 lto_gcov_summary.histogram[new_ix].num_counters
1643 += saved_profile_info->histogram[h_ix].num_counters;
1646 /* Watch roundoff errors. */
1647 if (lto_gcov_summary.sum_max < max_runs)
1648 lto_gcov_summary.sum_max = max_runs;
1650 /* If merging already happent at WPA time, we are done. */
1651 if (flag_ltrans)
1652 return;
1654 /* Now compute count_materialization_scale of each node.
1655 During LTRANS we already have values of count_materialization_scale
1656 computed, so just update them. */
1657 FOR_EACH_FUNCTION (node)
1658 if (node->lto_file_data
1659 && node->lto_file_data->profile_info.runs)
1661 int scale;
1663 scale = RDIV (node->count_materialization_scale * max_runs,
1664 node->lto_file_data->profile_info.runs);
1665 node->count_materialization_scale = scale;
1666 if (scale < 0)
1667 fatal_error ("Profile information in %s corrupted",
1668 file_data->file_name);
1670 if (scale == REG_BR_PROB_BASE)
1671 continue;
1672 for (edge = node->callees; edge; edge = edge->next_callee)
1673 edge->count = apply_scale (edge->count, scale);
1674 node->count = apply_scale (node->count, scale);
1678 /* Input and merge the symtab from each of the .o files passed to
1679 lto1. */
1681 void
1682 input_symtab (void)
1684 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1685 struct lto_file_decl_data *file_data;
1686 unsigned int j = 0;
1687 struct cgraph_node *node;
1689 while ((file_data = file_data_vec[j++]))
1691 const char *data;
1692 size_t len;
1693 struct lto_input_block *ib;
1694 vec<symtab_node *> nodes;
1696 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1697 &data, &len);
1698 if (!ib)
1699 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1700 input_profile_summary (ib, file_data);
1701 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1702 nodes = input_cgraph_1 (file_data, ib);
1703 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1704 ib, data, len);
1706 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1707 &data, &len);
1708 if (!ib)
1709 fatal_error ("cannot find LTO section refs in %s",
1710 file_data->file_name);
1711 input_refs (ib, nodes);
1712 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1713 ib, data, len);
1714 if (flag_ltrans)
1715 input_cgraph_opt_summary (nodes);
1716 nodes.release ();
1719 merge_profile_summaries (file_data_vec);
1720 get_working_sets ();
1723 /* Clear out the aux field that was used to store enough state to
1724 tell which nodes should be overwritten. */
1725 FOR_EACH_FUNCTION (node)
1727 /* Some nodes may have been created by cgraph_node. This
1728 happens when the callgraph contains nested functions. If the
1729 node for the parent function was never emitted to the gimple
1730 file, cgraph_node will create a node for it when setting the
1731 context of the nested function. */
1732 if (node->lto_file_data)
1733 node->aux = NULL;
1737 /* True when we need optimization summary for NODE. */
1739 static int
1740 output_cgraph_opt_summary_p (struct cgraph_node *node)
1742 return (node->clone_of
1743 && (node->clone.tree_map
1744 || node->clone.args_to_skip
1745 || node->clone.combined_args_to_skip));
1748 /* Output optimization summary for EDGE to OB. */
1749 static void
1750 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1751 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1755 /* Output optimization summary for NODE to OB. */
1757 static void
1758 output_node_opt_summary (struct output_block *ob,
1759 struct cgraph_node *node,
1760 lto_symtab_encoder_t encoder)
1762 unsigned int index;
1763 bitmap_iterator bi;
1764 struct ipa_replace_map *map;
1765 struct bitpack_d bp;
1766 int i;
1767 struct cgraph_edge *e;
1769 if (node->clone.args_to_skip)
1771 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1772 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1773 streamer_write_uhwi (ob, index);
1775 else
1776 streamer_write_uhwi (ob, 0);
1777 if (node->clone.combined_args_to_skip)
1779 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1780 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1781 streamer_write_uhwi (ob, index);
1783 else
1784 streamer_write_uhwi (ob, 0);
1785 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1786 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1788 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1789 mechanism to store function local declarations into summaries. */
1790 gcc_assert (!map->old_tree);
1791 streamer_write_uhwi (ob, map->parm_num);
1792 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1793 stream_write_tree (ob, map->new_tree, true);
1794 bp = bitpack_create (ob->main_stream);
1795 bp_pack_value (&bp, map->replace_p, 1);
1796 bp_pack_value (&bp, map->ref_p, 1);
1797 streamer_write_bitpack (&bp);
1800 if (lto_symtab_encoder_in_partition_p (encoder, node))
1802 for (e = node->callees; e; e = e->next_callee)
1803 output_edge_opt_summary (ob, e);
1804 for (e = node->indirect_calls; e; e = e->next_callee)
1805 output_edge_opt_summary (ob, e);
1809 /* Output optimization summaries stored in callgraph.
1810 At the moment it is the clone info structure. */
1812 static void
1813 output_cgraph_opt_summary (void)
1815 int i, n_nodes;
1816 lto_symtab_encoder_t encoder;
1817 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1818 unsigned count = 0;
1820 ob->symbol = NULL;
1821 encoder = ob->decl_state->symtab_node_encoder;
1822 n_nodes = lto_symtab_encoder_size (encoder);
1823 for (i = 0; i < n_nodes; i++)
1825 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1826 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1827 if (cnode && output_cgraph_opt_summary_p (cnode))
1828 count++;
1830 streamer_write_uhwi (ob, count);
1831 for (i = 0; i < n_nodes; i++)
1833 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1834 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1835 if (cnode && output_cgraph_opt_summary_p (cnode))
1837 streamer_write_uhwi (ob, i);
1838 output_node_opt_summary (ob, cnode, encoder);
1841 produce_asm (ob, NULL);
1842 destroy_output_block (ob);
1845 /* Input optimisation summary of EDGE. */
1847 static void
1848 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
1849 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
1853 /* Input optimisation summary of NODE. */
1855 static void
1856 input_node_opt_summary (struct cgraph_node *node,
1857 struct lto_input_block *ib_main,
1858 struct data_in *data_in)
1860 int i;
1861 int count;
1862 int bit;
1863 struct bitpack_d bp;
1864 struct cgraph_edge *e;
1866 count = streamer_read_uhwi (ib_main);
1867 if (count)
1868 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1869 for (i = 0; i < count; i++)
1871 bit = streamer_read_uhwi (ib_main);
1872 bitmap_set_bit (node->clone.args_to_skip, bit);
1874 count = streamer_read_uhwi (ib_main);
1875 if (count)
1876 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1877 for (i = 0; i < count; i++)
1879 bit = streamer_read_uhwi (ib_main);
1880 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1882 count = streamer_read_uhwi (ib_main);
1883 for (i = 0; i < count; i++)
1885 struct ipa_replace_map *map = ggc_alloc<ipa_replace_map> ();
1887 vec_safe_push (node->clone.tree_map, map);
1888 map->parm_num = streamer_read_uhwi (ib_main);
1889 map->old_tree = NULL;
1890 map->new_tree = stream_read_tree (ib_main, data_in);
1891 bp = streamer_read_bitpack (ib_main);
1892 map->replace_p = bp_unpack_value (&bp, 1);
1893 map->ref_p = bp_unpack_value (&bp, 1);
1895 for (e = node->callees; e; e = e->next_callee)
1896 input_edge_opt_summary (e, ib_main);
1897 for (e = node->indirect_calls; e; e = e->next_callee)
1898 input_edge_opt_summary (e, ib_main);
1901 /* Read section in file FILE_DATA of length LEN with data DATA. */
1903 static void
1904 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1905 const char *data, size_t len,
1906 vec<symtab_node *> nodes)
1908 const struct lto_function_header *header =
1909 (const struct lto_function_header *) data;
1910 const int cfg_offset = sizeof (struct lto_function_header);
1911 const int main_offset = cfg_offset + header->cfg_size;
1912 const int string_offset = main_offset + header->main_size;
1913 struct data_in *data_in;
1914 unsigned int i;
1915 unsigned int count;
1917 lto_input_block ib_main ((const char *) data + main_offset,
1918 header->main_size);
1920 data_in =
1921 lto_data_in_create (file_data, (const char *) data + string_offset,
1922 header->string_size, vNULL);
1923 count = streamer_read_uhwi (&ib_main);
1925 for (i = 0; i < count; i++)
1927 int ref = streamer_read_uhwi (&ib_main);
1928 input_node_opt_summary (dyn_cast<cgraph_node *> (nodes[ref]),
1929 &ib_main, data_in);
1931 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1932 len);
1933 lto_data_in_delete (data_in);
1936 /* Input optimization summary of cgraph. */
1938 static void
1939 input_cgraph_opt_summary (vec<symtab_node *> nodes)
1941 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1942 struct lto_file_decl_data *file_data;
1943 unsigned int j = 0;
1945 while ((file_data = file_data_vec[j++]))
1947 size_t len;
1948 const char *data =
1949 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1950 &len);
1952 if (data)
1953 input_cgraph_opt_section (file_data, data, len, nodes);