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