Only allow e500 double in SPE_SIMD_REGNO_P registers.
[official-gcc.git] / gcc / lto-cgraph.c
blobb5ae3c016edb4e2acfb87762a3d24a3f1184db1b
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 "vec.h"
44 #include "machmode.h"
45 #include "hard-reg-set.h"
46 #include "function.h"
47 #include "diagnostic-core.h"
48 #include "except.h"
49 #include "timevar.h"
50 #include "lto-streamer.h"
51 #include "data-streamer.h"
52 #include "tree-streamer.h"
53 #include "gcov-io.h"
54 #include "tree-pass.h"
55 #include "profile.h"
56 #include "context.h"
57 #include "pass_manager.h"
58 #include "ipa-utils.h"
60 /* True when asm nodes has been output. */
61 bool asm_nodes_output = false;
63 static void output_cgraph_opt_summary (void);
64 static void input_cgraph_opt_summary (vec<symtab_node *> nodes);
66 /* Number of LDPR values known to GCC. */
67 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
69 /* All node orders are ofsetted by ORDER_BASE. */
70 static int order_base;
72 /* Cgraph streaming is organized as set of record whose type
73 is indicated by a tag. */
74 enum LTO_symtab_tags
76 /* Must leave 0 for the stopper. */
78 /* Cgraph node without body available. */
79 LTO_symtab_unavail_node = 1,
80 /* Cgraph node with function body. */
81 LTO_symtab_analyzed_node,
82 /* Cgraph edges. */
83 LTO_symtab_edge,
84 LTO_symtab_indirect_edge,
85 LTO_symtab_variable,
86 LTO_symtab_last_tag
89 /* Create a new symtab encoder.
90 if FOR_INPUT, the encoder allocate only datastructures needed
91 to read the symtab. */
93 lto_symtab_encoder_t
94 lto_symtab_encoder_new (bool for_input)
96 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
98 if (!for_input)
99 encoder->map = new hash_map<symtab_node *, size_t>;
100 encoder->nodes.create (0);
101 return encoder;
105 /* Delete ENCODER and its components. */
107 void
108 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
110 encoder->nodes.release ();
111 if (encoder->map)
112 delete encoder->map;
113 free (encoder);
117 /* Return the existing reference number of NODE in the symtab encoder in
118 output block OB. Assign a new reference if this is the first time
119 NODE is encoded. */
122 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
123 symtab_node *node)
125 int ref;
127 if (!encoder->map)
129 lto_encoder_entry entry = {node, false, false, false};
131 ref = encoder->nodes.length ();
132 encoder->nodes.safe_push (entry);
133 return ref;
136 size_t *slot = encoder->map->get (node);
137 if (!slot || !*slot)
139 lto_encoder_entry entry = {node, false, false, false};
140 ref = encoder->nodes.length ();
141 if (!slot)
142 encoder->map->put (node, ref + 1);
143 encoder->nodes.safe_push (entry);
145 else
146 ref = *slot - 1;
148 return ref;
151 /* Remove NODE from encoder. */
153 bool
154 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
155 symtab_node *node)
157 int index;
158 lto_encoder_entry last_node;
160 size_t *slot = encoder->map->get (node);
161 if (slot == NULL || !*slot)
162 return false;
164 index = *slot - 1;
165 gcc_checking_assert (encoder->nodes[index].node == node);
167 /* Remove from vector. We do this by swapping node with the last element
168 of the vector. */
169 last_node = encoder->nodes.pop ();
170 if (last_node.node != node)
172 gcc_assert (encoder->map->put (last_node.node, index + 1));
174 /* Move the last element to the original spot of NODE. */
175 encoder->nodes[index] = last_node;
178 /* Remove element from hash table. */
179 encoder->map->remove (node);
180 return true;
184 /* Return TRUE if we should encode initializer of NODE (if any). */
186 bool
187 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
188 struct cgraph_node *node)
190 int index = lto_symtab_encoder_lookup (encoder, node);
191 return encoder->nodes[index].body;
194 /* Return TRUE if we should encode body of NODE (if any). */
196 static void
197 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
198 struct cgraph_node *node)
200 int index = lto_symtab_encoder_encode (encoder, node);
201 gcc_checking_assert (encoder->nodes[index].node == node);
202 encoder->nodes[index].body = true;
205 /* Return TRUE if we should encode initializer of NODE (if any). */
207 bool
208 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
209 varpool_node *node)
211 int index = lto_symtab_encoder_lookup (encoder, node);
212 if (index == LCC_NOT_FOUND)
213 return false;
214 return encoder->nodes[index].initializer;
217 /* Return TRUE if we should encode initializer of NODE (if any). */
219 static void
220 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
221 varpool_node *node)
223 int index = lto_symtab_encoder_lookup (encoder, node);
224 encoder->nodes[index].initializer = true;
227 /* Return TRUE if we should encode initializer of NODE (if any). */
229 bool
230 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
231 symtab_node *node)
233 int index = lto_symtab_encoder_lookup (encoder, node);
234 if (index == LCC_NOT_FOUND)
235 return false;
236 return encoder->nodes[index].in_partition;
239 /* Return TRUE if we should encode body of NODE (if any). */
241 void
242 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
243 symtab_node *node)
245 int index = lto_symtab_encoder_encode (encoder, node);
246 encoder->nodes[index].in_partition = true;
249 /* Output the cgraph EDGE to OB using ENCODER. */
251 static void
252 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
253 lto_symtab_encoder_t encoder)
255 unsigned int uid;
256 intptr_t ref;
257 struct bitpack_d bp;
259 if (edge->indirect_unknown_callee)
260 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
261 LTO_symtab_indirect_edge);
262 else
263 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
264 LTO_symtab_edge);
266 ref = lto_symtab_encoder_lookup (encoder, edge->caller);
267 gcc_assert (ref != LCC_NOT_FOUND);
268 streamer_write_hwi_stream (ob->main_stream, ref);
270 if (!edge->indirect_unknown_callee)
272 ref = lto_symtab_encoder_lookup (encoder, edge->callee);
273 gcc_assert (ref != LCC_NOT_FOUND);
274 streamer_write_hwi_stream (ob->main_stream, ref);
277 streamer_write_gcov_count_stream (ob->main_stream, edge->count);
279 bp = bitpack_create (ob->main_stream);
280 uid = (!gimple_has_body_p (edge->caller->decl)
281 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt) + 1);
282 bp_pack_enum (&bp, cgraph_inline_failed_t,
283 CIF_N_REASONS, edge->inline_failed);
284 bp_pack_var_len_unsigned (&bp, uid);
285 bp_pack_var_len_unsigned (&bp, edge->frequency);
286 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
287 bp_pack_value (&bp, edge->speculative, 1);
288 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
289 bp_pack_value (&bp, edge->can_throw_external, 1);
290 bp_pack_value (&bp, edge->in_polymorphic_cdtor, 1);
291 if (edge->indirect_unknown_callee)
293 int flags = edge->indirect_info->ecf_flags;
294 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
295 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
296 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
297 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
298 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
299 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
300 /* Flags that should not appear on indirect calls. */
301 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
302 | ECF_MAY_BE_ALLOCA
303 | ECF_SIBCALL
304 | ECF_LEAF
305 | ECF_NOVOPS)));
307 streamer_write_bitpack (&bp);
308 if (edge->indirect_unknown_callee)
310 streamer_write_hwi_stream (ob->main_stream,
311 edge->indirect_info->common_target_id);
312 if (edge->indirect_info->common_target_id)
313 streamer_write_hwi_stream
314 (ob->main_stream, edge->indirect_info->common_target_probability);
318 /* Return if NODE contain references from other partitions. */
320 bool
321 referenced_from_other_partition_p (symtab_node *node, lto_symtab_encoder_t encoder)
323 int i;
324 struct ipa_ref *ref = NULL;
326 for (i = 0; node->iterate_referring (i, ref); i++)
328 if (ref->referring->in_other_partition
329 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
330 return true;
332 return false;
335 /* Return true when node is reachable from other partition. */
337 bool
338 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
340 struct cgraph_edge *e;
341 if (!node->definition)
342 return false;
343 if (node->global.inlined_to)
344 return false;
345 for (e = node->callers; e; e = e->next_caller)
346 if (e->caller->in_other_partition
347 || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
348 return true;
349 return false;
352 /* Return if NODE contain references from other partitions. */
354 bool
355 referenced_from_this_partition_p (symtab_node *node,
356 lto_symtab_encoder_t encoder)
358 int i;
359 struct ipa_ref *ref = NULL;
361 for (i = 0; node->iterate_referring (i, ref); i++)
362 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
363 return true;
364 return false;
367 /* Return true when node is reachable from other partition. */
369 bool
370 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
372 struct cgraph_edge *e;
373 for (e = node->callers; e; e = e->next_caller)
374 if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
375 return true;
376 return false;
379 /* Output the cgraph NODE to OB. ENCODER is used to find the
380 reference number of NODE->inlined_to. SET is the set of nodes we
381 are writing to the current file. If NODE is not in SET, then NODE
382 is a boundary of a cgraph_node_set and we pretend NODE just has a
383 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
384 that have had their callgraph node written so far. This is used to
385 determine if NODE is a clone of a previously written node. */
387 static void
388 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
389 lto_symtab_encoder_t encoder)
391 unsigned int tag;
392 struct bitpack_d bp;
393 bool boundary_p;
394 intptr_t ref;
395 bool in_other_partition = false;
396 struct cgraph_node *clone_of, *ultimate_clone_of;
397 ipa_opt_pass_d *pass;
398 int i;
399 bool alias_p;
400 const char *comdat;
401 const char *section;
402 tree group;
404 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
406 if (node->analyzed && !boundary_p)
407 tag = LTO_symtab_analyzed_node;
408 else
409 tag = LTO_symtab_unavail_node;
411 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
412 tag);
413 streamer_write_hwi_stream (ob->main_stream, node->order);
415 /* In WPA mode, we only output part of the call-graph. Also, we
416 fake cgraph node attributes. There are two cases that we care.
418 Boundary nodes: There are nodes that are not part of SET but are
419 called from within SET. We artificially make them look like
420 externally visible nodes with no function body.
422 Cherry-picked nodes: These are nodes we pulled from other
423 translation units into SET during IPA-inlining. We make them as
424 local static nodes to prevent clashes with other local statics. */
425 if (boundary_p && node->analyzed
426 && node->get_partitioning_class () == SYMBOL_PARTITION)
428 /* Inline clones can not be part of boundary.
429 gcc_assert (!node->global.inlined_to);
431 FIXME: At the moment they can be, when partition contains an inline
432 clone that is clone of inline clone from outside partition. We can
433 reshape the clone tree and make other tree to be the root, but it
434 needs a bit extra work and will be promplty done by cgraph_remove_node
435 after reading back. */
436 in_other_partition = 1;
439 clone_of = node->clone_of;
440 while (clone_of
441 && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
442 if (clone_of->prev_sibling_clone)
443 clone_of = clone_of->prev_sibling_clone;
444 else
445 clone_of = clone_of->clone_of;
447 /* See if body of the master function is output. If not, we are seeing only
448 an declaration and we do not need to pass down clone tree. */
449 ultimate_clone_of = clone_of;
450 while (ultimate_clone_of && ultimate_clone_of->clone_of)
451 ultimate_clone_of = ultimate_clone_of->clone_of;
453 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
454 clone_of = NULL;
456 if (tag == LTO_symtab_analyzed_node)
457 gcc_assert (clone_of || !node->clone_of);
458 if (!clone_of)
459 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
460 else
461 streamer_write_hwi_stream (ob->main_stream, ref);
464 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
465 streamer_write_gcov_count_stream (ob->main_stream, node->count);
466 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
468 streamer_write_hwi_stream (ob->main_stream,
469 node->ipa_transforms_to_apply.length ());
470 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
471 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
473 if (tag == LTO_symtab_analyzed_node)
475 if (node->global.inlined_to)
477 ref = lto_symtab_encoder_lookup (encoder, node->global.inlined_to);
478 gcc_assert (ref != LCC_NOT_FOUND);
480 else
481 ref = LCC_NOT_FOUND;
483 streamer_write_hwi_stream (ob->main_stream, ref);
486 group = node->get_comdat_group ();
487 if (group)
488 comdat = IDENTIFIER_POINTER (group);
489 else
490 comdat = "";
491 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
493 if (group)
495 if (node->same_comdat_group && !boundary_p)
497 ref = lto_symtab_encoder_lookup (encoder,
498 node->same_comdat_group);
499 gcc_assert (ref != LCC_NOT_FOUND);
501 else
502 ref = LCC_NOT_FOUND;
503 streamer_write_hwi_stream (ob->main_stream, ref);
506 section = node->get_section ();
507 if (!section)
508 section = "";
510 streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
512 bp = bitpack_create (ob->main_stream);
513 bp_pack_value (&bp, node->local.local, 1);
514 bp_pack_value (&bp, node->externally_visible, 1);
515 bp_pack_value (&bp, node->no_reorder, 1);
516 bp_pack_value (&bp, node->definition, 1);
517 bp_pack_value (&bp, node->local.versionable, 1);
518 bp_pack_value (&bp, node->local.can_change_signature, 1);
519 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
520 bp_pack_value (&bp, node->force_output, 1);
521 bp_pack_value (&bp, node->forced_by_abi, 1);
522 bp_pack_value (&bp, node->unique_name, 1);
523 bp_pack_value (&bp, node->body_removed, 1);
524 bp_pack_value (&bp, node->implicit_section, 1);
525 bp_pack_value (&bp, node->address_taken, 1);
526 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
527 && node->get_partitioning_class () == SYMBOL_PARTITION
528 && (reachable_from_other_partition_p (node, encoder)
529 || referenced_from_other_partition_p (node, encoder)), 1);
530 bp_pack_value (&bp, node->lowered, 1);
531 bp_pack_value (&bp, in_other_partition, 1);
532 /* Real aliases in a boundary become non-aliases. However we still stream
533 alias info on weakrefs.
534 TODO: We lose a bit of information here - when we know that variable is
535 defined in other unit, we may use the info on aliases to resolve
536 symbol1 != symbol2 type tests that we can do only for locally defined objects
537 otherwise. */
538 alias_p = node->alias && (!boundary_p || node->weakref);
539 bp_pack_value (&bp, alias_p, 1);
540 bp_pack_value (&bp, node->weakref, 1);
541 bp_pack_value (&bp, node->frequency, 2);
542 bp_pack_value (&bp, node->only_called_at_startup, 1);
543 bp_pack_value (&bp, node->only_called_at_exit, 1);
544 bp_pack_value (&bp, node->tm_clone, 1);
545 bp_pack_value (&bp, node->calls_comdat_local, 1);
546 bp_pack_value (&bp, node->icf_merged, 1);
547 bp_pack_value (&bp, node->thunk.thunk_p && !boundary_p, 1);
548 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
549 LDPR_NUM_KNOWN, node->resolution);
550 streamer_write_bitpack (&bp);
551 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
553 if (node->thunk.thunk_p && !boundary_p)
555 streamer_write_uhwi_stream
556 (ob->main_stream,
557 1 + (node->thunk.this_adjusting != 0) * 2
558 + (node->thunk.virtual_offset_p != 0) * 4);
559 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
560 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
562 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
563 if (DECL_STATIC_CONSTRUCTOR (node->decl))
564 streamer_write_hwi_stream (ob->main_stream, node->get_init_priority ());
565 if (DECL_STATIC_DESTRUCTOR (node->decl))
566 streamer_write_hwi_stream (ob->main_stream, node->get_fini_priority ());
569 /* Output the varpool NODE to OB.
570 If NODE is not in SET, then NODE is a boundary. */
572 static void
573 lto_output_varpool_node (struct lto_simple_output_block *ob, varpool_node *node,
574 lto_symtab_encoder_t encoder)
576 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
577 struct bitpack_d bp;
578 int ref;
579 bool alias_p;
580 const char *comdat;
581 const char *section;
582 tree group;
584 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
585 LTO_symtab_variable);
586 streamer_write_hwi_stream (ob->main_stream, node->order);
587 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
588 bp = bitpack_create (ob->main_stream);
589 bp_pack_value (&bp, node->externally_visible, 1);
590 bp_pack_value (&bp, node->no_reorder, 1);
591 bp_pack_value (&bp, node->force_output, 1);
592 bp_pack_value (&bp, node->forced_by_abi, 1);
593 bp_pack_value (&bp, node->unique_name, 1);
594 bp_pack_value (&bp, node->body_removed, 1);
595 bp_pack_value (&bp, node->implicit_section, 1);
596 bp_pack_value (&bp, node->writeonly, 1);
597 bp_pack_value (&bp, node->definition, 1);
598 alias_p = node->alias && (!boundary_p || node->weakref);
599 bp_pack_value (&bp, alias_p, 1);
600 bp_pack_value (&bp, node->weakref, 1);
601 bp_pack_value (&bp, node->analyzed && !boundary_p, 1);
602 gcc_assert (node->definition || !node->analyzed);
603 /* Constant pool initializers can be de-unified into individual ltrans units.
604 FIXME: Alternatively at -Os we may want to avoid generating for them the local
605 labels and share them across LTRANS partitions. */
606 if (node->get_partitioning_class () != SYMBOL_PARTITION)
608 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
609 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
611 else
613 bp_pack_value (&bp, node->definition
614 && referenced_from_other_partition_p (node, encoder), 1);
615 bp_pack_value (&bp, node->analyzed
616 && boundary_p && !DECL_EXTERNAL (node->decl), 1);
617 /* in_other_partition. */
619 bp_pack_value (&bp, node->tls_model, 3);
620 bp_pack_value (&bp, node->used_by_single_function, 1);
621 streamer_write_bitpack (&bp);
623 group = node->get_comdat_group ();
624 if (group)
625 comdat = IDENTIFIER_POINTER (group);
626 else
627 comdat = "";
628 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
630 if (group)
632 if (node->same_comdat_group && !boundary_p)
634 ref = lto_symtab_encoder_lookup (encoder,
635 node->same_comdat_group);
636 gcc_assert (ref != LCC_NOT_FOUND);
638 else
639 ref = LCC_NOT_FOUND;
640 streamer_write_hwi_stream (ob->main_stream, ref);
643 section = node->get_section ();
644 if (!section)
645 section = "";
646 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
648 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
649 LDPR_NUM_KNOWN, node->resolution);
652 /* Output the varpool NODE to OB.
653 If NODE is not in SET, then NODE is a boundary. */
655 static void
656 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
657 lto_symtab_encoder_t encoder)
659 struct bitpack_d bp;
660 int nref;
661 int uid = ref->lto_stmt_uid;
662 struct cgraph_node *node;
664 bp = bitpack_create (ob->main_stream);
665 bp_pack_value (&bp, ref->use, 2);
666 bp_pack_value (&bp, ref->speculative, 1);
667 streamer_write_bitpack (&bp);
668 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
669 gcc_assert (nref != LCC_NOT_FOUND);
670 streamer_write_hwi_stream (ob->main_stream, nref);
672 node = dyn_cast <cgraph_node *> (ref->referring);
673 if (node)
675 if (ref->stmt)
676 uid = gimple_uid (ref->stmt) + 1;
677 streamer_write_hwi_stream (ob->main_stream, uid);
681 /* Stream out profile_summary to OB. */
683 static void
684 output_profile_summary (struct lto_simple_output_block *ob)
686 unsigned h_ix;
687 struct bitpack_d bp;
689 if (profile_info)
691 /* We do not output num and run_max, they are not used by
692 GCC profile feedback and they are difficult to merge from multiple
693 units. */
694 gcc_assert (profile_info->runs);
695 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
696 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
698 /* sum_all is needed for computing the working set with the
699 histogram. */
700 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
702 /* Create and output a bitpack of non-zero histogram entries indices. */
703 bp = bitpack_create (ob->main_stream);
704 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
705 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
706 streamer_write_bitpack (&bp);
707 /* Now stream out only those non-zero entries. */
708 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
710 if (!profile_info->histogram[h_ix].num_counters)
711 continue;
712 streamer_write_gcov_count_stream (ob->main_stream,
713 profile_info->histogram[h_ix].num_counters);
714 streamer_write_gcov_count_stream (ob->main_stream,
715 profile_info->histogram[h_ix].min_value);
716 streamer_write_gcov_count_stream (ob->main_stream,
717 profile_info->histogram[h_ix].cum_value);
719 /* IPA-profile computes hot bb threshold based on cumulated
720 whole program profile. We need to stream it down to ltrans. */
721 if (flag_wpa)
722 streamer_write_gcov_count_stream (ob->main_stream,
723 get_hot_bb_threshold ());
725 else
726 streamer_write_uhwi_stream (ob->main_stream, 0);
729 /* Output all callees or indirect outgoing edges. EDGE must be the first such
730 edge. */
732 static void
733 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
734 struct lto_simple_output_block *ob,
735 lto_symtab_encoder_t encoder)
737 if (!edge)
738 return;
740 /* Output edges in backward direction, so the reconstructed callgraph match
741 and it is easy to associate call sites in the IPA pass summaries. */
742 while (edge->next_callee)
743 edge = edge->next_callee;
744 for (; edge; edge = edge->prev_callee)
745 lto_output_edge (ob, edge, encoder);
748 /* Output the part of the cgraph in SET. */
750 static void
751 output_refs (lto_symtab_encoder_t encoder)
753 lto_symtab_encoder_iterator lsei;
754 struct lto_simple_output_block *ob;
755 int count;
756 struct ipa_ref *ref;
757 int i;
759 ob = lto_create_simple_output_block (LTO_section_refs);
761 for (lsei = lsei_start_in_partition (encoder); !lsei_end_p (lsei);
762 lsei_next_in_partition (&lsei))
764 symtab_node *node = lsei_node (lsei);
766 count = node->ref_list.nreferences ();
767 if (count)
769 streamer_write_gcov_count_stream (ob->main_stream, count);
770 streamer_write_uhwi_stream (ob->main_stream,
771 lto_symtab_encoder_lookup (encoder, node));
772 for (i = 0; node->iterate_reference (i, ref); i++)
773 lto_output_ref (ob, ref, encoder);
777 streamer_write_uhwi_stream (ob->main_stream, 0);
779 lto_destroy_simple_output_block (ob);
782 /* Add NODE into encoder as well as nodes it is cloned from.
783 Do it in a way so clones appear first. */
785 static void
786 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
787 bool include_body)
789 if (node->clone_of)
790 add_node_to (encoder, node->clone_of, include_body);
791 else if (include_body)
792 lto_set_symtab_encoder_encode_body (encoder, node);
793 lto_symtab_encoder_encode (encoder, node);
796 /* Add all references in NODE to encoders. */
798 static void
799 create_references (lto_symtab_encoder_t encoder, symtab_node *node)
801 int i;
802 struct ipa_ref *ref = NULL;
803 for (i = 0; node->iterate_reference (i, ref); i++)
804 if (is_a <cgraph_node *> (ref->referred))
805 add_node_to (encoder, dyn_cast <cgraph_node *> (ref->referred), false);
806 else
807 lto_symtab_encoder_encode (encoder, ref->referred);
810 /* Find all symbols we want to stream into given partition and insert them
811 to encoders.
813 The function actually replaces IN_ENCODER by new one. The reason is that
814 streaming code needs clone's origin to be streamed before clone. This
815 means that we need to insert the nodes in specific order. This order is
816 ignored by the partitioning logic earlier. */
818 lto_symtab_encoder_t
819 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
821 struct cgraph_edge *edge;
822 int i;
823 lto_symtab_encoder_t encoder;
824 lto_symtab_encoder_iterator lsei;
825 hash_set<void *> reachable_call_targets;
827 encoder = lto_symtab_encoder_new (false);
829 /* Go over all entries in the IN_ENCODER and duplicate them to
830 ENCODER. At the same time insert masters of clones so
831 every master appears before clone. */
832 for (lsei = lsei_start_function_in_partition (in_encoder);
833 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
835 struct cgraph_node *node = lsei_cgraph_node (lsei);
836 add_node_to (encoder, node, true);
837 lto_set_symtab_encoder_in_partition (encoder, node);
838 create_references (encoder, node);
839 /* For proper debug info, we need to ship the origins, too. */
840 if (DECL_ABSTRACT_ORIGIN (node->decl))
842 struct cgraph_node *origin_node
843 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node->decl));
844 add_node_to (encoder, origin_node, true);
847 for (lsei = lsei_start_variable_in_partition (in_encoder);
848 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
850 varpool_node *vnode = lsei_varpool_node (lsei);
852 lto_set_symtab_encoder_in_partition (encoder, vnode);
853 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
854 create_references (encoder, vnode);
855 /* For proper debug info, we need to ship the origins, too. */
856 if (DECL_ABSTRACT_ORIGIN (vnode->decl))
858 varpool_node *origin_node
859 = varpool_node::get (DECL_ABSTRACT_ORIGIN (vnode->decl));
860 lto_set_symtab_encoder_in_partition (encoder, origin_node);
863 /* Pickle in also the initializer of all referenced readonly variables
864 to help folding. Constant pool variables are not shared, so we must
865 pickle those too. */
866 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
868 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
869 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
871 if (!lto_symtab_encoder_encode_initializer_p (encoder,
872 vnode)
873 && vnode->ctor_useable_for_folding_p ())
875 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
876 create_references (encoder, vnode);
881 /* Go over all the nodes again to include callees that are not in
882 SET. */
883 for (lsei = lsei_start_function_in_partition (encoder);
884 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
886 struct cgraph_node *node = lsei_cgraph_node (lsei);
887 for (edge = node->callees; edge; edge = edge->next_callee)
889 struct cgraph_node *callee = edge->callee;
890 if (!lto_symtab_encoder_in_partition_p (encoder, callee))
892 /* We should have moved all the inlines. */
893 gcc_assert (!callee->global.inlined_to);
894 add_node_to (encoder, callee, false);
897 /* Add all possible targets for late devirtualization. */
898 if (flag_devirtualize)
899 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
900 if (edge->indirect_info->polymorphic)
902 unsigned int i;
903 void *cache_token;
904 bool final;
905 vec <cgraph_node *>targets
906 = possible_polymorphic_call_targets
907 (edge, &final, &cache_token);
908 if (!reachable_call_targets.add (cache_token))
910 for (i = 0; i < targets.length (); i++)
912 struct cgraph_node *callee = targets[i];
914 /* Adding an external declarations into the unit serves
915 no purpose and just increases its boundary. */
916 if (callee->definition
917 && !lto_symtab_encoder_in_partition_p
918 (encoder, callee))
920 gcc_assert (!callee->global.inlined_to);
921 add_node_to (encoder, callee, false);
927 lto_symtab_encoder_delete (in_encoder);
928 return encoder;
931 /* Output the part of the symtab in SET and VSET. */
933 void
934 output_symtab (void)
936 struct cgraph_node *node;
937 struct lto_simple_output_block *ob;
938 lto_symtab_encoder_iterator lsei;
939 int i, n_nodes;
940 lto_symtab_encoder_t encoder;
942 if (flag_wpa)
943 output_cgraph_opt_summary ();
945 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
947 output_profile_summary (ob);
949 /* An encoder for cgraph nodes should have been created by
950 ipa_write_summaries_1. */
951 gcc_assert (ob->decl_state->symtab_node_encoder);
952 encoder = ob->decl_state->symtab_node_encoder;
954 /* Write out the nodes. We must first output a node and then its clones,
955 otherwise at a time reading back the node there would be nothing to clone
956 from. */
957 n_nodes = lto_symtab_encoder_size (encoder);
958 for (i = 0; i < n_nodes; i++)
960 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
961 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
962 lto_output_node (ob, cnode, encoder);
963 else
964 lto_output_varpool_node (ob, dyn_cast<varpool_node *> (node), encoder);
967 /* Go over the nodes in SET again to write edges. */
968 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
969 lsei_next_function_in_partition (&lsei))
971 node = lsei_cgraph_node (lsei);
972 output_outgoing_cgraph_edges (node->callees, ob, encoder);
973 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
976 streamer_write_uhwi_stream (ob->main_stream, 0);
978 lto_destroy_simple_output_block (ob);
980 /* Emit toplevel asms.
981 When doing WPA we must output every asm just once. Since we do not partition asm
982 nodes at all, output them to first output. This is kind of hack, but should work
983 well. */
984 if (!asm_nodes_output)
986 asm_nodes_output = true;
987 lto_output_toplevel_asms ();
990 output_refs (encoder);
993 /* Return identifier encoded in IB as a plain string. */
995 static tree
996 read_identifier (struct lto_input_block *ib)
998 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
999 tree id;
1001 if (ib->data[ib->p + len])
1002 lto_section_overrun (ib);
1003 if (!len)
1005 ib->p++;
1006 return NULL;
1008 id = get_identifier (ib->data + ib->p);
1009 ib->p += len + 1;
1010 return id;
1013 /* Return string encoded in IB, NULL if string is empty. */
1015 static const char *
1016 read_string (struct lto_input_block *ib)
1018 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1019 const char *str;
1021 if (ib->data[ib->p + len])
1022 lto_section_overrun (ib);
1023 if (!len)
1025 ib->p++;
1026 return NULL;
1028 str = ib->data + ib->p;
1029 ib->p += len + 1;
1030 return str;
1033 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
1034 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
1035 NODE or to replace the values in it, for instance because the first
1036 time we saw it, the function body was not available but now it
1037 is. BP is a bitpack with all the bitflags for NODE read from the
1038 stream. */
1040 static void
1041 input_overwrite_node (struct lto_file_decl_data *file_data,
1042 struct cgraph_node *node,
1043 enum LTO_symtab_tags tag,
1044 struct bitpack_d *bp)
1046 node->aux = (void *) tag;
1047 node->lto_file_data = file_data;
1049 node->local.local = bp_unpack_value (bp, 1);
1050 node->externally_visible = bp_unpack_value (bp, 1);
1051 node->no_reorder = bp_unpack_value (bp, 1);
1052 node->definition = bp_unpack_value (bp, 1);
1053 node->local.versionable = bp_unpack_value (bp, 1);
1054 node->local.can_change_signature = bp_unpack_value (bp, 1);
1055 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
1056 node->force_output = bp_unpack_value (bp, 1);
1057 node->forced_by_abi = bp_unpack_value (bp, 1);
1058 node->unique_name = bp_unpack_value (bp, 1);
1059 node->body_removed = bp_unpack_value (bp, 1);
1060 node->implicit_section = bp_unpack_value (bp, 1);
1061 node->address_taken = bp_unpack_value (bp, 1);
1062 node->used_from_other_partition = bp_unpack_value (bp, 1);
1063 node->lowered = bp_unpack_value (bp, 1);
1064 node->analyzed = tag == LTO_symtab_analyzed_node;
1065 node->in_other_partition = bp_unpack_value (bp, 1);
1066 if (node->in_other_partition
1067 /* Avoid updating decl when we are seeing just inline clone.
1068 When inlining function that has functions already inlined into it,
1069 we produce clones of inline clones.
1071 WPA partitioning might put each clone into different unit and
1072 we might end up streaming inline clone from other partition
1073 to support clone we are interested in. */
1074 && (!node->clone_of
1075 || node->clone_of->decl != node->decl))
1077 DECL_EXTERNAL (node->decl) = 1;
1078 TREE_STATIC (node->decl) = 0;
1080 node->alias = bp_unpack_value (bp, 1);
1081 node->weakref = bp_unpack_value (bp, 1);
1082 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
1083 node->only_called_at_startup = bp_unpack_value (bp, 1);
1084 node->only_called_at_exit = bp_unpack_value (bp, 1);
1085 node->tm_clone = bp_unpack_value (bp, 1);
1086 node->calls_comdat_local = bp_unpack_value (bp, 1);
1087 node->icf_merged = bp_unpack_value (bp, 1);
1088 node->thunk.thunk_p = bp_unpack_value (bp, 1);
1089 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
1090 LDPR_NUM_KNOWN);
1091 gcc_assert (flag_ltrans
1092 || (!node->in_other_partition
1093 && !node->used_from_other_partition));
1096 /* Return string alias is alias of. */
1098 static tree
1099 get_alias_symbol (tree decl)
1101 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1102 return get_identifier (TREE_STRING_POINTER
1103 (TREE_VALUE (TREE_VALUE (alias))));
1106 /* Read a node from input_block IB. TAG is the node's tag just read.
1107 Return the node read or overwriten. */
1109 static struct cgraph_node *
1110 input_node (struct lto_file_decl_data *file_data,
1111 struct lto_input_block *ib,
1112 enum LTO_symtab_tags tag,
1113 vec<symtab_node *> nodes)
1115 gcc::pass_manager *passes = g->get_passes ();
1116 tree fn_decl;
1117 struct cgraph_node *node;
1118 struct bitpack_d bp;
1119 unsigned decl_index;
1120 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1121 int clone_ref;
1122 int order;
1123 int i, count;
1124 tree group;
1125 const char *section;
1126 order = streamer_read_hwi (ib) + order_base;
1127 clone_ref = streamer_read_hwi (ib);
1129 decl_index = streamer_read_uhwi (ib);
1130 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1132 if (clone_ref != LCC_NOT_FOUND)
1134 node = dyn_cast<cgraph_node *> (nodes[clone_ref])->create_clone (fn_decl,
1135 0, CGRAPH_FREQ_BASE, false,
1136 vNULL, false, NULL, NULL);
1138 else
1140 /* Declaration of functions can be already merged with a declaration
1141 from other input file. We keep cgraph unmerged until after streaming
1142 of ipa passes is done. Alays forcingly create a fresh node. */
1143 node = symtab->create_empty ();
1144 node->decl = fn_decl;
1145 node->register_symbol ();
1148 node->order = order;
1149 if (order >= symtab->order)
1150 symtab->order = order + 1;
1152 node->count = streamer_read_gcov_count (ib);
1153 node->count_materialization_scale = streamer_read_hwi (ib);
1155 count = streamer_read_hwi (ib);
1156 node->ipa_transforms_to_apply = vNULL;
1157 for (i = 0; i < count; i++)
1159 opt_pass *pass;
1160 int pid = streamer_read_hwi (ib);
1162 gcc_assert (pid < passes->passes_by_id_size);
1163 pass = passes->passes_by_id[pid];
1164 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *) pass);
1167 if (tag == LTO_symtab_analyzed_node)
1168 ref = streamer_read_hwi (ib);
1170 group = read_identifier (ib);
1171 if (group)
1172 ref2 = streamer_read_hwi (ib);
1174 /* Make sure that we have not read this node before. Nodes that
1175 have already been read will have their tag stored in the 'aux'
1176 field. Since built-in functions can be referenced in multiple
1177 functions, they are expected to be read more than once. */
1178 if (node->aux && !DECL_BUILT_IN (node->decl))
1179 internal_error ("bytecode stream: found multiple instances of cgraph "
1180 "node with uid %d", node->uid);
1182 node->tp_first_run = streamer_read_uhwi (ib);
1184 bp = streamer_read_bitpack (ib);
1186 input_overwrite_node (file_data, node, tag, &bp);
1188 /* Store a reference for now, and fix up later to be a pointer. */
1189 node->global.inlined_to = (cgraph_node *) (intptr_t) ref;
1191 if (group)
1193 node->set_comdat_group (group);
1194 /* Store a reference for now, and fix up later to be a pointer. */
1195 node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
1197 else
1198 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1199 section = read_string (ib);
1200 if (section)
1201 node->set_section_for_node (section);
1203 if (node->thunk.thunk_p)
1205 int type = streamer_read_uhwi (ib);
1206 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1207 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1209 node->thunk.fixed_offset = fixed_offset;
1210 node->thunk.this_adjusting = (type & 2);
1211 node->thunk.virtual_value = virtual_value;
1212 node->thunk.virtual_offset_p = (type & 4);
1214 if (node->alias && !node->analyzed && node->weakref)
1215 node->alias_target = get_alias_symbol (node->decl);
1216 node->profile_id = streamer_read_hwi (ib);
1217 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1218 node->set_init_priority (streamer_read_hwi (ib));
1219 if (DECL_STATIC_DESTRUCTOR (node->decl))
1220 node->set_fini_priority (streamer_read_hwi (ib));
1221 return node;
1224 /* Read a node from input_block IB. TAG is the node's tag just read.
1225 Return the node read or overwriten. */
1227 static varpool_node *
1228 input_varpool_node (struct lto_file_decl_data *file_data,
1229 struct lto_input_block *ib)
1231 int decl_index;
1232 tree var_decl;
1233 varpool_node *node;
1234 struct bitpack_d bp;
1235 int ref = LCC_NOT_FOUND;
1236 int order;
1237 tree group;
1238 const char *section;
1240 order = streamer_read_hwi (ib) + order_base;
1241 decl_index = streamer_read_uhwi (ib);
1242 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1244 /* Declaration of functions can be already merged with a declaration
1245 from other input file. We keep cgraph unmerged until after streaming
1246 of ipa passes is done. Alays forcingly create a fresh node. */
1247 node = varpool_node::create_empty ();
1248 node->decl = var_decl;
1249 node->register_symbol ();
1251 node->order = order;
1252 if (order >= symtab->order)
1253 symtab->order = order + 1;
1254 node->lto_file_data = file_data;
1256 bp = streamer_read_bitpack (ib);
1257 node->externally_visible = bp_unpack_value (&bp, 1);
1258 node->no_reorder = bp_unpack_value (&bp, 1);
1259 node->force_output = bp_unpack_value (&bp, 1);
1260 node->forced_by_abi = bp_unpack_value (&bp, 1);
1261 node->unique_name = bp_unpack_value (&bp, 1);
1262 node->body_removed = bp_unpack_value (&bp, 1);
1263 node->implicit_section = bp_unpack_value (&bp, 1);
1264 node->writeonly = bp_unpack_value (&bp, 1);
1265 node->definition = bp_unpack_value (&bp, 1);
1266 node->alias = bp_unpack_value (&bp, 1);
1267 node->weakref = bp_unpack_value (&bp, 1);
1268 node->analyzed = bp_unpack_value (&bp, 1);
1269 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1270 node->in_other_partition = bp_unpack_value (&bp, 1);
1271 if (node->in_other_partition)
1273 DECL_EXTERNAL (node->decl) = 1;
1274 TREE_STATIC (node->decl) = 0;
1276 if (node->alias && !node->analyzed && node->weakref)
1277 node->alias_target = get_alias_symbol (node->decl);
1278 node->tls_model = (enum tls_model)bp_unpack_value (&bp, 3);
1279 node->used_by_single_function = (enum tls_model)bp_unpack_value (&bp, 1);
1280 group = read_identifier (ib);
1281 if (group)
1283 node->set_comdat_group (group);
1284 ref = streamer_read_hwi (ib);
1285 /* Store a reference for now, and fix up later to be a pointer. */
1286 node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1288 else
1289 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1290 section = read_string (ib);
1291 if (section)
1292 node->set_section_for_node (section);
1293 node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1294 LDPR_NUM_KNOWN);
1295 gcc_assert (flag_ltrans
1296 || (!node->in_other_partition
1297 && !node->used_from_other_partition));
1299 return node;
1302 /* Read a node from input_block IB. TAG is the node's tag just read.
1303 Return the node read or overwriten. */
1305 static void
1306 input_ref (struct lto_input_block *ib,
1307 symtab_node *referring_node,
1308 vec<symtab_node *> nodes)
1310 symtab_node *node = NULL;
1311 struct bitpack_d bp;
1312 enum ipa_ref_use use;
1313 bool speculative;
1314 struct ipa_ref *ref;
1316 bp = streamer_read_bitpack (ib);
1317 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1318 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1319 node = nodes[streamer_read_hwi (ib)];
1320 ref = referring_node->create_reference (node, use);
1321 ref->speculative = speculative;
1322 if (is_a <cgraph_node *> (referring_node))
1323 ref->lto_stmt_uid = streamer_read_hwi (ib);
1326 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1327 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1328 edge being read is indirect (in the sense that it has
1329 indirect_unknown_callee set). */
1331 static void
1332 input_edge (struct lto_input_block *ib, vec<symtab_node *> nodes,
1333 bool indirect)
1335 struct cgraph_node *caller, *callee;
1336 struct cgraph_edge *edge;
1337 unsigned int stmt_id;
1338 gcov_type count;
1339 int freq;
1340 cgraph_inline_failed_t inline_failed;
1341 struct bitpack_d bp;
1342 int ecf_flags = 0;
1344 caller = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1345 if (caller == NULL || caller->decl == NULL_TREE)
1346 internal_error ("bytecode stream: no caller found while reading edge");
1348 if (!indirect)
1350 callee = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1351 if (callee == NULL || callee->decl == NULL_TREE)
1352 internal_error ("bytecode stream: no callee found while reading edge");
1354 else
1355 callee = NULL;
1357 count = streamer_read_gcov_count (ib);
1359 bp = streamer_read_bitpack (ib);
1360 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_t, CIF_N_REASONS);
1361 stmt_id = bp_unpack_var_len_unsigned (&bp);
1362 freq = (int) bp_unpack_var_len_unsigned (&bp);
1364 if (indirect)
1365 edge = caller->create_indirect_edge (NULL, 0, count, freq);
1366 else
1367 edge = caller->create_edge (callee, NULL, count, freq);
1369 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1370 edge->speculative = bp_unpack_value (&bp, 1);
1371 edge->lto_stmt_uid = stmt_id;
1372 edge->inline_failed = inline_failed;
1373 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1374 edge->can_throw_external = bp_unpack_value (&bp, 1);
1375 edge->in_polymorphic_cdtor = bp_unpack_value (&bp, 1);
1376 if (indirect)
1378 if (bp_unpack_value (&bp, 1))
1379 ecf_flags |= ECF_CONST;
1380 if (bp_unpack_value (&bp, 1))
1381 ecf_flags |= ECF_PURE;
1382 if (bp_unpack_value (&bp, 1))
1383 ecf_flags |= ECF_NORETURN;
1384 if (bp_unpack_value (&bp, 1))
1385 ecf_flags |= ECF_MALLOC;
1386 if (bp_unpack_value (&bp, 1))
1387 ecf_flags |= ECF_NOTHROW;
1388 if (bp_unpack_value (&bp, 1))
1389 ecf_flags |= ECF_RETURNS_TWICE;
1390 edge->indirect_info->ecf_flags = ecf_flags;
1391 edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1392 if (edge->indirect_info->common_target_id)
1393 edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1398 /* Read a cgraph from IB using the info in FILE_DATA. */
1400 static vec<symtab_node *>
1401 input_cgraph_1 (struct lto_file_decl_data *file_data,
1402 struct lto_input_block *ib)
1404 enum LTO_symtab_tags tag;
1405 vec<symtab_node *> nodes = vNULL;
1406 symtab_node *node;
1407 unsigned i;
1409 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1410 order_base = symtab->order;
1411 while (tag)
1413 if (tag == LTO_symtab_edge)
1414 input_edge (ib, nodes, false);
1415 else if (tag == LTO_symtab_indirect_edge)
1416 input_edge (ib, nodes, true);
1417 else if (tag == LTO_symtab_variable)
1419 node = input_varpool_node (file_data, ib);
1420 nodes.safe_push (node);
1421 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1423 else
1425 node = input_node (file_data, ib, tag, nodes);
1426 if (node == NULL || node->decl == NULL_TREE)
1427 internal_error ("bytecode stream: found empty cgraph node");
1428 nodes.safe_push (node);
1429 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1432 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1435 lto_input_toplevel_asms (file_data, order_base);
1437 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1438 #ifdef ENABLE_CHECKING
1439 FOR_EACH_VEC_ELT (nodes, i, node)
1440 gcc_assert (node->aux || !is_a <cgraph_node *> (node));
1441 #endif
1442 FOR_EACH_VEC_ELT (nodes, i, node)
1444 int ref;
1445 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1447 ref = (int) (intptr_t) cnode->global.inlined_to;
1449 /* We share declaration of builtins, so we may read same node twice. */
1450 if (!node->aux)
1451 continue;
1452 node->aux = NULL;
1454 /* Fixup inlined_to from reference to pointer. */
1455 if (ref != LCC_NOT_FOUND)
1456 dyn_cast<cgraph_node *> (node)->global.inlined_to
1457 = dyn_cast<cgraph_node *> (nodes[ref]);
1458 else
1459 cnode->global.inlined_to = NULL;
1462 ref = (int) (intptr_t) node->same_comdat_group;
1464 /* Fixup same_comdat_group from reference to pointer. */
1465 if (ref != LCC_NOT_FOUND)
1466 node->same_comdat_group = nodes[ref];
1467 else
1468 node->same_comdat_group = NULL;
1470 FOR_EACH_VEC_ELT (nodes, i, node)
1471 node->aux = is_a <cgraph_node *> (node) ? (void *)1 : NULL;
1472 return nodes;
1475 /* Input ipa_refs. */
1477 static void
1478 input_refs (struct lto_input_block *ib,
1479 vec<symtab_node *> nodes)
1481 int count;
1482 int idx;
1483 while (true)
1485 symtab_node *node;
1486 count = streamer_read_uhwi (ib);
1487 if (!count)
1488 break;
1489 idx = streamer_read_uhwi (ib);
1490 node = nodes[idx];
1491 while (count)
1493 input_ref (ib, node, nodes);
1494 count--;
1500 static struct gcov_ctr_summary lto_gcov_summary;
1502 /* Input profile_info from IB. */
1503 static void
1504 input_profile_summary (struct lto_input_block *ib,
1505 struct lto_file_decl_data *file_data)
1507 unsigned h_ix;
1508 struct bitpack_d bp;
1509 unsigned int runs = streamer_read_uhwi (ib);
1510 if (runs)
1512 file_data->profile_info.runs = runs;
1513 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1514 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1516 memset (file_data->profile_info.histogram, 0,
1517 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1518 /* Input the bitpack of non-zero histogram indices. */
1519 bp = streamer_read_bitpack (ib);
1520 /* Read in and unpack the full bitpack, flagging non-zero
1521 histogram entries by setting the num_counters non-zero. */
1522 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1524 file_data->profile_info.histogram[h_ix].num_counters
1525 = bp_unpack_value (&bp, 1);
1527 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1529 if (!file_data->profile_info.histogram[h_ix].num_counters)
1530 continue;
1532 file_data->profile_info.histogram[h_ix].num_counters
1533 = streamer_read_gcov_count (ib);
1534 file_data->profile_info.histogram[h_ix].min_value
1535 = streamer_read_gcov_count (ib);
1536 file_data->profile_info.histogram[h_ix].cum_value
1537 = streamer_read_gcov_count (ib);
1539 /* IPA-profile computes hot bb threshold based on cumulated
1540 whole program profile. We need to stream it down to ltrans. */
1541 if (flag_ltrans)
1542 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1547 /* Rescale profile summaries to the same number of runs in the whole unit. */
1549 static void
1550 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1552 struct lto_file_decl_data *file_data;
1553 unsigned int j, h_ix;
1554 gcov_unsigned_t max_runs = 0;
1555 struct cgraph_node *node;
1556 struct cgraph_edge *edge;
1557 gcov_type saved_sum_all = 0;
1558 gcov_ctr_summary *saved_profile_info = 0;
1559 int saved_scale = 0;
1561 /* Find unit with maximal number of runs. If we ever get serious about
1562 roundoff errors, we might also consider computing smallest common
1563 multiply. */
1564 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1565 if (max_runs < file_data->profile_info.runs)
1566 max_runs = file_data->profile_info.runs;
1568 if (!max_runs)
1569 return;
1571 /* Simple overflow check. We probably don't need to support that many train
1572 runs. Such a large value probably imply data corruption anyway. */
1573 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1575 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1576 INT_MAX / REG_BR_PROB_BASE);
1577 return;
1580 profile_info = &lto_gcov_summary;
1581 lto_gcov_summary.runs = max_runs;
1582 lto_gcov_summary.sum_max = 0;
1583 memset (lto_gcov_summary.histogram, 0,
1584 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1586 /* Rescale all units to the maximal number of runs.
1587 sum_max can not be easily merged, as we have no idea what files come from
1588 the same run. We do not use the info anyway, so leave it 0. */
1589 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1590 if (file_data->profile_info.runs)
1592 int scale = GCOV_COMPUTE_SCALE (max_runs,
1593 file_data->profile_info.runs);
1594 lto_gcov_summary.sum_max
1595 = MAX (lto_gcov_summary.sum_max,
1596 apply_scale (file_data->profile_info.sum_max, scale));
1597 lto_gcov_summary.sum_all
1598 = MAX (lto_gcov_summary.sum_all,
1599 apply_scale (file_data->profile_info.sum_all, scale));
1600 /* Save a pointer to the profile_info with the largest
1601 scaled sum_all and the scale for use in merging the
1602 histogram. */
1603 if (!saved_profile_info
1604 || lto_gcov_summary.sum_all > saved_sum_all)
1606 saved_profile_info = &file_data->profile_info;
1607 saved_sum_all = lto_gcov_summary.sum_all;
1608 saved_scale = scale;
1612 gcc_assert (saved_profile_info);
1614 /* Scale up the histogram from the profile that had the largest
1615 scaled sum_all above. */
1616 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1618 /* Scale up the min value as we did the corresponding sum_all
1619 above. Use that to find the new histogram index. */
1620 gcov_type scaled_min
1621 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1622 saved_scale);
1623 /* The new index may be shared with another scaled histogram entry,
1624 so we need to account for a non-zero histogram entry at new_ix. */
1625 unsigned new_ix = gcov_histo_index (scaled_min);
1626 lto_gcov_summary.histogram[new_ix].min_value
1627 = (lto_gcov_summary.histogram[new_ix].num_counters
1628 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1629 : scaled_min);
1630 /* Some of the scaled counter values would ostensibly need to be placed
1631 into different (larger) histogram buckets, but we keep things simple
1632 here and place the scaled cumulative counter value in the bucket
1633 corresponding to the scaled minimum counter value. */
1634 lto_gcov_summary.histogram[new_ix].cum_value
1635 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1636 saved_scale);
1637 lto_gcov_summary.histogram[new_ix].num_counters
1638 += saved_profile_info->histogram[h_ix].num_counters;
1641 /* Watch roundoff errors. */
1642 if (lto_gcov_summary.sum_max < max_runs)
1643 lto_gcov_summary.sum_max = max_runs;
1645 /* If merging already happent at WPA time, we are done. */
1646 if (flag_ltrans)
1647 return;
1649 /* Now compute count_materialization_scale of each node.
1650 During LTRANS we already have values of count_materialization_scale
1651 computed, so just update them. */
1652 FOR_EACH_FUNCTION (node)
1653 if (node->lto_file_data
1654 && node->lto_file_data->profile_info.runs)
1656 int scale;
1658 scale = RDIV (node->count_materialization_scale * max_runs,
1659 node->lto_file_data->profile_info.runs);
1660 node->count_materialization_scale = scale;
1661 if (scale < 0)
1662 fatal_error ("Profile information in %s corrupted",
1663 file_data->file_name);
1665 if (scale == REG_BR_PROB_BASE)
1666 continue;
1667 for (edge = node->callees; edge; edge = edge->next_callee)
1668 edge->count = apply_scale (edge->count, scale);
1669 node->count = apply_scale (node->count, scale);
1673 /* Input and merge the symtab from each of the .o files passed to
1674 lto1. */
1676 void
1677 input_symtab (void)
1679 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1680 struct lto_file_decl_data *file_data;
1681 unsigned int j = 0;
1682 struct cgraph_node *node;
1684 while ((file_data = file_data_vec[j++]))
1686 const char *data;
1687 size_t len;
1688 struct lto_input_block *ib;
1689 vec<symtab_node *> nodes;
1691 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1692 &data, &len);
1693 if (!ib)
1694 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1695 input_profile_summary (ib, file_data);
1696 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1697 nodes = input_cgraph_1 (file_data, ib);
1698 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1699 ib, data, len);
1701 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1702 &data, &len);
1703 if (!ib)
1704 fatal_error ("cannot find LTO section refs in %s",
1705 file_data->file_name);
1706 input_refs (ib, nodes);
1707 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1708 ib, data, len);
1709 if (flag_ltrans)
1710 input_cgraph_opt_summary (nodes);
1711 nodes.release ();
1714 merge_profile_summaries (file_data_vec);
1715 get_working_sets ();
1718 /* Clear out the aux field that was used to store enough state to
1719 tell which nodes should be overwritten. */
1720 FOR_EACH_FUNCTION (node)
1722 /* Some nodes may have been created by cgraph_node. This
1723 happens when the callgraph contains nested functions. If the
1724 node for the parent function was never emitted to the gimple
1725 file, cgraph_node will create a node for it when setting the
1726 context of the nested function. */
1727 if (node->lto_file_data)
1728 node->aux = NULL;
1732 /* True when we need optimization summary for NODE. */
1734 static int
1735 output_cgraph_opt_summary_p (struct cgraph_node *node)
1737 return (node->clone_of
1738 && (node->clone.tree_map
1739 || node->clone.args_to_skip
1740 || node->clone.combined_args_to_skip));
1743 /* Output optimization summary for EDGE to OB. */
1744 static void
1745 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1746 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1750 /* Output optimization summary for NODE to OB. */
1752 static void
1753 output_node_opt_summary (struct output_block *ob,
1754 struct cgraph_node *node,
1755 lto_symtab_encoder_t encoder)
1757 unsigned int index;
1758 bitmap_iterator bi;
1759 struct ipa_replace_map *map;
1760 struct bitpack_d bp;
1761 int i;
1762 struct cgraph_edge *e;
1764 if (node->clone.args_to_skip)
1766 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1767 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1768 streamer_write_uhwi (ob, index);
1770 else
1771 streamer_write_uhwi (ob, 0);
1772 if (node->clone.combined_args_to_skip)
1774 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1775 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1776 streamer_write_uhwi (ob, index);
1778 else
1779 streamer_write_uhwi (ob, 0);
1780 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1781 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1783 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1784 mechanism to store function local declarations into summaries. */
1785 gcc_assert (!map->old_tree);
1786 streamer_write_uhwi (ob, map->parm_num);
1787 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1788 stream_write_tree (ob, map->new_tree, true);
1789 bp = bitpack_create (ob->main_stream);
1790 bp_pack_value (&bp, map->replace_p, 1);
1791 bp_pack_value (&bp, map->ref_p, 1);
1792 streamer_write_bitpack (&bp);
1795 if (lto_symtab_encoder_in_partition_p (encoder, node))
1797 for (e = node->callees; e; e = e->next_callee)
1798 output_edge_opt_summary (ob, e);
1799 for (e = node->indirect_calls; e; e = e->next_callee)
1800 output_edge_opt_summary (ob, e);
1804 /* Output optimization summaries stored in callgraph.
1805 At the moment it is the clone info structure. */
1807 static void
1808 output_cgraph_opt_summary (void)
1810 int i, n_nodes;
1811 lto_symtab_encoder_t encoder;
1812 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1813 unsigned count = 0;
1815 ob->symbol = NULL;
1816 encoder = ob->decl_state->symtab_node_encoder;
1817 n_nodes = lto_symtab_encoder_size (encoder);
1818 for (i = 0; i < n_nodes; i++)
1820 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1821 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1822 if (cnode && output_cgraph_opt_summary_p (cnode))
1823 count++;
1825 streamer_write_uhwi (ob, count);
1826 for (i = 0; i < n_nodes; i++)
1828 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1829 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1830 if (cnode && output_cgraph_opt_summary_p (cnode))
1832 streamer_write_uhwi (ob, i);
1833 output_node_opt_summary (ob, cnode, encoder);
1836 produce_asm (ob, NULL);
1837 destroy_output_block (ob);
1840 /* Input optimisation summary of EDGE. */
1842 static void
1843 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
1844 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
1848 /* Input optimisation summary of NODE. */
1850 static void
1851 input_node_opt_summary (struct cgraph_node *node,
1852 struct lto_input_block *ib_main,
1853 struct data_in *data_in)
1855 int i;
1856 int count;
1857 int bit;
1858 struct bitpack_d bp;
1859 struct cgraph_edge *e;
1861 count = streamer_read_uhwi (ib_main);
1862 if (count)
1863 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1864 for (i = 0; i < count; i++)
1866 bit = streamer_read_uhwi (ib_main);
1867 bitmap_set_bit (node->clone.args_to_skip, bit);
1869 count = streamer_read_uhwi (ib_main);
1870 if (count)
1871 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1872 for (i = 0; i < count; i++)
1874 bit = streamer_read_uhwi (ib_main);
1875 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1877 count = streamer_read_uhwi (ib_main);
1878 for (i = 0; i < count; i++)
1880 struct ipa_replace_map *map = ggc_alloc<ipa_replace_map> ();
1882 vec_safe_push (node->clone.tree_map, map);
1883 map->parm_num = streamer_read_uhwi (ib_main);
1884 map->old_tree = NULL;
1885 map->new_tree = stream_read_tree (ib_main, data_in);
1886 bp = streamer_read_bitpack (ib_main);
1887 map->replace_p = bp_unpack_value (&bp, 1);
1888 map->ref_p = bp_unpack_value (&bp, 1);
1890 for (e = node->callees; e; e = e->next_callee)
1891 input_edge_opt_summary (e, ib_main);
1892 for (e = node->indirect_calls; e; e = e->next_callee)
1893 input_edge_opt_summary (e, ib_main);
1896 /* Read section in file FILE_DATA of length LEN with data DATA. */
1898 static void
1899 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1900 const char *data, size_t len,
1901 vec<symtab_node *> nodes)
1903 const struct lto_function_header *header =
1904 (const struct lto_function_header *) data;
1905 const int cfg_offset = sizeof (struct lto_function_header);
1906 const int main_offset = cfg_offset + header->cfg_size;
1907 const int string_offset = main_offset + header->main_size;
1908 struct data_in *data_in;
1909 unsigned int i;
1910 unsigned int count;
1912 lto_input_block ib_main ((const char *) data + main_offset,
1913 header->main_size);
1915 data_in =
1916 lto_data_in_create (file_data, (const char *) data + string_offset,
1917 header->string_size, vNULL);
1918 count = streamer_read_uhwi (&ib_main);
1920 for (i = 0; i < count; i++)
1922 int ref = streamer_read_uhwi (&ib_main);
1923 input_node_opt_summary (dyn_cast<cgraph_node *> (nodes[ref]),
1924 &ib_main, data_in);
1926 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1927 len);
1928 lto_data_in_delete (data_in);
1931 /* Input optimization summary of cgraph. */
1933 static void
1934 input_cgraph_opt_summary (vec<symtab_node *> nodes)
1936 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1937 struct lto_file_decl_data *file_data;
1938 unsigned int j = 0;
1940 while ((file_data = file_data_vec[j++]))
1942 size_t len;
1943 const char *data =
1944 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1945 &len);
1947 if (data)
1948 input_cgraph_opt_section (file_data, data, len, nodes);