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