OSDN Git Service

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