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
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
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/>. */
22 #include "coretypes.h"
25 #include "basic-block.h"
26 #include "tree-ssa-alias.h"
27 #include "internal-fn.h"
28 #include "gimple-expr.h"
33 #include "lto-streamer.h"
36 #include "ipa-inline.h"
37 #include "ipa-utils.h"
38 #include "lto-partition.h"
40 vec
<ltrans_partition
> ltrans_partitions
;
42 static void add_symbol_to_partition (ltrans_partition part
, symtab_node
*node
);
45 /* Create new partition with name NAME. */
47 static ltrans_partition
48 new_partition (const char *name
)
50 ltrans_partition part
= XCNEW (struct ltrans_partition_def
);
51 part
->encoder
= lto_symtab_encoder_new (false);
54 ltrans_partitions
.safe_push (part
);
58 /* Free memory used by ltrans datastructures. */
61 free_ltrans_partitions (void)
64 ltrans_partition part
;
65 for (idx
= 0; ltrans_partitions
.iterate (idx
, &part
); idx
++)
67 if (part
->initializers_visited
)
68 delete part
->initializers_visited
;
69 /* Symtab encoder is freed after streaming. */
72 ltrans_partitions
.release ();
75 /* Return true if symbol is already in some partition. */
78 symbol_partitioned_p (symtab_node
*node
)
83 /* Add references into the partition. */
85 add_references_to_partition (ltrans_partition part
, symtab_node
*node
)
88 struct ipa_ref
*ref
= NULL
;
90 /* Add all duplicated references to the partition. */
91 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
92 if (ref
->referred
->get_partitioning_class () == SYMBOL_DUPLICATE
)
93 add_symbol_to_partition (part
, ref
->referred
);
94 /* References to a readonly variable may be constant foled into its value.
95 Recursively look into the initializers of the constant variable and add
97 else if (is_a
<varpool_node
*> (ref
->referred
)
98 && dyn_cast
<varpool_node
*> (ref
->referred
)
99 ->ctor_useable_for_folding_p ()
100 && !lto_symtab_encoder_in_partition_p (part
->encoder
, ref
->referred
))
102 if (!part
->initializers_visited
)
103 part
->initializers_visited
= new hash_set
<symtab_node
*>;
104 if (!part
->initializers_visited
->add (ref
->referred
))
105 add_references_to_partition (part
, ref
->referred
);
109 /* Helper function for add_symbol_to_partition doing the actual dirty work
110 of adding NODE to PART. */
113 add_symbol_to_partition_1 (ltrans_partition part
, symtab_node
*node
)
115 enum symbol_partitioning_class c
= node
->get_partitioning_class ();
119 /* If NODE is already there, we have nothing to do. */
120 if (lto_symtab_encoder_in_partition_p (part
->encoder
, node
))
123 /* non-duplicated aliases or tunks of a duplicated symbol needs to be output
126 Be lax about comdats; they may or may not be duplicated and we may
127 end up in need to duplicate keyed comdat because it has unkeyed alias. */
128 if (c
== SYMBOL_PARTITION
&& !DECL_COMDAT (node
->decl
)
129 && symbol_partitioned_p (node
))
132 /* Be sure that we never try to duplicate partitioned symbol
133 or add external symbol. */
134 gcc_assert (c
!= SYMBOL_EXTERNAL
135 && (c
== SYMBOL_DUPLICATE
|| !symbol_partitioned_p (node
)));
137 lto_set_symtab_encoder_in_partition (part
->encoder
, node
);
139 if (symbol_partitioned_p (node
))
141 node
->in_other_partition
= 1;
142 if (symtab
->dump_file
)
143 fprintf (symtab
->dump_file
,
144 "Symbol node %s now used in multiple partitions\n",
147 node
->aux
= (void *)((size_t)node
->aux
+ 1);
149 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
151 struct cgraph_edge
*e
;
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
)
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
)
182 bool added
= add_symbol_to_partition_1 (part
, node1
);
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. */
193 contained_in_symbol (symtab_node
*node
)
195 /* Weakrefs are never contained in anything. */
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
;
205 else if (varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
))
206 return vnode
->ultimate_alias_target ();
210 /* Add symbol NODE to partition. When definition of NODE is part
211 of other symbol definition, add the other symbol, too. */
214 add_symbol_to_partition (ltrans_partition part
, symtab_node
*node
)
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
)
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. */
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
,
251 /* After UNDO we no longer know what was visited. */
252 if (partition
->initializers_visited
)
253 delete 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
267 lto_1_to_1_map (void)
270 struct lto_file_decl_data
*file_data
;
271 hash_map
<lto_file_decl_data
*, ltrans_partition
> pmap
;
272 ltrans_partition partition
;
275 FOR_EACH_SYMBOL (node
)
277 if (node
->get_partitioning_class () != SYMBOL_PARTITION
278 || symbol_partitioned_p (node
))
281 file_data
= node
->lto_file_data
;
285 ltrans_partition
*slot
= &pmap
.get_or_insert (file_data
);
290 partition
= new_partition (file_data
->file_name
);
295 else if (!file_data
&& ltrans_partitions
.length ())
296 partition
= ltrans_partitions
[0];
299 partition
= new_partition ("");
300 pmap
.put (NULL
, partition
);
304 add_symbol_to_partition (partition
, node
);
307 /* If the cgraph is empty, create one cgraph node set so that there is still
308 an output file for any variables that need to be exported in a DSO. */
310 new_partition ("empty");
314 /* Maximal partitioning. Put every new symbol into new partition if possible. */
320 ltrans_partition partition
;
323 FOR_EACH_SYMBOL (node
)
325 if (node
->get_partitioning_class () != SYMBOL_PARTITION
326 || symbol_partitioned_p (node
))
328 partition
= new_partition (node
->asm_name ());
329 add_symbol_to_partition (partition
, node
);
333 new_partition ("empty");
336 /* Helper function for qsort; sort nodes by order. noreorder functions must have
337 been removed earlier. */
339 node_cmp (const void *pa
, const void *pb
)
341 const struct cgraph_node
*a
= *(const struct cgraph_node
* const *) pa
;
342 const struct cgraph_node
*b
= *(const struct cgraph_node
* const *) pb
;
344 /* Profile reorder flag enables function reordering based on first execution
345 of a function. All functions with profile are placed in ascending
346 order at the beginning. */
348 if (flag_profile_reorder_functions
)
350 /* Functions with time profile are sorted in ascending order. */
351 if (a
->tp_first_run
&& b
->tp_first_run
)
352 return a
->tp_first_run
!= b
->tp_first_run
353 ? a
->tp_first_run
- b
->tp_first_run
354 : a
->order
- b
->order
;
356 /* Functions with time profile are sorted before the functions
357 that do not have the profile. */
358 if (a
->tp_first_run
|| b
->tp_first_run
)
359 return b
->tp_first_run
- a
->tp_first_run
;
362 return b
->order
- a
->order
;
365 /* Helper function for qsort; sort nodes by order. */
367 varpool_node_cmp (const void *pa
, const void *pb
)
369 const symtab_node
*a
= *static_cast<const symtab_node
* const *> (pa
);
370 const symtab_node
*b
= *static_cast<const symtab_node
* const *> (pb
);
371 return b
->order
- a
->order
;
374 /* Add all symtab nodes from NEXT_NODE to PARTITION in order. */
377 add_sorted_nodes (vec
<symtab_node
*> &next_nodes
, ltrans_partition partition
)
382 next_nodes
.qsort (varpool_node_cmp
);
383 FOR_EACH_VEC_ELT (next_nodes
, i
, node
)
384 if (!symbol_partitioned_p (node
))
385 add_symbol_to_partition (partition
, node
);
389 /* Group cgraph nodes into equally-sized partitions.
391 The partitioning algorithm is simple: nodes are taken in predefined order.
392 The order corresponds to the order we want functions to have in the final
393 output. In the future this will be given by function reordering pass, but
394 at the moment we use the topological order, which is a good approximation.
396 The goal is to partition this linear order into intervals (partitions) so
397 that all the partitions have approximately the same size and the number of
398 callgraph or IPA reference edges crossing boundaries is minimal.
400 This is a lot faster (O(n) in size of callgraph) than algorithms doing
401 priority-based graph clustering that are generally O(n^2) and, since
402 WHOPR is designed to make things go well across partitions, it leads
405 We compute the expected size of a partition as:
407 max (total_size / lto_partitions, min_partition_size)
409 We use dynamic expected size of partition so small programs are partitioned
410 into enough partitions to allow use of multiple CPUs, while large programs
411 are not partitioned too much. Creating too many partitions significantly
412 increases the streaming overhead.
414 In the future, we would like to bound the maximal size of partitions so as
415 to prevent the LTRANS stage from consuming too much memory. At the moment,
416 however, the WPA stage is the most memory intensive for large benchmarks,
417 since too many types and declarations are read into memory.
419 The function implements a simple greedy algorithm. Nodes are being added
420 to the current partition until after 3/4 of the expected partition size is
421 reached. Past this threshold, we keep track of boundary size (number of
422 edges going to other partitions) and continue adding functions until after
423 the current partition has grown to twice the expected partition size. Then
424 the process is undone to the point where the minimal ratio of boundary size
425 and in-partition calls was reached. */
428 lto_balanced_map (int n_lto_partitions
)
431 int n_varpool_nodes
= 0, varpool_pos
= 0, best_varpool_pos
= 0;
432 struct cgraph_node
**order
= XNEWVEC (cgraph_node
*, symtab
->cgraph_max_uid
);
433 auto_vec
<cgraph_node
*> noreorder
;
434 auto_vec
<varpool_node
*> varpool_order
;
436 struct cgraph_node
*node
;
437 int total_size
= 0, best_total_size
= 0;
439 ltrans_partition partition
;
440 int last_visited_node
= 0;
442 int cost
= 0, internal
= 0;
443 int best_n_nodes
= 0, best_i
= 0, best_cost
=
444 INT_MAX
, best_internal
= 0;
446 int current_order
= -1;
447 int noreorder_pos
= 0;
449 FOR_EACH_VARIABLE (vnode
)
450 gcc_assert (!vnode
->aux
);
452 FOR_EACH_DEFINED_FUNCTION (node
)
453 if (node
->get_partitioning_class () == SYMBOL_PARTITION
)
455 if (node
->no_reorder
)
456 noreorder
.safe_push (node
);
458 order
[n_nodes
++] = node
;
460 total_size
+= inline_summary (node
)->size
;
463 /* Streaming works best when the source units do not cross partition
464 boundaries much. This is because importing function from a source
465 unit tends to import a lot of global trees defined there. We should
466 get better about minimizing the function bounday, but until that
467 things works smoother if we order in source order. */
468 qsort (order
, n_nodes
, sizeof (struct cgraph_node
*), node_cmp
);
469 noreorder
.qsort (node_cmp
);
471 if (symtab
->dump_file
)
473 for(i
= 0; i
< n_nodes
; i
++)
474 fprintf (symtab
->dump_file
, "Balanced map symbol order:%s:%u\n",
475 order
[i
]->name (), order
[i
]->tp_first_run
);
476 for(i
= 0; i
< (int)noreorder
.length(); i
++)
477 fprintf (symtab
->dump_file
, "Balanced map symbol no_reorder:%s:%u\n",
478 noreorder
[i
]->name (), noreorder
[i
]->tp_first_run
);
481 /* Collect all variables that should not be reordered. */
482 FOR_EACH_VARIABLE (vnode
)
483 if (vnode
->get_partitioning_class () == SYMBOL_PARTITION
484 && (!flag_toplevel_reorder
|| vnode
->no_reorder
))
485 varpool_order
.safe_push (vnode
);
486 n_varpool_nodes
= varpool_order
.length ();
487 varpool_order
.qsort (varpool_node_cmp
);
489 /* Compute partition size and create the first partition. */
490 partition_size
= total_size
/ n_lto_partitions
;
491 if (partition_size
< PARAM_VALUE (MIN_PARTITION_SIZE
))
492 partition_size
= PARAM_VALUE (MIN_PARTITION_SIZE
);
494 partition
= new_partition ("");
495 if (symtab
->dump_file
)
496 fprintf (symtab
->dump_file
, "Total unit size: %i, partition size: %i\n",
497 total_size
, partition_size
);
499 auto_vec
<symtab_node
*> next_nodes
;
501 for (i
= 0; i
< n_nodes
; i
++)
503 if (symbol_partitioned_p (order
[i
]))
506 current_order
= order
[i
]->order
;
508 /* Output noreorder and varpool in program order first. */
509 next_nodes
.truncate (0);
510 while (varpool_pos
< n_varpool_nodes
511 && varpool_order
[varpool_pos
]->order
< current_order
)
512 next_nodes
.safe_push (varpool_order
[varpool_pos
++]);
513 while (noreorder_pos
< (int)noreorder
.length ()
514 && noreorder
[noreorder_pos
]->order
< current_order
)
516 if (!noreorder
[noreorder_pos
]->alias
)
517 total_size
-= inline_summary (noreorder
[noreorder_pos
])->size
;
518 next_nodes
.safe_push (noreorder
[noreorder_pos
++]);
520 add_sorted_nodes (next_nodes
, partition
);
522 add_symbol_to_partition (partition
, order
[i
]);
523 if (!order
[i
]->alias
)
524 total_size
-= inline_summary (order
[i
])->size
;
527 /* Once we added a new node to the partition, we also want to add
528 all referenced variables unless they was already added into some
530 add_symbol_to_partition adds possibly multiple nodes and
531 variables that are needed to satisfy needs of ORDER[i].
532 We remember last visited cgraph and varpool node from last iteration
533 of outer loop that allows us to process every new addition.
535 At the same time we compute size of the boundary into COST. Every
536 callgraph or IPA reference edge leaving the partition contributes into
537 COST. Every edge inside partition was earlier computed as one leaving
538 it and thus we need to subtract it from COST. */
539 while (last_visited_node
< lto_symtab_encoder_size (partition
->encoder
))
541 symtab_node
*refs_node
;
543 struct ipa_ref
*ref
= NULL
;
544 symtab_node
*snode
= lto_symtab_encoder_deref (partition
->encoder
,
547 if (cgraph_node
*node
= dyn_cast
<cgraph_node
*> (snode
))
549 struct cgraph_edge
*edge
;
555 gcc_assert (node
->definition
|| node
->weakref
);
557 /* Compute boundary cost of callgraph edges. */
558 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
559 if (edge
->callee
->definition
)
561 int edge_cost
= edge
->frequency
;
566 gcc_assert (edge_cost
> 0);
567 index
= lto_symtab_encoder_lookup (partition
->encoder
,
569 if (index
!= LCC_NOT_FOUND
570 && index
< last_visited_node
- 1)
571 cost
-= edge_cost
, internal
+= edge_cost
;
575 for (edge
= node
->callers
; edge
; edge
= edge
->next_caller
)
577 int edge_cost
= edge
->frequency
;
580 gcc_assert (edge
->caller
->definition
);
583 gcc_assert (edge_cost
> 0);
584 index
= lto_symtab_encoder_lookup (partition
->encoder
,
586 if (index
!= LCC_NOT_FOUND
587 && index
< last_visited_node
- 1)
599 /* Compute boundary cost of IPA REF edges and at the same time look into
600 variables referenced from current partition and try to add them. */
601 for (j
= 0; refs_node
->iterate_reference (j
, ref
); j
++)
602 if (is_a
<varpool_node
*> (ref
->referred
))
606 vnode
= dyn_cast
<varpool_node
*> (ref
->referred
);
607 if (!vnode
->definition
)
609 if (!symbol_partitioned_p (vnode
) && flag_toplevel_reorder
610 && !vnode
->no_reorder
611 && vnode
->get_partitioning_class () == SYMBOL_PARTITION
)
612 add_symbol_to_partition (partition
, vnode
);
613 index
= lto_symtab_encoder_lookup (partition
->encoder
,
615 if (index
!= LCC_NOT_FOUND
616 && index
< last_visited_node
- 1)
625 node
= dyn_cast
<cgraph_node
*> (ref
->referred
);
626 if (!node
->definition
)
628 index
= lto_symtab_encoder_lookup (partition
->encoder
,
630 if (index
!= LCC_NOT_FOUND
631 && index
< last_visited_node
- 1)
636 for (j
= 0; refs_node
->iterate_referring (j
, ref
); j
++)
637 if (is_a
<varpool_node
*> (ref
->referring
))
641 vnode
= dyn_cast
<varpool_node
*> (ref
->referring
);
642 gcc_assert (vnode
->definition
);
643 /* It is better to couple variables with their users, because it allows them
644 to be removed. Coupling with objects they refer to only helps to reduce
645 number of symbols promoted to hidden. */
646 if (!symbol_partitioned_p (vnode
) && flag_toplevel_reorder
647 && !vnode
->no_reorder
648 && !vnode
->can_remove_if_no_refs_p ()
649 && vnode
->get_partitioning_class () == SYMBOL_PARTITION
)
650 add_symbol_to_partition (partition
, vnode
);
651 index
= lto_symtab_encoder_lookup (partition
->encoder
,
653 if (index
!= LCC_NOT_FOUND
654 && index
< last_visited_node
- 1)
663 node
= dyn_cast
<cgraph_node
*> (ref
->referring
);
664 gcc_assert (node
->definition
);
665 index
= lto_symtab_encoder_lookup (partition
->encoder
,
667 if (index
!= LCC_NOT_FOUND
668 && index
< last_visited_node
- 1)
675 /* If the partition is large enough, start looking for smallest boundary cost. */
676 if (partition
->insns
< partition_size
* 3 / 4
677 || best_cost
== INT_MAX
679 || (best_internal
* (HOST_WIDE_INT
) cost
680 > (internal
* (HOST_WIDE_INT
)best_cost
)))
681 && partition
->insns
< partition_size
* 5 / 4))
684 best_internal
= internal
;
686 best_n_nodes
= lto_symtab_encoder_size (partition
->encoder
);
687 best_total_size
= total_size
;
688 best_varpool_pos
= varpool_pos
;
690 if (symtab
->dump_file
)
691 fprintf (symtab
->dump_file
, "Step %i: added %s/%i, size %i, cost %i/%i "
692 "best %i/%i, step %i\n", i
,
693 order
[i
]->name (), order
[i
]->order
,
694 partition
->insns
, cost
, internal
,
695 best_cost
, best_internal
, best_i
);
696 /* Partition is too large, unwind into step when best cost was reached and
697 start new partition. */
698 if (partition
->insns
> 2 * partition_size
)
702 if (symtab
->dump_file
)
703 fprintf (symtab
->dump_file
, "Unwinding %i insertions to step %i\n",
705 undo_partition (partition
, best_n_nodes
);
706 varpool_pos
= best_varpool_pos
;
709 /* When we are finished, avoid creating empty partition. */
710 while (i
< n_nodes
- 1 && symbol_partitioned_p (order
[i
+ 1]))
712 if (i
== n_nodes
- 1)
714 partition
= new_partition ("");
715 last_visited_node
= 0;
716 total_size
= best_total_size
;
719 if (symtab
->dump_file
)
720 fprintf (symtab
->dump_file
, "New partition\n");
724 /* Since the size of partitions is just approximate, update the size after
725 we finished current one. */
726 if (npartitions
< n_lto_partitions
)
727 partition_size
= total_size
/ (n_lto_partitions
- npartitions
);
729 partition_size
= INT_MAX
;
731 if (partition_size
< PARAM_VALUE (MIN_PARTITION_SIZE
))
732 partition_size
= PARAM_VALUE (MIN_PARTITION_SIZE
);
737 next_nodes
.truncate (0);
739 /* Varables that are not reachable from the code go into last partition. */
740 if (flag_toplevel_reorder
)
742 FOR_EACH_VARIABLE (vnode
)
743 if (vnode
->get_partitioning_class () == SYMBOL_PARTITION
744 && !symbol_partitioned_p (vnode
)
745 && !vnode
->no_reorder
)
746 next_nodes
.safe_push (vnode
);
749 /* Output remaining ordered symbols. */
750 while (varpool_pos
< n_varpool_nodes
)
751 next_nodes
.safe_push (varpool_order
[varpool_pos
++]);
752 while (noreorder_pos
< (int)noreorder
.length ())
753 next_nodes
.safe_push (noreorder
[noreorder_pos
++]);
754 add_sorted_nodes (next_nodes
, partition
);
759 /* Mangle NODE symbol name into a local name.
760 This is necessary to do
761 1) if two or more static vars of same assembler name
762 are merged into single ltrans unit.
763 2) if prevoiusly static var was promoted hidden to avoid possible conflict
764 with symbols defined out of the LTO world.
768 privatize_symbol_name (symtab_node
*node
)
770 tree decl
= node
->decl
;
771 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
773 /* Our renaming machinery do not handle more than one change of assembler name.
774 We should not need more than one anyway. */
775 if (node
->lto_file_data
776 && lto_get_decl_name_mapping (node
->lto_file_data
, name
) != name
)
778 if (symtab
->dump_file
)
779 fprintf (symtab
->dump_file
,
780 "Not privatizing symbol name: %s. It privatized already.\n",
784 /* Avoid mangling of already mangled clones.
785 ??? should have a flag whether a symbol has a 'private' name already,
786 since we produce some symbols like that i.e. for global constructors
787 that are not really clones. */
788 if (node
->unique_name
)
790 if (symtab
->dump_file
)
791 fprintf (symtab
->dump_file
,
792 "Not privatizing symbol name: %s. Has unique name.\n",
796 symtab
->change_decl_assembler_name (decl
,
797 clone_function_name (decl
, "lto_priv"));
798 if (node
->lto_file_data
)
799 lto_record_renamed_decl (node
->lto_file_data
, name
,
801 (DECL_ASSEMBLER_NAME (decl
)));
802 if (symtab
->dump_file
)
803 fprintf (symtab
->dump_file
,
804 "Privatizing symbol name: %s -> %s\n",
805 name
, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
)));
809 /* Promote variable VNODE to be static. */
812 promote_symbol (symtab_node
*node
)
814 /* We already promoted ... */
815 if (DECL_VISIBILITY (node
->decl
) == VISIBILITY_HIDDEN
816 && DECL_VISIBILITY_SPECIFIED (node
->decl
)
817 && TREE_PUBLIC (node
->decl
))
820 gcc_checking_assert (!TREE_PUBLIC (node
->decl
)
821 && !DECL_EXTERNAL (node
->decl
));
822 /* Be sure that newly public symbol does not conflict with anything already
823 defined by the non-LTO part. */
824 privatize_symbol_name (node
);
825 TREE_PUBLIC (node
->decl
) = 1;
826 DECL_VISIBILITY (node
->decl
) = VISIBILITY_HIDDEN
;
827 DECL_VISIBILITY_SPECIFIED (node
->decl
) = true;
828 if (symtab
->dump_file
)
829 fprintf (symtab
->dump_file
,
830 "Promoting as hidden: %s\n", node
->name ());
833 /* Return true if NODE needs named section even if it won't land in the partition
835 FIXME: we should really not use named sections for inline clones and master clones. */
838 may_need_named_section_p (lto_symtab_encoder_t encoder
, symtab_node
*node
)
840 struct cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
843 if (node
->real_symbol_p ())
846 || (lto_symtab_encoder_lookup (encoder
, node
) != LCC_NOT_FOUND
847 && lto_symtab_encoder_encode_body_p (encoder
,
851 /* If NODE represents a static variable. See if there are other variables
852 of the same name in partition ENCODER (or in whole compilation unit if
853 ENCODER is NULL) and if so, mangle the statics. Always mangle all
854 conflicting statics, so we reduce changes of silently miscompiling
855 asm statements referring to them by symbol name. */
858 rename_statics (lto_symtab_encoder_t encoder
, symtab_node
*node
)
860 tree decl
= node
->decl
;
862 tree name
= DECL_ASSEMBLER_NAME (decl
);
864 /* See if this is static symbol. */
865 if ((node
->externally_visible
866 /* FIXME: externally_visible is somewhat illogically not set for
867 external symbols (i.e. those not defined). Remove this test
868 once this is fixed. */
869 || DECL_EXTERNAL (node
->decl
)
870 || !node
->real_symbol_p ())
871 && !may_need_named_section_p (encoder
, node
))
874 /* Now walk symbols sharing the same name and see if there are any conflicts.
875 (all types of symbols counts here, since we can not have static of the
876 same name as external or public symbol.) */
877 for (s
= symtab_node::get_for_asmname (name
);
878 s
; s
= s
->next_sharing_asm_name
)
879 if ((s
->real_symbol_p () || may_need_named_section_p (encoder
, s
))
880 && s
->decl
!= node
->decl
882 || lto_symtab_encoder_lookup (encoder
, s
) != LCC_NOT_FOUND
))
885 /* OK, no confict, so we have nothing to do. */
889 if (symtab
->dump_file
)
890 fprintf (symtab
->dump_file
,
891 "Renaming statics with asm name: %s\n", node
->name ());
893 /* Assign every symbol in the set that shares the same ASM name an unique
895 for (s
= symtab_node::get_for_asmname (name
); s
;)
896 if (!s
->externally_visible
897 && ((s
->real_symbol_p ()
898 && !DECL_EXTERNAL (node
->decl
)
899 && !TREE_PUBLIC (node
->decl
))
900 || may_need_named_section_p (encoder
, s
))
902 || lto_symtab_encoder_lookup (encoder
, s
) != LCC_NOT_FOUND
))
904 if (privatize_symbol_name (s
))
905 /* Re-start from beginning since we do not know how many symbols changed a name. */
906 s
= symtab_node::get_for_asmname (name
);
907 else s
= s
->next_sharing_asm_name
;
909 else s
= s
->next_sharing_asm_name
;
912 /* Find out all static decls that need to be promoted to global because
913 of cross file sharing. This function must be run in the WPA mode after
914 all inlinees are added. */
917 lto_promote_cross_file_statics (void)
921 gcc_assert (flag_wpa
);
923 /* First compute boundaries. */
924 n_sets
= ltrans_partitions
.length ();
925 for (i
= 0; i
< n_sets
; i
++)
927 ltrans_partition part
928 = ltrans_partitions
[i
];
929 part
->encoder
= compute_ltrans_boundary (part
->encoder
);
932 /* Look at boundaries and promote symbols as needed. */
933 for (i
= 0; i
< n_sets
; i
++)
935 lto_symtab_encoder_iterator lsei
;
936 lto_symtab_encoder_t encoder
= ltrans_partitions
[i
]->encoder
;
938 for (lsei
= lsei_start (encoder
); !lsei_end_p (lsei
);
941 symtab_node
*node
= lsei_node (lsei
);
943 /* If symbol is static, rename it if its assembler name clash with
944 anything else in this unit. */
945 rename_statics (encoder
, node
);
947 /* No need to promote if symbol already is externally visible ... */
948 if (node
->externally_visible
949 /* ... or if it is part of current partition ... */
950 || lto_symtab_encoder_in_partition_p (encoder
, node
)
951 /* ... or if we do not partition it. This mean that it will
952 appear in every partition refernecing it. */
953 || node
->get_partitioning_class () != SYMBOL_PARTITION
)
956 promote_symbol (node
);
961 /* Rename statics in the whole unit in the case that
962 we do -flto-partition=none. */
965 lto_promote_statics_nonwpa (void)
968 FOR_EACH_SYMBOL (node
)
969 rename_statics (NULL
, node
);