OSDN Git Service

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