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