OSDN Git Service

* lto-symtab.c (lto_symtab_entry_def) Add vnode.
[pf3gnuchains/gcc-fork.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 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 "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"
49
50 /* Create a new cgraph encoder.  */
51
52 lto_cgraph_encoder_t
53 lto_cgraph_encoder_new (void)
54 {
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;
59 }
60
61
62 /* Delete ENCODER and its components.  */
63
64 void
65 lto_cgraph_encoder_delete (lto_cgraph_encoder_t encoder)
66 {
67    VEC_free (cgraph_node_ptr, heap, encoder->nodes);
68    pointer_map_destroy (encoder->map);
69    free (encoder);
70 }
71
72
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.  */
76
77 int
78 lto_cgraph_encoder_encode (lto_cgraph_encoder_t encoder,
79                            struct cgraph_node *node)
80 {
81   int ref;
82   void **slot;
83
84   slot = pointer_map_contains (encoder->map, node);
85   if (!slot)
86     {
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);
91     }
92   else
93     ref = (int) (intptr_t) *slot;
94
95   return ref;
96 }
97
98
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.  */
101
102 int
103 lto_cgraph_encoder_lookup (lto_cgraph_encoder_t encoder,
104                            struct cgraph_node *node)
105 {
106   void **slot = pointer_map_contains (encoder->map, node);
107   return (slot ? (int) (intptr_t) *slot : LCC_NOT_FOUND);
108 }
109
110
111 /* Return the cgraph node corresponding to REF using ENCODER.  */
112
113 struct cgraph_node *
114 lto_cgraph_encoder_deref (lto_cgraph_encoder_t encoder, int ref)
115 {
116   if (ref == LCC_NOT_FOUND)
117     return NULL;
118
119   return VEC_index (cgraph_node_ptr, encoder->nodes, ref);
120 }
121
122
123 /* Return number of encoded nodes in ENCODER.  */
124
125 static int
126 lto_cgraph_encoder_size (lto_cgraph_encoder_t encoder)
127 {
128   return VEC_length (cgraph_node_ptr, encoder->nodes);
129 }
130
131
132 /* Output the cgraph EDGE to OB using ENCODER.  */
133
134 static void
135 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
136                  lto_cgraph_encoder_t encoder)
137 {
138   unsigned int uid;
139   intptr_t ref;
140   struct bitpack_d *bp;
141
142   if (edge->indirect_unknown_callee)
143     lto_output_uleb128_stream (ob->main_stream, LTO_cgraph_indirect_edge);
144   else
145     lto_output_uleb128_stream (ob->main_stream, LTO_cgraph_edge);
146
147   ref = lto_cgraph_encoder_lookup (encoder, edge->caller);
148   gcc_assert (ref != LCC_NOT_FOUND);
149   lto_output_sleb128_stream (ob->main_stream, ref);
150
151   if (!edge->indirect_unknown_callee)
152     {
153       ref = lto_cgraph_encoder_lookup (encoder, edge->callee);
154       gcc_assert (ref != LCC_NOT_FOUND);
155       lto_output_sleb128_stream (ob->main_stream, ref);
156     }
157
158   lto_output_sleb128_stream (ob->main_stream, edge->count);
159
160   bp = bitpack_create ();
161   uid = flag_wpa ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt);
162   bp_pack_value (bp, uid, HOST_BITS_PER_INT);
163   bp_pack_value (bp, edge->inline_failed, HOST_BITS_PER_INT);
164   bp_pack_value (bp, edge->frequency, HOST_BITS_PER_INT);
165   bp_pack_value (bp, edge->loop_nest, 30);
166   bp_pack_value (bp, edge->indirect_inlining_edge, 1);
167   bp_pack_value (bp, edge->call_stmt_cannot_inline_p, 1);
168   bp_pack_value (bp, edge->can_throw_external, 1);
169   lto_output_bitpack (ob->main_stream, bp);
170   bitpack_delete (bp);
171 }
172
173 /* Return true when node is reachable from other partition.  */
174
175 static bool
176 reachable_from_other_partition_p (struct cgraph_node *node, cgraph_node_set set)
177 {
178   struct cgraph_edge *e;
179   if (node->needed)
180     return true;
181   if (!node->analyzed)
182     return false;
183   if (node->global.inlined_to)
184     return false;
185   for (e = node->callers; e; e = e->next_caller)
186     if (!cgraph_node_in_set_p (e->caller, set))
187       return true;
188   return false;
189 }
190
191 /* Output the cgraph NODE to OB.  ENCODER is used to find the
192    reference number of NODE->inlined_to.  SET is the set of nodes we
193    are writing to the current file.  If NODE is not in SET, then NODE
194    is a boundary of a cgraph_node_set and we pretend NODE just has a
195    decl and no callees.  WRITTEN_DECLS is the set of FUNCTION_DECLs
196    that have had their callgraph node written so far.  This is used to
197    determine if NODE is a clone of a previously written node.  */
198
199 static void
200 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
201                  lto_cgraph_encoder_t encoder, cgraph_node_set set,
202                  bitmap written_decls)
203 {
204   unsigned int tag;
205   struct bitpack_d *bp;
206   unsigned local, externally_visible, inlinable, analyzed;
207   bool boundary_p, wrote_decl_p;
208   intptr_t ref;
209   bool in_other_partition = false;
210
211   boundary_p = !cgraph_node_in_set_p (node, set);
212   wrote_decl_p = bitmap_bit_p (written_decls, DECL_UID (node->decl));
213
214   switch (cgraph_function_body_availability (node))
215     {
216     case AVAIL_NOT_AVAILABLE:
217       tag = LTO_cgraph_unavail_node;
218       break;
219
220     case AVAIL_AVAILABLE:
221     case AVAIL_LOCAL:
222       tag = LTO_cgraph_avail_node;
223       break;
224
225     case AVAIL_OVERWRITABLE:
226       tag = LTO_cgraph_overwritable_node;
227       break;
228
229     default:
230       gcc_unreachable ();
231     }
232
233   if (boundary_p)
234     tag = LTO_cgraph_unavail_node;
235
236   lto_output_uleb128_stream (ob->main_stream, tag);
237
238   local = node->local.local;
239   externally_visible = node->local.externally_visible;
240   inlinable = node->local.inlinable;
241   analyzed = node->analyzed;
242
243   /* In WPA mode, we only output part of the call-graph.  Also, we
244      fake cgraph node attributes.  There are two cases that we care.
245
246      Boundary nodes: There are nodes that are not part of SET but are
247      called from within SET.  We artificially make them look like
248      externally visible nodes with no function body.
249
250      Cherry-picked nodes:  These are nodes we pulled from other
251      translation units into SET during IPA-inlining.  We make them as
252      local static nodes to prevent clashes with other local statics.  */
253   if (boundary_p)
254     {
255       /* Inline clones can not be part of boundary.  
256          gcc_assert (!node->global.inlined_to);  
257
258          FIXME: At the moment they can be, when partition contains an inline
259          clone that is clone of inline clone from outside partition.  We can
260          reshape the clone tree and make other tree to be the root, but it
261          needs a bit extra work and will be promplty done by cgraph_remove_node
262          after reading back.  */
263       in_other_partition = 1;
264       analyzed = 0;
265     }
266
267   lto_output_uleb128_stream (ob->main_stream, wrote_decl_p);
268
269   if (!wrote_decl_p)
270     bitmap_set_bit (written_decls, DECL_UID (node->decl));
271
272   lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
273   lto_output_sleb128_stream (ob->main_stream, node->count);
274
275   bp = bitpack_create ();
276   bp_pack_value (bp, local, 1);
277   bp_pack_value (bp, externally_visible, 1);
278   bp_pack_value (bp, node->local.finalized, 1);
279   bp_pack_value (bp, inlinable, 1);
280   bp_pack_value (bp, node->local.disregard_inline_limits, 1);
281   bp_pack_value (bp, node->local.redefined_extern_inline, 1);
282   bp_pack_value (bp, node->local.for_functions_valid, 1);
283   bp_pack_value (bp, node->local.vtable_method, 1);
284   bp_pack_value (bp, node->needed, 1);
285   bp_pack_value (bp, node->address_taken, 1);
286   bp_pack_value (bp, node->abstract_and_needed, 1);
287   bp_pack_value (bp, node->reachable, 1);
288   bp_pack_value (bp, analyzed && reachable_from_other_partition_p (node, set), 1);
289   bp_pack_value (bp, node->lowered, 1);
290   bp_pack_value (bp, analyzed, 1);
291   bp_pack_value (bp, in_other_partition, 1);
292   bp_pack_value (bp, node->process, 1);
293   bp_pack_value (bp, node->alias, 1);
294   bp_pack_value (bp, node->finalized_by_frontend, 1);
295   bp_pack_value (bp, node->frequency, 2);
296   lto_output_bitpack (ob->main_stream, bp);
297   bitpack_delete (bp);
298
299   if (tag != LTO_cgraph_unavail_node)
300     {
301       lto_output_sleb128_stream (ob->main_stream,
302                                  node->local.inline_summary.estimated_self_stack_size);
303       lto_output_sleb128_stream (ob->main_stream,
304                                  node->local.inline_summary.self_size);
305       lto_output_sleb128_stream (ob->main_stream,
306                                  node->local.inline_summary.size_inlining_benefit);
307       lto_output_sleb128_stream (ob->main_stream,
308                                  node->local.inline_summary.self_time);
309       lto_output_sleb128_stream (ob->main_stream,
310                                  node->local.inline_summary.time_inlining_benefit);
311     }
312
313   /* FIXME lto: Outputting global info is not neccesary until after
314      inliner was run.  Global structure holds results of propagation
315      done by inliner.  */
316   lto_output_sleb128_stream (ob->main_stream,
317                              node->global.estimated_stack_size);
318   lto_output_sleb128_stream (ob->main_stream,
319                              node->global.stack_frame_offset);
320   if (node->global.inlined_to && !boundary_p)
321     {
322       ref = lto_cgraph_encoder_lookup (encoder, node->global.inlined_to);
323       gcc_assert (ref != LCC_NOT_FOUND);
324     }
325   else
326     ref = LCC_NOT_FOUND;
327   lto_output_sleb128_stream (ob->main_stream, ref);
328
329   lto_output_sleb128_stream (ob->main_stream, node->global.time);
330   lto_output_sleb128_stream (ob->main_stream, node->global.size);
331   lto_output_sleb128_stream (ob->main_stream,
332                              node->global.estimated_growth);
333   lto_output_uleb128_stream (ob->main_stream, node->global.inlined);
334   if (node->same_comdat_group && !boundary_p)
335     {
336       ref = lto_cgraph_encoder_lookup (encoder, node->same_comdat_group);
337       gcc_assert (ref != LCC_NOT_FOUND);
338     }
339   else
340     ref = LCC_NOT_FOUND;
341   lto_output_sleb128_stream (ob->main_stream, ref);
342
343   if (node->same_body)
344     {
345       struct cgraph_node *alias;
346       unsigned long alias_count = 1;
347       for (alias = node->same_body; alias->next; alias = alias->next)
348         alias_count++;
349       lto_output_uleb128_stream (ob->main_stream, alias_count);
350       do
351         {
352           lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
353                                     alias->decl);
354           if (alias->thunk.thunk_p)
355             {
356               lto_output_uleb128_stream
357                  (ob->main_stream,
358                   1 + (alias->thunk.this_adjusting != 0) * 2
359                   + (alias->thunk.virtual_offset_p != 0) * 4);
360               lto_output_uleb128_stream (ob->main_stream,
361                                          alias->thunk.fixed_offset);
362               lto_output_uleb128_stream (ob->main_stream,
363                                          alias->thunk.virtual_value);
364               lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
365                                         alias->thunk.alias);
366             }
367           else
368             {
369               lto_output_uleb128_stream (ob->main_stream, 0);
370               lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
371                                         alias->thunk.alias);
372             }
373           alias = alias->previous;
374         }
375       while (alias);
376     }
377   else
378     lto_output_uleb128_stream (ob->main_stream, 0);
379 }
380
381 /* Output the varpool NODE to OB. 
382    If NODE is not in SET, then NODE is a boundary.  */
383
384 static void
385 lto_output_varpool_node (struct lto_simple_output_block *ob, struct varpool_node *node,
386                          varpool_node_set set)
387 {
388   bool boundary_p = !varpool_node_in_set_p (node, set) && node->analyzed;
389   struct bitpack_d *bp;
390   struct varpool_node *alias;
391   int count = 0;
392
393   lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
394   bp = bitpack_create ();
395   bp_pack_value (bp, node->externally_visible, 1);
396   bp_pack_value (bp, node->force_output, 1);
397   bp_pack_value (bp, node->finalized, 1);
398   gcc_assert (node->finalized || !node->analyzed);
399   gcc_assert (node->needed);
400   gcc_assert (!node->alias);
401   /* FIXME: We have no idea how we move references around.  For moment assume that
402      everything is used externally.  */
403   bp_pack_value (bp, flag_wpa, 1);  /* used_from_other_parition.  */
404   bp_pack_value (bp, boundary_p, 1);  /* in_other_partition.  */
405   /* Also emit any extra name aliases.  */
406   for (alias = node->extra_name; alias; alias = alias->next)
407     count++;
408   bp_pack_value (bp, count != 0, 1);
409   lto_output_bitpack (ob->main_stream, bp);
410   bitpack_delete (bp);
411
412   if (count)
413     {
414       lto_output_uleb128_stream (ob->main_stream, count);
415       for (alias = node->extra_name; alias; alias = alias->next)
416         lto_output_var_decl_index (ob->decl_state, ob->main_stream, alias->decl);
417     }
418 }
419
420 /* Stream out profile_summary to OB.  */
421
422 static void
423 output_profile_summary (struct lto_simple_output_block *ob)
424 {
425   if (profile_info)
426     {
427       /* We do not output num, it is not terribly useful.  */
428       gcc_assert (profile_info->runs);
429       lto_output_uleb128_stream (ob->main_stream, profile_info->runs);
430       lto_output_sleb128_stream (ob->main_stream, profile_info->sum_all);
431       lto_output_sleb128_stream (ob->main_stream, profile_info->run_max);
432       lto_output_sleb128_stream (ob->main_stream, profile_info->sum_max);
433     }
434   else
435     lto_output_uleb128_stream (ob->main_stream, 0);
436 }
437
438 /* Add NODE into encoder as well as nodes it is cloned from.
439    Do it in a way so clones appear first.  */
440 static void
441 add_node_to (lto_cgraph_encoder_t encoder, struct cgraph_node *node)
442 {
443   if (node->clone_of)
444     add_node_to (encoder, node->clone_of);
445   lto_cgraph_encoder_encode (encoder, node);
446 }
447
448 /* Output all callees or indirect outgoing edges.  EDGE must be the first such
449    edge.  */
450
451 static void
452 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
453                               struct lto_simple_output_block *ob,
454                               lto_cgraph_encoder_t encoder)
455 {
456   if (!edge)
457     return;
458
459   /* Output edges in backward direction, so the reconstructed callgraph match
460      and it is easy to associate call sites in the IPA pass summaries.  */
461   while (edge->next_callee)
462     edge = edge->next_callee;
463   for (; edge; edge = edge->prev_callee)
464     lto_output_edge (ob, edge, encoder);
465 }
466
467 /* Output the part of the cgraph in SET.  */
468
469 void
470 output_cgraph (cgraph_node_set set)
471 {
472   struct cgraph_node *node;
473   struct lto_simple_output_block *ob;
474   cgraph_node_set_iterator csi;
475   struct cgraph_edge *edge;
476   int i, n_nodes;
477   bitmap written_decls;
478   lto_cgraph_encoder_t encoder;
479   struct cgraph_asm_node *can;
480
481   ob = lto_create_simple_output_block (LTO_section_cgraph);
482
483   output_profile_summary (ob);
484
485   /* An encoder for cgraph nodes should have been created by
486      ipa_write_summaries_1.  */
487   gcc_assert (ob->decl_state->cgraph_node_encoder);
488   encoder = ob->decl_state->cgraph_node_encoder;
489
490   /* The FUNCTION_DECLs for which we have written a node.  The first
491      node found is written as the "original" node, the remaining nodes
492      are considered its clones.  */
493   written_decls = lto_bitmap_alloc ();
494
495   /* Go over all the nodes in SET and assign references.  */
496   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
497     {
498       node = csi_node (csi);
499       add_node_to (encoder, node);
500     }
501
502   /* Go over all the nodes again to include callees that are not in
503      SET.  */
504   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
505     {
506       node = csi_node (csi);
507       for (edge = node->callees; edge; edge = edge->next_callee)
508         {
509           struct cgraph_node *callee = edge->callee;
510           if (!cgraph_node_in_set_p (callee, set))
511             {
512               /* We should have moved all the inlines.  */
513               gcc_assert (!callee->global.inlined_to);
514               add_node_to (encoder, callee);
515             }
516         }
517     }
518
519   /* Write out the nodes.  We must first output a node and then its clones,
520      otherwise at a time reading back the node there would be nothing to clone
521      from.  */
522   n_nodes = lto_cgraph_encoder_size (encoder);
523   for (i = 0; i < n_nodes; i++)
524     {
525       node = lto_cgraph_encoder_deref (encoder, i);
526       lto_output_node (ob, node, encoder, set, written_decls);
527     }
528
529   lto_bitmap_free (written_decls);
530
531   /* Go over the nodes in SET again to write edges.  */
532   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
533     {
534       node = csi_node (csi);
535       output_outgoing_cgraph_edges (node->callees, ob, encoder);
536       output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
537     }
538
539   lto_output_uleb128_stream (ob->main_stream, 0);
540
541   /* Emit toplevel asms.  */
542   for (can = cgraph_asm_nodes; can; can = can->next)
543     {
544       int len = TREE_STRING_LENGTH (can->asm_str);
545       lto_output_uleb128_stream (ob->main_stream, len);
546       for (i = 0; i < len; ++i)
547         lto_output_1_stream (ob->main_stream,
548                              TREE_STRING_POINTER (can->asm_str)[i]);
549     }
550
551   lto_output_uleb128_stream (ob->main_stream, 0);
552
553   lto_destroy_simple_output_block (ob);
554 }
555
556 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
557    STACK_SIZE, SELF_TIME and SELF_SIZE.  This is called either to initialize
558    NODE or to replace the values in it, for instance because the first
559    time we saw it, the function body was not available but now it
560    is.  BP is a bitpack with all the bitflags for NODE read from the
561    stream.  */
562
563 static void
564 input_overwrite_node (struct lto_file_decl_data *file_data,
565                       struct cgraph_node *node,
566                       enum LTO_cgraph_tags tag,
567                       struct bitpack_d *bp,
568                       unsigned int stack_size,
569                       unsigned int self_time,
570                       unsigned int time_inlining_benefit,
571                       unsigned int self_size,
572                       unsigned int size_inlining_benefit)
573 {
574   node->aux = (void *) tag;
575   node->local.inline_summary.estimated_self_stack_size = stack_size;
576   node->local.inline_summary.self_time = self_time;
577   node->local.inline_summary.time_inlining_benefit = time_inlining_benefit;
578   node->local.inline_summary.self_size = self_size;
579   node->local.inline_summary.size_inlining_benefit = size_inlining_benefit;
580   node->global.time = self_time;
581   node->global.size = self_size;
582   node->local.lto_file_data = file_data;
583
584   node->local.local = bp_unpack_value (bp, 1);
585   node->local.externally_visible = bp_unpack_value (bp, 1);
586   node->local.finalized = bp_unpack_value (bp, 1);
587   node->local.inlinable = bp_unpack_value (bp, 1);
588   node->local.disregard_inline_limits = bp_unpack_value (bp, 1);
589   node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
590   node->local.for_functions_valid = bp_unpack_value (bp, 1);
591   node->local.vtable_method = bp_unpack_value (bp, 1);
592   node->needed = bp_unpack_value (bp, 1);
593   node->address_taken = bp_unpack_value (bp, 1);
594   node->abstract_and_needed = bp_unpack_value (bp, 1);
595   node->reachable = bp_unpack_value (bp, 1);
596   node->reachable_from_other_partition = bp_unpack_value (bp, 1);
597   node->lowered = bp_unpack_value (bp, 1);
598   node->analyzed = bp_unpack_value (bp, 1);
599   node->in_other_partition = bp_unpack_value (bp, 1);
600   node->process = bp_unpack_value (bp, 1);
601   node->alias = bp_unpack_value (bp, 1);
602   node->finalized_by_frontend = bp_unpack_value (bp, 1);
603   node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
604 }
605
606 /* Output the part of the cgraph in SET.  */
607
608 void
609 output_varpool (varpool_node_set set)
610 {
611   struct varpool_node *node;
612   struct lto_simple_output_block *ob;
613   int len = 0;
614
615   ob = lto_create_simple_output_block (LTO_section_varpool);
616
617   for (node = varpool_nodes; node; node = node->next)
618     if (node->needed && node->analyzed)
619       len++;
620
621   lto_output_uleb128_stream (ob->main_stream, len);
622
623   /* Write out the nodes.  We must first output a node and then its clones,
624      otherwise at a time reading back the node there would be nothing to clone
625      from.  */
626   for (node = varpool_nodes; node; node = node->next)
627     if (node->needed && node->analyzed)
628       lto_output_varpool_node (ob, node, set);
629
630   lto_destroy_simple_output_block (ob);
631 }
632
633 /* Read a node from input_block IB.  TAG is the node's tag just read.
634    Return the node read or overwriten.  */
635
636 static struct cgraph_node *
637 input_node (struct lto_file_decl_data *file_data,
638             struct lto_input_block *ib,
639             enum LTO_cgraph_tags tag)
640 {
641   tree fn_decl;
642   struct cgraph_node *node;
643   struct bitpack_d *bp;
644   int stack_size = 0;
645   unsigned decl_index;
646   bool clone_p;
647   int estimated_stack_size = 0;
648   int stack_frame_offset = 0;
649   int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
650   int estimated_growth = 0;
651   int time = 0;
652   int size = 0;
653   int self_time = 0;
654   int self_size = 0;
655   int time_inlining_benefit = 0;
656   int size_inlining_benefit = 0;
657   unsigned long same_body_count = 0;
658   bool inlined = false;
659
660   clone_p = (lto_input_uleb128 (ib) != 0);
661
662   decl_index = lto_input_uleb128 (ib);
663   fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
664
665   if (clone_p)
666     node = cgraph_clone_node (cgraph_node (fn_decl), 0,
667                               CGRAPH_FREQ_BASE, 0, false, NULL);
668
669   else
670     node = cgraph_node (fn_decl);
671
672   node->count = lto_input_sleb128 (ib);
673   bp = lto_input_bitpack (ib);
674
675   if (tag != LTO_cgraph_unavail_node)
676     {
677       stack_size = lto_input_sleb128 (ib);
678       self_size = lto_input_sleb128 (ib);
679       size_inlining_benefit = lto_input_sleb128 (ib);
680       self_time = lto_input_sleb128 (ib);
681       time_inlining_benefit = lto_input_sleb128 (ib);
682     }
683
684   estimated_stack_size = lto_input_sleb128 (ib);
685   stack_frame_offset = lto_input_sleb128 (ib);
686   ref = lto_input_sleb128 (ib);
687   time = lto_input_sleb128 (ib);
688   size = lto_input_sleb128 (ib);
689   estimated_growth = lto_input_sleb128 (ib);
690   inlined = lto_input_uleb128 (ib);
691   ref2 = lto_input_sleb128 (ib);
692   same_body_count = lto_input_uleb128 (ib);
693
694   /* Make sure that we have not read this node before.  Nodes that
695      have already been read will have their tag stored in the 'aux'
696      field.  Since built-in functions can be referenced in multiple
697      functions, they are expected to be read more than once.  */
698   if (node->aux && !DECL_IS_BUILTIN (node->decl))
699     internal_error ("bytecode stream: found multiple instances of cgraph "
700                     "node %d", node->uid);
701
702   input_overwrite_node (file_data, node, tag, bp, stack_size, self_time,
703                         time_inlining_benefit, self_size,
704                         size_inlining_benefit);
705   bitpack_delete (bp);
706
707   node->global.estimated_stack_size = estimated_stack_size;
708   node->global.stack_frame_offset = stack_frame_offset;
709   node->global.time = time;
710   node->global.size = size;
711
712   /* Store a reference for now, and fix up later to be a pointer.  */
713   node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
714
715   node->global.estimated_growth = estimated_growth;
716   node->global.inlined = inlined;
717
718   /* Store a reference for now, and fix up later to be a pointer.  */
719   node->same_comdat_group = (cgraph_node_ptr) (intptr_t) ref2;
720
721   while (same_body_count-- > 0)
722     {
723       tree alias_decl;
724       int type;
725       decl_index = lto_input_uleb128 (ib);
726       alias_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
727       type = lto_input_uleb128 (ib);
728       if (!type)
729         {
730           tree real_alias;
731           decl_index = lto_input_uleb128 (ib);
732           real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
733           cgraph_same_body_alias (alias_decl, real_alias);
734         }
735       else
736         {
737           HOST_WIDE_INT fixed_offset = lto_input_uleb128 (ib);
738           HOST_WIDE_INT virtual_value = lto_input_uleb128 (ib);
739           tree real_alias;
740           decl_index = lto_input_uleb128 (ib);
741           real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
742           cgraph_add_thunk (alias_decl, fn_decl, type & 2, fixed_offset,
743                             virtual_value,
744                             (type & 4) ? size_int (virtual_value) : NULL_TREE,
745                             real_alias);
746         }
747     }
748   return node;
749 }
750
751 /* Read a node from input_block IB.  TAG is the node's tag just read.
752    Return the node read or overwriten.  */
753
754 static struct varpool_node *
755 input_varpool_node (struct lto_file_decl_data *file_data,
756                     struct lto_input_block *ib)
757 {
758   int decl_index;
759   tree var_decl;
760   struct varpool_node *node;
761   struct bitpack_d *bp;
762   bool aliases_p;
763   int count;
764
765   decl_index = lto_input_uleb128 (ib);
766   var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
767   node = varpool_node (var_decl);
768
769   bp = lto_input_bitpack (ib);
770   node->externally_visible = bp_unpack_value (bp, 1);
771   node->force_output = bp_unpack_value (bp, 1);
772   node->finalized = bp_unpack_value (bp, 1);
773   node->analyzed = 1; 
774   node->used_from_other_partition = bp_unpack_value (bp, 1);
775   node->in_other_partition = bp_unpack_value (bp, 1);
776   aliases_p = bp_unpack_value (bp, 1);
777   if (node->finalized)
778     varpool_mark_needed_node (node);
779   bitpack_delete (bp);
780   if (aliases_p)
781     {
782       count = lto_input_uleb128 (ib);
783       for (; count > 0; count --)
784         {
785           tree decl = lto_file_decl_data_get_var_decl (file_data,
786                                                        lto_input_uleb128 (ib));
787           varpool_extra_name_alias (decl, var_decl);
788         }
789     }
790   return node;
791 }
792
793
794 /* Read an edge from IB.  NODES points to a vector of previously read nodes for
795    decoding caller and callee of the edge to be read.  If INDIRECT is true, the
796    edge being read is indirect (in the sense that it has
797    indirect_unknown_callee set).  */
798
799 static void
800 input_edge (struct lto_input_block *ib, VEC(cgraph_node_ptr, heap) *nodes,
801             bool indirect)
802 {
803   struct cgraph_node *caller, *callee;
804   struct cgraph_edge *edge;
805   unsigned int stmt_id;
806   gcov_type count;
807   int freq;
808   unsigned int nest;
809   cgraph_inline_failed_t inline_failed;
810   struct bitpack_d *bp;
811   enum ld_plugin_symbol_resolution caller_resolution;
812
813   caller = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
814   if (caller == NULL || caller->decl == NULL_TREE)
815     internal_error ("bytecode stream: no caller found while reading edge");
816
817   if (!indirect)
818     {
819       callee = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
820       if (callee == NULL || callee->decl == NULL_TREE)
821         internal_error ("bytecode stream: no callee found while reading edge");
822     }
823   else
824     callee = NULL;
825
826   count = (gcov_type) lto_input_sleb128 (ib);
827
828   bp = lto_input_bitpack (ib);
829   stmt_id = (unsigned int) bp_unpack_value (bp, HOST_BITS_PER_INT);
830   inline_failed = (cgraph_inline_failed_t) bp_unpack_value (bp,
831                                                             HOST_BITS_PER_INT);
832   freq = (int) bp_unpack_value (bp, HOST_BITS_PER_INT);
833   nest = (unsigned) bp_unpack_value (bp, 30);
834
835   /* If the caller was preempted, don't create the edge.
836      ???  Should we ever have edges from a preempted caller?  */
837   caller_resolution = lto_symtab_get_resolution (caller->decl);
838   if (caller_resolution == LDPR_PREEMPTED_REG
839       || caller_resolution == LDPR_PREEMPTED_IR)
840     return;
841
842   if (indirect)
843     edge = cgraph_create_indirect_edge (caller, NULL, count, freq, nest);
844   else
845     edge = cgraph_create_edge (caller, callee, NULL, count, freq, nest);
846
847   edge->indirect_inlining_edge = bp_unpack_value (bp, 1);
848   edge->lto_stmt_uid = stmt_id;
849   edge->inline_failed = inline_failed;
850   edge->call_stmt_cannot_inline_p = bp_unpack_value (bp, 1);
851   edge->can_throw_external = bp_unpack_value (bp, 1);
852   bitpack_delete (bp);
853 }
854
855
856 /* Read a cgraph from IB using the info in FILE_DATA.  */
857
858 static void
859 input_cgraph_1 (struct lto_file_decl_data *file_data,
860                 struct lto_input_block *ib)
861 {
862   enum LTO_cgraph_tags tag;
863   VEC(cgraph_node_ptr, heap) *nodes = NULL;
864   struct cgraph_node *node;
865   unsigned i;
866   unsigned HOST_WIDE_INT len;
867
868   tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
869   while (tag)
870     {
871       if (tag == LTO_cgraph_edge)
872         input_edge (ib, nodes, false);
873       else if (tag == LTO_cgraph_indirect_edge)
874         input_edge (ib, nodes, true);
875       else
876         {
877           node = input_node (file_data, ib, tag);
878           if (node == NULL || node->decl == NULL_TREE)
879             internal_error ("bytecode stream: found empty cgraph node");
880           VEC_safe_push (cgraph_node_ptr, heap, nodes, node);
881           lto_cgraph_encoder_encode (file_data->cgraph_node_encoder, node);
882         }
883
884       tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
885     }
886
887   /* Input toplevel asms.  */
888   len = lto_input_uleb128 (ib);
889   while (len)
890     {
891       char *str = (char *)xmalloc (len + 1);
892       for (i = 0; i < len; ++i)
893         str[i] = lto_input_1_unsigned (ib);
894       cgraph_add_asm_node (build_string (len, str));
895       free (str);
896
897       len = lto_input_uleb128 (ib);
898     }
899
900   for (i = 0; VEC_iterate (cgraph_node_ptr, nodes, i, node); i++)
901     {
902       int ref = (int) (intptr_t) node->global.inlined_to;
903
904       /* Fixup inlined_to from reference to pointer.  */
905       if (ref != LCC_NOT_FOUND)
906         node->global.inlined_to = VEC_index (cgraph_node_ptr, nodes, ref);
907       else
908         node->global.inlined_to = NULL;
909
910       ref = (int) (intptr_t) node->same_comdat_group;
911
912       /* Fixup same_comdat_group from reference to pointer.  */
913       if (ref != LCC_NOT_FOUND)
914         node->same_comdat_group = VEC_index (cgraph_node_ptr, nodes, ref);
915       else
916         node->same_comdat_group = NULL;
917     }
918
919   VEC_free (cgraph_node_ptr, heap, nodes);
920 }
921
922 /* Read a varpool from IB using the info in FILE_DATA.  */
923
924 static void
925 input_varpool_1 (struct lto_file_decl_data *file_data,
926                 struct lto_input_block *ib)
927 {
928   unsigned HOST_WIDE_INT len;
929
930   len = lto_input_uleb128 (ib);
931   while (len)
932     {
933       input_varpool_node (file_data, ib);
934       len--;
935     }
936 }
937
938 static struct gcov_ctr_summary lto_gcov_summary;
939
940 /* Input profile_info from IB.  */
941 static void
942 input_profile_summary (struct lto_input_block *ib)
943 {
944   unsigned int runs = lto_input_uleb128 (ib);
945   if (runs)
946     {
947       if (!profile_info)
948         {
949           profile_info = &lto_gcov_summary;
950           lto_gcov_summary.runs = runs;
951           lto_gcov_summary.sum_all = lto_input_sleb128 (ib);
952           lto_gcov_summary.run_max = lto_input_sleb128 (ib);
953           lto_gcov_summary.sum_max = lto_input_sleb128 (ib);
954         }
955       /* We can support this by scaling all counts to nearest common multiple
956          of all different runs, but it is perhaps not worth the effort.  */
957       else if (profile_info->runs != runs
958                || profile_info->sum_all != lto_input_sleb128 (ib)
959                || profile_info->run_max != lto_input_sleb128 (ib)
960                || profile_info->sum_max != lto_input_sleb128 (ib))
961         sorry ("Combining units with different profiles is not supported.");
962       /* We allow some units to have profile and other to not have one.  This will
963          just make unprofiled units to be size optimized that is sane.  */
964     }
965
966 }
967
968 /* Input and merge the cgraph from each of the .o files passed to
969    lto1.  */
970
971 void
972 input_cgraph (void)
973 {
974   struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
975   struct lto_file_decl_data *file_data;
976   unsigned int j = 0;
977   struct cgraph_node *node;
978
979   while ((file_data = file_data_vec[j++]))
980     {
981       const char *data;
982       size_t len;
983       struct lto_input_block *ib;
984
985       ib = lto_create_simple_input_block (file_data, LTO_section_cgraph,
986                                           &data, &len);
987       input_profile_summary (ib);
988       file_data->cgraph_node_encoder = lto_cgraph_encoder_new ();
989       input_cgraph_1 (file_data, ib);
990       lto_destroy_simple_input_block (file_data, LTO_section_cgraph,
991                                       ib, data, len);
992
993       ib = lto_create_simple_input_block (file_data, LTO_section_varpool,
994                                           &data, &len);
995       input_varpool_1 (file_data, ib);
996       lto_destroy_simple_input_block (file_data, LTO_section_varpool,
997                                       ib, data, len);
998
999       /* Assume that every file read needs to be processed by LTRANS.  */
1000       if (flag_wpa)
1001         lto_mark_file_for_ltrans (file_data);
1002     }
1003
1004   /* Clear out the aux field that was used to store enough state to
1005      tell which nodes should be overwritten.  */
1006   for (node = cgraph_nodes; node; node = node->next)
1007     {
1008       /* Some nodes may have been created by cgraph_node.  This
1009          happens when the callgraph contains nested functions.  If the
1010          node for the parent function was never emitted to the gimple
1011          file, cgraph_node will create a node for it when setting the
1012          context of the nested function.  */
1013       if (node->local.lto_file_data)
1014         node->aux = NULL;
1015     }
1016 }