]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/lto-cgraph.c
gimple-predict.h: New file.
[thirdparty/gcc.git] / gcc / lto-cgraph.c
1 /* Write and read the cgraph to the memory mapped representation of a
2 .o file.
3
4 Copyright (C) 2009-2015 Free Software Foundation, Inc.
5 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
6
7 This file is part of GCC.
8
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.
13
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.
18
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/>. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "backend.h"
27 #include "predict.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "rtl.h"
31 #include "alias.h"
32 #include "fold-const.h"
33 #include "stringpool.h"
34 #include "internal-fn.h"
35 #include "flags.h"
36 #include "insn-config.h"
37 #include "expmed.h"
38 #include "dojump.h"
39 #include "explow.h"
40 #include "calls.h"
41 #include "emit-rtl.h"
42 #include "varasm.h"
43 #include "stmt.h"
44 #include "expr.h"
45 #include "params.h"
46 #include "langhooks.h"
47 #include "diagnostic-core.h"
48 #include "except.h"
49 #include "timevar.h"
50 #include "cgraph.h"
51 #include "lto-streamer.h"
52 #include "data-streamer.h"
53 #include "tree-streamer.h"
54 #include "gcov-io.h"
55 #include "tree-pass.h"
56 #include "profile.h"
57 #include "context.h"
58 #include "pass_manager.h"
59 #include "ipa-utils.h"
60 #include "omp-low.h"
61 #include "ipa-chkp.h"
62
63 /* True when asm nodes has been output. */
64 bool asm_nodes_output = false;
65
66 static void output_cgraph_opt_summary (void);
67 static void input_cgraph_opt_summary (vec<symtab_node *> nodes);
68
69 /* Number of LDPR values known to GCC. */
70 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
71
72 /* All node orders are ofsetted by ORDER_BASE. */
73 static int order_base;
74
75 /* Cgraph streaming is organized as set of record whose type
76 is indicated by a tag. */
77 enum LTO_symtab_tags
78 {
79 /* Must leave 0 for the stopper. */
80
81 /* Cgraph node without body available. */
82 LTO_symtab_unavail_node = 1,
83 /* Cgraph node with function body. */
84 LTO_symtab_analyzed_node,
85 /* Cgraph edges. */
86 LTO_symtab_edge,
87 LTO_symtab_indirect_edge,
88 LTO_symtab_variable,
89 LTO_symtab_last_tag
90 };
91
92 /* Create a new symtab encoder.
93 if FOR_INPUT, the encoder allocate only datastructures needed
94 to read the symtab. */
95
96 lto_symtab_encoder_t
97 lto_symtab_encoder_new (bool for_input)
98 {
99 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
100
101 if (!for_input)
102 encoder->map = new hash_map<symtab_node *, size_t>;
103 encoder->nodes.create (0);
104 return encoder;
105 }
106
107
108 /* Delete ENCODER and its components. */
109
110 void
111 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
112 {
113 encoder->nodes.release ();
114 if (encoder->map)
115 delete encoder->map;
116 free (encoder);
117 }
118
119
120 /* Return the existing reference number of NODE in the symtab encoder in
121 output block OB. Assign a new reference if this is the first time
122 NODE is encoded. */
123
124 int
125 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
126 symtab_node *node)
127 {
128 int ref;
129
130 if (!encoder->map)
131 {
132 lto_encoder_entry entry = {node, false, false, false};
133
134 ref = encoder->nodes.length ();
135 encoder->nodes.safe_push (entry);
136 return ref;
137 }
138
139 size_t *slot = encoder->map->get (node);
140 if (!slot || !*slot)
141 {
142 lto_encoder_entry entry = {node, false, false, false};
143 ref = encoder->nodes.length ();
144 if (!slot)
145 encoder->map->put (node, ref + 1);
146 encoder->nodes.safe_push (entry);
147 }
148 else
149 ref = *slot - 1;
150
151 return ref;
152 }
153
154 /* Remove NODE from encoder. */
155
156 bool
157 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
158 symtab_node *node)
159 {
160 int index;
161 lto_encoder_entry last_node;
162
163 size_t *slot = encoder->map->get (node);
164 if (slot == NULL || !*slot)
165 return false;
166
167 index = *slot - 1;
168 gcc_checking_assert (encoder->nodes[index].node == node);
169
170 /* Remove from vector. We do this by swapping node with the last element
171 of the vector. */
172 last_node = encoder->nodes.pop ();
173 if (last_node.node != node)
174 {
175 gcc_assert (encoder->map->put (last_node.node, index + 1));
176
177 /* Move the last element to the original spot of NODE. */
178 encoder->nodes[index] = last_node;
179 }
180
181 /* Remove element from hash table. */
182 encoder->map->remove (node);
183 return true;
184 }
185
186
187 /* Return TRUE if we should encode the body of NODE (if any). */
188
189 bool
190 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
191 struct cgraph_node *node)
192 {
193 int index = lto_symtab_encoder_lookup (encoder, node);
194 return encoder->nodes[index].body;
195 }
196
197 /* Specify that we encode the body of NODE in this partition. */
198
199 static void
200 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
201 struct cgraph_node *node)
202 {
203 int index = lto_symtab_encoder_encode (encoder, node);
204 gcc_checking_assert (encoder->nodes[index].node == node);
205 encoder->nodes[index].body = true;
206 }
207
208 /* Return TRUE if we should encode initializer of NODE (if any). */
209
210 bool
211 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
212 varpool_node *node)
213 {
214 int index = lto_symtab_encoder_lookup (encoder, node);
215 if (index == LCC_NOT_FOUND)
216 return false;
217 return encoder->nodes[index].initializer;
218 }
219
220 /* Specify that we should encode initializer of NODE (if any). */
221
222 static void
223 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
224 varpool_node *node)
225 {
226 int index = lto_symtab_encoder_lookup (encoder, node);
227 encoder->nodes[index].initializer = true;
228 }
229
230 /* Return TRUE if NODE is in this partition. */
231
232 bool
233 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
234 symtab_node *node)
235 {
236 int index = lto_symtab_encoder_lookup (encoder, node);
237 if (index == LCC_NOT_FOUND)
238 return false;
239 return encoder->nodes[index].in_partition;
240 }
241
242 /* Specify that NODE is in this partition. */
243
244 void
245 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
246 symtab_node *node)
247 {
248 int index = lto_symtab_encoder_encode (encoder, node);
249 encoder->nodes[index].in_partition = true;
250 }
251
252 /* Output the cgraph EDGE to OB using ENCODER. */
253
254 static void
255 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
256 lto_symtab_encoder_t encoder)
257 {
258 unsigned int uid;
259 intptr_t ref;
260 struct bitpack_d bp;
261
262 if (edge->indirect_unknown_callee)
263 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
264 LTO_symtab_indirect_edge);
265 else
266 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
267 LTO_symtab_edge);
268
269 ref = lto_symtab_encoder_lookup (encoder, edge->caller);
270 gcc_assert (ref != LCC_NOT_FOUND);
271 streamer_write_hwi_stream (ob->main_stream, ref);
272
273 if (!edge->indirect_unknown_callee)
274 {
275 ref = lto_symtab_encoder_lookup (encoder, edge->callee);
276 gcc_assert (ref != LCC_NOT_FOUND);
277 streamer_write_hwi_stream (ob->main_stream, ref);
278 }
279
280 streamer_write_gcov_count_stream (ob->main_stream, edge->count);
281
282 bp = bitpack_create (ob->main_stream);
283 uid = (!gimple_has_body_p (edge->caller->decl)
284 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt) + 1);
285 bp_pack_enum (&bp, cgraph_inline_failed_t,
286 CIF_N_REASONS, edge->inline_failed);
287 bp_pack_var_len_unsigned (&bp, uid);
288 bp_pack_var_len_unsigned (&bp, edge->frequency);
289 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
290 bp_pack_value (&bp, edge->speculative, 1);
291 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
292 bp_pack_value (&bp, edge->can_throw_external, 1);
293 bp_pack_value (&bp, edge->in_polymorphic_cdtor, 1);
294 if (edge->indirect_unknown_callee)
295 {
296 int flags = edge->indirect_info->ecf_flags;
297 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
298 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
299 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
300 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
301 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
302 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
303 /* Flags that should not appear on indirect calls. */
304 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
305 | ECF_MAY_BE_ALLOCA
306 | ECF_SIBCALL
307 | ECF_LEAF
308 | ECF_NOVOPS)));
309 }
310 streamer_write_bitpack (&bp);
311 if (edge->indirect_unknown_callee)
312 {
313 streamer_write_hwi_stream (ob->main_stream,
314 edge->indirect_info->common_target_id);
315 if (edge->indirect_info->common_target_id)
316 streamer_write_hwi_stream
317 (ob->main_stream, edge->indirect_info->common_target_probability);
318 }
319 }
320
321 /* Return if NODE contain references from other partitions. */
322
323 bool
324 referenced_from_other_partition_p (symtab_node *node, lto_symtab_encoder_t encoder)
325 {
326 int i;
327 struct ipa_ref *ref = NULL;
328
329 for (i = 0; node->iterate_referring (i, ref); i++)
330 {
331 /* Ignore references from non-offloadable nodes while streaming NODE into
332 offload LTO section. */
333 if (!ref->referring->need_lto_streaming)
334 continue;
335
336 if (ref->referring->in_other_partition
337 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
338 return true;
339 }
340 return false;
341 }
342
343 /* Return true when node is reachable from other partition. */
344
345 bool
346 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
347 {
348 struct cgraph_edge *e;
349 if (!node->definition)
350 return false;
351 if (node->global.inlined_to)
352 return false;
353 for (e = node->callers; e; e = e->next_caller)
354 {
355 /* Ignore references from non-offloadable nodes while streaming NODE into
356 offload LTO section. */
357 if (!e->caller->need_lto_streaming)
358 continue;
359
360 if (e->caller->in_other_partition
361 || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
362 return true;
363 }
364 return false;
365 }
366
367 /* Return if NODE contain references from other partitions. */
368
369 bool
370 referenced_from_this_partition_p (symtab_node *node,
371 lto_symtab_encoder_t encoder)
372 {
373 int i;
374 struct ipa_ref *ref = NULL;
375
376 for (i = 0; node->iterate_referring (i, ref); i++)
377 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
378 return true;
379 return false;
380 }
381
382 /* Return true when node is reachable from other partition. */
383
384 bool
385 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
386 {
387 struct cgraph_edge *e;
388 for (e = node->callers; e; e = e->next_caller)
389 if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
390 return true;
391 return false;
392 }
393
394 /* Output the cgraph NODE to OB. ENCODER is used to find the
395 reference number of NODE->inlined_to. SET is the set of nodes we
396 are writing to the current file. If NODE is not in SET, then NODE
397 is a boundary of a cgraph_node_set and we pretend NODE just has a
398 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
399 that have had their callgraph node written so far. This is used to
400 determine if NODE is a clone of a previously written node. */
401
402 static void
403 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
404 lto_symtab_encoder_t encoder)
405 {
406 unsigned int tag;
407 struct bitpack_d bp;
408 bool boundary_p;
409 intptr_t ref;
410 bool in_other_partition = false;
411 struct cgraph_node *clone_of, *ultimate_clone_of;
412 ipa_opt_pass_d *pass;
413 int i;
414 const char *comdat;
415 const char *section;
416 tree group;
417
418 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
419
420 if (node->analyzed && (!boundary_p || node->alias || node->thunk.thunk_p))
421 tag = LTO_symtab_analyzed_node;
422 else
423 tag = LTO_symtab_unavail_node;
424
425 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
426 tag);
427 streamer_write_hwi_stream (ob->main_stream, node->order);
428
429 /* In WPA mode, we only output part of the call-graph. Also, we
430 fake cgraph node attributes. There are two cases that we care.
431
432 Boundary nodes: There are nodes that are not part of SET but are
433 called from within SET. We artificially make them look like
434 externally visible nodes with no function body.
435
436 Cherry-picked nodes: These are nodes we pulled from other
437 translation units into SET during IPA-inlining. We make them as
438 local static nodes to prevent clashes with other local statics. */
439 if (boundary_p && node->analyzed
440 && node->get_partitioning_class () == SYMBOL_PARTITION)
441 {
442 /* Inline clones can not be part of boundary.
443 gcc_assert (!node->global.inlined_to);
444
445 FIXME: At the moment they can be, when partition contains an inline
446 clone that is clone of inline clone from outside partition. We can
447 reshape the clone tree and make other tree to be the root, but it
448 needs a bit extra work and will be promplty done by cgraph_remove_node
449 after reading back. */
450 in_other_partition = 1;
451 }
452
453 clone_of = node->clone_of;
454 while (clone_of
455 && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
456 if (clone_of->prev_sibling_clone)
457 clone_of = clone_of->prev_sibling_clone;
458 else
459 clone_of = clone_of->clone_of;
460
461 /* See if body of the master function is output. If not, we are seeing only
462 an declaration and we do not need to pass down clone tree. */
463 ultimate_clone_of = clone_of;
464 while (ultimate_clone_of && ultimate_clone_of->clone_of)
465 ultimate_clone_of = ultimate_clone_of->clone_of;
466
467 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
468 clone_of = NULL;
469
470 if (tag == LTO_symtab_analyzed_node)
471 gcc_assert (clone_of || !node->clone_of);
472 if (!clone_of)
473 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
474 else
475 streamer_write_hwi_stream (ob->main_stream, ref);
476
477
478 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
479 streamer_write_gcov_count_stream (ob->main_stream, node->count);
480 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
481
482 streamer_write_hwi_stream (ob->main_stream,
483 node->ipa_transforms_to_apply.length ());
484 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
485 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
486
487 if (tag == LTO_symtab_analyzed_node)
488 {
489 if (node->global.inlined_to)
490 {
491 ref = lto_symtab_encoder_lookup (encoder, node->global.inlined_to);
492 gcc_assert (ref != LCC_NOT_FOUND);
493 }
494 else
495 ref = LCC_NOT_FOUND;
496
497 streamer_write_hwi_stream (ob->main_stream, ref);
498 }
499
500 group = node->get_comdat_group ();
501 if (group)
502 comdat = IDENTIFIER_POINTER (group);
503 else
504 comdat = "";
505 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
506
507 if (group)
508 {
509 if (node->same_comdat_group && !boundary_p)
510 {
511 ref = lto_symtab_encoder_lookup (encoder,
512 node->same_comdat_group);
513 gcc_assert (ref != LCC_NOT_FOUND);
514 }
515 else
516 ref = LCC_NOT_FOUND;
517 streamer_write_hwi_stream (ob->main_stream, ref);
518 }
519
520 section = node->get_section ();
521 if (!section)
522 section = "";
523
524 streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
525
526 bp = bitpack_create (ob->main_stream);
527 bp_pack_value (&bp, node->local.local, 1);
528 bp_pack_value (&bp, node->externally_visible, 1);
529 bp_pack_value (&bp, node->no_reorder, 1);
530 bp_pack_value (&bp, node->definition, 1);
531 bp_pack_value (&bp, node->local.versionable, 1);
532 bp_pack_value (&bp, node->local.can_change_signature, 1);
533 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
534 bp_pack_value (&bp, node->force_output, 1);
535 bp_pack_value (&bp, node->forced_by_abi, 1);
536 bp_pack_value (&bp, node->unique_name, 1);
537 bp_pack_value (&bp, node->body_removed, 1);
538 bp_pack_value (&bp, node->implicit_section, 1);
539 bp_pack_value (&bp, node->address_taken, 1);
540 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
541 && node->get_partitioning_class () == SYMBOL_PARTITION
542 && (reachable_from_other_partition_p (node, encoder)
543 || referenced_from_other_partition_p (node, encoder)), 1);
544 bp_pack_value (&bp, node->lowered, 1);
545 bp_pack_value (&bp, in_other_partition, 1);
546 bp_pack_value (&bp, node->alias, 1);
547 bp_pack_value (&bp, node->weakref, 1);
548 bp_pack_value (&bp, node->frequency, 2);
549 bp_pack_value (&bp, node->only_called_at_startup, 1);
550 bp_pack_value (&bp, node->only_called_at_exit, 1);
551 bp_pack_value (&bp, node->tm_clone, 1);
552 bp_pack_value (&bp, node->calls_comdat_local, 1);
553 bp_pack_value (&bp, node->icf_merged, 1);
554 bp_pack_value (&bp, node->nonfreeing_fn, 1);
555 bp_pack_value (&bp, node->thunk.thunk_p, 1);
556 bp_pack_value (&bp, node->parallelized_function, 1);
557 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
558 LDPR_NUM_KNOWN, node->resolution);
559 bp_pack_value (&bp, node->instrumentation_clone, 1);
560 bp_pack_value (&bp, node->split_part, 1);
561 streamer_write_bitpack (&bp);
562 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
563
564 if (node->thunk.thunk_p)
565 {
566 streamer_write_uhwi_stream
567 (ob->main_stream,
568 1 + (node->thunk.this_adjusting != 0) * 2
569 + (node->thunk.virtual_offset_p != 0) * 4
570 + (node->thunk.add_pointer_bounds_args != 0) * 8);
571 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
572 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
573 }
574 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
575 if (DECL_STATIC_CONSTRUCTOR (node->decl))
576 streamer_write_hwi_stream (ob->main_stream, node->get_init_priority ());
577 if (DECL_STATIC_DESTRUCTOR (node->decl))
578 streamer_write_hwi_stream (ob->main_stream, node->get_fini_priority ());
579
580 if (node->instrumentation_clone)
581 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->orig_decl);
582 }
583
584 /* Output the varpool NODE to OB.
585 If NODE is not in SET, then NODE is a boundary. */
586
587 static void
588 lto_output_varpool_node (struct lto_simple_output_block *ob, varpool_node *node,
589 lto_symtab_encoder_t encoder)
590 {
591 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
592 bool encode_initializer_p
593 = (node->definition
594 && lto_symtab_encoder_encode_initializer_p (encoder, node));
595 struct bitpack_d bp;
596 int ref;
597 const char *comdat;
598 const char *section;
599 tree group;
600
601 gcc_assert (!encode_initializer_p || node->definition);
602 gcc_assert (boundary_p || encode_initializer_p);
603
604 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
605 LTO_symtab_variable);
606 streamer_write_hwi_stream (ob->main_stream, node->order);
607 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
608 bp = bitpack_create (ob->main_stream);
609 bp_pack_value (&bp, node->externally_visible, 1);
610 bp_pack_value (&bp, node->no_reorder, 1);
611 bp_pack_value (&bp, node->force_output, 1);
612 bp_pack_value (&bp, node->forced_by_abi, 1);
613 bp_pack_value (&bp, node->unique_name, 1);
614 bp_pack_value (&bp,
615 node->body_removed
616 || (!encode_initializer_p && !node->alias && node->definition),
617 1);
618 bp_pack_value (&bp, node->implicit_section, 1);
619 bp_pack_value (&bp, node->writeonly, 1);
620 bp_pack_value (&bp, node->definition && (encode_initializer_p || node->alias),
621 1);
622 bp_pack_value (&bp, node->alias, 1);
623 bp_pack_value (&bp, node->weakref, 1);
624 bp_pack_value (&bp, node->analyzed && !boundary_p, 1);
625 gcc_assert (node->definition || !node->analyzed);
626 /* Constant pool initializers can be de-unified into individual ltrans units.
627 FIXME: Alternatively at -Os we may want to avoid generating for them the local
628 labels and share them across LTRANS partitions. */
629 if (node->get_partitioning_class () != SYMBOL_PARTITION)
630 {
631 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
632 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
633 }
634 else
635 {
636 bp_pack_value (&bp, node->definition
637 && referenced_from_other_partition_p (node, encoder), 1);
638 bp_pack_value (&bp, node->analyzed
639 && boundary_p && !DECL_EXTERNAL (node->decl), 1);
640 /* in_other_partition. */
641 }
642 bp_pack_value (&bp, node->tls_model, 3);
643 bp_pack_value (&bp, node->used_by_single_function, 1);
644 bp_pack_value (&bp, node->need_bounds_init, 1);
645 streamer_write_bitpack (&bp);
646
647 group = node->get_comdat_group ();
648 if (group)
649 comdat = IDENTIFIER_POINTER (group);
650 else
651 comdat = "";
652 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
653
654 if (group)
655 {
656 if (node->same_comdat_group && !boundary_p)
657 {
658 ref = lto_symtab_encoder_lookup (encoder,
659 node->same_comdat_group);
660 gcc_assert (ref != LCC_NOT_FOUND);
661 }
662 else
663 ref = LCC_NOT_FOUND;
664 streamer_write_hwi_stream (ob->main_stream, ref);
665 }
666
667 section = node->get_section ();
668 if (!section)
669 section = "";
670 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
671
672 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
673 LDPR_NUM_KNOWN, node->resolution);
674 }
675
676 /* Output the varpool NODE to OB.
677 If NODE is not in SET, then NODE is a boundary. */
678
679 static void
680 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
681 lto_symtab_encoder_t encoder)
682 {
683 struct bitpack_d bp;
684 int nref;
685 int uid = ref->lto_stmt_uid;
686 struct cgraph_node *node;
687
688 bp = bitpack_create (ob->main_stream);
689 bp_pack_value (&bp, ref->use, 3);
690 bp_pack_value (&bp, ref->speculative, 1);
691 streamer_write_bitpack (&bp);
692 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
693 gcc_assert (nref != LCC_NOT_FOUND);
694 streamer_write_hwi_stream (ob->main_stream, nref);
695
696 node = dyn_cast <cgraph_node *> (ref->referring);
697 if (node)
698 {
699 if (ref->stmt)
700 uid = gimple_uid (ref->stmt) + 1;
701 streamer_write_hwi_stream (ob->main_stream, uid);
702 }
703 }
704
705 /* Stream out profile_summary to OB. */
706
707 static void
708 output_profile_summary (struct lto_simple_output_block *ob)
709 {
710 unsigned h_ix;
711 struct bitpack_d bp;
712
713 if (profile_info)
714 {
715 /* We do not output num and run_max, they are not used by
716 GCC profile feedback and they are difficult to merge from multiple
717 units. */
718 gcc_assert (profile_info->runs);
719 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
720 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
721
722 /* sum_all is needed for computing the working set with the
723 histogram. */
724 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
725
726 /* Create and output a bitpack of non-zero histogram entries indices. */
727 bp = bitpack_create (ob->main_stream);
728 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
729 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
730 streamer_write_bitpack (&bp);
731 /* Now stream out only those non-zero entries. */
732 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
733 {
734 if (!profile_info->histogram[h_ix].num_counters)
735 continue;
736 streamer_write_gcov_count_stream (ob->main_stream,
737 profile_info->histogram[h_ix].num_counters);
738 streamer_write_gcov_count_stream (ob->main_stream,
739 profile_info->histogram[h_ix].min_value);
740 streamer_write_gcov_count_stream (ob->main_stream,
741 profile_info->histogram[h_ix].cum_value);
742 }
743 /* IPA-profile computes hot bb threshold based on cumulated
744 whole program profile. We need to stream it down to ltrans. */
745 if (flag_wpa)
746 streamer_write_gcov_count_stream (ob->main_stream,
747 get_hot_bb_threshold ());
748 }
749 else
750 streamer_write_uhwi_stream (ob->main_stream, 0);
751 }
752
753 /* Output all callees or indirect outgoing edges. EDGE must be the first such
754 edge. */
755
756 static void
757 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
758 struct lto_simple_output_block *ob,
759 lto_symtab_encoder_t encoder)
760 {
761 if (!edge)
762 return;
763
764 /* Output edges in backward direction, so the reconstructed callgraph match
765 and it is easy to associate call sites in the IPA pass summaries. */
766 while (edge->next_callee)
767 edge = edge->next_callee;
768 for (; edge; edge = edge->prev_callee)
769 lto_output_edge (ob, edge, encoder);
770 }
771
772 /* Output the part of the cgraph in SET. */
773
774 static void
775 output_refs (lto_symtab_encoder_t encoder)
776 {
777 struct lto_simple_output_block *ob;
778 int count;
779 struct ipa_ref *ref;
780
781 ob = lto_create_simple_output_block (LTO_section_refs);
782
783 for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
784 {
785 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
786
787 /* IPA_REF_ALIAS and IPA_REF_CHKP references are always preserved
788 in the boundary. Alias node can't have other references and
789 can be always handled as if it's not in the boundary. */
790 if (!node->alias && !lto_symtab_encoder_in_partition_p (encoder, node))
791 {
792 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
793 /* Output IPA_REF_CHKP reference. */
794 if (cnode
795 && cnode->instrumented_version
796 && !cnode->instrumentation_clone)
797 {
798 for (int i = 0; node->iterate_reference (i, ref); i++)
799 if (ref->use == IPA_REF_CHKP)
800 {
801 if (lto_symtab_encoder_lookup (encoder, ref->referred)
802 != LCC_NOT_FOUND)
803 {
804 int nref = lto_symtab_encoder_lookup (encoder, node);
805 streamer_write_gcov_count_stream (ob->main_stream, 1);
806 streamer_write_uhwi_stream (ob->main_stream, nref);
807 lto_output_ref (ob, ref, encoder);
808 }
809 break;
810 }
811 }
812 continue;
813 }
814
815 count = node->ref_list.nreferences ();
816 if (count)
817 {
818 streamer_write_gcov_count_stream (ob->main_stream, count);
819 streamer_write_uhwi_stream (ob->main_stream,
820 lto_symtab_encoder_lookup (encoder, node));
821 for (int i = 0; node->iterate_reference (i, ref); i++)
822 lto_output_ref (ob, ref, encoder);
823 }
824 }
825
826 streamer_write_uhwi_stream (ob->main_stream, 0);
827
828 lto_destroy_simple_output_block (ob);
829 }
830
831 /* Add NODE into encoder as well as nodes it is cloned from.
832 Do it in a way so clones appear first. */
833
834 static void
835 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
836 bool include_body)
837 {
838 if (node->clone_of)
839 add_node_to (encoder, node->clone_of, include_body);
840 else if (include_body)
841 lto_set_symtab_encoder_encode_body (encoder, node);
842 lto_symtab_encoder_encode (encoder, node);
843 }
844
845 /* Add all references in NODE to encoders. */
846
847 static void
848 create_references (lto_symtab_encoder_t encoder, symtab_node *node)
849 {
850 int i;
851 struct ipa_ref *ref = NULL;
852 for (i = 0; node->iterate_reference (i, ref); i++)
853 if (is_a <cgraph_node *> (ref->referred))
854 add_node_to (encoder, dyn_cast <cgraph_node *> (ref->referred), false);
855 else
856 lto_symtab_encoder_encode (encoder, ref->referred);
857 }
858
859 /* Select what needs to be streamed out. In regular lto mode stream everything.
860 In offload lto mode stream only nodes marked as offloadable. */
861 void
862 select_what_to_stream (void)
863 {
864 struct symtab_node *snode;
865 FOR_EACH_SYMBOL (snode)
866 snode->need_lto_streaming = !lto_stream_offload_p || snode->offloadable;
867 }
868
869 /* Find all symbols we want to stream into given partition and insert them
870 to encoders.
871
872 The function actually replaces IN_ENCODER by new one. The reason is that
873 streaming code needs clone's origin to be streamed before clone. This
874 means that we need to insert the nodes in specific order. This order is
875 ignored by the partitioning logic earlier. */
876
877 lto_symtab_encoder_t
878 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
879 {
880 struct cgraph_edge *edge;
881 int i;
882 lto_symtab_encoder_t encoder;
883 lto_symtab_encoder_iterator lsei;
884 hash_set<void *> reachable_call_targets;
885
886 encoder = lto_symtab_encoder_new (false);
887
888 /* Go over all entries in the IN_ENCODER and duplicate them to
889 ENCODER. At the same time insert masters of clones so
890 every master appears before clone. */
891 for (lsei = lsei_start_function_in_partition (in_encoder);
892 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
893 {
894 struct cgraph_node *node = lsei_cgraph_node (lsei);
895 if (!node->need_lto_streaming)
896 continue;
897 add_node_to (encoder, node, true);
898 lto_set_symtab_encoder_in_partition (encoder, node);
899 create_references (encoder, node);
900 /* For proper debug info, we need to ship the origins, too. */
901 if (DECL_ABSTRACT_ORIGIN (node->decl))
902 {
903 struct cgraph_node *origin_node
904 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (node->decl));
905 origin_node->used_as_abstract_origin = true;
906 add_node_to (encoder, origin_node, true);
907 }
908 }
909 for (lsei = lsei_start_variable_in_partition (in_encoder);
910 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
911 {
912 varpool_node *vnode = lsei_varpool_node (lsei);
913
914 if (!vnode->need_lto_streaming)
915 continue;
916 lto_set_symtab_encoder_in_partition (encoder, vnode);
917 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
918 create_references (encoder, vnode);
919 /* For proper debug info, we need to ship the origins, too. */
920 if (DECL_ABSTRACT_ORIGIN (vnode->decl))
921 {
922 varpool_node *origin_node
923 = varpool_node::get (DECL_ABSTRACT_ORIGIN (vnode->decl));
924 lto_set_symtab_encoder_in_partition (encoder, origin_node);
925 }
926 }
927 /* Pickle in also the initializer of all referenced readonly variables
928 to help folding. Constant pool variables are not shared, so we must
929 pickle those too. */
930 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
931 {
932 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
933 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
934 {
935 if (!lto_symtab_encoder_encode_initializer_p (encoder,
936 vnode)
937 && (((vnode->ctor_useable_for_folding_p ()
938 && (!DECL_VIRTUAL_P (vnode->decl)
939 || !flag_wpa
940 || flag_ltrans_devirtualize))
941 || POINTER_BOUNDS_P (vnode->decl))))
942 {
943 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
944 create_references (encoder, vnode);
945 }
946 }
947 }
948
949 /* Go over all the nodes again to include callees that are not in
950 SET. */
951 for (lsei = lsei_start_function_in_partition (encoder);
952 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
953 {
954 struct cgraph_node *node = lsei_cgraph_node (lsei);
955 for (edge = node->callees; edge; edge = edge->next_callee)
956 {
957 struct cgraph_node *callee = edge->callee;
958 if (!lto_symtab_encoder_in_partition_p (encoder, callee))
959 {
960 /* We should have moved all the inlines. */
961 gcc_assert (!callee->global.inlined_to);
962 add_node_to (encoder, callee, false);
963 }
964 }
965 /* Add all possible targets for late devirtualization. */
966 if (flag_ltrans_devirtualize || !flag_wpa)
967 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
968 if (edge->indirect_info->polymorphic)
969 {
970 unsigned int i;
971 void *cache_token;
972 bool final;
973 vec <cgraph_node *>targets
974 = possible_polymorphic_call_targets
975 (edge, &final, &cache_token);
976 if (!reachable_call_targets.add (cache_token))
977 {
978 for (i = 0; i < targets.length (); i++)
979 {
980 struct cgraph_node *callee = targets[i];
981
982 /* Adding an external declarations into the unit serves
983 no purpose and just increases its boundary. */
984 if (callee->definition
985 && !lto_symtab_encoder_in_partition_p
986 (encoder, callee))
987 {
988 gcc_assert (!callee->global.inlined_to);
989 add_node_to (encoder, callee, false);
990 }
991 }
992 }
993 }
994 }
995 /* Be sure to also insert alias targert and thunk callees. These needs
996 to stay to aid local calling conventions. */
997 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
998 {
999 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1000 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1001
1002 if (node->alias && node->analyzed)
1003 create_references (encoder, node);
1004 if (cnode
1005 && cnode->thunk.thunk_p)
1006 add_node_to (encoder, cnode->callees->callee, false);
1007 }
1008 lto_symtab_encoder_delete (in_encoder);
1009 return encoder;
1010 }
1011
1012 /* Output the part of the symtab in SET and VSET. */
1013
1014 void
1015 output_symtab (void)
1016 {
1017 struct cgraph_node *node;
1018 struct lto_simple_output_block *ob;
1019 int i, n_nodes;
1020 lto_symtab_encoder_t encoder;
1021
1022 if (flag_wpa)
1023 output_cgraph_opt_summary ();
1024
1025 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
1026
1027 output_profile_summary (ob);
1028
1029 /* An encoder for cgraph nodes should have been created by
1030 ipa_write_summaries_1. */
1031 gcc_assert (ob->decl_state->symtab_node_encoder);
1032 encoder = ob->decl_state->symtab_node_encoder;
1033
1034 /* Write out the nodes. We must first output a node and then its clones,
1035 otherwise at a time reading back the node there would be nothing to clone
1036 from. */
1037 n_nodes = lto_symtab_encoder_size (encoder);
1038 for (i = 0; i < n_nodes; i++)
1039 {
1040 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1041 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1042 lto_output_node (ob, cnode, encoder);
1043 else
1044 lto_output_varpool_node (ob, dyn_cast<varpool_node *> (node), encoder);
1045 }
1046
1047 /* Go over the nodes in SET again to write edges. */
1048 for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
1049 {
1050 node = dyn_cast <cgraph_node *> (lto_symtab_encoder_deref (encoder, i));
1051 if (node
1052 && (node->thunk.thunk_p
1053 || lto_symtab_encoder_in_partition_p (encoder, node)))
1054 {
1055 output_outgoing_cgraph_edges (node->callees, ob, encoder);
1056 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
1057 }
1058 }
1059
1060 streamer_write_uhwi_stream (ob->main_stream, 0);
1061
1062 lto_destroy_simple_output_block (ob);
1063
1064 /* Emit toplevel asms.
1065 When doing WPA we must output every asm just once. Since we do not partition asm
1066 nodes at all, output them to first output. This is kind of hack, but should work
1067 well. */
1068 if (!asm_nodes_output)
1069 {
1070 asm_nodes_output = true;
1071 lto_output_toplevel_asms ();
1072 }
1073
1074 output_refs (encoder);
1075 }
1076
1077 /* Return identifier encoded in IB as a plain string. */
1078
1079 static tree
1080 read_identifier (struct lto_input_block *ib)
1081 {
1082 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1083 tree id;
1084
1085 if (ib->data[ib->p + len])
1086 lto_section_overrun (ib);
1087 if (!len)
1088 {
1089 ib->p++;
1090 return NULL;
1091 }
1092 id = get_identifier (ib->data + ib->p);
1093 ib->p += len + 1;
1094 return id;
1095 }
1096
1097 /* Return string encoded in IB, NULL if string is empty. */
1098
1099 static const char *
1100 read_string (struct lto_input_block *ib)
1101 {
1102 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1103 const char *str;
1104
1105 if (ib->data[ib->p + len])
1106 lto_section_overrun (ib);
1107 if (!len)
1108 {
1109 ib->p++;
1110 return NULL;
1111 }
1112 str = ib->data + ib->p;
1113 ib->p += len + 1;
1114 return str;
1115 }
1116
1117 /* Output function/variable tables that will allow libgomp to look up offload
1118 target code.
1119 OFFLOAD_FUNCS is filled in expand_omp_target, OFFLOAD_VARS is filled in
1120 varpool_node::get_create. In WHOPR (partitioned) mode during the WPA stage
1121 both OFFLOAD_FUNCS and OFFLOAD_VARS are filled by input_offload_tables. */
1122
1123 void
1124 output_offload_tables (void)
1125 {
1126 if (vec_safe_is_empty (offload_funcs) && vec_safe_is_empty (offload_vars))
1127 return;
1128
1129 struct lto_simple_output_block *ob
1130 = lto_create_simple_output_block (LTO_section_offload_table);
1131
1132 for (unsigned i = 0; i < vec_safe_length (offload_funcs); i++)
1133 {
1134 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1135 LTO_symtab_last_tag, LTO_symtab_unavail_node);
1136 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
1137 (*offload_funcs)[i]);
1138 }
1139
1140 for (unsigned i = 0; i < vec_safe_length (offload_vars); i++)
1141 {
1142 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1143 LTO_symtab_last_tag, LTO_symtab_variable);
1144 lto_output_var_decl_index (ob->decl_state, ob->main_stream,
1145 (*offload_vars)[i]);
1146 }
1147
1148 streamer_write_uhwi_stream (ob->main_stream, 0);
1149 lto_destroy_simple_output_block (ob);
1150
1151 /* In WHOPR mode during the WPA stage the joint offload tables need to be
1152 streamed to one partition only. That's why we free offload_funcs and
1153 offload_vars after the first call of output_offload_tables. */
1154 if (flag_wpa)
1155 {
1156 vec_free (offload_funcs);
1157 vec_free (offload_vars);
1158 }
1159 }
1160
1161 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
1162 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
1163 NODE or to replace the values in it, for instance because the first
1164 time we saw it, the function body was not available but now it
1165 is. BP is a bitpack with all the bitflags for NODE read from the
1166 stream. */
1167
1168 static void
1169 input_overwrite_node (struct lto_file_decl_data *file_data,
1170 struct cgraph_node *node,
1171 enum LTO_symtab_tags tag,
1172 struct bitpack_d *bp)
1173 {
1174 node->aux = (void *) tag;
1175 node->lto_file_data = file_data;
1176
1177 node->local.local = bp_unpack_value (bp, 1);
1178 node->externally_visible = bp_unpack_value (bp, 1);
1179 node->no_reorder = bp_unpack_value (bp, 1);
1180 node->definition = bp_unpack_value (bp, 1);
1181 node->local.versionable = bp_unpack_value (bp, 1);
1182 node->local.can_change_signature = bp_unpack_value (bp, 1);
1183 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
1184 node->force_output = bp_unpack_value (bp, 1);
1185 node->forced_by_abi = bp_unpack_value (bp, 1);
1186 node->unique_name = bp_unpack_value (bp, 1);
1187 node->body_removed = bp_unpack_value (bp, 1);
1188 node->implicit_section = bp_unpack_value (bp, 1);
1189 node->address_taken = bp_unpack_value (bp, 1);
1190 node->used_from_other_partition = bp_unpack_value (bp, 1);
1191 node->lowered = bp_unpack_value (bp, 1);
1192 node->analyzed = tag == LTO_symtab_analyzed_node;
1193 node->in_other_partition = bp_unpack_value (bp, 1);
1194 if (node->in_other_partition
1195 /* Avoid updating decl when we are seeing just inline clone.
1196 When inlining function that has functions already inlined into it,
1197 we produce clones of inline clones.
1198
1199 WPA partitioning might put each clone into different unit and
1200 we might end up streaming inline clone from other partition
1201 to support clone we are interested in. */
1202 && (!node->clone_of
1203 || node->clone_of->decl != node->decl))
1204 {
1205 DECL_EXTERNAL (node->decl) = 1;
1206 TREE_STATIC (node->decl) = 0;
1207 }
1208 node->alias = bp_unpack_value (bp, 1);
1209 node->weakref = bp_unpack_value (bp, 1);
1210 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
1211 node->only_called_at_startup = bp_unpack_value (bp, 1);
1212 node->only_called_at_exit = bp_unpack_value (bp, 1);
1213 node->tm_clone = bp_unpack_value (bp, 1);
1214 node->calls_comdat_local = bp_unpack_value (bp, 1);
1215 node->icf_merged = bp_unpack_value (bp, 1);
1216 node->nonfreeing_fn = bp_unpack_value (bp, 1);
1217 node->thunk.thunk_p = bp_unpack_value (bp, 1);
1218 node->parallelized_function = bp_unpack_value (bp, 1);
1219 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
1220 LDPR_NUM_KNOWN);
1221 node->instrumentation_clone = bp_unpack_value (bp, 1);
1222 node->split_part = bp_unpack_value (bp, 1);
1223 gcc_assert (flag_ltrans
1224 || (!node->in_other_partition
1225 && !node->used_from_other_partition));
1226 }
1227
1228 /* Return string alias is alias of. */
1229
1230 static tree
1231 get_alias_symbol (tree decl)
1232 {
1233 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1234 return get_identifier (TREE_STRING_POINTER
1235 (TREE_VALUE (TREE_VALUE (alias))));
1236 }
1237
1238 /* Read a node from input_block IB. TAG is the node's tag just read.
1239 Return the node read or overwriten. */
1240
1241 static struct cgraph_node *
1242 input_node (struct lto_file_decl_data *file_data,
1243 struct lto_input_block *ib,
1244 enum LTO_symtab_tags tag,
1245 vec<symtab_node *> nodes)
1246 {
1247 gcc::pass_manager *passes = g->get_passes ();
1248 tree fn_decl;
1249 struct cgraph_node *node;
1250 struct bitpack_d bp;
1251 unsigned decl_index;
1252 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1253 int clone_ref;
1254 int order;
1255 int i, count;
1256 tree group;
1257 const char *section;
1258 order = streamer_read_hwi (ib) + order_base;
1259 clone_ref = streamer_read_hwi (ib);
1260
1261 decl_index = streamer_read_uhwi (ib);
1262 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1263
1264 if (clone_ref != LCC_NOT_FOUND)
1265 {
1266 node = dyn_cast<cgraph_node *> (nodes[clone_ref])->create_clone (fn_decl,
1267 0, CGRAPH_FREQ_BASE, false,
1268 vNULL, false, NULL, NULL);
1269 }
1270 else
1271 {
1272 /* Declaration of functions can be already merged with a declaration
1273 from other input file. We keep cgraph unmerged until after streaming
1274 of ipa passes is done. Alays forcingly create a fresh node. */
1275 node = symtab->create_empty ();
1276 node->decl = fn_decl;
1277 node->register_symbol ();
1278 }
1279
1280 node->order = order;
1281 if (order >= symtab->order)
1282 symtab->order = order + 1;
1283
1284 node->count = streamer_read_gcov_count (ib);
1285 node->count_materialization_scale = streamer_read_hwi (ib);
1286
1287 count = streamer_read_hwi (ib);
1288 node->ipa_transforms_to_apply = vNULL;
1289 for (i = 0; i < count; i++)
1290 {
1291 opt_pass *pass;
1292 int pid = streamer_read_hwi (ib);
1293
1294 gcc_assert (pid < passes->passes_by_id_size);
1295 pass = passes->passes_by_id[pid];
1296 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *) pass);
1297 }
1298
1299 if (tag == LTO_symtab_analyzed_node)
1300 ref = streamer_read_hwi (ib);
1301
1302 group = read_identifier (ib);
1303 if (group)
1304 ref2 = streamer_read_hwi (ib);
1305
1306 /* Make sure that we have not read this node before. Nodes that
1307 have already been read will have their tag stored in the 'aux'
1308 field. Since built-in functions can be referenced in multiple
1309 functions, they are expected to be read more than once. */
1310 if (node->aux && !DECL_BUILT_IN (node->decl))
1311 internal_error ("bytecode stream: found multiple instances of cgraph "
1312 "node with uid %d", node->uid);
1313
1314 node->tp_first_run = streamer_read_uhwi (ib);
1315
1316 bp = streamer_read_bitpack (ib);
1317
1318 input_overwrite_node (file_data, node, tag, &bp);
1319
1320 /* Store a reference for now, and fix up later to be a pointer. */
1321 node->global.inlined_to = (cgraph_node *) (intptr_t) ref;
1322
1323 if (group)
1324 {
1325 node->set_comdat_group (group);
1326 /* Store a reference for now, and fix up later to be a pointer. */
1327 node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
1328 }
1329 else
1330 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1331 section = read_string (ib);
1332 if (section)
1333 node->set_section_for_node (section);
1334
1335 if (node->thunk.thunk_p)
1336 {
1337 int type = streamer_read_uhwi (ib);
1338 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1339 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1340
1341 node->thunk.fixed_offset = fixed_offset;
1342 node->thunk.this_adjusting = (type & 2);
1343 node->thunk.virtual_value = virtual_value;
1344 node->thunk.virtual_offset_p = (type & 4);
1345 node->thunk.add_pointer_bounds_args = (type & 8);
1346 }
1347 if (node->alias && !node->analyzed && node->weakref)
1348 node->alias_target = get_alias_symbol (node->decl);
1349 node->profile_id = streamer_read_hwi (ib);
1350 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1351 node->set_init_priority (streamer_read_hwi (ib));
1352 if (DECL_STATIC_DESTRUCTOR (node->decl))
1353 node->set_fini_priority (streamer_read_hwi (ib));
1354
1355 if (node->instrumentation_clone)
1356 {
1357 decl_index = streamer_read_uhwi (ib);
1358 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1359 node->orig_decl = fn_decl;
1360 }
1361
1362 return node;
1363 }
1364
1365 /* Read a node from input_block IB. TAG is the node's tag just read.
1366 Return the node read or overwriten. */
1367
1368 static varpool_node *
1369 input_varpool_node (struct lto_file_decl_data *file_data,
1370 struct lto_input_block *ib)
1371 {
1372 int decl_index;
1373 tree var_decl;
1374 varpool_node *node;
1375 struct bitpack_d bp;
1376 int ref = LCC_NOT_FOUND;
1377 int order;
1378 tree group;
1379 const char *section;
1380
1381 order = streamer_read_hwi (ib) + order_base;
1382 decl_index = streamer_read_uhwi (ib);
1383 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1384
1385 /* Declaration of functions can be already merged with a declaration
1386 from other input file. We keep cgraph unmerged until after streaming
1387 of ipa passes is done. Alays forcingly create a fresh node. */
1388 node = varpool_node::create_empty ();
1389 node->decl = var_decl;
1390 node->register_symbol ();
1391
1392 node->order = order;
1393 if (order >= symtab->order)
1394 symtab->order = order + 1;
1395 node->lto_file_data = file_data;
1396
1397 bp = streamer_read_bitpack (ib);
1398 node->externally_visible = bp_unpack_value (&bp, 1);
1399 node->no_reorder = bp_unpack_value (&bp, 1);
1400 node->force_output = bp_unpack_value (&bp, 1);
1401 node->forced_by_abi = bp_unpack_value (&bp, 1);
1402 node->unique_name = bp_unpack_value (&bp, 1);
1403 node->body_removed = bp_unpack_value (&bp, 1);
1404 node->implicit_section = bp_unpack_value (&bp, 1);
1405 node->writeonly = bp_unpack_value (&bp, 1);
1406 node->definition = bp_unpack_value (&bp, 1);
1407 node->alias = bp_unpack_value (&bp, 1);
1408 node->weakref = bp_unpack_value (&bp, 1);
1409 node->analyzed = bp_unpack_value (&bp, 1);
1410 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1411 node->in_other_partition = bp_unpack_value (&bp, 1);
1412 if (node->in_other_partition)
1413 {
1414 DECL_EXTERNAL (node->decl) = 1;
1415 TREE_STATIC (node->decl) = 0;
1416 }
1417 if (node->alias && !node->analyzed && node->weakref)
1418 node->alias_target = get_alias_symbol (node->decl);
1419 node->tls_model = (enum tls_model)bp_unpack_value (&bp, 3);
1420 node->used_by_single_function = (enum tls_model)bp_unpack_value (&bp, 1);
1421 node->need_bounds_init = bp_unpack_value (&bp, 1);
1422 group = read_identifier (ib);
1423 if (group)
1424 {
1425 node->set_comdat_group (group);
1426 ref = streamer_read_hwi (ib);
1427 /* Store a reference for now, and fix up later to be a pointer. */
1428 node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1429 }
1430 else
1431 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1432 section = read_string (ib);
1433 if (section)
1434 node->set_section_for_node (section);
1435 node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1436 LDPR_NUM_KNOWN);
1437 gcc_assert (flag_ltrans
1438 || (!node->in_other_partition
1439 && !node->used_from_other_partition));
1440
1441 return node;
1442 }
1443
1444 /* Read a node from input_block IB. TAG is the node's tag just read.
1445 Return the node read or overwriten. */
1446
1447 static void
1448 input_ref (struct lto_input_block *ib,
1449 symtab_node *referring_node,
1450 vec<symtab_node *> nodes)
1451 {
1452 symtab_node *node = NULL;
1453 struct bitpack_d bp;
1454 enum ipa_ref_use use;
1455 bool speculative;
1456 struct ipa_ref *ref;
1457
1458 bp = streamer_read_bitpack (ib);
1459 use = (enum ipa_ref_use) bp_unpack_value (&bp, 3);
1460 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1461 node = nodes[streamer_read_hwi (ib)];
1462 ref = referring_node->create_reference (node, use);
1463 ref->speculative = speculative;
1464 if (is_a <cgraph_node *> (referring_node))
1465 ref->lto_stmt_uid = streamer_read_hwi (ib);
1466 }
1467
1468 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1469 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1470 edge being read is indirect (in the sense that it has
1471 indirect_unknown_callee set). */
1472
1473 static void
1474 input_edge (struct lto_input_block *ib, vec<symtab_node *> nodes,
1475 bool indirect)
1476 {
1477 struct cgraph_node *caller, *callee;
1478 struct cgraph_edge *edge;
1479 unsigned int stmt_id;
1480 gcov_type count;
1481 int freq;
1482 cgraph_inline_failed_t inline_failed;
1483 struct bitpack_d bp;
1484 int ecf_flags = 0;
1485
1486 caller = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1487 if (caller == NULL || caller->decl == NULL_TREE)
1488 internal_error ("bytecode stream: no caller found while reading edge");
1489
1490 if (!indirect)
1491 {
1492 callee = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1493 if (callee == NULL || callee->decl == NULL_TREE)
1494 internal_error ("bytecode stream: no callee found while reading edge");
1495 }
1496 else
1497 callee = NULL;
1498
1499 count = streamer_read_gcov_count (ib);
1500
1501 bp = streamer_read_bitpack (ib);
1502 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_t, CIF_N_REASONS);
1503 stmt_id = bp_unpack_var_len_unsigned (&bp);
1504 freq = (int) bp_unpack_var_len_unsigned (&bp);
1505
1506 if (indirect)
1507 edge = caller->create_indirect_edge (NULL, 0, count, freq);
1508 else
1509 edge = caller->create_edge (callee, NULL, count, freq);
1510
1511 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1512 edge->speculative = bp_unpack_value (&bp, 1);
1513 edge->lto_stmt_uid = stmt_id;
1514 edge->inline_failed = inline_failed;
1515 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1516 edge->can_throw_external = bp_unpack_value (&bp, 1);
1517 edge->in_polymorphic_cdtor = bp_unpack_value (&bp, 1);
1518 if (indirect)
1519 {
1520 if (bp_unpack_value (&bp, 1))
1521 ecf_flags |= ECF_CONST;
1522 if (bp_unpack_value (&bp, 1))
1523 ecf_flags |= ECF_PURE;
1524 if (bp_unpack_value (&bp, 1))
1525 ecf_flags |= ECF_NORETURN;
1526 if (bp_unpack_value (&bp, 1))
1527 ecf_flags |= ECF_MALLOC;
1528 if (bp_unpack_value (&bp, 1))
1529 ecf_flags |= ECF_NOTHROW;
1530 if (bp_unpack_value (&bp, 1))
1531 ecf_flags |= ECF_RETURNS_TWICE;
1532 edge->indirect_info->ecf_flags = ecf_flags;
1533 edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1534 if (edge->indirect_info->common_target_id)
1535 edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1536 }
1537 }
1538
1539
1540 /* Read a cgraph from IB using the info in FILE_DATA. */
1541
1542 static vec<symtab_node *>
1543 input_cgraph_1 (struct lto_file_decl_data *file_data,
1544 struct lto_input_block *ib)
1545 {
1546 enum LTO_symtab_tags tag;
1547 vec<symtab_node *> nodes = vNULL;
1548 symtab_node *node;
1549 unsigned i;
1550
1551 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1552 order_base = symtab->order;
1553 while (tag)
1554 {
1555 if (tag == LTO_symtab_edge)
1556 input_edge (ib, nodes, false);
1557 else if (tag == LTO_symtab_indirect_edge)
1558 input_edge (ib, nodes, true);
1559 else if (tag == LTO_symtab_variable)
1560 {
1561 node = input_varpool_node (file_data, ib);
1562 nodes.safe_push (node);
1563 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1564 }
1565 else
1566 {
1567 node = input_node (file_data, ib, tag, nodes);
1568 if (node == NULL || node->decl == NULL_TREE)
1569 internal_error ("bytecode stream: found empty cgraph node");
1570 nodes.safe_push (node);
1571 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1572 }
1573
1574 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1575 }
1576
1577 lto_input_toplevel_asms (file_data, order_base);
1578
1579 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1580 #ifdef ENABLE_CHECKING
1581 FOR_EACH_VEC_ELT (nodes, i, node)
1582 gcc_assert (node->aux || !is_a <cgraph_node *> (node));
1583 #endif
1584 FOR_EACH_VEC_ELT (nodes, i, node)
1585 {
1586 int ref;
1587 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1588 {
1589 ref = (int) (intptr_t) cnode->global.inlined_to;
1590
1591 /* We share declaration of builtins, so we may read same node twice. */
1592 if (!node->aux)
1593 continue;
1594 node->aux = NULL;
1595
1596 /* Fixup inlined_to from reference to pointer. */
1597 if (ref != LCC_NOT_FOUND)
1598 dyn_cast<cgraph_node *> (node)->global.inlined_to
1599 = dyn_cast<cgraph_node *> (nodes[ref]);
1600 else
1601 cnode->global.inlined_to = NULL;
1602
1603 /* Compute instrumented_version. */
1604 if (cnode->instrumentation_clone)
1605 {
1606 gcc_assert (cnode->orig_decl);
1607
1608 cnode->instrumented_version = cgraph_node::get (cnode->orig_decl);
1609 if (cnode->instrumented_version)
1610 {
1611 /* We may have multiple nodes for a single function which
1612 will be merged later. To have a proper merge we need
1613 to keep instrumentation_version reference between nodes
1614 consistent: each instrumented_version reference should
1615 have proper reverse reference. Thus don't break existing
1616 instrumented_version reference if it already exists. */
1617 if (cnode->instrumented_version->instrumented_version)
1618 cnode->instrumented_version = NULL;
1619 else
1620 cnode->instrumented_version->instrumented_version = cnode;
1621 }
1622
1623 /* Restore decl names reference except for wrapper functions. */
1624 if (!chkp_wrap_function (cnode->orig_decl))
1625 {
1626 tree name = DECL_ASSEMBLER_NAME (cnode->decl);
1627 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
1628 TREE_CHAIN (name) = DECL_ASSEMBLER_NAME (cnode->orig_decl);
1629 }
1630 }
1631 }
1632
1633 ref = (int) (intptr_t) node->same_comdat_group;
1634
1635 /* Fixup same_comdat_group from reference to pointer. */
1636 if (ref != LCC_NOT_FOUND)
1637 node->same_comdat_group = nodes[ref];
1638 else
1639 node->same_comdat_group = NULL;
1640 }
1641 FOR_EACH_VEC_ELT (nodes, i, node)
1642 node->aux = is_a <cgraph_node *> (node) ? (void *)1 : NULL;
1643 return nodes;
1644 }
1645
1646 /* Input ipa_refs. */
1647
1648 static void
1649 input_refs (struct lto_input_block *ib,
1650 vec<symtab_node *> nodes)
1651 {
1652 int count;
1653 int idx;
1654 while (true)
1655 {
1656 symtab_node *node;
1657 count = streamer_read_uhwi (ib);
1658 if (!count)
1659 break;
1660 idx = streamer_read_uhwi (ib);
1661 node = nodes[idx];
1662 while (count)
1663 {
1664 input_ref (ib, node, nodes);
1665 count--;
1666 }
1667 }
1668 }
1669
1670
1671 static struct gcov_ctr_summary lto_gcov_summary;
1672
1673 /* Input profile_info from IB. */
1674 static void
1675 input_profile_summary (struct lto_input_block *ib,
1676 struct lto_file_decl_data *file_data)
1677 {
1678 unsigned h_ix;
1679 struct bitpack_d bp;
1680 unsigned int runs = streamer_read_uhwi (ib);
1681 if (runs)
1682 {
1683 file_data->profile_info.runs = runs;
1684 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1685 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1686
1687 memset (file_data->profile_info.histogram, 0,
1688 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1689 /* Input the bitpack of non-zero histogram indices. */
1690 bp = streamer_read_bitpack (ib);
1691 /* Read in and unpack the full bitpack, flagging non-zero
1692 histogram entries by setting the num_counters non-zero. */
1693 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1694 {
1695 file_data->profile_info.histogram[h_ix].num_counters
1696 = bp_unpack_value (&bp, 1);
1697 }
1698 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1699 {
1700 if (!file_data->profile_info.histogram[h_ix].num_counters)
1701 continue;
1702
1703 file_data->profile_info.histogram[h_ix].num_counters
1704 = streamer_read_gcov_count (ib);
1705 file_data->profile_info.histogram[h_ix].min_value
1706 = streamer_read_gcov_count (ib);
1707 file_data->profile_info.histogram[h_ix].cum_value
1708 = streamer_read_gcov_count (ib);
1709 }
1710 /* IPA-profile computes hot bb threshold based on cumulated
1711 whole program profile. We need to stream it down to ltrans. */
1712 if (flag_ltrans)
1713 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1714 }
1715
1716 }
1717
1718 /* Rescale profile summaries to the same number of runs in the whole unit. */
1719
1720 static void
1721 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1722 {
1723 struct lto_file_decl_data *file_data;
1724 unsigned int j, h_ix;
1725 gcov_unsigned_t max_runs = 0;
1726 struct cgraph_node *node;
1727 struct cgraph_edge *edge;
1728 gcov_type saved_sum_all = 0;
1729 gcov_ctr_summary *saved_profile_info = 0;
1730 int saved_scale = 0;
1731
1732 /* Find unit with maximal number of runs. If we ever get serious about
1733 roundoff errors, we might also consider computing smallest common
1734 multiply. */
1735 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1736 if (max_runs < file_data->profile_info.runs)
1737 max_runs = file_data->profile_info.runs;
1738
1739 if (!max_runs)
1740 return;
1741
1742 /* Simple overflow check. We probably don't need to support that many train
1743 runs. Such a large value probably imply data corruption anyway. */
1744 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1745 {
1746 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1747 INT_MAX / REG_BR_PROB_BASE);
1748 return;
1749 }
1750
1751 profile_info = &lto_gcov_summary;
1752 lto_gcov_summary.runs = max_runs;
1753 lto_gcov_summary.sum_max = 0;
1754 memset (lto_gcov_summary.histogram, 0,
1755 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1756
1757 /* Rescale all units to the maximal number of runs.
1758 sum_max can not be easily merged, as we have no idea what files come from
1759 the same run. We do not use the info anyway, so leave it 0. */
1760 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1761 if (file_data->profile_info.runs)
1762 {
1763 int scale = GCOV_COMPUTE_SCALE (max_runs,
1764 file_data->profile_info.runs);
1765 lto_gcov_summary.sum_max
1766 = MAX (lto_gcov_summary.sum_max,
1767 apply_scale (file_data->profile_info.sum_max, scale));
1768 lto_gcov_summary.sum_all
1769 = MAX (lto_gcov_summary.sum_all,
1770 apply_scale (file_data->profile_info.sum_all, scale));
1771 /* Save a pointer to the profile_info with the largest
1772 scaled sum_all and the scale for use in merging the
1773 histogram. */
1774 if (!saved_profile_info
1775 || lto_gcov_summary.sum_all > saved_sum_all)
1776 {
1777 saved_profile_info = &file_data->profile_info;
1778 saved_sum_all = lto_gcov_summary.sum_all;
1779 saved_scale = scale;
1780 }
1781 }
1782
1783 gcc_assert (saved_profile_info);
1784
1785 /* Scale up the histogram from the profile that had the largest
1786 scaled sum_all above. */
1787 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1788 {
1789 /* Scale up the min value as we did the corresponding sum_all
1790 above. Use that to find the new histogram index. */
1791 gcov_type scaled_min
1792 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1793 saved_scale);
1794 /* The new index may be shared with another scaled histogram entry,
1795 so we need to account for a non-zero histogram entry at new_ix. */
1796 unsigned new_ix = gcov_histo_index (scaled_min);
1797 lto_gcov_summary.histogram[new_ix].min_value
1798 = (lto_gcov_summary.histogram[new_ix].num_counters
1799 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1800 : scaled_min);
1801 /* Some of the scaled counter values would ostensibly need to be placed
1802 into different (larger) histogram buckets, but we keep things simple
1803 here and place the scaled cumulative counter value in the bucket
1804 corresponding to the scaled minimum counter value. */
1805 lto_gcov_summary.histogram[new_ix].cum_value
1806 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1807 saved_scale);
1808 lto_gcov_summary.histogram[new_ix].num_counters
1809 += saved_profile_info->histogram[h_ix].num_counters;
1810 }
1811
1812 /* Watch roundoff errors. */
1813 if (lto_gcov_summary.sum_max < max_runs)
1814 lto_gcov_summary.sum_max = max_runs;
1815
1816 /* If merging already happent at WPA time, we are done. */
1817 if (flag_ltrans)
1818 return;
1819
1820 /* Now compute count_materialization_scale of each node.
1821 During LTRANS we already have values of count_materialization_scale
1822 computed, so just update them. */
1823 FOR_EACH_FUNCTION (node)
1824 if (node->lto_file_data
1825 && node->lto_file_data->profile_info.runs)
1826 {
1827 int scale;
1828
1829 scale = RDIV (node->count_materialization_scale * max_runs,
1830 node->lto_file_data->profile_info.runs);
1831 node->count_materialization_scale = scale;
1832 if (scale < 0)
1833 fatal_error (input_location, "Profile information in %s corrupted",
1834 file_data->file_name);
1835
1836 if (scale == REG_BR_PROB_BASE)
1837 continue;
1838 for (edge = node->callees; edge; edge = edge->next_callee)
1839 edge->count = apply_scale (edge->count, scale);
1840 node->count = apply_scale (node->count, scale);
1841 }
1842 }
1843
1844 /* Input and merge the symtab from each of the .o files passed to
1845 lto1. */
1846
1847 void
1848 input_symtab (void)
1849 {
1850 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1851 struct lto_file_decl_data *file_data;
1852 unsigned int j = 0;
1853 struct cgraph_node *node;
1854
1855 while ((file_data = file_data_vec[j++]))
1856 {
1857 const char *data;
1858 size_t len;
1859 struct lto_input_block *ib;
1860 vec<symtab_node *> nodes;
1861
1862 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1863 &data, &len);
1864 if (!ib)
1865 fatal_error (input_location,
1866 "cannot find LTO cgraph in %s", file_data->file_name);
1867 input_profile_summary (ib, file_data);
1868 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1869 nodes = input_cgraph_1 (file_data, ib);
1870 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1871 ib, data, len);
1872
1873 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1874 &data, &len);
1875 if (!ib)
1876 fatal_error (input_location, "cannot find LTO section refs in %s",
1877 file_data->file_name);
1878 input_refs (ib, nodes);
1879 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1880 ib, data, len);
1881 if (flag_ltrans)
1882 input_cgraph_opt_summary (nodes);
1883 nodes.release ();
1884 }
1885
1886 merge_profile_summaries (file_data_vec);
1887 get_working_sets ();
1888
1889
1890 /* Clear out the aux field that was used to store enough state to
1891 tell which nodes should be overwritten. */
1892 FOR_EACH_FUNCTION (node)
1893 {
1894 /* Some nodes may have been created by cgraph_node. This
1895 happens when the callgraph contains nested functions. If the
1896 node for the parent function was never emitted to the gimple
1897 file, cgraph_node will create a node for it when setting the
1898 context of the nested function. */
1899 if (node->lto_file_data)
1900 node->aux = NULL;
1901 }
1902 }
1903
1904 /* Input function/variable tables that will allow libgomp to look up offload
1905 target code, and store them into OFFLOAD_FUNCS and OFFLOAD_VARS. */
1906
1907 void
1908 input_offload_tables (void)
1909 {
1910 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1911 struct lto_file_decl_data *file_data;
1912 unsigned int j = 0;
1913
1914 while ((file_data = file_data_vec[j++]))
1915 {
1916 const char *data;
1917 size_t len;
1918 struct lto_input_block *ib
1919 = lto_create_simple_input_block (file_data, LTO_section_offload_table,
1920 &data, &len);
1921 if (!ib)
1922 continue;
1923
1924 enum LTO_symtab_tags tag
1925 = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1926 while (tag)
1927 {
1928 if (tag == LTO_symtab_unavail_node)
1929 {
1930 int decl_index = streamer_read_uhwi (ib);
1931 tree fn_decl
1932 = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1933 vec_safe_push (offload_funcs, fn_decl);
1934 }
1935 else if (tag == LTO_symtab_variable)
1936 {
1937 int decl_index = streamer_read_uhwi (ib);
1938 tree var_decl
1939 = lto_file_decl_data_get_var_decl (file_data, decl_index);
1940 vec_safe_push (offload_vars, var_decl);
1941 }
1942 else
1943 fatal_error (input_location,
1944 "invalid offload table in %s", file_data->file_name);
1945
1946 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1947 }
1948
1949 lto_destroy_simple_input_block (file_data, LTO_section_offload_table,
1950 ib, data, len);
1951 }
1952 }
1953
1954 /* True when we need optimization summary for NODE. */
1955
1956 static int
1957 output_cgraph_opt_summary_p (struct cgraph_node *node)
1958 {
1959 return (node->clone_of
1960 && (node->clone.tree_map
1961 || node->clone.args_to_skip
1962 || node->clone.combined_args_to_skip));
1963 }
1964
1965 /* Output optimization summary for EDGE to OB. */
1966 static void
1967 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1968 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1969 {
1970 }
1971
1972 /* Output optimization summary for NODE to OB. */
1973
1974 static void
1975 output_node_opt_summary (struct output_block *ob,
1976 struct cgraph_node *node,
1977 lto_symtab_encoder_t encoder)
1978 {
1979 unsigned int index;
1980 bitmap_iterator bi;
1981 struct ipa_replace_map *map;
1982 struct bitpack_d bp;
1983 int i;
1984 struct cgraph_edge *e;
1985
1986 if (node->clone.args_to_skip)
1987 {
1988 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1989 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1990 streamer_write_uhwi (ob, index);
1991 }
1992 else
1993 streamer_write_uhwi (ob, 0);
1994 if (node->clone.combined_args_to_skip)
1995 {
1996 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1997 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1998 streamer_write_uhwi (ob, index);
1999 }
2000 else
2001 streamer_write_uhwi (ob, 0);
2002 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
2003 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
2004 {
2005 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
2006 mechanism to store function local declarations into summaries. */
2007 gcc_assert (!map->old_tree);
2008 streamer_write_uhwi (ob, map->parm_num);
2009 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
2010 stream_write_tree (ob, map->new_tree, true);
2011 bp = bitpack_create (ob->main_stream);
2012 bp_pack_value (&bp, map->replace_p, 1);
2013 bp_pack_value (&bp, map->ref_p, 1);
2014 streamer_write_bitpack (&bp);
2015 }
2016
2017 if (lto_symtab_encoder_in_partition_p (encoder, node))
2018 {
2019 for (e = node->callees; e; e = e->next_callee)
2020 output_edge_opt_summary (ob, e);
2021 for (e = node->indirect_calls; e; e = e->next_callee)
2022 output_edge_opt_summary (ob, e);
2023 }
2024 }
2025
2026 /* Output optimization summaries stored in callgraph.
2027 At the moment it is the clone info structure. */
2028
2029 static void
2030 output_cgraph_opt_summary (void)
2031 {
2032 int i, n_nodes;
2033 lto_symtab_encoder_t encoder;
2034 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
2035 unsigned count = 0;
2036
2037 ob->symbol = NULL;
2038 encoder = ob->decl_state->symtab_node_encoder;
2039 n_nodes = lto_symtab_encoder_size (encoder);
2040 for (i = 0; i < n_nodes; i++)
2041 {
2042 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
2043 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2044 if (cnode && output_cgraph_opt_summary_p (cnode))
2045 count++;
2046 }
2047 streamer_write_uhwi (ob, count);
2048 for (i = 0; i < n_nodes; i++)
2049 {
2050 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
2051 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2052 if (cnode && output_cgraph_opt_summary_p (cnode))
2053 {
2054 streamer_write_uhwi (ob, i);
2055 output_node_opt_summary (ob, cnode, encoder);
2056 }
2057 }
2058 produce_asm (ob, NULL);
2059 destroy_output_block (ob);
2060 }
2061
2062 /* Input optimisation summary of EDGE. */
2063
2064 static void
2065 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
2066 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
2067 {
2068 }
2069
2070 /* Input optimisation summary of NODE. */
2071
2072 static void
2073 input_node_opt_summary (struct cgraph_node *node,
2074 struct lto_input_block *ib_main,
2075 struct data_in *data_in)
2076 {
2077 int i;
2078 int count;
2079 int bit;
2080 struct bitpack_d bp;
2081 struct cgraph_edge *e;
2082
2083 count = streamer_read_uhwi (ib_main);
2084 if (count)
2085 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
2086 for (i = 0; i < count; i++)
2087 {
2088 bit = streamer_read_uhwi (ib_main);
2089 bitmap_set_bit (node->clone.args_to_skip, bit);
2090 }
2091 count = streamer_read_uhwi (ib_main);
2092 if (count)
2093 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
2094 for (i = 0; i < count; i++)
2095 {
2096 bit = streamer_read_uhwi (ib_main);
2097 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
2098 }
2099 count = streamer_read_uhwi (ib_main);
2100 for (i = 0; i < count; i++)
2101 {
2102 struct ipa_replace_map *map = ggc_alloc<ipa_replace_map> ();
2103
2104 vec_safe_push (node->clone.tree_map, map);
2105 map->parm_num = streamer_read_uhwi (ib_main);
2106 map->old_tree = NULL;
2107 map->new_tree = stream_read_tree (ib_main, data_in);
2108 bp = streamer_read_bitpack (ib_main);
2109 map->replace_p = bp_unpack_value (&bp, 1);
2110 map->ref_p = bp_unpack_value (&bp, 1);
2111 }
2112 for (e = node->callees; e; e = e->next_callee)
2113 input_edge_opt_summary (e, ib_main);
2114 for (e = node->indirect_calls; e; e = e->next_callee)
2115 input_edge_opt_summary (e, ib_main);
2116 }
2117
2118 /* Read section in file FILE_DATA of length LEN with data DATA. */
2119
2120 static void
2121 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
2122 const char *data, size_t len,
2123 vec<symtab_node *> nodes)
2124 {
2125 const struct lto_function_header *header =
2126 (const struct lto_function_header *) data;
2127 const int cfg_offset = sizeof (struct lto_function_header);
2128 const int main_offset = cfg_offset + header->cfg_size;
2129 const int string_offset = main_offset + header->main_size;
2130 struct data_in *data_in;
2131 unsigned int i;
2132 unsigned int count;
2133
2134 lto_input_block ib_main ((const char *) data + main_offset,
2135 header->main_size, file_data->mode_table);
2136
2137 data_in =
2138 lto_data_in_create (file_data, (const char *) data + string_offset,
2139 header->string_size, vNULL);
2140 count = streamer_read_uhwi (&ib_main);
2141
2142 for (i = 0; i < count; i++)
2143 {
2144 int ref = streamer_read_uhwi (&ib_main);
2145 input_node_opt_summary (dyn_cast<cgraph_node *> (nodes[ref]),
2146 &ib_main, data_in);
2147 }
2148 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
2149 len);
2150 lto_data_in_delete (data_in);
2151 }
2152
2153 /* Input optimization summary of cgraph. */
2154
2155 static void
2156 input_cgraph_opt_summary (vec<symtab_node *> nodes)
2157 {
2158 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2159 struct lto_file_decl_data *file_data;
2160 unsigned int j = 0;
2161
2162 while ((file_data = file_data_vec[j++]))
2163 {
2164 size_t len;
2165 const char *data =
2166 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
2167 &len);
2168
2169 if (data)
2170 input_cgraph_opt_section (file_data, data, len, nodes);
2171 }
2172 }