OSDN Git Service

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