2009-09-09 Segher Boessenkool <segher@kernel.crashing.org>
[official-gcc.git] / gcc / lto-cgraph.c
blob309a1e6e96426a5fdb579fe28c7b1f3f1611838f
1 /* Write and read the cgraph to the memory mapped representation of a
2 .o file.
4 Copyright 2009 Free Software Foundation, Inc.
5 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
7 This file is part of GCC.
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.
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.
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/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "toplev.h"
28 #include "tree.h"
29 #include "expr.h"
30 #include "flags.h"
31 #include "params.h"
32 #include "input.h"
33 #include "varray.h"
34 #include "hashtab.h"
35 #include "langhooks.h"
36 #include "basic-block.h"
37 #include "tree-flow.h"
38 #include "cgraph.h"
39 #include "function.h"
40 #include "ggc.h"
41 #include "diagnostic.h"
42 #include "except.h"
43 #include "vec.h"
44 #include "timevar.h"
45 #include "output.h"
46 #include "pointer-set.h"
47 #include "lto-streamer.h"
48 #include "gcov-io.h"
50 /* Create a new cgraph encoder. */
52 lto_cgraph_encoder_t
53 lto_cgraph_encoder_new (void)
55 lto_cgraph_encoder_t encoder = XCNEW (struct lto_cgraph_encoder_d);
56 encoder->map = pointer_map_create ();
57 encoder->nodes = NULL;
58 return encoder;
62 /* Delete ENCODER and its components. */
64 void
65 lto_cgraph_encoder_delete (lto_cgraph_encoder_t encoder)
67 VEC_free (cgraph_node_ptr, heap, encoder->nodes);
68 pointer_map_destroy (encoder->map);
69 free (encoder);
73 /* Return the existing reference number of NODE in the cgraph encoder in
74 output block OB. Assign a new reference if this is the first time
75 NODE is encoded. */
77 int
78 lto_cgraph_encoder_encode (lto_cgraph_encoder_t encoder,
79 struct cgraph_node *node)
81 int ref;
82 void **slot;
84 slot = pointer_map_contains (encoder->map, node);
85 if (!slot)
87 ref = VEC_length (cgraph_node_ptr, encoder->nodes);
88 slot = pointer_map_insert (encoder->map, node);
89 *slot = (void *) (intptr_t) ref;
90 VEC_safe_push (cgraph_node_ptr, heap, encoder->nodes, node);
92 else
93 ref = (int) (intptr_t) *slot;
95 return ref;
99 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
100 or LCC_NOT_FOUND if it is not there. */
103 lto_cgraph_encoder_lookup (lto_cgraph_encoder_t encoder,
104 struct cgraph_node *node)
106 void **slot = pointer_map_contains (encoder->map, node);
107 return (slot ? (int) (intptr_t) *slot : LCC_NOT_FOUND);
111 /* Return the cgraph node corresponding to REF using ENCODER. */
113 struct cgraph_node *
114 lto_cgraph_encoder_deref (lto_cgraph_encoder_t encoder, int ref)
116 if (ref == LCC_NOT_FOUND)
117 return NULL;
119 return VEC_index (cgraph_node_ptr, encoder->nodes, ref);
123 /* Return number of encoded nodes in ENCODER. */
125 static int
126 lto_cgraph_encoder_size (lto_cgraph_encoder_t encoder)
128 return VEC_length (cgraph_node_ptr, encoder->nodes);
132 /* Output the cgraph EDGE to OB using ENCODER. */
134 static void
135 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
136 lto_cgraph_encoder_t encoder)
138 unsigned int uid;
139 intptr_t ref;
140 struct bitpack_d *bp;
142 lto_output_uleb128_stream (ob->main_stream, LTO_cgraph_edge);
144 ref = lto_cgraph_encoder_lookup (encoder, edge->caller);
145 gcc_assert (ref != LCC_NOT_FOUND);
146 lto_output_sleb128_stream (ob->main_stream, ref);
148 ref = lto_cgraph_encoder_lookup (encoder, edge->callee);
149 gcc_assert (ref != LCC_NOT_FOUND);
150 lto_output_sleb128_stream (ob->main_stream, ref);
152 lto_output_sleb128_stream (ob->main_stream, edge->count);
154 bp = bitpack_create ();
155 uid = flag_wpa ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt);
156 bp_pack_value (bp, uid, HOST_BITS_PER_INT);
157 bp_pack_value (bp, edge->inline_failed, HOST_BITS_PER_INT);
158 bp_pack_value (bp, edge->frequency, HOST_BITS_PER_INT);
159 bp_pack_value (bp, edge->loop_nest, 30);
160 bp_pack_value (bp, edge->indirect_call, 1);
161 bp_pack_value (bp, edge->call_stmt_cannot_inline_p, 1);
162 bp_pack_value (bp, edge->can_throw_external, 1);
163 lto_output_bitpack (ob->main_stream, bp);
164 bitpack_delete (bp);
168 /* Output the cgraph NODE to OB. ENCODER is used to find the
169 reference number of NODE->inlined_to. SET is the set of nodes we
170 are writing to the current file. If NODE is not in SET, then NODE
171 is a boundary of a cgraph_node_set and we pretend NODE just has a
172 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
173 that have had their callgraph node written so far. This is used to
174 determine if NODE is a clone of a previously written node. */
176 static void
177 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
178 lto_cgraph_encoder_t encoder, cgraph_node_set set,
179 bitmap written_decls)
181 unsigned int tag;
182 struct bitpack_d *bp;
183 unsigned local, externally_visible, inlinable, analyzed;
184 bool boundary_p, wrote_decl_p;
185 intptr_t ref;
187 boundary_p = !cgraph_node_in_set_p (node, set);
188 wrote_decl_p = bitmap_bit_p (written_decls, DECL_UID (node->decl));
190 switch (cgraph_function_body_availability (node))
192 case AVAIL_NOT_AVAILABLE:
193 tag = LTO_cgraph_unavail_node;
194 break;
196 case AVAIL_AVAILABLE:
197 case AVAIL_LOCAL:
198 tag = LTO_cgraph_avail_node;
199 break;
201 case AVAIL_OVERWRITABLE:
202 tag = LTO_cgraph_overwritable_node;
203 break;
205 default:
206 gcc_unreachable ();
209 if (boundary_p)
210 tag = LTO_cgraph_unavail_node;
212 lto_output_uleb128_stream (ob->main_stream, tag);
214 local = node->local.local;
215 externally_visible = node->local.externally_visible;
216 inlinable = node->local.inlinable;
217 analyzed = node->analyzed;
219 /* In WPA mode, we only output part of the call-graph. Also, we
220 fake cgraph node attributes. There are two cases that we care.
222 Boundary nodes: There are nodes that are not part of SET but are
223 called from within SET. We artificially make them look like
224 externally visible nodes with no function body.
226 Cherry-picked nodes: These are nodes we pulled from other
227 translation units into SET during IPA-inlining. We make them as
228 local static nodes to prevent clashes with other local statics. */
229 if (boundary_p)
231 /* Inline clones can not be part of boundary. */
232 gcc_assert (!node->global.inlined_to);
233 local = 0;
234 externally_visible = 1;
235 inlinable = 0;
236 analyzed = 0;
238 else if (lto_forced_extern_inline_p (node->decl))
240 local = 1;
241 externally_visible = 0;
242 inlinable = 1;
245 lto_output_uleb128_stream (ob->main_stream, wrote_decl_p);
247 if (!wrote_decl_p)
248 bitmap_set_bit (written_decls, DECL_UID (node->decl));
250 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
251 lto_output_sleb128_stream (ob->main_stream, node->count);
253 bp = bitpack_create ();
254 bp_pack_value (bp, local, 1);
255 bp_pack_value (bp, externally_visible, 1);
256 bp_pack_value (bp, node->local.finalized, 1);
257 bp_pack_value (bp, inlinable, 1);
258 bp_pack_value (bp, node->local.disregard_inline_limits, 1);
259 bp_pack_value (bp, node->local.redefined_extern_inline, 1);
260 bp_pack_value (bp, node->local.for_functions_valid, 1);
261 bp_pack_value (bp, node->local.vtable_method, 1);
262 bp_pack_value (bp, node->needed, 1);
263 bp_pack_value (bp, node->address_taken, 1);
264 bp_pack_value (bp, node->abstract_and_needed, 1);
265 bp_pack_value (bp, node->reachable, 1);
266 bp_pack_value (bp, node->lowered, 1);
267 bp_pack_value (bp, analyzed, 1);
268 bp_pack_value (bp, node->process, 1);
269 bp_pack_value (bp, node->alias, 1);
270 bp_pack_value (bp, node->finalized_by_frontend, 1);
271 lto_output_bitpack (ob->main_stream, bp);
272 bitpack_delete (bp);
274 if (tag != LTO_cgraph_unavail_node)
276 lto_output_sleb128_stream (ob->main_stream,
277 node->local.inline_summary.estimated_self_stack_size);
278 lto_output_sleb128_stream (ob->main_stream,
279 node->local.inline_summary.self_size);
280 lto_output_sleb128_stream (ob->main_stream,
281 node->local.inline_summary.size_inlining_benefit);
282 lto_output_sleb128_stream (ob->main_stream,
283 node->local.inline_summary.self_time);
284 lto_output_sleb128_stream (ob->main_stream,
285 node->local.inline_summary.time_inlining_benefit);
288 /* FIXME lto: Outputting global info is not neccesary until after
289 inliner was run. Global structure holds results of propagation
290 done by inliner. */
291 lto_output_sleb128_stream (ob->main_stream,
292 node->global.estimated_stack_size);
293 lto_output_sleb128_stream (ob->main_stream,
294 node->global.stack_frame_offset);
295 if (node->global.inlined_to && !boundary_p)
297 ref = lto_cgraph_encoder_lookup (encoder, node->global.inlined_to);
298 gcc_assert (ref != LCC_NOT_FOUND);
300 else
301 ref = LCC_NOT_FOUND;
302 lto_output_sleb128_stream (ob->main_stream, ref);
304 lto_output_sleb128_stream (ob->main_stream, node->global.time);
305 lto_output_sleb128_stream (ob->main_stream, node->global.size);
306 lto_output_sleb128_stream (ob->main_stream,
307 node->global.estimated_growth);
308 lto_output_uleb128_stream (ob->main_stream, node->global.inlined);
309 if (node->same_body)
311 struct cgraph_node *alias;
312 unsigned long alias_count = 1;
313 for (alias = node->same_body; alias->next; alias = alias->next)
314 alias_count++;
315 lto_output_uleb128_stream (ob->main_stream, alias_count);
318 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
319 alias->decl);
320 if (alias->thunk.thunk_p)
322 lto_output_uleb128_stream
323 (ob->main_stream,
324 1 + (alias->thunk.this_adjusting != 0) * 2
325 + (alias->thunk.virtual_offset_p != 0) * 4);
326 lto_output_uleb128_stream (ob->main_stream,
327 alias->thunk.fixed_offset);
328 lto_output_uleb128_stream (ob->main_stream,
329 alias->thunk.virtual_value);
330 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
331 alias->thunk.alias);
333 else
334 lto_output_uleb128_stream (ob->main_stream, 0);
335 alias = alias->previous;
337 while (alias);
339 else
340 lto_output_uleb128_stream (ob->main_stream, 0);
343 /* Stream out profile_summary to OB. */
345 static void
346 output_profile_summary (struct lto_simple_output_block *ob)
348 if (profile_info)
350 /* We do not output num, it is not terribly useful. */
351 gcc_assert (profile_info->runs);
352 lto_output_uleb128_stream (ob->main_stream, profile_info->runs);
353 lto_output_sleb128_stream (ob->main_stream, profile_info->sum_all);
354 lto_output_sleb128_stream (ob->main_stream, profile_info->run_max);
355 lto_output_sleb128_stream (ob->main_stream, profile_info->sum_max);
357 else
358 lto_output_uleb128_stream (ob->main_stream, 0);
362 /* Output the part of the cgraph in SET. */
364 void
365 output_cgraph (cgraph_node_set set)
367 struct cgraph_node *node;
368 struct lto_simple_output_block *ob;
369 cgraph_node_set_iterator csi;
370 struct cgraph_edge *edge;
371 int i, n_nodes;
372 bitmap written_decls;
373 lto_cgraph_encoder_t encoder;
374 struct cgraph_asm_node *can;
376 ob = lto_create_simple_output_block (LTO_section_cgraph);
378 output_profile_summary (ob);
380 /* An encoder for cgraph nodes should have been created by
381 ipa_write_summaries_1. */
382 gcc_assert (ob->decl_state->cgraph_node_encoder);
383 encoder = ob->decl_state->cgraph_node_encoder;
385 /* The FUNCTION_DECLs for which we have written a node. The first
386 node found is written as the "original" node, the remaining nodes
387 are considered its clones. */
388 written_decls = lto_bitmap_alloc ();
390 /* Go over all the nodes in SET and assign references. */
391 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
393 node = csi_node (csi);
394 lto_cgraph_encoder_encode (encoder, node);
397 /* Go over all the nodes again to include callees that are not in
398 SET. */
399 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
401 node = csi_node (csi);
402 for (edge = node->callees; edge; edge = edge->next_callee)
404 struct cgraph_node *callee = edge->callee;
405 if (!cgraph_node_in_set_p (callee, set))
407 /* We should have moved all the inlines. */
408 gcc_assert (!callee->global.inlined_to);
409 lto_cgraph_encoder_encode (encoder, callee);
414 /* Write out the nodes. */
415 n_nodes = lto_cgraph_encoder_size (encoder);
416 for (i = 0; i < n_nodes; i++)
418 node = lto_cgraph_encoder_deref (encoder, i);
419 lto_output_node (ob, node, encoder, set, written_decls);
422 lto_bitmap_free (written_decls);
424 /* Go over the nodes in SET again to write edges. */
425 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
427 node = csi_node (csi);
428 if (node->callees)
430 /* Output edges in backward direction, so the reconstructed callgraph
431 match and it is easy to associate call sites in the IPA pass summaries. */
432 edge = node->callees;
433 while (edge->next_callee)
434 edge = edge->next_callee;
435 for (; edge; edge = edge->prev_callee)
436 lto_output_edge (ob, edge, encoder);
440 lto_output_uleb128_stream (ob->main_stream, 0);
442 /* Emit toplevel asms. */
443 for (can = cgraph_asm_nodes; can; can = can->next)
445 int len = TREE_STRING_LENGTH (can->asm_str);
446 lto_output_uleb128_stream (ob->main_stream, len);
447 for (i = 0; i < len; ++i)
448 lto_output_1_stream (ob->main_stream,
449 TREE_STRING_POINTER (can->asm_str)[i]);
452 lto_output_uleb128_stream (ob->main_stream, 0);
454 lto_destroy_simple_output_block (ob);
458 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
459 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
460 NODE or to replace the values in it, for instance because the first
461 time we saw it, the function body was not available but now it
462 is. BP is a bitpack with all the bitflags for NODE read from the
463 stream. */
465 static void
466 input_overwrite_node (struct lto_file_decl_data *file_data,
467 struct cgraph_node *node,
468 enum LTO_cgraph_tags tag,
469 struct bitpack_d *bp,
470 unsigned int stack_size,
471 unsigned int self_time,
472 unsigned int time_inlining_benefit,
473 unsigned int self_size,
474 unsigned int size_inlining_benefit)
476 node->aux = (void *) tag;
477 node->local.inline_summary.estimated_self_stack_size = stack_size;
478 node->local.inline_summary.self_time = self_time;
479 node->local.inline_summary.time_inlining_benefit = time_inlining_benefit;
480 node->local.inline_summary.self_size = self_size;
481 node->local.inline_summary.size_inlining_benefit = size_inlining_benefit;
482 node->global.time = self_time;
483 node->global.size = self_size;
484 node->local.lto_file_data = file_data;
486 node->local.local = bp_unpack_value (bp, 1);
487 node->local.externally_visible = bp_unpack_value (bp, 1);
488 node->local.finalized = bp_unpack_value (bp, 1);
489 node->local.inlinable = bp_unpack_value (bp, 1);
490 node->local.disregard_inline_limits = bp_unpack_value (bp, 1);
491 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
492 node->local.for_functions_valid = bp_unpack_value (bp, 1);
493 node->local.vtable_method = bp_unpack_value (bp, 1);
494 node->needed = bp_unpack_value (bp, 1);
495 node->address_taken = bp_unpack_value (bp, 1);
496 node->abstract_and_needed = bp_unpack_value (bp, 1);
497 node->reachable = bp_unpack_value (bp, 1);
498 node->lowered = bp_unpack_value (bp, 1);
499 node->analyzed = bp_unpack_value (bp, 1);
500 node->process = bp_unpack_value (bp, 1);
501 node->alias = bp_unpack_value (bp, 1);
502 node->finalized_by_frontend = bp_unpack_value (bp, 1);
506 /* Read a node from input_block IB. TAG is the node's tag just read.
507 Return the node read or overwriten. */
509 static struct cgraph_node *
510 input_node (struct lto_file_decl_data *file_data,
511 struct lto_input_block *ib,
512 enum LTO_cgraph_tags tag)
514 tree fn_decl;
515 struct cgraph_node *node;
516 struct bitpack_d *bp;
517 int stack_size = 0;
518 unsigned decl_index;
519 bool clone_p;
520 int estimated_stack_size = 0;
521 int stack_frame_offset = 0;
522 int ref = LCC_NOT_FOUND;
523 int estimated_growth = 0;
524 int time = 0;
525 int size = 0;
526 int self_time = 0;
527 int self_size = 0;
528 int time_inlining_benefit = 0;
529 int size_inlining_benefit = 0;
530 unsigned long same_body_count = 0;
531 bool inlined = false;
533 clone_p = (lto_input_uleb128 (ib) != 0);
535 decl_index = lto_input_uleb128 (ib);
536 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
538 if (clone_p)
539 node = cgraph_clone_node (cgraph_node (fn_decl), 0,
540 CGRAPH_FREQ_BASE, 0, false, NULL);
542 else
543 node = cgraph_node (fn_decl);
545 node->count = lto_input_sleb128 (ib);
546 bp = lto_input_bitpack (ib);
548 if (tag != LTO_cgraph_unavail_node)
550 stack_size = lto_input_sleb128 (ib);
551 self_size = lto_input_sleb128 (ib);
552 size_inlining_benefit = lto_input_sleb128 (ib);
553 self_time = lto_input_sleb128 (ib);
554 time_inlining_benefit = lto_input_sleb128 (ib);
557 estimated_stack_size = lto_input_sleb128 (ib);
558 stack_frame_offset = lto_input_sleb128 (ib);
559 ref = lto_input_sleb128 (ib);
560 time = lto_input_sleb128 (ib);
561 size = lto_input_sleb128 (ib);
562 estimated_growth = lto_input_sleb128 (ib);
563 inlined = lto_input_uleb128 (ib);
564 same_body_count = lto_input_uleb128 (ib);
566 /* Make sure that we have not read this node before. Nodes that
567 have already been read will have their tag stored in the 'aux'
568 field. Since built-in functions can be referenced in multiple
569 functions, they are expected to be read more than once. */
570 if (node->aux && !DECL_IS_BUILTIN (node->decl))
571 internal_error ("bytecode stream: found multiple instances of cgraph "
572 "node %d", node->uid);
574 input_overwrite_node (file_data, node, tag, bp, stack_size, self_time,
575 time_inlining_benefit, self_size,
576 size_inlining_benefit);
577 bitpack_delete (bp);
579 node->global.estimated_stack_size = estimated_stack_size;
580 node->global.stack_frame_offset = stack_frame_offset;
581 node->global.time = time;
582 node->global.size = size;
584 /* Store a reference for now, and fix up later to be a pointer. */
585 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
587 node->global.estimated_growth = estimated_growth;
588 node->global.inlined = inlined;
590 while (same_body_count-- > 0)
592 tree alias_decl;
593 int type;
594 decl_index = lto_input_uleb128 (ib);
595 alias_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
596 type = lto_input_uleb128 (ib);
597 if (!type)
598 cgraph_same_body_alias (alias_decl, fn_decl);
599 else
601 HOST_WIDE_INT fixed_offset = lto_input_uleb128 (ib);
602 HOST_WIDE_INT virtual_value = lto_input_uleb128 (ib);
603 tree real_alias;
604 decl_index = lto_input_uleb128 (ib);
605 real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
606 cgraph_add_thunk (alias_decl, fn_decl, type & 2, fixed_offset,
607 virtual_value,
608 (type & 4) ? size_int (virtual_value) : NULL_TREE,
609 real_alias);
612 return node;
616 /* Read an edge from IB. NODES points to a vector of previously read
617 nodes for decoding caller and callee of the edge to be read. */
619 static void
620 input_edge (struct lto_input_block *ib, VEC(cgraph_node_ptr, heap) *nodes)
622 struct cgraph_node *caller, *callee;
623 struct cgraph_edge *edge;
624 unsigned int stmt_id;
625 gcov_type count;
626 int freq;
627 unsigned int nest;
628 cgraph_inline_failed_t inline_failed;
629 struct bitpack_d *bp;
630 enum ld_plugin_symbol_resolution caller_resolution;
632 caller = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
633 if (caller == NULL || caller->decl == NULL_TREE)
634 internal_error ("bytecode stream: no caller found while reading edge");
636 callee = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
637 if (callee == NULL || callee->decl == NULL_TREE)
638 internal_error ("bytecode stream: no callee found while reading edge");
640 count = (gcov_type) lto_input_sleb128 (ib);
642 bp = lto_input_bitpack (ib);
643 stmt_id = (unsigned int) bp_unpack_value (bp, HOST_BITS_PER_INT);
644 inline_failed = (cgraph_inline_failed_t) bp_unpack_value (bp,
645 HOST_BITS_PER_INT);
646 freq = (int) bp_unpack_value (bp, HOST_BITS_PER_INT);
647 nest = (unsigned) bp_unpack_value (bp, 30);
649 /* If the caller was preempted, don't create the edge.
650 ??? Should we ever have edges from a preempted caller? */
651 caller_resolution = lto_symtab_get_resolution (caller->decl);
652 if (caller_resolution == LDPR_PREEMPTED_REG
653 || caller_resolution == LDPR_PREEMPTED_IR)
654 return;
656 edge = cgraph_create_edge (caller, callee, NULL, count, freq, nest);
657 edge->lto_stmt_uid = stmt_id;
658 edge->inline_failed = inline_failed;
659 edge->indirect_call = bp_unpack_value (bp, 1);
660 edge->call_stmt_cannot_inline_p = bp_unpack_value (bp, 1);
661 edge->can_throw_external = bp_unpack_value (bp, 1);
662 bitpack_delete (bp);
666 /* Read a cgraph from IB using the info in FILE_DATA. */
668 static void
669 input_cgraph_1 (struct lto_file_decl_data *file_data,
670 struct lto_input_block *ib)
672 enum LTO_cgraph_tags tag;
673 VEC(cgraph_node_ptr, heap) *nodes = NULL;
674 struct cgraph_node *node;
675 unsigned i;
676 unsigned HOST_WIDE_INT len;
678 tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
679 while (tag)
681 if (tag == LTO_cgraph_edge)
682 input_edge (ib, nodes);
683 else
685 node = input_node (file_data, ib, tag);
686 if (node == NULL || node->decl == NULL_TREE)
687 internal_error ("bytecode stream: found empty cgraph node");
688 VEC_safe_push (cgraph_node_ptr, heap, nodes, node);
689 lto_cgraph_encoder_encode (file_data->cgraph_node_encoder, node);
692 tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
695 /* Input toplevel asms. */
696 len = lto_input_uleb128 (ib);
697 while (len)
699 char *str = (char *)xmalloc (len + 1);
700 for (i = 0; i < len; ++i)
701 str[i] = lto_input_1_unsigned (ib);
702 cgraph_add_asm_node (build_string (len, str));
703 free (str);
705 len = lto_input_uleb128 (ib);
708 for (i = 0; VEC_iterate (cgraph_node_ptr, nodes, i, node); i++)
710 const int ref = (int) (intptr_t) node->global.inlined_to;
712 /* Fixup inlined_to from reference to pointer. */
713 if (ref != LCC_NOT_FOUND)
714 node->global.inlined_to = VEC_index (cgraph_node_ptr, nodes, ref);
715 else
716 node->global.inlined_to = NULL;
719 VEC_free (cgraph_node_ptr, heap, nodes);
722 static struct gcov_ctr_summary lto_gcov_summary;
724 /* Input profile_info from IB. */
725 static void
726 input_profile_summary (struct lto_input_block *ib)
728 unsigned int runs = lto_input_uleb128 (ib);
729 if (runs)
731 if (!profile_info)
733 profile_info = &lto_gcov_summary;
734 lto_gcov_summary.runs = runs;
735 lto_gcov_summary.sum_all = lto_input_sleb128 (ib);
736 lto_gcov_summary.run_max = lto_input_sleb128 (ib);
737 lto_gcov_summary.sum_max = lto_input_sleb128 (ib);
739 /* We can support this by scaling all counts to nearest common multiple
740 of all different runs, but it is perhaps not worth the effort. */
741 else if (profile_info->runs != runs
742 || profile_info->sum_all != lto_input_sleb128 (ib)
743 || profile_info->run_max != lto_input_sleb128 (ib)
744 || profile_info->sum_max != lto_input_sleb128 (ib))
745 sorry ("Combining units with different profiles is not supported.");
746 /* We allow some units to have profile and other to not have one. This will
747 just make unprofiled units to be size optimized that is sane. */
752 /* Input and merge the cgraph from each of the .o files passed to
753 lto1. */
755 void
756 input_cgraph (void)
758 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
759 struct lto_file_decl_data *file_data;
760 unsigned int j = 0;
761 struct cgraph_node *node;
763 while ((file_data = file_data_vec[j++]))
765 const char *data;
766 size_t len;
767 struct lto_input_block *ib;
769 ib = lto_create_simple_input_block (file_data, LTO_section_cgraph,
770 &data, &len);
771 input_profile_summary (ib);
772 file_data->cgraph_node_encoder = lto_cgraph_encoder_new ();
773 input_cgraph_1 (file_data, ib);
774 lto_destroy_simple_input_block (file_data, LTO_section_cgraph,
775 ib, data, len);
777 /* Assume that every file read needs to be processed by LTRANS. */
778 if (flag_wpa)
779 lto_mark_file_for_ltrans (file_data);
782 /* Clear out the aux field that was used to store enough state to
783 tell which nodes should be overwritten. */
784 for (node = cgraph_nodes; node; node = node->next)
786 /* Some nodes may have been created by cgraph_node. This
787 happens when the callgraph contains nested functions. If the
788 node for the parent function was never emitted to the gimple
789 file, cgraph_node will create a node for it when setting the
790 context of the nested function. */
791 if (node->local.lto_file_data)
792 node->aux = NULL;