* cgraph.h: Flatten. Remove all include files.
[official-gcc.git] / gcc / lto / lto-partition.c
blobc38931b3f9541e556627a66c19dc00d0699d4f8d
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 lto_set_symtab_encoder_in_partition (part->encoder, node);
152 if (symbol_partitioned_p (node))
154 node->in_other_partition = 1;
155 if (symtab->dump_file)
156 fprintf (symtab->dump_file,
157 "Symbol node %s now used in multiple partitions\n",
158 node->name ());
160 node->aux = (void *)((size_t)node->aux + 1);
162 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
164 struct cgraph_edge *e;
165 if (!node->alias)
166 part->insns += inline_summary (cnode)->self_size;
168 /* Add all inline clones and callees that are duplicated. */
169 for (e = cnode->callees; e; e = e->next_callee)
170 if (!e->inline_failed)
171 add_symbol_to_partition_1 (part, e->callee);
172 else if (e->callee->get_partitioning_class () == SYMBOL_DUPLICATE)
173 add_symbol_to_partition (part, e->callee);
175 /* Add all thunks associated with the function. */
176 for (e = cnode->callers; e; e = e->next_caller)
177 if (e->caller->thunk.thunk_p)
178 add_symbol_to_partition_1 (part, e->caller);
181 add_references_to_partition (part, node);
183 /* Add all aliases associated with the symbol. */
185 FOR_EACH_ALIAS (node, ref)
186 if (!node->weakref)
187 add_symbol_to_partition_1 (part, ref->referring);
189 /* Ensure that SAME_COMDAT_GROUP lists all allways added in a group. */
190 if (node->same_comdat_group)
191 for (node1 = node->same_comdat_group;
192 node1 != node; node1 = node1->same_comdat_group)
193 if (!node->alias)
195 bool added = add_symbol_to_partition_1 (part, node1);
196 gcc_assert (added);
198 return true;
201 /* If symbol NODE is really part of other symbol's definition (i.e. it is
202 internal label, thunk, alias or so), return the outer symbol.
203 When add_symbol_to_partition_1 is called on the outer symbol it must
204 eventually add NODE, too. */
205 static symtab_node *
206 contained_in_symbol (symtab_node *node)
208 /* Weakrefs are never contained in anything. */
209 if (node->weakref)
210 return node;
211 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
213 cnode = cnode->function_symbol ();
214 if (cnode->global.inlined_to)
215 cnode = cnode->global.inlined_to;
216 return cnode;
218 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
219 return vnode->ultimate_alias_target ();
220 return node;
223 /* Add symbol NODE to partition. When definition of NODE is part
224 of other symbol definition, add the other symbol, too. */
226 static void
227 add_symbol_to_partition (ltrans_partition part, symtab_node *node)
229 symtab_node *node1;
231 /* Verify that we do not try to duplicate something that can not be. */
232 gcc_checking_assert (node->get_partitioning_class () == SYMBOL_DUPLICATE
233 || !symbol_partitioned_p (node));
235 while ((node1 = contained_in_symbol (node)) != node)
236 node = node1;
238 /* If we have duplicated symbol contained in something we can not duplicate,
239 we are very badly screwed. The other way is possible, so we do not
240 assert this in add_symbol_to_partition_1.
242 Be lax about comdats; they may or may not be duplicated and we may
243 end up in need to duplicate keyed comdat because it has unkeyed alias. */
245 gcc_assert (node->get_partitioning_class () == SYMBOL_DUPLICATE
246 || DECL_COMDAT (node->decl)
247 || !symbol_partitioned_p (node));
249 add_symbol_to_partition_1 (part, node);
252 /* Undo all additions until number of cgraph nodes in PARITION is N_CGRAPH_NODES
253 and number of varpool nodes is N_VARPOOL_NODES. */
255 static void
256 undo_partition (ltrans_partition partition, unsigned int n_nodes)
258 while (lto_symtab_encoder_size (partition->encoder) > (int)n_nodes)
260 symtab_node *node = lto_symtab_encoder_deref (partition->encoder,
261 n_nodes);
262 cgraph_node *cnode;
264 /* After UNDO we no longer know what was visited. */
265 if (partition->initializers_visited)
266 delete partition->initializers_visited;
267 partition->initializers_visited = NULL;
269 if (!node->alias && (cnode = dyn_cast <cgraph_node *> (node)))
270 partition->insns -= inline_summary (cnode)->self_size;
271 lto_symtab_encoder_delete_node (partition->encoder, node);
272 node->aux = (void *)((size_t)node->aux - 1);
276 /* Group cgrah nodes by input files. This is used mainly for testing
277 right now. */
279 void
280 lto_1_to_1_map (void)
282 symtab_node *node;
283 struct lto_file_decl_data *file_data;
284 hash_map<lto_file_decl_data *, ltrans_partition> pmap;
285 ltrans_partition partition;
286 int npartitions = 0;
288 FOR_EACH_SYMBOL (node)
290 if (node->get_partitioning_class () != SYMBOL_PARTITION
291 || symbol_partitioned_p (node))
292 continue;
294 file_data = node->lto_file_data;
296 if (file_data)
298 ltrans_partition *slot = &pmap.get_or_insert (file_data);
299 if (*slot)
300 partition = *slot;
301 else
303 partition = new_partition (file_data->file_name);
304 *slot = partition;
305 npartitions++;
308 else if (!file_data && ltrans_partitions.length ())
309 partition = ltrans_partitions[0];
310 else
312 partition = new_partition ("");
313 pmap.put (NULL, partition);
314 npartitions++;
317 add_symbol_to_partition (partition, node);
320 /* If the cgraph is empty, create one cgraph node set so that there is still
321 an output file for any variables that need to be exported in a DSO. */
322 if (!npartitions)
323 new_partition ("empty");
327 /* Maximal partitioning. Put every new symbol into new partition if possible. */
329 void
330 lto_max_map (void)
332 symtab_node *node;
333 ltrans_partition partition;
334 int npartitions = 0;
336 FOR_EACH_SYMBOL (node)
338 if (node->get_partitioning_class () != SYMBOL_PARTITION
339 || symbol_partitioned_p (node))
340 continue;
341 partition = new_partition (node->asm_name ());
342 add_symbol_to_partition (partition, node);
343 npartitions++;
345 if (!npartitions)
346 new_partition ("empty");
349 /* Helper function for qsort; sort nodes by order. noreorder functions must have
350 been removed earlier. */
351 static int
352 node_cmp (const void *pa, const void *pb)
354 const struct cgraph_node *a = *(const struct cgraph_node * const *) pa;
355 const struct cgraph_node *b = *(const struct cgraph_node * const *) pb;
357 /* Profile reorder flag enables function reordering based on first execution
358 of a function. All functions with profile are placed in ascending
359 order at the beginning. */
361 if (flag_profile_reorder_functions)
363 /* Functions with time profile are sorted in ascending order. */
364 if (a->tp_first_run && b->tp_first_run)
365 return a->tp_first_run != b->tp_first_run
366 ? a->tp_first_run - b->tp_first_run
367 : a->order - b->order;
369 /* Functions with time profile are sorted before the functions
370 that do not have the profile. */
371 if (a->tp_first_run || b->tp_first_run)
372 return b->tp_first_run - a->tp_first_run;
375 return b->order - a->order;
378 /* Helper function for qsort; sort nodes by order. */
379 static int
380 varpool_node_cmp (const void *pa, const void *pb)
382 const symtab_node *a = *static_cast<const symtab_node * const *> (pa);
383 const symtab_node *b = *static_cast<const symtab_node * const *> (pb);
384 return b->order - a->order;
387 /* Add all symtab nodes from NEXT_NODE to PARTITION in order. */
389 static void
390 add_sorted_nodes (vec<symtab_node *> &next_nodes, ltrans_partition partition)
392 unsigned i;
393 symtab_node *node;
395 next_nodes.qsort (varpool_node_cmp);
396 FOR_EACH_VEC_ELT (next_nodes, i, node)
397 if (!symbol_partitioned_p (node))
398 add_symbol_to_partition (partition, node);
402 /* Group cgraph nodes into equally-sized partitions.
404 The partitioning algorithm is simple: nodes are taken in predefined order.
405 The order corresponds to the order we want functions to have in the final
406 output. In the future this will be given by function reordering pass, but
407 at the moment we use the topological order, which is a good approximation.
409 The goal is to partition this linear order into intervals (partitions) so
410 that all the partitions have approximately the same size and the number of
411 callgraph or IPA reference edges crossing boundaries is minimal.
413 This is a lot faster (O(n) in size of callgraph) than algorithms doing
414 priority-based graph clustering that are generally O(n^2) and, since
415 WHOPR is designed to make things go well across partitions, it leads
416 to good results.
418 We compute the expected size of a partition as:
420 max (total_size / lto_partitions, min_partition_size)
422 We use dynamic expected size of partition so small programs are partitioned
423 into enough partitions to allow use of multiple CPUs, while large programs
424 are not partitioned too much. Creating too many partitions significantly
425 increases the streaming overhead.
427 In the future, we would like to bound the maximal size of partitions so as
428 to prevent the LTRANS stage from consuming too much memory. At the moment,
429 however, the WPA stage is the most memory intensive for large benchmarks,
430 since too many types and declarations are read into memory.
432 The function implements a simple greedy algorithm. Nodes are being added
433 to the current partition until after 3/4 of the expected partition size is
434 reached. Past this threshold, we keep track of boundary size (number of
435 edges going to other partitions) and continue adding functions until after
436 the current partition has grown to twice the expected partition size. Then
437 the process is undone to the point where the minimal ratio of boundary size
438 and in-partition calls was reached. */
440 void
441 lto_balanced_map (int n_lto_partitions)
443 int n_nodes = 0;
444 int n_varpool_nodes = 0, varpool_pos = 0, best_varpool_pos = 0;
445 struct cgraph_node **order = XNEWVEC (cgraph_node *, symtab->cgraph_max_uid);
446 auto_vec<cgraph_node *> noreorder;
447 auto_vec<varpool_node *> varpool_order;
448 int i;
449 struct cgraph_node *node;
450 int total_size = 0, best_total_size = 0;
451 int partition_size;
452 ltrans_partition partition;
453 int last_visited_node = 0;
454 varpool_node *vnode;
455 int cost = 0, internal = 0;
456 int best_n_nodes = 0, best_i = 0, best_cost =
457 INT_MAX, best_internal = 0;
458 int npartitions;
459 int current_order = -1;
460 int noreorder_pos = 0;
462 FOR_EACH_VARIABLE (vnode)
463 gcc_assert (!vnode->aux);
465 FOR_EACH_DEFINED_FUNCTION (node)
466 if (node->get_partitioning_class () == SYMBOL_PARTITION)
468 if (node->no_reorder)
469 noreorder.safe_push (node);
470 else
471 order[n_nodes++] = node;
472 if (!node->alias)
473 total_size += inline_summary (node)->size;
476 /* Streaming works best when the source units do not cross partition
477 boundaries much. This is because importing function from a source
478 unit tends to import a lot of global trees defined there. We should
479 get better about minimizing the function bounday, but until that
480 things works smoother if we order in source order. */
481 qsort (order, n_nodes, sizeof (struct cgraph_node *), node_cmp);
482 noreorder.qsort (node_cmp);
484 if (symtab->dump_file)
486 for(i = 0; i < n_nodes; i++)
487 fprintf (symtab->dump_file, "Balanced map symbol order:%s:%u\n",
488 order[i]->name (), order[i]->tp_first_run);
489 for(i = 0; i < (int)noreorder.length(); i++)
490 fprintf (symtab->dump_file, "Balanced map symbol no_reorder:%s:%u\n",
491 noreorder[i]->name (), noreorder[i]->tp_first_run);
494 /* Collect all variables that should not be reordered. */
495 FOR_EACH_VARIABLE (vnode)
496 if (vnode->get_partitioning_class () == SYMBOL_PARTITION
497 && (!flag_toplevel_reorder || vnode->no_reorder))
498 varpool_order.safe_push (vnode);
499 n_varpool_nodes = varpool_order.length ();
500 varpool_order.qsort (varpool_node_cmp);
502 /* Compute partition size and create the first partition. */
503 partition_size = total_size / n_lto_partitions;
504 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
505 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
506 npartitions = 1;
507 partition = new_partition ("");
508 if (symtab->dump_file)
509 fprintf (symtab->dump_file, "Total unit size: %i, partition size: %i\n",
510 total_size, partition_size);
512 auto_vec<symtab_node *> next_nodes;
514 for (i = 0; i < n_nodes; i++)
516 if (symbol_partitioned_p (order[i]))
517 continue;
519 current_order = order[i]->order;
521 /* Output noreorder and varpool in program order first. */
522 next_nodes.truncate (0);
523 while (varpool_pos < n_varpool_nodes
524 && varpool_order[varpool_pos]->order < current_order)
525 next_nodes.safe_push (varpool_order[varpool_pos++]);
526 while (noreorder_pos < (int)noreorder.length ()
527 && noreorder[noreorder_pos]->order < current_order)
529 if (!noreorder[noreorder_pos]->alias)
530 total_size -= inline_summary (noreorder[noreorder_pos])->size;
531 next_nodes.safe_push (noreorder[noreorder_pos++]);
533 add_sorted_nodes (next_nodes, partition);
535 add_symbol_to_partition (partition, order[i]);
536 if (!order[i]->alias)
537 total_size -= inline_summary (order[i])->size;
540 /* Once we added a new node to the partition, we also want to add
541 all referenced variables unless they was already added into some
542 earlier partition.
543 add_symbol_to_partition adds possibly multiple nodes and
544 variables that are needed to satisfy needs of ORDER[i].
545 We remember last visited cgraph and varpool node from last iteration
546 of outer loop that allows us to process every new addition.
548 At the same time we compute size of the boundary into COST. Every
549 callgraph or IPA reference edge leaving the partition contributes into
550 COST. Every edge inside partition was earlier computed as one leaving
551 it and thus we need to subtract it from COST. */
552 while (last_visited_node < lto_symtab_encoder_size (partition->encoder))
554 symtab_node *refs_node;
555 int j;
556 struct ipa_ref *ref = NULL;
557 symtab_node *snode = lto_symtab_encoder_deref (partition->encoder,
558 last_visited_node);
560 if (cgraph_node *node = dyn_cast <cgraph_node *> (snode))
562 struct cgraph_edge *edge;
564 refs_node = node;
566 last_visited_node++;
568 gcc_assert (node->definition || node->weakref);
570 /* Compute boundary cost of callgraph edges. */
571 for (edge = node->callees; edge; edge = edge->next_callee)
572 if (edge->callee->definition)
574 int edge_cost = edge->frequency;
575 int index;
577 if (!edge_cost)
578 edge_cost = 1;
579 gcc_assert (edge_cost > 0);
580 index = lto_symtab_encoder_lookup (partition->encoder,
581 edge->callee);
582 if (index != LCC_NOT_FOUND
583 && index < last_visited_node - 1)
584 cost -= edge_cost, internal += edge_cost;
585 else
586 cost += edge_cost;
588 for (edge = node->callers; edge; edge = edge->next_caller)
590 int edge_cost = edge->frequency;
591 int index;
593 gcc_assert (edge->caller->definition);
594 if (!edge_cost)
595 edge_cost = 1;
596 gcc_assert (edge_cost > 0);
597 index = lto_symtab_encoder_lookup (partition->encoder,
598 edge->caller);
599 if (index != LCC_NOT_FOUND
600 && index < last_visited_node - 1)
601 cost -= edge_cost;
602 else
603 cost += edge_cost;
606 else
608 refs_node = snode;
609 last_visited_node++;
612 /* Compute boundary cost of IPA REF edges and at the same time look into
613 variables referenced from current partition and try to add them. */
614 for (j = 0; refs_node->iterate_reference (j, ref); j++)
615 if (is_a <varpool_node *> (ref->referred))
617 int index;
619 vnode = dyn_cast <varpool_node *> (ref->referred);
620 if (!vnode->definition)
621 continue;
622 if (!symbol_partitioned_p (vnode) && flag_toplevel_reorder
623 && !vnode->no_reorder
624 && vnode->get_partitioning_class () == SYMBOL_PARTITION)
625 add_symbol_to_partition (partition, vnode);
626 index = lto_symtab_encoder_lookup (partition->encoder,
627 vnode);
628 if (index != LCC_NOT_FOUND
629 && index < last_visited_node - 1)
630 cost--, internal++;
631 else
632 cost++;
634 else
636 int index;
638 node = dyn_cast <cgraph_node *> (ref->referred);
639 if (!node->definition)
640 continue;
641 index = lto_symtab_encoder_lookup (partition->encoder,
642 node);
643 if (index != LCC_NOT_FOUND
644 && index < last_visited_node - 1)
645 cost--, internal++;
646 else
647 cost++;
649 for (j = 0; refs_node->iterate_referring (j, ref); j++)
650 if (is_a <varpool_node *> (ref->referring))
652 int index;
654 vnode = dyn_cast <varpool_node *> (ref->referring);
655 gcc_assert (vnode->definition);
656 /* It is better to couple variables with their users, because it allows them
657 to be removed. Coupling with objects they refer to only helps to reduce
658 number of symbols promoted to hidden. */
659 if (!symbol_partitioned_p (vnode) && flag_toplevel_reorder
660 && !vnode->no_reorder
661 && !vnode->can_remove_if_no_refs_p ()
662 && vnode->get_partitioning_class () == SYMBOL_PARTITION)
663 add_symbol_to_partition (partition, vnode);
664 index = lto_symtab_encoder_lookup (partition->encoder,
665 vnode);
666 if (index != LCC_NOT_FOUND
667 && index < last_visited_node - 1)
668 cost--;
669 else
670 cost++;
672 else
674 int index;
676 node = dyn_cast <cgraph_node *> (ref->referring);
677 gcc_assert (node->definition);
678 index = lto_symtab_encoder_lookup (partition->encoder,
679 node);
680 if (index != LCC_NOT_FOUND
681 && index < last_visited_node - 1)
682 cost--;
683 else
684 cost++;
688 /* If the partition is large enough, start looking for smallest boundary cost. */
689 if (partition->insns < partition_size * 3 / 4
690 || best_cost == INT_MAX
691 || ((!cost
692 || (best_internal * (HOST_WIDE_INT) cost
693 > (internal * (HOST_WIDE_INT)best_cost)))
694 && partition->insns < partition_size * 5 / 4))
696 best_cost = cost;
697 best_internal = internal;
698 best_i = i;
699 best_n_nodes = lto_symtab_encoder_size (partition->encoder);
700 best_total_size = total_size;
701 best_varpool_pos = varpool_pos;
703 if (symtab->dump_file)
704 fprintf (symtab->dump_file, "Step %i: added %s/%i, size %i, cost %i/%i "
705 "best %i/%i, step %i\n", i,
706 order[i]->name (), order[i]->order,
707 partition->insns, cost, internal,
708 best_cost, best_internal, best_i);
709 /* Partition is too large, unwind into step when best cost was reached and
710 start new partition. */
711 if (partition->insns > 2 * partition_size)
713 if (best_i != i)
715 if (symtab->dump_file)
716 fprintf (symtab->dump_file, "Unwinding %i insertions to step %i\n",
717 i - best_i, best_i);
718 undo_partition (partition, best_n_nodes);
719 varpool_pos = best_varpool_pos;
721 i = best_i;
722 /* When we are finished, avoid creating empty partition. */
723 while (i < n_nodes - 1 && symbol_partitioned_p (order[i + 1]))
724 i++;
725 if (i == n_nodes - 1)
726 break;
727 partition = new_partition ("");
728 last_visited_node = 0;
729 total_size = best_total_size;
730 cost = 0;
732 if (symtab->dump_file)
733 fprintf (symtab->dump_file, "New partition\n");
734 best_n_nodes = 0;
735 best_cost = INT_MAX;
737 /* Since the size of partitions is just approximate, update the size after
738 we finished current one. */
739 if (npartitions < n_lto_partitions)
740 partition_size = total_size / (n_lto_partitions - npartitions);
741 else
742 partition_size = INT_MAX;
744 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
745 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
746 npartitions ++;
750 next_nodes.truncate (0);
752 /* Varables that are not reachable from the code go into last partition. */
753 if (flag_toplevel_reorder)
755 FOR_EACH_VARIABLE (vnode)
756 if (vnode->get_partitioning_class () == SYMBOL_PARTITION
757 && !symbol_partitioned_p (vnode)
758 && !vnode->no_reorder)
759 next_nodes.safe_push (vnode);
762 /* Output remaining ordered symbols. */
763 while (varpool_pos < n_varpool_nodes)
764 next_nodes.safe_push (varpool_order[varpool_pos++]);
765 while (noreorder_pos < (int)noreorder.length ())
766 next_nodes.safe_push (noreorder[noreorder_pos++]);
767 add_sorted_nodes (next_nodes, partition);
769 free (order);
772 /* Mangle NODE symbol name into a local name.
773 This is necessary to do
774 1) if two or more static vars of same assembler name
775 are merged into single ltrans unit.
776 2) if prevoiusly static var was promoted hidden to avoid possible conflict
777 with symbols defined out of the LTO world.
780 static bool
781 privatize_symbol_name (symtab_node *node)
783 tree decl = node->decl;
784 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
786 /* Our renaming machinery do not handle more than one change of assembler name.
787 We should not need more than one anyway. */
788 if (node->lto_file_data
789 && lto_get_decl_name_mapping (node->lto_file_data, name) != name)
791 if (symtab->dump_file)
792 fprintf (symtab->dump_file,
793 "Not privatizing symbol name: %s. It privatized already.\n",
794 name);
795 return false;
797 /* Avoid mangling of already mangled clones.
798 ??? should have a flag whether a symbol has a 'private' name already,
799 since we produce some symbols like that i.e. for global constructors
800 that are not really clones. */
801 if (node->unique_name)
803 if (symtab->dump_file)
804 fprintf (symtab->dump_file,
805 "Not privatizing symbol name: %s. Has unique name.\n",
806 name);
807 return false;
809 symtab->change_decl_assembler_name (decl,
810 clone_function_name (decl, "lto_priv"));
811 if (node->lto_file_data)
812 lto_record_renamed_decl (node->lto_file_data, name,
813 IDENTIFIER_POINTER
814 (DECL_ASSEMBLER_NAME (decl)));
815 if (symtab->dump_file)
816 fprintf (symtab->dump_file,
817 "Privatizing symbol name: %s -> %s\n",
818 name, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
819 return true;
822 /* Promote variable VNODE to be static. */
824 static void
825 promote_symbol (symtab_node *node)
827 /* We already promoted ... */
828 if (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
829 && DECL_VISIBILITY_SPECIFIED (node->decl)
830 && TREE_PUBLIC (node->decl))
831 return;
833 gcc_checking_assert (!TREE_PUBLIC (node->decl)
834 && !DECL_EXTERNAL (node->decl));
835 /* Be sure that newly public symbol does not conflict with anything already
836 defined by the non-LTO part. */
837 privatize_symbol_name (node);
838 TREE_PUBLIC (node->decl) = 1;
839 DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
840 DECL_VISIBILITY_SPECIFIED (node->decl) = true;
841 if (symtab->dump_file)
842 fprintf (symtab->dump_file,
843 "Promoting as hidden: %s\n", node->name ());
846 /* Return true if NODE needs named section even if it won't land in the partition
847 symbol table.
848 FIXME: we should really not use named sections for inline clones and master clones. */
850 static bool
851 may_need_named_section_p (lto_symtab_encoder_t encoder, symtab_node *node)
853 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
854 if (!cnode)
855 return false;
856 if (node->real_symbol_p ())
857 return false;
858 return (!encoder
859 || (lto_symtab_encoder_lookup (encoder, node) != LCC_NOT_FOUND
860 && lto_symtab_encoder_encode_body_p (encoder,
861 cnode)));
864 /* If NODE represents a static variable. See if there are other variables
865 of the same name in partition ENCODER (or in whole compilation unit if
866 ENCODER is NULL) and if so, mangle the statics. Always mangle all
867 conflicting statics, so we reduce changes of silently miscompiling
868 asm statements referring to them by symbol name. */
870 static void
871 rename_statics (lto_symtab_encoder_t encoder, symtab_node *node)
873 tree decl = node->decl;
874 symtab_node *s;
875 tree name = DECL_ASSEMBLER_NAME (decl);
877 /* See if this is static symbol. */
878 if ((node->externally_visible
879 /* FIXME: externally_visible is somewhat illogically not set for
880 external symbols (i.e. those not defined). Remove this test
881 once this is fixed. */
882 || DECL_EXTERNAL (node->decl)
883 || !node->real_symbol_p ())
884 && !may_need_named_section_p (encoder, node))
885 return;
887 /* Now walk symbols sharing the same name and see if there are any conflicts.
888 (all types of symbols counts here, since we can not have static of the
889 same name as external or public symbol.) */
890 for (s = symtab_node::get_for_asmname (name);
891 s; s = s->next_sharing_asm_name)
892 if ((s->real_symbol_p () || may_need_named_section_p (encoder, s))
893 && s->decl != node->decl
894 && (!encoder
895 || lto_symtab_encoder_lookup (encoder, s) != LCC_NOT_FOUND))
896 break;
898 /* OK, no confict, so we have nothing to do. */
899 if (!s)
900 return;
902 if (symtab->dump_file)
903 fprintf (symtab->dump_file,
904 "Renaming statics with asm name: %s\n", node->name ());
906 /* Assign every symbol in the set that shares the same ASM name an unique
907 mangled name. */
908 for (s = symtab_node::get_for_asmname (name); s;)
909 if (!s->externally_visible
910 && ((s->real_symbol_p ()
911 && !DECL_EXTERNAL (node->decl)
912 && !TREE_PUBLIC (node->decl))
913 || may_need_named_section_p (encoder, s))
914 && (!encoder
915 || lto_symtab_encoder_lookup (encoder, s) != LCC_NOT_FOUND))
917 if (privatize_symbol_name (s))
918 /* Re-start from beginning since we do not know how many symbols changed a name. */
919 s = symtab_node::get_for_asmname (name);
920 else s = s->next_sharing_asm_name;
922 else s = s->next_sharing_asm_name;
925 /* Find out all static decls that need to be promoted to global because
926 of cross file sharing. This function must be run in the WPA mode after
927 all inlinees are added. */
929 void
930 lto_promote_cross_file_statics (void)
932 unsigned i, n_sets;
934 gcc_assert (flag_wpa);
936 /* First compute boundaries. */
937 n_sets = ltrans_partitions.length ();
938 for (i = 0; i < n_sets; i++)
940 ltrans_partition part
941 = ltrans_partitions[i];
942 part->encoder = compute_ltrans_boundary (part->encoder);
945 /* Look at boundaries and promote symbols as needed. */
946 for (i = 0; i < n_sets; i++)
948 lto_symtab_encoder_iterator lsei;
949 lto_symtab_encoder_t encoder = ltrans_partitions[i]->encoder;
951 for (lsei = lsei_start (encoder); !lsei_end_p (lsei);
952 lsei_next (&lsei))
954 symtab_node *node = lsei_node (lsei);
956 /* If symbol is static, rename it if its assembler name clash with
957 anything else in this unit. */
958 rename_statics (encoder, node);
960 /* No need to promote if symbol already is externally visible ... */
961 if (node->externally_visible
962 /* ... or if it is part of current partition ... */
963 || lto_symtab_encoder_in_partition_p (encoder, node)
964 /* ... or if we do not partition it. This mean that it will
965 appear in every partition refernecing it. */
966 || node->get_partitioning_class () != SYMBOL_PARTITION)
967 continue;
969 promote_symbol (node);
974 /* Rename statics in the whole unit in the case that
975 we do -flto-partition=none. */
977 void
978 lto_promote_statics_nonwpa (void)
980 symtab_node *node;
981 FOR_EACH_SYMBOL (node)
982 rename_statics (NULL, node);