OSDN Git Service

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