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