svn merge -r215707:216846 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / gcc / lto / lto-partition.c
blob0a0f50d28f9cd101e0a604a40a3c3a602dbc8477
1 /* LTO partitioning logic routines.
2 Copyright (C) 2009-2014 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 "tree.h"
25 #include "predict.h"
26 #include "vec.h"
27 #include "hashtab.h"
28 #include "hash-set.h"
29 #include "machmode.h"
30 #include "tm.h"
31 #include "hard-reg-set.h"
32 #include "input.h"
33 #include "function.h"
34 #include "basic-block.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "gimple-expr.h"
38 #include "is-a.h"
39 #include "gimple.h"
40 #include "hash-map.h"
41 #include "plugin-api.h"
42 #include "ipa-ref.h"
43 #include "cgraph.h"
44 #include "lto-streamer.h"
45 #include "timevar.h"
46 #include "params.h"
47 #include "alloc-pool.h"
48 #include "ipa-prop.h"
49 #include "ipa-inline.h"
50 #include "ipa-utils.h"
51 #include "lto-partition.h"
53 vec<ltrans_partition> ltrans_partitions;
55 static void add_symbol_to_partition (ltrans_partition part, symtab_node *node);
58 /* Create new partition with name NAME. */
60 static ltrans_partition
61 new_partition (const char *name)
63 ltrans_partition part = XCNEW (struct ltrans_partition_def);
64 part->encoder = lto_symtab_encoder_new (false);
65 part->name = name;
66 part->insns = 0;
67 ltrans_partitions.safe_push (part);
68 return part;
71 /* Free memory used by ltrans datastructures. */
73 void
74 free_ltrans_partitions (void)
76 unsigned int idx;
77 ltrans_partition part;
78 for (idx = 0; ltrans_partitions.iterate (idx, &part); idx++)
80 if (part->initializers_visited)
81 delete part->initializers_visited;
82 /* Symtab encoder is freed after streaming. */
83 free (part);
85 ltrans_partitions.release ();
88 /* Return true if symbol is already in some partition. */
90 static inline bool
91 symbol_partitioned_p (symtab_node *node)
93 return node->aux;
96 /* Add references into the partition. */
97 static void
98 add_references_to_partition (ltrans_partition part, symtab_node *node)
100 int i;
101 struct ipa_ref *ref = NULL;
103 /* Add all duplicated references to the partition. */
104 for (i = 0; node->iterate_reference (i, ref); i++)
105 if (ref->referred->get_partitioning_class () == SYMBOL_DUPLICATE)
106 add_symbol_to_partition (part, ref->referred);
107 /* References to a readonly variable may be constant foled into its value.
108 Recursively look into the initializers of the constant variable and add
109 references, too. */
110 else if (is_a <varpool_node *> (ref->referred)
111 && dyn_cast <varpool_node *> (ref->referred)
112 ->ctor_useable_for_folding_p ()
113 && !lto_symtab_encoder_in_partition_p (part->encoder, ref->referred))
115 if (!part->initializers_visited)
116 part->initializers_visited = new hash_set<symtab_node *>;
117 if (!part->initializers_visited->add (ref->referred))
118 add_references_to_partition (part, ref->referred);
122 /* Helper function for add_symbol_to_partition doing the actual dirty work
123 of adding NODE to PART. */
125 static bool
126 add_symbol_to_partition_1 (ltrans_partition part, symtab_node *node)
128 enum symbol_partitioning_class c = node->get_partitioning_class ();
129 struct ipa_ref *ref;
130 symtab_node *node1;
132 /* If NODE is already there, we have nothing to do. */
133 if (lto_symtab_encoder_in_partition_p (part->encoder, node))
134 return true;
136 /* non-duplicated aliases or tunks of a duplicated symbol needs to be output
137 just once.
139 Be lax about comdats; they may or may not be duplicated and we may
140 end up in need to duplicate keyed comdat because it has unkeyed alias. */
141 if (c == SYMBOL_PARTITION && !DECL_COMDAT (node->decl)
142 && symbol_partitioned_p (node))
143 return false;
145 /* Be sure that we never try to duplicate partitioned symbol
146 or add external symbol. */
147 gcc_assert (c != SYMBOL_EXTERNAL
148 && (c == SYMBOL_DUPLICATE || !symbol_partitioned_p (node)));
150 node->need_dump = true;
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_summary (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);
182 add_references_to_partition (part, node);
184 /* Add all aliases associated with the symbol. */
186 FOR_EACH_ALIAS (node, ref)
187 if (!node->weakref)
188 add_symbol_to_partition_1 (part, ref->referring);
190 /* Ensure that SAME_COMDAT_GROUP lists all allways added in a group. */
191 if (node->same_comdat_group)
192 for (node1 = node->same_comdat_group;
193 node1 != node; node1 = node1->same_comdat_group)
194 if (!node->alias)
196 bool added = add_symbol_to_partition_1 (part, node1);
197 gcc_assert (added);
199 return true;
202 /* If symbol NODE is really part of other symbol's definition (i.e. it is
203 internal label, thunk, alias or so), return the outer symbol.
204 When add_symbol_to_partition_1 is called on the outer symbol it must
205 eventually add NODE, too. */
206 static symtab_node *
207 contained_in_symbol (symtab_node *node)
209 /* Weakrefs are never contained in anything. */
210 if (node->weakref)
211 return node;
212 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
214 cnode = cnode->function_symbol ();
215 if (cnode->global.inlined_to)
216 cnode = cnode->global.inlined_to;
217 return cnode;
219 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
220 return vnode->ultimate_alias_target ();
221 return node;
224 /* Add symbol NODE to partition. When definition of NODE is part
225 of other symbol definition, add the other symbol, too. */
227 static void
228 add_symbol_to_partition (ltrans_partition part, symtab_node *node)
230 symtab_node *node1;
232 /* Verify that we do not try to duplicate something that can not be. */
233 gcc_checking_assert (node->get_partitioning_class () == SYMBOL_DUPLICATE
234 || !symbol_partitioned_p (node));
236 while ((node1 = contained_in_symbol (node)) != node)
237 node = node1;
239 /* If we have duplicated symbol contained in something we can not duplicate,
240 we are very badly screwed. The other way is possible, so we do not
241 assert this in add_symbol_to_partition_1.
243 Be lax about comdats; they may or may not be duplicated and we may
244 end up in need to duplicate keyed comdat because it has unkeyed alias. */
246 gcc_assert (node->get_partitioning_class () == SYMBOL_DUPLICATE
247 || DECL_COMDAT (node->decl)
248 || !symbol_partitioned_p (node));
250 add_symbol_to_partition_1 (part, node);
253 /* Undo all additions until number of cgraph nodes in PARITION is N_CGRAPH_NODES
254 and number of varpool nodes is N_VARPOOL_NODES. */
256 static void
257 undo_partition (ltrans_partition partition, unsigned int n_nodes)
259 while (lto_symtab_encoder_size (partition->encoder) > (int)n_nodes)
261 symtab_node *node = lto_symtab_encoder_deref (partition->encoder,
262 n_nodes);
263 cgraph_node *cnode;
265 /* After UNDO we no longer know what was visited. */
266 if (partition->initializers_visited)
267 delete partition->initializers_visited;
268 partition->initializers_visited = NULL;
270 if (!node->alias && (cnode = dyn_cast <cgraph_node *> (node)))
271 partition->insns -= inline_summary (cnode)->self_size;
272 lto_symtab_encoder_delete_node (partition->encoder, node);
273 node->aux = (void *)((size_t)node->aux - 1);
277 /* Group cgrah nodes by input files. This is used mainly for testing
278 right now. */
280 void
281 lto_1_to_1_map (void)
283 symtab_node *node;
284 struct lto_file_decl_data *file_data;
285 hash_map<lto_file_decl_data *, ltrans_partition> pmap;
286 ltrans_partition partition;
287 int npartitions = 0;
289 FOR_EACH_SYMBOL (node)
291 if (node->get_partitioning_class () != SYMBOL_PARTITION
292 || symbol_partitioned_p (node))
293 continue;
295 file_data = node->lto_file_data;
297 if (file_data)
299 ltrans_partition *slot = &pmap.get_or_insert (file_data);
300 if (*slot)
301 partition = *slot;
302 else
304 partition = new_partition (file_data->file_name);
305 *slot = partition;
306 npartitions++;
309 else if (!file_data && ltrans_partitions.length ())
310 partition = ltrans_partitions[0];
311 else
313 partition = new_partition ("");
314 pmap.put (NULL, partition);
315 npartitions++;
318 add_symbol_to_partition (partition, node);
321 /* If the cgraph is empty, create one cgraph node set so that there is still
322 an output file for any variables that need to be exported in a DSO. */
323 if (!npartitions)
324 new_partition ("empty");
328 /* Maximal partitioning. Put every new symbol into new partition if possible. */
330 void
331 lto_max_map (void)
333 symtab_node *node;
334 ltrans_partition partition;
335 int npartitions = 0;
337 FOR_EACH_SYMBOL (node)
339 if (node->get_partitioning_class () != SYMBOL_PARTITION
340 || symbol_partitioned_p (node))
341 continue;
342 partition = new_partition (node->asm_name ());
343 add_symbol_to_partition (partition, node);
344 npartitions++;
346 if (!npartitions)
347 new_partition ("empty");
350 /* Helper function for qsort; sort nodes by order. noreorder functions must have
351 been removed earlier. */
352 static int
353 node_cmp (const void *pa, const void *pb)
355 const struct cgraph_node *a = *(const struct cgraph_node * const *) pa;
356 const struct cgraph_node *b = *(const struct cgraph_node * const *) pb;
358 /* Profile reorder flag enables function reordering based on first execution
359 of a function. All functions with profile are placed in ascending
360 order at the beginning. */
362 if (flag_profile_reorder_functions)
364 /* Functions with time profile are sorted in ascending order. */
365 if (a->tp_first_run && b->tp_first_run)
366 return a->tp_first_run != b->tp_first_run
367 ? a->tp_first_run - b->tp_first_run
368 : a->order - b->order;
370 /* Functions with time profile are sorted before the functions
371 that do not have the profile. */
372 if (a->tp_first_run || b->tp_first_run)
373 return b->tp_first_run - a->tp_first_run;
376 return b->order - a->order;
379 /* Helper function for qsort; sort nodes by order. */
380 static int
381 varpool_node_cmp (const void *pa, const void *pb)
383 const symtab_node *a = *static_cast<const symtab_node * const *> (pa);
384 const symtab_node *b = *static_cast<const symtab_node * const *> (pb);
385 return b->order - a->order;
388 /* Add all symtab nodes from NEXT_NODE to PARTITION in order. */
390 static void
391 add_sorted_nodes (vec<symtab_node *> &next_nodes, ltrans_partition partition)
393 unsigned i;
394 symtab_node *node;
396 next_nodes.qsort (varpool_node_cmp);
397 FOR_EACH_VEC_ELT (next_nodes, i, node)
398 if (!symbol_partitioned_p (node))
399 add_symbol_to_partition (partition, node);
403 /* Group cgraph nodes into equally-sized partitions.
405 The partitioning algorithm is simple: nodes are taken in predefined order.
406 The order corresponds to the order we want functions to have in the final
407 output. In the future this will be given by function reordering pass, but
408 at the moment we use the topological order, which is a good approximation.
410 The goal is to partition this linear order into intervals (partitions) so
411 that all the partitions have approximately the same size and the number of
412 callgraph or IPA reference edges crossing boundaries is minimal.
414 This is a lot faster (O(n) in size of callgraph) than algorithms doing
415 priority-based graph clustering that are generally O(n^2) and, since
416 WHOPR is designed to make things go well across partitions, it leads
417 to good results.
419 We compute the expected size of a partition as:
421 max (total_size / lto_partitions, min_partition_size)
423 We use dynamic expected size of partition so small programs are partitioned
424 into enough partitions to allow use of multiple CPUs, while large programs
425 are not partitioned too much. Creating too many partitions significantly
426 increases the streaming overhead.
428 In the future, we would like to bound the maximal size of partitions so as
429 to prevent the LTRANS stage from consuming too much memory. At the moment,
430 however, the WPA stage is the most memory intensive for large benchmarks,
431 since too many types and declarations are read into memory.
433 The function implements a simple greedy algorithm. Nodes are being added
434 to the current partition until after 3/4 of the expected partition size is
435 reached. Past this threshold, we keep track of boundary size (number of
436 edges going to other partitions) and continue adding functions until after
437 the current partition has grown to twice the expected partition size. Then
438 the process is undone to the point where the minimal ratio of boundary size
439 and in-partition calls was reached. */
441 void
442 lto_balanced_map (int n_lto_partitions)
444 int n_nodes = 0;
445 int n_varpool_nodes = 0, varpool_pos = 0, best_varpool_pos = 0;
446 struct cgraph_node **order = XNEWVEC (cgraph_node *, symtab->cgraph_max_uid);
447 auto_vec<cgraph_node *> noreorder;
448 auto_vec<varpool_node *> varpool_order;
449 int i;
450 struct cgraph_node *node;
451 int total_size = 0, best_total_size = 0;
452 int partition_size;
453 ltrans_partition partition;
454 int last_visited_node = 0;
455 varpool_node *vnode;
456 int cost = 0, internal = 0;
457 int best_n_nodes = 0, best_i = 0, best_cost =
458 INT_MAX, best_internal = 0;
459 int npartitions;
460 int current_order = -1;
461 int noreorder_pos = 0;
463 FOR_EACH_VARIABLE (vnode)
464 gcc_assert (!vnode->aux);
466 FOR_EACH_DEFINED_FUNCTION (node)
467 if (node->get_partitioning_class () == SYMBOL_PARTITION)
469 if (node->no_reorder)
470 noreorder.safe_push (node);
471 else
472 order[n_nodes++] = node;
473 if (!node->alias)
474 total_size += inline_summary (node)->size;
477 /* Streaming works best when the source units do not cross partition
478 boundaries much. This is because importing function from a source
479 unit tends to import a lot of global trees defined there. We should
480 get better about minimizing the function bounday, but until that
481 things works smoother if we order in source order. */
482 qsort (order, n_nodes, sizeof (struct cgraph_node *), node_cmp);
483 noreorder.qsort (node_cmp);
485 if (symtab->dump_file)
487 for(i = 0; i < n_nodes; i++)
488 fprintf (symtab->dump_file, "Balanced map symbol order:%s:%u\n",
489 order[i]->name (), order[i]->tp_first_run);
490 for(i = 0; i < (int)noreorder.length(); i++)
491 fprintf (symtab->dump_file, "Balanced map symbol no_reorder:%s:%u\n",
492 noreorder[i]->name (), noreorder[i]->tp_first_run);
495 /* Collect all variables that should not be reordered. */
496 FOR_EACH_VARIABLE (vnode)
497 if (vnode->get_partitioning_class () == SYMBOL_PARTITION
498 && (!flag_toplevel_reorder || vnode->no_reorder))
499 varpool_order.safe_push (vnode);
500 n_varpool_nodes = varpool_order.length ();
501 varpool_order.qsort (varpool_node_cmp);
503 /* Compute partition size and create the first partition. */
504 partition_size = total_size / n_lto_partitions;
505 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
506 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
507 npartitions = 1;
508 partition = new_partition ("");
509 if (symtab->dump_file)
510 fprintf (symtab->dump_file, "Total unit size: %i, partition size: %i\n",
511 total_size, partition_size);
513 auto_vec<symtab_node *> next_nodes;
515 for (i = 0; i < n_nodes; i++)
517 if (symbol_partitioned_p (order[i]))
518 continue;
520 current_order = order[i]->order;
522 /* Output noreorder and varpool in program order first. */
523 next_nodes.truncate (0);
524 while (varpool_pos < n_varpool_nodes
525 && varpool_order[varpool_pos]->order < current_order)
526 next_nodes.safe_push (varpool_order[varpool_pos++]);
527 while (noreorder_pos < (int)noreorder.length ()
528 && noreorder[noreorder_pos]->order < current_order)
530 if (!noreorder[noreorder_pos]->alias)
531 total_size -= inline_summary (noreorder[noreorder_pos])->size;
532 next_nodes.safe_push (noreorder[noreorder_pos++]);
534 add_sorted_nodes (next_nodes, partition);
536 add_symbol_to_partition (partition, order[i]);
537 if (!order[i]->alias)
538 total_size -= inline_summary (order[i])->size;
541 /* Once we added a new node to the partition, we also want to add
542 all referenced variables unless they was already added into some
543 earlier partition.
544 add_symbol_to_partition adds possibly multiple nodes and
545 variables that are needed to satisfy needs of ORDER[i].
546 We remember last visited cgraph and varpool node from last iteration
547 of outer loop that allows us to process every new addition.
549 At the same time we compute size of the boundary into COST. Every
550 callgraph or IPA reference edge leaving the partition contributes into
551 COST. Every edge inside partition was earlier computed as one leaving
552 it and thus we need to subtract it from COST. */
553 while (last_visited_node < lto_symtab_encoder_size (partition->encoder))
555 symtab_node *refs_node;
556 int j;
557 struct ipa_ref *ref = NULL;
558 symtab_node *snode = lto_symtab_encoder_deref (partition->encoder,
559 last_visited_node);
561 if (cgraph_node *node = dyn_cast <cgraph_node *> (snode))
563 struct cgraph_edge *edge;
565 refs_node = node;
567 last_visited_node++;
569 gcc_assert (node->definition || node->weakref);
571 /* Compute boundary cost of callgraph edges. */
572 for (edge = node->callees; edge; edge = edge->next_callee)
573 if (edge->callee->definition)
575 int edge_cost = edge->frequency;
576 int index;
578 if (!edge_cost)
579 edge_cost = 1;
580 gcc_assert (edge_cost > 0);
581 index = lto_symtab_encoder_lookup (partition->encoder,
582 edge->callee);
583 if (index != LCC_NOT_FOUND
584 && index < last_visited_node - 1)
585 cost -= edge_cost, internal += edge_cost;
586 else
587 cost += edge_cost;
589 for (edge = node->callers; edge; edge = edge->next_caller)
591 int edge_cost = edge->frequency;
592 int index;
594 gcc_assert (edge->caller->definition);
595 if (!edge_cost)
596 edge_cost = 1;
597 gcc_assert (edge_cost > 0);
598 index = lto_symtab_encoder_lookup (partition->encoder,
599 edge->caller);
600 if (index != LCC_NOT_FOUND
601 && index < last_visited_node - 1)
602 cost -= edge_cost;
603 else
604 cost += edge_cost;
607 else
609 refs_node = snode;
610 last_visited_node++;
613 /* Compute boundary cost of IPA REF edges and at the same time look into
614 variables referenced from current partition and try to add them. */
615 for (j = 0; refs_node->iterate_reference (j, ref); j++)
616 if (is_a <varpool_node *> (ref->referred))
618 int index;
620 vnode = dyn_cast <varpool_node *> (ref->referred);
621 if (!vnode->definition)
622 continue;
623 if (!symbol_partitioned_p (vnode) && flag_toplevel_reorder
624 && !vnode->no_reorder
625 && vnode->get_partitioning_class () == SYMBOL_PARTITION)
626 add_symbol_to_partition (partition, vnode);
627 index = lto_symtab_encoder_lookup (partition->encoder,
628 vnode);
629 if (index != LCC_NOT_FOUND
630 && index < last_visited_node - 1)
631 cost--, internal++;
632 else
633 cost++;
635 else
637 int index;
639 node = dyn_cast <cgraph_node *> (ref->referred);
640 if (!node->definition)
641 continue;
642 index = lto_symtab_encoder_lookup (partition->encoder,
643 node);
644 if (index != LCC_NOT_FOUND
645 && index < last_visited_node - 1)
646 cost--, internal++;
647 else
648 cost++;
650 for (j = 0; refs_node->iterate_referring (j, ref); j++)
651 if (is_a <varpool_node *> (ref->referring))
653 int index;
655 vnode = dyn_cast <varpool_node *> (ref->referring);
656 gcc_assert (vnode->definition);
657 /* It is better to couple variables with their users, because it allows them
658 to be removed. Coupling with objects they refer to only helps to reduce
659 number of symbols promoted to hidden. */
660 if (!symbol_partitioned_p (vnode) && flag_toplevel_reorder
661 && !vnode->no_reorder
662 && !vnode->can_remove_if_no_refs_p ()
663 && vnode->get_partitioning_class () == SYMBOL_PARTITION)
664 add_symbol_to_partition (partition, vnode);
665 index = lto_symtab_encoder_lookup (partition->encoder,
666 vnode);
667 if (index != LCC_NOT_FOUND
668 && index < last_visited_node - 1)
669 cost--;
670 else
671 cost++;
673 else
675 int index;
677 node = dyn_cast <cgraph_node *> (ref->referring);
678 gcc_assert (node->definition);
679 index = lto_symtab_encoder_lookup (partition->encoder,
680 node);
681 if (index != LCC_NOT_FOUND
682 && index < last_visited_node - 1)
683 cost--;
684 else
685 cost++;
689 /* If the partition is large enough, start looking for smallest boundary cost. */
690 if (partition->insns < partition_size * 3 / 4
691 || best_cost == INT_MAX
692 || ((!cost
693 || (best_internal * (HOST_WIDE_INT) cost
694 > (internal * (HOST_WIDE_INT)best_cost)))
695 && partition->insns < partition_size * 5 / 4))
697 best_cost = cost;
698 best_internal = internal;
699 best_i = i;
700 best_n_nodes = lto_symtab_encoder_size (partition->encoder);
701 best_total_size = total_size;
702 best_varpool_pos = varpool_pos;
704 if (symtab->dump_file)
705 fprintf (symtab->dump_file, "Step %i: added %s/%i, size %i, cost %i/%i "
706 "best %i/%i, step %i\n", i,
707 order[i]->name (), order[i]->order,
708 partition->insns, cost, internal,
709 best_cost, best_internal, best_i);
710 /* Partition is too large, unwind into step when best cost was reached and
711 start new partition. */
712 if (partition->insns > 2 * partition_size)
714 if (best_i != i)
716 if (symtab->dump_file)
717 fprintf (symtab->dump_file, "Unwinding %i insertions to step %i\n",
718 i - best_i, best_i);
719 undo_partition (partition, best_n_nodes);
720 varpool_pos = best_varpool_pos;
722 i = best_i;
723 /* When we are finished, avoid creating empty partition. */
724 while (i < n_nodes - 1 && symbol_partitioned_p (order[i + 1]))
725 i++;
726 if (i == n_nodes - 1)
727 break;
728 partition = new_partition ("");
729 last_visited_node = 0;
730 total_size = best_total_size;
731 cost = 0;
733 if (symtab->dump_file)
734 fprintf (symtab->dump_file, "New partition\n");
735 best_n_nodes = 0;
736 best_cost = INT_MAX;
738 /* Since the size of partitions is just approximate, update the size after
739 we finished current one. */
740 if (npartitions < n_lto_partitions)
741 partition_size = total_size / (n_lto_partitions - npartitions);
742 else
743 partition_size = INT_MAX;
745 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
746 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
747 npartitions ++;
751 next_nodes.truncate (0);
753 /* Varables that are not reachable from the code go into last partition. */
754 if (flag_toplevel_reorder)
756 FOR_EACH_VARIABLE (vnode)
757 if (vnode->get_partitioning_class () == SYMBOL_PARTITION
758 && !symbol_partitioned_p (vnode)
759 && !vnode->no_reorder)
760 next_nodes.safe_push (vnode);
763 /* Output remaining ordered symbols. */
764 while (varpool_pos < n_varpool_nodes)
765 next_nodes.safe_push (varpool_order[varpool_pos++]);
766 while (noreorder_pos < (int)noreorder.length ())
767 next_nodes.safe_push (noreorder[noreorder_pos++]);
768 add_sorted_nodes (next_nodes, partition);
770 free (order);
773 /* Mangle NODE symbol name into a local name.
774 This is necessary to do
775 1) if two or more static vars of same assembler name
776 are merged into single ltrans unit.
777 2) if prevoiusly static var was promoted hidden to avoid possible conflict
778 with symbols defined out of the LTO world.
781 static bool
782 privatize_symbol_name (symtab_node *node)
784 tree decl = node->decl;
785 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
787 /* Our renaming machinery do not handle more than one change of assembler name.
788 We should not need more than one anyway. */
789 if (node->lto_file_data
790 && lto_get_decl_name_mapping (node->lto_file_data, name) != name)
792 if (symtab->dump_file)
793 fprintf (symtab->dump_file,
794 "Not privatizing symbol name: %s. It privatized already.\n",
795 name);
796 return false;
798 /* Avoid mangling of already mangled clones.
799 ??? should have a flag whether a symbol has a 'private' name already,
800 since we produce some symbols like that i.e. for global constructors
801 that are not really clones. */
802 if (node->unique_name)
804 if (symtab->dump_file)
805 fprintf (symtab->dump_file,
806 "Not privatizing symbol name: %s. Has unique name.\n",
807 name);
808 return false;
810 symtab->change_decl_assembler_name (decl,
811 clone_function_name (decl, "lto_priv"));
812 if (node->lto_file_data)
813 lto_record_renamed_decl (node->lto_file_data, name,
814 IDENTIFIER_POINTER
815 (DECL_ASSEMBLER_NAME (decl)));
816 if (symtab->dump_file)
817 fprintf (symtab->dump_file,
818 "Privatizing symbol name: %s -> %s\n",
819 name, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
820 return true;
823 /* Promote variable VNODE to be static. */
825 static void
826 promote_symbol (symtab_node *node)
828 /* We already promoted ... */
829 if (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
830 && DECL_VISIBILITY_SPECIFIED (node->decl)
831 && TREE_PUBLIC (node->decl))
832 return;
834 gcc_checking_assert (!TREE_PUBLIC (node->decl)
835 && !DECL_EXTERNAL (node->decl));
836 /* Be sure that newly public symbol does not conflict with anything already
837 defined by the non-LTO part. */
838 privatize_symbol_name (node);
839 TREE_PUBLIC (node->decl) = 1;
840 DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
841 DECL_VISIBILITY_SPECIFIED (node->decl) = true;
842 if (symtab->dump_file)
843 fprintf (symtab->dump_file,
844 "Promoting as hidden: %s\n", node->name ());
847 /* Return true if NODE needs named section even if it won't land in the partition
848 symbol table.
849 FIXME: we should really not use named sections for inline clones and master clones. */
851 static bool
852 may_need_named_section_p (lto_symtab_encoder_t encoder, symtab_node *node)
854 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
855 if (!cnode)
856 return false;
857 if (node->real_symbol_p ())
858 return false;
859 return (!encoder
860 || (lto_symtab_encoder_lookup (encoder, node) != LCC_NOT_FOUND
861 && lto_symtab_encoder_encode_body_p (encoder,
862 cnode)));
865 /* If NODE represents a static variable. See if there are other variables
866 of the same name in partition ENCODER (or in whole compilation unit if
867 ENCODER is NULL) and if so, mangle the statics. Always mangle all
868 conflicting statics, so we reduce changes of silently miscompiling
869 asm statements referring to them by symbol name. */
871 static void
872 rename_statics (lto_symtab_encoder_t encoder, symtab_node *node)
874 tree decl = node->decl;
875 symtab_node *s;
876 tree name = DECL_ASSEMBLER_NAME (decl);
878 /* See if this is static symbol. */
879 if ((node->externally_visible
880 /* FIXME: externally_visible is somewhat illogically not set for
881 external symbols (i.e. those not defined). Remove this test
882 once this is fixed. */
883 || DECL_EXTERNAL (node->decl)
884 || !node->real_symbol_p ())
885 && !may_need_named_section_p (encoder, node))
886 return;
888 /* Now walk symbols sharing the same name and see if there are any conflicts.
889 (all types of symbols counts here, since we can not have static of the
890 same name as external or public symbol.) */
891 for (s = symtab_node::get_for_asmname (name);
892 s; s = s->next_sharing_asm_name)
893 if ((s->real_symbol_p () || may_need_named_section_p (encoder, s))
894 && s->decl != node->decl
895 && (!encoder
896 || lto_symtab_encoder_lookup (encoder, s) != LCC_NOT_FOUND))
897 break;
899 /* OK, no confict, so we have nothing to do. */
900 if (!s)
901 return;
903 if (symtab->dump_file)
904 fprintf (symtab->dump_file,
905 "Renaming statics with asm name: %s\n", node->name ());
907 /* Assign every symbol in the set that shares the same ASM name an unique
908 mangled name. */
909 for (s = symtab_node::get_for_asmname (name); s;)
910 if (!s->externally_visible
911 && ((s->real_symbol_p ()
912 && !DECL_EXTERNAL (node->decl)
913 && !TREE_PUBLIC (node->decl))
914 || may_need_named_section_p (encoder, s))
915 && (!encoder
916 || lto_symtab_encoder_lookup (encoder, s) != LCC_NOT_FOUND))
918 if (privatize_symbol_name (s))
919 /* Re-start from beginning since we do not know how many symbols changed a name. */
920 s = symtab_node::get_for_asmname (name);
921 else s = s->next_sharing_asm_name;
923 else s = s->next_sharing_asm_name;
926 /* Find out all static decls that need to be promoted to global because
927 of cross file sharing. This function must be run in the WPA mode after
928 all inlinees are added. */
930 void
931 lto_promote_cross_file_statics (void)
933 unsigned i, n_sets;
935 gcc_assert (flag_wpa);
937 select_what_to_dump (false);
939 /* First compute boundaries. */
940 n_sets = ltrans_partitions.length ();
941 for (i = 0; i < n_sets; i++)
943 ltrans_partition part
944 = ltrans_partitions[i];
945 part->encoder = compute_ltrans_boundary (part->encoder);
948 /* Look at boundaries and promote symbols as needed. */
949 for (i = 0; i < n_sets; i++)
951 lto_symtab_encoder_iterator lsei;
952 lto_symtab_encoder_t encoder = ltrans_partitions[i]->encoder;
954 for (lsei = lsei_start (encoder); !lsei_end_p (lsei);
955 lsei_next (&lsei))
957 symtab_node *node = lsei_node (lsei);
959 /* If symbol is static, rename it if its assembler name clash with
960 anything else in this unit. */
961 rename_statics (encoder, node);
963 /* No need to promote if symbol already is externally visible ... */
964 if (node->externally_visible
965 /* ... or if it is part of current partition ... */
966 || lto_symtab_encoder_in_partition_p (encoder, node)
967 /* ... or if we do not partition it. This mean that it will
968 appear in every partition refernecing it. */
969 || node->get_partitioning_class () != SYMBOL_PARTITION)
970 continue;
972 promote_symbol (node);
977 /* Rename statics in the whole unit in the case that
978 we do -flto-partition=none. */
980 void
981 lto_promote_statics_nonwpa (void)
983 symtab_node *node;
984 FOR_EACH_SYMBOL (node)
985 rename_statics (NULL, node);