Remove a trivial assert (missed in previous checkin)
[official-gcc.git] / gcc / lto-cgraph.c
blobfcba1b92acd0b9dbbd697164b7fafc09b042275f
1 /* Write and read the cgraph to the memory mapped representation of a
2 .o file.
4 Copyright (C) 2009-2013 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 "expr.h"
29 #include "flags.h"
30 #include "params.h"
31 #include "input.h"
32 #include "hashtab.h"
33 #include "langhooks.h"
34 #include "basic-block.h"
35 #include "tree-flow.h"
36 #include "cgraph.h"
37 #include "function.h"
38 #include "ggc.h"
39 #include "diagnostic-core.h"
40 #include "except.h"
41 #include "vec.h"
42 #include "timevar.h"
43 #include "pointer-set.h"
44 #include "lto-streamer.h"
45 #include "data-streamer.h"
46 #include "tree-streamer.h"
47 #include "gcov-io.h"
48 #include "tree-pass.h"
49 #include "profile.h"
50 #include "context.h"
51 #include "pass_manager.h"
53 static void output_cgraph_opt_summary (void);
54 static void input_cgraph_opt_summary (vec<symtab_node> nodes);
56 /* Number of LDPR values known to GCC. */
57 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
59 /* All node orders are ofsetted by ORDER_BASE. */
60 static int order_base;
62 /* Cgraph streaming is organized as set of record whose type
63 is indicated by a tag. */
64 enum LTO_symtab_tags
66 /* Must leave 0 for the stopper. */
68 /* Cgraph node without body available. */
69 LTO_symtab_unavail_node = 1,
70 /* Cgraph node with function body. */
71 LTO_symtab_analyzed_node,
72 /* Cgraph edges. */
73 LTO_symtab_edge,
74 LTO_symtab_indirect_edge,
75 LTO_symtab_variable,
76 LTO_symtab_last_tag
79 /* Create a new symtab encoder.
80 if FOR_INPUT, the encoder allocate only datastructures needed
81 to read the symtab. */
83 lto_symtab_encoder_t
84 lto_symtab_encoder_new (bool for_input)
86 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
88 if (!for_input)
89 encoder->map = pointer_map_create ();
90 encoder->nodes.create (0);
91 return encoder;
95 /* Delete ENCODER and its components. */
97 void
98 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
100 encoder->nodes.release ();
101 if (encoder->map)
102 pointer_map_destroy (encoder->map);
103 free (encoder);
107 /* Return the existing reference number of NODE in the symtab encoder in
108 output block OB. Assign a new reference if this is the first time
109 NODE is encoded. */
112 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
113 symtab_node node)
115 int ref;
116 void **slot;
118 if (!encoder->map)
120 lto_encoder_entry entry = {node, false, false, false};
122 ref = encoder->nodes.length ();
123 encoder->nodes.safe_push (entry);
124 return ref;
127 slot = pointer_map_contains (encoder->map, node);
128 if (!slot || !*slot)
130 lto_encoder_entry entry = {node, false, false, false};
131 ref = encoder->nodes.length ();
132 if (!slot)
133 slot = pointer_map_insert (encoder->map, node);
134 *slot = (void *) (intptr_t) (ref + 1);
135 encoder->nodes.safe_push (entry);
137 else
138 ref = (size_t) *slot - 1;
140 return ref;
143 /* Remove NODE from encoder. */
145 bool
146 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
147 symtab_node node)
149 void **slot, **last_slot;
150 int index;
151 lto_encoder_entry last_node;
153 slot = pointer_map_contains (encoder->map, node);
154 if (slot == NULL || !*slot)
155 return false;
157 index = (size_t) *slot - 1;
158 gcc_checking_assert (encoder->nodes[index].node == node);
160 /* Remove from vector. We do this by swapping node with the last element
161 of the vector. */
162 last_node = encoder->nodes.pop ();
163 if (last_node.node != node)
165 last_slot = pointer_map_contains (encoder->map, last_node.node);
166 gcc_checking_assert (last_slot && *last_slot);
167 *last_slot = (void *)(size_t) (index + 1);
169 /* Move the last element to the original spot of NODE. */
170 encoder->nodes[index] = last_node;
173 /* Remove element from hash table. */
174 *slot = NULL;
175 return true;
179 /* Return TRUE if we should encode initializer of NODE (if any). */
181 bool
182 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
183 struct cgraph_node *node)
185 int index = lto_symtab_encoder_lookup (encoder, (symtab_node)node);
186 return encoder->nodes[index].body;
189 /* Return TRUE if we should encode body of NODE (if any). */
191 static void
192 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
193 struct cgraph_node *node)
195 int index = lto_symtab_encoder_encode (encoder, (symtab_node)node);
196 gcc_checking_assert (encoder->nodes[index].node == (symtab_node)node);
197 encoder->nodes[index].body = true;
200 /* Return TRUE if we should encode initializer of NODE (if any). */
202 bool
203 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
204 struct varpool_node *node)
206 int index = lto_symtab_encoder_lookup (encoder, (symtab_node)node);
207 if (index == LCC_NOT_FOUND)
208 return false;
209 return encoder->nodes[index].initializer;
212 /* Return TRUE if we should encode initializer of NODE (if any). */
214 static void
215 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
216 struct varpool_node *node)
218 int index = lto_symtab_encoder_lookup (encoder, (symtab_node)node);
219 encoder->nodes[index].initializer = true;
222 /* Return TRUE if we should encode initializer of NODE (if any). */
224 bool
225 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
226 symtab_node node)
228 int index = lto_symtab_encoder_lookup (encoder, (symtab_node)node);
229 if (index == LCC_NOT_FOUND)
230 return false;
231 return encoder->nodes[index].in_partition;
234 /* Return TRUE if we should encode body of NODE (if any). */
236 void
237 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
238 symtab_node node)
240 int index = lto_symtab_encoder_encode (encoder, (symtab_node)node);
241 encoder->nodes[index].in_partition = true;
244 /* Output the cgraph EDGE to OB using ENCODER. */
246 static void
247 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
248 lto_symtab_encoder_t encoder)
250 unsigned int uid;
251 intptr_t ref;
252 struct bitpack_d bp;
254 if (edge->indirect_unknown_callee)
255 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
256 LTO_symtab_indirect_edge);
257 else
258 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
259 LTO_symtab_edge);
261 ref = lto_symtab_encoder_lookup (encoder, (symtab_node)edge->caller);
262 gcc_assert (ref != LCC_NOT_FOUND);
263 streamer_write_hwi_stream (ob->main_stream, ref);
265 if (!edge->indirect_unknown_callee)
267 ref = lto_symtab_encoder_lookup (encoder, (symtab_node)edge->callee);
268 gcc_assert (ref != LCC_NOT_FOUND);
269 streamer_write_hwi_stream (ob->main_stream, ref);
272 streamer_write_gcov_count_stream (ob->main_stream, edge->count);
274 bp = bitpack_create (ob->main_stream);
275 uid = (!gimple_has_body_p (edge->caller->symbol.decl)
276 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt) + 1);
277 bp_pack_enum (&bp, cgraph_inline_failed_enum,
278 CIF_N_REASONS, edge->inline_failed);
279 bp_pack_var_len_unsigned (&bp, uid);
280 bp_pack_var_len_unsigned (&bp, edge->frequency);
281 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
282 bp_pack_value (&bp, edge->speculative, 1);
283 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
284 bp_pack_value (&bp, edge->can_throw_external, 1);
285 if (edge->indirect_unknown_callee)
287 int flags = edge->indirect_info->ecf_flags;
288 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
289 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
290 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
291 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
292 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
293 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
294 /* Flags that should not appear on indirect calls. */
295 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
296 | ECF_MAY_BE_ALLOCA
297 | ECF_SIBCALL
298 | ECF_LEAF
299 | ECF_NOVOPS)));
301 streamer_write_bitpack (&bp);
302 if (edge->indirect_unknown_callee)
304 streamer_write_hwi_stream (ob->main_stream,
305 edge->indirect_info->common_target_id);
306 if (edge->indirect_info->common_target_id)
307 streamer_write_hwi_stream
308 (ob->main_stream, edge->indirect_info->common_target_probability);
312 /* Return if LIST contain references from other partitions. */
314 bool
315 referenced_from_other_partition_p (struct ipa_ref_list *list, lto_symtab_encoder_t encoder)
317 int i;
318 struct ipa_ref *ref;
319 for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
321 if (ref->referring->symbol.in_other_partition
322 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
323 return true;
325 return false;
328 /* Return true when node is reachable from other partition. */
330 bool
331 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
333 struct cgraph_edge *e;
334 if (!node->symbol.definition)
335 return false;
336 if (node->global.inlined_to)
337 return false;
338 for (e = node->callers; e; e = e->next_caller)
339 if (e->caller->symbol.in_other_partition
340 || !lto_symtab_encoder_in_partition_p (encoder, (symtab_node)e->caller))
341 return true;
342 return false;
345 /* Return if LIST contain references from other partitions. */
347 bool
348 referenced_from_this_partition_p (struct ipa_ref_list *list,
349 lto_symtab_encoder_t encoder)
351 int i;
352 struct ipa_ref *ref;
353 for (i = 0; ipa_ref_list_referring_iterate (list, i, ref); i++)
354 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
355 return true;
356 return false;
359 /* Return true when node is reachable from other partition. */
361 bool
362 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
364 struct cgraph_edge *e;
365 for (e = node->callers; e; e = e->next_caller)
366 if (lto_symtab_encoder_in_partition_p (encoder, (symtab_node)e->caller))
367 return true;
368 return false;
371 /* Output the cgraph NODE to OB. ENCODER is used to find the
372 reference number of NODE->inlined_to. SET is the set of nodes we
373 are writing to the current file. If NODE is not in SET, then NODE
374 is a boundary of a cgraph_node_set and we pretend NODE just has a
375 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
376 that have had their callgraph node written so far. This is used to
377 determine if NODE is a clone of a previously written node. */
379 static void
380 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
381 lto_symtab_encoder_t encoder)
383 unsigned int tag;
384 struct bitpack_d bp;
385 bool boundary_p;
386 intptr_t ref;
387 bool in_other_partition = false;
388 struct cgraph_node *clone_of, *ultimate_clone_of;
389 struct ipa_opt_pass_d *pass;
390 int i;
391 bool alias_p;
393 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, (symtab_node)node);
395 if (node->symbol.analyzed && !boundary_p)
396 tag = LTO_symtab_analyzed_node;
397 else
398 tag = LTO_symtab_unavail_node;
400 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
401 tag);
402 streamer_write_hwi_stream (ob->main_stream, node->symbol.order);
404 /* In WPA mode, we only output part of the call-graph. Also, we
405 fake cgraph node attributes. There are two cases that we care.
407 Boundary nodes: There are nodes that are not part of SET but are
408 called from within SET. We artificially make them look like
409 externally visible nodes with no function body.
411 Cherry-picked nodes: These are nodes we pulled from other
412 translation units into SET during IPA-inlining. We make them as
413 local static nodes to prevent clashes with other local statics. */
414 if (boundary_p && node->symbol.analyzed && !DECL_EXTERNAL (node->symbol.decl))
416 /* Inline clones can not be part of boundary.
417 gcc_assert (!node->global.inlined_to);
419 FIXME: At the moment they can be, when partition contains an inline
420 clone that is clone of inline clone from outside partition. We can
421 reshape the clone tree and make other tree to be the root, but it
422 needs a bit extra work and will be promplty done by cgraph_remove_node
423 after reading back. */
424 in_other_partition = 1;
427 clone_of = node->clone_of;
428 while (clone_of
429 && (ref = lto_symtab_encoder_lookup (encoder, (symtab_node)clone_of)) == LCC_NOT_FOUND)
430 if (clone_of->prev_sibling_clone)
431 clone_of = clone_of->prev_sibling_clone;
432 else
433 clone_of = clone_of->clone_of;
435 /* See if body of the master function is output. If not, we are seeing only
436 an declaration and we do not need to pass down clone tree. */
437 ultimate_clone_of = clone_of;
438 while (ultimate_clone_of && ultimate_clone_of->clone_of)
439 ultimate_clone_of = ultimate_clone_of->clone_of;
441 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
442 clone_of = NULL;
444 if (tag == LTO_symtab_analyzed_node)
445 gcc_assert (clone_of || !node->clone_of);
446 if (!clone_of)
447 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
448 else
449 streamer_write_hwi_stream (ob->main_stream, ref);
452 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->symbol.decl);
453 streamer_write_gcov_count_stream (ob->main_stream, node->count);
454 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
456 streamer_write_hwi_stream (ob->main_stream,
457 node->ipa_transforms_to_apply.length ());
458 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
459 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
461 if (tag == LTO_symtab_analyzed_node)
463 if (node->global.inlined_to)
465 ref = lto_symtab_encoder_lookup (encoder, (symtab_node)node->global.inlined_to);
466 gcc_assert (ref != LCC_NOT_FOUND);
468 else
469 ref = LCC_NOT_FOUND;
471 streamer_write_hwi_stream (ob->main_stream, ref);
474 if (node->symbol.same_comdat_group && !boundary_p)
476 ref = lto_symtab_encoder_lookup (encoder,
477 node->symbol.same_comdat_group);
478 gcc_assert (ref != LCC_NOT_FOUND);
480 else
481 ref = LCC_NOT_FOUND;
482 streamer_write_hwi_stream (ob->main_stream, ref);
484 bp = bitpack_create (ob->main_stream);
485 bp_pack_value (&bp, node->local.local, 1);
486 bp_pack_value (&bp, node->symbol.externally_visible, 1);
487 bp_pack_value (&bp, node->symbol.definition, 1);
488 bp_pack_value (&bp, node->local.versionable, 1);
489 bp_pack_value (&bp, node->local.can_change_signature, 1);
490 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
491 bp_pack_value (&bp, node->symbol.force_output, 1);
492 bp_pack_value (&bp, node->symbol.forced_by_abi, 1);
493 bp_pack_value (&bp, node->symbol.unique_name, 1);
494 bp_pack_value (&bp, node->symbol.address_taken, 1);
495 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
496 && !DECL_EXTERNAL (node->symbol.decl)
497 && !DECL_COMDAT (node->symbol.decl)
498 && (reachable_from_other_partition_p (node, encoder)
499 || referenced_from_other_partition_p (&node->symbol.ref_list,
500 encoder)), 1);
501 bp_pack_value (&bp, node->lowered, 1);
502 bp_pack_value (&bp, in_other_partition, 1);
503 /* Real aliases in a boundary become non-aliases. However we still stream
504 alias info on weakrefs.
505 TODO: We lose a bit of information here - when we know that variable is
506 defined in other unit, we may use the info on aliases to resolve
507 symbol1 != symbol2 type tests that we can do only for locally defined objects
508 otherwise. */
509 alias_p = node->symbol.alias && (!boundary_p || node->symbol.weakref);
510 bp_pack_value (&bp, alias_p, 1);
511 bp_pack_value (&bp, node->symbol.weakref, 1);
512 bp_pack_value (&bp, node->frequency, 2);
513 bp_pack_value (&bp, node->only_called_at_startup, 1);
514 bp_pack_value (&bp, node->only_called_at_exit, 1);
515 bp_pack_value (&bp, node->tm_clone, 1);
516 bp_pack_value (&bp, node->thunk.thunk_p && !boundary_p, 1);
517 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
518 LDPR_NUM_KNOWN, node->symbol.resolution);
519 streamer_write_bitpack (&bp);
521 if (node->thunk.thunk_p && !boundary_p)
523 streamer_write_uhwi_stream
524 (ob->main_stream,
525 1 + (node->thunk.this_adjusting != 0) * 2
526 + (node->thunk.virtual_offset_p != 0) * 4);
527 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
528 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
530 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
533 /* Output the varpool NODE to OB.
534 If NODE is not in SET, then NODE is a boundary. */
536 static void
537 lto_output_varpool_node (struct lto_simple_output_block *ob, struct varpool_node *node,
538 lto_symtab_encoder_t encoder)
540 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, (symtab_node)node);
541 struct bitpack_d bp;
542 int ref;
543 bool alias_p;
545 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
546 LTO_symtab_variable);
547 streamer_write_hwi_stream (ob->main_stream, node->symbol.order);
548 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->symbol.decl);
549 bp = bitpack_create (ob->main_stream);
550 bp_pack_value (&bp, node->symbol.externally_visible, 1);
551 bp_pack_value (&bp, node->symbol.force_output, 1);
552 bp_pack_value (&bp, node->symbol.forced_by_abi, 1);
553 bp_pack_value (&bp, node->symbol.unique_name, 1);
554 bp_pack_value (&bp, node->symbol.definition, 1);
555 alias_p = node->symbol.alias && (!boundary_p || node->symbol.weakref);
556 bp_pack_value (&bp, alias_p, 1);
557 bp_pack_value (&bp, node->symbol.weakref, 1);
558 bp_pack_value (&bp, node->symbol.analyzed && !boundary_p, 1);
559 gcc_assert (node->symbol.definition || !node->symbol.analyzed);
560 /* Constant pool initializers can be de-unified into individual ltrans units.
561 FIXME: Alternatively at -Os we may want to avoid generating for them the local
562 labels and share them across LTRANS partitions. */
563 if (DECL_IN_CONSTANT_POOL (node->symbol.decl)
564 && !DECL_EXTERNAL (node->symbol.decl)
565 && !DECL_COMDAT (node->symbol.decl))
567 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
568 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
570 else
572 bp_pack_value (&bp, node->symbol.definition
573 && referenced_from_other_partition_p (&node->symbol.ref_list,
574 encoder), 1);
575 bp_pack_value (&bp, node->symbol.analyzed
576 && boundary_p && !DECL_EXTERNAL (node->symbol.decl), 1);
577 /* in_other_partition. */
579 streamer_write_bitpack (&bp);
580 if (node->symbol.same_comdat_group && !boundary_p)
582 ref = lto_symtab_encoder_lookup (encoder,
583 node->symbol.same_comdat_group);
584 gcc_assert (ref != LCC_NOT_FOUND);
586 else
587 ref = LCC_NOT_FOUND;
588 streamer_write_hwi_stream (ob->main_stream, ref);
589 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
590 LDPR_NUM_KNOWN, node->symbol.resolution);
593 /* Output the varpool NODE to OB.
594 If NODE is not in SET, then NODE is a boundary. */
596 static void
597 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
598 lto_symtab_encoder_t encoder)
600 struct bitpack_d bp;
601 int nref;
602 int uid = ref->lto_stmt_uid;
603 struct cgraph_node *node;
605 bp = bitpack_create (ob->main_stream);
606 bp_pack_value (&bp, ref->use, 2);
607 bp_pack_value (&bp, ref->speculative, 1);
608 streamer_write_bitpack (&bp);
609 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
610 gcc_assert (nref != LCC_NOT_FOUND);
611 streamer_write_hwi_stream (ob->main_stream, nref);
613 node = dyn_cast <cgraph_node> (ref->referring);
614 if (node)
616 if (ref->stmt)
617 uid = gimple_uid (ref->stmt) + 1;
618 streamer_write_hwi_stream (ob->main_stream, uid);
622 /* Stream out profile_summary to OB. */
624 static void
625 output_profile_summary (struct lto_simple_output_block *ob)
627 unsigned h_ix;
628 struct bitpack_d bp;
630 if (profile_info)
632 /* We do not output num and run_max, they are not used by
633 GCC profile feedback and they are difficult to merge from multiple
634 units. */
635 gcc_assert (profile_info->runs);
636 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
637 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
639 /* sum_all is needed for computing the working set with the
640 histogram. */
641 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
643 /* Create and output a bitpack of non-zero histogram entries indices. */
644 bp = bitpack_create (ob->main_stream);
645 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
646 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
647 streamer_write_bitpack (&bp);
648 /* Now stream out only those non-zero entries. */
649 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
651 if (!profile_info->histogram[h_ix].num_counters)
652 continue;
653 streamer_write_gcov_count_stream (ob->main_stream,
654 profile_info->histogram[h_ix].num_counters);
655 streamer_write_gcov_count_stream (ob->main_stream,
656 profile_info->histogram[h_ix].min_value);
657 streamer_write_gcov_count_stream (ob->main_stream,
658 profile_info->histogram[h_ix].cum_value);
660 /* IPA-profile computes hot bb threshold based on cumulated
661 whole program profile. We need to stream it down to ltrans. */
662 if (flag_wpa)
663 streamer_write_gcov_count_stream (ob->main_stream,
664 get_hot_bb_threshold ());
666 else
667 streamer_write_uhwi_stream (ob->main_stream, 0);
670 /* Output all callees or indirect outgoing edges. EDGE must be the first such
671 edge. */
673 static void
674 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
675 struct lto_simple_output_block *ob,
676 lto_symtab_encoder_t encoder)
678 if (!edge)
679 return;
681 /* Output edges in backward direction, so the reconstructed callgraph match
682 and it is easy to associate call sites in the IPA pass summaries. */
683 while (edge->next_callee)
684 edge = edge->next_callee;
685 for (; edge; edge = edge->prev_callee)
686 lto_output_edge (ob, edge, encoder);
689 /* Output the part of the cgraph in SET. */
691 static void
692 output_refs (lto_symtab_encoder_t encoder)
694 lto_symtab_encoder_iterator lsei;
695 struct lto_simple_output_block *ob;
696 int count;
697 struct ipa_ref *ref;
698 int i;
700 ob = lto_create_simple_output_block (LTO_section_refs);
702 for (lsei = lsei_start_in_partition (encoder); !lsei_end_p (lsei);
703 lsei_next_in_partition (&lsei))
705 symtab_node node = lsei_node (lsei);
707 count = ipa_ref_list_nreferences (&node->symbol.ref_list);
708 if (count)
710 streamer_write_gcov_count_stream (ob->main_stream, count);
711 streamer_write_uhwi_stream (ob->main_stream,
712 lto_symtab_encoder_lookup (encoder, node));
713 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list,
714 i, ref); i++)
715 lto_output_ref (ob, ref, encoder);
719 streamer_write_uhwi_stream (ob->main_stream, 0);
721 lto_destroy_simple_output_block (ob);
724 /* Add NODE into encoder as well as nodes it is cloned from.
725 Do it in a way so clones appear first. */
727 static void
728 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
729 bool include_body)
731 if (node->clone_of)
732 add_node_to (encoder, node->clone_of, include_body);
733 else if (include_body)
734 lto_set_symtab_encoder_encode_body (encoder, node);
735 lto_symtab_encoder_encode (encoder, (symtab_node)node);
738 /* Add all references in LIST to encoders. */
740 static void
741 add_references (lto_symtab_encoder_t encoder,
742 struct ipa_ref_list *list)
744 int i;
745 struct ipa_ref *ref;
746 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
747 if (is_a <cgraph_node> (ref->referred))
748 add_node_to (encoder, ipa_ref_node (ref), false);
749 else
750 lto_symtab_encoder_encode (encoder, ref->referred);
753 /* Find all symbols we want to stream into given partition and insert them
754 to encoders.
756 The function actually replaces IN_ENCODER by new one. The reason is that
757 streaming code needs clone's origin to be streamed before clone. This
758 means that we need to insert the nodes in specific order. This order is
759 ignored by the partitioning logic earlier. */
761 lto_symtab_encoder_t
762 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
764 struct cgraph_node *node;
765 struct cgraph_edge *edge;
766 int i;
767 lto_symtab_encoder_t encoder;
768 lto_symtab_encoder_iterator lsei;
770 encoder = lto_symtab_encoder_new (false);
772 /* Go over all entries in the IN_ENCODER and duplicate them to
773 ENCODER. At the same time insert masters of clones so
774 every master appears before clone. */
775 for (lsei = lsei_start_function_in_partition (in_encoder);
776 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
778 node = lsei_cgraph_node (lsei);
779 add_node_to (encoder, node, true);
780 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)node);
781 add_references (encoder, &node->symbol.ref_list);
782 /* For proper debug info, we need to ship the origins, too. */
783 if (DECL_ABSTRACT_ORIGIN (node->symbol.decl))
785 struct cgraph_node *origin_node
786 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (node->symbol.decl));
787 add_node_to (encoder, origin_node, true);
790 for (lsei = lsei_start_variable_in_partition (in_encoder);
791 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
793 struct varpool_node *vnode = lsei_varpool_node (lsei);
795 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)vnode);
796 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
797 add_references (encoder, &vnode->symbol.ref_list);
798 /* For proper debug info, we need to ship the origins, too. */
799 if (DECL_ABSTRACT_ORIGIN (vnode->symbol.decl))
801 struct varpool_node *origin_node
802 = varpool_get_node (DECL_ABSTRACT_ORIGIN (node->symbol.decl));
803 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)origin_node);
806 /* Pickle in also the initializer of all referenced readonly variables
807 to help folding. Constant pool variables are not shared, so we must
808 pickle those too. */
809 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
811 symtab_node node = lto_symtab_encoder_deref (encoder, i);
812 if (varpool_node *vnode = dyn_cast <varpool_node> (node))
814 if (!lto_symtab_encoder_encode_initializer_p (encoder,
815 vnode)
816 && ctor_for_folding (vnode->symbol.decl) != error_mark_node)
818 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
819 add_references (encoder, &vnode->symbol.ref_list);
824 /* Go over all the nodes again to include callees that are not in
825 SET. */
826 for (lsei = lsei_start_function_in_partition (encoder);
827 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
829 node = lsei_cgraph_node (lsei);
830 for (edge = node->callees; edge; edge = edge->next_callee)
832 struct cgraph_node *callee = edge->callee;
833 if (!lto_symtab_encoder_in_partition_p (encoder, (symtab_node)callee))
835 /* We should have moved all the inlines. */
836 gcc_assert (!callee->global.inlined_to);
837 add_node_to (encoder, callee, false);
841 lto_symtab_encoder_delete (in_encoder);
842 return encoder;
845 /* Output the part of the symtab in SET and VSET. */
847 void
848 output_symtab (void)
850 struct cgraph_node *node;
851 struct lto_simple_output_block *ob;
852 lto_symtab_encoder_iterator lsei;
853 int i, n_nodes;
854 lto_symtab_encoder_t encoder;
855 static bool asm_nodes_output = false;
857 if (flag_wpa)
858 output_cgraph_opt_summary ();
860 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
862 output_profile_summary (ob);
864 /* An encoder for cgraph nodes should have been created by
865 ipa_write_summaries_1. */
866 gcc_assert (ob->decl_state->symtab_node_encoder);
867 encoder = ob->decl_state->symtab_node_encoder;
869 /* Write out the nodes. We must first output a node and then its clones,
870 otherwise at a time reading back the node there would be nothing to clone
871 from. */
872 n_nodes = lto_symtab_encoder_size (encoder);
873 for (i = 0; i < n_nodes; i++)
875 symtab_node node = lto_symtab_encoder_deref (encoder, i);
876 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
877 lto_output_node (ob, cnode, encoder);
878 else
879 lto_output_varpool_node (ob, varpool (node), encoder);
883 /* Go over the nodes in SET again to write edges. */
884 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
885 lsei_next_function_in_partition (&lsei))
887 node = lsei_cgraph_node (lsei);
888 output_outgoing_cgraph_edges (node->callees, ob, encoder);
889 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
892 streamer_write_uhwi_stream (ob->main_stream, 0);
894 lto_destroy_simple_output_block (ob);
896 /* Emit toplevel asms.
897 When doing WPA we must output every asm just once. Since we do not partition asm
898 nodes at all, output them to first output. This is kind of hack, but should work
899 well. */
900 if (!asm_nodes_output)
902 asm_nodes_output = true;
903 lto_output_toplevel_asms ();
906 output_refs (encoder);
909 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
910 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
911 NODE or to replace the values in it, for instance because the first
912 time we saw it, the function body was not available but now it
913 is. BP is a bitpack with all the bitflags for NODE read from the
914 stream. */
916 static void
917 input_overwrite_node (struct lto_file_decl_data *file_data,
918 struct cgraph_node *node,
919 enum LTO_symtab_tags tag,
920 struct bitpack_d *bp)
922 node->symbol.aux = (void *) tag;
923 node->symbol.lto_file_data = file_data;
925 node->local.local = bp_unpack_value (bp, 1);
926 node->symbol.externally_visible = bp_unpack_value (bp, 1);
927 node->symbol.definition = bp_unpack_value (bp, 1);
928 node->local.versionable = bp_unpack_value (bp, 1);
929 node->local.can_change_signature = bp_unpack_value (bp, 1);
930 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
931 node->symbol.force_output = bp_unpack_value (bp, 1);
932 node->symbol.forced_by_abi = bp_unpack_value (bp, 1);
933 node->symbol.unique_name = bp_unpack_value (bp, 1);
934 node->symbol.address_taken = bp_unpack_value (bp, 1);
935 node->symbol.used_from_other_partition = bp_unpack_value (bp, 1);
936 node->lowered = bp_unpack_value (bp, 1);
937 node->symbol.analyzed = tag == LTO_symtab_analyzed_node;
938 node->symbol.in_other_partition = bp_unpack_value (bp, 1);
939 if (node->symbol.in_other_partition
940 /* Avoid updating decl when we are seeing just inline clone.
941 When inlining function that has functions already inlined into it,
942 we produce clones of inline clones.
944 WPA partitioning might put each clone into different unit and
945 we might end up streaming inline clone from other partition
946 to support clone we are interested in. */
947 && (!node->clone_of
948 || node->clone_of->symbol.decl != node->symbol.decl))
950 DECL_EXTERNAL (node->symbol.decl) = 1;
951 TREE_STATIC (node->symbol.decl) = 0;
953 node->symbol.alias = bp_unpack_value (bp, 1);
954 node->symbol.weakref = bp_unpack_value (bp, 1);
955 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
956 node->only_called_at_startup = bp_unpack_value (bp, 1);
957 node->only_called_at_exit = bp_unpack_value (bp, 1);
958 node->tm_clone = bp_unpack_value (bp, 1);
959 node->thunk.thunk_p = bp_unpack_value (bp, 1);
960 node->symbol.resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
961 LDPR_NUM_KNOWN);
964 /* Return string alias is alias of. */
966 static tree
967 get_alias_symbol (tree decl)
969 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
970 return get_identifier (TREE_STRING_POINTER
971 (TREE_VALUE (TREE_VALUE (alias))));
974 /* Read a node from input_block IB. TAG is the node's tag just read.
975 Return the node read or overwriten. */
977 static struct cgraph_node *
978 input_node (struct lto_file_decl_data *file_data,
979 struct lto_input_block *ib,
980 enum LTO_symtab_tags tag,
981 vec<symtab_node> nodes)
983 gcc::pass_manager *passes = g->get_passes ();
984 tree fn_decl;
985 struct cgraph_node *node;
986 struct bitpack_d bp;
987 unsigned decl_index;
988 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
989 int clone_ref;
990 int order;
991 int i, count;
993 order = streamer_read_hwi (ib) + order_base;
994 clone_ref = streamer_read_hwi (ib);
996 decl_index = streamer_read_uhwi (ib);
997 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
999 if (clone_ref != LCC_NOT_FOUND)
1001 node = cgraph_clone_node (cgraph (nodes[clone_ref]), fn_decl,
1002 0, CGRAPH_FREQ_BASE, false,
1003 vNULL, false, NULL);
1005 else
1007 /* Declaration of functions can be already merged with a declaration
1008 from other input file. We keep cgraph unmerged until after streaming
1009 of ipa passes is done. Alays forcingly create a fresh node. */
1010 node = cgraph_create_empty_node ();
1011 node->symbol.decl = fn_decl;
1012 symtab_register_node ((symtab_node)node);
1015 node->symbol.order = order;
1016 if (order >= symtab_order)
1017 symtab_order = order + 1;
1019 node->count = streamer_read_gcov_count (ib);
1020 node->count_materialization_scale = streamer_read_hwi (ib);
1022 count = streamer_read_hwi (ib);
1023 node->ipa_transforms_to_apply = vNULL;
1024 for (i = 0; i < count; i++)
1026 struct opt_pass *pass;
1027 int pid = streamer_read_hwi (ib);
1029 gcc_assert (pid < passes->passes_by_id_size);
1030 pass = passes->passes_by_id[pid];
1031 node->ipa_transforms_to_apply.safe_push ((struct ipa_opt_pass_d *) pass);
1034 if (tag == LTO_symtab_analyzed_node)
1035 ref = streamer_read_hwi (ib);
1037 ref2 = streamer_read_hwi (ib);
1039 /* Make sure that we have not read this node before. Nodes that
1040 have already been read will have their tag stored in the 'aux'
1041 field. Since built-in functions can be referenced in multiple
1042 functions, they are expected to be read more than once. */
1043 if (node->symbol.aux && !DECL_BUILT_IN (node->symbol.decl))
1044 internal_error ("bytecode stream: found multiple instances of cgraph "
1045 "node with uid %d", node->uid);
1047 bp = streamer_read_bitpack (ib);
1048 input_overwrite_node (file_data, node, tag, &bp);
1050 /* Store a reference for now, and fix up later to be a pointer. */
1051 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
1053 /* Store a reference for now, and fix up later to be a pointer. */
1054 node->symbol.same_comdat_group = (symtab_node) (intptr_t) ref2;
1056 if (node->thunk.thunk_p)
1058 int type = streamer_read_uhwi (ib);
1059 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1060 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1062 node->thunk.fixed_offset = fixed_offset;
1063 node->thunk.this_adjusting = (type & 2);
1064 node->thunk.virtual_value = virtual_value;
1065 node->thunk.virtual_offset_p = (type & 4);
1067 if (node->symbol.alias && !node->symbol.analyzed && node->symbol.weakref)
1068 node->symbol.alias_target = get_alias_symbol (node->symbol.decl);
1069 node->profile_id = streamer_read_hwi (ib);
1070 return node;
1073 /* Read a node from input_block IB. TAG is the node's tag just read.
1074 Return the node read or overwriten. */
1076 static struct varpool_node *
1077 input_varpool_node (struct lto_file_decl_data *file_data,
1078 struct lto_input_block *ib)
1080 int decl_index;
1081 tree var_decl;
1082 struct varpool_node *node;
1083 struct bitpack_d bp;
1084 int ref = LCC_NOT_FOUND;
1085 int order;
1087 order = streamer_read_hwi (ib) + order_base;
1088 decl_index = streamer_read_uhwi (ib);
1089 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1091 /* Declaration of functions can be already merged with a declaration
1092 from other input file. We keep cgraph unmerged until after streaming
1093 of ipa passes is done. Alays forcingly create a fresh node. */
1094 node = varpool_create_empty_node ();
1095 node->symbol.decl = var_decl;
1096 symtab_register_node ((symtab_node)node);
1098 node->symbol.order = order;
1099 if (order >= symtab_order)
1100 symtab_order = order + 1;
1101 node->symbol.lto_file_data = file_data;
1103 bp = streamer_read_bitpack (ib);
1104 node->symbol.externally_visible = bp_unpack_value (&bp, 1);
1105 node->symbol.force_output = bp_unpack_value (&bp, 1);
1106 node->symbol.forced_by_abi = bp_unpack_value (&bp, 1);
1107 node->symbol.unique_name = bp_unpack_value (&bp, 1);
1108 node->symbol.definition = bp_unpack_value (&bp, 1);
1109 node->symbol.alias = bp_unpack_value (&bp, 1);
1110 node->symbol.weakref = bp_unpack_value (&bp, 1);
1111 node->symbol.analyzed = bp_unpack_value (&bp, 1);
1112 node->symbol.used_from_other_partition = bp_unpack_value (&bp, 1);
1113 node->symbol.in_other_partition = bp_unpack_value (&bp, 1);
1114 if (node->symbol.in_other_partition)
1116 DECL_EXTERNAL (node->symbol.decl) = 1;
1117 TREE_STATIC (node->symbol.decl) = 0;
1119 if (node->symbol.alias && !node->symbol.analyzed && node->symbol.weakref)
1120 node->symbol.alias_target = get_alias_symbol (node->symbol.decl);
1121 ref = streamer_read_hwi (ib);
1122 /* Store a reference for now, and fix up later to be a pointer. */
1123 node->symbol.same_comdat_group = (symtab_node) (intptr_t) ref;
1124 node->symbol.resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1125 LDPR_NUM_KNOWN);
1127 return node;
1130 /* Read a node from input_block IB. TAG is the node's tag just read.
1131 Return the node read or overwriten. */
1133 static void
1134 input_ref (struct lto_input_block *ib,
1135 symtab_node referring_node,
1136 vec<symtab_node> nodes)
1138 symtab_node node = NULL;
1139 struct bitpack_d bp;
1140 enum ipa_ref_use use;
1141 bool speculative;
1142 struct ipa_ref *ref;
1144 bp = streamer_read_bitpack (ib);
1145 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1146 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1147 node = nodes[streamer_read_hwi (ib)];
1148 ref = ipa_record_reference (referring_node, node, use, NULL);
1149 ref->speculative = speculative;
1150 if (is_a <cgraph_node> (referring_node))
1151 ref->lto_stmt_uid = streamer_read_hwi (ib);
1154 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1155 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1156 edge being read is indirect (in the sense that it has
1157 indirect_unknown_callee set). */
1159 static void
1160 input_edge (struct lto_input_block *ib, vec<symtab_node> nodes,
1161 bool indirect)
1163 struct cgraph_node *caller, *callee;
1164 struct cgraph_edge *edge;
1165 unsigned int stmt_id;
1166 gcov_type count;
1167 int freq;
1168 cgraph_inline_failed_t inline_failed;
1169 struct bitpack_d bp;
1170 int ecf_flags = 0;
1172 caller = cgraph (nodes[streamer_read_hwi (ib)]);
1173 if (caller == NULL || caller->symbol.decl == NULL_TREE)
1174 internal_error ("bytecode stream: no caller found while reading edge");
1176 if (!indirect)
1178 callee = cgraph (nodes[streamer_read_hwi (ib)]);
1179 if (callee == NULL || callee->symbol.decl == NULL_TREE)
1180 internal_error ("bytecode stream: no callee found while reading edge");
1182 else
1183 callee = NULL;
1185 count = streamer_read_gcov_count (ib);
1187 bp = streamer_read_bitpack (ib);
1188 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_enum, CIF_N_REASONS);
1189 stmt_id = bp_unpack_var_len_unsigned (&bp);
1190 freq = (int) bp_unpack_var_len_unsigned (&bp);
1192 if (indirect)
1193 edge = cgraph_create_indirect_edge (caller, NULL, 0, count, freq);
1194 else
1195 edge = cgraph_create_edge (caller, callee, NULL, count, freq);
1197 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1198 edge->speculative = bp_unpack_value (&bp, 1);
1199 edge->lto_stmt_uid = stmt_id;
1200 edge->inline_failed = inline_failed;
1201 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1202 edge->can_throw_external = bp_unpack_value (&bp, 1);
1203 if (indirect)
1205 if (bp_unpack_value (&bp, 1))
1206 ecf_flags |= ECF_CONST;
1207 if (bp_unpack_value (&bp, 1))
1208 ecf_flags |= ECF_PURE;
1209 if (bp_unpack_value (&bp, 1))
1210 ecf_flags |= ECF_NORETURN;
1211 if (bp_unpack_value (&bp, 1))
1212 ecf_flags |= ECF_MALLOC;
1213 if (bp_unpack_value (&bp, 1))
1214 ecf_flags |= ECF_NOTHROW;
1215 if (bp_unpack_value (&bp, 1))
1216 ecf_flags |= ECF_RETURNS_TWICE;
1217 edge->indirect_info->ecf_flags = ecf_flags;
1218 edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1219 if (edge->indirect_info->common_target_id)
1220 edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1225 /* Read a cgraph from IB using the info in FILE_DATA. */
1227 static vec<symtab_node>
1228 input_cgraph_1 (struct lto_file_decl_data *file_data,
1229 struct lto_input_block *ib)
1231 enum LTO_symtab_tags tag;
1232 vec<symtab_node> nodes = vNULL;
1233 symtab_node node;
1234 unsigned i;
1236 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1237 order_base = symtab_order;
1238 while (tag)
1240 if (tag == LTO_symtab_edge)
1241 input_edge (ib, nodes, false);
1242 else if (tag == LTO_symtab_indirect_edge)
1243 input_edge (ib, nodes, true);
1244 else if (tag == LTO_symtab_variable)
1246 node = (symtab_node)input_varpool_node (file_data, ib);
1247 nodes.safe_push (node);
1248 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1250 else
1252 node = (symtab_node)input_node (file_data, ib, tag, nodes);
1253 if (node == NULL || node->symbol.decl == NULL_TREE)
1254 internal_error ("bytecode stream: found empty cgraph node");
1255 nodes.safe_push (node);
1256 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1259 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1262 lto_input_toplevel_asms (file_data, order_base);
1264 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1265 #ifdef ENABLE_CHECKING
1266 FOR_EACH_VEC_ELT (nodes, i, node)
1267 gcc_assert (node->symbol.aux || !is_a <cgraph_node> (node));
1268 #endif
1269 FOR_EACH_VEC_ELT (nodes, i, node)
1271 int ref;
1272 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
1274 ref = (int) (intptr_t) cnode->global.inlined_to;
1276 /* We share declaration of builtins, so we may read same node twice. */
1277 if (!node->symbol.aux)
1278 continue;
1279 node->symbol.aux = NULL;
1281 /* Fixup inlined_to from reference to pointer. */
1282 if (ref != LCC_NOT_FOUND)
1283 cgraph (node)->global.inlined_to = cgraph (nodes[ref]);
1284 else
1285 cnode->global.inlined_to = NULL;
1288 ref = (int) (intptr_t) node->symbol.same_comdat_group;
1290 /* Fixup same_comdat_group from reference to pointer. */
1291 if (ref != LCC_NOT_FOUND)
1292 node->symbol.same_comdat_group = nodes[ref];
1293 else
1294 node->symbol.same_comdat_group = NULL;
1296 FOR_EACH_VEC_ELT (nodes, i, node)
1297 node->symbol.aux = is_a <cgraph_node> (node) ? (void *)1 : NULL;
1298 return nodes;
1301 /* Input ipa_refs. */
1303 static void
1304 input_refs (struct lto_input_block *ib,
1305 vec<symtab_node> nodes)
1307 int count;
1308 int idx;
1309 while (true)
1311 symtab_node node;
1312 count = streamer_read_uhwi (ib);
1313 if (!count)
1314 break;
1315 idx = streamer_read_uhwi (ib);
1316 node = nodes[idx];
1317 while (count)
1319 input_ref (ib, node, nodes);
1320 count--;
1326 static struct gcov_ctr_summary lto_gcov_summary;
1328 /* Input profile_info from IB. */
1329 static void
1330 input_profile_summary (struct lto_input_block *ib,
1331 struct lto_file_decl_data *file_data)
1333 unsigned h_ix;
1334 struct bitpack_d bp;
1335 unsigned int runs = streamer_read_uhwi (ib);
1336 if (runs)
1338 file_data->profile_info.runs = runs;
1339 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1340 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1342 memset (file_data->profile_info.histogram, 0,
1343 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1344 /* Input the bitpack of non-zero histogram indices. */
1345 bp = streamer_read_bitpack (ib);
1346 /* Read in and unpack the full bitpack, flagging non-zero
1347 histogram entries by setting the num_counters non-zero. */
1348 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1350 file_data->profile_info.histogram[h_ix].num_counters
1351 = bp_unpack_value (&bp, 1);
1353 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1355 if (!file_data->profile_info.histogram[h_ix].num_counters)
1356 continue;
1358 file_data->profile_info.histogram[h_ix].num_counters
1359 = streamer_read_gcov_count (ib);
1360 file_data->profile_info.histogram[h_ix].min_value
1361 = streamer_read_gcov_count (ib);
1362 file_data->profile_info.histogram[h_ix].cum_value
1363 = streamer_read_gcov_count (ib);
1365 /* IPA-profile computes hot bb threshold based on cumulated
1366 whole program profile. We need to stream it down to ltrans. */
1367 if (flag_ltrans)
1368 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1373 /* Rescale profile summaries to the same number of runs in the whole unit. */
1375 static void
1376 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1378 struct lto_file_decl_data *file_data;
1379 unsigned int j, h_ix;
1380 gcov_unsigned_t max_runs = 0;
1381 struct cgraph_node *node;
1382 struct cgraph_edge *edge;
1383 gcov_type saved_sum_all = 0;
1384 gcov_ctr_summary *saved_profile_info = 0;
1385 int saved_scale = 0;
1387 /* Find unit with maximal number of runs. If we ever get serious about
1388 roundoff errors, we might also consider computing smallest common
1389 multiply. */
1390 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1391 if (max_runs < file_data->profile_info.runs)
1392 max_runs = file_data->profile_info.runs;
1394 if (!max_runs)
1395 return;
1397 /* Simple overflow check. We probably don't need to support that many train
1398 runs. Such a large value probably imply data corruption anyway. */
1399 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1401 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1402 INT_MAX / REG_BR_PROB_BASE);
1403 return;
1406 profile_info = &lto_gcov_summary;
1407 lto_gcov_summary.runs = max_runs;
1408 lto_gcov_summary.sum_max = 0;
1409 memset (lto_gcov_summary.histogram, 0,
1410 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1412 /* Rescale all units to the maximal number of runs.
1413 sum_max can not be easily merged, as we have no idea what files come from
1414 the same run. We do not use the info anyway, so leave it 0. */
1415 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1416 if (file_data->profile_info.runs)
1418 int scale = GCOV_COMPUTE_SCALE (max_runs,
1419 file_data->profile_info.runs);
1420 lto_gcov_summary.sum_max
1421 = MAX (lto_gcov_summary.sum_max,
1422 apply_scale (file_data->profile_info.sum_max, scale));
1423 lto_gcov_summary.sum_all
1424 = MAX (lto_gcov_summary.sum_all,
1425 apply_scale (file_data->profile_info.sum_all, scale));
1426 /* Save a pointer to the profile_info with the largest
1427 scaled sum_all and the scale for use in merging the
1428 histogram. */
1429 if (!saved_profile_info
1430 || lto_gcov_summary.sum_all > saved_sum_all)
1432 saved_profile_info = &file_data->profile_info;
1433 saved_sum_all = lto_gcov_summary.sum_all;
1434 saved_scale = scale;
1438 gcc_assert (saved_profile_info);
1440 /* Scale up the histogram from the profile that had the largest
1441 scaled sum_all above. */
1442 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1444 /* Scale up the min value as we did the corresponding sum_all
1445 above. Use that to find the new histogram index. */
1446 gcov_type scaled_min
1447 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1448 saved_scale);
1449 /* The new index may be shared with another scaled histogram entry,
1450 so we need to account for a non-zero histogram entry at new_ix. */
1451 unsigned new_ix = gcov_histo_index (scaled_min);
1452 lto_gcov_summary.histogram[new_ix].min_value
1453 = (lto_gcov_summary.histogram[new_ix].num_counters
1454 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1455 : scaled_min);
1456 /* Some of the scaled counter values would ostensibly need to be placed
1457 into different (larger) histogram buckets, but we keep things simple
1458 here and place the scaled cumulative counter value in the bucket
1459 corresponding to the scaled minimum counter value. */
1460 lto_gcov_summary.histogram[new_ix].cum_value
1461 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1462 saved_scale);
1463 lto_gcov_summary.histogram[new_ix].num_counters
1464 += saved_profile_info->histogram[h_ix].num_counters;
1467 /* Watch roundoff errors. */
1468 if (lto_gcov_summary.sum_max < max_runs)
1469 lto_gcov_summary.sum_max = max_runs;
1471 /* If merging already happent at WPA time, we are done. */
1472 if (flag_ltrans)
1473 return;
1475 /* Now compute count_materialization_scale of each node.
1476 During LTRANS we already have values of count_materialization_scale
1477 computed, so just update them. */
1478 FOR_EACH_FUNCTION (node)
1479 if (node->symbol.lto_file_data
1480 && node->symbol.lto_file_data->profile_info.runs)
1482 int scale;
1484 scale = RDIV (node->count_materialization_scale * max_runs,
1485 node->symbol.lto_file_data->profile_info.runs);
1486 node->count_materialization_scale = scale;
1487 if (scale < 0)
1488 fatal_error ("Profile information in %s corrupted",
1489 file_data->file_name);
1491 if (scale == REG_BR_PROB_BASE)
1492 continue;
1493 for (edge = node->callees; edge; edge = edge->next_callee)
1494 edge->count = apply_scale (edge->count, scale);
1495 node->count = apply_scale (node->count, scale);
1499 /* Input and merge the symtab from each of the .o files passed to
1500 lto1. */
1502 void
1503 input_symtab (void)
1505 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1506 struct lto_file_decl_data *file_data;
1507 unsigned int j = 0;
1508 struct cgraph_node *node;
1510 while ((file_data = file_data_vec[j++]))
1512 const char *data;
1513 size_t len;
1514 struct lto_input_block *ib;
1515 vec<symtab_node> nodes;
1517 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1518 &data, &len);
1519 if (!ib)
1520 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1521 input_profile_summary (ib, file_data);
1522 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1523 nodes = input_cgraph_1 (file_data, ib);
1524 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1525 ib, data, len);
1527 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1528 &data, &len);
1529 if (!ib)
1530 fatal_error("cannot find LTO section refs in %s", file_data->file_name);
1531 input_refs (ib, nodes);
1532 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1533 ib, data, len);
1534 if (flag_ltrans)
1535 input_cgraph_opt_summary (nodes);
1536 nodes.release ();
1539 merge_profile_summaries (file_data_vec);
1540 get_working_sets ();
1543 /* Clear out the aux field that was used to store enough state to
1544 tell which nodes should be overwritten. */
1545 FOR_EACH_FUNCTION (node)
1547 /* Some nodes may have been created by cgraph_node. This
1548 happens when the callgraph contains nested functions. If the
1549 node for the parent function was never emitted to the gimple
1550 file, cgraph_node will create a node for it when setting the
1551 context of the nested function. */
1552 if (node->symbol.lto_file_data)
1553 node->symbol.aux = NULL;
1557 /* True when we need optimization summary for NODE. */
1559 static int
1560 output_cgraph_opt_summary_p (struct cgraph_node *node)
1562 return (node->clone_of
1563 && (node->clone.tree_map
1564 || node->clone.args_to_skip
1565 || node->clone.combined_args_to_skip));
1568 /* Output optimization summary for EDGE to OB. */
1569 static void
1570 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1571 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1575 /* Output optimization summary for NODE to OB. */
1577 static void
1578 output_node_opt_summary (struct output_block *ob,
1579 struct cgraph_node *node,
1580 lto_symtab_encoder_t encoder)
1582 unsigned int index;
1583 bitmap_iterator bi;
1584 struct ipa_replace_map *map;
1585 struct bitpack_d bp;
1586 int i;
1587 struct cgraph_edge *e;
1589 if (node->clone.args_to_skip)
1591 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1592 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1593 streamer_write_uhwi (ob, index);
1595 else
1596 streamer_write_uhwi (ob, 0);
1597 if (node->clone.combined_args_to_skip)
1599 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1600 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1601 streamer_write_uhwi (ob, index);
1603 else
1604 streamer_write_uhwi (ob, 0);
1605 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1606 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1608 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1609 mechanism to store function local declarations into summaries. */
1610 gcc_assert (!map->old_tree);
1611 streamer_write_uhwi (ob, map->parm_num);
1612 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1613 stream_write_tree (ob, map->new_tree, true);
1614 bp = bitpack_create (ob->main_stream);
1615 bp_pack_value (&bp, map->replace_p, 1);
1616 bp_pack_value (&bp, map->ref_p, 1);
1617 streamer_write_bitpack (&bp);
1620 if (lto_symtab_encoder_in_partition_p (encoder, (symtab_node) node))
1622 for (e = node->callees; e; e = e->next_callee)
1623 output_edge_opt_summary (ob, e);
1624 for (e = node->indirect_calls; e; e = e->next_callee)
1625 output_edge_opt_summary (ob, e);
1629 /* Output optimization summaries stored in callgraph.
1630 At the moment it is the clone info structure. */
1632 static void
1633 output_cgraph_opt_summary (void)
1635 int i, n_nodes;
1636 lto_symtab_encoder_t encoder;
1637 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1638 unsigned count = 0;
1640 ob->cgraph_node = NULL;
1641 encoder = ob->decl_state->symtab_node_encoder;
1642 n_nodes = lto_symtab_encoder_size (encoder);
1643 for (i = 0; i < n_nodes; i++)
1645 symtab_node node = lto_symtab_encoder_deref (encoder, i);
1646 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1647 if (cnode && output_cgraph_opt_summary_p (cnode))
1648 count++;
1650 streamer_write_uhwi (ob, count);
1651 for (i = 0; i < n_nodes; i++)
1653 symtab_node node = lto_symtab_encoder_deref (encoder, i);
1654 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
1655 if (cnode && output_cgraph_opt_summary_p (cnode))
1657 streamer_write_uhwi (ob, i);
1658 output_node_opt_summary (ob, cnode, encoder);
1661 produce_asm (ob, NULL);
1662 destroy_output_block (ob);
1665 /* Input optimisation summary of EDGE. */
1667 static void
1668 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
1669 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
1673 /* Input optimisation summary of NODE. */
1675 static void
1676 input_node_opt_summary (struct cgraph_node *node,
1677 struct lto_input_block *ib_main,
1678 struct data_in *data_in)
1680 int i;
1681 int count;
1682 int bit;
1683 struct bitpack_d bp;
1684 struct cgraph_edge *e;
1686 count = streamer_read_uhwi (ib_main);
1687 if (count)
1688 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1689 for (i = 0; i < count; i++)
1691 bit = streamer_read_uhwi (ib_main);
1692 bitmap_set_bit (node->clone.args_to_skip, bit);
1694 count = streamer_read_uhwi (ib_main);
1695 if (count)
1696 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1697 for (i = 0; i < count; i++)
1699 bit = streamer_read_uhwi (ib_main);
1700 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1702 count = streamer_read_uhwi (ib_main);
1703 for (i = 0; i < count; i++)
1705 struct ipa_replace_map *map = ggc_alloc_ipa_replace_map ();
1707 vec_safe_push (node->clone.tree_map, map);
1708 map->parm_num = streamer_read_uhwi (ib_main);
1709 map->old_tree = NULL;
1710 map->new_tree = stream_read_tree (ib_main, data_in);
1711 bp = streamer_read_bitpack (ib_main);
1712 map->replace_p = bp_unpack_value (&bp, 1);
1713 map->ref_p = bp_unpack_value (&bp, 1);
1715 for (e = node->callees; e; e = e->next_callee)
1716 input_edge_opt_summary (e, ib_main);
1717 for (e = node->indirect_calls; e; e = e->next_callee)
1718 input_edge_opt_summary (e, ib_main);
1721 /* Read section in file FILE_DATA of length LEN with data DATA. */
1723 static void
1724 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1725 const char *data, size_t len,
1726 vec<symtab_node> nodes)
1728 const struct lto_function_header *header =
1729 (const struct lto_function_header *) data;
1730 const int cfg_offset = sizeof (struct lto_function_header);
1731 const int main_offset = cfg_offset + header->cfg_size;
1732 const int string_offset = main_offset + header->main_size;
1733 struct data_in *data_in;
1734 struct lto_input_block ib_main;
1735 unsigned int i;
1736 unsigned int count;
1738 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1739 header->main_size);
1741 data_in =
1742 lto_data_in_create (file_data, (const char *) data + string_offset,
1743 header->string_size, vNULL);
1744 count = streamer_read_uhwi (&ib_main);
1746 for (i = 0; i < count; i++)
1748 int ref = streamer_read_uhwi (&ib_main);
1749 input_node_opt_summary (cgraph (nodes[ref]),
1750 &ib_main, data_in);
1752 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1753 len);
1754 lto_data_in_delete (data_in);
1757 /* Input optimization summary of cgraph. */
1759 static void
1760 input_cgraph_opt_summary (vec<symtab_node> nodes)
1762 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1763 struct lto_file_decl_data *file_data;
1764 unsigned int j = 0;
1766 while ((file_data = file_data_vec[j++]))
1768 size_t len;
1769 const char *data =
1770 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1771 &len);
1773 if (data)
1774 input_cgraph_opt_section (file_data, data, len, nodes);