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