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