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