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