OSDN Git Service

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