OSDN Git Service

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