OSDN Git Service

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