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