OSDN Git Service

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