2014-07-30 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / lto / lto-partition.c
blobcb08a88ad0e2f306917e0eaf156f978e60dc0f89
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 "gcc-symtab.h"
26 #include "basic-block.h"
27 #include "tree-ssa-alias.h"
28 #include "internal-fn.h"
29 #include "gimple-expr.h"
30 #include "is-a.h"
31 #include "gimple.h"
32 #include "tm.h"
33 #include "cgraph.h"
34 #include "lto-streamer.h"
35 #include "timevar.h"
36 #include "params.h"
37 #include "ipa-inline.h"
38 #include "ipa-utils.h"
39 #include "lto-partition.h"
41 vec<ltrans_partition> ltrans_partitions;
43 static void add_symbol_to_partition (ltrans_partition part, symtab_node *node);
46 /* Create new partition with name NAME. */
48 static ltrans_partition
49 new_partition (const char *name)
51 ltrans_partition part = XCNEW (struct ltrans_partition_def);
52 part->encoder = lto_symtab_encoder_new (false);
53 part->name = name;
54 part->insns = 0;
55 ltrans_partitions.safe_push (part);
56 return part;
59 /* Free memory used by ltrans datastructures. */
61 void
62 free_ltrans_partitions (void)
64 unsigned int idx;
65 ltrans_partition part;
66 for (idx = 0; ltrans_partitions.iterate (idx, &part); idx++)
68 if (part->initializers_visited)
69 pointer_set_destroy (part->initializers_visited);
70 /* Symtab encoder is freed after streaming. */
71 free (part);
73 ltrans_partitions.release ();
76 /* Return true if symbol is already in some partition. */
78 static inline bool
79 symbol_partitioned_p (symtab_node *node)
81 return node->aux;
84 /* Add references into the partition. */
85 static void
86 add_references_to_partition (ltrans_partition part, symtab_node *node)
88 int i;
89 struct ipa_ref *ref = NULL;
91 /* Add all duplicated references to the partition. */
92 for (i = 0; node->iterate_reference (i, ref); i++)
93 if (ref->referred->get_partitioning_class () == SYMBOL_DUPLICATE)
94 add_symbol_to_partition (part, ref->referred);
95 /* References to a readonly variable may be constant foled into its value.
96 Recursively look into the initializers of the constant variable and add
97 references, too. */
98 else if (is_a <varpool_node *> (ref->referred)
99 && dyn_cast <varpool_node *> (ref->referred)
100 ->ctor_useable_for_folding_p ()
101 && !lto_symtab_encoder_in_partition_p (part->encoder, ref->referred))
103 if (!part->initializers_visited)
104 part->initializers_visited = pointer_set_create ();
105 if (!pointer_set_insert (part->initializers_visited, ref->referred))
106 add_references_to_partition (part, ref->referred);
110 /* Helper function for add_symbol_to_partition doing the actual dirty work
111 of adding NODE to PART. */
113 static bool
114 add_symbol_to_partition_1 (ltrans_partition part, symtab_node *node)
116 enum symbol_partitioning_class c = node->get_partitioning_class ();
117 struct ipa_ref *ref;
118 symtab_node *node1;
120 /* If NODE is already there, we have nothing to do. */
121 if (lto_symtab_encoder_in_partition_p (part->encoder, node))
122 return true;
124 /* non-duplicated aliases or tunks of a duplicated symbol needs to be output
125 just once.
127 Be lax about comdats; they may or may not be duplicated and we may
128 end up in need to duplicate keyed comdat because it has unkeyed alias. */
129 if (c == SYMBOL_PARTITION && !DECL_COMDAT (node->decl)
130 && symbol_partitioned_p (node))
131 return false;
133 /* Be sure that we never try to duplicate partitioned symbol
134 or add external symbol. */
135 gcc_assert (c != SYMBOL_EXTERNAL
136 && (c == SYMBOL_DUPLICATE || !symbol_partitioned_p (node)));
138 lto_set_symtab_encoder_in_partition (part->encoder, node);
140 if (symbol_partitioned_p (node))
142 node->in_other_partition = 1;
143 if (cgraph_dump_file)
144 fprintf (cgraph_dump_file, "Symbol node %s now used in multiple partitions\n",
145 node->name ());
147 node->aux = (void *)((size_t)node->aux + 1);
149 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
151 struct cgraph_edge *e;
152 if (!node->alias)
153 part->insns += inline_summary (cnode)->self_size;
155 /* Add all inline clones and callees that are duplicated. */
156 for (e = cnode->callees; e; e = e->next_callee)
157 if (!e->inline_failed)
158 add_symbol_to_partition_1 (part, e->callee);
159 else if (e->callee->get_partitioning_class () == SYMBOL_DUPLICATE)
160 add_symbol_to_partition (part, e->callee);
162 /* Add all thunks associated with the function. */
163 for (e = cnode->callers; e; e = e->next_caller)
164 if (e->caller->thunk.thunk_p)
165 add_symbol_to_partition_1 (part, e->caller);
168 add_references_to_partition (part, node);
170 /* Add all aliases associated with the symbol. */
172 FOR_EACH_ALIAS (node, ref)
173 if (!node->weakref)
174 add_symbol_to_partition_1 (part, ref->referring);
176 /* Ensure that SAME_COMDAT_GROUP lists all allways added in a group. */
177 if (node->same_comdat_group)
178 for (node1 = node->same_comdat_group;
179 node1 != node; node1 = node1->same_comdat_group)
180 if (!node->alias)
182 bool added = add_symbol_to_partition_1 (part, node1);
183 gcc_assert (added);
185 return true;
188 /* If symbol NODE is really part of other symbol's definition (i.e. it is
189 internal label, thunk, alias or so), return the outer symbol.
190 When add_symbol_to_partition_1 is called on the outer symbol it must
191 eventually add NODE, too. */
192 static symtab_node *
193 contained_in_symbol (symtab_node *node)
195 /* Weakrefs are never contained in anything. */
196 if (node->weakref)
197 return node;
198 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
200 cnode = cnode->function_symbol ();
201 if (cnode->global.inlined_to)
202 cnode = cnode->global.inlined_to;
203 return cnode;
205 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
206 return vnode->ultimate_alias_target ();
207 return node;
210 /* Add symbol NODE to partition. When definition of NODE is part
211 of other symbol definition, add the other symbol, too. */
213 static void
214 add_symbol_to_partition (ltrans_partition part, symtab_node *node)
216 symtab_node *node1;
218 /* Verify that we do not try to duplicate something that can not be. */
219 gcc_checking_assert (node->get_partitioning_class () == SYMBOL_DUPLICATE
220 || !symbol_partitioned_p (node));
222 while ((node1 = contained_in_symbol (node)) != node)
223 node = node1;
225 /* If we have duplicated symbol contained in something we can not duplicate,
226 we are very badly screwed. The other way is possible, so we do not
227 assert this in add_symbol_to_partition_1.
229 Be lax about comdats; they may or may not be duplicated and we may
230 end up in need to duplicate keyed comdat because it has unkeyed alias. */
232 gcc_assert (node->get_partitioning_class () == SYMBOL_DUPLICATE
233 || DECL_COMDAT (node->decl)
234 || !symbol_partitioned_p (node));
236 add_symbol_to_partition_1 (part, node);
239 /* Undo all additions until number of cgraph nodes in PARITION is N_CGRAPH_NODES
240 and number of varpool nodes is N_VARPOOL_NODES. */
242 static void
243 undo_partition (ltrans_partition partition, unsigned int n_nodes)
245 while (lto_symtab_encoder_size (partition->encoder) > (int)n_nodes)
247 symtab_node *node = lto_symtab_encoder_deref (partition->encoder,
248 n_nodes);
249 cgraph_node *cnode;
251 /* After UNDO we no longer know what was visited. */
252 if (partition->initializers_visited)
253 pointer_set_destroy (partition->initializers_visited);
254 partition->initializers_visited = NULL;
256 if (!node->alias && (cnode = dyn_cast <cgraph_node *> (node)))
257 partition->insns -= inline_summary (cnode)->self_size;
258 lto_symtab_encoder_delete_node (partition->encoder, node);
259 node->aux = (void *)((size_t)node->aux - 1);
263 /* Group cgrah nodes by input files. This is used mainly for testing
264 right now. */
266 void
267 lto_1_to_1_map (void)
269 symtab_node *node;
270 struct lto_file_decl_data *file_data;
271 struct pointer_map_t *pmap;
272 ltrans_partition partition;
273 void **slot;
274 int npartitions = 0;
276 pmap = pointer_map_create ();
278 FOR_EACH_SYMBOL (node)
280 if (node->get_partitioning_class () != SYMBOL_PARTITION
281 || symbol_partitioned_p (node))
282 continue;
284 file_data = node->lto_file_data;
286 if (file_data)
288 slot = pointer_map_contains (pmap, file_data);
289 if (slot)
290 partition = (ltrans_partition) *slot;
291 else
293 partition = new_partition (file_data->file_name);
294 slot = pointer_map_insert (pmap, file_data);
295 *slot = partition;
296 npartitions++;
299 else if (!file_data && ltrans_partitions.length ())
300 partition = ltrans_partitions[0];
301 else
303 partition = new_partition ("");
304 slot = pointer_map_insert (pmap, NULL);
305 *slot = partition;
306 npartitions++;
309 add_symbol_to_partition (partition, node);
312 /* If the cgraph is empty, create one cgraph node set so that there is still
313 an output file for any variables that need to be exported in a DSO. */
314 if (!npartitions)
315 new_partition ("empty");
317 pointer_map_destroy (pmap);
321 /* Maximal partitioning. Put every new symbol into new partition if possible. */
323 void
324 lto_max_map (void)
326 symtab_node *node;
327 ltrans_partition partition;
328 int npartitions = 0;
330 FOR_EACH_SYMBOL (node)
332 if (node->get_partitioning_class () != SYMBOL_PARTITION
333 || symbol_partitioned_p (node))
334 continue;
335 partition = new_partition (node->asm_name ());
336 add_symbol_to_partition (partition, node);
337 npartitions++;
339 if (!npartitions)
340 new_partition ("empty");
343 /* Helper function for qsort; sort nodes by order. */
344 static int
345 node_cmp (const void *pa, const void *pb)
347 const struct cgraph_node *a = *(const struct cgraph_node * const *) pa;
348 const struct cgraph_node *b = *(const struct cgraph_node * const *) pb;
350 /* Profile reorder flag enables function reordering based on first execution
351 of a function. All functions with profile are placed in ascending
352 order at the beginning. */
354 if (flag_profile_reorder_functions)
356 /* Functions with time profile are sorted in ascending order. */
357 if (a->tp_first_run && b->tp_first_run)
358 return a->tp_first_run != b->tp_first_run
359 ? a->tp_first_run - b->tp_first_run
360 : a->order - b->order;
362 /* Functions with time profile are sorted before the functions
363 that do not have the profile. */
364 if (a->tp_first_run || b->tp_first_run)
365 return b->tp_first_run - a->tp_first_run;
368 return b->order - a->order;
371 /* Helper function for qsort; sort nodes by order. */
372 static int
373 varpool_node_cmp (const void *pa, const void *pb)
375 const varpool_node *a = *(const varpool_node * const *) pa;
376 const varpool_node *b = *(const varpool_node * const *) pb;
377 return b->order - a->order;
380 /* Group cgraph nodes into equally-sized partitions.
382 The partitioning algorithm is simple: nodes are taken in predefined order.
383 The order corresponds to the order we want functions to have in the final
384 output. In the future this will be given by function reordering pass, but
385 at the moment we use the topological order, which is a good approximation.
387 The goal is to partition this linear order into intervals (partitions) so
388 that all the partitions have approximately the same size and the number of
389 callgraph or IPA reference edges crossing boundaries is minimal.
391 This is a lot faster (O(n) in size of callgraph) than algorithms doing
392 priority-based graph clustering that are generally O(n^2) and, since
393 WHOPR is designed to make things go well across partitions, it leads
394 to good results.
396 We compute the expected size of a partition as:
398 max (total_size / lto_partitions, min_partition_size)
400 We use dynamic expected size of partition so small programs are partitioned
401 into enough partitions to allow use of multiple CPUs, while large programs
402 are not partitioned too much. Creating too many partitions significantly
403 increases the streaming overhead.
405 In the future, we would like to bound the maximal size of partitions so as
406 to prevent the LTRANS stage from consuming too much memory. At the moment,
407 however, the WPA stage is the most memory intensive for large benchmarks,
408 since too many types and declarations are read into memory.
410 The function implements a simple greedy algorithm. Nodes are being added
411 to the current partition until after 3/4 of the expected partition size is
412 reached. Past this threshold, we keep track of boundary size (number of
413 edges going to other partitions) and continue adding functions until after
414 the current partition has grown to twice the expected partition size. Then
415 the process is undone to the point where the minimal ratio of boundary size
416 and in-partition calls was reached. */
418 void
419 lto_balanced_map (int n_lto_partitions)
421 int n_nodes = 0;
422 int n_varpool_nodes = 0, varpool_pos = 0, best_varpool_pos = 0;
423 struct cgraph_node **order = XNEWVEC (struct cgraph_node *, cgraph_max_uid);
424 varpool_node **varpool_order = NULL;
425 int i;
426 struct cgraph_node *node;
427 int total_size = 0, best_total_size = 0;
428 int partition_size;
429 ltrans_partition partition;
430 int last_visited_node = 0;
431 varpool_node *vnode;
432 int cost = 0, internal = 0;
433 int best_n_nodes = 0, best_i = 0, best_cost =
434 INT_MAX, best_internal = 0;
435 int npartitions;
436 int current_order = -1;
438 FOR_EACH_VARIABLE (vnode)
439 gcc_assert (!vnode->aux);
441 FOR_EACH_DEFINED_FUNCTION (node)
442 if (node->get_partitioning_class () == SYMBOL_PARTITION)
444 order[n_nodes++] = node;
445 if (!node->alias)
446 total_size += inline_summary (node)->size;
449 /* Streaming works best when the source units do not cross partition
450 boundaries much. This is because importing function from a source
451 unit tends to import a lot of global trees defined there. We should
452 get better about minimizing the function bounday, but until that
453 things works smoother if we order in source order. */
454 qsort (order, n_nodes, sizeof (struct cgraph_node *), node_cmp);
456 if (cgraph_dump_file)
457 for(i = 0; i < n_nodes; i++)
458 fprintf (cgraph_dump_file, "Balanced map symbol order:%s:%u\n", order[i]->name (), order[i]->tp_first_run);
460 if (!flag_toplevel_reorder)
462 FOR_EACH_VARIABLE (vnode)
463 if (vnode->get_partitioning_class () == SYMBOL_PARTITION)
464 n_varpool_nodes++;
465 varpool_order = XNEWVEC (varpool_node *, n_varpool_nodes);
467 n_varpool_nodes = 0;
468 FOR_EACH_VARIABLE (vnode)
469 if (vnode->get_partitioning_class () == SYMBOL_PARTITION)
470 varpool_order[n_varpool_nodes++] = vnode;
471 qsort (varpool_order, n_varpool_nodes, sizeof (varpool_node *),
472 varpool_node_cmp);
475 /* Compute partition size and create the first partition. */
476 partition_size = total_size / n_lto_partitions;
477 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
478 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
479 npartitions = 1;
480 partition = new_partition ("");
481 if (cgraph_dump_file)
482 fprintf (cgraph_dump_file, "Total unit size: %i, partition size: %i\n",
483 total_size, partition_size);
485 for (i = 0; i < n_nodes; i++)
487 if (symbol_partitioned_p (order[i]))
488 continue;
490 current_order = order[i]->order;
492 if (!flag_toplevel_reorder)
493 while (varpool_pos < n_varpool_nodes
494 && varpool_order[varpool_pos]->order < current_order)
496 if (!symbol_partitioned_p (varpool_order[varpool_pos]))
497 add_symbol_to_partition (partition, varpool_order[varpool_pos]);
498 varpool_pos++;
501 add_symbol_to_partition (partition, order[i]);
502 if (!order[i]->alias)
503 total_size -= inline_summary (order[i])->size;
506 /* Once we added a new node to the partition, we also want to add
507 all referenced variables unless they was already added into some
508 earlier partition.
509 add_symbol_to_partition adds possibly multiple nodes and
510 variables that are needed to satisfy needs of ORDER[i].
511 We remember last visited cgraph and varpool node from last iteration
512 of outer loop that allows us to process every new addition.
514 At the same time we compute size of the boundary into COST. Every
515 callgraph or IPA reference edge leaving the partition contributes into
516 COST. Every edge inside partition was earlier computed as one leaving
517 it and thus we need to subtract it from COST. */
518 while (last_visited_node < lto_symtab_encoder_size (partition->encoder))
520 symtab_node *refs_node;
521 int j;
522 struct ipa_ref *ref = NULL;
523 symtab_node *snode = lto_symtab_encoder_deref (partition->encoder,
524 last_visited_node);
526 if (cgraph_node *node = dyn_cast <cgraph_node *> (snode))
528 struct cgraph_edge *edge;
530 refs_node = node;
532 last_visited_node++;
534 gcc_assert (node->definition || node->weakref);
536 /* Compute boundary cost of callgraph edges. */
537 for (edge = node->callees; edge; edge = edge->next_callee)
538 if (edge->callee->definition)
540 int edge_cost = edge->frequency;
541 int index;
543 if (!edge_cost)
544 edge_cost = 1;
545 gcc_assert (edge_cost > 0);
546 index = lto_symtab_encoder_lookup (partition->encoder,
547 edge->callee);
548 if (index != LCC_NOT_FOUND
549 && index < last_visited_node - 1)
550 cost -= edge_cost, internal += edge_cost;
551 else
552 cost += edge_cost;
554 for (edge = node->callers; edge; edge = edge->next_caller)
556 int edge_cost = edge->frequency;
557 int index;
559 gcc_assert (edge->caller->definition);
560 if (!edge_cost)
561 edge_cost = 1;
562 gcc_assert (edge_cost > 0);
563 index = lto_symtab_encoder_lookup (partition->encoder,
564 edge->caller);
565 if (index != LCC_NOT_FOUND
566 && index < last_visited_node - 1)
567 cost -= edge_cost;
568 else
569 cost += edge_cost;
572 else
574 refs_node = snode;
575 last_visited_node++;
578 /* Compute boundary cost of IPA REF edges and at the same time look into
579 variables referenced from current partition and try to add them. */
580 for (j = 0; refs_node->iterate_reference (j, ref); j++)
581 if (is_a <varpool_node *> (ref->referred))
583 int index;
585 vnode = dyn_cast <varpool_node *> (ref->referred);
586 if (!vnode->definition)
587 continue;
588 if (!symbol_partitioned_p (vnode) && flag_toplevel_reorder
589 && vnode->get_partitioning_class () == SYMBOL_PARTITION)
590 add_symbol_to_partition (partition, vnode);
591 index = lto_symtab_encoder_lookup (partition->encoder,
592 vnode);
593 if (index != LCC_NOT_FOUND
594 && index < last_visited_node - 1)
595 cost--, internal++;
596 else
597 cost++;
599 else
601 int index;
603 node = dyn_cast <cgraph_node *> (ref->referred);
604 if (!node->definition)
605 continue;
606 index = lto_symtab_encoder_lookup (partition->encoder,
607 node);
608 if (index != LCC_NOT_FOUND
609 && index < last_visited_node - 1)
610 cost--, internal++;
611 else
612 cost++;
614 for (j = 0; refs_node->iterate_referring (j, ref); j++)
615 if (is_a <varpool_node *> (ref->referring))
617 int index;
619 vnode = dyn_cast <varpool_node *> (ref->referring);
620 gcc_assert (vnode->definition);
621 /* It is better to couple variables with their users, because it allows them
622 to be removed. Coupling with objects they refer to only helps to reduce
623 number of symbols promoted to hidden. */
624 if (!symbol_partitioned_p (vnode) && flag_toplevel_reorder
625 && !vnode->can_remove_if_no_refs_p ()
626 && vnode->get_partitioning_class () == SYMBOL_PARTITION)
627 add_symbol_to_partition (partition, vnode);
628 index = lto_symtab_encoder_lookup (partition->encoder,
629 vnode);
630 if (index != LCC_NOT_FOUND
631 && index < last_visited_node - 1)
632 cost--;
633 else
634 cost++;
636 else
638 int index;
640 node = dyn_cast <cgraph_node *> (ref->referring);
641 gcc_assert (node->definition);
642 index = lto_symtab_encoder_lookup (partition->encoder,
643 node);
644 if (index != LCC_NOT_FOUND
645 && index < last_visited_node - 1)
646 cost--;
647 else
648 cost++;
652 /* If the partition is large enough, start looking for smallest boundary cost. */
653 if (partition->insns < partition_size * 3 / 4
654 || best_cost == INT_MAX
655 || ((!cost
656 || (best_internal * (HOST_WIDE_INT) cost
657 > (internal * (HOST_WIDE_INT)best_cost)))
658 && partition->insns < partition_size * 5 / 4))
660 best_cost = cost;
661 best_internal = internal;
662 best_i = i;
663 best_n_nodes = lto_symtab_encoder_size (partition->encoder);
664 best_total_size = total_size;
665 best_varpool_pos = varpool_pos;
667 if (cgraph_dump_file)
668 fprintf (cgraph_dump_file, "Step %i: added %s/%i, size %i, cost %i/%i "
669 "best %i/%i, step %i\n", i,
670 order[i]->name (), order[i]->order,
671 partition->insns, cost, internal,
672 best_cost, best_internal, best_i);
673 /* Partition is too large, unwind into step when best cost was reached and
674 start new partition. */
675 if (partition->insns > 2 * partition_size)
677 if (best_i != i)
679 if (cgraph_dump_file)
680 fprintf (cgraph_dump_file, "Unwinding %i insertions to step %i\n",
681 i - best_i, best_i);
682 undo_partition (partition, best_n_nodes);
683 varpool_pos = best_varpool_pos;
685 i = best_i;
686 /* When we are finished, avoid creating empty partition. */
687 while (i < n_nodes - 1 && symbol_partitioned_p (order[i + 1]))
688 i++;
689 if (i == n_nodes - 1)
690 break;
691 partition = new_partition ("");
692 last_visited_node = 0;
693 total_size = best_total_size;
694 cost = 0;
696 if (cgraph_dump_file)
697 fprintf (cgraph_dump_file, "New partition\n");
698 best_n_nodes = 0;
699 best_cost = INT_MAX;
701 /* Since the size of partitions is just approximate, update the size after
702 we finished current one. */
703 if (npartitions < n_lto_partitions)
704 partition_size = total_size / (n_lto_partitions - npartitions);
705 else
706 partition_size = INT_MAX;
708 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
709 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
710 npartitions ++;
714 /* Varables that are not reachable from the code go into last partition. */
715 if (flag_toplevel_reorder)
717 FOR_EACH_VARIABLE (vnode)
718 if (vnode->get_partitioning_class () == SYMBOL_PARTITION
719 && !symbol_partitioned_p (vnode))
720 add_symbol_to_partition (partition, vnode);
722 else
724 while (varpool_pos < n_varpool_nodes)
726 if (!symbol_partitioned_p (varpool_order[varpool_pos]))
727 add_symbol_to_partition (partition, varpool_order[varpool_pos]);
728 varpool_pos++;
730 free (varpool_order);
732 free (order);
735 /* Mangle NODE symbol name into a local name.
736 This is necessary to do
737 1) if two or more static vars of same assembler name
738 are merged into single ltrans unit.
739 2) if prevoiusly static var was promoted hidden to avoid possible conflict
740 with symbols defined out of the LTO world.
743 static bool
744 privatize_symbol_name (symtab_node *node)
746 tree decl = node->decl;
747 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
749 /* Our renaming machinery do not handle more than one change of assembler name.
750 We should not need more than one anyway. */
751 if (node->lto_file_data
752 && lto_get_decl_name_mapping (node->lto_file_data, name) != name)
754 if (cgraph_dump_file)
755 fprintf (cgraph_dump_file,
756 "Not privatizing symbol name: %s. It privatized already.\n",
757 name);
758 return false;
760 /* Avoid mangling of already mangled clones.
761 ??? should have a flag whether a symbol has a 'private' name already,
762 since we produce some symbols like that i.e. for global constructors
763 that are not really clones. */
764 if (node->unique_name)
766 if (cgraph_dump_file)
767 fprintf (cgraph_dump_file,
768 "Not privatizing symbol name: %s. Has unique name.\n",
769 name);
770 return false;
772 change_decl_assembler_name (decl, clone_function_name (decl, "lto_priv"));
773 if (node->lto_file_data)
774 lto_record_renamed_decl (node->lto_file_data, name,
775 IDENTIFIER_POINTER
776 (DECL_ASSEMBLER_NAME (decl)));
777 if (cgraph_dump_file)
778 fprintf (cgraph_dump_file,
779 "Privatizing symbol name: %s -> %s\n",
780 name, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
781 return true;
784 /* Promote variable VNODE to be static. */
786 static void
787 promote_symbol (symtab_node *node)
789 /* We already promoted ... */
790 if (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
791 && DECL_VISIBILITY_SPECIFIED (node->decl)
792 && TREE_PUBLIC (node->decl))
793 return;
795 gcc_checking_assert (!TREE_PUBLIC (node->decl)
796 && !DECL_EXTERNAL (node->decl));
797 /* Be sure that newly public symbol does not conflict with anything already
798 defined by the non-LTO part. */
799 privatize_symbol_name (node);
800 TREE_PUBLIC (node->decl) = 1;
801 DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
802 DECL_VISIBILITY_SPECIFIED (node->decl) = true;
803 if (cgraph_dump_file)
804 fprintf (cgraph_dump_file,
805 "Promoting as hidden: %s\n", node->name ());
808 /* Return true if NODE needs named section even if it won't land in the partition
809 symbol table.
810 FIXME: we should really not use named sections for inline clones and master clones. */
812 static bool
813 may_need_named_section_p (lto_symtab_encoder_t encoder, symtab_node *node)
815 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
816 if (!cnode)
817 return false;
818 if (node->real_symbol_p ())
819 return false;
820 return (!encoder
821 || (lto_symtab_encoder_lookup (encoder, node) != LCC_NOT_FOUND
822 && lto_symtab_encoder_encode_body_p (encoder,
823 cnode)));
826 /* If NODE represents a static variable. See if there are other variables
827 of the same name in partition ENCODER (or in whole compilation unit if
828 ENCODER is NULL) and if so, mangle the statics. Always mangle all
829 conflicting statics, so we reduce changes of silently miscompiling
830 asm statements referring to them by symbol name. */
832 static void
833 rename_statics (lto_symtab_encoder_t encoder, symtab_node *node)
835 tree decl = node->decl;
836 symtab_node *s;
837 tree name = DECL_ASSEMBLER_NAME (decl);
839 /* See if this is static symbol. */
840 if ((node->externally_visible
841 /* FIXME: externally_visible is somewhat illogically not set for
842 external symbols (i.e. those not defined). Remove this test
843 once this is fixed. */
844 || DECL_EXTERNAL (node->decl)
845 || !node->real_symbol_p ())
846 && !may_need_named_section_p (encoder, node))
847 return;
849 /* Now walk symbols sharing the same name and see if there are any conflicts.
850 (all types of symbols counts here, since we can not have static of the
851 same name as external or public symbol.) */
852 for (s = symtab_node_for_asm (name);
853 s; s = s->next_sharing_asm_name)
854 if ((s->real_symbol_p () || may_need_named_section_p (encoder, s))
855 && s->decl != node->decl
856 && (!encoder
857 || lto_symtab_encoder_lookup (encoder, s) != LCC_NOT_FOUND))
858 break;
860 /* OK, no confict, so we have nothing to do. */
861 if (!s)
862 return;
864 if (cgraph_dump_file)
865 fprintf (cgraph_dump_file,
866 "Renaming statics with asm name: %s\n", node->name ());
868 /* Assign every symbol in the set that shares the same ASM name an unique
869 mangled name. */
870 for (s = symtab_node_for_asm (name); s;)
871 if (!s->externally_visible
872 && ((s->real_symbol_p ()
873 && !DECL_EXTERNAL (node->decl)
874 && !TREE_PUBLIC (node->decl))
875 || may_need_named_section_p (encoder, s))
876 && (!encoder
877 || lto_symtab_encoder_lookup (encoder, s) != LCC_NOT_FOUND))
879 if (privatize_symbol_name (s))
880 /* Re-start from beginning since we do not know how many symbols changed a name. */
881 s = symtab_node_for_asm (name);
882 else s = s->next_sharing_asm_name;
884 else s = s->next_sharing_asm_name;
887 /* Find out all static decls that need to be promoted to global because
888 of cross file sharing. This function must be run in the WPA mode after
889 all inlinees are added. */
891 void
892 lto_promote_cross_file_statics (void)
894 unsigned i, n_sets;
896 gcc_assert (flag_wpa);
898 /* First compute boundaries. */
899 n_sets = ltrans_partitions.length ();
900 for (i = 0; i < n_sets; i++)
902 ltrans_partition part
903 = ltrans_partitions[i];
904 part->encoder = compute_ltrans_boundary (part->encoder);
907 /* Look at boundaries and promote symbols as needed. */
908 for (i = 0; i < n_sets; i++)
910 lto_symtab_encoder_iterator lsei;
911 lto_symtab_encoder_t encoder = ltrans_partitions[i]->encoder;
913 for (lsei = lsei_start (encoder); !lsei_end_p (lsei);
914 lsei_next (&lsei))
916 symtab_node *node = lsei_node (lsei);
918 /* If symbol is static, rename it if its assembler name clash with
919 anything else in this unit. */
920 rename_statics (encoder, node);
922 /* No need to promote if symbol already is externally visible ... */
923 if (node->externally_visible
924 /* ... or if it is part of current partition ... */
925 || lto_symtab_encoder_in_partition_p (encoder, node)
926 /* ... or if we do not partition it. This mean that it will
927 appear in every partition refernecing it. */
928 || node->get_partitioning_class () != SYMBOL_PARTITION)
929 continue;
931 promote_symbol (node);
936 /* Rename statics in the whole unit in the case that
937 we do -flto-partition=none. */
939 void
940 lto_promote_statics_nonwpa (void)
942 symtab_node *node;
943 FOR_EACH_SYMBOL (node)
944 rename_statics (NULL, node);