Introduce gimple_omp_continue
[official-gcc.git] / gcc / lto-cgraph.c
bloba48e61eb89339e8e28837d7a79c32ee1803a05c7
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 "hash-set.h"
41 #include "langhooks.h"
42 #include "bitmap.h"
43 #include "function.h"
44 #include "diagnostic-core.h"
45 #include "except.h"
46 #include "timevar.h"
47 #include "lto-streamer.h"
48 #include "data-streamer.h"
49 #include "tree-streamer.h"
50 #include "gcov-io.h"
51 #include "tree-pass.h"
52 #include "profile.h"
53 #include "context.h"
54 #include "pass_manager.h"
55 #include "ipa-utils.h"
57 /* True when asm nodes has been output. */
58 bool asm_nodes_output = false;
60 static void output_cgraph_opt_summary (void);
61 static void input_cgraph_opt_summary (vec<symtab_node *> nodes);
63 /* Number of LDPR values known to GCC. */
64 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
66 /* All node orders are ofsetted by ORDER_BASE. */
67 static int order_base;
69 /* Cgraph streaming is organized as set of record whose type
70 is indicated by a tag. */
71 enum LTO_symtab_tags
73 /* Must leave 0 for the stopper. */
75 /* Cgraph node without body available. */
76 LTO_symtab_unavail_node = 1,
77 /* Cgraph node with function body. */
78 LTO_symtab_analyzed_node,
79 /* Cgraph edges. */
80 LTO_symtab_edge,
81 LTO_symtab_indirect_edge,
82 LTO_symtab_variable,
83 LTO_symtab_last_tag
86 /* Create a new symtab encoder.
87 if FOR_INPUT, the encoder allocate only datastructures needed
88 to read the symtab. */
90 lto_symtab_encoder_t
91 lto_symtab_encoder_new (bool for_input)
93 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
95 if (!for_input)
96 encoder->map = new hash_map<symtab_node *, size_t>;
97 encoder->nodes.create (0);
98 return encoder;
102 /* Delete ENCODER and its components. */
104 void
105 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
107 encoder->nodes.release ();
108 if (encoder->map)
109 delete encoder->map;
110 free (encoder);
114 /* Return the existing reference number of NODE in the symtab encoder in
115 output block OB. Assign a new reference if this is the first time
116 NODE is encoded. */
119 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
120 symtab_node *node)
122 int ref;
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 size_t *slot = encoder->map->get (node);
134 if (!slot || !*slot)
136 lto_encoder_entry entry = {node, false, false, false};
137 ref = encoder->nodes.length ();
138 if (!slot)
139 encoder->map->put (node, ref + 1);
140 encoder->nodes.safe_push (entry);
142 else
143 ref = *slot - 1;
145 return ref;
148 /* Remove NODE from encoder. */
150 bool
151 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
152 symtab_node *node)
154 int index;
155 lto_encoder_entry last_node;
157 size_t *slot = encoder->map->get (node);
158 if (slot == NULL || !*slot)
159 return false;
161 index = *slot - 1;
162 gcc_checking_assert (encoder->nodes[index].node == node);
164 /* Remove from vector. We do this by swapping node with the last element
165 of the vector. */
166 last_node = encoder->nodes.pop ();
167 if (last_node.node != node)
169 gcc_assert (encoder->map->put (last_node.node, index + 1));
171 /* Move the last element to the original spot of NODE. */
172 encoder->nodes[index] = last_node;
175 /* Remove element from hash table. */
176 encoder->map->remove (node);
177 return true;
181 /* Return TRUE if we should encode initializer of NODE (if any). */
183 bool
184 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
185 struct cgraph_node *node)
187 int index = lto_symtab_encoder_lookup (encoder, node);
188 return encoder->nodes[index].body;
191 /* Return TRUE if we should encode body of NODE (if any). */
193 static void
194 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
195 struct cgraph_node *node)
197 int index = lto_symtab_encoder_encode (encoder, node);
198 gcc_checking_assert (encoder->nodes[index].node == node);
199 encoder->nodes[index].body = true;
202 /* Return TRUE if we should encode initializer of NODE (if any). */
204 bool
205 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
206 varpool_node *node)
208 int index = lto_symtab_encoder_lookup (encoder, node);
209 if (index == LCC_NOT_FOUND)
210 return false;
211 return encoder->nodes[index].initializer;
214 /* Return TRUE if we should encode initializer of NODE (if any). */
216 static void
217 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
218 varpool_node *node)
220 int index = lto_symtab_encoder_lookup (encoder, node);
221 encoder->nodes[index].initializer = true;
224 /* Return TRUE if we should encode initializer of NODE (if any). */
226 bool
227 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
228 symtab_node *node)
230 int index = lto_symtab_encoder_lookup (encoder, node);
231 if (index == LCC_NOT_FOUND)
232 return false;
233 return encoder->nodes[index].in_partition;
236 /* Return TRUE if we should encode body of NODE (if any). */
238 void
239 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
240 symtab_node *node)
242 int index = lto_symtab_encoder_encode (encoder, node);
243 encoder->nodes[index].in_partition = true;
246 /* Output the cgraph EDGE to OB using ENCODER. */
248 static void
249 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
250 lto_symtab_encoder_t encoder)
252 unsigned int uid;
253 intptr_t ref;
254 struct bitpack_d bp;
256 if (edge->indirect_unknown_callee)
257 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
258 LTO_symtab_indirect_edge);
259 else
260 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
261 LTO_symtab_edge);
263 ref = lto_symtab_encoder_lookup (encoder, edge->caller);
264 gcc_assert (ref != LCC_NOT_FOUND);
265 streamer_write_hwi_stream (ob->main_stream, ref);
267 if (!edge->indirect_unknown_callee)
269 ref = lto_symtab_encoder_lookup (encoder, edge->callee);
270 gcc_assert (ref != LCC_NOT_FOUND);
271 streamer_write_hwi_stream (ob->main_stream, ref);
274 streamer_write_gcov_count_stream (ob->main_stream, edge->count);
276 bp = bitpack_create (ob->main_stream);
277 uid = (!gimple_has_body_p (edge->caller->decl)
278 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt) + 1);
279 bp_pack_enum (&bp, cgraph_inline_failed_t,
280 CIF_N_REASONS, edge->inline_failed);
281 bp_pack_var_len_unsigned (&bp, uid);
282 bp_pack_var_len_unsigned (&bp, edge->frequency);
283 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
284 bp_pack_value (&bp, edge->speculative, 1);
285 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
286 bp_pack_value (&bp, edge->can_throw_external, 1);
287 bp_pack_value (&bp, edge->in_polymorphic_cdtor, 1);
288 if (edge->indirect_unknown_callee)
290 int flags = edge->indirect_info->ecf_flags;
291 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
292 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
293 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
294 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
295 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
296 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
297 /* Flags that should not appear on indirect calls. */
298 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
299 | ECF_MAY_BE_ALLOCA
300 | ECF_SIBCALL
301 | ECF_LEAF
302 | ECF_NOVOPS)));
304 streamer_write_bitpack (&bp);
305 if (edge->indirect_unknown_callee)
307 streamer_write_hwi_stream (ob->main_stream,
308 edge->indirect_info->common_target_id);
309 if (edge->indirect_info->common_target_id)
310 streamer_write_hwi_stream
311 (ob->main_stream, edge->indirect_info->common_target_probability);
315 /* Return if NODE contain references from other partitions. */
317 bool
318 referenced_from_other_partition_p (symtab_node *node, lto_symtab_encoder_t encoder)
320 int i;
321 struct ipa_ref *ref = NULL;
323 for (i = 0; node->iterate_referring (i, ref); i++)
325 if (ref->referring->in_other_partition
326 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
327 return true;
329 return false;
332 /* Return true when node is reachable from other partition. */
334 bool
335 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
337 struct cgraph_edge *e;
338 if (!node->definition)
339 return false;
340 if (node->global.inlined_to)
341 return false;
342 for (e = node->callers; e; e = e->next_caller)
343 if (e->caller->in_other_partition
344 || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
345 return true;
346 return false;
349 /* Return if NODE contain references from other partitions. */
351 bool
352 referenced_from_this_partition_p (symtab_node *node,
353 lto_symtab_encoder_t encoder)
355 int i;
356 struct ipa_ref *ref = NULL;
358 for (i = 0; node->iterate_referring (i, ref); i++)
359 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
360 return true;
361 return false;
364 /* Return true when node is reachable from other partition. */
366 bool
367 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
369 struct cgraph_edge *e;
370 for (e = node->callers; e; e = e->next_caller)
371 if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
372 return true;
373 return false;
376 /* Output the cgraph NODE to OB. ENCODER is used to find the
377 reference number of NODE->inlined_to. SET is the set of nodes we
378 are writing to the current file. If NODE is not in SET, then NODE
379 is a boundary of a cgraph_node_set and we pretend NODE just has a
380 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
381 that have had their callgraph node written so far. This is used to
382 determine if NODE is a clone of a previously written node. */
384 static void
385 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
386 lto_symtab_encoder_t encoder)
388 unsigned int tag;
389 struct bitpack_d bp;
390 bool boundary_p;
391 intptr_t ref;
392 bool in_other_partition = false;
393 struct cgraph_node *clone_of, *ultimate_clone_of;
394 ipa_opt_pass_d *pass;
395 int i;
396 bool alias_p;
397 const char *comdat;
398 const char *section;
399 tree group;
401 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
403 if (node->analyzed && !boundary_p)
404 tag = LTO_symtab_analyzed_node;
405 else
406 tag = LTO_symtab_unavail_node;
408 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
409 tag);
410 streamer_write_hwi_stream (ob->main_stream, node->order);
412 /* In WPA mode, we only output part of the call-graph. Also, we
413 fake cgraph node attributes. There are two cases that we care.
415 Boundary nodes: There are nodes that are not part of SET but are
416 called from within SET. We artificially make them look like
417 externally visible nodes with no function body.
419 Cherry-picked nodes: These are nodes we pulled from other
420 translation units into SET during IPA-inlining. We make them as
421 local static nodes to prevent clashes with other local statics. */
422 if (boundary_p && node->analyzed
423 && node->get_partitioning_class () == SYMBOL_PARTITION)
425 /* Inline clones can not be part of boundary.
426 gcc_assert (!node->global.inlined_to);
428 FIXME: At the moment they can be, when partition contains an inline
429 clone that is clone of inline clone from outside partition. We can
430 reshape the clone tree and make other tree to be the root, but it
431 needs a bit extra work and will be promplty done by cgraph_remove_node
432 after reading back. */
433 in_other_partition = 1;
436 clone_of = node->clone_of;
437 while (clone_of
438 && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
439 if (clone_of->prev_sibling_clone)
440 clone_of = clone_of->prev_sibling_clone;
441 else
442 clone_of = clone_of->clone_of;
444 /* See if body of the master function is output. If not, we are seeing only
445 an declaration and we do not need to pass down clone tree. */
446 ultimate_clone_of = clone_of;
447 while (ultimate_clone_of && ultimate_clone_of->clone_of)
448 ultimate_clone_of = ultimate_clone_of->clone_of;
450 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
451 clone_of = NULL;
453 if (tag == LTO_symtab_analyzed_node)
454 gcc_assert (clone_of || !node->clone_of);
455 if (!clone_of)
456 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
457 else
458 streamer_write_hwi_stream (ob->main_stream, ref);
461 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
462 streamer_write_gcov_count_stream (ob->main_stream, node->count);
463 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
465 streamer_write_hwi_stream (ob->main_stream,
466 node->ipa_transforms_to_apply.length ());
467 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
468 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
470 if (tag == LTO_symtab_analyzed_node)
472 if (node->global.inlined_to)
474 ref = lto_symtab_encoder_lookup (encoder, node->global.inlined_to);
475 gcc_assert (ref != LCC_NOT_FOUND);
477 else
478 ref = LCC_NOT_FOUND;
480 streamer_write_hwi_stream (ob->main_stream, ref);
483 group = node->get_comdat_group ();
484 if (group)
485 comdat = IDENTIFIER_POINTER (group);
486 else
487 comdat = "";
488 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
490 if (group)
492 if (node->same_comdat_group && !boundary_p)
494 ref = lto_symtab_encoder_lookup (encoder,
495 node->same_comdat_group);
496 gcc_assert (ref != LCC_NOT_FOUND);
498 else
499 ref = LCC_NOT_FOUND;
500 streamer_write_hwi_stream (ob->main_stream, ref);
503 section = node->get_section ();
504 if (!section)
505 section = "";
507 streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
509 bp = bitpack_create (ob->main_stream);
510 bp_pack_value (&bp, node->local.local, 1);
511 bp_pack_value (&bp, node->externally_visible, 1);
512 bp_pack_value (&bp, node->no_reorder, 1);
513 bp_pack_value (&bp, node->definition, 1);
514 bp_pack_value (&bp, node->local.versionable, 1);
515 bp_pack_value (&bp, node->local.can_change_signature, 1);
516 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
517 bp_pack_value (&bp, node->force_output, 1);
518 bp_pack_value (&bp, node->forced_by_abi, 1);
519 bp_pack_value (&bp, node->unique_name, 1);
520 bp_pack_value (&bp, node->body_removed, 1);
521 bp_pack_value (&bp, node->implicit_section, 1);
522 bp_pack_value (&bp, node->address_taken, 1);
523 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
524 && node->get_partitioning_class () == SYMBOL_PARTITION
525 && (reachable_from_other_partition_p (node, encoder)
526 || referenced_from_other_partition_p (node, encoder)), 1);
527 bp_pack_value (&bp, node->lowered, 1);
528 bp_pack_value (&bp, in_other_partition, 1);
529 /* Real aliases in a boundary become non-aliases. However we still stream
530 alias info on weakrefs.
531 TODO: We lose a bit of information here - when we know that variable is
532 defined in other unit, we may use the info on aliases to resolve
533 symbol1 != symbol2 type tests that we can do only for locally defined objects
534 otherwise. */
535 alias_p = node->alias && (!boundary_p || node->weakref);
536 bp_pack_value (&bp, alias_p, 1);
537 bp_pack_value (&bp, node->weakref, 1);
538 bp_pack_value (&bp, node->frequency, 2);
539 bp_pack_value (&bp, node->only_called_at_startup, 1);
540 bp_pack_value (&bp, node->only_called_at_exit, 1);
541 bp_pack_value (&bp, node->tm_clone, 1);
542 bp_pack_value (&bp, node->calls_comdat_local, 1);
543 bp_pack_value (&bp, node->thunk.thunk_p && !boundary_p, 1);
544 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
545 LDPR_NUM_KNOWN, node->resolution);
546 streamer_write_bitpack (&bp);
547 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
549 if (node->thunk.thunk_p && !boundary_p)
551 streamer_write_uhwi_stream
552 (ob->main_stream,
553 1 + (node->thunk.this_adjusting != 0) * 2
554 + (node->thunk.virtual_offset_p != 0) * 4);
555 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
556 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
558 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
559 if (DECL_STATIC_CONSTRUCTOR (node->decl))
560 streamer_write_hwi_stream (ob->main_stream, node->get_init_priority ());
561 if (DECL_STATIC_DESTRUCTOR (node->decl))
562 streamer_write_hwi_stream (ob->main_stream, node->get_fini_priority ());
565 /* Output the varpool NODE to OB.
566 If NODE is not in SET, then NODE is a boundary. */
568 static void
569 lto_output_varpool_node (struct lto_simple_output_block *ob, varpool_node *node,
570 lto_symtab_encoder_t encoder)
572 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
573 struct bitpack_d bp;
574 int ref;
575 bool alias_p;
576 const char *comdat;
577 const char *section;
578 tree group;
580 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
581 LTO_symtab_variable);
582 streamer_write_hwi_stream (ob->main_stream, node->order);
583 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
584 bp = bitpack_create (ob->main_stream);
585 bp_pack_value (&bp, node->externally_visible, 1);
586 bp_pack_value (&bp, node->no_reorder, 1);
587 bp_pack_value (&bp, node->force_output, 1);
588 bp_pack_value (&bp, node->forced_by_abi, 1);
589 bp_pack_value (&bp, node->unique_name, 1);
590 bp_pack_value (&bp, node->body_removed, 1);
591 bp_pack_value (&bp, node->implicit_section, 1);
592 bp_pack_value (&bp, node->writeonly, 1);
593 bp_pack_value (&bp, node->definition, 1);
594 alias_p = node->alias && (!boundary_p || node->weakref);
595 bp_pack_value (&bp, alias_p, 1);
596 bp_pack_value (&bp, node->weakref, 1);
597 bp_pack_value (&bp, node->analyzed && !boundary_p, 1);
598 gcc_assert (node->definition || !node->analyzed);
599 /* Constant pool initializers can be de-unified into individual ltrans units.
600 FIXME: Alternatively at -Os we may want to avoid generating for them the local
601 labels and share them across LTRANS partitions. */
602 if (node->get_partitioning_class () != SYMBOL_PARTITION)
604 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
605 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
607 else
609 bp_pack_value (&bp, node->definition
610 && referenced_from_other_partition_p (node, encoder), 1);
611 bp_pack_value (&bp, node->analyzed
612 && boundary_p && !DECL_EXTERNAL (node->decl), 1);
613 /* in_other_partition. */
615 bp_pack_value (&bp, node->tls_model, 3);
616 bp_pack_value (&bp, node->used_by_single_function, 1);
617 streamer_write_bitpack (&bp);
619 group = node->get_comdat_group ();
620 if (group)
621 comdat = IDENTIFIER_POINTER (group);
622 else
623 comdat = "";
624 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
626 if (group)
628 if (node->same_comdat_group && !boundary_p)
630 ref = lto_symtab_encoder_lookup (encoder,
631 node->same_comdat_group);
632 gcc_assert (ref != LCC_NOT_FOUND);
634 else
635 ref = LCC_NOT_FOUND;
636 streamer_write_hwi_stream (ob->main_stream, ref);
639 section = node->get_section ();
640 if (!section)
641 section = "";
642 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
644 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
645 LDPR_NUM_KNOWN, node->resolution);
648 /* Output the varpool NODE to OB.
649 If NODE is not in SET, then NODE is a boundary. */
651 static void
652 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
653 lto_symtab_encoder_t encoder)
655 struct bitpack_d bp;
656 int nref;
657 int uid = ref->lto_stmt_uid;
658 struct cgraph_node *node;
660 bp = bitpack_create (ob->main_stream);
661 bp_pack_value (&bp, ref->use, 2);
662 bp_pack_value (&bp, ref->speculative, 1);
663 streamer_write_bitpack (&bp);
664 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
665 gcc_assert (nref != LCC_NOT_FOUND);
666 streamer_write_hwi_stream (ob->main_stream, nref);
668 node = dyn_cast <cgraph_node *> (ref->referring);
669 if (node)
671 if (ref->stmt)
672 uid = gimple_uid (ref->stmt) + 1;
673 streamer_write_hwi_stream (ob->main_stream, uid);
677 /* Stream out profile_summary to OB. */
679 static void
680 output_profile_summary (struct lto_simple_output_block *ob)
682 unsigned h_ix;
683 struct bitpack_d bp;
685 if (profile_info)
687 /* We do not output num and run_max, they are not used by
688 GCC profile feedback and they are difficult to merge from multiple
689 units. */
690 gcc_assert (profile_info->runs);
691 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
692 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
694 /* sum_all is needed for computing the working set with the
695 histogram. */
696 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
698 /* Create and output a bitpack of non-zero histogram entries indices. */
699 bp = bitpack_create (ob->main_stream);
700 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
701 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
702 streamer_write_bitpack (&bp);
703 /* Now stream out only those non-zero entries. */
704 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
706 if (!profile_info->histogram[h_ix].num_counters)
707 continue;
708 streamer_write_gcov_count_stream (ob->main_stream,
709 profile_info->histogram[h_ix].num_counters);
710 streamer_write_gcov_count_stream (ob->main_stream,
711 profile_info->histogram[h_ix].min_value);
712 streamer_write_gcov_count_stream (ob->main_stream,
713 profile_info->histogram[h_ix].cum_value);
715 /* IPA-profile computes hot bb threshold based on cumulated
716 whole program profile. We need to stream it down to ltrans. */
717 if (flag_wpa)
718 streamer_write_gcov_count_stream (ob->main_stream,
719 get_hot_bb_threshold ());
721 else
722 streamer_write_uhwi_stream (ob->main_stream, 0);
725 /* Output all callees or indirect outgoing edges. EDGE must be the first such
726 edge. */
728 static void
729 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
730 struct lto_simple_output_block *ob,
731 lto_symtab_encoder_t encoder)
733 if (!edge)
734 return;
736 /* Output edges in backward direction, so the reconstructed callgraph match
737 and it is easy to associate call sites in the IPA pass summaries. */
738 while (edge->next_callee)
739 edge = edge->next_callee;
740 for (; edge; edge = edge->prev_callee)
741 lto_output_edge (ob, edge, encoder);
744 /* Output the part of the cgraph in SET. */
746 static void
747 output_refs (lto_symtab_encoder_t encoder)
749 lto_symtab_encoder_iterator lsei;
750 struct lto_simple_output_block *ob;
751 int count;
752 struct ipa_ref *ref;
753 int i;
755 ob = lto_create_simple_output_block (LTO_section_refs);
757 for (lsei = lsei_start_in_partition (encoder); !lsei_end_p (lsei);
758 lsei_next_in_partition (&lsei))
760 symtab_node *node = lsei_node (lsei);
762 count = node->ref_list.nreferences ();
763 if (count)
765 streamer_write_gcov_count_stream (ob->main_stream, count);
766 streamer_write_uhwi_stream (ob->main_stream,
767 lto_symtab_encoder_lookup (encoder, node));
768 for (i = 0; node->iterate_reference (i, ref); i++)
769 lto_output_ref (ob, ref, encoder);
773 streamer_write_uhwi_stream (ob->main_stream, 0);
775 lto_destroy_simple_output_block (ob);
778 /* Add NODE into encoder as well as nodes it is cloned from.
779 Do it in a way so clones appear first. */
781 static void
782 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
783 bool include_body)
785 if (node->clone_of)
786 add_node_to (encoder, node->clone_of, include_body);
787 else if (include_body)
788 lto_set_symtab_encoder_encode_body (encoder, node);
789 lto_symtab_encoder_encode (encoder, node);
792 /* Add all references in NODE to encoders. */
794 static void
795 create_references (lto_symtab_encoder_t encoder, symtab_node *node)
797 int i;
798 struct ipa_ref *ref = NULL;
799 for (i = 0; node->iterate_reference (i, ref); i++)
800 if (is_a <cgraph_node *> (ref->referred))
801 add_node_to (encoder, dyn_cast <cgraph_node *> (ref->referred), false);
802 else
803 lto_symtab_encoder_encode (encoder, ref->referred);
806 /* Find all symbols we want to stream into given partition and insert them
807 to encoders.
809 The function actually replaces IN_ENCODER by new one. The reason is that
810 streaming code needs clone's origin to be streamed before clone. This
811 means that we need to insert the nodes in specific order. This order is
812 ignored by the partitioning logic earlier. */
814 lto_symtab_encoder_t
815 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
817 struct cgraph_edge *edge;
818 int i;
819 lto_symtab_encoder_t encoder;
820 lto_symtab_encoder_iterator lsei;
821 hash_set<void *> reachable_call_targets;
823 encoder = lto_symtab_encoder_new (false);
825 /* Go over all entries in the IN_ENCODER and duplicate them to
826 ENCODER. At the same time insert masters of clones so
827 every master appears before clone. */
828 for (lsei = lsei_start_function_in_partition (in_encoder);
829 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
831 struct cgraph_node *node = lsei_cgraph_node (lsei);
832 add_node_to (encoder, node, true);
833 lto_set_symtab_encoder_in_partition (encoder, node);
834 create_references (encoder, node);
835 /* For proper debug info, we need to ship the origins, too. */
836 if (DECL_ABSTRACT_ORIGIN (node->decl))
838 struct cgraph_node *origin_node
839 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node->decl));
840 add_node_to (encoder, origin_node, true);
843 for (lsei = lsei_start_variable_in_partition (in_encoder);
844 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
846 varpool_node *vnode = lsei_varpool_node (lsei);
848 lto_set_symtab_encoder_in_partition (encoder, vnode);
849 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
850 create_references (encoder, vnode);
851 /* For proper debug info, we need to ship the origins, too. */
852 if (DECL_ABSTRACT_ORIGIN (vnode->decl))
854 varpool_node *origin_node
855 = varpool_node::get (DECL_ABSTRACT_ORIGIN (vnode->decl));
856 lto_set_symtab_encoder_in_partition (encoder, origin_node);
859 /* Pickle in also the initializer of all referenced readonly variables
860 to help folding. Constant pool variables are not shared, so we must
861 pickle those too. */
862 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
864 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
865 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
867 if (!lto_symtab_encoder_encode_initializer_p (encoder,
868 vnode)
869 && vnode->ctor_useable_for_folding_p ())
871 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
872 create_references (encoder, vnode);
877 /* Go over all the nodes again to include callees that are not in
878 SET. */
879 for (lsei = lsei_start_function_in_partition (encoder);
880 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
882 struct cgraph_node *node = lsei_cgraph_node (lsei);
883 for (edge = node->callees; edge; edge = edge->next_callee)
885 struct cgraph_node *callee = edge->callee;
886 if (!lto_symtab_encoder_in_partition_p (encoder, callee))
888 /* We should have moved all the inlines. */
889 gcc_assert (!callee->global.inlined_to);
890 add_node_to (encoder, callee, false);
893 /* Add all possible targets for late devirtualization. */
894 if (flag_devirtualize)
895 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
896 if (edge->indirect_info->polymorphic)
898 unsigned int i;
899 void *cache_token;
900 bool final;
901 vec <cgraph_node *>targets
902 = possible_polymorphic_call_targets
903 (edge, &final, &cache_token);
904 if (!reachable_call_targets.add (cache_token))
906 for (i = 0; i < targets.length (); i++)
908 struct cgraph_node *callee = targets[i];
910 /* Adding an external declarations into the unit serves
911 no purpose and just increases its boundary. */
912 if (callee->definition
913 && !lto_symtab_encoder_in_partition_p
914 (encoder, callee))
916 gcc_assert (!callee->global.inlined_to);
917 add_node_to (encoder, callee, false);
923 lto_symtab_encoder_delete (in_encoder);
924 return encoder;
927 /* Output the part of the symtab in SET and VSET. */
929 void
930 output_symtab (void)
932 struct cgraph_node *node;
933 struct lto_simple_output_block *ob;
934 lto_symtab_encoder_iterator lsei;
935 int i, n_nodes;
936 lto_symtab_encoder_t encoder;
938 if (flag_wpa)
939 output_cgraph_opt_summary ();
941 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
943 output_profile_summary (ob);
945 /* An encoder for cgraph nodes should have been created by
946 ipa_write_summaries_1. */
947 gcc_assert (ob->decl_state->symtab_node_encoder);
948 encoder = ob->decl_state->symtab_node_encoder;
950 /* Write out the nodes. We must first output a node and then its clones,
951 otherwise at a time reading back the node there would be nothing to clone
952 from. */
953 n_nodes = lto_symtab_encoder_size (encoder);
954 for (i = 0; i < n_nodes; i++)
956 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
957 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
958 lto_output_node (ob, cnode, encoder);
959 else
960 lto_output_varpool_node (ob, dyn_cast<varpool_node *> (node), encoder);
963 /* Go over the nodes in SET again to write edges. */
964 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
965 lsei_next_function_in_partition (&lsei))
967 node = lsei_cgraph_node (lsei);
968 output_outgoing_cgraph_edges (node->callees, ob, encoder);
969 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
972 streamer_write_uhwi_stream (ob->main_stream, 0);
974 lto_destroy_simple_output_block (ob);
976 /* Emit toplevel asms.
977 When doing WPA we must output every asm just once. Since we do not partition asm
978 nodes at all, output them to first output. This is kind of hack, but should work
979 well. */
980 if (!asm_nodes_output)
982 asm_nodes_output = true;
983 lto_output_toplevel_asms ();
986 output_refs (encoder);
989 /* Return identifier encoded in IB as a plain string. */
991 static tree
992 read_identifier (struct lto_input_block *ib)
994 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
995 tree id;
997 if (ib->data[ib->p + len])
998 lto_section_overrun (ib);
999 if (!len)
1001 ib->p++;
1002 return NULL;
1004 id = get_identifier (ib->data + ib->p);
1005 ib->p += len + 1;
1006 return id;
1009 /* Return string encoded in IB, NULL if string is empty. */
1011 static const char *
1012 read_string (struct lto_input_block *ib)
1014 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1015 const char *str;
1017 if (ib->data[ib->p + len])
1018 lto_section_overrun (ib);
1019 if (!len)
1021 ib->p++;
1022 return NULL;
1024 str = ib->data + ib->p;
1025 ib->p += len + 1;
1026 return str;
1029 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
1030 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
1031 NODE or to replace the values in it, for instance because the first
1032 time we saw it, the function body was not available but now it
1033 is. BP is a bitpack with all the bitflags for NODE read from the
1034 stream. */
1036 static void
1037 input_overwrite_node (struct lto_file_decl_data *file_data,
1038 struct cgraph_node *node,
1039 enum LTO_symtab_tags tag,
1040 struct bitpack_d *bp)
1042 node->aux = (void *) tag;
1043 node->lto_file_data = file_data;
1045 node->local.local = bp_unpack_value (bp, 1);
1046 node->externally_visible = bp_unpack_value (bp, 1);
1047 node->no_reorder = bp_unpack_value (bp, 1);
1048 node->definition = bp_unpack_value (bp, 1);
1049 node->local.versionable = bp_unpack_value (bp, 1);
1050 node->local.can_change_signature = bp_unpack_value (bp, 1);
1051 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
1052 node->force_output = bp_unpack_value (bp, 1);
1053 node->forced_by_abi = bp_unpack_value (bp, 1);
1054 node->unique_name = bp_unpack_value (bp, 1);
1055 node->body_removed = bp_unpack_value (bp, 1);
1056 node->implicit_section = bp_unpack_value (bp, 1);
1057 node->address_taken = bp_unpack_value (bp, 1);
1058 node->used_from_other_partition = bp_unpack_value (bp, 1);
1059 node->lowered = bp_unpack_value (bp, 1);
1060 node->analyzed = tag == LTO_symtab_analyzed_node;
1061 node->in_other_partition = bp_unpack_value (bp, 1);
1062 if (node->in_other_partition
1063 /* Avoid updating decl when we are seeing just inline clone.
1064 When inlining function that has functions already inlined into it,
1065 we produce clones of inline clones.
1067 WPA partitioning might put each clone into different unit and
1068 we might end up streaming inline clone from other partition
1069 to support clone we are interested in. */
1070 && (!node->clone_of
1071 || node->clone_of->decl != node->decl))
1073 DECL_EXTERNAL (node->decl) = 1;
1074 TREE_STATIC (node->decl) = 0;
1076 node->alias = bp_unpack_value (bp, 1);
1077 node->weakref = bp_unpack_value (bp, 1);
1078 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
1079 node->only_called_at_startup = bp_unpack_value (bp, 1);
1080 node->only_called_at_exit = bp_unpack_value (bp, 1);
1081 node->tm_clone = bp_unpack_value (bp, 1);
1082 node->calls_comdat_local = bp_unpack_value (bp, 1);
1083 node->thunk.thunk_p = bp_unpack_value (bp, 1);
1084 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
1085 LDPR_NUM_KNOWN);
1086 gcc_assert (flag_ltrans
1087 || (!node->in_other_partition
1088 && !node->used_from_other_partition));
1091 /* Return string alias is alias of. */
1093 static tree
1094 get_alias_symbol (tree decl)
1096 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1097 return get_identifier (TREE_STRING_POINTER
1098 (TREE_VALUE (TREE_VALUE (alias))));
1101 /* Read a node from input_block IB. TAG is the node's tag just read.
1102 Return the node read or overwriten. */
1104 static struct cgraph_node *
1105 input_node (struct lto_file_decl_data *file_data,
1106 struct lto_input_block *ib,
1107 enum LTO_symtab_tags tag,
1108 vec<symtab_node *> nodes)
1110 gcc::pass_manager *passes = g->get_passes ();
1111 tree fn_decl;
1112 struct cgraph_node *node;
1113 struct bitpack_d bp;
1114 unsigned decl_index;
1115 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1116 int clone_ref;
1117 int order;
1118 int i, count;
1119 tree group;
1120 const char *section;
1121 order = streamer_read_hwi (ib) + order_base;
1122 clone_ref = streamer_read_hwi (ib);
1124 decl_index = streamer_read_uhwi (ib);
1125 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1127 if (clone_ref != LCC_NOT_FOUND)
1129 node = dyn_cast<cgraph_node *> (nodes[clone_ref])->create_clone (fn_decl,
1130 0, CGRAPH_FREQ_BASE, false,
1131 vNULL, false, NULL, NULL);
1133 else
1135 /* Declaration of functions can be already merged with a declaration
1136 from other input file. We keep cgraph unmerged until after streaming
1137 of ipa passes is done. Alays forcingly create a fresh node. */
1138 node = symtab->create_empty ();
1139 node->decl = fn_decl;
1140 node->register_symbol ();
1143 node->order = order;
1144 if (order >= symtab->order)
1145 symtab->order = order + 1;
1147 node->count = streamer_read_gcov_count (ib);
1148 node->count_materialization_scale = streamer_read_hwi (ib);
1150 count = streamer_read_hwi (ib);
1151 node->ipa_transforms_to_apply = vNULL;
1152 for (i = 0; i < count; i++)
1154 opt_pass *pass;
1155 int pid = streamer_read_hwi (ib);
1157 gcc_assert (pid < passes->passes_by_id_size);
1158 pass = passes->passes_by_id[pid];
1159 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *) pass);
1162 if (tag == LTO_symtab_analyzed_node)
1163 ref = streamer_read_hwi (ib);
1165 group = read_identifier (ib);
1166 if (group)
1167 ref2 = streamer_read_hwi (ib);
1169 /* Make sure that we have not read this node before. Nodes that
1170 have already been read will have their tag stored in the 'aux'
1171 field. Since built-in functions can be referenced in multiple
1172 functions, they are expected to be read more than once. */
1173 if (node->aux && !DECL_BUILT_IN (node->decl))
1174 internal_error ("bytecode stream: found multiple instances of cgraph "
1175 "node with uid %d", node->uid);
1177 node->tp_first_run = streamer_read_uhwi (ib);
1179 bp = streamer_read_bitpack (ib);
1181 input_overwrite_node (file_data, node, tag, &bp);
1183 /* Store a reference for now, and fix up later to be a pointer. */
1184 node->global.inlined_to = (cgraph_node *) (intptr_t) ref;
1186 if (group)
1188 node->set_comdat_group (group);
1189 /* Store a reference for now, and fix up later to be a pointer. */
1190 node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
1192 else
1193 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1194 section = read_string (ib);
1195 if (section)
1196 node->set_section_for_node (section);
1198 if (node->thunk.thunk_p)
1200 int type = streamer_read_uhwi (ib);
1201 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1202 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1204 node->thunk.fixed_offset = fixed_offset;
1205 node->thunk.this_adjusting = (type & 2);
1206 node->thunk.virtual_value = virtual_value;
1207 node->thunk.virtual_offset_p = (type & 4);
1209 if (node->alias && !node->analyzed && node->weakref)
1210 node->alias_target = get_alias_symbol (node->decl);
1211 node->profile_id = streamer_read_hwi (ib);
1212 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1213 node->set_init_priority (streamer_read_hwi (ib));
1214 if (DECL_STATIC_DESTRUCTOR (node->decl))
1215 node->set_fini_priority (streamer_read_hwi (ib));
1216 return node;
1219 /* Read a node from input_block IB. TAG is the node's tag just read.
1220 Return the node read or overwriten. */
1222 static varpool_node *
1223 input_varpool_node (struct lto_file_decl_data *file_data,
1224 struct lto_input_block *ib)
1226 int decl_index;
1227 tree var_decl;
1228 varpool_node *node;
1229 struct bitpack_d bp;
1230 int ref = LCC_NOT_FOUND;
1231 int order;
1232 tree group;
1233 const char *section;
1235 order = streamer_read_hwi (ib) + order_base;
1236 decl_index = streamer_read_uhwi (ib);
1237 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1239 /* Declaration of functions can be already merged with a declaration
1240 from other input file. We keep cgraph unmerged until after streaming
1241 of ipa passes is done. Alays forcingly create a fresh node. */
1242 node = varpool_node::create_empty ();
1243 node->decl = var_decl;
1244 node->register_symbol ();
1246 node->order = order;
1247 if (order >= symtab->order)
1248 symtab->order = order + 1;
1249 node->lto_file_data = file_data;
1251 bp = streamer_read_bitpack (ib);
1252 node->externally_visible = bp_unpack_value (&bp, 1);
1253 node->no_reorder = bp_unpack_value (&bp, 1);
1254 node->force_output = bp_unpack_value (&bp, 1);
1255 node->forced_by_abi = bp_unpack_value (&bp, 1);
1256 node->unique_name = bp_unpack_value (&bp, 1);
1257 node->body_removed = bp_unpack_value (&bp, 1);
1258 node->implicit_section = bp_unpack_value (&bp, 1);
1259 node->writeonly = bp_unpack_value (&bp, 1);
1260 node->definition = bp_unpack_value (&bp, 1);
1261 node->alias = bp_unpack_value (&bp, 1);
1262 node->weakref = bp_unpack_value (&bp, 1);
1263 node->analyzed = bp_unpack_value (&bp, 1);
1264 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1265 node->in_other_partition = bp_unpack_value (&bp, 1);
1266 if (node->in_other_partition)
1268 DECL_EXTERNAL (node->decl) = 1;
1269 TREE_STATIC (node->decl) = 0;
1271 if (node->alias && !node->analyzed && node->weakref)
1272 node->alias_target = get_alias_symbol (node->decl);
1273 node->tls_model = (enum tls_model)bp_unpack_value (&bp, 3);
1274 node->used_by_single_function = (enum tls_model)bp_unpack_value (&bp, 1);
1275 group = read_identifier (ib);
1276 if (group)
1278 node->set_comdat_group (group);
1279 ref = streamer_read_hwi (ib);
1280 /* Store a reference for now, and fix up later to be a pointer. */
1281 node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1283 else
1284 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1285 section = read_string (ib);
1286 if (section)
1287 node->set_section_for_node (section);
1288 node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1289 LDPR_NUM_KNOWN);
1290 gcc_assert (flag_ltrans
1291 || (!node->in_other_partition
1292 && !node->used_from_other_partition));
1294 return node;
1297 /* Read a node from input_block IB. TAG is the node's tag just read.
1298 Return the node read or overwriten. */
1300 static void
1301 input_ref (struct lto_input_block *ib,
1302 symtab_node *referring_node,
1303 vec<symtab_node *> nodes)
1305 symtab_node *node = NULL;
1306 struct bitpack_d bp;
1307 enum ipa_ref_use use;
1308 bool speculative;
1309 struct ipa_ref *ref;
1311 bp = streamer_read_bitpack (ib);
1312 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1313 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1314 node = nodes[streamer_read_hwi (ib)];
1315 ref = referring_node->create_reference (node, use);
1316 ref->speculative = speculative;
1317 if (is_a <cgraph_node *> (referring_node))
1318 ref->lto_stmt_uid = streamer_read_hwi (ib);
1321 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1322 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1323 edge being read is indirect (in the sense that it has
1324 indirect_unknown_callee set). */
1326 static void
1327 input_edge (struct lto_input_block *ib, vec<symtab_node *> nodes,
1328 bool indirect)
1330 struct cgraph_node *caller, *callee;
1331 struct cgraph_edge *edge;
1332 unsigned int stmt_id;
1333 gcov_type count;
1334 int freq;
1335 cgraph_inline_failed_t inline_failed;
1336 struct bitpack_d bp;
1337 int ecf_flags = 0;
1339 caller = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1340 if (caller == NULL || caller->decl == NULL_TREE)
1341 internal_error ("bytecode stream: no caller found while reading edge");
1343 if (!indirect)
1345 callee = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1346 if (callee == NULL || callee->decl == NULL_TREE)
1347 internal_error ("bytecode stream: no callee found while reading edge");
1349 else
1350 callee = NULL;
1352 count = streamer_read_gcov_count (ib);
1354 bp = streamer_read_bitpack (ib);
1355 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_t, CIF_N_REASONS);
1356 stmt_id = bp_unpack_var_len_unsigned (&bp);
1357 freq = (int) bp_unpack_var_len_unsigned (&bp);
1359 if (indirect)
1360 edge = caller->create_indirect_edge (NULL, 0, count, freq);
1361 else
1362 edge = caller->create_edge (callee, NULL, count, freq);
1364 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1365 edge->speculative = bp_unpack_value (&bp, 1);
1366 edge->lto_stmt_uid = stmt_id;
1367 edge->inline_failed = inline_failed;
1368 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1369 edge->can_throw_external = bp_unpack_value (&bp, 1);
1370 edge->in_polymorphic_cdtor = bp_unpack_value (&bp, 1);
1371 if (indirect)
1373 if (bp_unpack_value (&bp, 1))
1374 ecf_flags |= ECF_CONST;
1375 if (bp_unpack_value (&bp, 1))
1376 ecf_flags |= ECF_PURE;
1377 if (bp_unpack_value (&bp, 1))
1378 ecf_flags |= ECF_NORETURN;
1379 if (bp_unpack_value (&bp, 1))
1380 ecf_flags |= ECF_MALLOC;
1381 if (bp_unpack_value (&bp, 1))
1382 ecf_flags |= ECF_NOTHROW;
1383 if (bp_unpack_value (&bp, 1))
1384 ecf_flags |= ECF_RETURNS_TWICE;
1385 edge->indirect_info->ecf_flags = ecf_flags;
1386 edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1387 if (edge->indirect_info->common_target_id)
1388 edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1393 /* Read a cgraph from IB using the info in FILE_DATA. */
1395 static vec<symtab_node *>
1396 input_cgraph_1 (struct lto_file_decl_data *file_data,
1397 struct lto_input_block *ib)
1399 enum LTO_symtab_tags tag;
1400 vec<symtab_node *> nodes = vNULL;
1401 symtab_node *node;
1402 unsigned i;
1404 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1405 order_base = symtab->order;
1406 while (tag)
1408 if (tag == LTO_symtab_edge)
1409 input_edge (ib, nodes, false);
1410 else if (tag == LTO_symtab_indirect_edge)
1411 input_edge (ib, nodes, true);
1412 else if (tag == LTO_symtab_variable)
1414 node = input_varpool_node (file_data, ib);
1415 nodes.safe_push (node);
1416 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1418 else
1420 node = input_node (file_data, ib, tag, nodes);
1421 if (node == NULL || node->decl == NULL_TREE)
1422 internal_error ("bytecode stream: found empty cgraph node");
1423 nodes.safe_push (node);
1424 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1427 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1430 lto_input_toplevel_asms (file_data, order_base);
1432 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1433 #ifdef ENABLE_CHECKING
1434 FOR_EACH_VEC_ELT (nodes, i, node)
1435 gcc_assert (node->aux || !is_a <cgraph_node *> (node));
1436 #endif
1437 FOR_EACH_VEC_ELT (nodes, i, node)
1439 int ref;
1440 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1442 ref = (int) (intptr_t) cnode->global.inlined_to;
1444 /* We share declaration of builtins, so we may read same node twice. */
1445 if (!node->aux)
1446 continue;
1447 node->aux = NULL;
1449 /* Fixup inlined_to from reference to pointer. */
1450 if (ref != LCC_NOT_FOUND)
1451 dyn_cast<cgraph_node *> (node)->global.inlined_to
1452 = dyn_cast<cgraph_node *> (nodes[ref]);
1453 else
1454 cnode->global.inlined_to = NULL;
1457 ref = (int) (intptr_t) node->same_comdat_group;
1459 /* Fixup same_comdat_group from reference to pointer. */
1460 if (ref != LCC_NOT_FOUND)
1461 node->same_comdat_group = nodes[ref];
1462 else
1463 node->same_comdat_group = NULL;
1465 FOR_EACH_VEC_ELT (nodes, i, node)
1466 node->aux = is_a <cgraph_node *> (node) ? (void *)1 : NULL;
1467 return nodes;
1470 /* Input ipa_refs. */
1472 static void
1473 input_refs (struct lto_input_block *ib,
1474 vec<symtab_node *> nodes)
1476 int count;
1477 int idx;
1478 while (true)
1480 symtab_node *node;
1481 count = streamer_read_uhwi (ib);
1482 if (!count)
1483 break;
1484 idx = streamer_read_uhwi (ib);
1485 node = nodes[idx];
1486 while (count)
1488 input_ref (ib, node, nodes);
1489 count--;
1495 static struct gcov_ctr_summary lto_gcov_summary;
1497 /* Input profile_info from IB. */
1498 static void
1499 input_profile_summary (struct lto_input_block *ib,
1500 struct lto_file_decl_data *file_data)
1502 unsigned h_ix;
1503 struct bitpack_d bp;
1504 unsigned int runs = streamer_read_uhwi (ib);
1505 if (runs)
1507 file_data->profile_info.runs = runs;
1508 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1509 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1511 memset (file_data->profile_info.histogram, 0,
1512 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1513 /* Input the bitpack of non-zero histogram indices. */
1514 bp = streamer_read_bitpack (ib);
1515 /* Read in and unpack the full bitpack, flagging non-zero
1516 histogram entries by setting the num_counters non-zero. */
1517 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1519 file_data->profile_info.histogram[h_ix].num_counters
1520 = bp_unpack_value (&bp, 1);
1522 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1524 if (!file_data->profile_info.histogram[h_ix].num_counters)
1525 continue;
1527 file_data->profile_info.histogram[h_ix].num_counters
1528 = streamer_read_gcov_count (ib);
1529 file_data->profile_info.histogram[h_ix].min_value
1530 = streamer_read_gcov_count (ib);
1531 file_data->profile_info.histogram[h_ix].cum_value
1532 = streamer_read_gcov_count (ib);
1534 /* IPA-profile computes hot bb threshold based on cumulated
1535 whole program profile. We need to stream it down to ltrans. */
1536 if (flag_ltrans)
1537 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1542 /* Rescale profile summaries to the same number of runs in the whole unit. */
1544 static void
1545 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1547 struct lto_file_decl_data *file_data;
1548 unsigned int j, h_ix;
1549 gcov_unsigned_t max_runs = 0;
1550 struct cgraph_node *node;
1551 struct cgraph_edge *edge;
1552 gcov_type saved_sum_all = 0;
1553 gcov_ctr_summary *saved_profile_info = 0;
1554 int saved_scale = 0;
1556 /* Find unit with maximal number of runs. If we ever get serious about
1557 roundoff errors, we might also consider computing smallest common
1558 multiply. */
1559 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1560 if (max_runs < file_data->profile_info.runs)
1561 max_runs = file_data->profile_info.runs;
1563 if (!max_runs)
1564 return;
1566 /* Simple overflow check. We probably don't need to support that many train
1567 runs. Such a large value probably imply data corruption anyway. */
1568 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1570 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1571 INT_MAX / REG_BR_PROB_BASE);
1572 return;
1575 profile_info = &lto_gcov_summary;
1576 lto_gcov_summary.runs = max_runs;
1577 lto_gcov_summary.sum_max = 0;
1578 memset (lto_gcov_summary.histogram, 0,
1579 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1581 /* Rescale all units to the maximal number of runs.
1582 sum_max can not be easily merged, as we have no idea what files come from
1583 the same run. We do not use the info anyway, so leave it 0. */
1584 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1585 if (file_data->profile_info.runs)
1587 int scale = GCOV_COMPUTE_SCALE (max_runs,
1588 file_data->profile_info.runs);
1589 lto_gcov_summary.sum_max
1590 = MAX (lto_gcov_summary.sum_max,
1591 apply_scale (file_data->profile_info.sum_max, scale));
1592 lto_gcov_summary.sum_all
1593 = MAX (lto_gcov_summary.sum_all,
1594 apply_scale (file_data->profile_info.sum_all, scale));
1595 /* Save a pointer to the profile_info with the largest
1596 scaled sum_all and the scale for use in merging the
1597 histogram. */
1598 if (!saved_profile_info
1599 || lto_gcov_summary.sum_all > saved_sum_all)
1601 saved_profile_info = &file_data->profile_info;
1602 saved_sum_all = lto_gcov_summary.sum_all;
1603 saved_scale = scale;
1607 gcc_assert (saved_profile_info);
1609 /* Scale up the histogram from the profile that had the largest
1610 scaled sum_all above. */
1611 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1613 /* Scale up the min value as we did the corresponding sum_all
1614 above. Use that to find the new histogram index. */
1615 gcov_type scaled_min
1616 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1617 saved_scale);
1618 /* The new index may be shared with another scaled histogram entry,
1619 so we need to account for a non-zero histogram entry at new_ix. */
1620 unsigned new_ix = gcov_histo_index (scaled_min);
1621 lto_gcov_summary.histogram[new_ix].min_value
1622 = (lto_gcov_summary.histogram[new_ix].num_counters
1623 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1624 : scaled_min);
1625 /* Some of the scaled counter values would ostensibly need to be placed
1626 into different (larger) histogram buckets, but we keep things simple
1627 here and place the scaled cumulative counter value in the bucket
1628 corresponding to the scaled minimum counter value. */
1629 lto_gcov_summary.histogram[new_ix].cum_value
1630 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1631 saved_scale);
1632 lto_gcov_summary.histogram[new_ix].num_counters
1633 += saved_profile_info->histogram[h_ix].num_counters;
1636 /* Watch roundoff errors. */
1637 if (lto_gcov_summary.sum_max < max_runs)
1638 lto_gcov_summary.sum_max = max_runs;
1640 /* If merging already happent at WPA time, we are done. */
1641 if (flag_ltrans)
1642 return;
1644 /* Now compute count_materialization_scale of each node.
1645 During LTRANS we already have values of count_materialization_scale
1646 computed, so just update them. */
1647 FOR_EACH_FUNCTION (node)
1648 if (node->lto_file_data
1649 && node->lto_file_data->profile_info.runs)
1651 int scale;
1653 scale = RDIV (node->count_materialization_scale * max_runs,
1654 node->lto_file_data->profile_info.runs);
1655 node->count_materialization_scale = scale;
1656 if (scale < 0)
1657 fatal_error ("Profile information in %s corrupted",
1658 file_data->file_name);
1660 if (scale == REG_BR_PROB_BASE)
1661 continue;
1662 for (edge = node->callees; edge; edge = edge->next_callee)
1663 edge->count = apply_scale (edge->count, scale);
1664 node->count = apply_scale (node->count, scale);
1668 /* Input and merge the symtab from each of the .o files passed to
1669 lto1. */
1671 void
1672 input_symtab (void)
1674 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1675 struct lto_file_decl_data *file_data;
1676 unsigned int j = 0;
1677 struct cgraph_node *node;
1679 while ((file_data = file_data_vec[j++]))
1681 const char *data;
1682 size_t len;
1683 struct lto_input_block *ib;
1684 vec<symtab_node *> nodes;
1686 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1687 &data, &len);
1688 if (!ib)
1689 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1690 input_profile_summary (ib, file_data);
1691 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1692 nodes = input_cgraph_1 (file_data, ib);
1693 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1694 ib, data, len);
1696 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1697 &data, &len);
1698 if (!ib)
1699 fatal_error ("cannot find LTO section refs in %s",
1700 file_data->file_name);
1701 input_refs (ib, nodes);
1702 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1703 ib, data, len);
1704 if (flag_ltrans)
1705 input_cgraph_opt_summary (nodes);
1706 nodes.release ();
1709 merge_profile_summaries (file_data_vec);
1710 get_working_sets ();
1713 /* Clear out the aux field that was used to store enough state to
1714 tell which nodes should be overwritten. */
1715 FOR_EACH_FUNCTION (node)
1717 /* Some nodes may have been created by cgraph_node. This
1718 happens when the callgraph contains nested functions. If the
1719 node for the parent function was never emitted to the gimple
1720 file, cgraph_node will create a node for it when setting the
1721 context of the nested function. */
1722 if (node->lto_file_data)
1723 node->aux = NULL;
1727 /* True when we need optimization summary for NODE. */
1729 static int
1730 output_cgraph_opt_summary_p (struct cgraph_node *node)
1732 return (node->clone_of
1733 && (node->clone.tree_map
1734 || node->clone.args_to_skip
1735 || node->clone.combined_args_to_skip));
1738 /* Output optimization summary for EDGE to OB. */
1739 static void
1740 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1741 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1745 /* Output optimization summary for NODE to OB. */
1747 static void
1748 output_node_opt_summary (struct output_block *ob,
1749 struct cgraph_node *node,
1750 lto_symtab_encoder_t encoder)
1752 unsigned int index;
1753 bitmap_iterator bi;
1754 struct ipa_replace_map *map;
1755 struct bitpack_d bp;
1756 int i;
1757 struct cgraph_edge *e;
1759 if (node->clone.args_to_skip)
1761 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1762 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1763 streamer_write_uhwi (ob, index);
1765 else
1766 streamer_write_uhwi (ob, 0);
1767 if (node->clone.combined_args_to_skip)
1769 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1770 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1771 streamer_write_uhwi (ob, index);
1773 else
1774 streamer_write_uhwi (ob, 0);
1775 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1776 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1778 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1779 mechanism to store function local declarations into summaries. */
1780 gcc_assert (!map->old_tree);
1781 streamer_write_uhwi (ob, map->parm_num);
1782 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1783 stream_write_tree (ob, map->new_tree, true);
1784 bp = bitpack_create (ob->main_stream);
1785 bp_pack_value (&bp, map->replace_p, 1);
1786 bp_pack_value (&bp, map->ref_p, 1);
1787 streamer_write_bitpack (&bp);
1790 if (lto_symtab_encoder_in_partition_p (encoder, node))
1792 for (e = node->callees; e; e = e->next_callee)
1793 output_edge_opt_summary (ob, e);
1794 for (e = node->indirect_calls; e; e = e->next_callee)
1795 output_edge_opt_summary (ob, e);
1799 /* Output optimization summaries stored in callgraph.
1800 At the moment it is the clone info structure. */
1802 static void
1803 output_cgraph_opt_summary (void)
1805 int i, n_nodes;
1806 lto_symtab_encoder_t encoder;
1807 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1808 unsigned count = 0;
1810 ob->symbol = NULL;
1811 encoder = ob->decl_state->symtab_node_encoder;
1812 n_nodes = lto_symtab_encoder_size (encoder);
1813 for (i = 0; i < n_nodes; i++)
1815 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1816 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1817 if (cnode && output_cgraph_opt_summary_p (cnode))
1818 count++;
1820 streamer_write_uhwi (ob, count);
1821 for (i = 0; i < n_nodes; i++)
1823 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1824 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1825 if (cnode && output_cgraph_opt_summary_p (cnode))
1827 streamer_write_uhwi (ob, i);
1828 output_node_opt_summary (ob, cnode, encoder);
1831 produce_asm (ob, NULL);
1832 destroy_output_block (ob);
1835 /* Input optimisation summary of EDGE. */
1837 static void
1838 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
1839 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
1843 /* Input optimisation summary of NODE. */
1845 static void
1846 input_node_opt_summary (struct cgraph_node *node,
1847 struct lto_input_block *ib_main,
1848 struct data_in *data_in)
1850 int i;
1851 int count;
1852 int bit;
1853 struct bitpack_d bp;
1854 struct cgraph_edge *e;
1856 count = streamer_read_uhwi (ib_main);
1857 if (count)
1858 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1859 for (i = 0; i < count; i++)
1861 bit = streamer_read_uhwi (ib_main);
1862 bitmap_set_bit (node->clone.args_to_skip, bit);
1864 count = streamer_read_uhwi (ib_main);
1865 if (count)
1866 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1867 for (i = 0; i < count; i++)
1869 bit = streamer_read_uhwi (ib_main);
1870 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1872 count = streamer_read_uhwi (ib_main);
1873 for (i = 0; i < count; i++)
1875 struct ipa_replace_map *map = ggc_alloc<ipa_replace_map> ();
1877 vec_safe_push (node->clone.tree_map, map);
1878 map->parm_num = streamer_read_uhwi (ib_main);
1879 map->old_tree = NULL;
1880 map->new_tree = stream_read_tree (ib_main, data_in);
1881 bp = streamer_read_bitpack (ib_main);
1882 map->replace_p = bp_unpack_value (&bp, 1);
1883 map->ref_p = bp_unpack_value (&bp, 1);
1885 for (e = node->callees; e; e = e->next_callee)
1886 input_edge_opt_summary (e, ib_main);
1887 for (e = node->indirect_calls; e; e = e->next_callee)
1888 input_edge_opt_summary (e, ib_main);
1891 /* Read section in file FILE_DATA of length LEN with data DATA. */
1893 static void
1894 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1895 const char *data, size_t len,
1896 vec<symtab_node *> nodes)
1898 const struct lto_function_header *header =
1899 (const struct lto_function_header *) data;
1900 const int cfg_offset = sizeof (struct lto_function_header);
1901 const int main_offset = cfg_offset + header->cfg_size;
1902 const int string_offset = main_offset + header->main_size;
1903 struct data_in *data_in;
1904 unsigned int i;
1905 unsigned int count;
1907 lto_input_block ib_main ((const char *) data + main_offset,
1908 header->main_size);
1910 data_in =
1911 lto_data_in_create (file_data, (const char *) data + string_offset,
1912 header->string_size, vNULL);
1913 count = streamer_read_uhwi (&ib_main);
1915 for (i = 0; i < count; i++)
1917 int ref = streamer_read_uhwi (&ib_main);
1918 input_node_opt_summary (dyn_cast<cgraph_node *> (nodes[ref]),
1919 &ib_main, data_in);
1921 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1922 len);
1923 lto_data_in_delete (data_in);
1926 /* Input optimization summary of cgraph. */
1928 static void
1929 input_cgraph_opt_summary (vec<symtab_node *> nodes)
1931 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1932 struct lto_file_decl_data *file_data;
1933 unsigned int j = 0;
1935 while ((file_data = file_data_vec[j++]))
1937 size_t len;
1938 const char *data =
1939 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1940 &len);
1942 if (data)
1943 input_cgraph_opt_section (file_data, data, len, nodes);