OSDN Git Service

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