1 /* LTO partitioning logic routines.
2 Copyright (C) 2009-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
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"
28 #include "basic-block.h"
31 #include "hard-reg-set.h"
33 #include "fold-const.h"
34 #include "internal-fn.h"
37 #include "alloc-pool.h"
38 #include "lto-streamer.h"
41 #include "symbol-summary.h"
43 #include "ipa-inline.h"
44 #include "ipa-utils.h"
45 #include "lto-partition.h"
46 #include "stringpool.h"
48 vec
<ltrans_partition
> ltrans_partitions
;
50 static void add_symbol_to_partition (ltrans_partition part
, symtab_node
*node
);
53 /* Create new partition with name NAME. */
55 static ltrans_partition
56 new_partition (const char *name
)
58 ltrans_partition part
= XCNEW (struct ltrans_partition_def
);
59 part
->encoder
= lto_symtab_encoder_new (false);
63 ltrans_partitions
.safe_push (part
);
67 /* Free memory used by ltrans datastructures. */
70 free_ltrans_partitions (void)
73 ltrans_partition part
;
74 for (idx
= 0; ltrans_partitions
.iterate (idx
, &part
); idx
++)
76 if (part
->initializers_visited
)
77 delete part
->initializers_visited
;
78 /* Symtab encoder is freed after streaming. */
81 ltrans_partitions
.release ();
84 /* Return true if symbol is already in some partition. */
87 symbol_partitioned_p (symtab_node
*node
)
92 /* Add references into the partition. */
94 add_references_to_partition (ltrans_partition part
, symtab_node
*node
)
97 struct ipa_ref
*ref
= NULL
;
99 /* Add all duplicated references to the partition. */
100 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
101 if (ref
->referred
->get_partitioning_class () == SYMBOL_DUPLICATE
)
102 add_symbol_to_partition (part
, ref
->referred
);
103 /* References to a readonly variable may be constant foled into its value.
104 Recursively look into the initializers of the constant variable and add
106 else if (is_a
<varpool_node
*> (ref
->referred
)
107 && (dyn_cast
<varpool_node
*> (ref
->referred
)
108 ->ctor_useable_for_folding_p ()
109 || POINTER_BOUNDS_P (ref
->referred
->decl
))
110 && !lto_symtab_encoder_in_partition_p (part
->encoder
, ref
->referred
))
112 if (!part
->initializers_visited
)
113 part
->initializers_visited
= new hash_set
<symtab_node
*>;
114 if (!part
->initializers_visited
->add (ref
->referred
))
115 add_references_to_partition (part
, ref
->referred
);
119 /* Helper function for add_symbol_to_partition doing the actual dirty work
120 of adding NODE to PART. */
123 add_symbol_to_partition_1 (ltrans_partition part
, symtab_node
*node
)
125 enum symbol_partitioning_class c
= node
->get_partitioning_class ();
129 /* If NODE is already there, we have nothing to do. */
130 if (lto_symtab_encoder_in_partition_p (part
->encoder
, node
))
133 /* non-duplicated aliases or tunks of a duplicated symbol needs to be output
136 Be lax about comdats; they may or may not be duplicated and we may
137 end up in need to duplicate keyed comdat because it has unkeyed alias. */
138 if (c
== SYMBOL_PARTITION
&& !DECL_COMDAT (node
->decl
)
139 && symbol_partitioned_p (node
))
142 /* Be sure that we never try to duplicate partitioned symbol
143 or add external symbol. */
144 gcc_assert (c
!= SYMBOL_EXTERNAL
145 && (c
== SYMBOL_DUPLICATE
|| !symbol_partitioned_p (node
)));
149 lto_set_symtab_encoder_in_partition (part
->encoder
, node
);
151 if (symbol_partitioned_p (node
))
153 node
->in_other_partition
= 1;
154 if (symtab
->dump_file
)
155 fprintf (symtab
->dump_file
,
156 "Symbol node %s now used in multiple partitions\n",
159 node
->aux
= (void *)((size_t)node
->aux
+ 1);
161 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
163 struct cgraph_edge
*e
;
165 part
->insns
+= inline_summaries
->get (cnode
)->self_size
;
167 /* Add all inline clones and callees that are duplicated. */
168 for (e
= cnode
->callees
; e
; e
= e
->next_callee
)
169 if (!e
->inline_failed
)
170 add_symbol_to_partition_1 (part
, e
->callee
);
171 else if (e
->callee
->get_partitioning_class () == SYMBOL_DUPLICATE
)
172 add_symbol_to_partition (part
, e
->callee
);
174 /* Add all thunks associated with the function. */
175 for (e
= cnode
->callers
; e
; e
= e
->next_caller
)
176 if (e
->caller
->thunk
.thunk_p
)
177 add_symbol_to_partition_1 (part
, e
->caller
);
179 /* Instrumented version is actually the same function.
180 Therefore put it into the same partition. */
181 if (cnode
->instrumented_version
)
182 add_symbol_to_partition_1 (part
, cnode
->instrumented_version
);
185 add_references_to_partition (part
, node
);
187 /* Add all aliases associated with the symbol. */
189 FOR_EACH_ALIAS (node
, ref
)
191 add_symbol_to_partition_1 (part
, ref
->referring
);
193 /* Ensure that SAME_COMDAT_GROUP lists all allways added in a group. */
194 if (node
->same_comdat_group
)
195 for (node1
= node
->same_comdat_group
;
196 node1
!= node
; node1
= node1
->same_comdat_group
)
199 bool added
= add_symbol_to_partition_1 (part
, node1
);
205 /* If symbol NODE is really part of other symbol's definition (i.e. it is
206 internal label, thunk, alias or so), return the outer symbol.
207 When add_symbol_to_partition_1 is called on the outer symbol it must
208 eventually add NODE, too. */
210 contained_in_symbol (symtab_node
*node
)
212 /* Weakrefs are never contained in anything. */
215 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
217 cnode
= cnode
->function_symbol ();
218 if (cnode
->global
.inlined_to
)
219 cnode
= cnode
->global
.inlined_to
;
222 else if (varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
))
223 return vnode
->ultimate_alias_target ();
227 /* Add symbol NODE to partition. When definition of NODE is part
228 of other symbol definition, add the other symbol, too. */
231 add_symbol_to_partition (ltrans_partition part
, symtab_node
*node
)
235 /* Verify that we do not try to duplicate something that can not be. */
236 gcc_checking_assert (node
->get_partitioning_class () == SYMBOL_DUPLICATE
237 || !symbol_partitioned_p (node
));
239 while ((node1
= contained_in_symbol (node
)) != node
)
242 /* If we have duplicated symbol contained in something we can not duplicate,
243 we are very badly screwed. The other way is possible, so we do not
244 assert this in add_symbol_to_partition_1.
246 Be lax about comdats; they may or may not be duplicated and we may
247 end up in need to duplicate keyed comdat because it has unkeyed alias. */
249 gcc_assert (node
->get_partitioning_class () == SYMBOL_DUPLICATE
250 || DECL_COMDAT (node
->decl
)
251 || !symbol_partitioned_p (node
));
253 add_symbol_to_partition_1 (part
, node
);
256 /* Undo all additions until number of cgraph nodes in PARITION is N_CGRAPH_NODES
257 and number of varpool nodes is N_VARPOOL_NODES. */
260 undo_partition (ltrans_partition partition
, unsigned int n_nodes
)
262 while (lto_symtab_encoder_size (partition
->encoder
) > (int)n_nodes
)
264 symtab_node
*node
= lto_symtab_encoder_deref (partition
->encoder
,
266 partition
->symbols
--;
269 /* After UNDO we no longer know what was visited. */
270 if (partition
->initializers_visited
)
271 delete partition
->initializers_visited
;
272 partition
->initializers_visited
= NULL
;
274 if (!node
->alias
&& (cnode
= dyn_cast
<cgraph_node
*> (node
)))
275 partition
->insns
-= inline_summaries
->get (cnode
)->self_size
;
276 lto_symtab_encoder_delete_node (partition
->encoder
, node
);
277 node
->aux
= (void *)((size_t)node
->aux
- 1);
281 /* Group cgrah nodes by input files. This is used mainly for testing
285 lto_1_to_1_map (void)
288 struct lto_file_decl_data
*file_data
;
289 hash_map
<lto_file_decl_data
*, ltrans_partition
> pmap
;
290 ltrans_partition partition
;
293 FOR_EACH_SYMBOL (node
)
295 if (node
->get_partitioning_class () != SYMBOL_PARTITION
296 || symbol_partitioned_p (node
))
299 file_data
= node
->lto_file_data
;
303 ltrans_partition
*slot
= &pmap
.get_or_insert (file_data
);
308 partition
= new_partition (file_data
->file_name
);
313 else if (!file_data
&& ltrans_partitions
.length ())
314 partition
= ltrans_partitions
[0];
317 partition
= new_partition ("");
318 pmap
.put (NULL
, partition
);
322 add_symbol_to_partition (partition
, node
);
325 /* If the cgraph is empty, create one cgraph node set so that there is still
326 an output file for any variables that need to be exported in a DSO. */
328 new_partition ("empty");
332 /* Maximal partitioning. Put every new symbol into new partition if possible. */
338 ltrans_partition partition
;
341 FOR_EACH_SYMBOL (node
)
343 if (node
->get_partitioning_class () != SYMBOL_PARTITION
344 || symbol_partitioned_p (node
))
346 partition
= new_partition (node
->asm_name ());
347 add_symbol_to_partition (partition
, node
);
351 new_partition ("empty");
354 /* Helper function for qsort; sort nodes by order. noreorder functions must have
355 been removed earlier. */
357 node_cmp (const void *pa
, const void *pb
)
359 const struct cgraph_node
*a
= *(const struct cgraph_node
* const *) pa
;
360 const struct cgraph_node
*b
= *(const struct cgraph_node
* const *) pb
;
362 /* Profile reorder flag enables function reordering based on first execution
363 of a function. All functions with profile are placed in ascending
364 order at the beginning. */
366 if (flag_profile_reorder_functions
)
368 /* Functions with time profile are sorted in ascending order. */
369 if (a
->tp_first_run
&& b
->tp_first_run
)
370 return a
->tp_first_run
!= b
->tp_first_run
371 ? a
->tp_first_run
- b
->tp_first_run
372 : a
->order
- b
->order
;
374 /* Functions with time profile are sorted before the functions
375 that do not have the profile. */
376 if (a
->tp_first_run
|| b
->tp_first_run
)
377 return b
->tp_first_run
- a
->tp_first_run
;
380 return b
->order
- a
->order
;
383 /* Helper function for qsort; sort nodes by order. */
385 varpool_node_cmp (const void *pa
, const void *pb
)
387 const symtab_node
*a
= *static_cast<const symtab_node
* const *> (pa
);
388 const symtab_node
*b
= *static_cast<const symtab_node
* const *> (pb
);
389 return b
->order
- a
->order
;
392 /* Add all symtab nodes from NEXT_NODE to PARTITION in order. */
395 add_sorted_nodes (vec
<symtab_node
*> &next_nodes
, ltrans_partition partition
)
400 next_nodes
.qsort (varpool_node_cmp
);
401 FOR_EACH_VEC_ELT (next_nodes
, i
, node
)
402 if (!symbol_partitioned_p (node
))
403 add_symbol_to_partition (partition
, node
);
407 /* Group cgraph nodes into equally-sized partitions.
409 The partitioning algorithm is simple: nodes are taken in predefined order.
410 The order corresponds to the order we want functions to have in the final
411 output. In the future this will be given by function reordering pass, but
412 at the moment we use the topological order, which is a good approximation.
414 The goal is to partition this linear order into intervals (partitions) so
415 that all the partitions have approximately the same size and the number of
416 callgraph or IPA reference edges crossing boundaries is minimal.
418 This is a lot faster (O(n) in size of callgraph) than algorithms doing
419 priority-based graph clustering that are generally O(n^2) and, since
420 WHOPR is designed to make things go well across partitions, it leads
423 We compute the expected size of a partition as:
425 max (total_size / lto_partitions, min_partition_size)
427 We use dynamic expected size of partition so small programs are partitioned
428 into enough partitions to allow use of multiple CPUs, while large programs
429 are not partitioned too much. Creating too many partitions significantly
430 increases the streaming overhead.
432 In the future, we would like to bound the maximal size of partitions so as
433 to prevent the LTRANS stage from consuming too much memory. At the moment,
434 however, the WPA stage is the most memory intensive for large benchmarks,
435 since too many types and declarations are read into memory.
437 The function implements a simple greedy algorithm. Nodes are being added
438 to the current partition until after 3/4 of the expected partition size is
439 reached. Past this threshold, we keep track of boundary size (number of
440 edges going to other partitions) and continue adding functions until after
441 the current partition has grown to twice the expected partition size. Then
442 the process is undone to the point where the minimal ratio of boundary size
443 and in-partition calls was reached. */
446 lto_balanced_map (int n_lto_partitions
)
449 int n_varpool_nodes
= 0, varpool_pos
= 0, best_varpool_pos
= 0;
450 struct cgraph_node
**order
= XNEWVEC (cgraph_node
*, symtab
->cgraph_max_uid
);
451 auto_vec
<cgraph_node
*> noreorder
;
452 auto_vec
<varpool_node
*> varpool_order
;
454 struct cgraph_node
*node
;
455 int original_total_size
, total_size
= 0, best_total_size
= 0;
457 ltrans_partition partition
;
458 int last_visited_node
= 0;
460 int cost
= 0, internal
= 0;
461 int best_n_nodes
= 0, best_i
= 0, best_cost
=
462 INT_MAX
, best_internal
= 0;
464 int current_order
= -1;
465 int noreorder_pos
= 0;
467 FOR_EACH_VARIABLE (vnode
)
468 gcc_assert (!vnode
->aux
);
470 FOR_EACH_DEFINED_FUNCTION (node
)
471 if (node
->get_partitioning_class () == SYMBOL_PARTITION
)
473 if (node
->no_reorder
)
474 noreorder
.safe_push (node
);
476 order
[n_nodes
++] = node
;
478 total_size
+= inline_summaries
->get (node
)->size
;
481 original_total_size
= total_size
;
483 /* Streaming works best when the source units do not cross partition
484 boundaries much. This is because importing function from a source
485 unit tends to import a lot of global trees defined there. We should
486 get better about minimizing the function bounday, but until that
487 things works smoother if we order in source order. */
488 qsort (order
, n_nodes
, sizeof (struct cgraph_node
*), node_cmp
);
489 noreorder
.qsort (node_cmp
);
491 if (symtab
->dump_file
)
493 for(i
= 0; i
< n_nodes
; i
++)
494 fprintf (symtab
->dump_file
, "Balanced map symbol order:%s:%u\n",
495 order
[i
]->name (), order
[i
]->tp_first_run
);
496 for(i
= 0; i
< (int)noreorder
.length(); i
++)
497 fprintf (symtab
->dump_file
, "Balanced map symbol no_reorder:%s:%u\n",
498 noreorder
[i
]->name (), noreorder
[i
]->tp_first_run
);
501 /* Collect all variables that should not be reordered. */
502 FOR_EACH_VARIABLE (vnode
)
503 if (vnode
->get_partitioning_class () == SYMBOL_PARTITION
504 && (!flag_toplevel_reorder
|| vnode
->no_reorder
))
505 varpool_order
.safe_push (vnode
);
506 n_varpool_nodes
= varpool_order
.length ();
507 varpool_order
.qsort (varpool_node_cmp
);
509 /* Compute partition size and create the first partition. */
510 partition_size
= total_size
/ n_lto_partitions
;
511 if (partition_size
< PARAM_VALUE (MIN_PARTITION_SIZE
))
512 partition_size
= PARAM_VALUE (MIN_PARTITION_SIZE
);
514 partition
= new_partition ("");
515 if (symtab
->dump_file
)
516 fprintf (symtab
->dump_file
, "Total unit size: %i, partition size: %i\n",
517 total_size
, partition_size
);
519 auto_vec
<symtab_node
*> next_nodes
;
521 for (i
= 0; i
< n_nodes
; i
++)
523 if (symbol_partitioned_p (order
[i
]))
526 current_order
= order
[i
]->order
;
528 /* Output noreorder and varpool in program order first. */
529 next_nodes
.truncate (0);
530 while (varpool_pos
< n_varpool_nodes
531 && varpool_order
[varpool_pos
]->order
< current_order
)
532 next_nodes
.safe_push (varpool_order
[varpool_pos
++]);
533 while (noreorder_pos
< (int)noreorder
.length ()
534 && noreorder
[noreorder_pos
]->order
< current_order
)
536 if (!noreorder
[noreorder_pos
]->alias
)
537 total_size
-= inline_summaries
->get (noreorder
[noreorder_pos
])->size
;
538 next_nodes
.safe_push (noreorder
[noreorder_pos
++]);
540 add_sorted_nodes (next_nodes
, partition
);
542 add_symbol_to_partition (partition
, order
[i
]);
543 if (!order
[i
]->alias
)
544 total_size
-= inline_summaries
->get (order
[i
])->size
;
547 /* Once we added a new node to the partition, we also want to add
548 all referenced variables unless they was already added into some
550 add_symbol_to_partition adds possibly multiple nodes and
551 variables that are needed to satisfy needs of ORDER[i].
552 We remember last visited cgraph and varpool node from last iteration
553 of outer loop that allows us to process every new addition.
555 At the same time we compute size of the boundary into COST. Every
556 callgraph or IPA reference edge leaving the partition contributes into
557 COST. Every edge inside partition was earlier computed as one leaving
558 it and thus we need to subtract it from COST. */
559 while (last_visited_node
< lto_symtab_encoder_size (partition
->encoder
))
561 symtab_node
*refs_node
;
563 struct ipa_ref
*ref
= NULL
;
564 symtab_node
*snode
= lto_symtab_encoder_deref (partition
->encoder
,
567 if (cgraph_node
*node
= dyn_cast
<cgraph_node
*> (snode
))
569 struct cgraph_edge
*edge
;
575 gcc_assert (node
->definition
|| node
->weakref
);
577 /* Compute boundary cost of callgraph edges. */
578 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
579 if (edge
->callee
->definition
)
581 int edge_cost
= edge
->frequency
;
586 gcc_assert (edge_cost
> 0);
587 index
= lto_symtab_encoder_lookup (partition
->encoder
,
589 if (index
!= LCC_NOT_FOUND
590 && index
< last_visited_node
- 1)
591 cost
-= edge_cost
, internal
+= edge_cost
;
595 for (edge
= node
->callers
; edge
; edge
= edge
->next_caller
)
597 int edge_cost
= edge
->frequency
;
600 gcc_assert (edge
->caller
->definition
);
603 gcc_assert (edge_cost
> 0);
604 index
= lto_symtab_encoder_lookup (partition
->encoder
,
606 if (index
!= LCC_NOT_FOUND
607 && index
< last_visited_node
- 1)
619 /* Compute boundary cost of IPA REF edges and at the same time look into
620 variables referenced from current partition and try to add them. */
621 for (j
= 0; refs_node
->iterate_reference (j
, ref
); j
++)
622 if (is_a
<varpool_node
*> (ref
->referred
))
626 vnode
= dyn_cast
<varpool_node
*> (ref
->referred
);
627 if (!vnode
->definition
)
629 if (!symbol_partitioned_p (vnode
) && flag_toplevel_reorder
630 && !vnode
->no_reorder
631 && vnode
->get_partitioning_class () == SYMBOL_PARTITION
)
632 add_symbol_to_partition (partition
, vnode
);
633 index
= lto_symtab_encoder_lookup (partition
->encoder
,
635 if (index
!= LCC_NOT_FOUND
636 && index
< last_visited_node
- 1)
645 node
= dyn_cast
<cgraph_node
*> (ref
->referred
);
646 if (!node
->definition
)
648 index
= lto_symtab_encoder_lookup (partition
->encoder
,
650 if (index
!= LCC_NOT_FOUND
651 && index
< last_visited_node
- 1)
656 for (j
= 0; refs_node
->iterate_referring (j
, ref
); j
++)
657 if (is_a
<varpool_node
*> (ref
->referring
))
661 vnode
= dyn_cast
<varpool_node
*> (ref
->referring
);
662 gcc_assert (vnode
->definition
);
663 /* It is better to couple variables with their users, because it allows them
664 to be removed. Coupling with objects they refer to only helps to reduce
665 number of symbols promoted to hidden. */
666 if (!symbol_partitioned_p (vnode
) && flag_toplevel_reorder
667 && !vnode
->no_reorder
668 && !vnode
->can_remove_if_no_refs_p ()
669 && vnode
->get_partitioning_class () == SYMBOL_PARTITION
)
670 add_symbol_to_partition (partition
, vnode
);
671 index
= lto_symtab_encoder_lookup (partition
->encoder
,
673 if (index
!= LCC_NOT_FOUND
674 && index
< last_visited_node
- 1)
683 node
= dyn_cast
<cgraph_node
*> (ref
->referring
);
684 gcc_assert (node
->definition
);
685 index
= lto_symtab_encoder_lookup (partition
->encoder
,
687 if (index
!= LCC_NOT_FOUND
688 && index
< last_visited_node
- 1)
695 /* If the partition is large enough, start looking for smallest boundary cost. */
696 if (partition
->insns
< partition_size
* 3 / 4
697 || best_cost
== INT_MAX
699 || (best_internal
* (HOST_WIDE_INT
) cost
700 > (internal
* (HOST_WIDE_INT
)best_cost
)))
701 && partition
->insns
< partition_size
* 5 / 4))
704 best_internal
= internal
;
706 best_n_nodes
= lto_symtab_encoder_size (partition
->encoder
);
707 best_total_size
= total_size
;
708 best_varpool_pos
= varpool_pos
;
710 if (symtab
->dump_file
)
711 fprintf (symtab
->dump_file
, "Step %i: added %s/%i, size %i, cost %i/%i "
712 "best %i/%i, step %i\n", i
,
713 order
[i
]->name (), order
[i
]->order
,
714 partition
->insns
, cost
, internal
,
715 best_cost
, best_internal
, best_i
);
716 /* Partition is too large, unwind into step when best cost was reached and
717 start new partition. */
718 if (partition
->insns
> 2 * partition_size
)
722 if (symtab
->dump_file
)
723 fprintf (symtab
->dump_file
, "Unwinding %i insertions to step %i\n",
725 undo_partition (partition
, best_n_nodes
);
726 varpool_pos
= best_varpool_pos
;
729 /* When we are finished, avoid creating empty partition. */
730 while (i
< n_nodes
- 1 && symbol_partitioned_p (order
[i
+ 1]))
732 if (i
== n_nodes
- 1)
734 partition
= new_partition ("");
735 last_visited_node
= 0;
736 total_size
= best_total_size
;
739 if (symtab
->dump_file
)
740 fprintf (symtab
->dump_file
, "New partition\n");
744 /* Since the size of partitions is just approximate, update the size after
745 we finished current one. */
746 if (npartitions
< n_lto_partitions
)
747 partition_size
= total_size
/ (n_lto_partitions
- npartitions
);
749 partition_size
= INT_MAX
;
751 if (partition_size
< PARAM_VALUE (MIN_PARTITION_SIZE
))
752 partition_size
= PARAM_VALUE (MIN_PARTITION_SIZE
);
757 next_nodes
.truncate (0);
759 /* Varables that are not reachable from the code go into last partition. */
760 if (flag_toplevel_reorder
)
762 FOR_EACH_VARIABLE (vnode
)
763 if (vnode
->get_partitioning_class () == SYMBOL_PARTITION
764 && !symbol_partitioned_p (vnode
)
765 && !vnode
->no_reorder
)
766 next_nodes
.safe_push (vnode
);
769 /* Output remaining ordered symbols. */
770 while (varpool_pos
< n_varpool_nodes
)
771 next_nodes
.safe_push (varpool_order
[varpool_pos
++]);
772 while (noreorder_pos
< (int)noreorder
.length ())
773 next_nodes
.safe_push (noreorder
[noreorder_pos
++]);
774 add_sorted_nodes (next_nodes
, partition
);
778 if (symtab
->dump_file
)
780 fprintf (symtab
->dump_file
, "\nPartition sizes:\n");
781 unsigned partitions
= ltrans_partitions
.length ();
783 for (unsigned i
= 0; i
< partitions
; i
++)
785 ltrans_partition p
= ltrans_partitions
[i
];
786 fprintf (symtab
->dump_file
, "partition %d contains %d (%2.2f%%)"
787 " symbols and %d (%2.2f%%) insns\n", i
, p
->symbols
,
788 100.0 * p
->symbols
/ n_nodes
, p
->insns
,
789 100.0 * p
->insns
/ original_total_size
);
792 fprintf (symtab
->dump_file
, "\n");
796 /* Return true if we must not change the name of the NODE. The name as
797 extracted from the corresponding decl should be passed in NAME. */
800 must_not_rename (symtab_node
*node
, const char *name
)
802 /* Our renaming machinery do not handle more than one change of assembler name.
803 We should not need more than one anyway. */
804 if (node
->lto_file_data
805 && lto_get_decl_name_mapping (node
->lto_file_data
, name
) != name
)
807 if (symtab
->dump_file
)
808 fprintf (symtab
->dump_file
,
809 "Not privatizing symbol name: %s. It privatized already.\n",
813 /* Avoid mangling of already mangled clones.
814 ??? should have a flag whether a symbol has a 'private' name already,
815 since we produce some symbols like that i.e. for global constructors
816 that are not really clones. */
817 if (node
->unique_name
)
819 if (symtab
->dump_file
)
820 fprintf (symtab
->dump_file
,
821 "Not privatizing symbol name: %s. Has unique name.\n",
828 /* If we are an offload compiler, we may have to rewrite symbols to be
829 valid on this target. Return either PTR or a modified version of it. */
832 maybe_rewrite_identifier (const char *ptr
)
834 #if defined ACCEL_COMPILER && (defined NO_DOT_IN_LABEL || defined NO_DOLLAR_IN_LABEL)
835 #ifndef NO_DOT_IN_LABEL
837 const char reject
[] = "$";
838 #elif !defined NO_DOLLAR_IN_LABEL
840 const char reject
[] = ".";
843 const char reject
[] = ".$";
847 const char *match
= ptr
;
850 size_t off
= strcspn (match
, reject
);
851 if (match
[off
] == '\0')
855 copy
= xstrdup (ptr
);
866 /* Ensure that the symbol in NODE is valid for the target, and if not,
870 validize_symbol_for_target (symtab_node
*node
)
872 tree decl
= node
->decl
;
873 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
875 if (must_not_rename (node
, name
))
878 const char *name2
= maybe_rewrite_identifier (name
);
881 symtab
->change_decl_assembler_name (decl
, get_identifier (name2
));
882 if (node
->lto_file_data
)
883 lto_record_renamed_decl (node
->lto_file_data
, name
,
885 (DECL_ASSEMBLER_NAME (decl
)));
889 /* Helper for privatize_symbol_name. Mangle NODE symbol name
890 represented by DECL. */
893 privatize_symbol_name_1 (symtab_node
*node
, tree decl
)
895 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
897 if (must_not_rename (node
, name
))
900 name
= maybe_rewrite_identifier (name
);
901 symtab
->change_decl_assembler_name (decl
,
902 clone_function_name_1 (name
,
905 if (node
->lto_file_data
)
906 lto_record_renamed_decl (node
->lto_file_data
, name
,
908 (DECL_ASSEMBLER_NAME (decl
)));
910 if (symtab
->dump_file
)
911 fprintf (symtab
->dump_file
,
912 "Privatizing symbol name: %s -> %s\n",
913 name
, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
)));
918 /* Mangle NODE symbol name into a local name.
919 This is necessary to do
920 1) if two or more static vars of same assembler name
921 are merged into single ltrans unit.
922 2) if previously static var was promoted hidden to avoid possible conflict
923 with symbols defined out of the LTO world. */
926 privatize_symbol_name (symtab_node
*node
)
928 if (!privatize_symbol_name_1 (node
, node
->decl
))
931 /* We could change name which is a target of transparent alias
932 chain of instrumented function name. Fix alias chain if so .*/
933 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
935 tree iname
= NULL_TREE
;
936 if (cnode
->instrumentation_clone
)
938 /* If we want to privatize instrumentation clone
939 then we also need to privatize original function. */
940 if (cnode
->instrumented_version
)
941 privatize_symbol_name (cnode
->instrumented_version
);
943 privatize_symbol_name_1 (cnode
, cnode
->orig_decl
);
944 iname
= DECL_ASSEMBLER_NAME (cnode
->decl
);
945 TREE_CHAIN (iname
) = DECL_ASSEMBLER_NAME (cnode
->orig_decl
);
947 else if (cnode
->instrumented_version
948 && cnode
->instrumented_version
->orig_decl
== cnode
->decl
)
950 iname
= DECL_ASSEMBLER_NAME (cnode
->instrumented_version
->decl
);
951 TREE_CHAIN (iname
) = DECL_ASSEMBLER_NAME (cnode
->decl
);
958 /* Promote variable VNODE to be static. */
961 promote_symbol (symtab_node
*node
)
963 /* We already promoted ... */
964 if (DECL_VISIBILITY (node
->decl
) == VISIBILITY_HIDDEN
965 && DECL_VISIBILITY_SPECIFIED (node
->decl
)
966 && TREE_PUBLIC (node
->decl
))
968 validize_symbol_for_target (node
);
972 gcc_checking_assert (!TREE_PUBLIC (node
->decl
)
973 && !DECL_EXTERNAL (node
->decl
));
974 /* Be sure that newly public symbol does not conflict with anything already
975 defined by the non-LTO part. */
976 privatize_symbol_name (node
);
977 TREE_PUBLIC (node
->decl
) = 1;
978 DECL_VISIBILITY (node
->decl
) = VISIBILITY_HIDDEN
;
979 DECL_VISIBILITY_SPECIFIED (node
->decl
) = true;
980 if (symtab
->dump_file
)
981 fprintf (symtab
->dump_file
,
982 "Promoting as hidden: %s\n", node
->name ());
985 /* Return true if NODE needs named section even if it won't land in the partition
987 FIXME: we should really not use named sections for inline clones and master clones. */
990 may_need_named_section_p (lto_symtab_encoder_t encoder
, symtab_node
*node
)
992 struct cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
995 if (node
->real_symbol_p ())
998 || (lto_symtab_encoder_lookup (encoder
, node
) != LCC_NOT_FOUND
999 && lto_symtab_encoder_encode_body_p (encoder
,
1003 /* If NODE represents a static variable. See if there are other variables
1004 of the same name in partition ENCODER (or in whole compilation unit if
1005 ENCODER is NULL) and if so, mangle the statics. Always mangle all
1006 conflicting statics, so we reduce changes of silently miscompiling
1007 asm statements referring to them by symbol name. */
1010 rename_statics (lto_symtab_encoder_t encoder
, symtab_node
*node
)
1012 tree decl
= node
->decl
;
1014 tree name
= DECL_ASSEMBLER_NAME (decl
);
1016 /* See if this is static symbol. */
1017 if ((node
->externally_visible
1018 /* FIXME: externally_visible is somewhat illogically not set for
1019 external symbols (i.e. those not defined). Remove this test
1020 once this is fixed. */
1021 || DECL_EXTERNAL (node
->decl
)
1022 || !node
->real_symbol_p ())
1023 && !may_need_named_section_p (encoder
, node
))
1026 /* Now walk symbols sharing the same name and see if there are any conflicts.
1027 (all types of symbols counts here, since we can not have static of the
1028 same name as external or public symbol.) */
1029 for (s
= symtab_node::get_for_asmname (name
);
1030 s
; s
= s
->next_sharing_asm_name
)
1031 if ((s
->real_symbol_p () || may_need_named_section_p (encoder
, s
))
1032 && s
->decl
!= node
->decl
1034 || lto_symtab_encoder_lookup (encoder
, s
) != LCC_NOT_FOUND
))
1037 /* OK, no confict, so we have nothing to do. */
1041 if (symtab
->dump_file
)
1042 fprintf (symtab
->dump_file
,
1043 "Renaming statics with asm name: %s\n", node
->name ());
1045 /* Assign every symbol in the set that shares the same ASM name an unique
1047 for (s
= symtab_node::get_for_asmname (name
); s
;)
1048 if (!s
->externally_visible
1049 && ((s
->real_symbol_p ()
1050 && !DECL_EXTERNAL (node
->decl
)
1051 && !TREE_PUBLIC (node
->decl
))
1052 || may_need_named_section_p (encoder
, s
))
1054 || lto_symtab_encoder_lookup (encoder
, s
) != LCC_NOT_FOUND
))
1056 if (privatize_symbol_name (s
))
1057 /* Re-start from beginning since we do not know how many symbols changed a name. */
1058 s
= symtab_node::get_for_asmname (name
);
1059 else s
= s
->next_sharing_asm_name
;
1061 else s
= s
->next_sharing_asm_name
;
1064 /* Find out all static decls that need to be promoted to global because
1065 of cross file sharing. This function must be run in the WPA mode after
1066 all inlinees are added. */
1069 lto_promote_cross_file_statics (void)
1073 gcc_assert (flag_wpa
);
1075 lto_stream_offload_p
= false;
1076 select_what_to_stream ();
1078 /* First compute boundaries. */
1079 n_sets
= ltrans_partitions
.length ();
1080 for (i
= 0; i
< n_sets
; i
++)
1082 ltrans_partition part
1083 = ltrans_partitions
[i
];
1084 part
->encoder
= compute_ltrans_boundary (part
->encoder
);
1087 /* Look at boundaries and promote symbols as needed. */
1088 for (i
= 0; i
< n_sets
; i
++)
1090 lto_symtab_encoder_iterator lsei
;
1091 lto_symtab_encoder_t encoder
= ltrans_partitions
[i
]->encoder
;
1093 for (lsei
= lsei_start (encoder
); !lsei_end_p (lsei
);
1096 symtab_node
*node
= lsei_node (lsei
);
1098 /* If symbol is static, rename it if its assembler name clash with
1099 anything else in this unit. */
1100 rename_statics (encoder
, node
);
1102 /* No need to promote if symbol already is externally visible ... */
1103 if (node
->externally_visible
1104 /* ... or if it is part of current partition ... */
1105 || lto_symtab_encoder_in_partition_p (encoder
, node
)
1106 /* ... or if we do not partition it. This mean that it will
1107 appear in every partition refernecing it. */
1108 || node
->get_partitioning_class () != SYMBOL_PARTITION
)
1110 validize_symbol_for_target (node
);
1114 promote_symbol (node
);
1119 /* Rename statics in the whole unit in the case that
1120 we do -flto-partition=none. */
1123 lto_promote_statics_nonwpa (void)
1126 FOR_EACH_SYMBOL (node
)
1128 rename_statics (NULL
, node
);
1129 validize_symbol_for_target (node
);