2015-06-25 Andrew MacLeod <amacleod@redhat.com>
[official-gcc.git] / gcc / lto / lto-partition.c
blobdb830fd0863725e7c419477e28b555fb6af01f6a
1 /* LTO partitioning logic routines.
2 Copyright (C) 2009-2015 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 "toplev.h"
24 #include "alias.h"
25 #include "symtab.h"
26 #include "options.h"
27 #include "tree.h"
28 #include "fold-const.h"
29 #include "predict.h"
30 #include "tm.h"
31 #include "hard-reg-set.h"
32 #include "function.h"
33 #include "basic-block.h"
34 #include "tree-ssa-alias.h"
35 #include "internal-fn.h"
36 #include "gimple-expr.h"
37 #include "gimple.h"
38 #include "cgraph.h"
39 #include "lto-streamer.h"
40 #include "timevar.h"
41 #include "params.h"
42 #include "alloc-pool.h"
43 #include "symbol-summary.h"
44 #include "ipa-prop.h"
45 #include "ipa-inline.h"
46 #include "ipa-utils.h"
47 #include "lto-partition.h"
48 #include "stringpool.h"
50 vec<ltrans_partition> ltrans_partitions;
52 static void add_symbol_to_partition (ltrans_partition part, symtab_node *node);
55 /* Create new partition with name NAME. */
57 static ltrans_partition
58 new_partition (const char *name)
60 ltrans_partition part = XCNEW (struct ltrans_partition_def);
61 part->encoder = lto_symtab_encoder_new (false);
62 part->name = name;
63 part->insns = 0;
64 part->symbols = 0;
65 ltrans_partitions.safe_push (part);
66 return part;
69 /* Free memory used by ltrans datastructures. */
71 void
72 free_ltrans_partitions (void)
74 unsigned int idx;
75 ltrans_partition part;
76 for (idx = 0; ltrans_partitions.iterate (idx, &part); idx++)
78 if (part->initializers_visited)
79 delete part->initializers_visited;
80 /* Symtab encoder is freed after streaming. */
81 free (part);
83 ltrans_partitions.release ();
86 /* Return true if symbol is already in some partition. */
88 static inline bool
89 symbol_partitioned_p (symtab_node *node)
91 return node->aux;
94 /* Add references into the partition. */
95 static void
96 add_references_to_partition (ltrans_partition part, symtab_node *node)
98 int i;
99 struct ipa_ref *ref = NULL;
101 /* Add all duplicated references to the partition. */
102 for (i = 0; node->iterate_reference (i, ref); i++)
103 if (ref->referred->get_partitioning_class () == SYMBOL_DUPLICATE)
104 add_symbol_to_partition (part, ref->referred);
105 /* References to a readonly variable may be constant foled into its value.
106 Recursively look into the initializers of the constant variable and add
107 references, too. */
108 else if (is_a <varpool_node *> (ref->referred)
109 && (dyn_cast <varpool_node *> (ref->referred)
110 ->ctor_useable_for_folding_p ()
111 || POINTER_BOUNDS_P (ref->referred->decl))
112 && !lto_symtab_encoder_in_partition_p (part->encoder, ref->referred))
114 if (!part->initializers_visited)
115 part->initializers_visited = new hash_set<symtab_node *>;
116 if (!part->initializers_visited->add (ref->referred))
117 add_references_to_partition (part, ref->referred);
121 /* Helper function for add_symbol_to_partition doing the actual dirty work
122 of adding NODE to PART. */
124 static bool
125 add_symbol_to_partition_1 (ltrans_partition part, symtab_node *node)
127 enum symbol_partitioning_class c = node->get_partitioning_class ();
128 struct ipa_ref *ref;
129 symtab_node *node1;
131 /* If NODE is already there, we have nothing to do. */
132 if (lto_symtab_encoder_in_partition_p (part->encoder, node))
133 return true;
135 /* non-duplicated aliases or tunks of a duplicated symbol needs to be output
136 just once.
138 Be lax about comdats; they may or may not be duplicated and we may
139 end up in need to duplicate keyed comdat because it has unkeyed alias. */
140 if (c == SYMBOL_PARTITION && !DECL_COMDAT (node->decl)
141 && symbol_partitioned_p (node))
142 return false;
144 /* Be sure that we never try to duplicate partitioned symbol
145 or add external symbol. */
146 gcc_assert (c != SYMBOL_EXTERNAL
147 && (c == SYMBOL_DUPLICATE || !symbol_partitioned_p (node)));
149 part->symbols++;
151 lto_set_symtab_encoder_in_partition (part->encoder, node);
153 if (symbol_partitioned_p (node))
155 node->in_other_partition = 1;
156 if (symtab->dump_file)
157 fprintf (symtab->dump_file,
158 "Symbol node %s now used in multiple partitions\n",
159 node->name ());
161 node->aux = (void *)((size_t)node->aux + 1);
163 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
165 struct cgraph_edge *e;
166 if (!node->alias)
167 part->insns += inline_summaries->get (cnode)->self_size;
169 /* Add all inline clones and callees that are duplicated. */
170 for (e = cnode->callees; e; e = e->next_callee)
171 if (!e->inline_failed)
172 add_symbol_to_partition_1 (part, e->callee);
173 else if (e->callee->get_partitioning_class () == SYMBOL_DUPLICATE)
174 add_symbol_to_partition (part, e->callee);
176 /* Add all thunks associated with the function. */
177 for (e = cnode->callers; e; e = e->next_caller)
178 if (e->caller->thunk.thunk_p)
179 add_symbol_to_partition_1 (part, e->caller);
181 /* Instrumented version is actually the same function.
182 Therefore put it into the same partition. */
183 if (cnode->instrumented_version)
184 add_symbol_to_partition_1 (part, cnode->instrumented_version);
187 add_references_to_partition (part, node);
189 /* Add all aliases associated with the symbol. */
191 FOR_EACH_ALIAS (node, ref)
192 if (!node->weakref)
193 add_symbol_to_partition_1 (part, ref->referring);
195 /* Ensure that SAME_COMDAT_GROUP lists all allways added in a group. */
196 if (node->same_comdat_group)
197 for (node1 = node->same_comdat_group;
198 node1 != node; node1 = node1->same_comdat_group)
199 if (!node->alias)
201 bool added = add_symbol_to_partition_1 (part, node1);
202 gcc_assert (added);
204 return true;
207 /* If symbol NODE is really part of other symbol's definition (i.e. it is
208 internal label, thunk, alias or so), return the outer symbol.
209 When add_symbol_to_partition_1 is called on the outer symbol it must
210 eventually add NODE, too. */
211 static symtab_node *
212 contained_in_symbol (symtab_node *node)
214 /* Weakrefs are never contained in anything. */
215 if (node->weakref)
216 return node;
217 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
219 cnode = cnode->function_symbol ();
220 if (cnode->global.inlined_to)
221 cnode = cnode->global.inlined_to;
222 return cnode;
224 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
225 return vnode->ultimate_alias_target ();
226 return node;
229 /* Add symbol NODE to partition. When definition of NODE is part
230 of other symbol definition, add the other symbol, too. */
232 static void
233 add_symbol_to_partition (ltrans_partition part, symtab_node *node)
235 symtab_node *node1;
237 /* Verify that we do not try to duplicate something that can not be. */
238 gcc_checking_assert (node->get_partitioning_class () == SYMBOL_DUPLICATE
239 || !symbol_partitioned_p (node));
241 while ((node1 = contained_in_symbol (node)) != node)
242 node = node1;
244 /* If we have duplicated symbol contained in something we can not duplicate,
245 we are very badly screwed. The other way is possible, so we do not
246 assert this in add_symbol_to_partition_1.
248 Be lax about comdats; they may or may not be duplicated and we may
249 end up in need to duplicate keyed comdat because it has unkeyed alias. */
251 gcc_assert (node->get_partitioning_class () == SYMBOL_DUPLICATE
252 || DECL_COMDAT (node->decl)
253 || !symbol_partitioned_p (node));
255 add_symbol_to_partition_1 (part, node);
258 /* Undo all additions until number of cgraph nodes in PARITION is N_CGRAPH_NODES
259 and number of varpool nodes is N_VARPOOL_NODES. */
261 static void
262 undo_partition (ltrans_partition partition, unsigned int n_nodes)
264 while (lto_symtab_encoder_size (partition->encoder) > (int)n_nodes)
266 symtab_node *node = lto_symtab_encoder_deref (partition->encoder,
267 n_nodes);
268 partition->symbols--;
269 cgraph_node *cnode;
271 /* After UNDO we no longer know what was visited. */
272 if (partition->initializers_visited)
273 delete partition->initializers_visited;
274 partition->initializers_visited = NULL;
276 if (!node->alias && (cnode = dyn_cast <cgraph_node *> (node)))
277 partition->insns -= inline_summaries->get (cnode)->self_size;
278 lto_symtab_encoder_delete_node (partition->encoder, node);
279 node->aux = (void *)((size_t)node->aux - 1);
283 /* Group cgrah nodes by input files. This is used mainly for testing
284 right now. */
286 void
287 lto_1_to_1_map (void)
289 symtab_node *node;
290 struct lto_file_decl_data *file_data;
291 hash_map<lto_file_decl_data *, ltrans_partition> pmap;
292 ltrans_partition partition;
293 int npartitions = 0;
295 FOR_EACH_SYMBOL (node)
297 if (node->get_partitioning_class () != SYMBOL_PARTITION
298 || symbol_partitioned_p (node))
299 continue;
301 file_data = node->lto_file_data;
303 if (file_data)
305 ltrans_partition *slot = &pmap.get_or_insert (file_data);
306 if (*slot)
307 partition = *slot;
308 else
310 partition = new_partition (file_data->file_name);
311 *slot = partition;
312 npartitions++;
315 else if (!file_data && ltrans_partitions.length ())
316 partition = ltrans_partitions[0];
317 else
319 partition = new_partition ("");
320 pmap.put (NULL, partition);
321 npartitions++;
324 add_symbol_to_partition (partition, node);
327 /* If the cgraph is empty, create one cgraph node set so that there is still
328 an output file for any variables that need to be exported in a DSO. */
329 if (!npartitions)
330 new_partition ("empty");
334 /* Maximal partitioning. Put every new symbol into new partition if possible. */
336 void
337 lto_max_map (void)
339 symtab_node *node;
340 ltrans_partition partition;
341 int npartitions = 0;
343 FOR_EACH_SYMBOL (node)
345 if (node->get_partitioning_class () != SYMBOL_PARTITION
346 || symbol_partitioned_p (node))
347 continue;
348 partition = new_partition (node->asm_name ());
349 add_symbol_to_partition (partition, node);
350 npartitions++;
352 if (!npartitions)
353 new_partition ("empty");
356 /* Helper function for qsort; sort nodes by order. noreorder functions must have
357 been removed earlier. */
358 static int
359 node_cmp (const void *pa, const void *pb)
361 const struct cgraph_node *a = *(const struct cgraph_node * const *) pa;
362 const struct cgraph_node *b = *(const struct cgraph_node * const *) pb;
364 /* Profile reorder flag enables function reordering based on first execution
365 of a function. All functions with profile are placed in ascending
366 order at the beginning. */
368 if (flag_profile_reorder_functions)
370 /* Functions with time profile are sorted in ascending order. */
371 if (a->tp_first_run && b->tp_first_run)
372 return a->tp_first_run != b->tp_first_run
373 ? a->tp_first_run - b->tp_first_run
374 : a->order - b->order;
376 /* Functions with time profile are sorted before the functions
377 that do not have the profile. */
378 if (a->tp_first_run || b->tp_first_run)
379 return b->tp_first_run - a->tp_first_run;
382 return b->order - a->order;
385 /* Helper function for qsort; sort nodes by order. */
386 static int
387 varpool_node_cmp (const void *pa, const void *pb)
389 const symtab_node *a = *static_cast<const symtab_node * const *> (pa);
390 const symtab_node *b = *static_cast<const symtab_node * const *> (pb);
391 return b->order - a->order;
394 /* Add all symtab nodes from NEXT_NODE to PARTITION in order. */
396 static void
397 add_sorted_nodes (vec<symtab_node *> &next_nodes, ltrans_partition partition)
399 unsigned i;
400 symtab_node *node;
402 next_nodes.qsort (varpool_node_cmp);
403 FOR_EACH_VEC_ELT (next_nodes, i, node)
404 if (!symbol_partitioned_p (node))
405 add_symbol_to_partition (partition, node);
409 /* Group cgraph nodes into equally-sized partitions.
411 The partitioning algorithm is simple: nodes are taken in predefined order.
412 The order corresponds to the order we want functions to have in the final
413 output. In the future this will be given by function reordering pass, but
414 at the moment we use the topological order, which is a good approximation.
416 The goal is to partition this linear order into intervals (partitions) so
417 that all the partitions have approximately the same size and the number of
418 callgraph or IPA reference edges crossing boundaries is minimal.
420 This is a lot faster (O(n) in size of callgraph) than algorithms doing
421 priority-based graph clustering that are generally O(n^2) and, since
422 WHOPR is designed to make things go well across partitions, it leads
423 to good results.
425 We compute the expected size of a partition as:
427 max (total_size / lto_partitions, min_partition_size)
429 We use dynamic expected size of partition so small programs are partitioned
430 into enough partitions to allow use of multiple CPUs, while large programs
431 are not partitioned too much. Creating too many partitions significantly
432 increases the streaming overhead.
434 In the future, we would like to bound the maximal size of partitions so as
435 to prevent the LTRANS stage from consuming too much memory. At the moment,
436 however, the WPA stage is the most memory intensive for large benchmarks,
437 since too many types and declarations are read into memory.
439 The function implements a simple greedy algorithm. Nodes are being added
440 to the current partition until after 3/4 of the expected partition size is
441 reached. Past this threshold, we keep track of boundary size (number of
442 edges going to other partitions) and continue adding functions until after
443 the current partition has grown to twice the expected partition size. Then
444 the process is undone to the point where the minimal ratio of boundary size
445 and in-partition calls was reached. */
447 void
448 lto_balanced_map (int n_lto_partitions)
450 int n_nodes = 0;
451 int n_varpool_nodes = 0, varpool_pos = 0, best_varpool_pos = 0;
452 struct cgraph_node **order = XNEWVEC (cgraph_node *, symtab->cgraph_max_uid);
453 auto_vec<cgraph_node *> noreorder;
454 auto_vec<varpool_node *> varpool_order;
455 int i;
456 struct cgraph_node *node;
457 int original_total_size, total_size = 0, best_total_size = 0;
458 int partition_size;
459 ltrans_partition partition;
460 int last_visited_node = 0;
461 varpool_node *vnode;
462 int cost = 0, internal = 0;
463 int best_n_nodes = 0, best_i = 0, best_cost =
464 INT_MAX, best_internal = 0;
465 int npartitions;
466 int current_order = -1;
467 int noreorder_pos = 0;
469 FOR_EACH_VARIABLE (vnode)
470 gcc_assert (!vnode->aux);
472 FOR_EACH_DEFINED_FUNCTION (node)
473 if (node->get_partitioning_class () == SYMBOL_PARTITION)
475 if (node->no_reorder)
476 noreorder.safe_push (node);
477 else
478 order[n_nodes++] = node;
479 if (!node->alias)
480 total_size += inline_summaries->get (node)->size;
483 original_total_size = total_size;
485 /* Streaming works best when the source units do not cross partition
486 boundaries much. This is because importing function from a source
487 unit tends to import a lot of global trees defined there. We should
488 get better about minimizing the function bounday, but until that
489 things works smoother if we order in source order. */
490 qsort (order, n_nodes, sizeof (struct cgraph_node *), node_cmp);
491 noreorder.qsort (node_cmp);
493 if (symtab->dump_file)
495 for(i = 0; i < n_nodes; i++)
496 fprintf (symtab->dump_file, "Balanced map symbol order:%s:%u\n",
497 order[i]->name (), order[i]->tp_first_run);
498 for(i = 0; i < (int)noreorder.length(); i++)
499 fprintf (symtab->dump_file, "Balanced map symbol no_reorder:%s:%u\n",
500 noreorder[i]->name (), noreorder[i]->tp_first_run);
503 /* Collect all variables that should not be reordered. */
504 FOR_EACH_VARIABLE (vnode)
505 if (vnode->get_partitioning_class () == SYMBOL_PARTITION
506 && (!flag_toplevel_reorder || vnode->no_reorder))
507 varpool_order.safe_push (vnode);
508 n_varpool_nodes = varpool_order.length ();
509 varpool_order.qsort (varpool_node_cmp);
511 /* Compute partition size and create the first partition. */
512 partition_size = total_size / n_lto_partitions;
513 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
514 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
515 npartitions = 1;
516 partition = new_partition ("");
517 if (symtab->dump_file)
518 fprintf (symtab->dump_file, "Total unit size: %i, partition size: %i\n",
519 total_size, partition_size);
521 auto_vec<symtab_node *> next_nodes;
523 for (i = 0; i < n_nodes; i++)
525 if (symbol_partitioned_p (order[i]))
526 continue;
528 current_order = order[i]->order;
530 /* Output noreorder and varpool in program order first. */
531 next_nodes.truncate (0);
532 while (varpool_pos < n_varpool_nodes
533 && varpool_order[varpool_pos]->order < current_order)
534 next_nodes.safe_push (varpool_order[varpool_pos++]);
535 while (noreorder_pos < (int)noreorder.length ()
536 && noreorder[noreorder_pos]->order < current_order)
538 if (!noreorder[noreorder_pos]->alias)
539 total_size -= inline_summaries->get (noreorder[noreorder_pos])->size;
540 next_nodes.safe_push (noreorder[noreorder_pos++]);
542 add_sorted_nodes (next_nodes, partition);
544 add_symbol_to_partition (partition, order[i]);
545 if (!order[i]->alias)
546 total_size -= inline_summaries->get (order[i])->size;
549 /* Once we added a new node to the partition, we also want to add
550 all referenced variables unless they was already added into some
551 earlier partition.
552 add_symbol_to_partition adds possibly multiple nodes and
553 variables that are needed to satisfy needs of ORDER[i].
554 We remember last visited cgraph and varpool node from last iteration
555 of outer loop that allows us to process every new addition.
557 At the same time we compute size of the boundary into COST. Every
558 callgraph or IPA reference edge leaving the partition contributes into
559 COST. Every edge inside partition was earlier computed as one leaving
560 it and thus we need to subtract it from COST. */
561 while (last_visited_node < lto_symtab_encoder_size (partition->encoder))
563 symtab_node *refs_node;
564 int j;
565 struct ipa_ref *ref = NULL;
566 symtab_node *snode = lto_symtab_encoder_deref (partition->encoder,
567 last_visited_node);
569 if (cgraph_node *node = dyn_cast <cgraph_node *> (snode))
571 struct cgraph_edge *edge;
573 refs_node = node;
575 last_visited_node++;
577 gcc_assert (node->definition || node->weakref);
579 /* Compute boundary cost of callgraph edges. */
580 for (edge = node->callees; edge; edge = edge->next_callee)
581 if (edge->callee->definition)
583 int edge_cost = edge->frequency;
584 int index;
586 if (!edge_cost)
587 edge_cost = 1;
588 gcc_assert (edge_cost > 0);
589 index = lto_symtab_encoder_lookup (partition->encoder,
590 edge->callee);
591 if (index != LCC_NOT_FOUND
592 && index < last_visited_node - 1)
593 cost -= edge_cost, internal += edge_cost;
594 else
595 cost += edge_cost;
597 for (edge = node->callers; edge; edge = edge->next_caller)
599 int edge_cost = edge->frequency;
600 int index;
602 gcc_assert (edge->caller->definition);
603 if (!edge_cost)
604 edge_cost = 1;
605 gcc_assert (edge_cost > 0);
606 index = lto_symtab_encoder_lookup (partition->encoder,
607 edge->caller);
608 if (index != LCC_NOT_FOUND
609 && index < last_visited_node - 1)
610 cost -= edge_cost;
611 else
612 cost += edge_cost;
615 else
617 refs_node = snode;
618 last_visited_node++;
621 /* Compute boundary cost of IPA REF edges and at the same time look into
622 variables referenced from current partition and try to add them. */
623 for (j = 0; refs_node->iterate_reference (j, ref); j++)
624 if (is_a <varpool_node *> (ref->referred))
626 int index;
628 vnode = dyn_cast <varpool_node *> (ref->referred);
629 if (!vnode->definition)
630 continue;
631 if (!symbol_partitioned_p (vnode) && flag_toplevel_reorder
632 && !vnode->no_reorder
633 && vnode->get_partitioning_class () == SYMBOL_PARTITION)
634 add_symbol_to_partition (partition, vnode);
635 index = lto_symtab_encoder_lookup (partition->encoder,
636 vnode);
637 if (index != LCC_NOT_FOUND
638 && index < last_visited_node - 1)
639 cost--, internal++;
640 else
641 cost++;
643 else
645 int index;
647 node = dyn_cast <cgraph_node *> (ref->referred);
648 if (!node->definition)
649 continue;
650 index = lto_symtab_encoder_lookup (partition->encoder,
651 node);
652 if (index != LCC_NOT_FOUND
653 && index < last_visited_node - 1)
654 cost--, internal++;
655 else
656 cost++;
658 for (j = 0; refs_node->iterate_referring (j, ref); j++)
659 if (is_a <varpool_node *> (ref->referring))
661 int index;
663 vnode = dyn_cast <varpool_node *> (ref->referring);
664 gcc_assert (vnode->definition);
665 /* It is better to couple variables with their users, because it allows them
666 to be removed. Coupling with objects they refer to only helps to reduce
667 number of symbols promoted to hidden. */
668 if (!symbol_partitioned_p (vnode) && flag_toplevel_reorder
669 && !vnode->no_reorder
670 && !vnode->can_remove_if_no_refs_p ()
671 && vnode->get_partitioning_class () == SYMBOL_PARTITION)
672 add_symbol_to_partition (partition, vnode);
673 index = lto_symtab_encoder_lookup (partition->encoder,
674 vnode);
675 if (index != LCC_NOT_FOUND
676 && index < last_visited_node - 1)
677 cost--;
678 else
679 cost++;
681 else
683 int index;
685 node = dyn_cast <cgraph_node *> (ref->referring);
686 gcc_assert (node->definition);
687 index = lto_symtab_encoder_lookup (partition->encoder,
688 node);
689 if (index != LCC_NOT_FOUND
690 && index < last_visited_node - 1)
691 cost--;
692 else
693 cost++;
697 /* If the partition is large enough, start looking for smallest boundary cost. */
698 if (partition->insns < partition_size * 3 / 4
699 || best_cost == INT_MAX
700 || ((!cost
701 || (best_internal * (HOST_WIDE_INT) cost
702 > (internal * (HOST_WIDE_INT)best_cost)))
703 && partition->insns < partition_size * 5 / 4))
705 best_cost = cost;
706 best_internal = internal;
707 best_i = i;
708 best_n_nodes = lto_symtab_encoder_size (partition->encoder);
709 best_total_size = total_size;
710 best_varpool_pos = varpool_pos;
712 if (symtab->dump_file)
713 fprintf (symtab->dump_file, "Step %i: added %s/%i, size %i, cost %i/%i "
714 "best %i/%i, step %i\n", i,
715 order[i]->name (), order[i]->order,
716 partition->insns, cost, internal,
717 best_cost, best_internal, best_i);
718 /* Partition is too large, unwind into step when best cost was reached and
719 start new partition. */
720 if (partition->insns > 2 * partition_size)
722 if (best_i != i)
724 if (symtab->dump_file)
725 fprintf (symtab->dump_file, "Unwinding %i insertions to step %i\n",
726 i - best_i, best_i);
727 undo_partition (partition, best_n_nodes);
728 varpool_pos = best_varpool_pos;
730 i = best_i;
731 /* When we are finished, avoid creating empty partition. */
732 while (i < n_nodes - 1 && symbol_partitioned_p (order[i + 1]))
733 i++;
734 if (i == n_nodes - 1)
735 break;
736 partition = new_partition ("");
737 last_visited_node = 0;
738 total_size = best_total_size;
739 cost = 0;
741 if (symtab->dump_file)
742 fprintf (symtab->dump_file, "New partition\n");
743 best_n_nodes = 0;
744 best_cost = INT_MAX;
746 /* Since the size of partitions is just approximate, update the size after
747 we finished current one. */
748 if (npartitions < n_lto_partitions)
749 partition_size = total_size / (n_lto_partitions - npartitions);
750 else
751 partition_size = INT_MAX;
753 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
754 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
755 npartitions ++;
759 next_nodes.truncate (0);
761 /* Varables that are not reachable from the code go into last partition. */
762 if (flag_toplevel_reorder)
764 FOR_EACH_VARIABLE (vnode)
765 if (vnode->get_partitioning_class () == SYMBOL_PARTITION
766 && !symbol_partitioned_p (vnode)
767 && !vnode->no_reorder)
768 next_nodes.safe_push (vnode);
771 /* Output remaining ordered symbols. */
772 while (varpool_pos < n_varpool_nodes)
773 next_nodes.safe_push (varpool_order[varpool_pos++]);
774 while (noreorder_pos < (int)noreorder.length ())
775 next_nodes.safe_push (noreorder[noreorder_pos++]);
776 add_sorted_nodes (next_nodes, partition);
778 free (order);
780 if (symtab->dump_file)
782 fprintf (symtab->dump_file, "\nPartition sizes:\n");
783 unsigned partitions = ltrans_partitions.length ();
785 for (unsigned i = 0; i < partitions ; i++)
787 ltrans_partition p = ltrans_partitions[i];
788 fprintf (symtab->dump_file, "partition %d contains %d (%2.2f%%)"
789 " symbols and %d (%2.2f%%) insns\n", i, p->symbols,
790 100.0 * p->symbols / n_nodes, p->insns,
791 100.0 * p->insns / original_total_size);
794 fprintf (symtab->dump_file, "\n");
798 /* Return true if we must not change the name of the NODE. The name as
799 extracted from the corresponding decl should be passed in NAME. */
801 static bool
802 must_not_rename (symtab_node *node, const char *name)
804 /* Our renaming machinery do not handle more than one change of assembler name.
805 We should not need more than one anyway. */
806 if (node->lto_file_data
807 && lto_get_decl_name_mapping (node->lto_file_data, name) != name)
809 if (symtab->dump_file)
810 fprintf (symtab->dump_file,
811 "Not privatizing symbol name: %s. It privatized already.\n",
812 name);
813 return true;
815 /* Avoid mangling of already mangled clones.
816 ??? should have a flag whether a symbol has a 'private' name already,
817 since we produce some symbols like that i.e. for global constructors
818 that are not really clones. */
819 if (node->unique_name)
821 if (symtab->dump_file)
822 fprintf (symtab->dump_file,
823 "Not privatizing symbol name: %s. Has unique name.\n",
824 name);
825 return true;
827 return false;
830 /* If we are an offload compiler, we may have to rewrite symbols to be
831 valid on this target. Return either PTR or a modified version of it. */
833 static const char *
834 maybe_rewrite_identifier (const char *ptr)
836 #if defined ACCEL_COMPILER && (defined NO_DOT_IN_LABEL || defined NO_DOLLAR_IN_LABEL)
837 #ifndef NO_DOT_IN_LABEL
838 char valid = '.';
839 const char reject[] = "$";
840 #elif !defined NO_DOLLAR_IN_LABEL
841 char valid = '$';
842 const char reject[] = ".";
843 #else
844 char valid = '_';
845 const char reject[] = ".$";
846 #endif
848 char *copy = NULL;
849 const char *match = ptr;
850 for (;;)
852 size_t off = strcspn (match, reject);
853 if (match[off] == '\0')
854 break;
855 if (copy == NULL)
857 copy = xstrdup (ptr);
858 match = copy;
860 copy[off] = valid;
862 return match;
863 #else
864 return ptr;
865 #endif
868 /* Ensure that the symbol in NODE is valid for the target, and if not,
869 rewrite it. */
871 static void
872 validize_symbol_for_target (symtab_node *node)
874 tree decl = node->decl;
875 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
877 if (must_not_rename (node, name))
878 return;
880 const char *name2 = maybe_rewrite_identifier (name);
881 if (name2 != name)
883 symtab->change_decl_assembler_name (decl, get_identifier (name2));
884 if (node->lto_file_data)
885 lto_record_renamed_decl (node->lto_file_data, name,
886 IDENTIFIER_POINTER
887 (DECL_ASSEMBLER_NAME (decl)));
891 /* Helper for privatize_symbol_name. Mangle NODE symbol name
892 represented by DECL. */
894 static bool
895 privatize_symbol_name_1 (symtab_node *node, tree decl)
897 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
899 if (must_not_rename (node, name))
900 return false;
902 name = maybe_rewrite_identifier (name);
903 symtab->change_decl_assembler_name (decl,
904 clone_function_name_1 (name,
905 "lto_priv"));
907 if (node->lto_file_data)
908 lto_record_renamed_decl (node->lto_file_data, name,
909 IDENTIFIER_POINTER
910 (DECL_ASSEMBLER_NAME (decl)));
912 if (symtab->dump_file)
913 fprintf (symtab->dump_file,
914 "Privatizing symbol name: %s -> %s\n",
915 name, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
917 return true;
920 /* Mangle NODE symbol name into a local name.
921 This is necessary to do
922 1) if two or more static vars of same assembler name
923 are merged into single ltrans unit.
924 2) if previously static var was promoted hidden to avoid possible conflict
925 with symbols defined out of the LTO world. */
927 static bool
928 privatize_symbol_name (symtab_node *node)
930 if (!privatize_symbol_name_1 (node, node->decl))
931 return false;
933 /* We could change name which is a target of transparent alias
934 chain of instrumented function name. Fix alias chain if so .*/
935 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
937 tree iname = NULL_TREE;
938 if (cnode->instrumentation_clone)
940 /* If we want to privatize instrumentation clone
941 then we also need to privatize original function. */
942 if (cnode->instrumented_version)
943 privatize_symbol_name (cnode->instrumented_version);
944 else
945 privatize_symbol_name_1 (cnode, cnode->orig_decl);
946 iname = DECL_ASSEMBLER_NAME (cnode->decl);
947 TREE_CHAIN (iname) = DECL_ASSEMBLER_NAME (cnode->orig_decl);
949 else if (cnode->instrumented_version
950 && cnode->instrumented_version->orig_decl == cnode->decl)
952 iname = DECL_ASSEMBLER_NAME (cnode->instrumented_version->decl);
953 TREE_CHAIN (iname) = DECL_ASSEMBLER_NAME (cnode->decl);
957 return true;
960 /* Promote variable VNODE to be static. */
962 static void
963 promote_symbol (symtab_node *node)
965 /* We already promoted ... */
966 if (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
967 && DECL_VISIBILITY_SPECIFIED (node->decl)
968 && TREE_PUBLIC (node->decl))
970 validize_symbol_for_target (node);
971 return;
974 gcc_checking_assert (!TREE_PUBLIC (node->decl)
975 && !DECL_EXTERNAL (node->decl));
976 /* Be sure that newly public symbol does not conflict with anything already
977 defined by the non-LTO part. */
978 privatize_symbol_name (node);
979 TREE_PUBLIC (node->decl) = 1;
980 DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
981 DECL_VISIBILITY_SPECIFIED (node->decl) = true;
982 if (symtab->dump_file)
983 fprintf (symtab->dump_file,
984 "Promoting as hidden: %s\n", node->name ());
987 /* Return true if NODE needs named section even if it won't land in the partition
988 symbol table.
989 FIXME: we should really not use named sections for inline clones and master clones. */
991 static bool
992 may_need_named_section_p (lto_symtab_encoder_t encoder, symtab_node *node)
994 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
995 if (!cnode)
996 return false;
997 if (node->real_symbol_p ())
998 return false;
999 return (!encoder
1000 || (lto_symtab_encoder_lookup (encoder, node) != LCC_NOT_FOUND
1001 && lto_symtab_encoder_encode_body_p (encoder,
1002 cnode)));
1005 /* If NODE represents a static variable. See if there are other variables
1006 of the same name in partition ENCODER (or in whole compilation unit if
1007 ENCODER is NULL) and if so, mangle the statics. Always mangle all
1008 conflicting statics, so we reduce changes of silently miscompiling
1009 asm statements referring to them by symbol name. */
1011 static void
1012 rename_statics (lto_symtab_encoder_t encoder, symtab_node *node)
1014 tree decl = node->decl;
1015 symtab_node *s;
1016 tree name = DECL_ASSEMBLER_NAME (decl);
1018 /* See if this is static symbol. */
1019 if ((node->externally_visible
1020 /* FIXME: externally_visible is somewhat illogically not set for
1021 external symbols (i.e. those not defined). Remove this test
1022 once this is fixed. */
1023 || DECL_EXTERNAL (node->decl)
1024 || !node->real_symbol_p ())
1025 && !may_need_named_section_p (encoder, node))
1026 return;
1028 /* Now walk symbols sharing the same name and see if there are any conflicts.
1029 (all types of symbols counts here, since we can not have static of the
1030 same name as external or public symbol.) */
1031 for (s = symtab_node::get_for_asmname (name);
1032 s; s = s->next_sharing_asm_name)
1033 if ((s->real_symbol_p () || may_need_named_section_p (encoder, s))
1034 && s->decl != node->decl
1035 && (!encoder
1036 || lto_symtab_encoder_lookup (encoder, s) != LCC_NOT_FOUND))
1037 break;
1039 /* OK, no confict, so we have nothing to do. */
1040 if (!s)
1041 return;
1043 if (symtab->dump_file)
1044 fprintf (symtab->dump_file,
1045 "Renaming statics with asm name: %s\n", node->name ());
1047 /* Assign every symbol in the set that shares the same ASM name an unique
1048 mangled name. */
1049 for (s = symtab_node::get_for_asmname (name); s;)
1050 if (!s->externally_visible
1051 && ((s->real_symbol_p ()
1052 && !DECL_EXTERNAL (node->decl)
1053 && !TREE_PUBLIC (node->decl))
1054 || may_need_named_section_p (encoder, s))
1055 && (!encoder
1056 || lto_symtab_encoder_lookup (encoder, s) != LCC_NOT_FOUND))
1058 if (privatize_symbol_name (s))
1059 /* Re-start from beginning since we do not know how many symbols changed a name. */
1060 s = symtab_node::get_for_asmname (name);
1061 else s = s->next_sharing_asm_name;
1063 else s = s->next_sharing_asm_name;
1066 /* Find out all static decls that need to be promoted to global because
1067 of cross file sharing. This function must be run in the WPA mode after
1068 all inlinees are added. */
1070 void
1071 lto_promote_cross_file_statics (void)
1073 unsigned i, n_sets;
1075 gcc_assert (flag_wpa);
1077 lto_stream_offload_p = false;
1078 select_what_to_stream ();
1080 /* First compute boundaries. */
1081 n_sets = ltrans_partitions.length ();
1082 for (i = 0; i < n_sets; i++)
1084 ltrans_partition part
1085 = ltrans_partitions[i];
1086 part->encoder = compute_ltrans_boundary (part->encoder);
1089 /* Look at boundaries and promote symbols as needed. */
1090 for (i = 0; i < n_sets; i++)
1092 lto_symtab_encoder_iterator lsei;
1093 lto_symtab_encoder_t encoder = ltrans_partitions[i]->encoder;
1095 for (lsei = lsei_start (encoder); !lsei_end_p (lsei);
1096 lsei_next (&lsei))
1098 symtab_node *node = lsei_node (lsei);
1100 /* If symbol is static, rename it if its assembler name clash with
1101 anything else in this unit. */
1102 rename_statics (encoder, node);
1104 /* No need to promote if symbol already is externally visible ... */
1105 if (node->externally_visible
1106 /* ... or if it is part of current partition ... */
1107 || lto_symtab_encoder_in_partition_p (encoder, node)
1108 /* ... or if we do not partition it. This mean that it will
1109 appear in every partition refernecing it. */
1110 || node->get_partitioning_class () != SYMBOL_PARTITION)
1112 validize_symbol_for_target (node);
1113 continue;
1116 promote_symbol (node);
1121 /* Rename statics in the whole unit in the case that
1122 we do -flto-partition=none. */
1124 void
1125 lto_promote_statics_nonwpa (void)
1127 symtab_node *node;
1128 FOR_EACH_SYMBOL (node)
1130 rename_statics (NULL, node);
1131 validize_symbol_for_target (node);