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