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