OSDN Git Service

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