clang-format: Enhance list of FOR_EACH macros
[official-gcc.git] / gcc / lto-cgraph.c
blob67a9024ab28b94c0b1932d66901810898ea4d9cf
1 /* Write and read the cgraph to the memory mapped representation of a
2 .o file.
4 Copyright (C) 2009-2015 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 "backend.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "predict.h"
31 #include "stringpool.h"
32 #include "tree-streamer.h"
33 #include "cgraph.h"
34 #include "tree-pass.h"
35 #include "profile.h"
36 #include "context.h"
37 #include "pass_manager.h"
38 #include "ipa-utils.h"
39 #include "omp-low.h"
40 #include "ipa-chkp.h"
42 /* True when asm nodes has been output. */
43 bool asm_nodes_output = false;
45 static void output_cgraph_opt_summary (void);
46 static void input_cgraph_opt_summary (vec<symtab_node *> nodes);
48 /* Number of LDPR values known to GCC. */
49 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
51 /* All node orders are ofsetted by ORDER_BASE. */
52 static int order_base;
54 /* Cgraph streaming is organized as set of record whose type
55 is indicated by a tag. */
56 enum LTO_symtab_tags
58 /* Must leave 0 for the stopper. */
60 /* Cgraph node without body available. */
61 LTO_symtab_unavail_node = 1,
62 /* Cgraph node with function body. */
63 LTO_symtab_analyzed_node,
64 /* Cgraph edges. */
65 LTO_symtab_edge,
66 LTO_symtab_indirect_edge,
67 LTO_symtab_variable,
68 LTO_symtab_last_tag
71 /* Create a new symtab encoder.
72 if FOR_INPUT, the encoder allocate only datastructures needed
73 to read the symtab. */
75 lto_symtab_encoder_t
76 lto_symtab_encoder_new (bool for_input)
78 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
80 if (!for_input)
81 encoder->map = new hash_map<symtab_node *, size_t>;
82 encoder->nodes.create (0);
83 return encoder;
87 /* Delete ENCODER and its components. */
89 void
90 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
92 encoder->nodes.release ();
93 if (encoder->map)
94 delete encoder->map;
95 free (encoder);
99 /* Return the existing reference number of NODE in the symtab encoder in
100 output block OB. Assign a new reference if this is the first time
101 NODE is encoded. */
104 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
105 symtab_node *node)
107 int ref;
109 if (!encoder->map)
111 lto_encoder_entry entry = {node, false, false, false};
113 ref = encoder->nodes.length ();
114 encoder->nodes.safe_push (entry);
115 return ref;
118 size_t *slot = encoder->map->get (node);
119 if (!slot || !*slot)
121 lto_encoder_entry entry = {node, false, false, false};
122 ref = encoder->nodes.length ();
123 if (!slot)
124 encoder->map->put (node, ref + 1);
125 encoder->nodes.safe_push (entry);
127 else
128 ref = *slot - 1;
130 return ref;
133 /* Remove NODE from encoder. */
135 bool
136 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
137 symtab_node *node)
139 int index;
140 lto_encoder_entry last_node;
142 size_t *slot = encoder->map->get (node);
143 if (slot == NULL || !*slot)
144 return false;
146 index = *slot - 1;
147 gcc_checking_assert (encoder->nodes[index].node == node);
149 /* Remove from vector. We do this by swapping node with the last element
150 of the vector. */
151 last_node = encoder->nodes.pop ();
152 if (last_node.node != node)
154 gcc_assert (encoder->map->put (last_node.node, index + 1));
156 /* Move the last element to the original spot of NODE. */
157 encoder->nodes[index] = last_node;
160 /* Remove element from hash table. */
161 encoder->map->remove (node);
162 return true;
166 /* Return TRUE if we should encode the body of NODE (if any). */
168 bool
169 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
170 struct cgraph_node *node)
172 int index = lto_symtab_encoder_lookup (encoder, node);
173 return encoder->nodes[index].body;
176 /* Specify that we encode the body of NODE in this partition. */
178 static void
179 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
180 struct cgraph_node *node)
182 int index = lto_symtab_encoder_encode (encoder, node);
183 gcc_checking_assert (encoder->nodes[index].node == node);
184 encoder->nodes[index].body = true;
187 /* Return TRUE if we should encode initializer of NODE (if any). */
189 bool
190 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
191 varpool_node *node)
193 int index = lto_symtab_encoder_lookup (encoder, node);
194 if (index == LCC_NOT_FOUND)
195 return false;
196 return encoder->nodes[index].initializer;
199 /* Specify that we should encode initializer of NODE (if any). */
201 static void
202 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
203 varpool_node *node)
205 int index = lto_symtab_encoder_lookup (encoder, node);
206 encoder->nodes[index].initializer = true;
209 /* Return TRUE if NODE is in this partition. */
211 bool
212 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
213 symtab_node *node)
215 int index = lto_symtab_encoder_lookup (encoder, node);
216 if (index == LCC_NOT_FOUND)
217 return false;
218 return encoder->nodes[index].in_partition;
221 /* Specify that NODE is in this partition. */
223 void
224 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
225 symtab_node *node)
227 int index = lto_symtab_encoder_encode (encoder, node);
228 encoder->nodes[index].in_partition = true;
231 /* Output the cgraph EDGE to OB using ENCODER. */
233 static void
234 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
235 lto_symtab_encoder_t encoder)
237 unsigned int uid;
238 intptr_t ref;
239 struct bitpack_d bp;
241 if (edge->indirect_unknown_callee)
242 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
243 LTO_symtab_indirect_edge);
244 else
245 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
246 LTO_symtab_edge);
248 ref = lto_symtab_encoder_lookup (encoder, edge->caller);
249 gcc_assert (ref != LCC_NOT_FOUND);
250 streamer_write_hwi_stream (ob->main_stream, ref);
252 if (!edge->indirect_unknown_callee)
254 ref = lto_symtab_encoder_lookup (encoder, edge->callee);
255 gcc_assert (ref != LCC_NOT_FOUND);
256 streamer_write_hwi_stream (ob->main_stream, ref);
259 streamer_write_gcov_count_stream (ob->main_stream, edge->count);
261 bp = bitpack_create (ob->main_stream);
262 uid = (!gimple_has_body_p (edge->caller->decl)
263 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt) + 1);
264 bp_pack_enum (&bp, cgraph_inline_failed_t,
265 CIF_N_REASONS, edge->inline_failed);
266 bp_pack_var_len_unsigned (&bp, uid);
267 bp_pack_var_len_unsigned (&bp, edge->frequency);
268 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
269 bp_pack_value (&bp, edge->speculative, 1);
270 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
271 bp_pack_value (&bp, edge->can_throw_external, 1);
272 bp_pack_value (&bp, edge->in_polymorphic_cdtor, 1);
273 if (edge->indirect_unknown_callee)
275 int flags = edge->indirect_info->ecf_flags;
276 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
277 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
278 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
279 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
280 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
281 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
282 /* Flags that should not appear on indirect calls. */
283 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
284 | ECF_MAY_BE_ALLOCA
285 | ECF_SIBCALL
286 | ECF_LEAF
287 | ECF_NOVOPS)));
289 streamer_write_bitpack (&bp);
290 if (edge->indirect_unknown_callee)
292 streamer_write_hwi_stream (ob->main_stream,
293 edge->indirect_info->common_target_id);
294 if (edge->indirect_info->common_target_id)
295 streamer_write_hwi_stream
296 (ob->main_stream, edge->indirect_info->common_target_probability);
300 /* Return if NODE contain references from other partitions. */
302 bool
303 referenced_from_other_partition_p (symtab_node *node, lto_symtab_encoder_t encoder)
305 int i;
306 struct ipa_ref *ref = NULL;
308 for (i = 0; node->iterate_referring (i, ref); i++)
310 /* Ignore references from non-offloadable nodes while streaming NODE into
311 offload LTO section. */
312 if (!ref->referring->need_lto_streaming)
313 continue;
315 if (ref->referring->in_other_partition
316 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
317 return true;
319 return false;
322 /* Return true when node is reachable from other partition. */
324 bool
325 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
327 struct cgraph_edge *e;
328 if (!node->definition)
329 return false;
330 if (node->global.inlined_to)
331 return false;
332 for (e = node->callers; e; e = e->next_caller)
334 /* Ignore references from non-offloadable nodes while streaming NODE into
335 offload LTO section. */
336 if (!e->caller->need_lto_streaming)
337 continue;
339 if (e->caller->in_other_partition
340 || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
341 return true;
343 return false;
346 /* Return if NODE contain references from other partitions. */
348 bool
349 referenced_from_this_partition_p (symtab_node *node,
350 lto_symtab_encoder_t encoder)
352 int i;
353 struct ipa_ref *ref = NULL;
355 for (i = 0; node->iterate_referring (i, ref); i++)
356 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
357 return true;
358 return false;
361 /* Return true when node is reachable from other partition. */
363 bool
364 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
366 struct cgraph_edge *e;
367 for (e = node->callers; e; e = e->next_caller)
368 if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
369 return true;
370 return false;
373 /* Output the cgraph NODE to OB. ENCODER is used to find the
374 reference number of NODE->inlined_to. SET is the set of nodes we
375 are writing to the current file. If NODE is not in SET, then NODE
376 is a boundary of a cgraph_node_set and we pretend NODE just has a
377 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
378 that have had their callgraph node written so far. This is used to
379 determine if NODE is a clone of a previously written node. */
381 static void
382 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
383 lto_symtab_encoder_t encoder)
385 unsigned int tag;
386 struct bitpack_d bp;
387 bool boundary_p;
388 intptr_t ref;
389 bool in_other_partition = false;
390 struct cgraph_node *clone_of, *ultimate_clone_of;
391 ipa_opt_pass_d *pass;
392 int i;
393 const char *comdat;
394 const char *section;
395 tree group;
397 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
399 if (node->analyzed && (!boundary_p || node->alias || node->thunk.thunk_p))
400 tag = LTO_symtab_analyzed_node;
401 else
402 tag = LTO_symtab_unavail_node;
404 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
405 tag);
406 streamer_write_hwi_stream (ob->main_stream, node->order);
408 /* In WPA mode, we only output part of the call-graph. Also, we
409 fake cgraph node attributes. There are two cases that we care.
411 Boundary nodes: There are nodes that are not part of SET but are
412 called from within SET. We artificially make them look like
413 externally visible nodes with no function body.
415 Cherry-picked nodes: These are nodes we pulled from other
416 translation units into SET during IPA-inlining. We make them as
417 local static nodes to prevent clashes with other local statics. */
418 if (boundary_p && node->analyzed
419 && node->get_partitioning_class () == SYMBOL_PARTITION)
421 /* Inline clones can not be part of boundary.
422 gcc_assert (!node->global.inlined_to);
424 FIXME: At the moment they can be, when partition contains an inline
425 clone that is clone of inline clone from outside partition. We can
426 reshape the clone tree and make other tree to be the root, but it
427 needs a bit extra work and will be promplty done by cgraph_remove_node
428 after reading back. */
429 in_other_partition = 1;
432 clone_of = node->clone_of;
433 while (clone_of
434 && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
435 if (clone_of->prev_sibling_clone)
436 clone_of = clone_of->prev_sibling_clone;
437 else
438 clone_of = clone_of->clone_of;
440 /* See if body of the master function is output. If not, we are seeing only
441 an declaration and we do not need to pass down clone tree. */
442 ultimate_clone_of = clone_of;
443 while (ultimate_clone_of && ultimate_clone_of->clone_of)
444 ultimate_clone_of = ultimate_clone_of->clone_of;
446 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
447 clone_of = NULL;
449 if (tag == LTO_symtab_analyzed_node)
450 gcc_assert (clone_of || !node->clone_of);
451 if (!clone_of)
452 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
453 else
454 streamer_write_hwi_stream (ob->main_stream, ref);
457 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
458 streamer_write_gcov_count_stream (ob->main_stream, node->count);
459 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
461 streamer_write_hwi_stream (ob->main_stream,
462 node->ipa_transforms_to_apply.length ());
463 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
464 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
466 if (tag == LTO_symtab_analyzed_node)
468 if (node->global.inlined_to)
470 ref = lto_symtab_encoder_lookup (encoder, node->global.inlined_to);
471 gcc_assert (ref != LCC_NOT_FOUND);
473 else
474 ref = LCC_NOT_FOUND;
476 streamer_write_hwi_stream (ob->main_stream, ref);
479 group = node->get_comdat_group ();
480 if (group)
481 comdat = IDENTIFIER_POINTER (group);
482 else
483 comdat = "";
484 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
486 if (group)
488 if (node->same_comdat_group && !boundary_p)
490 ref = lto_symtab_encoder_lookup (encoder,
491 node->same_comdat_group);
492 gcc_assert (ref != LCC_NOT_FOUND);
494 else
495 ref = LCC_NOT_FOUND;
496 streamer_write_hwi_stream (ob->main_stream, ref);
499 section = node->get_section ();
500 if (!section)
501 section = "";
503 streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
505 bp = bitpack_create (ob->main_stream);
506 bp_pack_value (&bp, node->local.local, 1);
507 bp_pack_value (&bp, node->externally_visible, 1);
508 bp_pack_value (&bp, node->no_reorder, 1);
509 bp_pack_value (&bp, node->definition, 1);
510 bp_pack_value (&bp, node->local.versionable, 1);
511 bp_pack_value (&bp, node->local.can_change_signature, 1);
512 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
513 bp_pack_value (&bp, node->force_output, 1);
514 bp_pack_value (&bp, node->forced_by_abi, 1);
515 bp_pack_value (&bp, node->unique_name, 1);
516 bp_pack_value (&bp, node->body_removed, 1);
517 bp_pack_value (&bp, node->implicit_section, 1);
518 bp_pack_value (&bp, node->address_taken, 1);
519 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
520 && node->get_partitioning_class () == SYMBOL_PARTITION
521 && (reachable_from_other_partition_p (node, encoder)
522 || referenced_from_other_partition_p (node, encoder)), 1);
523 bp_pack_value (&bp, node->lowered, 1);
524 bp_pack_value (&bp, in_other_partition, 1);
525 bp_pack_value (&bp, node->alias, 1);
526 bp_pack_value (&bp, node->weakref, 1);
527 bp_pack_value (&bp, node->frequency, 2);
528 bp_pack_value (&bp, node->only_called_at_startup, 1);
529 bp_pack_value (&bp, node->only_called_at_exit, 1);
530 bp_pack_value (&bp, node->tm_clone, 1);
531 bp_pack_value (&bp, node->calls_comdat_local, 1);
532 bp_pack_value (&bp, node->icf_merged, 1);
533 bp_pack_value (&bp, node->nonfreeing_fn, 1);
534 bp_pack_value (&bp, node->thunk.thunk_p, 1);
535 bp_pack_value (&bp, node->parallelized_function, 1);
536 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
537 LDPR_NUM_KNOWN, node->resolution);
538 bp_pack_value (&bp, node->instrumentation_clone, 1);
539 bp_pack_value (&bp, node->split_part, 1);
540 streamer_write_bitpack (&bp);
541 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
543 if (node->thunk.thunk_p)
545 streamer_write_uhwi_stream
546 (ob->main_stream,
547 1 + (node->thunk.this_adjusting != 0) * 2
548 + (node->thunk.virtual_offset_p != 0) * 4
549 + (node->thunk.add_pointer_bounds_args != 0) * 8);
550 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
551 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
553 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
554 if (DECL_STATIC_CONSTRUCTOR (node->decl))
555 streamer_write_hwi_stream (ob->main_stream, node->get_init_priority ());
556 if (DECL_STATIC_DESTRUCTOR (node->decl))
557 streamer_write_hwi_stream (ob->main_stream, node->get_fini_priority ());
559 if (node->instrumentation_clone)
560 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->orig_decl);
563 /* Output the varpool NODE to OB.
564 If NODE is not in SET, then NODE is a boundary. */
566 static void
567 lto_output_varpool_node (struct lto_simple_output_block *ob, varpool_node *node,
568 lto_symtab_encoder_t encoder)
570 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
571 bool encode_initializer_p
572 = (node->definition
573 && lto_symtab_encoder_encode_initializer_p (encoder, node));
574 struct bitpack_d bp;
575 int ref;
576 const char *comdat;
577 const char *section;
578 tree group;
580 gcc_assert (!encode_initializer_p || node->definition);
581 gcc_assert (boundary_p || encode_initializer_p);
583 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
584 LTO_symtab_variable);
585 streamer_write_hwi_stream (ob->main_stream, node->order);
586 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
587 bp = bitpack_create (ob->main_stream);
588 bp_pack_value (&bp, node->externally_visible, 1);
589 bp_pack_value (&bp, node->no_reorder, 1);
590 bp_pack_value (&bp, node->force_output, 1);
591 bp_pack_value (&bp, node->forced_by_abi, 1);
592 bp_pack_value (&bp, node->unique_name, 1);
593 bp_pack_value (&bp,
594 node->body_removed
595 || (!encode_initializer_p && !node->alias && node->definition),
597 bp_pack_value (&bp, node->implicit_section, 1);
598 bp_pack_value (&bp, node->writeonly, 1);
599 bp_pack_value (&bp, node->definition && (encode_initializer_p || node->alias),
601 bp_pack_value (&bp, node->alias, 1);
602 bp_pack_value (&bp, node->weakref, 1);
603 bp_pack_value (&bp, node->analyzed && !boundary_p, 1);
604 gcc_assert (node->definition || !node->analyzed);
605 /* Constant pool initializers can be de-unified into individual ltrans units.
606 FIXME: Alternatively at -Os we may want to avoid generating for them the local
607 labels and share them across LTRANS partitions. */
608 if (node->get_partitioning_class () != SYMBOL_PARTITION)
610 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
611 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
613 else
615 bp_pack_value (&bp, node->definition
616 && referenced_from_other_partition_p (node, encoder), 1);
617 bp_pack_value (&bp, node->analyzed
618 && boundary_p && !DECL_EXTERNAL (node->decl), 1);
619 /* in_other_partition. */
621 bp_pack_value (&bp, node->tls_model, 3);
622 bp_pack_value (&bp, node->used_by_single_function, 1);
623 bp_pack_value (&bp, node->need_bounds_init, 1);
624 streamer_write_bitpack (&bp);
626 group = node->get_comdat_group ();
627 if (group)
628 comdat = IDENTIFIER_POINTER (group);
629 else
630 comdat = "";
631 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
633 if (group)
635 if (node->same_comdat_group && !boundary_p)
637 ref = lto_symtab_encoder_lookup (encoder,
638 node->same_comdat_group);
639 gcc_assert (ref != LCC_NOT_FOUND);
641 else
642 ref = LCC_NOT_FOUND;
643 streamer_write_hwi_stream (ob->main_stream, ref);
646 section = node->get_section ();
647 if (!section)
648 section = "";
649 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
651 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
652 LDPR_NUM_KNOWN, node->resolution);
655 /* Output the varpool NODE to OB.
656 If NODE is not in SET, then NODE is a boundary. */
658 static void
659 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
660 lto_symtab_encoder_t encoder)
662 struct bitpack_d bp;
663 int nref;
664 int uid = ref->lto_stmt_uid;
665 struct cgraph_node *node;
667 bp = bitpack_create (ob->main_stream);
668 bp_pack_value (&bp, ref->use, 3);
669 bp_pack_value (&bp, ref->speculative, 1);
670 streamer_write_bitpack (&bp);
671 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
672 gcc_assert (nref != LCC_NOT_FOUND);
673 streamer_write_hwi_stream (ob->main_stream, nref);
675 node = dyn_cast <cgraph_node *> (ref->referring);
676 if (node)
678 if (ref->stmt)
679 uid = gimple_uid (ref->stmt) + 1;
680 streamer_write_hwi_stream (ob->main_stream, uid);
684 /* Stream out profile_summary to OB. */
686 static void
687 output_profile_summary (struct lto_simple_output_block *ob)
689 unsigned h_ix;
690 struct bitpack_d bp;
692 if (profile_info)
694 /* We do not output num and run_max, they are not used by
695 GCC profile feedback and they are difficult to merge from multiple
696 units. */
697 gcc_assert (profile_info->runs);
698 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
699 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
701 /* sum_all is needed for computing the working set with the
702 histogram. */
703 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
705 /* Create and output a bitpack of non-zero histogram entries indices. */
706 bp = bitpack_create (ob->main_stream);
707 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
708 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
709 streamer_write_bitpack (&bp);
710 /* Now stream out only those non-zero entries. */
711 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
713 if (!profile_info->histogram[h_ix].num_counters)
714 continue;
715 streamer_write_gcov_count_stream (ob->main_stream,
716 profile_info->histogram[h_ix].num_counters);
717 streamer_write_gcov_count_stream (ob->main_stream,
718 profile_info->histogram[h_ix].min_value);
719 streamer_write_gcov_count_stream (ob->main_stream,
720 profile_info->histogram[h_ix].cum_value);
722 /* IPA-profile computes hot bb threshold based on cumulated
723 whole program profile. We need to stream it down to ltrans. */
724 if (flag_wpa)
725 streamer_write_gcov_count_stream (ob->main_stream,
726 get_hot_bb_threshold ());
728 else
729 streamer_write_uhwi_stream (ob->main_stream, 0);
732 /* Output all callees or indirect outgoing edges. EDGE must be the first such
733 edge. */
735 static void
736 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
737 struct lto_simple_output_block *ob,
738 lto_symtab_encoder_t encoder)
740 if (!edge)
741 return;
743 /* Output edges in backward direction, so the reconstructed callgraph match
744 and it is easy to associate call sites in the IPA pass summaries. */
745 while (edge->next_callee)
746 edge = edge->next_callee;
747 for (; edge; edge = edge->prev_callee)
748 lto_output_edge (ob, edge, encoder);
751 /* Output the part of the cgraph in SET. */
753 static void
754 output_refs (lto_symtab_encoder_t encoder)
756 struct lto_simple_output_block *ob;
757 int count;
758 struct ipa_ref *ref;
760 ob = lto_create_simple_output_block (LTO_section_refs);
762 for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
764 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
766 /* IPA_REF_ALIAS and IPA_REF_CHKP references are always preserved
767 in the boundary. Alias node can't have other references and
768 can be always handled as if it's not in the boundary. */
769 if (!node->alias && !lto_symtab_encoder_in_partition_p (encoder, node))
771 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
772 /* Output IPA_REF_CHKP reference. */
773 if (cnode
774 && cnode->instrumented_version
775 && !cnode->instrumentation_clone)
777 for (int i = 0; node->iterate_reference (i, ref); i++)
778 if (ref->use == IPA_REF_CHKP)
780 if (lto_symtab_encoder_lookup (encoder, ref->referred)
781 != LCC_NOT_FOUND)
783 int nref = lto_symtab_encoder_lookup (encoder, node);
784 streamer_write_gcov_count_stream (ob->main_stream, 1);
785 streamer_write_uhwi_stream (ob->main_stream, nref);
786 lto_output_ref (ob, ref, encoder);
788 break;
791 continue;
794 count = node->ref_list.nreferences ();
795 if (count)
797 streamer_write_gcov_count_stream (ob->main_stream, count);
798 streamer_write_uhwi_stream (ob->main_stream,
799 lto_symtab_encoder_lookup (encoder, node));
800 for (int i = 0; node->iterate_reference (i, ref); i++)
801 lto_output_ref (ob, ref, encoder);
805 streamer_write_uhwi_stream (ob->main_stream, 0);
807 lto_destroy_simple_output_block (ob);
810 /* Add NODE into encoder as well as nodes it is cloned from.
811 Do it in a way so clones appear first. */
813 static void
814 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
815 bool include_body)
817 if (node->clone_of)
818 add_node_to (encoder, node->clone_of, include_body);
819 else if (include_body)
820 lto_set_symtab_encoder_encode_body (encoder, node);
821 lto_symtab_encoder_encode (encoder, node);
824 /* Add all references in NODE to encoders. */
826 static void
827 create_references (lto_symtab_encoder_t encoder, symtab_node *node)
829 int i;
830 struct ipa_ref *ref = NULL;
831 for (i = 0; node->iterate_reference (i, ref); i++)
832 if (is_a <cgraph_node *> (ref->referred))
833 add_node_to (encoder, dyn_cast <cgraph_node *> (ref->referred), false);
834 else
835 lto_symtab_encoder_encode (encoder, ref->referred);
838 /* Select what needs to be streamed out. In regular lto mode stream everything.
839 In offload lto mode stream only nodes marked as offloadable. */
840 void
841 select_what_to_stream (void)
843 struct symtab_node *snode;
844 FOR_EACH_SYMBOL (snode)
845 snode->need_lto_streaming = !lto_stream_offload_p || snode->offloadable;
848 /* Find all symbols we want to stream into given partition and insert them
849 to encoders.
851 The function actually replaces IN_ENCODER by new one. The reason is that
852 streaming code needs clone's origin to be streamed before clone. This
853 means that we need to insert the nodes in specific order. This order is
854 ignored by the partitioning logic earlier. */
856 lto_symtab_encoder_t
857 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
859 struct cgraph_edge *edge;
860 int i;
861 lto_symtab_encoder_t encoder;
862 lto_symtab_encoder_iterator lsei;
863 hash_set<void *> reachable_call_targets;
865 encoder = lto_symtab_encoder_new (false);
867 /* Go over all entries in the IN_ENCODER and duplicate them to
868 ENCODER. At the same time insert masters of clones so
869 every master appears before clone. */
870 for (lsei = lsei_start_function_in_partition (in_encoder);
871 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
873 struct cgraph_node *node = lsei_cgraph_node (lsei);
874 if (!node->need_lto_streaming)
875 continue;
876 add_node_to (encoder, node, true);
877 lto_set_symtab_encoder_in_partition (encoder, node);
878 create_references (encoder, node);
880 for (lsei = lsei_start_variable_in_partition (in_encoder);
881 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
883 varpool_node *vnode = lsei_varpool_node (lsei);
885 if (!vnode->need_lto_streaming)
886 continue;
887 lto_set_symtab_encoder_in_partition (encoder, vnode);
888 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
889 create_references (encoder, vnode);
891 /* Pickle in also the initializer of all referenced readonly variables
892 to help folding. Constant pool variables are not shared, so we must
893 pickle those too. */
894 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
896 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
897 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
899 if (!lto_symtab_encoder_encode_initializer_p (encoder,
900 vnode)
901 && (((vnode->ctor_useable_for_folding_p ()
902 && (!DECL_VIRTUAL_P (vnode->decl)
903 || !flag_wpa
904 || flag_ltrans_devirtualize))
905 || POINTER_BOUNDS_P (vnode->decl))))
907 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
908 create_references (encoder, vnode);
913 /* Go over all the nodes again to include callees that are not in
914 SET. */
915 for (lsei = lsei_start_function_in_partition (encoder);
916 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
918 struct cgraph_node *node = lsei_cgraph_node (lsei);
919 for (edge = node->callees; edge; edge = edge->next_callee)
921 struct cgraph_node *callee = edge->callee;
922 if (!lto_symtab_encoder_in_partition_p (encoder, callee))
924 /* We should have moved all the inlines. */
925 gcc_assert (!callee->global.inlined_to);
926 add_node_to (encoder, callee, false);
929 /* Add all possible targets for late devirtualization. */
930 if (flag_ltrans_devirtualize || !flag_wpa)
931 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
932 if (edge->indirect_info->polymorphic)
934 unsigned int i;
935 void *cache_token;
936 bool final;
937 vec <cgraph_node *>targets
938 = possible_polymorphic_call_targets
939 (edge, &final, &cache_token);
940 if (!reachable_call_targets.add (cache_token))
942 for (i = 0; i < targets.length (); i++)
944 struct cgraph_node *callee = targets[i];
946 /* Adding an external declarations into the unit serves
947 no purpose and just increases its boundary. */
948 if (callee->definition
949 && !lto_symtab_encoder_in_partition_p
950 (encoder, callee))
952 gcc_assert (!callee->global.inlined_to);
953 add_node_to (encoder, callee, false);
959 /* Be sure to also insert alias targert and thunk callees. These needs
960 to stay to aid local calling conventions. */
961 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
963 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
964 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
966 if (node->alias && node->analyzed)
967 create_references (encoder, node);
968 if (cnode
969 && cnode->thunk.thunk_p)
970 add_node_to (encoder, cnode->callees->callee, false);
972 lto_symtab_encoder_delete (in_encoder);
973 return encoder;
976 /* Output the part of the symtab in SET and VSET. */
978 void
979 output_symtab (void)
981 struct cgraph_node *node;
982 struct lto_simple_output_block *ob;
983 int i, n_nodes;
984 lto_symtab_encoder_t encoder;
986 if (flag_wpa)
987 output_cgraph_opt_summary ();
989 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
991 output_profile_summary (ob);
993 /* An encoder for cgraph nodes should have been created by
994 ipa_write_summaries_1. */
995 gcc_assert (ob->decl_state->symtab_node_encoder);
996 encoder = ob->decl_state->symtab_node_encoder;
998 /* Write out the nodes. We must first output a node and then its clones,
999 otherwise at a time reading back the node there would be nothing to clone
1000 from. */
1001 n_nodes = lto_symtab_encoder_size (encoder);
1002 for (i = 0; i < n_nodes; i++)
1004 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1005 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1006 lto_output_node (ob, cnode, encoder);
1007 else
1008 lto_output_varpool_node (ob, dyn_cast<varpool_node *> (node), encoder);
1011 /* Go over the nodes in SET again to write edges. */
1012 for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
1014 node = dyn_cast <cgraph_node *> (lto_symtab_encoder_deref (encoder, i));
1015 if (node
1016 && (node->thunk.thunk_p
1017 || lto_symtab_encoder_in_partition_p (encoder, node)))
1019 output_outgoing_cgraph_edges (node->callees, ob, encoder);
1020 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
1024 streamer_write_uhwi_stream (ob->main_stream, 0);
1026 lto_destroy_simple_output_block (ob);
1028 /* Emit toplevel asms.
1029 When doing WPA we must output every asm just once. Since we do not partition asm
1030 nodes at all, output them to first output. This is kind of hack, but should work
1031 well. */
1032 if (!asm_nodes_output)
1034 asm_nodes_output = true;
1035 lto_output_toplevel_asms ();
1038 output_refs (encoder);
1041 /* Return identifier encoded in IB as a plain string. */
1043 static tree
1044 read_identifier (struct lto_input_block *ib)
1046 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1047 tree id;
1049 if (ib->data[ib->p + len])
1050 lto_section_overrun (ib);
1051 if (!len)
1053 ib->p++;
1054 return NULL;
1056 id = get_identifier (ib->data + ib->p);
1057 ib->p += len + 1;
1058 return id;
1061 /* Return string encoded in IB, NULL if string is empty. */
1063 static const char *
1064 read_string (struct lto_input_block *ib)
1066 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1067 const char *str;
1069 if (ib->data[ib->p + len])
1070 lto_section_overrun (ib);
1071 if (!len)
1073 ib->p++;
1074 return NULL;
1076 str = ib->data + ib->p;
1077 ib->p += len + 1;
1078 return str;
1081 /* Output function/variable tables that will allow libgomp to look up offload
1082 target code.
1083 OFFLOAD_FUNCS is filled in expand_omp_target, OFFLOAD_VARS is filled in
1084 varpool_node::get_create. In WHOPR (partitioned) mode during the WPA stage
1085 both OFFLOAD_FUNCS and OFFLOAD_VARS are filled by input_offload_tables. */
1087 void
1088 output_offload_tables (void)
1090 if (vec_safe_is_empty (offload_funcs) && vec_safe_is_empty (offload_vars))
1091 return;
1093 struct lto_simple_output_block *ob
1094 = lto_create_simple_output_block (LTO_section_offload_table);
1096 for (unsigned i = 0; i < vec_safe_length (offload_funcs); i++)
1098 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1099 LTO_symtab_last_tag, LTO_symtab_unavail_node);
1100 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
1101 (*offload_funcs)[i]);
1104 for (unsigned i = 0; i < vec_safe_length (offload_vars); i++)
1106 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1107 LTO_symtab_last_tag, LTO_symtab_variable);
1108 lto_output_var_decl_index (ob->decl_state, ob->main_stream,
1109 (*offload_vars)[i]);
1112 streamer_write_uhwi_stream (ob->main_stream, 0);
1113 lto_destroy_simple_output_block (ob);
1115 /* In WHOPR mode during the WPA stage the joint offload tables need to be
1116 streamed to one partition only. That's why we free offload_funcs and
1117 offload_vars after the first call of output_offload_tables. */
1118 if (flag_wpa)
1120 vec_free (offload_funcs);
1121 vec_free (offload_vars);
1125 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
1126 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
1127 NODE or to replace the values in it, for instance because the first
1128 time we saw it, the function body was not available but now it
1129 is. BP is a bitpack with all the bitflags for NODE read from the
1130 stream. */
1132 static void
1133 input_overwrite_node (struct lto_file_decl_data *file_data,
1134 struct cgraph_node *node,
1135 enum LTO_symtab_tags tag,
1136 struct bitpack_d *bp)
1138 node->aux = (void *) tag;
1139 node->lto_file_data = file_data;
1141 node->local.local = bp_unpack_value (bp, 1);
1142 node->externally_visible = bp_unpack_value (bp, 1);
1143 node->no_reorder = bp_unpack_value (bp, 1);
1144 node->definition = bp_unpack_value (bp, 1);
1145 node->local.versionable = bp_unpack_value (bp, 1);
1146 node->local.can_change_signature = bp_unpack_value (bp, 1);
1147 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
1148 node->force_output = bp_unpack_value (bp, 1);
1149 node->forced_by_abi = bp_unpack_value (bp, 1);
1150 node->unique_name = bp_unpack_value (bp, 1);
1151 node->body_removed = bp_unpack_value (bp, 1);
1152 node->implicit_section = bp_unpack_value (bp, 1);
1153 node->address_taken = bp_unpack_value (bp, 1);
1154 node->used_from_other_partition = bp_unpack_value (bp, 1);
1155 node->lowered = bp_unpack_value (bp, 1);
1156 node->analyzed = tag == LTO_symtab_analyzed_node;
1157 node->in_other_partition = bp_unpack_value (bp, 1);
1158 if (node->in_other_partition
1159 /* Avoid updating decl when we are seeing just inline clone.
1160 When inlining function that has functions already inlined into it,
1161 we produce clones of inline clones.
1163 WPA partitioning might put each clone into different unit and
1164 we might end up streaming inline clone from other partition
1165 to support clone we are interested in. */
1166 && (!node->clone_of
1167 || node->clone_of->decl != node->decl))
1169 DECL_EXTERNAL (node->decl) = 1;
1170 TREE_STATIC (node->decl) = 0;
1172 node->alias = bp_unpack_value (bp, 1);
1173 node->weakref = bp_unpack_value (bp, 1);
1174 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
1175 node->only_called_at_startup = bp_unpack_value (bp, 1);
1176 node->only_called_at_exit = bp_unpack_value (bp, 1);
1177 node->tm_clone = bp_unpack_value (bp, 1);
1178 node->calls_comdat_local = bp_unpack_value (bp, 1);
1179 node->icf_merged = bp_unpack_value (bp, 1);
1180 node->nonfreeing_fn = bp_unpack_value (bp, 1);
1181 node->thunk.thunk_p = bp_unpack_value (bp, 1);
1182 node->parallelized_function = bp_unpack_value (bp, 1);
1183 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
1184 LDPR_NUM_KNOWN);
1185 node->instrumentation_clone = bp_unpack_value (bp, 1);
1186 node->split_part = bp_unpack_value (bp, 1);
1187 gcc_assert (flag_ltrans
1188 || (!node->in_other_partition
1189 && !node->used_from_other_partition));
1192 /* Return string alias is alias of. */
1194 static tree
1195 get_alias_symbol (tree decl)
1197 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1198 return get_identifier (TREE_STRING_POINTER
1199 (TREE_VALUE (TREE_VALUE (alias))));
1202 /* Read a node from input_block IB. TAG is the node's tag just read.
1203 Return the node read or overwriten. */
1205 static struct cgraph_node *
1206 input_node (struct lto_file_decl_data *file_data,
1207 struct lto_input_block *ib,
1208 enum LTO_symtab_tags tag,
1209 vec<symtab_node *> nodes)
1211 gcc::pass_manager *passes = g->get_passes ();
1212 tree fn_decl;
1213 struct cgraph_node *node;
1214 struct bitpack_d bp;
1215 unsigned decl_index;
1216 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1217 int clone_ref;
1218 int order;
1219 int i, count;
1220 tree group;
1221 const char *section;
1222 order = streamer_read_hwi (ib) + order_base;
1223 clone_ref = streamer_read_hwi (ib);
1225 decl_index = streamer_read_uhwi (ib);
1226 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1228 if (clone_ref != LCC_NOT_FOUND)
1230 node = dyn_cast<cgraph_node *> (nodes[clone_ref])->create_clone (fn_decl,
1231 0, CGRAPH_FREQ_BASE, false,
1232 vNULL, false, NULL, NULL);
1234 else
1236 /* Declaration of functions can be already merged with a declaration
1237 from other input file. We keep cgraph unmerged until after streaming
1238 of ipa passes is done. Alays forcingly create a fresh node. */
1239 node = symtab->create_empty ();
1240 node->decl = fn_decl;
1241 node->register_symbol ();
1244 node->order = order;
1245 if (order >= symtab->order)
1246 symtab->order = order + 1;
1248 node->count = streamer_read_gcov_count (ib);
1249 node->count_materialization_scale = streamer_read_hwi (ib);
1251 count = streamer_read_hwi (ib);
1252 node->ipa_transforms_to_apply = vNULL;
1253 for (i = 0; i < count; i++)
1255 opt_pass *pass;
1256 int pid = streamer_read_hwi (ib);
1258 gcc_assert (pid < passes->passes_by_id_size);
1259 pass = passes->passes_by_id[pid];
1260 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *) pass);
1263 if (tag == LTO_symtab_analyzed_node)
1264 ref = streamer_read_hwi (ib);
1266 group = read_identifier (ib);
1267 if (group)
1268 ref2 = streamer_read_hwi (ib);
1270 /* Make sure that we have not read this node before. Nodes that
1271 have already been read will have their tag stored in the 'aux'
1272 field. Since built-in functions can be referenced in multiple
1273 functions, they are expected to be read more than once. */
1274 if (node->aux && !DECL_BUILT_IN (node->decl))
1275 internal_error ("bytecode stream: found multiple instances of cgraph "
1276 "node with uid %d", node->uid);
1278 node->tp_first_run = streamer_read_uhwi (ib);
1280 bp = streamer_read_bitpack (ib);
1282 input_overwrite_node (file_data, node, tag, &bp);
1284 /* Store a reference for now, and fix up later to be a pointer. */
1285 node->global.inlined_to = (cgraph_node *) (intptr_t) ref;
1287 if (group)
1289 node->set_comdat_group (group);
1290 /* Store a reference for now, and fix up later to be a pointer. */
1291 node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
1293 else
1294 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1295 section = read_string (ib);
1296 if (section)
1297 node->set_section_for_node (section);
1299 if (node->thunk.thunk_p)
1301 int type = streamer_read_uhwi (ib);
1302 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1303 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1305 node->thunk.fixed_offset = fixed_offset;
1306 node->thunk.this_adjusting = (type & 2);
1307 node->thunk.virtual_value = virtual_value;
1308 node->thunk.virtual_offset_p = (type & 4);
1309 node->thunk.add_pointer_bounds_args = (type & 8);
1311 if (node->alias && !node->analyzed && node->weakref)
1312 node->alias_target = get_alias_symbol (node->decl);
1313 node->profile_id = streamer_read_hwi (ib);
1314 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1315 node->set_init_priority (streamer_read_hwi (ib));
1316 if (DECL_STATIC_DESTRUCTOR (node->decl))
1317 node->set_fini_priority (streamer_read_hwi (ib));
1319 if (node->instrumentation_clone)
1321 decl_index = streamer_read_uhwi (ib);
1322 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1323 node->orig_decl = fn_decl;
1326 return node;
1329 /* Read a node from input_block IB. TAG is the node's tag just read.
1330 Return the node read or overwriten. */
1332 static varpool_node *
1333 input_varpool_node (struct lto_file_decl_data *file_data,
1334 struct lto_input_block *ib)
1336 int decl_index;
1337 tree var_decl;
1338 varpool_node *node;
1339 struct bitpack_d bp;
1340 int ref = LCC_NOT_FOUND;
1341 int order;
1342 tree group;
1343 const char *section;
1345 order = streamer_read_hwi (ib) + order_base;
1346 decl_index = streamer_read_uhwi (ib);
1347 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1349 /* Declaration of functions can be already merged with a declaration
1350 from other input file. We keep cgraph unmerged until after streaming
1351 of ipa passes is done. Alays forcingly create a fresh node. */
1352 node = varpool_node::create_empty ();
1353 node->decl = var_decl;
1354 node->register_symbol ();
1356 node->order = order;
1357 if (order >= symtab->order)
1358 symtab->order = order + 1;
1359 node->lto_file_data = file_data;
1361 bp = streamer_read_bitpack (ib);
1362 node->externally_visible = bp_unpack_value (&bp, 1);
1363 node->no_reorder = bp_unpack_value (&bp, 1);
1364 node->force_output = bp_unpack_value (&bp, 1);
1365 node->forced_by_abi = bp_unpack_value (&bp, 1);
1366 node->unique_name = bp_unpack_value (&bp, 1);
1367 node->body_removed = bp_unpack_value (&bp, 1);
1368 node->implicit_section = bp_unpack_value (&bp, 1);
1369 node->writeonly = bp_unpack_value (&bp, 1);
1370 node->definition = bp_unpack_value (&bp, 1);
1371 node->alias = bp_unpack_value (&bp, 1);
1372 node->weakref = bp_unpack_value (&bp, 1);
1373 node->analyzed = bp_unpack_value (&bp, 1);
1374 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1375 node->in_other_partition = bp_unpack_value (&bp, 1);
1376 if (node->in_other_partition)
1378 DECL_EXTERNAL (node->decl) = 1;
1379 TREE_STATIC (node->decl) = 0;
1381 if (node->alias && !node->analyzed && node->weakref)
1382 node->alias_target = get_alias_symbol (node->decl);
1383 node->tls_model = (enum tls_model)bp_unpack_value (&bp, 3);
1384 node->used_by_single_function = (enum tls_model)bp_unpack_value (&bp, 1);
1385 node->need_bounds_init = bp_unpack_value (&bp, 1);
1386 group = read_identifier (ib);
1387 if (group)
1389 node->set_comdat_group (group);
1390 ref = streamer_read_hwi (ib);
1391 /* Store a reference for now, and fix up later to be a pointer. */
1392 node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1394 else
1395 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1396 section = read_string (ib);
1397 if (section)
1398 node->set_section_for_node (section);
1399 node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1400 LDPR_NUM_KNOWN);
1401 gcc_assert (flag_ltrans
1402 || (!node->in_other_partition
1403 && !node->used_from_other_partition));
1405 return node;
1408 /* Read a node from input_block IB. TAG is the node's tag just read.
1409 Return the node read or overwriten. */
1411 static void
1412 input_ref (struct lto_input_block *ib,
1413 symtab_node *referring_node,
1414 vec<symtab_node *> nodes)
1416 symtab_node *node = NULL;
1417 struct bitpack_d bp;
1418 enum ipa_ref_use use;
1419 bool speculative;
1420 struct ipa_ref *ref;
1422 bp = streamer_read_bitpack (ib);
1423 use = (enum ipa_ref_use) bp_unpack_value (&bp, 3);
1424 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1425 node = nodes[streamer_read_hwi (ib)];
1426 ref = referring_node->create_reference (node, use);
1427 ref->speculative = speculative;
1428 if (is_a <cgraph_node *> (referring_node))
1429 ref->lto_stmt_uid = streamer_read_hwi (ib);
1432 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1433 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1434 edge being read is indirect (in the sense that it has
1435 indirect_unknown_callee set). */
1437 static void
1438 input_edge (struct lto_input_block *ib, vec<symtab_node *> nodes,
1439 bool indirect)
1441 struct cgraph_node *caller, *callee;
1442 struct cgraph_edge *edge;
1443 unsigned int stmt_id;
1444 gcov_type count;
1445 int freq;
1446 cgraph_inline_failed_t inline_failed;
1447 struct bitpack_d bp;
1448 int ecf_flags = 0;
1450 caller = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1451 if (caller == NULL || caller->decl == NULL_TREE)
1452 internal_error ("bytecode stream: no caller found while reading edge");
1454 if (!indirect)
1456 callee = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1457 if (callee == NULL || callee->decl == NULL_TREE)
1458 internal_error ("bytecode stream: no callee found while reading edge");
1460 else
1461 callee = NULL;
1463 count = streamer_read_gcov_count (ib);
1465 bp = streamer_read_bitpack (ib);
1466 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_t, CIF_N_REASONS);
1467 stmt_id = bp_unpack_var_len_unsigned (&bp);
1468 freq = (int) bp_unpack_var_len_unsigned (&bp);
1470 if (indirect)
1471 edge = caller->create_indirect_edge (NULL, 0, count, freq);
1472 else
1473 edge = caller->create_edge (callee, NULL, count, freq);
1475 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1476 edge->speculative = bp_unpack_value (&bp, 1);
1477 edge->lto_stmt_uid = stmt_id;
1478 edge->inline_failed = inline_failed;
1479 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1480 edge->can_throw_external = bp_unpack_value (&bp, 1);
1481 edge->in_polymorphic_cdtor = bp_unpack_value (&bp, 1);
1482 if (indirect)
1484 if (bp_unpack_value (&bp, 1))
1485 ecf_flags |= ECF_CONST;
1486 if (bp_unpack_value (&bp, 1))
1487 ecf_flags |= ECF_PURE;
1488 if (bp_unpack_value (&bp, 1))
1489 ecf_flags |= ECF_NORETURN;
1490 if (bp_unpack_value (&bp, 1))
1491 ecf_flags |= ECF_MALLOC;
1492 if (bp_unpack_value (&bp, 1))
1493 ecf_flags |= ECF_NOTHROW;
1494 if (bp_unpack_value (&bp, 1))
1495 ecf_flags |= ECF_RETURNS_TWICE;
1496 edge->indirect_info->ecf_flags = ecf_flags;
1497 edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1498 if (edge->indirect_info->common_target_id)
1499 edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1504 /* Read a cgraph from IB using the info in FILE_DATA. */
1506 static vec<symtab_node *>
1507 input_cgraph_1 (struct lto_file_decl_data *file_data,
1508 struct lto_input_block *ib)
1510 enum LTO_symtab_tags tag;
1511 vec<symtab_node *> nodes = vNULL;
1512 symtab_node *node;
1513 unsigned i;
1515 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1516 order_base = symtab->order;
1517 while (tag)
1519 if (tag == LTO_symtab_edge)
1520 input_edge (ib, nodes, false);
1521 else if (tag == LTO_symtab_indirect_edge)
1522 input_edge (ib, nodes, true);
1523 else if (tag == LTO_symtab_variable)
1525 node = input_varpool_node (file_data, ib);
1526 nodes.safe_push (node);
1527 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1529 else
1531 node = input_node (file_data, ib, tag, nodes);
1532 if (node == NULL || node->decl == NULL_TREE)
1533 internal_error ("bytecode stream: found empty cgraph node");
1534 nodes.safe_push (node);
1535 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1538 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1541 lto_input_toplevel_asms (file_data, order_base);
1543 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1544 if (flag_checking)
1546 FOR_EACH_VEC_ELT (nodes, i, node)
1547 gcc_assert (node->aux || !is_a <cgraph_node *> (node));
1549 FOR_EACH_VEC_ELT (nodes, i, node)
1551 int ref;
1552 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1554 ref = (int) (intptr_t) cnode->global.inlined_to;
1556 /* We share declaration of builtins, so we may read same node twice. */
1557 if (!node->aux)
1558 continue;
1559 node->aux = NULL;
1561 /* Fixup inlined_to from reference to pointer. */
1562 if (ref != LCC_NOT_FOUND)
1563 dyn_cast<cgraph_node *> (node)->global.inlined_to
1564 = dyn_cast<cgraph_node *> (nodes[ref]);
1565 else
1566 cnode->global.inlined_to = NULL;
1568 /* Compute instrumented_version. */
1569 if (cnode->instrumentation_clone)
1571 gcc_assert (cnode->orig_decl);
1573 cnode->instrumented_version = cgraph_node::get (cnode->orig_decl);
1574 if (cnode->instrumented_version)
1576 /* We may have multiple nodes for a single function which
1577 will be merged later. To have a proper merge we need
1578 to keep instrumentation_version reference between nodes
1579 consistent: each instrumented_version reference should
1580 have proper reverse reference. Thus don't break existing
1581 instrumented_version reference if it already exists. */
1582 if (cnode->instrumented_version->instrumented_version)
1583 cnode->instrumented_version = NULL;
1584 else
1585 cnode->instrumented_version->instrumented_version = cnode;
1588 /* Restore decl names reference except for wrapper functions. */
1589 if (!chkp_wrap_function (cnode->orig_decl))
1591 tree name = DECL_ASSEMBLER_NAME (cnode->decl);
1592 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
1593 TREE_CHAIN (name) = DECL_ASSEMBLER_NAME (cnode->orig_decl);
1598 ref = (int) (intptr_t) node->same_comdat_group;
1600 /* Fixup same_comdat_group from reference to pointer. */
1601 if (ref != LCC_NOT_FOUND)
1602 node->same_comdat_group = nodes[ref];
1603 else
1604 node->same_comdat_group = NULL;
1606 FOR_EACH_VEC_ELT (nodes, i, node)
1607 node->aux = is_a <cgraph_node *> (node) ? (void *)1 : NULL;
1608 return nodes;
1611 /* Input ipa_refs. */
1613 static void
1614 input_refs (struct lto_input_block *ib,
1615 vec<symtab_node *> nodes)
1617 int count;
1618 int idx;
1619 while (true)
1621 symtab_node *node;
1622 count = streamer_read_uhwi (ib);
1623 if (!count)
1624 break;
1625 idx = streamer_read_uhwi (ib);
1626 node = nodes[idx];
1627 while (count)
1629 input_ref (ib, node, nodes);
1630 count--;
1636 static struct gcov_ctr_summary lto_gcov_summary;
1638 /* Input profile_info from IB. */
1639 static void
1640 input_profile_summary (struct lto_input_block *ib,
1641 struct lto_file_decl_data *file_data)
1643 unsigned h_ix;
1644 struct bitpack_d bp;
1645 unsigned int runs = streamer_read_uhwi (ib);
1646 if (runs)
1648 file_data->profile_info.runs = runs;
1649 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1650 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1652 memset (file_data->profile_info.histogram, 0,
1653 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1654 /* Input the bitpack of non-zero histogram indices. */
1655 bp = streamer_read_bitpack (ib);
1656 /* Read in and unpack the full bitpack, flagging non-zero
1657 histogram entries by setting the num_counters non-zero. */
1658 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1660 file_data->profile_info.histogram[h_ix].num_counters
1661 = bp_unpack_value (&bp, 1);
1663 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1665 if (!file_data->profile_info.histogram[h_ix].num_counters)
1666 continue;
1668 file_data->profile_info.histogram[h_ix].num_counters
1669 = streamer_read_gcov_count (ib);
1670 file_data->profile_info.histogram[h_ix].min_value
1671 = streamer_read_gcov_count (ib);
1672 file_data->profile_info.histogram[h_ix].cum_value
1673 = streamer_read_gcov_count (ib);
1675 /* IPA-profile computes hot bb threshold based on cumulated
1676 whole program profile. We need to stream it down to ltrans. */
1677 if (flag_ltrans)
1678 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1683 /* Rescale profile summaries to the same number of runs in the whole unit. */
1685 static void
1686 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1688 struct lto_file_decl_data *file_data;
1689 unsigned int j, h_ix;
1690 gcov_unsigned_t max_runs = 0;
1691 struct cgraph_node *node;
1692 struct cgraph_edge *edge;
1693 gcov_type saved_sum_all = 0;
1694 gcov_ctr_summary *saved_profile_info = 0;
1695 int saved_scale = 0;
1697 /* Find unit with maximal number of runs. If we ever get serious about
1698 roundoff errors, we might also consider computing smallest common
1699 multiply. */
1700 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1701 if (max_runs < file_data->profile_info.runs)
1702 max_runs = file_data->profile_info.runs;
1704 if (!max_runs)
1705 return;
1707 /* Simple overflow check. We probably don't need to support that many train
1708 runs. Such a large value probably imply data corruption anyway. */
1709 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1711 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1712 INT_MAX / REG_BR_PROB_BASE);
1713 return;
1716 profile_info = &lto_gcov_summary;
1717 lto_gcov_summary.runs = max_runs;
1718 lto_gcov_summary.sum_max = 0;
1719 memset (lto_gcov_summary.histogram, 0,
1720 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1722 /* Rescale all units to the maximal number of runs.
1723 sum_max can not be easily merged, as we have no idea what files come from
1724 the same run. We do not use the info anyway, so leave it 0. */
1725 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1726 if (file_data->profile_info.runs)
1728 int scale = GCOV_COMPUTE_SCALE (max_runs,
1729 file_data->profile_info.runs);
1730 lto_gcov_summary.sum_max
1731 = MAX (lto_gcov_summary.sum_max,
1732 apply_scale (file_data->profile_info.sum_max, scale));
1733 lto_gcov_summary.sum_all
1734 = MAX (lto_gcov_summary.sum_all,
1735 apply_scale (file_data->profile_info.sum_all, scale));
1736 /* Save a pointer to the profile_info with the largest
1737 scaled sum_all and the scale for use in merging the
1738 histogram. */
1739 if (!saved_profile_info
1740 || lto_gcov_summary.sum_all > saved_sum_all)
1742 saved_profile_info = &file_data->profile_info;
1743 saved_sum_all = lto_gcov_summary.sum_all;
1744 saved_scale = scale;
1748 gcc_assert (saved_profile_info);
1750 /* Scale up the histogram from the profile that had the largest
1751 scaled sum_all above. */
1752 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1754 /* Scale up the min value as we did the corresponding sum_all
1755 above. Use that to find the new histogram index. */
1756 gcov_type scaled_min
1757 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1758 saved_scale);
1759 /* The new index may be shared with another scaled histogram entry,
1760 so we need to account for a non-zero histogram entry at new_ix. */
1761 unsigned new_ix = gcov_histo_index (scaled_min);
1762 lto_gcov_summary.histogram[new_ix].min_value
1763 = (lto_gcov_summary.histogram[new_ix].num_counters
1764 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1765 : scaled_min);
1766 /* Some of the scaled counter values would ostensibly need to be placed
1767 into different (larger) histogram buckets, but we keep things simple
1768 here and place the scaled cumulative counter value in the bucket
1769 corresponding to the scaled minimum counter value. */
1770 lto_gcov_summary.histogram[new_ix].cum_value
1771 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1772 saved_scale);
1773 lto_gcov_summary.histogram[new_ix].num_counters
1774 += saved_profile_info->histogram[h_ix].num_counters;
1777 /* Watch roundoff errors. */
1778 if (lto_gcov_summary.sum_max < max_runs)
1779 lto_gcov_summary.sum_max = max_runs;
1781 /* If merging already happent at WPA time, we are done. */
1782 if (flag_ltrans)
1783 return;
1785 /* Now compute count_materialization_scale of each node.
1786 During LTRANS we already have values of count_materialization_scale
1787 computed, so just update them. */
1788 FOR_EACH_FUNCTION (node)
1789 if (node->lto_file_data
1790 && node->lto_file_data->profile_info.runs)
1792 int scale;
1794 scale = RDIV (node->count_materialization_scale * max_runs,
1795 node->lto_file_data->profile_info.runs);
1796 node->count_materialization_scale = scale;
1797 if (scale < 0)
1798 fatal_error (input_location, "Profile information in %s corrupted",
1799 file_data->file_name);
1801 if (scale == REG_BR_PROB_BASE)
1802 continue;
1803 for (edge = node->callees; edge; edge = edge->next_callee)
1804 edge->count = apply_scale (edge->count, scale);
1805 node->count = apply_scale (node->count, scale);
1809 /* Input and merge the symtab from each of the .o files passed to
1810 lto1. */
1812 void
1813 input_symtab (void)
1815 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1816 struct lto_file_decl_data *file_data;
1817 unsigned int j = 0;
1818 struct cgraph_node *node;
1820 while ((file_data = file_data_vec[j++]))
1822 const char *data;
1823 size_t len;
1824 struct lto_input_block *ib;
1825 vec<symtab_node *> nodes;
1827 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1828 &data, &len);
1829 if (!ib)
1830 fatal_error (input_location,
1831 "cannot find LTO cgraph in %s", file_data->file_name);
1832 input_profile_summary (ib, file_data);
1833 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1834 nodes = input_cgraph_1 (file_data, ib);
1835 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1836 ib, data, len);
1838 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1839 &data, &len);
1840 if (!ib)
1841 fatal_error (input_location, "cannot find LTO section refs in %s",
1842 file_data->file_name);
1843 input_refs (ib, nodes);
1844 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1845 ib, data, len);
1846 if (flag_ltrans)
1847 input_cgraph_opt_summary (nodes);
1848 nodes.release ();
1851 merge_profile_summaries (file_data_vec);
1852 get_working_sets ();
1855 /* Clear out the aux field that was used to store enough state to
1856 tell which nodes should be overwritten. */
1857 FOR_EACH_FUNCTION (node)
1859 /* Some nodes may have been created by cgraph_node. This
1860 happens when the callgraph contains nested functions. If the
1861 node for the parent function was never emitted to the gimple
1862 file, cgraph_node will create a node for it when setting the
1863 context of the nested function. */
1864 if (node->lto_file_data)
1865 node->aux = NULL;
1869 /* Input function/variable tables that will allow libgomp to look up offload
1870 target code, and store them into OFFLOAD_FUNCS and OFFLOAD_VARS. */
1872 void
1873 input_offload_tables (void)
1875 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1876 struct lto_file_decl_data *file_data;
1877 unsigned int j = 0;
1879 while ((file_data = file_data_vec[j++]))
1881 const char *data;
1882 size_t len;
1883 struct lto_input_block *ib
1884 = lto_create_simple_input_block (file_data, LTO_section_offload_table,
1885 &data, &len);
1886 if (!ib)
1887 continue;
1889 enum LTO_symtab_tags tag
1890 = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1891 while (tag)
1893 if (tag == LTO_symtab_unavail_node)
1895 int decl_index = streamer_read_uhwi (ib);
1896 tree fn_decl
1897 = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1898 vec_safe_push (offload_funcs, fn_decl);
1900 else if (tag == LTO_symtab_variable)
1902 int decl_index = streamer_read_uhwi (ib);
1903 tree var_decl
1904 = lto_file_decl_data_get_var_decl (file_data, decl_index);
1905 vec_safe_push (offload_vars, var_decl);
1907 else
1908 fatal_error (input_location,
1909 "invalid offload table in %s", file_data->file_name);
1911 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1914 lto_destroy_simple_input_block (file_data, LTO_section_offload_table,
1915 ib, data, len);
1919 /* True when we need optimization summary for NODE. */
1921 static int
1922 output_cgraph_opt_summary_p (struct cgraph_node *node)
1924 return (node->clone_of
1925 && (node->clone.tree_map
1926 || node->clone.args_to_skip
1927 || node->clone.combined_args_to_skip));
1930 /* Output optimization summary for EDGE to OB. */
1931 static void
1932 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1933 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1937 /* Output optimization summary for NODE to OB. */
1939 static void
1940 output_node_opt_summary (struct output_block *ob,
1941 struct cgraph_node *node,
1942 lto_symtab_encoder_t encoder)
1944 unsigned int index;
1945 bitmap_iterator bi;
1946 struct ipa_replace_map *map;
1947 struct bitpack_d bp;
1948 int i;
1949 struct cgraph_edge *e;
1951 if (node->clone.args_to_skip)
1953 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1954 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1955 streamer_write_uhwi (ob, index);
1957 else
1958 streamer_write_uhwi (ob, 0);
1959 if (node->clone.combined_args_to_skip)
1961 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1962 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1963 streamer_write_uhwi (ob, index);
1965 else
1966 streamer_write_uhwi (ob, 0);
1967 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1968 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1970 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1971 mechanism to store function local declarations into summaries. */
1972 gcc_assert (!map->old_tree);
1973 streamer_write_uhwi (ob, map->parm_num);
1974 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1975 stream_write_tree (ob, map->new_tree, true);
1976 bp = bitpack_create (ob->main_stream);
1977 bp_pack_value (&bp, map->replace_p, 1);
1978 bp_pack_value (&bp, map->ref_p, 1);
1979 streamer_write_bitpack (&bp);
1982 if (lto_symtab_encoder_in_partition_p (encoder, node))
1984 for (e = node->callees; e; e = e->next_callee)
1985 output_edge_opt_summary (ob, e);
1986 for (e = node->indirect_calls; e; e = e->next_callee)
1987 output_edge_opt_summary (ob, e);
1991 /* Output optimization summaries stored in callgraph.
1992 At the moment it is the clone info structure. */
1994 static void
1995 output_cgraph_opt_summary (void)
1997 int i, n_nodes;
1998 lto_symtab_encoder_t encoder;
1999 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
2000 unsigned count = 0;
2002 ob->symbol = NULL;
2003 encoder = ob->decl_state->symtab_node_encoder;
2004 n_nodes = lto_symtab_encoder_size (encoder);
2005 for (i = 0; i < n_nodes; i++)
2007 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
2008 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2009 if (cnode && output_cgraph_opt_summary_p (cnode))
2010 count++;
2012 streamer_write_uhwi (ob, count);
2013 for (i = 0; i < n_nodes; i++)
2015 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
2016 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2017 if (cnode && output_cgraph_opt_summary_p (cnode))
2019 streamer_write_uhwi (ob, i);
2020 output_node_opt_summary (ob, cnode, encoder);
2023 produce_asm (ob, NULL);
2024 destroy_output_block (ob);
2027 /* Input optimisation summary of EDGE. */
2029 static void
2030 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
2031 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
2035 /* Input optimisation summary of NODE. */
2037 static void
2038 input_node_opt_summary (struct cgraph_node *node,
2039 struct lto_input_block *ib_main,
2040 struct data_in *data_in)
2042 int i;
2043 int count;
2044 int bit;
2045 struct bitpack_d bp;
2046 struct cgraph_edge *e;
2048 count = streamer_read_uhwi (ib_main);
2049 if (count)
2050 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
2051 for (i = 0; i < count; i++)
2053 bit = streamer_read_uhwi (ib_main);
2054 bitmap_set_bit (node->clone.args_to_skip, bit);
2056 count = streamer_read_uhwi (ib_main);
2057 if (count)
2058 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
2059 for (i = 0; i < count; i++)
2061 bit = streamer_read_uhwi (ib_main);
2062 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
2064 count = streamer_read_uhwi (ib_main);
2065 for (i = 0; i < count; i++)
2067 struct ipa_replace_map *map = ggc_alloc<ipa_replace_map> ();
2069 vec_safe_push (node->clone.tree_map, map);
2070 map->parm_num = streamer_read_uhwi (ib_main);
2071 map->old_tree = NULL;
2072 map->new_tree = stream_read_tree (ib_main, data_in);
2073 bp = streamer_read_bitpack (ib_main);
2074 map->replace_p = bp_unpack_value (&bp, 1);
2075 map->ref_p = bp_unpack_value (&bp, 1);
2077 for (e = node->callees; e; e = e->next_callee)
2078 input_edge_opt_summary (e, ib_main);
2079 for (e = node->indirect_calls; e; e = e->next_callee)
2080 input_edge_opt_summary (e, ib_main);
2083 /* Read section in file FILE_DATA of length LEN with data DATA. */
2085 static void
2086 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
2087 const char *data, size_t len,
2088 vec<symtab_node *> nodes)
2090 const struct lto_function_header *header =
2091 (const struct lto_function_header *) data;
2092 const int cfg_offset = sizeof (struct lto_function_header);
2093 const int main_offset = cfg_offset + header->cfg_size;
2094 const int string_offset = main_offset + header->main_size;
2095 struct data_in *data_in;
2096 unsigned int i;
2097 unsigned int count;
2099 lto_input_block ib_main ((const char *) data + main_offset,
2100 header->main_size, file_data->mode_table);
2102 data_in =
2103 lto_data_in_create (file_data, (const char *) data + string_offset,
2104 header->string_size, vNULL);
2105 count = streamer_read_uhwi (&ib_main);
2107 for (i = 0; i < count; i++)
2109 int ref = streamer_read_uhwi (&ib_main);
2110 input_node_opt_summary (dyn_cast<cgraph_node *> (nodes[ref]),
2111 &ib_main, data_in);
2113 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
2114 len);
2115 lto_data_in_delete (data_in);
2118 /* Input optimization summary of cgraph. */
2120 static void
2121 input_cgraph_opt_summary (vec<symtab_node *> nodes)
2123 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2124 struct lto_file_decl_data *file_data;
2125 unsigned int j = 0;
2127 while ((file_data = file_data_vec[j++]))
2129 size_t len;
2130 const char *data =
2131 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
2132 &len);
2134 if (data)
2135 input_cgraph_opt_section (file_data, data, len, nodes);