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