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