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