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