[NDS32] new attribute no_prologue and new option -mret-in-naked-func.
[official-gcc.git] / gcc / lto / lto-partition.c
blobfd796e12a2dc7a660e8d5a1e3c1542e8a05d3752
1 /* LTO partitioning logic routines.
2 Copyright (C) 2009-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "target.h"
24 #include "function.h"
25 #include "basic-block.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "alloc-pool.h"
29 #include "stringpool.h"
30 #include "cgraph.h"
31 #include "lto-streamer.h"
32 #include "params.h"
33 #include "symbol-summary.h"
34 #include "tree-vrp.h"
35 #include "ipa-prop.h"
36 #include "ipa-fnsummary.h"
37 #include "lto-partition.h"
38 #include "sreal.h"
40 vec<ltrans_partition> ltrans_partitions;
42 static void add_symbol_to_partition (ltrans_partition part, symtab_node *node);
45 /* Helper for qsort; compare partitions and return one with smaller order. */
47 static int
48 cmp_partitions_order (const void *a, const void *b)
50 const struct ltrans_partition_def *pa
51 = *(struct ltrans_partition_def *const *)a;
52 const struct ltrans_partition_def *pb
53 = *(struct ltrans_partition_def *const *)b;
54 int ordera = -1, orderb = -1;
56 if (lto_symtab_encoder_size (pa->encoder))
57 ordera = lto_symtab_encoder_deref (pa->encoder, 0)->order;
58 if (lto_symtab_encoder_size (pb->encoder))
59 orderb = lto_symtab_encoder_deref (pb->encoder, 0)->order;
60 return orderb - ordera;
63 /* Create new partition with name NAME. */
65 static ltrans_partition
66 new_partition (const char *name)
68 ltrans_partition part = XCNEW (struct ltrans_partition_def);
69 part->encoder = lto_symtab_encoder_new (false);
70 part->name = name;
71 part->insns = 0;
72 part->symbols = 0;
73 ltrans_partitions.safe_push (part);
74 return part;
77 /* Free memory used by ltrans datastructures. */
79 void
80 free_ltrans_partitions (void)
82 unsigned int idx;
83 ltrans_partition part;
84 for (idx = 0; ltrans_partitions.iterate (idx, &part); idx++)
86 if (part->initializers_visited)
87 delete part->initializers_visited;
88 /* Symtab encoder is freed after streaming. */
89 free (part);
91 ltrans_partitions.release ();
94 /* Return true if symbol is already in some partition. */
96 static inline bool
97 symbol_partitioned_p (symtab_node *node)
99 return node->aux;
102 /* Add references into the partition. */
103 static void
104 add_references_to_partition (ltrans_partition part, symtab_node *node)
106 int i;
107 struct ipa_ref *ref = NULL;
109 /* Add all duplicated references to the partition. */
110 for (i = 0; node->iterate_reference (i, ref); i++)
111 if (ref->referred->get_partitioning_class () == SYMBOL_DUPLICATE)
112 add_symbol_to_partition (part, ref->referred);
113 /* References to a readonly variable may be constant foled into its value.
114 Recursively look into the initializers of the constant variable and add
115 references, too. */
116 else if (is_a <varpool_node *> (ref->referred)
117 && (dyn_cast <varpool_node *> (ref->referred)
118 ->ctor_useable_for_folding_p ()
119 || POINTER_BOUNDS_P (ref->referred->decl))
120 && !lto_symtab_encoder_in_partition_p (part->encoder, ref->referred))
122 if (!part->initializers_visited)
123 part->initializers_visited = new hash_set<symtab_node *>;
124 if (!part->initializers_visited->add (ref->referred))
125 add_references_to_partition (part, ref->referred);
129 /* Helper function for add_symbol_to_partition doing the actual dirty work
130 of adding NODE to PART. */
132 static bool
133 add_symbol_to_partition_1 (ltrans_partition part, symtab_node *node)
135 enum symbol_partitioning_class c = node->get_partitioning_class ();
136 struct ipa_ref *ref;
137 symtab_node *node1;
139 /* If NODE is already there, we have nothing to do. */
140 if (lto_symtab_encoder_in_partition_p (part->encoder, node))
141 return true;
143 /* non-duplicated aliases or tunks of a duplicated symbol needs to be output
144 just once.
146 Be lax about comdats; they may or may not be duplicated and we may
147 end up in need to duplicate keyed comdat because it has unkeyed alias. */
148 if (c == SYMBOL_PARTITION && !DECL_COMDAT (node->decl)
149 && symbol_partitioned_p (node))
150 return false;
152 /* Be sure that we never try to duplicate partitioned symbol
153 or add external symbol. */
154 gcc_assert (c != SYMBOL_EXTERNAL
155 && (c == SYMBOL_DUPLICATE || !symbol_partitioned_p (node)));
157 part->symbols++;
159 lto_set_symtab_encoder_in_partition (part->encoder, node);
161 if (symbol_partitioned_p (node))
163 node->in_other_partition = 1;
164 if (symtab->dump_file)
165 fprintf (symtab->dump_file,
166 "Symbol node %s now used in multiple partitions\n",
167 node->name ());
169 node->aux = (void *)((size_t)node->aux + 1);
171 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
173 struct cgraph_edge *e;
174 if (!node->alias && c == SYMBOL_PARTITION)
175 part->insns += ipa_fn_summaries->get (cnode)->size;
177 /* Add all inline clones and callees that are duplicated. */
178 for (e = cnode->callees; e; e = e->next_callee)
179 if (!e->inline_failed)
180 add_symbol_to_partition_1 (part, e->callee);
181 else if (e->callee->get_partitioning_class () == SYMBOL_DUPLICATE)
182 add_symbol_to_partition (part, e->callee);
184 /* Add all thunks associated with the function. */
185 for (e = cnode->callers; e; e = e->next_caller)
186 if (e->caller->thunk.thunk_p && !e->caller->global.inlined_to)
187 add_symbol_to_partition_1 (part, e->caller);
189 /* Instrumented version is actually the same function.
190 Therefore put it into the same partition. */
191 if (cnode->instrumented_version)
192 add_symbol_to_partition_1 (part, cnode->instrumented_version);
195 add_references_to_partition (part, node);
197 /* Add all aliases associated with the symbol. */
199 FOR_EACH_ALIAS (node, ref)
200 if (!ref->referring->transparent_alias)
201 add_symbol_to_partition_1 (part, ref->referring);
202 else
204 struct ipa_ref *ref2;
205 /* We do not need to add transparent aliases if they are not used.
206 However we must add aliases of transparent aliases if they exist. */
207 FOR_EACH_ALIAS (ref->referring, ref2)
209 /* Nested transparent aliases are not permitted. */
210 gcc_checking_assert (!ref2->referring->transparent_alias);
211 add_symbol_to_partition_1 (part, ref2->referring);
215 /* Ensure that SAME_COMDAT_GROUP lists all allways added in a group. */
216 if (node->same_comdat_group)
217 for (node1 = node->same_comdat_group;
218 node1 != node; node1 = node1->same_comdat_group)
219 if (!node->alias)
221 bool added = add_symbol_to_partition_1 (part, node1);
222 gcc_assert (added);
224 return true;
227 /* If symbol NODE is really part of other symbol's definition (i.e. it is
228 internal label, thunk, alias or so), return the outer symbol.
229 When add_symbol_to_partition_1 is called on the outer symbol it must
230 eventually add NODE, too. */
231 static symtab_node *
232 contained_in_symbol (symtab_node *node)
234 /* There is no need to consider transparent aliases to be part of the
235 definition: they are only useful insite the partition they are output
236 and thus we will always see an explicit reference to it. */
237 if (node->transparent_alias)
238 return node;
239 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
241 cnode = cnode->function_symbol ();
242 if (cnode->global.inlined_to)
243 cnode = cnode->global.inlined_to;
244 return cnode;
246 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
247 return vnode->ultimate_alias_target ();
248 return node;
251 /* Add symbol NODE to partition. When definition of NODE is part
252 of other symbol definition, add the other symbol, too. */
254 static void
255 add_symbol_to_partition (ltrans_partition part, symtab_node *node)
257 symtab_node *node1;
259 /* Verify that we do not try to duplicate something that can not be. */
260 gcc_checking_assert (node->get_partitioning_class () == SYMBOL_DUPLICATE
261 || !symbol_partitioned_p (node));
263 while ((node1 = contained_in_symbol (node)) != node)
264 node = node1;
266 /* If we have duplicated symbol contained in something we can not duplicate,
267 we are very badly screwed. The other way is possible, so we do not
268 assert this in add_symbol_to_partition_1.
270 Be lax about comdats; they may or may not be duplicated and we may
271 end up in need to duplicate keyed comdat because it has unkeyed alias. */
273 gcc_assert (node->get_partitioning_class () == SYMBOL_DUPLICATE
274 || DECL_COMDAT (node->decl)
275 || !symbol_partitioned_p (node));
277 add_symbol_to_partition_1 (part, node);
280 /* Undo all additions until number of cgraph nodes in PARITION is N_CGRAPH_NODES
281 and number of varpool nodes is N_VARPOOL_NODES. */
283 static void
284 undo_partition (ltrans_partition partition, unsigned int n_nodes)
286 while (lto_symtab_encoder_size (partition->encoder) > (int)n_nodes)
288 symtab_node *node = lto_symtab_encoder_deref (partition->encoder,
289 n_nodes);
290 partition->symbols--;
291 cgraph_node *cnode;
293 /* After UNDO we no longer know what was visited. */
294 if (partition->initializers_visited)
295 delete partition->initializers_visited;
296 partition->initializers_visited = NULL;
298 if (!node->alias && (cnode = dyn_cast <cgraph_node *> (node))
299 && node->get_partitioning_class () == SYMBOL_PARTITION)
300 partition->insns -= ipa_fn_summaries->get (cnode)->size;
301 lto_symtab_encoder_delete_node (partition->encoder, node);
302 node->aux = (void *)((size_t)node->aux - 1);
306 /* Group cgrah nodes by input files. This is used mainly for testing
307 right now. */
309 void
310 lto_1_to_1_map (void)
312 symtab_node *node;
313 struct lto_file_decl_data *file_data;
314 hash_map<lto_file_decl_data *, ltrans_partition> pmap;
315 ltrans_partition partition;
316 int npartitions = 0;
318 FOR_EACH_SYMBOL (node)
320 if (node->get_partitioning_class () != SYMBOL_PARTITION
321 || symbol_partitioned_p (node))
322 continue;
324 file_data = node->lto_file_data;
326 if (file_data)
328 ltrans_partition *slot = &pmap.get_or_insert (file_data);
329 if (*slot)
330 partition = *slot;
331 else
333 partition = new_partition (file_data->file_name);
334 *slot = partition;
335 npartitions++;
338 else if (!file_data && ltrans_partitions.length ())
339 partition = ltrans_partitions[0];
340 else
342 partition = new_partition ("");
343 pmap.put (NULL, partition);
344 npartitions++;
347 add_symbol_to_partition (partition, node);
350 /* If the cgraph is empty, create one cgraph node set so that there is still
351 an output file for any variables that need to be exported in a DSO. */
352 if (!npartitions)
353 new_partition ("empty");
355 /* Order partitions by order of symbols because they are linked into binary
356 that way. */
357 ltrans_partitions.qsort (cmp_partitions_order);
360 /* Maximal partitioning. Put every new symbol into new partition if possible. */
362 void
363 lto_max_map (void)
365 symtab_node *node;
366 ltrans_partition partition;
367 int npartitions = 0;
369 FOR_EACH_SYMBOL (node)
371 if (node->get_partitioning_class () != SYMBOL_PARTITION
372 || symbol_partitioned_p (node))
373 continue;
374 partition = new_partition (node->asm_name ());
375 add_symbol_to_partition (partition, node);
376 npartitions++;
378 if (!npartitions)
379 new_partition ("empty");
382 /* Helper function for qsort; sort nodes by order. noreorder functions must have
383 been removed earlier. */
384 static int
385 node_cmp (const void *pa, const void *pb)
387 const struct cgraph_node *a = *(const struct cgraph_node * const *) pa;
388 const struct cgraph_node *b = *(const struct cgraph_node * const *) pb;
390 /* Profile reorder flag enables function reordering based on first execution
391 of a function. All functions with profile are placed in ascending
392 order at the beginning. */
394 if (flag_profile_reorder_functions)
396 /* Functions with time profile are sorted in ascending order. */
397 if (a->tp_first_run && b->tp_first_run)
398 return a->tp_first_run != b->tp_first_run
399 ? a->tp_first_run - b->tp_first_run
400 : a->order - b->order;
402 /* Functions with time profile are sorted before the functions
403 that do not have the profile. */
404 if (a->tp_first_run || b->tp_first_run)
405 return b->tp_first_run - a->tp_first_run;
408 return b->order - a->order;
411 /* Helper function for qsort; sort nodes by order. */
412 static int
413 varpool_node_cmp (const void *pa, const void *pb)
415 const symtab_node *a = *static_cast<const symtab_node * const *> (pa);
416 const symtab_node *b = *static_cast<const symtab_node * const *> (pb);
417 return b->order - a->order;
420 /* Add all symtab nodes from NEXT_NODE to PARTITION in order. */
422 static void
423 add_sorted_nodes (vec<symtab_node *> &next_nodes, ltrans_partition partition)
425 unsigned i;
426 symtab_node *node;
428 next_nodes.qsort (varpool_node_cmp);
429 FOR_EACH_VEC_ELT (next_nodes, i, node)
430 if (!symbol_partitioned_p (node))
431 add_symbol_to_partition (partition, node);
434 /* Return true if we should account reference from N1 to N2 in cost
435 of partition boundary. */
437 bool
438 account_reference_p (symtab_node *n1, symtab_node *n2)
440 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (n1))
441 n1 = cnode;
442 /* Do not account references from aliases - they are never split across
443 partitions. */
444 if (n1->alias)
445 return false;
446 /* Do not account recursion - the code below will handle it incorrectly
447 otherwise. Do not account references to external symbols: they will
448 never become local. Finally do not account references to duplicated
449 symbols: they will be always local. */
450 if (n1 == n2
451 || !n2->definition
452 || n2->get_partitioning_class () != SYMBOL_PARTITION)
453 return false;
454 /* If referring node is external symbol do not account it to boundary
455 cost. Those are added into units only to enable possible constant
456 folding and devirtulization.
458 Here we do not know if it will ever be added to some partition
459 (this is decided by compute_ltrans_boundary) and second it is not
460 that likely that constant folding will actually use the reference. */
461 if (contained_in_symbol (n1)
462 ->get_partitioning_class () == SYMBOL_EXTERNAL)
463 return false;
464 return true;
468 /* Group cgraph nodes into equally-sized partitions.
470 The partitioning algorithm is simple: nodes are taken in predefined order.
471 The order corresponds to the order we want functions to have in the final
472 output. In the future this will be given by function reordering pass, but
473 at the moment we use the topological order, which is a good approximation.
475 The goal is to partition this linear order into intervals (partitions) so
476 that all the partitions have approximately the same size and the number of
477 callgraph or IPA reference edges crossing boundaries is minimal.
479 This is a lot faster (O(n) in size of callgraph) than algorithms doing
480 priority-based graph clustering that are generally O(n^2) and, since
481 WHOPR is designed to make things go well across partitions, it leads
482 to good results.
484 We compute the expected size of a partition as:
486 max (total_size / lto_partitions, min_partition_size)
488 We use dynamic expected size of partition so small programs are partitioned
489 into enough partitions to allow use of multiple CPUs, while large programs
490 are not partitioned too much. Creating too many partitions significantly
491 increases the streaming overhead.
493 In the future, we would like to bound the maximal size of partitions so as
494 to prevent the LTRANS stage from consuming too much memory. At the moment,
495 however, the WPA stage is the most memory intensive for large benchmarks,
496 since too many types and declarations are read into memory.
498 The function implements a simple greedy algorithm. Nodes are being added
499 to the current partition until after 3/4 of the expected partition size is
500 reached. Past this threshold, we keep track of boundary size (number of
501 edges going to other partitions) and continue adding functions until after
502 the current partition has grown to twice the expected partition size. Then
503 the process is undone to the point where the minimal ratio of boundary size
504 and in-partition calls was reached. */
506 void
507 lto_balanced_map (int n_lto_partitions, int max_partition_size)
509 int n_nodes = 0;
510 int n_varpool_nodes = 0, varpool_pos = 0, best_varpool_pos = 0;
511 struct cgraph_node **order = XNEWVEC (cgraph_node *, symtab->cgraph_max_uid);
512 auto_vec<cgraph_node *> noreorder;
513 auto_vec<varpool_node *> varpool_order;
514 int i;
515 struct cgraph_node *node;
516 int64_t original_total_size, total_size = 0;
517 int64_t partition_size;
518 ltrans_partition partition;
519 int last_visited_node = 0;
520 varpool_node *vnode;
521 int64_t cost = 0, internal = 0;
522 int best_n_nodes = 0, best_i = 0;
523 int64_t best_cost = -1, best_internal = 0, best_size = 0;
524 int npartitions;
525 int current_order = -1;
526 int noreorder_pos = 0;
528 FOR_EACH_VARIABLE (vnode)
529 gcc_assert (!vnode->aux);
531 FOR_EACH_DEFINED_FUNCTION (node)
532 if (node->get_partitioning_class () == SYMBOL_PARTITION)
534 if (node->no_reorder)
535 noreorder.safe_push (node);
536 else
537 order[n_nodes++] = node;
538 if (!node->alias)
539 total_size += ipa_fn_summaries->get (node)->size;
542 original_total_size = total_size;
544 /* Streaming works best when the source units do not cross partition
545 boundaries much. This is because importing function from a source
546 unit tends to import a lot of global trees defined there. We should
547 get better about minimizing the function bounday, but until that
548 things works smoother if we order in source order. */
549 qsort (order, n_nodes, sizeof (struct cgraph_node *), node_cmp);
550 noreorder.qsort (node_cmp);
552 if (symtab->dump_file)
554 for(i = 0; i < n_nodes; i++)
555 fprintf (symtab->dump_file, "Balanced map symbol order:%s:%u\n",
556 order[i]->name (), order[i]->tp_first_run);
557 for(i = 0; i < (int)noreorder.length(); i++)
558 fprintf (symtab->dump_file, "Balanced map symbol no_reorder:%s:%u\n",
559 noreorder[i]->name (), noreorder[i]->tp_first_run);
562 /* Collect all variables that should not be reordered. */
563 FOR_EACH_VARIABLE (vnode)
564 if (vnode->get_partitioning_class () == SYMBOL_PARTITION
565 && vnode->no_reorder)
566 varpool_order.safe_push (vnode);
567 n_varpool_nodes = varpool_order.length ();
568 varpool_order.qsort (varpool_node_cmp);
570 /* Compute partition size and create the first partition. */
571 if (PARAM_VALUE (MIN_PARTITION_SIZE) > max_partition_size)
572 fatal_error (input_location, "min partition size cannot be greater "
573 "than max partition size");
575 partition_size = total_size / n_lto_partitions;
576 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
577 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
578 npartitions = 1;
579 partition = new_partition ("");
580 if (symtab->dump_file)
581 fprintf (symtab->dump_file, "Total unit size: %" PRId64 ", partition size: %" PRId64 "\n",
582 total_size, partition_size);
584 auto_vec<symtab_node *> next_nodes;
586 for (i = 0; i < n_nodes; i++)
588 if (symbol_partitioned_p (order[i]))
589 continue;
591 current_order = order[i]->order;
593 /* Output noreorder and varpool in program order first. */
594 next_nodes.truncate (0);
595 while (varpool_pos < n_varpool_nodes
596 && varpool_order[varpool_pos]->order < current_order)
597 next_nodes.safe_push (varpool_order[varpool_pos++]);
598 while (noreorder_pos < (int)noreorder.length ()
599 && noreorder[noreorder_pos]->order < current_order)
600 next_nodes.safe_push (noreorder[noreorder_pos++]);
601 add_sorted_nodes (next_nodes, partition);
603 if (!symbol_partitioned_p (order[i]))
604 add_symbol_to_partition (partition, order[i]);
607 /* Once we added a new node to the partition, we also want to add
608 all referenced variables unless they was already added into some
609 earlier partition.
610 add_symbol_to_partition adds possibly multiple nodes and
611 variables that are needed to satisfy needs of ORDER[i].
612 We remember last visited cgraph and varpool node from last iteration
613 of outer loop that allows us to process every new addition.
615 At the same time we compute size of the boundary into COST. Every
616 callgraph or IPA reference edge leaving the partition contributes into
617 COST. Every edge inside partition was earlier computed as one leaving
618 it and thus we need to subtract it from COST. */
619 while (last_visited_node < lto_symtab_encoder_size (partition->encoder))
621 int j;
622 struct ipa_ref *ref = NULL;
623 symtab_node *snode = lto_symtab_encoder_deref (partition->encoder,
624 last_visited_node);
626 if (cgraph_node *node = dyn_cast <cgraph_node *> (snode))
628 struct cgraph_edge *edge;
631 last_visited_node++;
633 gcc_assert (node->definition || node->weakref);
635 /* Compute boundary cost of callgraph edges. */
636 for (edge = node->callees; edge; edge = edge->next_callee)
637 /* Inline edges will always end up local. */
638 if (edge->inline_failed
639 && account_reference_p (node, edge->callee))
641 int edge_cost = edge->frequency ();
642 int index;
644 if (!edge_cost)
645 edge_cost = 1;
646 gcc_assert (edge_cost > 0);
647 index = lto_symtab_encoder_lookup (partition->encoder,
648 edge->callee);
649 if (index != LCC_NOT_FOUND
650 && index < last_visited_node - 1)
651 cost -= edge_cost, internal += edge_cost;
652 else
653 cost += edge_cost;
655 for (edge = node->callers; edge; edge = edge->next_caller)
656 if (edge->inline_failed
657 && account_reference_p (edge->caller, node))
659 int edge_cost = edge->frequency ();
660 int index;
662 gcc_assert (edge->caller->definition);
663 if (!edge_cost)
664 edge_cost = 1;
665 gcc_assert (edge_cost > 0);
666 index = lto_symtab_encoder_lookup (partition->encoder,
667 edge->caller);
668 if (index != LCC_NOT_FOUND
669 && index < last_visited_node - 1)
670 cost -= edge_cost, internal += edge_cost;
671 else
672 cost += edge_cost;
675 else
676 last_visited_node++;
678 /* Compute boundary cost of IPA REF edges and at the same time look into
679 variables referenced from current partition and try to add them. */
680 for (j = 0; snode->iterate_reference (j, ref); j++)
681 if (!account_reference_p (snode, ref->referred))
683 else if (is_a <varpool_node *> (ref->referred))
685 int index;
687 vnode = dyn_cast <varpool_node *> (ref->referred);
688 if (!symbol_partitioned_p (vnode)
689 && !vnode->no_reorder
690 && vnode->get_partitioning_class () == SYMBOL_PARTITION)
691 add_symbol_to_partition (partition, vnode);
692 index = lto_symtab_encoder_lookup (partition->encoder,
693 vnode);
694 if (index != LCC_NOT_FOUND
695 && index < last_visited_node - 1)
696 cost--, internal++;
697 else
698 cost++;
700 else
702 int index;
704 node = dyn_cast <cgraph_node *> (ref->referred);
705 index = lto_symtab_encoder_lookup (partition->encoder,
706 node);
707 if (index != LCC_NOT_FOUND
708 && index < last_visited_node - 1)
709 cost--, internal++;
710 else
711 cost++;
713 for (j = 0; snode->iterate_referring (j, ref); j++)
714 if (!account_reference_p (ref->referring, snode))
716 else if (is_a <varpool_node *> (ref->referring))
718 int index;
720 vnode = dyn_cast <varpool_node *> (ref->referring);
721 gcc_assert (vnode->definition);
722 /* It is better to couple variables with their users,
723 because it allows them to be removed. Coupling
724 with objects they refer to only helps to reduce
725 number of symbols promoted to hidden. */
726 if (!symbol_partitioned_p (vnode)
727 && !vnode->no_reorder
728 && !vnode->can_remove_if_no_refs_p ()
729 && vnode->get_partitioning_class () == SYMBOL_PARTITION)
730 add_symbol_to_partition (partition, vnode);
731 index = lto_symtab_encoder_lookup (partition->encoder,
732 vnode);
733 if (index != LCC_NOT_FOUND
734 && index < last_visited_node - 1)
735 cost--, internal++;
736 else
737 cost++;
739 else
741 int index;
743 node = dyn_cast <cgraph_node *> (ref->referring);
744 gcc_assert (node->definition);
745 index = lto_symtab_encoder_lookup (partition->encoder,
746 node);
747 if (index != LCC_NOT_FOUND
748 && index < last_visited_node - 1)
749 cost--, internal++;
750 else
751 cost++;
755 gcc_assert (cost >= 0 && internal >= 0);
757 /* If the partition is large enough, start looking for smallest boundary cost.
758 If partition still seems too small (less than 7/8 of target weight) accept
759 any cost. If partition has right size, optimize for highest internal/cost.
760 Later we stop building partition if its size is 9/8 of the target wight. */
761 if (partition->insns < partition_size * 7 / 8
762 || best_cost == -1
763 || (!cost
764 || ((sreal)best_internal * (sreal) cost
765 < ((sreal) internal * (sreal)best_cost))))
767 best_cost = cost;
768 best_internal = internal;
769 best_size = partition->insns;
770 best_i = i;
771 best_n_nodes = lto_symtab_encoder_size (partition->encoder);
772 best_varpool_pos = varpool_pos;
774 if (symtab->dump_file)
775 fprintf (symtab->dump_file, "Step %i: added %s/%i, size %i, "
776 "cost %" PRId64 "/%" PRId64 " "
777 "best %" PRId64 "/%" PRId64", step %i\n", i,
778 order[i]->name (), order[i]->order,
779 partition->insns, cost, internal,
780 best_cost, best_internal, best_i);
781 /* Partition is too large, unwind into step when best cost was reached and
782 start new partition. */
783 if (partition->insns > 9 * partition_size / 8
784 || partition->insns > max_partition_size)
786 if (best_i != i)
788 if (symtab->dump_file)
789 fprintf (symtab->dump_file, "Unwinding %i insertions to step %i\n",
790 i - best_i, best_i);
791 undo_partition (partition, best_n_nodes);
792 varpool_pos = best_varpool_pos;
794 gcc_assert (best_size == partition->insns);
795 i = best_i;
796 if (symtab->dump_file)
797 fprintf (symtab->dump_file,
798 "Partition insns: %i (want %" PRId64 ")\n",
799 partition->insns, partition_size);
800 /* When we are finished, avoid creating empty partition. */
801 while (i < n_nodes - 1 && symbol_partitioned_p (order[i + 1]))
802 i++;
803 if (i == n_nodes - 1)
804 break;
805 total_size -= partition->insns;
806 partition = new_partition ("");
807 last_visited_node = 0;
808 cost = 0;
810 if (symtab->dump_file)
811 fprintf (symtab->dump_file, "New partition\n");
812 best_n_nodes = 0;
813 best_cost = -1;
815 /* Since the size of partitions is just approximate, update the size after
816 we finished current one. */
817 if (npartitions < n_lto_partitions)
818 partition_size = total_size / (n_lto_partitions - npartitions);
819 else
820 /* Watch for overflow. */
821 partition_size = INT_MAX / 16;
823 if (symtab->dump_file)
824 fprintf (symtab->dump_file,
825 "Total size: %" PRId64 " partition_size: %" PRId64 "\n",
826 total_size, partition_size);
827 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
828 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
829 npartitions ++;
833 next_nodes.truncate (0);
835 /* Varables that are not reachable from the code go into last partition. */
836 FOR_EACH_VARIABLE (vnode)
837 if (vnode->get_partitioning_class () == SYMBOL_PARTITION
838 && !symbol_partitioned_p (vnode))
839 next_nodes.safe_push (vnode);
841 /* Output remaining ordered symbols. */
842 while (varpool_pos < n_varpool_nodes)
843 next_nodes.safe_push (varpool_order[varpool_pos++]);
844 while (noreorder_pos < (int)noreorder.length ())
845 next_nodes.safe_push (noreorder[noreorder_pos++]);
846 /* For one partition the cost of boundary should be 0 unless we added final
847 symbols here (these are not accounted) or we have accounting bug. */
848 gcc_assert (next_nodes.length () || npartitions != 1 || !best_cost || best_cost == -1);
849 add_sorted_nodes (next_nodes, partition);
851 free (order);
853 if (symtab->dump_file)
855 fprintf (symtab->dump_file, "\nPartition sizes:\n");
856 unsigned partitions = ltrans_partitions.length ();
858 for (unsigned i = 0; i < partitions ; i++)
860 ltrans_partition p = ltrans_partitions[i];
861 fprintf (symtab->dump_file, "partition %d contains %d (%2.2f%%)"
862 " symbols and %d (%2.2f%%) insns\n", i, p->symbols,
863 100.0 * p->symbols / n_nodes, p->insns,
864 100.0 * p->insns / original_total_size);
867 fprintf (symtab->dump_file, "\n");
871 /* Return true if we must not change the name of the NODE. The name as
872 extracted from the corresponding decl should be passed in NAME. */
874 static bool
875 must_not_rename (symtab_node *node, const char *name)
877 /* Our renaming machinery do not handle more than one change of assembler name.
878 We should not need more than one anyway. */
879 if (node->lto_file_data
880 && lto_get_decl_name_mapping (node->lto_file_data, name) != name)
882 if (symtab->dump_file)
883 fprintf (symtab->dump_file,
884 "Not privatizing symbol name: %s. It privatized already.\n",
885 name);
886 return true;
888 /* Avoid mangling of already mangled clones.
889 ??? should have a flag whether a symbol has a 'private' name already,
890 since we produce some symbols like that i.e. for global constructors
891 that are not really clones. */
892 if (node->unique_name)
894 if (symtab->dump_file)
895 fprintf (symtab->dump_file,
896 "Not privatizing symbol name: %s. Has unique name.\n",
897 name);
898 return true;
900 return false;
903 /* If we are an offload compiler, we may have to rewrite symbols to be
904 valid on this target. Return either PTR or a modified version of it. */
906 static const char *
907 maybe_rewrite_identifier (const char *ptr)
909 #if defined ACCEL_COMPILER && (defined NO_DOT_IN_LABEL || defined NO_DOLLAR_IN_LABEL)
910 #ifndef NO_DOT_IN_LABEL
911 char valid = '.';
912 const char reject[] = "$";
913 #elif !defined NO_DOLLAR_IN_LABEL
914 char valid = '$';
915 const char reject[] = ".";
916 #else
917 char valid = '_';
918 const char reject[] = ".$";
919 #endif
921 char *copy = NULL;
922 const char *match = ptr;
923 for (;;)
925 size_t off = strcspn (match, reject);
926 if (match[off] == '\0')
927 break;
928 if (copy == NULL)
930 copy = xstrdup (ptr);
931 match = copy;
933 copy[off] = valid;
935 return match;
936 #else
937 return ptr;
938 #endif
941 /* Ensure that the symbol in NODE is valid for the target, and if not,
942 rewrite it. */
944 static void
945 validize_symbol_for_target (symtab_node *node)
947 tree decl = node->decl;
948 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
950 if (must_not_rename (node, name))
951 return;
953 const char *name2 = maybe_rewrite_identifier (name);
954 if (name2 != name)
956 symtab->change_decl_assembler_name (decl, get_identifier (name2));
957 if (node->lto_file_data)
958 lto_record_renamed_decl (node->lto_file_data, name,
959 IDENTIFIER_POINTER
960 (DECL_ASSEMBLER_NAME (decl)));
964 /* Helper for privatize_symbol_name. Mangle NODE symbol name
965 represented by DECL. */
967 static bool
968 privatize_symbol_name_1 (symtab_node *node, tree decl)
970 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
972 if (must_not_rename (node, name))
973 return false;
975 name = maybe_rewrite_identifier (name);
976 symtab->change_decl_assembler_name (decl,
977 clone_function_name_1 (name,
978 "lto_priv"));
980 if (node->lto_file_data)
981 lto_record_renamed_decl (node->lto_file_data, name,
982 IDENTIFIER_POINTER
983 (DECL_ASSEMBLER_NAME (decl)));
985 if (symtab->dump_file)
986 fprintf (symtab->dump_file,
987 "Privatizing symbol name: %s -> %s\n",
988 name, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
990 return true;
993 /* Mangle NODE symbol name into a local name.
994 This is necessary to do
995 1) if two or more static vars of same assembler name
996 are merged into single ltrans unit.
997 2) if previously static var was promoted hidden to avoid possible conflict
998 with symbols defined out of the LTO world. */
1000 static bool
1001 privatize_symbol_name (symtab_node *node)
1003 if (!privatize_symbol_name_1 (node, node->decl))
1004 return false;
1006 /* We could change name which is a target of transparent alias
1007 chain of instrumented function name. Fix alias chain if so .*/
1008 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1010 tree iname = NULL_TREE;
1011 if (cnode->instrumentation_clone)
1013 /* If we want to privatize instrumentation clone
1014 then we also need to privatize original function. */
1015 if (cnode->instrumented_version)
1016 privatize_symbol_name (cnode->instrumented_version);
1017 else
1018 privatize_symbol_name_1 (cnode, cnode->orig_decl);
1019 iname = DECL_ASSEMBLER_NAME (cnode->decl);
1020 TREE_CHAIN (iname) = DECL_ASSEMBLER_NAME (cnode->orig_decl);
1022 else if (cnode->instrumented_version
1023 && cnode->instrumented_version->orig_decl == cnode->decl)
1025 iname = DECL_ASSEMBLER_NAME (cnode->instrumented_version->decl);
1026 TREE_CHAIN (iname) = DECL_ASSEMBLER_NAME (cnode->decl);
1030 return true;
1033 /* Promote variable VNODE to be static. */
1035 static void
1036 promote_symbol (symtab_node *node)
1038 /* We already promoted ... */
1039 if (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
1040 && DECL_VISIBILITY_SPECIFIED (node->decl)
1041 && TREE_PUBLIC (node->decl))
1043 validize_symbol_for_target (node);
1044 return;
1047 gcc_checking_assert (!TREE_PUBLIC (node->decl)
1048 && !DECL_EXTERNAL (node->decl));
1049 /* Be sure that newly public symbol does not conflict with anything already
1050 defined by the non-LTO part. */
1051 privatize_symbol_name (node);
1052 TREE_PUBLIC (node->decl) = 1;
1053 DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
1054 DECL_VISIBILITY_SPECIFIED (node->decl) = true;
1055 if (symtab->dump_file)
1056 fprintf (symtab->dump_file,
1057 "Promoting as hidden: %s (%s)\n", node->name (),
1058 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
1060 /* Promoting a symbol also promotes all transparent aliases with exception
1061 of weakref where the visibility flags are always wrong and set to
1062 !PUBLIC. */
1063 ipa_ref *ref;
1064 for (unsigned i = 0; node->iterate_direct_aliases (i, ref); i++)
1066 struct symtab_node *alias = ref->referring;
1067 if (alias->transparent_alias && !alias->weakref)
1069 TREE_PUBLIC (alias->decl) = 1;
1070 DECL_VISIBILITY (alias->decl) = VISIBILITY_HIDDEN;
1071 DECL_VISIBILITY_SPECIFIED (alias->decl) = true;
1072 if (symtab->dump_file)
1073 fprintf (symtab->dump_file,
1074 "Promoting alias as hidden: %s\n",
1075 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
1077 gcc_assert (!alias->weakref || TREE_PUBLIC (alias->decl));
1081 /* Return true if NODE needs named section even if it won't land in
1082 the partition symbol table.
1084 FIXME: we should really not use named sections for inline clones
1085 and master clones. */
1087 static bool
1088 may_need_named_section_p (lto_symtab_encoder_t encoder, symtab_node *node)
1090 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1091 if (!cnode)
1092 return false;
1093 if (node->real_symbol_p ())
1094 return false;
1095 return (!encoder
1096 || (lto_symtab_encoder_lookup (encoder, node) != LCC_NOT_FOUND
1097 && lto_symtab_encoder_encode_body_p (encoder,
1098 cnode)));
1101 /* If NODE represents a static variable. See if there are other variables
1102 of the same name in partition ENCODER (or in whole compilation unit if
1103 ENCODER is NULL) and if so, mangle the statics. Always mangle all
1104 conflicting statics, so we reduce changes of silently miscompiling
1105 asm statements referring to them by symbol name. */
1107 static void
1108 rename_statics (lto_symtab_encoder_t encoder, symtab_node *node)
1110 tree decl = node->decl;
1111 symtab_node *s;
1112 tree name = DECL_ASSEMBLER_NAME (decl);
1114 /* See if this is static symbol. */
1115 if (((node->externally_visible && !node->weakref)
1116 /* FIXME: externally_visible is somewhat illogically not set for
1117 external symbols (i.e. those not defined). Remove this test
1118 once this is fixed. */
1119 || DECL_EXTERNAL (node->decl)
1120 || !node->real_symbol_p ())
1121 && !may_need_named_section_p (encoder, node))
1122 return;
1124 /* Now walk symbols sharing the same name and see if there are any conflicts.
1125 (all types of symbols counts here, since we can not have static of the
1126 same name as external or public symbol.) */
1127 for (s = symtab_node::get_for_asmname (name);
1128 s; s = s->next_sharing_asm_name)
1129 if ((s->real_symbol_p () || may_need_named_section_p (encoder, s))
1130 && s->decl != node->decl
1131 && (!encoder
1132 || lto_symtab_encoder_lookup (encoder, s) != LCC_NOT_FOUND))
1133 break;
1135 /* OK, no confict, so we have nothing to do. */
1136 if (!s)
1137 return;
1139 if (symtab->dump_file)
1140 fprintf (symtab->dump_file,
1141 "Renaming statics with asm name: %s\n", node->name ());
1143 /* Assign every symbol in the set that shares the same ASM name an unique
1144 mangled name. */
1145 for (s = symtab_node::get_for_asmname (name); s;)
1146 if ((!s->externally_visible || s->weakref)
1147 /* Transparent aliases having same name as target are renamed at a
1148 time their target gets new name. Transparent aliases that use
1149 separate assembler name require the name to be unique. */
1150 && (!s->transparent_alias || !s->definition || s->weakref
1151 || !symbol_table::assembler_names_equal_p
1152 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (s->decl)),
1153 IDENTIFIER_POINTER
1154 (DECL_ASSEMBLER_NAME (s->get_alias_target()->decl))))
1155 && ((s->real_symbol_p ()
1156 && !DECL_EXTERNAL (s->decl)
1157 && !TREE_PUBLIC (s->decl))
1158 || may_need_named_section_p (encoder, s))
1159 && (!encoder
1160 || lto_symtab_encoder_lookup (encoder, s) != LCC_NOT_FOUND))
1162 if (privatize_symbol_name (s))
1163 /* Re-start from beginning since we do not know how many
1164 symbols changed a name. */
1165 s = symtab_node::get_for_asmname (name);
1166 else s = s->next_sharing_asm_name;
1168 else s = s->next_sharing_asm_name;
1171 /* Find out all static decls that need to be promoted to global because
1172 of cross file sharing. This function must be run in the WPA mode after
1173 all inlinees are added. */
1175 void
1176 lto_promote_cross_file_statics (void)
1178 unsigned i, n_sets;
1180 gcc_assert (flag_wpa);
1182 lto_stream_offload_p = false;
1183 select_what_to_stream ();
1185 /* First compute boundaries. */
1186 n_sets = ltrans_partitions.length ();
1187 for (i = 0; i < n_sets; i++)
1189 ltrans_partition part
1190 = ltrans_partitions[i];
1191 part->encoder = compute_ltrans_boundary (part->encoder);
1194 /* Look at boundaries and promote symbols as needed. */
1195 for (i = 0; i < n_sets; i++)
1197 lto_symtab_encoder_iterator lsei;
1198 lto_symtab_encoder_t encoder = ltrans_partitions[i]->encoder;
1200 for (lsei = lsei_start (encoder); !lsei_end_p (lsei);
1201 lsei_next (&lsei))
1203 symtab_node *node = lsei_node (lsei);
1205 /* If symbol is static, rename it if its assembler name
1206 clashes with anything else in this unit. */
1207 rename_statics (encoder, node);
1209 /* No need to promote if symbol already is externally visible ... */
1210 if (node->externally_visible
1211 /* ... or if it is part of current partition ... */
1212 || lto_symtab_encoder_in_partition_p (encoder, node)
1213 /* ... or if we do not partition it. This mean that it will
1214 appear in every partition referencing it. */
1215 || node->get_partitioning_class () != SYMBOL_PARTITION)
1217 validize_symbol_for_target (node);
1218 continue;
1221 promote_symbol (node);
1226 /* Rename statics in the whole unit in the case that
1227 we do -flto-partition=none. */
1229 void
1230 lto_promote_statics_nonwpa (void)
1232 symtab_node *node;
1233 FOR_EACH_SYMBOL (node)
1235 rename_statics (NULL, node);
1236 validize_symbol_for_target (node);