OSDN Git Service

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