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