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