OSDN Git Service

* c-common.c (c_common_reswords): Add _Static_assert for C.
[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 Free Software Foundation, Inc.
5    Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "toplev.h"
28 #include "tree.h"
29 #include "expr.h"
30 #include "flags.h"
31 #include "params.h"
32 #include "input.h"
33 #include "hashtab.h"
34 #include "langhooks.h"
35 #include "basic-block.h"
36 #include "tree-flow.h"
37 #include "cgraph.h"
38 #include "function.h"
39 #include "ggc.h"
40 #include "diagnostic.h"
41 #include "except.h"
42 #include "vec.h"
43 #include "timevar.h"
44 #include "output.h"
45 #include "pointer-set.h"
46 #include "lto-streamer.h"
47 #include "gcov-io.h"
48
49 static void output_varpool (cgraph_node_set, varpool_node_set);
50
51 /* Cgraph streaming is organized as set of record whose type
52    is indicated by a tag.  */
53 enum LTO_cgraph_tags
54 {
55   /* Must leave 0 for the stopper.  */
56
57   /* Cgraph node without body available.  */
58   LTO_cgraph_unavail_node = 1,
59   /* Cgraph node with function body.  */
60   LTO_cgraph_analyzed_node,
61   /* Cgraph edges.  */
62   LTO_cgraph_edge,
63   LTO_cgraph_indirect_edge
64 };
65
66 /* Create a new cgraph encoder.  */
67
68 lto_cgraph_encoder_t
69 lto_cgraph_encoder_new (void)
70 {
71   lto_cgraph_encoder_t encoder = XCNEW (struct lto_cgraph_encoder_d);
72   encoder->map = pointer_map_create ();
73   encoder->nodes = NULL;
74   return encoder;
75 }
76
77
78 /* Delete ENCODER and its components.  */
79
80 void
81 lto_cgraph_encoder_delete (lto_cgraph_encoder_t encoder)
82 {
83    VEC_free (cgraph_node_ptr, heap, encoder->nodes);
84    pointer_map_destroy (encoder->map);
85    free (encoder);
86 }
87
88
89 /* Return the existing reference number of NODE in the cgraph encoder in
90    output block OB.  Assign a new reference if this is the first time
91    NODE is encoded.  */
92
93 int
94 lto_cgraph_encoder_encode (lto_cgraph_encoder_t encoder,
95                            struct cgraph_node *node)
96 {
97   int ref;
98   void **slot;
99
100   slot = pointer_map_contains (encoder->map, node);
101   if (!slot)
102     {
103       ref = VEC_length (cgraph_node_ptr, encoder->nodes);
104       slot = pointer_map_insert (encoder->map, node);
105       *slot = (void *) (intptr_t) ref;
106       VEC_safe_push (cgraph_node_ptr, heap, encoder->nodes, node);
107     }
108   else
109     ref = (int) (intptr_t) *slot;
110
111   return ref;
112 }
113
114 #define LCC_NOT_FOUND   (-1)
115
116 /* Look up NODE in encoder.  Return NODE's reference if it has been encoded
117    or LCC_NOT_FOUND if it is not there.  */
118
119 int
120 lto_cgraph_encoder_lookup (lto_cgraph_encoder_t encoder,
121                            struct cgraph_node *node)
122 {
123   void **slot = pointer_map_contains (encoder->map, node);
124   return (slot ? (int) (intptr_t) *slot : LCC_NOT_FOUND);
125 }
126
127
128 /* Return the cgraph node corresponding to REF using ENCODER.  */
129
130 struct cgraph_node *
131 lto_cgraph_encoder_deref (lto_cgraph_encoder_t encoder, int ref)
132 {
133   if (ref == LCC_NOT_FOUND)
134     return NULL;
135
136   return VEC_index (cgraph_node_ptr, encoder->nodes, ref);
137 }
138
139
140 /* Return number of encoded nodes in ENCODER.  */
141
142 static int
143 lto_cgraph_encoder_size (lto_cgraph_encoder_t encoder)
144 {
145   return VEC_length (cgraph_node_ptr, encoder->nodes);
146 }
147
148 /* Create a new varpool encoder.  */
149
150 lto_varpool_encoder_t
151 lto_varpool_encoder_new (void)
152 {
153   lto_varpool_encoder_t encoder = XCNEW (struct lto_varpool_encoder_d);
154   encoder->map = pointer_map_create ();
155   encoder->initializer = pointer_set_create ();
156   encoder->nodes = NULL;
157   return encoder;
158 }
159
160
161 /* Delete ENCODER and its components.  */
162
163 void
164 lto_varpool_encoder_delete (lto_varpool_encoder_t encoder)
165 {
166    VEC_free (varpool_node_ptr, heap, encoder->nodes);
167    pointer_map_destroy (encoder->map);
168    pointer_set_destroy (encoder->initializer);
169    free (encoder);
170 }
171
172
173 /* Return the existing reference number of NODE in the varpool encoder in
174    output block OB.  Assign a new reference if this is the first time
175    NODE is encoded.  */
176
177 int
178 lto_varpool_encoder_encode (lto_varpool_encoder_t encoder,
179                            struct varpool_node *node)
180 {
181   int ref;
182   void **slot;
183
184   slot = pointer_map_contains (encoder->map, node);
185   if (!slot)
186     {
187       ref = VEC_length (varpool_node_ptr, encoder->nodes);
188       slot = pointer_map_insert (encoder->map, node);
189       *slot = (void *) (intptr_t) ref;
190       VEC_safe_push (varpool_node_ptr, heap, encoder->nodes, node);
191     }
192   else
193     ref = (int) (intptr_t) *slot;
194
195   return ref;
196 }
197
198 /* Look up NODE in encoder.  Return NODE's reference if it has been encoded
199    or LCC_NOT_FOUND if it is not there.  */
200
201 int
202 lto_varpool_encoder_lookup (lto_varpool_encoder_t encoder,
203                            struct varpool_node *node)
204 {
205   void **slot = pointer_map_contains (encoder->map, node);
206   return (slot ? (int) (intptr_t) *slot : LCC_NOT_FOUND);
207 }
208
209
210 /* Return the varpool node corresponding to REF using ENCODER.  */
211
212 struct varpool_node *
213 lto_varpool_encoder_deref (lto_varpool_encoder_t encoder, int ref)
214 {
215   if (ref == LCC_NOT_FOUND)
216     return NULL;
217
218   return VEC_index (varpool_node_ptr, encoder->nodes, ref);
219 }
220
221
222 /* Return number of encoded nodes in ENCODER.  */
223
224 static int
225 lto_varpool_encoder_size (lto_varpool_encoder_t encoder)
226 {
227   return VEC_length (varpool_node_ptr, encoder->nodes);
228 }
229
230 /* Return TRUE if we should encode initializer of NODE (if any).  */
231
232 bool
233 lto_varpool_encoder_encode_initializer_p (lto_varpool_encoder_t encoder,
234                                           struct varpool_node *node)
235 {
236   return pointer_set_contains (encoder->initializer, node);
237 }
238
239 /* Return TRUE if we should encode initializer of NODE (if any).  */
240
241 static void
242 lto_set_varpool_encoder_encode_initializer (lto_varpool_encoder_t encoder,
243                                             struct varpool_node *node)
244 {
245   pointer_set_insert (encoder->initializer, node);
246 }
247
248 /* Output the cgraph EDGE to OB using ENCODER.  */
249
250 static void
251 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
252                  lto_cgraph_encoder_t encoder)
253 {
254   unsigned int uid;
255   intptr_t ref;
256   struct bitpack_d *bp;
257
258   if (edge->indirect_unknown_callee)
259     lto_output_uleb128_stream (ob->main_stream, LTO_cgraph_indirect_edge);
260   else
261     lto_output_uleb128_stream (ob->main_stream, LTO_cgraph_edge);
262
263   ref = lto_cgraph_encoder_lookup (encoder, edge->caller);
264   gcc_assert (ref != LCC_NOT_FOUND);
265   lto_output_sleb128_stream (ob->main_stream, ref);
266
267   if (!edge->indirect_unknown_callee)
268     {
269       ref = lto_cgraph_encoder_lookup (encoder, edge->callee);
270       gcc_assert (ref != LCC_NOT_FOUND);
271       lto_output_sleb128_stream (ob->main_stream, ref);
272     }
273
274   lto_output_sleb128_stream (ob->main_stream, edge->count);
275
276   bp = bitpack_create ();
277   uid = flag_wpa ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt);
278   bp_pack_value (bp, uid, HOST_BITS_PER_INT);
279   bp_pack_value (bp, edge->inline_failed, HOST_BITS_PER_INT);
280   bp_pack_value (bp, edge->frequency, HOST_BITS_PER_INT);
281   bp_pack_value (bp, edge->loop_nest, 30);
282   bp_pack_value (bp, edge->indirect_inlining_edge, 1);
283   bp_pack_value (bp, edge->call_stmt_cannot_inline_p, 1);
284   bp_pack_value (bp, edge->can_throw_external, 1);
285   if (edge->indirect_unknown_callee)
286     {
287       int flags = edge->indirect_info->ecf_flags;
288       bp_pack_value (bp, (flags & ECF_CONST) != 0, 1);
289       bp_pack_value (bp, (flags & ECF_PURE) != 0, 1);
290       bp_pack_value (bp, (flags & ECF_NORETURN) != 0, 1);
291       bp_pack_value (bp, (flags & ECF_MALLOC) != 0, 1);
292       bp_pack_value (bp, (flags & ECF_NOTHROW) != 0, 1);
293       bp_pack_value (bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
294       /* Flags that should not appear on indirect calls.  */
295       gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
296                              | ECF_MAY_BE_ALLOCA
297                              | ECF_SIBCALL
298                              | ECF_NOVOPS)));
299     }
300   lto_output_bitpack (ob->main_stream, bp);
301   bitpack_delete (bp);
302 }
303
304 /* Return if LIST contain references from other partitions.  */
305
306 bool
307 referenced_from_other_partition_p (struct ipa_ref_list *list, cgraph_node_set set,
308                                    varpool_node_set vset)
309 {
310   int i;
311   struct ipa_ref *ref;
312   for (i = 0; ipa_ref_list_refering_iterate (list, i, ref); i++)
313     {
314       if (ref->refering_type == IPA_REF_CGRAPH)
315         {
316           if (!cgraph_node_in_set_p (ipa_ref_refering_node (ref), set))
317             return true;
318         }
319       else
320         {
321           if (!varpool_node_in_set_p (ipa_ref_refering_varpool_node (ref),
322                                       vset))
323             return true;
324         }
325     }
326   return false;
327 }
328
329 /* Return true when node is reachable from other partition.  */
330
331 bool
332 reachable_from_other_partition_p (struct cgraph_node *node, cgraph_node_set set)
333 {
334   struct cgraph_edge *e;
335   if (!node->analyzed)
336     return false;
337   if (node->global.inlined_to)
338     return false;
339   for (e = node->callers; e; e = e->next_caller)
340     if (!cgraph_node_in_set_p (e->caller, set))
341       return true;
342   return false;
343 }
344
345 /* Return if LIST contain references from other partitions.  */
346
347 bool
348 referenced_from_this_partition_p (struct ipa_ref_list *list, cgraph_node_set set,
349                                   varpool_node_set vset)
350 {
351   int i;
352   struct ipa_ref *ref;
353   for (i = 0; ipa_ref_list_refering_iterate (list, i, ref); i++)
354     {
355       if (ref->refering_type == IPA_REF_CGRAPH)
356         {
357           if (cgraph_node_in_set_p (ipa_ref_refering_node (ref), set))
358             return true;
359         }
360       else
361         {
362           if (varpool_node_in_set_p (ipa_ref_refering_varpool_node (ref),
363                                      vset))
364             return true;
365         }
366     }
367   return false;
368 }
369
370 /* Return true when node is reachable from other partition.  */
371
372 bool
373 reachable_from_this_partition_p (struct cgraph_node *node, cgraph_node_set set)
374 {
375   struct cgraph_edge *e;
376   if (!node->analyzed)
377     return false;
378   if (node->global.inlined_to)
379     return false;
380   for (e = node->callers; e; e = e->next_caller)
381     if (cgraph_node_in_set_p (e->caller, set))
382       return true;
383   return false;
384 }
385
386 /* Output the cgraph NODE to OB.  ENCODER is used to find the
387    reference number of NODE->inlined_to.  SET is the set of nodes we
388    are writing to the current file.  If NODE is not in SET, then NODE
389    is a boundary of a cgraph_node_set and we pretend NODE just has a
390    decl and no callees.  WRITTEN_DECLS is the set of FUNCTION_DECLs
391    that have had their callgraph node written so far.  This is used to
392    determine if NODE is a clone of a previously written node.  */
393
394 static void
395 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
396                  lto_cgraph_encoder_t encoder, cgraph_node_set set,
397                  varpool_node_set vset,
398                  bitmap written_decls)
399 {
400   unsigned int tag;
401   struct bitpack_d *bp;
402   bool boundary_p, wrote_decl_p;
403   intptr_t ref;
404   bool in_other_partition = false;
405
406   boundary_p = !cgraph_node_in_set_p (node, set);
407   wrote_decl_p = bitmap_bit_p (written_decls, DECL_UID (node->decl));
408
409   if (node->analyzed && !boundary_p)
410     tag = LTO_cgraph_analyzed_node;
411   else
412     tag = LTO_cgraph_unavail_node;
413
414   lto_output_uleb128_stream (ob->main_stream, tag);
415
416   /* In WPA mode, we only output part of the call-graph.  Also, we
417      fake cgraph node attributes.  There are two cases that we care.
418
419      Boundary nodes: There are nodes that are not part of SET but are
420      called from within SET.  We artificially make them look like
421      externally visible nodes with no function body.
422
423      Cherry-picked nodes:  These are nodes we pulled from other
424      translation units into SET during IPA-inlining.  We make them as
425      local static nodes to prevent clashes with other local statics.  */
426   if (boundary_p && node->analyzed)
427     {
428       /* Inline clones can not be part of boundary.  
429          gcc_assert (!node->global.inlined_to);  
430
431          FIXME: At the moment they can be, when partition contains an inline
432          clone that is clone of inline clone from outside partition.  We can
433          reshape the clone tree and make other tree to be the root, but it
434          needs a bit extra work and will be promplty done by cgraph_remove_node
435          after reading back.  */
436       in_other_partition = 1;
437     }
438
439   lto_output_uleb128_stream (ob->main_stream, wrote_decl_p);
440
441   if (!wrote_decl_p)
442     bitmap_set_bit (written_decls, DECL_UID (node->decl));
443
444   lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
445   lto_output_sleb128_stream (ob->main_stream, node->count);
446
447   bp = bitpack_create ();
448   bp_pack_value (bp, node->local.local, 1);
449   bp_pack_value (bp, node->local.externally_visible, 1);
450   bp_pack_value (bp, node->local.finalized, 1);
451   bp_pack_value (bp, node->local.inlinable, 1);
452   bp_pack_value (bp, node->local.disregard_inline_limits, 1);
453   bp_pack_value (bp, node->local.redefined_extern_inline, 1);
454   bp_pack_value (bp, node->local.vtable_method, 1);
455   bp_pack_value (bp, node->needed, 1);
456   bp_pack_value (bp, node->address_taken, 1);
457   bp_pack_value (bp, node->abstract_and_needed, 1);
458   bp_pack_value (bp, tag == LTO_cgraph_analyzed_node
459                  && !DECL_EXTERNAL (node->decl)
460                  && (reachable_from_other_partition_p (node, set)
461                      || referenced_from_other_partition_p (&node->ref_list, set, vset)), 1);
462   bp_pack_value (bp, node->lowered, 1);
463   bp_pack_value (bp, in_other_partition, 1);
464   bp_pack_value (bp, node->alias, 1);
465   bp_pack_value (bp, node->finalized_by_frontend, 1);
466   bp_pack_value (bp, node->frequency, 2);
467   lto_output_bitpack (ob->main_stream, bp);
468   bitpack_delete (bp);
469
470   if (tag == LTO_cgraph_analyzed_node)
471     {
472       lto_output_sleb128_stream (ob->main_stream,
473                                  node->local.inline_summary.estimated_self_stack_size);
474       lto_output_sleb128_stream (ob->main_stream,
475                                  node->local.inline_summary.self_size);
476       lto_output_sleb128_stream (ob->main_stream,
477                                  node->local.inline_summary.size_inlining_benefit);
478       lto_output_sleb128_stream (ob->main_stream,
479                                  node->local.inline_summary.self_time);
480       lto_output_sleb128_stream (ob->main_stream,
481                                  node->local.inline_summary.time_inlining_benefit);
482       if (node->global.inlined_to)
483         {
484           ref = lto_cgraph_encoder_lookup (encoder, node->global.inlined_to);
485           gcc_assert (ref != LCC_NOT_FOUND);
486         }
487       else
488         ref = LCC_NOT_FOUND;
489
490       lto_output_sleb128_stream (ob->main_stream, ref);
491     }
492
493   if (node->same_comdat_group && !boundary_p)
494     {
495       ref = lto_cgraph_encoder_lookup (encoder, node->same_comdat_group);
496       gcc_assert (ref != LCC_NOT_FOUND);
497     }
498   else
499     ref = LCC_NOT_FOUND;
500   lto_output_sleb128_stream (ob->main_stream, ref);
501
502   if (node->same_body)
503     {
504       struct cgraph_node *alias;
505       unsigned long alias_count = 1;
506       for (alias = node->same_body; alias->next; alias = alias->next)
507         alias_count++;
508       lto_output_uleb128_stream (ob->main_stream, alias_count);
509       do
510         {
511           lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
512                                     alias->decl);
513           if (alias->thunk.thunk_p)
514             {
515               lto_output_uleb128_stream
516                  (ob->main_stream,
517                   1 + (alias->thunk.this_adjusting != 0) * 2
518                   + (alias->thunk.virtual_offset_p != 0) * 4);
519               lto_output_uleb128_stream (ob->main_stream,
520                                          alias->thunk.fixed_offset);
521               lto_output_uleb128_stream (ob->main_stream,
522                                          alias->thunk.virtual_value);
523               lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
524                                         alias->thunk.alias);
525             }
526           else
527             {
528               lto_output_uleb128_stream (ob->main_stream, 0);
529               lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
530                                         alias->thunk.alias);
531             }
532           alias = alias->previous;
533         }
534       while (alias);
535     }
536   else
537     lto_output_uleb128_stream (ob->main_stream, 0);
538 }
539
540 /* Output the varpool NODE to OB. 
541    If NODE is not in SET, then NODE is a boundary.  */
542
543 static void
544 lto_output_varpool_node (struct lto_simple_output_block *ob, struct varpool_node *node,
545                          cgraph_node_set set, varpool_node_set vset)
546 {
547   bool boundary_p = !varpool_node_in_set_p (node, vset) && node->analyzed;
548   struct bitpack_d *bp;
549   struct varpool_node *alias;
550   int count = 0;
551
552   lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
553   bp = bitpack_create ();
554   bp_pack_value (bp, node->externally_visible, 1);
555   bp_pack_value (bp, node->force_output, 1);
556   bp_pack_value (bp, node->finalized, 1);
557   bp_pack_value (bp, node->alias, 1);
558   gcc_assert (!node->alias || !node->extra_name);
559   gcc_assert (node->finalized || !node->analyzed);
560   gcc_assert (node->needed);
561   /* Constant pool initializers can be de-unified into individual ltrans units.
562      FIXME: Alternatively at -Os we may want to avoid generating for them the local
563      labels and share them across LTRANS partitions.  */
564   if (DECL_IN_CONSTANT_POOL (node->decl))
565     {
566       bp_pack_value (bp, 0, 1);  /* used_from_other_parition.  */
567       bp_pack_value (bp, 0, 1);  /* in_other_partition.  */
568     }
569   else
570     {
571       bp_pack_value (bp, node->analyzed
572                      && referenced_from_other_partition_p (&node->ref_list,
573                                                            set, vset), 1);
574       bp_pack_value (bp, boundary_p, 1);  /* in_other_partition.  */
575     }
576   /* Also emit any extra name aliases.  */
577   for (alias = node->extra_name; alias; alias = alias->next)
578     count++;
579   bp_pack_value (bp, count != 0, 1);
580   lto_output_bitpack (ob->main_stream, bp);
581   bitpack_delete (bp);
582
583   if (count)
584     {
585       lto_output_uleb128_stream (ob->main_stream, count);
586       for (alias = node->extra_name; alias; alias = alias->next)
587         lto_output_var_decl_index (ob->decl_state, ob->main_stream, alias->decl);
588     }
589 }
590
591 /* Output the varpool NODE to OB. 
592    If NODE is not in SET, then NODE is a boundary.  */
593
594 static void
595 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
596                 lto_cgraph_encoder_t encoder,
597                 lto_varpool_encoder_t varpool_encoder)
598 {
599   struct bitpack_d *bp = bitpack_create ();
600   bp_pack_value (bp, ref->refered_type, 1);
601   bp_pack_value (bp, ref->use, 2);
602   lto_output_bitpack (ob->main_stream, bp);
603   bitpack_delete (bp);
604   if (ref->refered_type == IPA_REF_CGRAPH)
605     {
606       int nref = lto_cgraph_encoder_lookup (encoder, ipa_ref_node (ref));
607       gcc_assert (nref != LCC_NOT_FOUND);
608       lto_output_sleb128_stream (ob->main_stream, nref);
609     }
610   else
611     {
612       int nref = lto_varpool_encoder_lookup (varpool_encoder,
613                                              ipa_ref_varpool_node (ref));
614       gcc_assert (nref != LCC_NOT_FOUND);
615       lto_output_sleb128_stream (ob->main_stream, nref);
616     }
617 }
618
619 /* Stream out profile_summary to OB.  */
620
621 static void
622 output_profile_summary (struct lto_simple_output_block *ob)
623 {
624   if (profile_info)
625     {
626       /* We do not output num, it is not terribly useful.  */
627       gcc_assert (profile_info->runs);
628       lto_output_uleb128_stream (ob->main_stream, profile_info->runs);
629       lto_output_sleb128_stream (ob->main_stream, profile_info->sum_all);
630       lto_output_sleb128_stream (ob->main_stream, profile_info->run_max);
631       lto_output_sleb128_stream (ob->main_stream, profile_info->sum_max);
632     }
633   else
634     lto_output_uleb128_stream (ob->main_stream, 0);
635 }
636
637 /* Add NODE into encoder as well as nodes it is cloned from.
638    Do it in a way so clones appear first.  */
639 static void
640 add_node_to (lto_cgraph_encoder_t encoder, struct cgraph_node *node)
641 {
642   if (node->clone_of)
643     add_node_to (encoder, node->clone_of);
644   lto_cgraph_encoder_encode (encoder, node);
645 }
646
647 /* Add all references in LIST to encoders.  */
648
649 static void
650 add_references (lto_cgraph_encoder_t encoder,
651                 lto_varpool_encoder_t varpool_encoder,
652                 struct ipa_ref_list *list)
653 {
654   int i;
655   struct ipa_ref *ref;
656   for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
657     if (ref->refered_type == IPA_REF_CGRAPH)
658       add_node_to (encoder, ipa_ref_node (ref));
659     else
660       {
661         struct varpool_node *vnode = ipa_ref_varpool_node (ref);
662         lto_varpool_encoder_encode (varpool_encoder, vnode);
663       }
664 }
665
666 /* Output all callees or indirect outgoing edges.  EDGE must be the first such
667    edge.  */
668
669 static void
670 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
671                               struct lto_simple_output_block *ob,
672                               lto_cgraph_encoder_t encoder)
673 {
674   if (!edge)
675     return;
676
677   /* Output edges in backward direction, so the reconstructed callgraph match
678      and it is easy to associate call sites in the IPA pass summaries.  */
679   while (edge->next_callee)
680     edge = edge->next_callee;
681   for (; edge; edge = edge->prev_callee)
682     lto_output_edge (ob, edge, encoder);
683 }
684
685 /* Output the part of the cgraph in SET.  */
686
687 static void
688 output_refs (cgraph_node_set set, varpool_node_set vset,
689              lto_cgraph_encoder_t encoder,
690              lto_varpool_encoder_t varpool_encoder)
691 {
692   cgraph_node_set_iterator csi;
693   varpool_node_set_iterator vsi;
694   struct lto_simple_output_block *ob;
695   int count;
696   struct ipa_ref *ref;
697   int i;
698
699   ob = lto_create_simple_output_block (LTO_section_refs);
700
701   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
702     {
703       struct cgraph_node *node = csi_node (csi);
704
705       count = ipa_ref_list_nreferences (&node->ref_list);
706       if (count)
707         {
708           lto_output_uleb128_stream (ob->main_stream, count);
709           lto_output_uleb128_stream (ob->main_stream,
710                                      lto_cgraph_encoder_lookup (encoder, node));
711           for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
712             lto_output_ref (ob, ref, encoder, varpool_encoder);
713         }
714     }
715
716   lto_output_uleb128_stream (ob->main_stream, 0);
717
718   for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
719     {
720       struct varpool_node *node = vsi_node (vsi);
721
722       count = ipa_ref_list_nreferences (&node->ref_list);
723       if (count)
724         {
725           lto_output_uleb128_stream (ob->main_stream, count);
726           lto_output_uleb128_stream (ob->main_stream,
727                                      lto_varpool_encoder_lookup (varpool_encoder,
728                                                                  node));
729           for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
730             lto_output_ref (ob, ref, encoder, varpool_encoder);
731         }
732     }
733
734   lto_output_uleb128_stream (ob->main_stream, 0);
735
736   lto_destroy_simple_output_block (ob);
737 }
738
739 /* Find out all cgraph and varpool nodes we want to encode in current unit
740    and insert them to encoders.  */
741 void
742 compute_ltrans_boundary (struct lto_out_decl_state *state,
743                          cgraph_node_set set, varpool_node_set vset)
744 {
745   struct cgraph_node *node;
746   cgraph_node_set_iterator csi;
747   varpool_node_set_iterator vsi;
748   struct cgraph_edge *edge;
749   int i;
750   lto_cgraph_encoder_t encoder;
751   lto_varpool_encoder_t varpool_encoder;
752
753   encoder = state->cgraph_node_encoder = lto_cgraph_encoder_new ();
754   varpool_encoder = state->varpool_node_encoder = lto_varpool_encoder_new ();
755
756   /* Go over all the nodes in SET and assign references.  */
757   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
758     {
759       node = csi_node (csi);
760       add_node_to (encoder, node);
761       add_references (encoder, varpool_encoder, &node->ref_list);
762     }
763   for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
764     {
765       struct varpool_node *vnode = vsi_node (vsi);
766       gcc_assert (!vnode->alias);
767       lto_varpool_encoder_encode (varpool_encoder, vnode);
768       lto_set_varpool_encoder_encode_initializer (varpool_encoder, vnode);
769       add_references (encoder, varpool_encoder, &vnode->ref_list);
770     }
771   /* Pickle in also the initializer of all referenced readonly variables
772      to help folding.  Constant pool variables are not shared, so we must
773      pickle those too.  */
774   for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
775     {
776       struct varpool_node *vnode = lto_varpool_encoder_deref (varpool_encoder, i);
777       if (DECL_INITIAL (vnode->decl)
778           && !lto_varpool_encoder_encode_initializer_p (varpool_encoder,
779                                                         vnode)
780           && (DECL_IN_CONSTANT_POOL (vnode->decl)
781               ||  TREE_READONLY (vnode->decl)))
782         {
783           lto_set_varpool_encoder_encode_initializer (varpool_encoder, vnode);
784           add_references (encoder, varpool_encoder, &vnode->ref_list);
785         }
786     }
787
788   /* Go over all the nodes again to include callees that are not in
789      SET.  */
790   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
791     {
792       node = csi_node (csi);
793       for (edge = node->callees; edge; edge = edge->next_callee)
794         {
795           struct cgraph_node *callee = edge->callee;
796           if (!cgraph_node_in_set_p (callee, set))
797             {
798               /* We should have moved all the inlines.  */
799               gcc_assert (!callee->global.inlined_to);
800               add_node_to (encoder, callee);
801             }
802         }
803     }
804 }
805
806 /* Output the part of the cgraph in SET.  */
807
808 void
809 output_cgraph (cgraph_node_set set, varpool_node_set vset)
810 {
811   struct cgraph_node *node;
812   struct lto_simple_output_block *ob;
813   cgraph_node_set_iterator csi;
814   int i, n_nodes;
815   bitmap written_decls;
816   lto_cgraph_encoder_t encoder;
817   lto_varpool_encoder_t varpool_encoder;
818   struct cgraph_asm_node *can;
819
820   ob = lto_create_simple_output_block (LTO_section_cgraph);
821
822   output_profile_summary (ob);
823
824   /* An encoder for cgraph nodes should have been created by
825      ipa_write_summaries_1.  */
826   gcc_assert (ob->decl_state->cgraph_node_encoder);
827   gcc_assert (ob->decl_state->varpool_node_encoder);
828   encoder = ob->decl_state->cgraph_node_encoder;
829   varpool_encoder = ob->decl_state->varpool_node_encoder;
830
831   /* The FUNCTION_DECLs for which we have written a node.  The first
832      node found is written as the "original" node, the remaining nodes
833      are considered its clones.  */
834   written_decls = lto_bitmap_alloc ();
835
836   /* Write out the nodes.  We must first output a node and then its clones,
837      otherwise at a time reading back the node there would be nothing to clone
838      from.  */
839   n_nodes = lto_cgraph_encoder_size (encoder);
840   for (i = 0; i < n_nodes; i++)
841     {
842       node = lto_cgraph_encoder_deref (encoder, i);
843       lto_output_node (ob, node, encoder, set, vset, written_decls);
844     }
845
846   lto_bitmap_free (written_decls);
847
848   /* Go over the nodes in SET again to write edges.  */
849   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
850     {
851       node = csi_node (csi);
852       output_outgoing_cgraph_edges (node->callees, ob, encoder);
853       output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
854     }
855
856   lto_output_uleb128_stream (ob->main_stream, 0);
857
858   /* Emit toplevel asms.  */
859   for (can = cgraph_asm_nodes; can; can = can->next)
860     {
861       int len = TREE_STRING_LENGTH (can->asm_str);
862       lto_output_uleb128_stream (ob->main_stream, len);
863       for (i = 0; i < len; ++i)
864         lto_output_1_stream (ob->main_stream,
865                              TREE_STRING_POINTER (can->asm_str)[i]);
866     }
867
868   lto_output_uleb128_stream (ob->main_stream, 0);
869
870   lto_destroy_simple_output_block (ob);
871   output_varpool (set, vset);
872   output_refs (set, vset, encoder, varpool_encoder);
873 }
874
875 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
876    STACK_SIZE, SELF_TIME and SELF_SIZE.  This is called either to initialize
877    NODE or to replace the values in it, for instance because the first
878    time we saw it, the function body was not available but now it
879    is.  BP is a bitpack with all the bitflags for NODE read from the
880    stream.  */
881
882 static void
883 input_overwrite_node (struct lto_file_decl_data *file_data,
884                       struct cgraph_node *node,
885                       enum LTO_cgraph_tags tag,
886                       struct bitpack_d *bp,
887                       unsigned int stack_size,
888                       unsigned int self_time,
889                       unsigned int time_inlining_benefit,
890                       unsigned int self_size,
891                       unsigned int size_inlining_benefit)
892 {
893   node->aux = (void *) tag;
894   node->local.inline_summary.estimated_self_stack_size = stack_size;
895   node->local.inline_summary.self_time = self_time;
896   node->local.inline_summary.time_inlining_benefit = time_inlining_benefit;
897   node->local.inline_summary.self_size = self_size;
898   node->local.inline_summary.size_inlining_benefit = size_inlining_benefit;
899   node->global.time = self_time;
900   node->global.size = self_size;
901   node->global.estimated_stack_size = stack_size;
902   node->global.estimated_growth = INT_MIN;
903   node->local.lto_file_data = file_data;
904
905   node->local.local = bp_unpack_value (bp, 1);
906   node->local.externally_visible = bp_unpack_value (bp, 1);
907   node->local.finalized = bp_unpack_value (bp, 1);
908   node->local.inlinable = bp_unpack_value (bp, 1);
909   node->local.disregard_inline_limits = bp_unpack_value (bp, 1);
910   node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
911   node->local.vtable_method = bp_unpack_value (bp, 1);
912   node->needed = bp_unpack_value (bp, 1);
913   node->address_taken = bp_unpack_value (bp, 1);
914   node->abstract_and_needed = bp_unpack_value (bp, 1);
915   node->reachable_from_other_partition = bp_unpack_value (bp, 1);
916   node->lowered = bp_unpack_value (bp, 1);
917   node->analyzed = tag == LTO_cgraph_analyzed_node;
918   node->in_other_partition = bp_unpack_value (bp, 1);
919   node->alias = bp_unpack_value (bp, 1);
920   node->finalized_by_frontend = bp_unpack_value (bp, 1);
921   node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
922 }
923
924 /* Output the part of the cgraph in SET.  */
925
926 static void
927 output_varpool (cgraph_node_set set, varpool_node_set vset)
928 {
929   struct lto_simple_output_block *ob = lto_create_simple_output_block (LTO_section_varpool);
930   lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
931   int len = lto_varpool_encoder_size (varpool_encoder), i;
932
933   lto_output_uleb128_stream (ob->main_stream, len);
934
935   /* Write out the nodes.  We must first output a node and then its clones,
936      otherwise at a time reading back the node there would be nothing to clone
937      from.  */
938   for (i = 0; i < len; i++)
939     {
940       lto_output_varpool_node (ob, lto_varpool_encoder_deref (varpool_encoder, i),
941                                set, vset);
942     }
943
944   lto_destroy_simple_output_block (ob);
945 }
946
947 /* Read a node from input_block IB.  TAG is the node's tag just read.
948    Return the node read or overwriten.  */
949
950 static struct cgraph_node *
951 input_node (struct lto_file_decl_data *file_data,
952             struct lto_input_block *ib,
953             enum LTO_cgraph_tags tag)
954 {
955   tree fn_decl;
956   struct cgraph_node *node;
957   struct bitpack_d *bp;
958   int stack_size = 0;
959   unsigned decl_index;
960   bool clone_p;
961   int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
962   int self_time = 0;
963   int self_size = 0;
964   int time_inlining_benefit = 0;
965   int size_inlining_benefit = 0;
966   unsigned long same_body_count = 0;
967
968   clone_p = (lto_input_uleb128 (ib) != 0);
969
970   decl_index = lto_input_uleb128 (ib);
971   fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
972
973   if (clone_p)
974     node = cgraph_clone_node (cgraph_node (fn_decl), 0,
975                               CGRAPH_FREQ_BASE, 0, false, NULL);
976
977   else
978     node = cgraph_node (fn_decl);
979
980   node->count = lto_input_sleb128 (ib);
981   bp = lto_input_bitpack (ib);
982
983   if (tag == LTO_cgraph_analyzed_node)
984     {
985       stack_size = lto_input_sleb128 (ib);
986       self_size = lto_input_sleb128 (ib);
987       size_inlining_benefit = lto_input_sleb128 (ib);
988       self_time = lto_input_sleb128 (ib);
989       time_inlining_benefit = lto_input_sleb128 (ib);
990
991       ref = lto_input_sleb128 (ib);
992     }
993
994   ref2 = lto_input_sleb128 (ib);
995   same_body_count = lto_input_uleb128 (ib);
996
997   /* Make sure that we have not read this node before.  Nodes that
998      have already been read will have their tag stored in the 'aux'
999      field.  Since built-in functions can be referenced in multiple
1000      functions, they are expected to be read more than once.  */
1001   if (node->aux && !DECL_IS_BUILTIN (node->decl))
1002     internal_error ("bytecode stream: found multiple instances of cgraph "
1003                     "node %d", node->uid);
1004
1005   input_overwrite_node (file_data, node, tag, bp, stack_size, self_time,
1006                         time_inlining_benefit, self_size,
1007                         size_inlining_benefit);
1008   bitpack_delete (bp);
1009
1010   /* Store a reference for now, and fix up later to be a pointer.  */
1011   node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
1012
1013   /* Store a reference for now, and fix up later to be a pointer.  */
1014   node->same_comdat_group = (cgraph_node_ptr) (intptr_t) ref2;
1015
1016   while (same_body_count-- > 0)
1017     {
1018       tree alias_decl;
1019       int type;
1020       decl_index = lto_input_uleb128 (ib);
1021       alias_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1022       type = lto_input_uleb128 (ib);
1023       if (!type)
1024         {
1025           tree real_alias;
1026           decl_index = lto_input_uleb128 (ib);
1027           real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1028           cgraph_same_body_alias (alias_decl, real_alias);
1029         }
1030       else
1031         {
1032           HOST_WIDE_INT fixed_offset = lto_input_uleb128 (ib);
1033           HOST_WIDE_INT virtual_value = lto_input_uleb128 (ib);
1034           tree real_alias;
1035           decl_index = lto_input_uleb128 (ib);
1036           real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1037           cgraph_add_thunk (alias_decl, fn_decl, type & 2, fixed_offset,
1038                             virtual_value,
1039                             (type & 4) ? size_int (virtual_value) : NULL_TREE,
1040                             real_alias);
1041         }
1042     }
1043   return node;
1044 }
1045
1046 /* Read a node from input_block IB.  TAG is the node's tag just read.
1047    Return the node read or overwriten.  */
1048
1049 static struct varpool_node *
1050 input_varpool_node (struct lto_file_decl_data *file_data,
1051                     struct lto_input_block *ib)
1052 {
1053   int decl_index;
1054   tree var_decl;
1055   struct varpool_node *node;
1056   struct bitpack_d *bp;
1057   bool aliases_p;
1058   int count;
1059
1060   decl_index = lto_input_uleb128 (ib);
1061   var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1062   node = varpool_node (var_decl);
1063
1064   bp = lto_input_bitpack (ib);
1065   node->externally_visible = bp_unpack_value (bp, 1);
1066   node->force_output = bp_unpack_value (bp, 1);
1067   node->finalized = bp_unpack_value (bp, 1);
1068   node->alias = bp_unpack_value (bp, 1);
1069   node->analyzed = node->finalized; 
1070   node->used_from_other_partition = bp_unpack_value (bp, 1);
1071   node->in_other_partition = bp_unpack_value (bp, 1);
1072   aliases_p = bp_unpack_value (bp, 1);
1073   if (node->finalized)
1074     varpool_mark_needed_node (node);
1075   bitpack_delete (bp);
1076   if (aliases_p)
1077     {
1078       count = lto_input_uleb128 (ib);
1079       for (; count > 0; count --)
1080         {
1081           tree decl = lto_file_decl_data_get_var_decl (file_data,
1082                                                        lto_input_uleb128 (ib));
1083           varpool_extra_name_alias (decl, var_decl);
1084         }
1085     }
1086   return node;
1087 }
1088
1089 /* Read a node from input_block IB.  TAG is the node's tag just read.
1090    Return the node read or overwriten.  */
1091
1092 static void
1093 input_ref (struct lto_input_block *ib,
1094            struct cgraph_node *refering_node,
1095            struct varpool_node *refering_varpool_node,
1096            VEC(cgraph_node_ptr, heap) *nodes,
1097            VEC(varpool_node_ptr, heap) *varpool_nodes)
1098 {
1099   struct cgraph_node *node = NULL;
1100   struct varpool_node *varpool_node = NULL;
1101   struct bitpack_d *bp;
1102   enum ipa_ref_type type;
1103   enum ipa_ref_use use;
1104
1105   bp = lto_input_bitpack (ib);
1106   type = (enum ipa_ref_type) bp_unpack_value (bp, 1);
1107   use = (enum ipa_ref_use) bp_unpack_value (bp, 2);
1108   bitpack_delete (bp);
1109   if (type == IPA_REF_CGRAPH)
1110     node = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1111   else
1112     varpool_node = VEC_index (varpool_node_ptr, varpool_nodes, lto_input_sleb128 (ib));
1113   ipa_record_reference (refering_node, refering_varpool_node,
1114                         node, varpool_node, use, NULL);
1115 }
1116
1117 /* Read an edge from IB.  NODES points to a vector of previously read nodes for
1118    decoding caller and callee of the edge to be read.  If INDIRECT is true, the
1119    edge being read is indirect (in the sense that it has
1120    indirect_unknown_callee set).  */
1121
1122 static void
1123 input_edge (struct lto_input_block *ib, VEC(cgraph_node_ptr, heap) *nodes,
1124             bool indirect)
1125 {
1126   struct cgraph_node *caller, *callee;
1127   struct cgraph_edge *edge;
1128   unsigned int stmt_id;
1129   gcov_type count;
1130   int freq;
1131   unsigned int nest;
1132   cgraph_inline_failed_t inline_failed;
1133   struct bitpack_d *bp;
1134   enum ld_plugin_symbol_resolution caller_resolution;
1135   int ecf_flags = 0;
1136
1137   caller = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1138   if (caller == NULL || caller->decl == NULL_TREE)
1139     internal_error ("bytecode stream: no caller found while reading edge");
1140
1141   if (!indirect)
1142     {
1143       callee = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1144       if (callee == NULL || callee->decl == NULL_TREE)
1145         internal_error ("bytecode stream: no callee found while reading edge");
1146     }
1147   else
1148     callee = NULL;
1149
1150   count = (gcov_type) lto_input_sleb128 (ib);
1151
1152   bp = lto_input_bitpack (ib);
1153   stmt_id = (unsigned int) bp_unpack_value (bp, HOST_BITS_PER_INT);
1154   inline_failed = (cgraph_inline_failed_t) bp_unpack_value (bp,
1155                                                             HOST_BITS_PER_INT);
1156   freq = (int) bp_unpack_value (bp, HOST_BITS_PER_INT);
1157   nest = (unsigned) bp_unpack_value (bp, 30);
1158
1159   /* If the caller was preempted, don't create the edge.
1160      ???  Should we ever have edges from a preempted caller?  */
1161   caller_resolution = lto_symtab_get_resolution (caller->decl);
1162   if (caller_resolution == LDPR_PREEMPTED_REG
1163       || caller_resolution == LDPR_PREEMPTED_IR)
1164     return;
1165
1166   if (indirect)
1167     edge = cgraph_create_indirect_edge (caller, NULL, 0, count, freq, nest);
1168   else
1169     edge = cgraph_create_edge (caller, callee, NULL, count, freq, nest);
1170
1171   edge->indirect_inlining_edge = bp_unpack_value (bp, 1);
1172   edge->lto_stmt_uid = stmt_id;
1173   edge->inline_failed = inline_failed;
1174   edge->call_stmt_cannot_inline_p = bp_unpack_value (bp, 1);
1175   edge->can_throw_external = bp_unpack_value (bp, 1);
1176   if (indirect)
1177     {
1178       if (bp_unpack_value (bp, 1))
1179         ecf_flags |= ECF_CONST;
1180       if (bp_unpack_value (bp, 1))
1181         ecf_flags |= ECF_PURE;
1182       if (bp_unpack_value (bp, 1))
1183         ecf_flags |= ECF_NORETURN;
1184       if (bp_unpack_value (bp, 1))
1185         ecf_flags |= ECF_MALLOC;
1186       if (bp_unpack_value (bp, 1))
1187         ecf_flags |= ECF_NOTHROW;
1188       if (bp_unpack_value (bp, 1))
1189         ecf_flags |= ECF_RETURNS_TWICE;
1190       edge->indirect_info->ecf_flags = ecf_flags;
1191     }
1192   bitpack_delete (bp);
1193 }
1194
1195
1196 /* Read a cgraph from IB using the info in FILE_DATA.  */
1197
1198 static VEC(cgraph_node_ptr, heap) *
1199 input_cgraph_1 (struct lto_file_decl_data *file_data,
1200                 struct lto_input_block *ib)
1201 {
1202   enum LTO_cgraph_tags tag;
1203   VEC(cgraph_node_ptr, heap) *nodes = NULL;
1204   struct cgraph_node *node;
1205   unsigned i;
1206   unsigned HOST_WIDE_INT len;
1207
1208   tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
1209   while (tag)
1210     {
1211       if (tag == LTO_cgraph_edge)
1212         input_edge (ib, nodes, false);
1213       else if (tag == LTO_cgraph_indirect_edge)
1214         input_edge (ib, nodes, true);
1215       else
1216         {
1217           node = input_node (file_data, ib, tag);
1218           if (node == NULL || node->decl == NULL_TREE)
1219             internal_error ("bytecode stream: found empty cgraph node");
1220           VEC_safe_push (cgraph_node_ptr, heap, nodes, node);
1221           lto_cgraph_encoder_encode (file_data->cgraph_node_encoder, node);
1222         }
1223
1224       tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
1225     }
1226
1227   /* Input toplevel asms.  */
1228   len = lto_input_uleb128 (ib);
1229   while (len)
1230     {
1231       char *str = (char *)xmalloc (len + 1);
1232       for (i = 0; i < len; ++i)
1233         str[i] = lto_input_1_unsigned (ib);
1234       cgraph_add_asm_node (build_string (len, str));
1235       free (str);
1236
1237       len = lto_input_uleb128 (ib);
1238     }
1239
1240   for (i = 0; VEC_iterate (cgraph_node_ptr, nodes, i, node); i++)
1241     {
1242       int ref = (int) (intptr_t) node->global.inlined_to;
1243
1244       /* Fixup inlined_to from reference to pointer.  */
1245       if (ref != LCC_NOT_FOUND)
1246         node->global.inlined_to = VEC_index (cgraph_node_ptr, nodes, ref);
1247       else
1248         node->global.inlined_to = NULL;
1249
1250       ref = (int) (intptr_t) node->same_comdat_group;
1251
1252       /* Fixup same_comdat_group from reference to pointer.  */
1253       if (ref != LCC_NOT_FOUND)
1254         node->same_comdat_group = VEC_index (cgraph_node_ptr, nodes, ref);
1255       else
1256         node->same_comdat_group = NULL;
1257     }
1258   return nodes;
1259 }
1260
1261 /* Read a varpool from IB using the info in FILE_DATA.  */
1262
1263 static VEC(varpool_node_ptr, heap) *
1264 input_varpool_1 (struct lto_file_decl_data *file_data,
1265                 struct lto_input_block *ib)
1266 {
1267   unsigned HOST_WIDE_INT len;
1268   VEC(varpool_node_ptr, heap) *varpool = NULL;
1269
1270   len = lto_input_uleb128 (ib);
1271   while (len)
1272     {
1273       VEC_safe_push (varpool_node_ptr, heap, varpool,
1274                      input_varpool_node (file_data, ib));
1275       len--;
1276     }
1277   return varpool;
1278 }
1279
1280 /* Input ipa_refs.  */
1281
1282 static void
1283 input_refs (struct lto_input_block *ib,
1284             VEC(cgraph_node_ptr, heap) *nodes,
1285             VEC(varpool_node_ptr, heap) *varpool)
1286 {
1287   int count;
1288   int idx;
1289   while (true)
1290     {
1291       struct cgraph_node *node;
1292       count = lto_input_uleb128 (ib);
1293       if (!count)
1294         break;
1295       idx = lto_input_uleb128 (ib);
1296       node = VEC_index (cgraph_node_ptr, nodes, idx);
1297       while (count)
1298         {
1299           input_ref (ib, node, NULL, nodes, varpool);
1300           count--;
1301         }
1302     }
1303   while (true)
1304     {
1305       struct varpool_node *node;
1306       count = lto_input_uleb128 (ib);
1307       if (!count)
1308         break;
1309       node = VEC_index (varpool_node_ptr, varpool, lto_input_uleb128 (ib));
1310       while (count)
1311         {
1312           input_ref (ib, NULL, node, nodes, varpool);
1313           count--;
1314         }
1315     }
1316 }
1317             
1318
1319 static struct gcov_ctr_summary lto_gcov_summary;
1320
1321 /* Input profile_info from IB.  */
1322 static void
1323 input_profile_summary (struct lto_input_block *ib)
1324 {
1325   unsigned int runs = lto_input_uleb128 (ib);
1326   if (runs)
1327     {
1328       if (!profile_info)
1329         {
1330           profile_info = &lto_gcov_summary;
1331           lto_gcov_summary.runs = runs;
1332           lto_gcov_summary.sum_all = lto_input_sleb128 (ib);
1333           lto_gcov_summary.run_max = lto_input_sleb128 (ib);
1334           lto_gcov_summary.sum_max = lto_input_sleb128 (ib);
1335         }
1336       /* We can support this by scaling all counts to nearest common multiple
1337          of all different runs, but it is perhaps not worth the effort.  */
1338       else if (profile_info->runs != runs
1339                || profile_info->sum_all != lto_input_sleb128 (ib)
1340                || profile_info->run_max != lto_input_sleb128 (ib)
1341                || profile_info->sum_max != lto_input_sleb128 (ib))
1342         sorry ("Combining units with different profiles is not supported.");
1343       /* We allow some units to have profile and other to not have one.  This will
1344          just make unprofiled units to be size optimized that is sane.  */
1345     }
1346
1347 }
1348
1349 /* Input and merge the cgraph from each of the .o files passed to
1350    lto1.  */
1351
1352 void
1353 input_cgraph (void)
1354 {
1355   struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1356   struct lto_file_decl_data *file_data;
1357   unsigned int j = 0;
1358   struct cgraph_node *node;
1359
1360   while ((file_data = file_data_vec[j++]))
1361     {
1362       const char *data;
1363       size_t len;
1364       struct lto_input_block *ib;
1365       VEC(cgraph_node_ptr, heap) *nodes;
1366       VEC(varpool_node_ptr, heap) *varpool;
1367
1368       ib = lto_create_simple_input_block (file_data, LTO_section_cgraph,
1369                                           &data, &len);
1370       input_profile_summary (ib);
1371       file_data->cgraph_node_encoder = lto_cgraph_encoder_new ();
1372       nodes = input_cgraph_1 (file_data, ib);
1373       lto_destroy_simple_input_block (file_data, LTO_section_cgraph,
1374                                       ib, data, len);
1375
1376       ib = lto_create_simple_input_block (file_data, LTO_section_varpool,
1377                                           &data, &len);
1378       varpool = input_varpool_1 (file_data, ib);
1379       lto_destroy_simple_input_block (file_data, LTO_section_varpool,
1380                                       ib, data, len);
1381
1382       ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1383                                           &data, &len);
1384       input_refs (ib, nodes, varpool);
1385       lto_destroy_simple_input_block (file_data, LTO_section_refs,
1386                                       ib, data, len);
1387       VEC_free (cgraph_node_ptr, heap, nodes);
1388       VEC_free (varpool_node_ptr, heap, varpool);
1389     }
1390
1391   /* Clear out the aux field that was used to store enough state to
1392      tell which nodes should be overwritten.  */
1393   for (node = cgraph_nodes; node; node = node->next)
1394     {
1395       /* Some nodes may have been created by cgraph_node.  This
1396          happens when the callgraph contains nested functions.  If the
1397          node for the parent function was never emitted to the gimple
1398          file, cgraph_node will create a node for it when setting the
1399          context of the nested function.  */
1400       if (node->local.lto_file_data)
1401         node->aux = NULL;
1402     }
1403 }