twe(4): Add two missing error checks.
[dragonfly.git] / contrib / gcc-5.0 / gcc / lto-cgraph.c
1 /* Write and read the cgraph to the memory mapped representation of a
2    .o file.
3
4    Copyright (C) 2009-2015 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 "hash-set.h"
28 #include "machmode.h"
29 #include "vec.h"
30 #include "double-int.h"
31 #include "input.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "wide-int.h"
35 #include "inchash.h"
36 #include "tree.h"
37 #include "fold-const.h"
38 #include "stringpool.h"
39 #include "predict.h"
40 #include "hard-reg-set.h"
41 #include "function.h"
42 #include "basic-block.h"
43 #include "tree-ssa-alias.h"
44 #include "internal-fn.h"
45 #include "gimple-expr.h"
46 #include "is-a.h"
47 #include "gimple.h"
48 #include "hashtab.h"
49 #include "rtl.h"
50 #include "flags.h"
51 #include "statistics.h"
52 #include "real.h"
53 #include "fixed-value.h"
54 #include "insn-config.h"
55 #include "expmed.h"
56 #include "dojump.h"
57 #include "explow.h"
58 #include "calls.h"
59 #include "emit-rtl.h"
60 #include "varasm.h"
61 #include "stmt.h"
62 #include "expr.h"
63 #include "params.h"
64 #include "langhooks.h"
65 #include "bitmap.h"
66 #include "diagnostic-core.h"
67 #include "except.h"
68 #include "timevar.h"
69 #include "hash-map.h"
70 #include "plugin-api.h"
71 #include "ipa-ref.h"
72 #include "cgraph.h"
73 #include "lto-streamer.h"
74 #include "data-streamer.h"
75 #include "tree-streamer.h"
76 #include "gcov-io.h"
77 #include "tree-pass.h"
78 #include "profile.h"
79 #include "context.h"
80 #include "pass_manager.h"
81 #include "ipa-utils.h"
82 #include "omp-low.h"
83
84 /* True when asm nodes has been output.  */
85 bool asm_nodes_output = false;
86
87 static void output_cgraph_opt_summary (void);
88 static void input_cgraph_opt_summary (vec<symtab_node *>  nodes);
89
90 /* Number of LDPR values known to GCC.  */
91 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
92
93 /* All node orders are ofsetted by ORDER_BASE.  */
94 static int order_base;
95
96 /* Cgraph streaming is organized as set of record whose type
97    is indicated by a tag.  */
98 enum LTO_symtab_tags
99 {
100   /* Must leave 0 for the stopper.  */
101
102   /* Cgraph node without body available.  */
103   LTO_symtab_unavail_node = 1,
104   /* Cgraph node with function body.  */
105   LTO_symtab_analyzed_node,
106   /* Cgraph edges.  */
107   LTO_symtab_edge,
108   LTO_symtab_indirect_edge,
109   LTO_symtab_variable,
110   LTO_symtab_last_tag
111 };
112
113 /* Create a new symtab encoder.
114    if FOR_INPUT, the encoder allocate only datastructures needed
115    to read the symtab.  */
116
117 lto_symtab_encoder_t
118 lto_symtab_encoder_new (bool for_input)
119 {
120   lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
121
122   if (!for_input)
123     encoder->map = new hash_map<symtab_node *, size_t>;
124   encoder->nodes.create (0);
125   return encoder;
126 }
127
128
129 /* Delete ENCODER and its components.  */
130
131 void
132 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
133 {
134    encoder->nodes.release ();
135    if (encoder->map)
136      delete encoder->map;
137    free (encoder);
138 }
139
140
141 /* Return the existing reference number of NODE in the symtab encoder in
142    output block OB.  Assign a new reference if this is the first time
143    NODE is encoded.  */
144
145 int
146 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
147                            symtab_node *node)
148 {
149   int ref;
150
151   if (!encoder->map)
152     {
153       lto_encoder_entry entry = {node, false, false, false};
154
155       ref = encoder->nodes.length ();
156       encoder->nodes.safe_push (entry);
157       return ref;
158     }
159
160   size_t *slot = encoder->map->get (node);
161   if (!slot || !*slot)
162     {
163       lto_encoder_entry entry = {node, false, false, false};
164       ref = encoder->nodes.length ();
165       if (!slot)
166         encoder->map->put (node, ref + 1);
167       encoder->nodes.safe_push (entry);
168     }
169   else
170     ref = *slot - 1;
171
172   return ref;
173 }
174
175 /* Remove NODE from encoder.  */
176
177 bool
178 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
179                                 symtab_node *node)
180 {
181   int index;
182   lto_encoder_entry last_node;
183
184   size_t *slot = encoder->map->get (node);
185   if (slot == NULL || !*slot)
186     return false;
187
188   index = *slot - 1;
189   gcc_checking_assert (encoder->nodes[index].node == node);
190
191   /* Remove from vector. We do this by swapping node with the last element
192      of the vector.  */
193   last_node = encoder->nodes.pop ();
194   if (last_node.node != node)
195     {
196       gcc_assert (encoder->map->put (last_node.node, index + 1));
197
198       /* Move the last element to the original spot of NODE.  */
199       encoder->nodes[index] = last_node;
200     }
201
202   /* Remove element from hash table.  */
203   encoder->map->remove (node);
204   return true;
205 }
206
207
208 /* Return TRUE if we should encode the body of NODE (if any).  */
209
210 bool
211 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
212                                   struct cgraph_node *node)
213 {
214   int index = lto_symtab_encoder_lookup (encoder, node);
215   return encoder->nodes[index].body;
216 }
217
218 /* Specify that we encode the body of NODE in this partition.  */
219
220 static void
221 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
222                                     struct cgraph_node *node)
223 {
224   int index = lto_symtab_encoder_encode (encoder, node);
225   gcc_checking_assert (encoder->nodes[index].node == node);
226   encoder->nodes[index].body = true;
227 }
228
229 /* Return TRUE if we should encode initializer of NODE (if any).  */
230
231 bool
232 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
233                                          varpool_node *node)
234 {
235   int index = lto_symtab_encoder_lookup (encoder, node);
236   if (index == LCC_NOT_FOUND)
237     return false;
238   return encoder->nodes[index].initializer;
239 }
240
241 /* Specify that we should encode initializer of NODE (if any).  */
242
243 static void
244 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
245                                            varpool_node *node)
246 {
247   int index = lto_symtab_encoder_lookup (encoder, node);
248   encoder->nodes[index].initializer = true;
249 }
250
251 /* Return TRUE if NODE is in this partition.  */
252
253 bool
254 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
255                                    symtab_node *node)
256 {
257   int index = lto_symtab_encoder_lookup (encoder, node);
258   if (index == LCC_NOT_FOUND)
259     return false;
260   return encoder->nodes[index].in_partition;
261 }
262
263 /* Specify that NODE is in this partition.  */
264
265 void
266 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
267                                      symtab_node *node)
268 {
269   int index = lto_symtab_encoder_encode (encoder, node);
270   encoder->nodes[index].in_partition = true;
271 }
272
273 /* Output the cgraph EDGE to OB using ENCODER.  */
274
275 static void
276 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
277                  lto_symtab_encoder_t encoder)
278 {
279   unsigned int uid;
280   intptr_t ref;
281   struct bitpack_d bp;
282
283   if (edge->indirect_unknown_callee)
284     streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
285                          LTO_symtab_indirect_edge);
286   else
287     streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
288                          LTO_symtab_edge);
289
290   ref = lto_symtab_encoder_lookup (encoder, edge->caller);
291   gcc_assert (ref != LCC_NOT_FOUND);
292   streamer_write_hwi_stream (ob->main_stream, ref);
293
294   if (!edge->indirect_unknown_callee)
295     {
296       ref = lto_symtab_encoder_lookup (encoder, edge->callee);
297       gcc_assert (ref != LCC_NOT_FOUND);
298       streamer_write_hwi_stream (ob->main_stream, ref);
299     }
300
301   streamer_write_gcov_count_stream (ob->main_stream, edge->count);
302
303   bp = bitpack_create (ob->main_stream);
304   uid = (!gimple_has_body_p (edge->caller->decl)
305          ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt) + 1);
306   bp_pack_enum (&bp, cgraph_inline_failed_t,
307                 CIF_N_REASONS, edge->inline_failed);
308   bp_pack_var_len_unsigned (&bp, uid);
309   bp_pack_var_len_unsigned (&bp, edge->frequency);
310   bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
311   bp_pack_value (&bp, edge->speculative, 1);
312   bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
313   bp_pack_value (&bp, edge->can_throw_external, 1);
314   bp_pack_value (&bp, edge->in_polymorphic_cdtor, 1);
315   if (edge->indirect_unknown_callee)
316     {
317       int flags = edge->indirect_info->ecf_flags;
318       bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
319       bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
320       bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
321       bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
322       bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
323       bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
324       /* Flags that should not appear on indirect calls.  */
325       gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
326                              | ECF_MAY_BE_ALLOCA
327                              | ECF_SIBCALL
328                              | ECF_LEAF
329                              | ECF_NOVOPS)));
330     }
331   streamer_write_bitpack (&bp);
332   if (edge->indirect_unknown_callee)
333     {
334       streamer_write_hwi_stream (ob->main_stream,
335                                  edge->indirect_info->common_target_id);
336       if (edge->indirect_info->common_target_id)
337         streamer_write_hwi_stream
338            (ob->main_stream, edge->indirect_info->common_target_probability);
339     }
340 }
341
342 /* Return if NODE contain references from other partitions.  */
343
344 bool
345 referenced_from_other_partition_p (symtab_node *node, lto_symtab_encoder_t encoder)
346 {
347   int i;
348   struct ipa_ref *ref = NULL;
349
350   for (i = 0; node->iterate_referring (i, ref); i++)
351     {
352       /* Ignore references from non-offloadable nodes while streaming NODE into
353          offload LTO section.  */
354       if (!ref->referring->need_lto_streaming)
355         continue;
356
357       if (ref->referring->in_other_partition
358           || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
359         return true;
360     }
361   return false;
362 }
363
364 /* Return true when node is reachable from other partition.  */
365
366 bool
367 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
368 {
369   struct cgraph_edge *e;
370   if (!node->definition)
371     return false;
372   if (node->global.inlined_to)
373     return false;
374   for (e = node->callers; e; e = e->next_caller)
375     {
376       /* Ignore references from non-offloadable nodes while streaming NODE into
377          offload LTO section.  */
378       if (!e->caller->need_lto_streaming)
379         continue;
380
381       if (e->caller->in_other_partition
382           || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
383         return true;
384     }
385   return false;
386 }
387
388 /* Return if NODE contain references from other partitions.  */
389
390 bool
391 referenced_from_this_partition_p (symtab_node *node,
392                                   lto_symtab_encoder_t encoder)
393 {
394   int i;
395   struct ipa_ref *ref = NULL;
396
397   for (i = 0; node->iterate_referring (i, ref); i++)
398     if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
399       return true;
400   return false;
401 }
402
403 /* Return true when node is reachable from other partition.  */
404
405 bool
406 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
407 {
408   struct cgraph_edge *e;
409   for (e = node->callers; e; e = e->next_caller)
410     if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
411       return true;
412   return false;
413 }
414
415 /* Output the cgraph NODE to OB.  ENCODER is used to find the
416    reference number of NODE->inlined_to.  SET is the set of nodes we
417    are writing to the current file.  If NODE is not in SET, then NODE
418    is a boundary of a cgraph_node_set and we pretend NODE just has a
419    decl and no callees.  WRITTEN_DECLS is the set of FUNCTION_DECLs
420    that have had their callgraph node written so far.  This is used to
421    determine if NODE is a clone of a previously written node.  */
422
423 static void
424 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
425                  lto_symtab_encoder_t encoder)
426 {
427   unsigned int tag;
428   struct bitpack_d bp;
429   bool boundary_p;
430   intptr_t ref;
431   bool in_other_partition = false;
432   struct cgraph_node *clone_of, *ultimate_clone_of;
433   ipa_opt_pass_d *pass;
434   int i;
435   const char *comdat;
436   const char *section;
437   tree group;
438
439   boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
440
441   if (node->analyzed && (!boundary_p || node->alias || node->thunk.thunk_p))
442     tag = LTO_symtab_analyzed_node;
443   else
444     tag = LTO_symtab_unavail_node;
445
446   streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
447                        tag);
448   streamer_write_hwi_stream (ob->main_stream, node->order);
449
450   /* In WPA mode, we only output part of the call-graph.  Also, we
451      fake cgraph node attributes.  There are two cases that we care.
452
453      Boundary nodes: There are nodes that are not part of SET but are
454      called from within SET.  We artificially make them look like
455      externally visible nodes with no function body.
456
457      Cherry-picked nodes:  These are nodes we pulled from other
458      translation units into SET during IPA-inlining.  We make them as
459      local static nodes to prevent clashes with other local statics.  */
460   if (boundary_p && node->analyzed
461       && node->get_partitioning_class () == SYMBOL_PARTITION)
462     {
463       /* Inline clones can not be part of boundary.  
464          gcc_assert (!node->global.inlined_to);  
465
466          FIXME: At the moment they can be, when partition contains an inline
467          clone that is clone of inline clone from outside partition.  We can
468          reshape the clone tree and make other tree to be the root, but it
469          needs a bit extra work and will be promplty done by cgraph_remove_node
470          after reading back.  */
471       in_other_partition = 1;
472     }
473
474   clone_of = node->clone_of;
475   while (clone_of
476          && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
477     if (clone_of->prev_sibling_clone)
478       clone_of = clone_of->prev_sibling_clone;
479     else
480       clone_of = clone_of->clone_of;
481
482   /* See if body of the master function is output.  If not, we are seeing only
483      an declaration and we do not need to pass down clone tree. */
484   ultimate_clone_of = clone_of;
485   while (ultimate_clone_of && ultimate_clone_of->clone_of)
486     ultimate_clone_of = ultimate_clone_of->clone_of;
487
488   if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
489     clone_of = NULL;
490
491   if (tag == LTO_symtab_analyzed_node)
492     gcc_assert (clone_of || !node->clone_of);
493   if (!clone_of)
494     streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
495   else
496     streamer_write_hwi_stream (ob->main_stream, ref);
497
498
499   lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
500   streamer_write_gcov_count_stream (ob->main_stream, node->count);
501   streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
502
503   streamer_write_hwi_stream (ob->main_stream,
504                              node->ipa_transforms_to_apply.length ());
505   FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
506     streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
507
508   if (tag == LTO_symtab_analyzed_node)
509     {
510       if (node->global.inlined_to)
511         {
512           ref = lto_symtab_encoder_lookup (encoder, node->global.inlined_to);
513           gcc_assert (ref != LCC_NOT_FOUND);
514         }
515       else
516         ref = LCC_NOT_FOUND;
517
518       streamer_write_hwi_stream (ob->main_stream, ref);
519     }
520
521   group = node->get_comdat_group ();
522   if (group)
523     comdat = IDENTIFIER_POINTER (group);
524   else
525     comdat = "";
526   streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
527
528   if (group)
529     {
530       if (node->same_comdat_group && !boundary_p)
531         {
532           ref = lto_symtab_encoder_lookup (encoder,
533                                            node->same_comdat_group);
534           gcc_assert (ref != LCC_NOT_FOUND);
535         }
536       else
537         ref = LCC_NOT_FOUND;
538       streamer_write_hwi_stream (ob->main_stream, ref);
539     }
540
541   section = node->get_section ();
542   if (!section)
543     section = "";
544
545   streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
546
547   bp = bitpack_create (ob->main_stream);
548   bp_pack_value (&bp, node->local.local, 1);
549   bp_pack_value (&bp, node->externally_visible, 1);
550   bp_pack_value (&bp, node->no_reorder, 1);
551   bp_pack_value (&bp, node->definition, 1);
552   bp_pack_value (&bp, node->local.versionable, 1);
553   bp_pack_value (&bp, node->local.can_change_signature, 1);
554   bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
555   bp_pack_value (&bp, node->force_output, 1);
556   bp_pack_value (&bp, node->forced_by_abi, 1);
557   bp_pack_value (&bp, node->unique_name, 1);
558   bp_pack_value (&bp, node->body_removed, 1);
559   bp_pack_value (&bp, node->implicit_section, 1);
560   bp_pack_value (&bp, node->address_taken, 1);
561   bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
562                  && node->get_partitioning_class () == SYMBOL_PARTITION
563                  && (reachable_from_other_partition_p (node, encoder)
564                      || referenced_from_other_partition_p (node, encoder)), 1);
565   bp_pack_value (&bp, node->lowered, 1);
566   bp_pack_value (&bp, in_other_partition, 1);
567   bp_pack_value (&bp, node->alias, 1);
568   bp_pack_value (&bp, node->weakref, 1);
569   bp_pack_value (&bp, node->frequency, 2);
570   bp_pack_value (&bp, node->only_called_at_startup, 1);
571   bp_pack_value (&bp, node->only_called_at_exit, 1);
572   bp_pack_value (&bp, node->tm_clone, 1);
573   bp_pack_value (&bp, node->calls_comdat_local, 1);
574   bp_pack_value (&bp, node->icf_merged, 1);
575   bp_pack_value (&bp, node->nonfreeing_fn, 1);
576   bp_pack_value (&bp, node->thunk.thunk_p, 1);
577   bp_pack_enum (&bp, ld_plugin_symbol_resolution,
578                 LDPR_NUM_KNOWN, node->resolution);
579   bp_pack_value (&bp, node->instrumentation_clone, 1);
580   streamer_write_bitpack (&bp);
581   streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
582
583   if (node->thunk.thunk_p)
584     {
585       streamer_write_uhwi_stream
586          (ob->main_stream,
587           1 + (node->thunk.this_adjusting != 0) * 2
588           + (node->thunk.virtual_offset_p != 0) * 4
589           + (node->thunk.add_pointer_bounds_args != 0) * 8);
590       streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
591       streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
592     }
593   streamer_write_hwi_stream (ob->main_stream, node->profile_id);
594   if (DECL_STATIC_CONSTRUCTOR (node->decl))
595     streamer_write_hwi_stream (ob->main_stream, node->get_init_priority ());
596   if (DECL_STATIC_DESTRUCTOR (node->decl))
597     streamer_write_hwi_stream (ob->main_stream, node->get_fini_priority ());
598
599   if (node->instrumentation_clone)
600     lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->orig_decl);
601 }
602
603 /* Output the varpool NODE to OB. 
604    If NODE is not in SET, then NODE is a boundary.  */
605
606 static void
607 lto_output_varpool_node (struct lto_simple_output_block *ob, varpool_node *node,
608                          lto_symtab_encoder_t encoder)
609 {
610   bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
611   struct bitpack_d bp;
612   int ref;
613   const char *comdat;
614   const char *section;
615   tree group;
616
617   streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
618                        LTO_symtab_variable);
619   streamer_write_hwi_stream (ob->main_stream, node->order);
620   lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
621   bp = bitpack_create (ob->main_stream);
622   bp_pack_value (&bp, node->externally_visible, 1);
623   bp_pack_value (&bp, node->no_reorder, 1);
624   bp_pack_value (&bp, node->force_output, 1);
625   bp_pack_value (&bp, node->forced_by_abi, 1);
626   bp_pack_value (&bp, node->unique_name, 1);
627   bp_pack_value (&bp, node->body_removed
628                  || !lto_symtab_encoder_encode_initializer_p (encoder, node), 1);
629   bp_pack_value (&bp, node->implicit_section, 1);
630   bp_pack_value (&bp, node->writeonly, 1);
631   bp_pack_value (&bp, node->definition, 1);
632   bp_pack_value (&bp, node->alias, 1);
633   bp_pack_value (&bp, node->weakref, 1);
634   bp_pack_value (&bp, node->analyzed && !boundary_p, 1);
635   gcc_assert (node->definition || !node->analyzed);
636   /* Constant pool initializers can be de-unified into individual ltrans units.
637      FIXME: Alternatively at -Os we may want to avoid generating for them the local
638      labels and share them across LTRANS partitions.  */
639   if (node->get_partitioning_class () != SYMBOL_PARTITION)
640     {
641       bp_pack_value (&bp, 0, 1);  /* used_from_other_parition.  */
642       bp_pack_value (&bp, 0, 1);  /* in_other_partition.  */
643     }
644   else
645     {
646       bp_pack_value (&bp, node->definition
647                      && referenced_from_other_partition_p (node, encoder), 1);
648       bp_pack_value (&bp, node->analyzed
649                      && boundary_p && !DECL_EXTERNAL (node->decl), 1);
650           /* in_other_partition.  */
651     }
652   bp_pack_value (&bp, node->tls_model, 3);
653   bp_pack_value (&bp, node->used_by_single_function, 1);
654   bp_pack_value (&bp, node->need_bounds_init, 1);
655   streamer_write_bitpack (&bp);
656
657   group = node->get_comdat_group ();
658   if (group)
659     comdat = IDENTIFIER_POINTER (group);
660   else
661     comdat = "";
662   streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
663
664   if (group)
665     {
666       if (node->same_comdat_group && !boundary_p)
667         {
668           ref = lto_symtab_encoder_lookup (encoder,
669                                            node->same_comdat_group);
670           gcc_assert (ref != LCC_NOT_FOUND);
671         }
672       else
673         ref = LCC_NOT_FOUND;
674       streamer_write_hwi_stream (ob->main_stream, ref);
675     }
676
677   section = node->get_section ();
678   if (!section)
679     section = "";
680   streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
681
682   streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
683                        LDPR_NUM_KNOWN, node->resolution);
684 }
685
686 /* Output the varpool NODE to OB. 
687    If NODE is not in SET, then NODE is a boundary.  */
688
689 static void
690 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
691                 lto_symtab_encoder_t encoder)
692 {
693   struct bitpack_d bp;
694   int nref;
695   int uid = ref->lto_stmt_uid;
696   struct cgraph_node *node;
697
698   bp = bitpack_create (ob->main_stream);
699   bp_pack_value (&bp, ref->use, 3);
700   bp_pack_value (&bp, ref->speculative, 1);
701   streamer_write_bitpack (&bp);
702   nref = lto_symtab_encoder_lookup (encoder, ref->referred);
703   gcc_assert (nref != LCC_NOT_FOUND);
704   streamer_write_hwi_stream (ob->main_stream, nref);
705   
706   node = dyn_cast <cgraph_node *> (ref->referring);
707   if (node)
708     {
709       if (ref->stmt)
710         uid = gimple_uid (ref->stmt) + 1;
711       streamer_write_hwi_stream (ob->main_stream, uid);
712     }
713 }
714
715 /* Stream out profile_summary to OB.  */
716
717 static void
718 output_profile_summary (struct lto_simple_output_block *ob)
719 {
720   unsigned h_ix;
721   struct bitpack_d bp;
722
723   if (profile_info)
724     {
725       /* We do not output num and run_max, they are not used by
726          GCC profile feedback and they are difficult to merge from multiple
727          units.  */
728       gcc_assert (profile_info->runs);
729       streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
730       streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
731
732       /* sum_all is needed for computing the working set with the
733          histogram.  */
734       streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
735
736       /* Create and output a bitpack of non-zero histogram entries indices.  */
737       bp = bitpack_create (ob->main_stream);
738       for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
739         bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
740       streamer_write_bitpack (&bp);
741       /* Now stream out only those non-zero entries.  */
742       for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
743         {
744           if (!profile_info->histogram[h_ix].num_counters)
745             continue;
746           streamer_write_gcov_count_stream (ob->main_stream,
747                                       profile_info->histogram[h_ix].num_counters);
748           streamer_write_gcov_count_stream (ob->main_stream,
749                                       profile_info->histogram[h_ix].min_value);
750           streamer_write_gcov_count_stream (ob->main_stream,
751                                       profile_info->histogram[h_ix].cum_value);
752          }
753       /* IPA-profile computes hot bb threshold based on cumulated
754          whole program profile.  We need to stream it down to ltrans.  */
755        if (flag_wpa)
756          streamer_write_gcov_count_stream (ob->main_stream,
757                                            get_hot_bb_threshold ());
758     }
759   else
760     streamer_write_uhwi_stream (ob->main_stream, 0);
761 }
762
763 /* Output all callees or indirect outgoing edges.  EDGE must be the first such
764    edge.  */
765
766 static void
767 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
768                               struct lto_simple_output_block *ob,
769                               lto_symtab_encoder_t encoder)
770 {
771   if (!edge)
772     return;
773
774   /* Output edges in backward direction, so the reconstructed callgraph match
775      and it is easy to associate call sites in the IPA pass summaries.  */
776   while (edge->next_callee)
777     edge = edge->next_callee;
778   for (; edge; edge = edge->prev_callee)
779     lto_output_edge (ob, edge, encoder);
780 }
781
782 /* Output the part of the cgraph in SET.  */
783
784 static void
785 output_refs (lto_symtab_encoder_t encoder)
786 {
787   struct lto_simple_output_block *ob;
788   int count;
789   struct ipa_ref *ref;
790
791   ob = lto_create_simple_output_block (LTO_section_refs);
792
793   for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
794     {
795       symtab_node *node = lto_symtab_encoder_deref (encoder, i);
796
797       if (!node->alias && !lto_symtab_encoder_in_partition_p (encoder, node))
798         continue;
799
800       count = node->ref_list.nreferences ();
801       if (count)
802         {
803           streamer_write_gcov_count_stream (ob->main_stream, count);
804           streamer_write_uhwi_stream (ob->main_stream,
805                                      lto_symtab_encoder_lookup (encoder, node));
806           for (int i = 0; node->iterate_reference (i, ref); i++)
807             lto_output_ref (ob, ref, encoder);
808         }
809     }
810
811   streamer_write_uhwi_stream (ob->main_stream, 0);
812
813   lto_destroy_simple_output_block (ob);
814 }
815
816 /* Add NODE into encoder as well as nodes it is cloned from.
817    Do it in a way so clones appear first.  */
818
819 static void
820 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
821              bool include_body)
822 {
823   if (node->clone_of)
824     add_node_to (encoder, node->clone_of, include_body);
825   else if (include_body)
826     lto_set_symtab_encoder_encode_body (encoder, node);
827   lto_symtab_encoder_encode (encoder, node);
828 }
829
830 /* Add all references in NODE to encoders.  */
831
832 static void
833 create_references (lto_symtab_encoder_t encoder, symtab_node *node)
834 {
835   int i;
836   struct ipa_ref *ref = NULL;
837   for (i = 0; node->iterate_reference (i, ref); i++)
838     if (is_a <cgraph_node *> (ref->referred))
839       add_node_to (encoder, dyn_cast <cgraph_node *> (ref->referred), false);
840     else
841       lto_symtab_encoder_encode (encoder, ref->referred);
842 }
843
844 /* Select what needs to be streamed out.  In regular lto mode stream everything.
845    In offload lto mode stream only nodes marked as offloadable.  */
846 void
847 select_what_to_stream (void)
848 {
849   struct symtab_node *snode;
850   FOR_EACH_SYMBOL (snode)
851     snode->need_lto_streaming = !lto_stream_offload_p || snode->offloadable;
852 }
853
854 /* Find all symbols we want to stream into given partition and insert them
855    to encoders.
856
857    The function actually replaces IN_ENCODER by new one.  The reason is that
858    streaming code needs clone's origin to be streamed before clone.  This
859    means that we need to insert the nodes in specific order.  This order is
860    ignored by the partitioning logic earlier.  */
861
862 lto_symtab_encoder_t 
863 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
864 {
865   struct cgraph_edge *edge;
866   int i;
867   lto_symtab_encoder_t encoder;
868   lto_symtab_encoder_iterator lsei;
869   hash_set<void *> reachable_call_targets;
870
871   encoder = lto_symtab_encoder_new (false);
872
873   /* Go over all entries in the IN_ENCODER and duplicate them to
874      ENCODER. At the same time insert masters of clones so
875      every master appears before clone.  */
876   for (lsei = lsei_start_function_in_partition (in_encoder);
877        !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
878     {
879       struct cgraph_node *node = lsei_cgraph_node (lsei);
880       if (!node->need_lto_streaming)
881         continue;
882       add_node_to (encoder, node, true);
883       lto_set_symtab_encoder_in_partition (encoder, node);
884       create_references (encoder, node);
885       /* For proper debug info, we need to ship the origins, too.  */
886       if (DECL_ABSTRACT_ORIGIN (node->decl))
887         {
888           struct cgraph_node *origin_node
889           = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (node->decl));
890           origin_node->used_as_abstract_origin = true;
891           add_node_to (encoder, origin_node, true);
892         }
893     }
894   for (lsei = lsei_start_variable_in_partition (in_encoder);
895        !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
896     {
897       varpool_node *vnode = lsei_varpool_node (lsei);
898
899       if (!vnode->need_lto_streaming)
900         continue;
901       lto_set_symtab_encoder_in_partition (encoder, vnode);
902       lto_set_symtab_encoder_encode_initializer (encoder, vnode);
903       create_references (encoder, vnode);
904       /* For proper debug info, we need to ship the origins, too.  */
905       if (DECL_ABSTRACT_ORIGIN (vnode->decl))
906         {
907           varpool_node *origin_node
908             = varpool_node::get (DECL_ABSTRACT_ORIGIN (vnode->decl));
909           lto_set_symtab_encoder_in_partition (encoder, origin_node);
910         }
911     }
912   /* Pickle in also the initializer of all referenced readonly variables
913      to help folding.  Constant pool variables are not shared, so we must
914      pickle those too.  */
915   for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
916     {
917       symtab_node *node = lto_symtab_encoder_deref (encoder, i);
918       if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
919         {
920           if (!lto_symtab_encoder_encode_initializer_p (encoder,
921                                                         vnode)
922               && (((vnode->ctor_useable_for_folding_p ()
923                    && (!DECL_VIRTUAL_P (vnode->decl)
924                        || !flag_wpa
925                        || flag_ltrans_devirtualize))
926                   || POINTER_BOUNDS_P (vnode->decl))))
927             {
928               lto_set_symtab_encoder_encode_initializer (encoder, vnode);
929               create_references (encoder, vnode);
930             }
931        }
932     }
933
934   /* Go over all the nodes again to include callees that are not in
935      SET.  */
936   for (lsei = lsei_start_function_in_partition (encoder);
937        !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
938     {
939       struct cgraph_node *node = lsei_cgraph_node (lsei);
940       for (edge = node->callees; edge; edge = edge->next_callee)
941         {
942           struct cgraph_node *callee = edge->callee;
943           if (!lto_symtab_encoder_in_partition_p (encoder, callee))
944             {
945               /* We should have moved all the inlines.  */
946               gcc_assert (!callee->global.inlined_to);
947               add_node_to (encoder, callee, false);
948             }
949         }
950       /* Add all possible targets for late devirtualization.  */
951       if (flag_ltrans_devirtualize || !flag_wpa)
952         for (edge = node->indirect_calls; edge; edge = edge->next_callee)
953           if (edge->indirect_info->polymorphic)
954             {
955               unsigned int i;
956               void *cache_token;
957               bool final;
958               vec <cgraph_node *>targets
959                 = possible_polymorphic_call_targets
960                     (edge, &final, &cache_token);
961               if (!reachable_call_targets.add (cache_token))
962                 {
963                   for (i = 0; i < targets.length (); i++)
964                     {
965                       struct cgraph_node *callee = targets[i];
966
967                       /* Adding an external declarations into the unit serves
968                          no purpose and just increases its boundary.  */
969                       if (callee->definition
970                           && !lto_symtab_encoder_in_partition_p
971                                (encoder, callee))
972                         {
973                           gcc_assert (!callee->global.inlined_to);
974                           add_node_to (encoder, callee, false);
975                         }
976                     }
977                 }
978             }
979     }
980   /* Be sure to also insert alias targert and thunk callees.  These needs
981      to stay to aid local calling conventions.  */
982   for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
983     {
984       symtab_node *node = lto_symtab_encoder_deref (encoder, i);
985       cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
986
987       if (node->alias && node->analyzed)
988         create_references (encoder, node);
989       if (cnode
990           && cnode->thunk.thunk_p)
991         add_node_to (encoder, cnode->callees->callee, false);
992     }
993   lto_symtab_encoder_delete (in_encoder);
994   return encoder;
995 }
996
997 /* Output the part of the symtab in SET and VSET.  */
998
999 void
1000 output_symtab (void)
1001 {
1002   struct cgraph_node *node;
1003   struct lto_simple_output_block *ob;
1004   int i, n_nodes;
1005   lto_symtab_encoder_t encoder;
1006
1007   if (flag_wpa)
1008     output_cgraph_opt_summary ();
1009
1010   ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
1011
1012   output_profile_summary (ob);
1013
1014   /* An encoder for cgraph nodes should have been created by
1015      ipa_write_summaries_1.  */
1016   gcc_assert (ob->decl_state->symtab_node_encoder);
1017   encoder = ob->decl_state->symtab_node_encoder;
1018
1019   /* Write out the nodes.  We must first output a node and then its clones,
1020      otherwise at a time reading back the node there would be nothing to clone
1021      from.  */
1022   n_nodes = lto_symtab_encoder_size (encoder);
1023   for (i = 0; i < n_nodes; i++)
1024     {
1025       symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1026       if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1027         lto_output_node (ob, cnode, encoder);
1028       else
1029         lto_output_varpool_node (ob, dyn_cast<varpool_node *> (node), encoder);
1030     }
1031
1032   /* Go over the nodes in SET again to write edges.  */
1033   for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
1034     {
1035       node = dyn_cast <cgraph_node *> (lto_symtab_encoder_deref (encoder, i));
1036       if (node
1037           && (node->thunk.thunk_p
1038               || lto_symtab_encoder_in_partition_p (encoder, node)))
1039         {
1040           output_outgoing_cgraph_edges (node->callees, ob, encoder);
1041           output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
1042         }
1043     }
1044
1045   streamer_write_uhwi_stream (ob->main_stream, 0);
1046
1047   lto_destroy_simple_output_block (ob);
1048
1049   /* Emit toplevel asms.
1050      When doing WPA we must output every asm just once.  Since we do not partition asm
1051      nodes at all, output them to first output.  This is kind of hack, but should work
1052      well.  */
1053   if (!asm_nodes_output)
1054     {
1055       asm_nodes_output = true;
1056       lto_output_toplevel_asms ();
1057     }
1058
1059   output_refs (encoder);
1060 }
1061
1062 /* Return identifier encoded in IB as a plain string.  */
1063
1064 static tree
1065 read_identifier (struct lto_input_block *ib)
1066 {
1067   unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1068   tree id;
1069
1070   if (ib->data[ib->p + len])
1071     lto_section_overrun (ib);
1072   if (!len)
1073     {
1074       ib->p++;
1075       return NULL;
1076     }
1077   id = get_identifier (ib->data + ib->p);
1078   ib->p += len + 1;
1079   return id;
1080 }
1081
1082 /* Return string encoded in IB, NULL if string is empty.  */
1083
1084 static const char *
1085 read_string (struct lto_input_block *ib)
1086 {
1087   unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1088   const char *str;
1089
1090   if (ib->data[ib->p + len])
1091     lto_section_overrun (ib);
1092   if (!len)
1093     {
1094       ib->p++;
1095       return NULL;
1096     }
1097   str = ib->data + ib->p;
1098   ib->p += len + 1;
1099   return str;
1100 }
1101
1102 /* Output function/variable tables that will allow libgomp to look up offload
1103    target code.
1104    OFFLOAD_FUNCS is filled in expand_omp_target, OFFLOAD_VARS is filled in
1105    varpool_node::get_create.  In WHOPR (partitioned) mode during the WPA stage
1106    both OFFLOAD_FUNCS and OFFLOAD_VARS are filled by input_offload_tables.  */
1107
1108 void
1109 output_offload_tables (void)
1110 {
1111   if (vec_safe_is_empty (offload_funcs) && vec_safe_is_empty (offload_vars))
1112     return;
1113
1114   struct lto_simple_output_block *ob
1115     = lto_create_simple_output_block (LTO_section_offload_table);
1116
1117   for (unsigned i = 0; i < vec_safe_length (offload_funcs); i++)
1118     {
1119       streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1120                            LTO_symtab_last_tag, LTO_symtab_unavail_node);
1121       lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
1122                                 (*offload_funcs)[i]);
1123     }
1124
1125   for (unsigned i = 0; i < vec_safe_length (offload_vars); i++)
1126     {
1127       streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1128                            LTO_symtab_last_tag, LTO_symtab_variable);
1129       lto_output_var_decl_index (ob->decl_state, ob->main_stream,
1130                                  (*offload_vars)[i]);
1131     }
1132
1133   streamer_write_uhwi_stream (ob->main_stream, 0);
1134   lto_destroy_simple_output_block (ob);
1135
1136   /* In WHOPR mode during the WPA stage the joint offload tables need to be
1137      streamed to one partition only.  That's why we free offload_funcs and
1138      offload_vars after the first call of output_offload_tables.  */
1139   if (flag_wpa)
1140     {
1141       vec_free (offload_funcs);
1142       vec_free (offload_vars);
1143     }
1144 }
1145
1146 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
1147    STACK_SIZE, SELF_TIME and SELF_SIZE.  This is called either to initialize
1148    NODE or to replace the values in it, for instance because the first
1149    time we saw it, the function body was not available but now it
1150    is.  BP is a bitpack with all the bitflags for NODE read from the
1151    stream.  */
1152
1153 static void
1154 input_overwrite_node (struct lto_file_decl_data *file_data,
1155                       struct cgraph_node *node,
1156                       enum LTO_symtab_tags tag,
1157                       struct bitpack_d *bp)
1158 {
1159   node->aux = (void *) tag;
1160   node->lto_file_data = file_data;
1161
1162   node->local.local = bp_unpack_value (bp, 1);
1163   node->externally_visible = bp_unpack_value (bp, 1);
1164   node->no_reorder = bp_unpack_value (bp, 1);
1165   node->definition = bp_unpack_value (bp, 1);
1166   node->local.versionable = bp_unpack_value (bp, 1);
1167   node->local.can_change_signature = bp_unpack_value (bp, 1);
1168   node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
1169   node->force_output = bp_unpack_value (bp, 1);
1170   node->forced_by_abi = bp_unpack_value (bp, 1);
1171   node->unique_name = bp_unpack_value (bp, 1);
1172   node->body_removed = bp_unpack_value (bp, 1);
1173   node->implicit_section = bp_unpack_value (bp, 1);
1174   node->address_taken = bp_unpack_value (bp, 1);
1175   node->used_from_other_partition = bp_unpack_value (bp, 1);
1176   node->lowered = bp_unpack_value (bp, 1);
1177   node->analyzed = tag == LTO_symtab_analyzed_node;
1178   node->in_other_partition = bp_unpack_value (bp, 1);
1179   if (node->in_other_partition
1180       /* Avoid updating decl when we are seeing just inline clone.
1181          When inlining function that has functions already inlined into it,
1182          we produce clones of inline clones.
1183
1184          WPA partitioning might put each clone into different unit and
1185          we might end up streaming inline clone from other partition
1186          to support clone we are interested in. */
1187       && (!node->clone_of
1188           || node->clone_of->decl != node->decl))
1189     {
1190       DECL_EXTERNAL (node->decl) = 1;
1191       TREE_STATIC (node->decl) = 0;
1192     }
1193   node->alias = bp_unpack_value (bp, 1);
1194   node->weakref = bp_unpack_value (bp, 1);
1195   node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
1196   node->only_called_at_startup = bp_unpack_value (bp, 1);
1197   node->only_called_at_exit = bp_unpack_value (bp, 1);
1198   node->tm_clone = bp_unpack_value (bp, 1);
1199   node->calls_comdat_local = bp_unpack_value (bp, 1);
1200   node->icf_merged = bp_unpack_value (bp, 1);
1201   node->nonfreeing_fn = bp_unpack_value (bp, 1);
1202   node->thunk.thunk_p = bp_unpack_value (bp, 1);
1203   node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
1204                                      LDPR_NUM_KNOWN);
1205   node->instrumentation_clone = bp_unpack_value (bp, 1);
1206   gcc_assert (flag_ltrans
1207               || (!node->in_other_partition
1208                   && !node->used_from_other_partition));
1209 }
1210
1211 /* Return string alias is alias of.  */
1212
1213 static tree
1214 get_alias_symbol (tree decl)
1215 {
1216   tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1217   return get_identifier (TREE_STRING_POINTER
1218                           (TREE_VALUE (TREE_VALUE (alias))));
1219 }
1220
1221 /* Read a node from input_block IB.  TAG is the node's tag just read.
1222    Return the node read or overwriten.  */
1223
1224 static struct cgraph_node *
1225 input_node (struct lto_file_decl_data *file_data,
1226             struct lto_input_block *ib,
1227             enum LTO_symtab_tags tag,
1228             vec<symtab_node *> nodes)
1229 {
1230   gcc::pass_manager *passes = g->get_passes ();
1231   tree fn_decl;
1232   struct cgraph_node *node;
1233   struct bitpack_d bp;
1234   unsigned decl_index;
1235   int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1236   int clone_ref;
1237   int order;
1238   int i, count;
1239   tree group;
1240   const char *section;
1241   order = streamer_read_hwi (ib) + order_base;
1242   clone_ref = streamer_read_hwi (ib);
1243
1244   decl_index = streamer_read_uhwi (ib);
1245   fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1246
1247   if (clone_ref != LCC_NOT_FOUND)
1248     {
1249       node = dyn_cast<cgraph_node *> (nodes[clone_ref])->create_clone (fn_decl,
1250         0, CGRAPH_FREQ_BASE, false,
1251         vNULL, false, NULL, NULL);
1252     }
1253   else
1254     {
1255       /* Declaration of functions can be already merged with a declaration
1256          from other input file.  We keep cgraph unmerged until after streaming
1257          of ipa passes is done.  Alays forcingly create a fresh node.  */
1258       node = symtab->create_empty ();
1259       node->decl = fn_decl;
1260       node->register_symbol ();
1261     }
1262
1263   node->order = order;
1264   if (order >= symtab->order)
1265     symtab->order = order + 1;
1266
1267   node->count = streamer_read_gcov_count (ib);
1268   node->count_materialization_scale = streamer_read_hwi (ib);
1269
1270   count = streamer_read_hwi (ib);
1271   node->ipa_transforms_to_apply = vNULL;
1272   for (i = 0; i < count; i++)
1273     {
1274       opt_pass *pass;
1275       int pid = streamer_read_hwi (ib);
1276
1277       gcc_assert (pid < passes->passes_by_id_size);
1278       pass = passes->passes_by_id[pid];
1279       node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *) pass);
1280     }
1281
1282   if (tag == LTO_symtab_analyzed_node)
1283     ref = streamer_read_hwi (ib);
1284
1285   group = read_identifier (ib);
1286   if (group)
1287     ref2 = streamer_read_hwi (ib);
1288
1289   /* Make sure that we have not read this node before.  Nodes that
1290      have already been read will have their tag stored in the 'aux'
1291      field.  Since built-in functions can be referenced in multiple
1292      functions, they are expected to be read more than once.  */
1293   if (node->aux && !DECL_BUILT_IN (node->decl))
1294     internal_error ("bytecode stream: found multiple instances of cgraph "
1295                     "node with uid %d", node->uid);
1296
1297   node->tp_first_run = streamer_read_uhwi (ib);
1298
1299   bp = streamer_read_bitpack (ib);
1300
1301   input_overwrite_node (file_data, node, tag, &bp);
1302
1303   /* Store a reference for now, and fix up later to be a pointer.  */
1304   node->global.inlined_to = (cgraph_node *) (intptr_t) ref;
1305
1306   if (group)
1307     {
1308       node->set_comdat_group (group);
1309       /* Store a reference for now, and fix up later to be a pointer.  */
1310       node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
1311     }
1312   else
1313     node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1314   section = read_string (ib);
1315   if (section)
1316     node->set_section_for_node (section);
1317
1318   if (node->thunk.thunk_p)
1319     {
1320       int type = streamer_read_uhwi (ib);
1321       HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1322       HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1323
1324       node->thunk.fixed_offset = fixed_offset;
1325       node->thunk.this_adjusting = (type & 2);
1326       node->thunk.virtual_value = virtual_value;
1327       node->thunk.virtual_offset_p = (type & 4);
1328       node->thunk.add_pointer_bounds_args = (type & 8);
1329     }
1330   if (node->alias && !node->analyzed && node->weakref)
1331     node->alias_target = get_alias_symbol (node->decl);
1332   node->profile_id = streamer_read_hwi (ib);
1333   if (DECL_STATIC_CONSTRUCTOR (node->decl))
1334     node->set_init_priority (streamer_read_hwi (ib));
1335   if (DECL_STATIC_DESTRUCTOR (node->decl))
1336     node->set_fini_priority (streamer_read_hwi (ib));
1337
1338   if (node->instrumentation_clone)
1339     {
1340       decl_index = streamer_read_uhwi (ib);
1341       fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1342       node->orig_decl = fn_decl;
1343     }
1344
1345   return node;
1346 }
1347
1348 /* Read a node from input_block IB.  TAG is the node's tag just read.
1349    Return the node read or overwriten.  */
1350
1351 static varpool_node *
1352 input_varpool_node (struct lto_file_decl_data *file_data,
1353                     struct lto_input_block *ib)
1354 {
1355   int decl_index;
1356   tree var_decl;
1357   varpool_node *node;
1358   struct bitpack_d bp;
1359   int ref = LCC_NOT_FOUND;
1360   int order;
1361   tree group;
1362   const char *section;
1363
1364   order = streamer_read_hwi (ib) + order_base;
1365   decl_index = streamer_read_uhwi (ib);
1366   var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1367
1368   /* Declaration of functions can be already merged with a declaration
1369      from other input file.  We keep cgraph unmerged until after streaming
1370      of ipa passes is done.  Alays forcingly create a fresh node.  */
1371   node = varpool_node::create_empty ();
1372   node->decl = var_decl;
1373   node->register_symbol ();
1374
1375   node->order = order;
1376   if (order >= symtab->order)
1377     symtab->order = order + 1;
1378   node->lto_file_data = file_data;
1379
1380   bp = streamer_read_bitpack (ib);
1381   node->externally_visible = bp_unpack_value (&bp, 1);
1382   node->no_reorder = bp_unpack_value (&bp, 1);
1383   node->force_output = bp_unpack_value (&bp, 1);
1384   node->forced_by_abi = bp_unpack_value (&bp, 1);
1385   node->unique_name = bp_unpack_value (&bp, 1);
1386   node->body_removed = bp_unpack_value (&bp, 1);
1387   node->implicit_section = bp_unpack_value (&bp, 1);
1388   node->writeonly = bp_unpack_value (&bp, 1);
1389   node->definition = bp_unpack_value (&bp, 1);
1390   node->alias = bp_unpack_value (&bp, 1);
1391   node->weakref = bp_unpack_value (&bp, 1);
1392   node->analyzed = bp_unpack_value (&bp, 1);
1393   node->used_from_other_partition = bp_unpack_value (&bp, 1);
1394   node->in_other_partition = bp_unpack_value (&bp, 1);
1395   if (node->in_other_partition)
1396     {
1397       DECL_EXTERNAL (node->decl) = 1;
1398       TREE_STATIC (node->decl) = 0;
1399     }
1400   if (node->alias && !node->analyzed && node->weakref)
1401     node->alias_target = get_alias_symbol (node->decl);
1402   node->tls_model = (enum tls_model)bp_unpack_value (&bp, 3);
1403   node->used_by_single_function = (enum tls_model)bp_unpack_value (&bp, 1);
1404   node->need_bounds_init = bp_unpack_value (&bp, 1);
1405   group = read_identifier (ib);
1406   if (group)
1407     {
1408       node->set_comdat_group (group);
1409       ref = streamer_read_hwi (ib);
1410       /* Store a reference for now, and fix up later to be a pointer.  */
1411       node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1412     }
1413   else
1414     node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1415   section = read_string (ib);
1416   if (section)
1417     node->set_section_for_node (section);
1418   node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1419                                                 LDPR_NUM_KNOWN);
1420   gcc_assert (flag_ltrans
1421               || (!node->in_other_partition
1422                   && !node->used_from_other_partition));
1423
1424   return node;
1425 }
1426
1427 /* Read a node from input_block IB.  TAG is the node's tag just read.
1428    Return the node read or overwriten.  */
1429
1430 static void
1431 input_ref (struct lto_input_block *ib,
1432            symtab_node *referring_node,
1433            vec<symtab_node *> nodes)
1434 {
1435   symtab_node *node = NULL;
1436   struct bitpack_d bp;
1437   enum ipa_ref_use use;
1438   bool speculative;
1439   struct ipa_ref *ref;
1440
1441   bp = streamer_read_bitpack (ib);
1442   use = (enum ipa_ref_use) bp_unpack_value (&bp, 3);
1443   speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1444   node = nodes[streamer_read_hwi (ib)];
1445   ref = referring_node->create_reference (node, use);
1446   ref->speculative = speculative;
1447   if (is_a <cgraph_node *> (referring_node))
1448     ref->lto_stmt_uid = streamer_read_hwi (ib);
1449 }
1450
1451 /* Read an edge from IB.  NODES points to a vector of previously read nodes for
1452    decoding caller and callee of the edge to be read.  If INDIRECT is true, the
1453    edge being read is indirect (in the sense that it has
1454    indirect_unknown_callee set).  */
1455
1456 static void
1457 input_edge (struct lto_input_block *ib, vec<symtab_node *> nodes,
1458             bool indirect)
1459 {
1460   struct cgraph_node *caller, *callee;
1461   struct cgraph_edge *edge;
1462   unsigned int stmt_id;
1463   gcov_type count;
1464   int freq;
1465   cgraph_inline_failed_t inline_failed;
1466   struct bitpack_d bp;
1467   int ecf_flags = 0;
1468
1469   caller = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1470   if (caller == NULL || caller->decl == NULL_TREE)
1471     internal_error ("bytecode stream: no caller found while reading edge");
1472
1473   if (!indirect)
1474     {
1475       callee = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1476       if (callee == NULL || callee->decl == NULL_TREE)
1477         internal_error ("bytecode stream: no callee found while reading edge");
1478     }
1479   else
1480     callee = NULL;
1481
1482   count = streamer_read_gcov_count (ib);
1483
1484   bp = streamer_read_bitpack (ib);
1485   inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_t, CIF_N_REASONS);
1486   stmt_id = bp_unpack_var_len_unsigned (&bp);
1487   freq = (int) bp_unpack_var_len_unsigned (&bp);
1488
1489   if (indirect)
1490     edge = caller->create_indirect_edge (NULL, 0, count, freq);
1491   else
1492     edge = caller->create_edge (callee, NULL, count, freq);
1493
1494   edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1495   edge->speculative = bp_unpack_value (&bp, 1);
1496   edge->lto_stmt_uid = stmt_id;
1497   edge->inline_failed = inline_failed;
1498   edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1499   edge->can_throw_external = bp_unpack_value (&bp, 1);
1500   edge->in_polymorphic_cdtor = bp_unpack_value (&bp, 1);
1501   if (indirect)
1502     {
1503       if (bp_unpack_value (&bp, 1))
1504         ecf_flags |= ECF_CONST;
1505       if (bp_unpack_value (&bp, 1))
1506         ecf_flags |= ECF_PURE;
1507       if (bp_unpack_value (&bp, 1))
1508         ecf_flags |= ECF_NORETURN;
1509       if (bp_unpack_value (&bp, 1))
1510         ecf_flags |= ECF_MALLOC;
1511       if (bp_unpack_value (&bp, 1))
1512         ecf_flags |= ECF_NOTHROW;
1513       if (bp_unpack_value (&bp, 1))
1514         ecf_flags |= ECF_RETURNS_TWICE;
1515       edge->indirect_info->ecf_flags = ecf_flags;
1516       edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1517       if (edge->indirect_info->common_target_id)
1518         edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1519     }
1520 }
1521
1522
1523 /* Read a cgraph from IB using the info in FILE_DATA.  */
1524
1525 static vec<symtab_node *> 
1526 input_cgraph_1 (struct lto_file_decl_data *file_data,
1527                 struct lto_input_block *ib)
1528 {
1529   enum LTO_symtab_tags tag;
1530   vec<symtab_node *> nodes = vNULL;
1531   symtab_node *node;
1532   unsigned i;
1533
1534   tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1535   order_base = symtab->order;
1536   while (tag)
1537     {
1538       if (tag == LTO_symtab_edge)
1539         input_edge (ib, nodes, false);
1540       else if (tag == LTO_symtab_indirect_edge)
1541         input_edge (ib, nodes, true);
1542       else if (tag == LTO_symtab_variable)
1543         {
1544           node = input_varpool_node (file_data, ib);
1545           nodes.safe_push (node);
1546           lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1547         }
1548       else
1549         {
1550           node = input_node (file_data, ib, tag, nodes);
1551           if (node == NULL || node->decl == NULL_TREE)
1552             internal_error ("bytecode stream: found empty cgraph node");
1553           nodes.safe_push (node);
1554           lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1555         }
1556
1557       tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1558     }
1559
1560   lto_input_toplevel_asms (file_data, order_base);
1561
1562   /* AUX pointers should be all non-zero for function nodes read from the stream.  */
1563 #ifdef ENABLE_CHECKING
1564   FOR_EACH_VEC_ELT (nodes, i, node)
1565     gcc_assert (node->aux || !is_a <cgraph_node *> (node));
1566 #endif
1567   FOR_EACH_VEC_ELT (nodes, i, node)
1568     {
1569       int ref;
1570       if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1571         {
1572           ref = (int) (intptr_t) cnode->global.inlined_to;
1573
1574           /* We share declaration of builtins, so we may read same node twice.  */
1575           if (!node->aux)
1576             continue;
1577           node->aux = NULL;
1578
1579           /* Fixup inlined_to from reference to pointer.  */
1580           if (ref != LCC_NOT_FOUND)
1581             dyn_cast<cgraph_node *> (node)->global.inlined_to
1582               = dyn_cast<cgraph_node *> (nodes[ref]);
1583           else
1584             cnode->global.inlined_to = NULL;
1585
1586           /* Compute instrumented_version.  */
1587           if (cnode->instrumentation_clone)
1588             {
1589               gcc_assert (cnode->orig_decl);
1590
1591               cnode->instrumented_version = cgraph_node::get (cnode->orig_decl);
1592               if (cnode->instrumented_version)
1593                 {
1594                   /* We may have multiple nodes for a single function which
1595                      will be merged later.  To have a proper merge we need
1596                      to keep instrumentation_version reference between nodes
1597                      consistent: each instrumented_version reference should
1598                      have proper reverse reference.  Thus don't break existing
1599                      instrumented_version reference if it already exists.  */
1600                   if (cnode->instrumented_version->instrumented_version)
1601                     cnode->instrumented_version = NULL;
1602                   else
1603                     cnode->instrumented_version->instrumented_version = cnode;
1604                 }
1605
1606               /* Restore decl names reference.  */
1607               if (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (cnode->decl))
1608                   && !TREE_CHAIN (DECL_ASSEMBLER_NAME (cnode->decl)))
1609                 TREE_CHAIN (DECL_ASSEMBLER_NAME (cnode->decl))
1610                   = DECL_ASSEMBLER_NAME (cnode->orig_decl);
1611             }
1612         }
1613
1614       ref = (int) (intptr_t) node->same_comdat_group;
1615
1616       /* Fixup same_comdat_group from reference to pointer.  */
1617       if (ref != LCC_NOT_FOUND)
1618         node->same_comdat_group = nodes[ref];
1619       else
1620         node->same_comdat_group = NULL;
1621     }
1622   FOR_EACH_VEC_ELT (nodes, i, node)
1623     node->aux = is_a <cgraph_node *> (node) ? (void *)1 : NULL;
1624   return nodes;
1625 }
1626
1627 /* Input ipa_refs.  */
1628
1629 static void
1630 input_refs (struct lto_input_block *ib,
1631             vec<symtab_node *> nodes)
1632 {
1633   int count;
1634   int idx;
1635   while (true)
1636     {
1637       symtab_node *node;
1638       count = streamer_read_uhwi (ib);
1639       if (!count)
1640         break;
1641       idx = streamer_read_uhwi (ib);
1642       node = nodes[idx];
1643       while (count)
1644         {
1645           input_ref (ib, node, nodes);
1646           count--;
1647         }
1648     }
1649 }
1650             
1651
1652 static struct gcov_ctr_summary lto_gcov_summary;
1653
1654 /* Input profile_info from IB.  */
1655 static void
1656 input_profile_summary (struct lto_input_block *ib,
1657                        struct lto_file_decl_data *file_data)
1658 {
1659   unsigned h_ix;
1660   struct bitpack_d bp;
1661   unsigned int runs = streamer_read_uhwi (ib);
1662   if (runs)
1663     {
1664       file_data->profile_info.runs = runs;
1665       file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1666       file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1667
1668       memset (file_data->profile_info.histogram, 0,
1669               sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1670       /* Input the bitpack of non-zero histogram indices.  */
1671       bp = streamer_read_bitpack (ib);
1672       /* Read in and unpack the full bitpack, flagging non-zero
1673          histogram entries by setting the num_counters non-zero.  */
1674       for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1675         {
1676           file_data->profile_info.histogram[h_ix].num_counters
1677               = bp_unpack_value (&bp, 1);
1678         }
1679       for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1680         {
1681           if (!file_data->profile_info.histogram[h_ix].num_counters)
1682             continue;
1683
1684           file_data->profile_info.histogram[h_ix].num_counters
1685               = streamer_read_gcov_count (ib);
1686           file_data->profile_info.histogram[h_ix].min_value
1687               = streamer_read_gcov_count (ib);
1688           file_data->profile_info.histogram[h_ix].cum_value
1689               = streamer_read_gcov_count (ib);
1690         }
1691       /* IPA-profile computes hot bb threshold based on cumulated
1692          whole program profile.  We need to stream it down to ltrans.  */
1693       if (flag_ltrans)
1694         set_hot_bb_threshold (streamer_read_gcov_count (ib));
1695     }
1696
1697 }
1698
1699 /* Rescale profile summaries to the same number of runs in the whole unit.  */
1700
1701 static void
1702 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1703 {
1704   struct lto_file_decl_data *file_data;
1705   unsigned int j, h_ix;
1706   gcov_unsigned_t max_runs = 0;
1707   struct cgraph_node *node;
1708   struct cgraph_edge *edge;
1709   gcov_type saved_sum_all = 0;
1710   gcov_ctr_summary *saved_profile_info = 0;
1711   int saved_scale = 0;
1712
1713   /* Find unit with maximal number of runs.  If we ever get serious about
1714      roundoff errors, we might also consider computing smallest common
1715      multiply.  */
1716   for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1717     if (max_runs < file_data->profile_info.runs)
1718       max_runs = file_data->profile_info.runs;
1719
1720   if (!max_runs)
1721     return;
1722
1723   /* Simple overflow check.  We probably don't need to support that many train
1724      runs. Such a large value probably imply data corruption anyway.  */
1725   if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1726     {
1727       sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1728              INT_MAX / REG_BR_PROB_BASE);
1729       return;
1730     }
1731
1732   profile_info = &lto_gcov_summary;
1733   lto_gcov_summary.runs = max_runs;
1734   lto_gcov_summary.sum_max = 0;
1735   memset (lto_gcov_summary.histogram, 0,
1736           sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1737
1738   /* Rescale all units to the maximal number of runs.
1739      sum_max can not be easily merged, as we have no idea what files come from
1740      the same run.  We do not use the info anyway, so leave it 0.  */
1741   for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1742     if (file_data->profile_info.runs)
1743       {
1744         int scale = GCOV_COMPUTE_SCALE (max_runs,
1745                                         file_data->profile_info.runs);
1746         lto_gcov_summary.sum_max
1747             = MAX (lto_gcov_summary.sum_max,
1748                    apply_scale (file_data->profile_info.sum_max, scale));
1749         lto_gcov_summary.sum_all
1750             = MAX (lto_gcov_summary.sum_all,
1751                    apply_scale (file_data->profile_info.sum_all, scale));
1752         /* Save a pointer to the profile_info with the largest
1753            scaled sum_all and the scale for use in merging the
1754            histogram.  */
1755         if (!saved_profile_info
1756             || lto_gcov_summary.sum_all > saved_sum_all)
1757           {
1758             saved_profile_info = &file_data->profile_info;
1759             saved_sum_all = lto_gcov_summary.sum_all;
1760             saved_scale = scale;
1761           }
1762       }
1763
1764   gcc_assert (saved_profile_info);
1765
1766   /* Scale up the histogram from the profile that had the largest
1767      scaled sum_all above.  */
1768   for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1769     {
1770       /* Scale up the min value as we did the corresponding sum_all
1771          above. Use that to find the new histogram index.  */
1772       gcov_type scaled_min
1773           = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1774                          saved_scale);
1775       /* The new index may be shared with another scaled histogram entry,
1776          so we need to account for a non-zero histogram entry at new_ix.  */
1777       unsigned new_ix = gcov_histo_index (scaled_min);
1778       lto_gcov_summary.histogram[new_ix].min_value
1779           = (lto_gcov_summary.histogram[new_ix].num_counters
1780              ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1781              : scaled_min);
1782       /* Some of the scaled counter values would ostensibly need to be placed
1783          into different (larger) histogram buckets, but we keep things simple
1784          here and place the scaled cumulative counter value in the bucket
1785          corresponding to the scaled minimum counter value.  */
1786       lto_gcov_summary.histogram[new_ix].cum_value
1787           += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1788                           saved_scale);
1789       lto_gcov_summary.histogram[new_ix].num_counters
1790           += saved_profile_info->histogram[h_ix].num_counters;
1791     }
1792
1793   /* Watch roundoff errors.  */
1794   if (lto_gcov_summary.sum_max < max_runs)
1795     lto_gcov_summary.sum_max = max_runs;
1796
1797   /* If merging already happent at WPA time, we are done.  */
1798   if (flag_ltrans)
1799     return;
1800
1801   /* Now compute count_materialization_scale of each node.
1802      During LTRANS we already have values of count_materialization_scale
1803      computed, so just update them.  */
1804   FOR_EACH_FUNCTION (node)
1805     if (node->lto_file_data
1806         && node->lto_file_data->profile_info.runs)
1807       {
1808         int scale;
1809
1810         scale = RDIV (node->count_materialization_scale * max_runs,
1811                       node->lto_file_data->profile_info.runs);
1812         node->count_materialization_scale = scale;
1813         if (scale < 0)
1814           fatal_error (input_location, "Profile information in %s corrupted",
1815                        file_data->file_name);
1816
1817         if (scale == REG_BR_PROB_BASE)
1818           continue;
1819         for (edge = node->callees; edge; edge = edge->next_callee)
1820           edge->count = apply_scale (edge->count, scale);
1821         node->count = apply_scale (node->count, scale);
1822       }
1823 }
1824
1825 /* Input and merge the symtab from each of the .o files passed to
1826    lto1.  */
1827
1828 void
1829 input_symtab (void)
1830 {
1831   struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1832   struct lto_file_decl_data *file_data;
1833   unsigned int j = 0;
1834   struct cgraph_node *node;
1835
1836   while ((file_data = file_data_vec[j++]))
1837     {
1838       const char *data;
1839       size_t len;
1840       struct lto_input_block *ib;
1841       vec<symtab_node *> nodes;
1842
1843       ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1844                                           &data, &len);
1845       if (!ib) 
1846         fatal_error (input_location,
1847                      "cannot find LTO cgraph in %s", file_data->file_name);
1848       input_profile_summary (ib, file_data);
1849       file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1850       nodes = input_cgraph_1 (file_data, ib);
1851       lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1852                                       ib, data, len);
1853
1854       ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1855                                           &data, &len);
1856       if (!ib)
1857         fatal_error (input_location, "cannot find LTO section refs in %s",
1858                      file_data->file_name);
1859       input_refs (ib, nodes);
1860       lto_destroy_simple_input_block (file_data, LTO_section_refs,
1861                                       ib, data, len);
1862       if (flag_ltrans)
1863         input_cgraph_opt_summary (nodes);
1864       nodes.release ();
1865     }
1866
1867   merge_profile_summaries (file_data_vec);
1868   get_working_sets ();
1869
1870
1871   /* Clear out the aux field that was used to store enough state to
1872      tell which nodes should be overwritten.  */
1873   FOR_EACH_FUNCTION (node)
1874     {
1875       /* Some nodes may have been created by cgraph_node.  This
1876          happens when the callgraph contains nested functions.  If the
1877          node for the parent function was never emitted to the gimple
1878          file, cgraph_node will create a node for it when setting the
1879          context of the nested function.  */
1880       if (node->lto_file_data)
1881         node->aux = NULL;
1882     }
1883 }
1884
1885 /* Input function/variable tables that will allow libgomp to look up offload
1886    target code, and store them into OFFLOAD_FUNCS and OFFLOAD_VARS.  */
1887
1888 void
1889 input_offload_tables (void)
1890 {
1891   struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1892   struct lto_file_decl_data *file_data;
1893   unsigned int j = 0;
1894
1895   while ((file_data = file_data_vec[j++]))
1896     {
1897       const char *data;
1898       size_t len;
1899       struct lto_input_block *ib
1900         = lto_create_simple_input_block (file_data, LTO_section_offload_table,
1901                                          &data, &len);
1902       if (!ib)
1903         continue;
1904
1905       enum LTO_symtab_tags tag
1906         = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1907       while (tag)
1908         {
1909           if (tag == LTO_symtab_unavail_node)
1910             {
1911               int decl_index = streamer_read_uhwi (ib);
1912               tree fn_decl
1913                 = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1914               vec_safe_push (offload_funcs, fn_decl);
1915             }
1916           else if (tag == LTO_symtab_variable)
1917             {
1918               int decl_index = streamer_read_uhwi (ib);
1919               tree var_decl
1920                 = lto_file_decl_data_get_var_decl (file_data, decl_index);
1921               vec_safe_push (offload_vars, var_decl);
1922             }
1923           else
1924             fatal_error (input_location,
1925                          "invalid offload table in %s", file_data->file_name);
1926
1927           tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1928         }
1929
1930       lto_destroy_simple_input_block (file_data, LTO_section_offload_table,
1931                                       ib, data, len);
1932     }
1933 }
1934
1935 /* True when we need optimization summary for NODE.  */
1936
1937 static int
1938 output_cgraph_opt_summary_p (struct cgraph_node *node)
1939 {
1940   return (node->clone_of
1941           && (node->clone.tree_map
1942               || node->clone.args_to_skip
1943               || node->clone.combined_args_to_skip));
1944 }
1945
1946 /* Output optimization summary for EDGE to OB.  */
1947 static void
1948 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1949                          struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1950 {
1951 }
1952
1953 /* Output optimization summary for NODE to OB.  */
1954
1955 static void
1956 output_node_opt_summary (struct output_block *ob,
1957                          struct cgraph_node *node,
1958                          lto_symtab_encoder_t encoder)
1959 {
1960   unsigned int index;
1961   bitmap_iterator bi;
1962   struct ipa_replace_map *map;
1963   struct bitpack_d bp;
1964   int i;
1965   struct cgraph_edge *e;
1966
1967   if (node->clone.args_to_skip)
1968     {
1969       streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1970       EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1971         streamer_write_uhwi (ob, index);
1972     }
1973   else
1974     streamer_write_uhwi (ob, 0);
1975   if (node->clone.combined_args_to_skip)
1976     {
1977       streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1978       EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1979         streamer_write_uhwi (ob, index);
1980     }
1981   else
1982     streamer_write_uhwi (ob, 0);
1983   streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1984   FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1985     {
1986       /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1987          mechanism to store function local declarations into summaries.  */
1988       gcc_assert (!map->old_tree);
1989       streamer_write_uhwi (ob, map->parm_num);
1990       gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1991       stream_write_tree (ob, map->new_tree, true);
1992       bp = bitpack_create (ob->main_stream);
1993       bp_pack_value (&bp, map->replace_p, 1);
1994       bp_pack_value (&bp, map->ref_p, 1);
1995       streamer_write_bitpack (&bp);
1996     }
1997
1998   if (lto_symtab_encoder_in_partition_p (encoder, node))
1999     {
2000       for (e = node->callees; e; e = e->next_callee)
2001         output_edge_opt_summary (ob, e);
2002       for (e = node->indirect_calls; e; e = e->next_callee)
2003         output_edge_opt_summary (ob, e);
2004     }
2005 }
2006
2007 /* Output optimization summaries stored in callgraph.
2008    At the moment it is the clone info structure.  */
2009
2010 static void
2011 output_cgraph_opt_summary (void)
2012 {
2013   int i, n_nodes;
2014   lto_symtab_encoder_t encoder;
2015   struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
2016   unsigned count = 0;
2017
2018   ob->symbol = NULL;
2019   encoder = ob->decl_state->symtab_node_encoder;
2020   n_nodes = lto_symtab_encoder_size (encoder);
2021   for (i = 0; i < n_nodes; i++)
2022     {
2023       symtab_node *node = lto_symtab_encoder_deref (encoder, i);
2024       cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2025       if (cnode && output_cgraph_opt_summary_p (cnode))
2026         count++;
2027     }
2028   streamer_write_uhwi (ob, count);
2029   for (i = 0; i < n_nodes; i++)
2030     {
2031       symtab_node *node = lto_symtab_encoder_deref (encoder, i);
2032       cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2033       if (cnode && output_cgraph_opt_summary_p (cnode))
2034         {
2035           streamer_write_uhwi (ob, i);
2036           output_node_opt_summary (ob, cnode, encoder);
2037         }
2038     }
2039   produce_asm (ob, NULL);
2040   destroy_output_block (ob);
2041 }
2042
2043 /* Input optimisation summary of EDGE.  */
2044
2045 static void
2046 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
2047                         struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
2048 {
2049 }
2050
2051 /* Input optimisation summary of NODE.  */
2052
2053 static void
2054 input_node_opt_summary (struct cgraph_node *node,
2055                         struct lto_input_block *ib_main,
2056                         struct data_in *data_in)
2057 {
2058   int i;
2059   int count;
2060   int bit;
2061   struct bitpack_d bp;
2062   struct cgraph_edge *e;
2063
2064   count = streamer_read_uhwi (ib_main);
2065   if (count)
2066     node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
2067   for (i = 0; i < count; i++)
2068     {
2069       bit = streamer_read_uhwi (ib_main);
2070       bitmap_set_bit (node->clone.args_to_skip, bit);
2071     }
2072   count = streamer_read_uhwi (ib_main);
2073   if (count)
2074     node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
2075   for (i = 0; i < count; i++)
2076     {
2077       bit = streamer_read_uhwi (ib_main);
2078       bitmap_set_bit (node->clone.combined_args_to_skip, bit);
2079     }
2080   count = streamer_read_uhwi (ib_main);
2081   for (i = 0; i < count; i++)
2082     {
2083       struct ipa_replace_map *map = ggc_alloc<ipa_replace_map> ();
2084
2085       vec_safe_push (node->clone.tree_map, map);
2086       map->parm_num = streamer_read_uhwi (ib_main);
2087       map->old_tree = NULL;
2088       map->new_tree = stream_read_tree (ib_main, data_in);
2089       bp = streamer_read_bitpack (ib_main);
2090       map->replace_p = bp_unpack_value (&bp, 1);
2091       map->ref_p = bp_unpack_value (&bp, 1);
2092     }
2093   for (e = node->callees; e; e = e->next_callee)
2094     input_edge_opt_summary (e, ib_main);
2095   for (e = node->indirect_calls; e; e = e->next_callee)
2096     input_edge_opt_summary (e, ib_main);
2097 }
2098
2099 /* Read section in file FILE_DATA of length LEN with data DATA.  */
2100
2101 static void
2102 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
2103                           const char *data, size_t len,
2104                           vec<symtab_node *> nodes)
2105 {
2106   const struct lto_function_header *header =
2107     (const struct lto_function_header *) data;
2108   const int cfg_offset = sizeof (struct lto_function_header);
2109   const int main_offset = cfg_offset + header->cfg_size;
2110   const int string_offset = main_offset + header->main_size;
2111   struct data_in *data_in;
2112   unsigned int i;
2113   unsigned int count;
2114
2115   lto_input_block ib_main ((const char *) data + main_offset,
2116                            header->main_size);
2117
2118   data_in =
2119     lto_data_in_create (file_data, (const char *) data + string_offset,
2120                         header->string_size, vNULL);
2121   count = streamer_read_uhwi (&ib_main);
2122
2123   for (i = 0; i < count; i++)
2124     {
2125       int ref = streamer_read_uhwi (&ib_main);
2126       input_node_opt_summary (dyn_cast<cgraph_node *> (nodes[ref]),
2127                               &ib_main, data_in);
2128     }
2129   lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
2130                          len);
2131   lto_data_in_delete (data_in);
2132 }
2133
2134 /* Input optimization summary of cgraph.  */
2135
2136 static void
2137 input_cgraph_opt_summary (vec<symtab_node *> nodes)
2138 {
2139   struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2140   struct lto_file_decl_data *file_data;
2141   unsigned int j = 0;
2142
2143   while ((file_data = file_data_vec[j++]))
2144     {
2145       size_t len;
2146       const char *data =
2147         lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
2148                               &len);
2149
2150       if (data)
2151         input_cgraph_opt_section (file_data, data, len, nodes);
2152     }
2153 }