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"
27 #include "double-int.h"
35 #include "fold-const.h"
38 #include "hard-reg-set.h"
41 #include "basic-block.h"
42 #include "tree-ssa-alias.h"
43 #include "internal-fn.h"
44 #include "gimple-expr.h"
48 #include "plugin-api.h"
51 #include "lto-streamer.h"
54 #include "alloc-pool.h"
55 #include "symbol-summary.h"
57 #include "ipa-inline.h"
58 #include "ipa-utils.h"
59 #include "lto-partition.h"
61 vec
<ltrans_partition
> ltrans_partitions
;
63 static void add_symbol_to_partition (ltrans_partition part
, symtab_node
*node
);
66 /* Create new partition with name NAME. */
68 static ltrans_partition
69 new_partition (const char *name
)
71 ltrans_partition part
= XCNEW (struct ltrans_partition_def
);
72 part
->encoder
= lto_symtab_encoder_new (false);
75 ltrans_partitions
.safe_push (part
);
79 /* Free memory used by ltrans datastructures. */
82 free_ltrans_partitions (void)
85 ltrans_partition part
;
86 for (idx
= 0; ltrans_partitions
.iterate (idx
, &part
); idx
++)
88 if (part
->initializers_visited
)
89 delete part
->initializers_visited
;
90 /* Symtab encoder is freed after streaming. */
93 ltrans_partitions
.release ();
96 /* Return true if symbol is already in some partition. */
99 symbol_partitioned_p (symtab_node
*node
)
104 /* Add references into the partition. */
106 add_references_to_partition (ltrans_partition part
, symtab_node
*node
)
109 struct ipa_ref
*ref
= NULL
;
111 /* Add all duplicated references to the partition. */
112 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
113 if (ref
->referred
->get_partitioning_class () == SYMBOL_DUPLICATE
)
114 add_symbol_to_partition (part
, ref
->referred
);
115 /* References to a readonly variable may be constant foled into its value.
116 Recursively look into the initializers of the constant variable and add
118 else if (is_a
<varpool_node
*> (ref
->referred
)
119 && (dyn_cast
<varpool_node
*> (ref
->referred
)
120 ->ctor_useable_for_folding_p ()
121 || POINTER_BOUNDS_P (ref
->referred
->decl
))
122 && !lto_symtab_encoder_in_partition_p (part
->encoder
, ref
->referred
))
124 if (!part
->initializers_visited
)
125 part
->initializers_visited
= new hash_set
<symtab_node
*>;
126 if (!part
->initializers_visited
->add (ref
->referred
))
127 add_references_to_partition (part
, ref
->referred
);
131 /* Helper function for add_symbol_to_partition doing the actual dirty work
132 of adding NODE to PART. */
135 add_symbol_to_partition_1 (ltrans_partition part
, symtab_node
*node
)
137 enum symbol_partitioning_class c
= node
->get_partitioning_class ();
141 /* If NODE is already there, we have nothing to do. */
142 if (lto_symtab_encoder_in_partition_p (part
->encoder
, node
))
145 /* non-duplicated aliases or tunks of a duplicated symbol needs to be output
148 Be lax about comdats; they may or may not be duplicated and we may
149 end up in need to duplicate keyed comdat because it has unkeyed alias. */
150 if (c
== SYMBOL_PARTITION
&& !DECL_COMDAT (node
->decl
)
151 && symbol_partitioned_p (node
))
154 /* Be sure that we never try to duplicate partitioned symbol
155 or add external symbol. */
156 gcc_assert (c
!= SYMBOL_EXTERNAL
157 && (c
== SYMBOL_DUPLICATE
|| !symbol_partitioned_p (node
)));
159 lto_set_symtab_encoder_in_partition (part
->encoder
, node
);
161 if (symbol_partitioned_p (node
))
163 node
->in_other_partition
= 1;
164 if (symtab
->dump_file
)
165 fprintf (symtab
->dump_file
,
166 "Symbol node %s now used in multiple partitions\n",
169 node
->aux
= (void *)((size_t)node
->aux
+ 1);
171 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
173 struct cgraph_edge
*e
;
175 part
->insns
+= inline_summaries
->get (cnode
)->self_size
;
177 /* Add all inline clones and callees that are duplicated. */
178 for (e
= cnode
->callees
; e
; e
= e
->next_callee
)
179 if (!e
->inline_failed
)
180 add_symbol_to_partition_1 (part
, e
->callee
);
181 else if (e
->callee
->get_partitioning_class () == SYMBOL_DUPLICATE
)
182 add_symbol_to_partition (part
, e
->callee
);
184 /* Add all thunks associated with the function. */
185 for (e
= cnode
->callers
; e
; e
= e
->next_caller
)
186 if (e
->caller
->thunk
.thunk_p
)
187 add_symbol_to_partition_1 (part
, e
->caller
);
189 /* Instrumented version is actually the same function.
190 Therefore put it into the same partition. */
191 if (cnode
->instrumented_version
)
192 add_symbol_to_partition_1 (part
, cnode
->instrumented_version
);
195 add_references_to_partition (part
, node
);
197 /* Add all aliases associated with the symbol. */
199 FOR_EACH_ALIAS (node
, ref
)
201 add_symbol_to_partition_1 (part
, ref
->referring
);
203 /* Ensure that SAME_COMDAT_GROUP lists all allways added in a group. */
204 if (node
->same_comdat_group
)
205 for (node1
= node
->same_comdat_group
;
206 node1
!= node
; node1
= node1
->same_comdat_group
)
209 bool added
= add_symbol_to_partition_1 (part
, node1
);
215 /* If symbol NODE is really part of other symbol's definition (i.e. it is
216 internal label, thunk, alias or so), return the outer symbol.
217 When add_symbol_to_partition_1 is called on the outer symbol it must
218 eventually add NODE, too. */
220 contained_in_symbol (symtab_node
*node
)
222 /* Weakrefs are never contained in anything. */
225 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
227 cnode
= cnode
->function_symbol ();
228 if (cnode
->global
.inlined_to
)
229 cnode
= cnode
->global
.inlined_to
;
232 else if (varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
))
233 return vnode
->ultimate_alias_target ();
237 /* Add symbol NODE to partition. When definition of NODE is part
238 of other symbol definition, add the other symbol, too. */
241 add_symbol_to_partition (ltrans_partition part
, symtab_node
*node
)
245 /* Verify that we do not try to duplicate something that can not be. */
246 gcc_checking_assert (node
->get_partitioning_class () == SYMBOL_DUPLICATE
247 || !symbol_partitioned_p (node
));
249 while ((node1
= contained_in_symbol (node
)) != node
)
252 /* If we have duplicated symbol contained in something we can not duplicate,
253 we are very badly screwed. The other way is possible, so we do not
254 assert this in add_symbol_to_partition_1.
256 Be lax about comdats; they may or may not be duplicated and we may
257 end up in need to duplicate keyed comdat because it has unkeyed alias. */
259 gcc_assert (node
->get_partitioning_class () == SYMBOL_DUPLICATE
260 || DECL_COMDAT (node
->decl
)
261 || !symbol_partitioned_p (node
));
263 add_symbol_to_partition_1 (part
, node
);
266 /* Undo all additions until number of cgraph nodes in PARITION is N_CGRAPH_NODES
267 and number of varpool nodes is N_VARPOOL_NODES. */
270 undo_partition (ltrans_partition partition
, unsigned int n_nodes
)
272 while (lto_symtab_encoder_size (partition
->encoder
) > (int)n_nodes
)
274 symtab_node
*node
= lto_symtab_encoder_deref (partition
->encoder
,
278 /* After UNDO we no longer know what was visited. */
279 if (partition
->initializers_visited
)
280 delete partition
->initializers_visited
;
281 partition
->initializers_visited
= NULL
;
283 if (!node
->alias
&& (cnode
= dyn_cast
<cgraph_node
*> (node
)))
284 partition
->insns
-= inline_summaries
->get (cnode
)->self_size
;
285 lto_symtab_encoder_delete_node (partition
->encoder
, node
);
286 node
->aux
= (void *)((size_t)node
->aux
- 1);
290 /* Group cgrah nodes by input files. This is used mainly for testing
294 lto_1_to_1_map (void)
297 struct lto_file_decl_data
*file_data
;
298 hash_map
<lto_file_decl_data
*, ltrans_partition
> pmap
;
299 ltrans_partition partition
;
302 FOR_EACH_SYMBOL (node
)
304 if (node
->get_partitioning_class () != SYMBOL_PARTITION
305 || symbol_partitioned_p (node
))
308 file_data
= node
->lto_file_data
;
312 ltrans_partition
*slot
= &pmap
.get_or_insert (file_data
);
317 partition
= new_partition (file_data
->file_name
);
322 else if (!file_data
&& ltrans_partitions
.length ())
323 partition
= ltrans_partitions
[0];
326 partition
= new_partition ("");
327 pmap
.put (NULL
, partition
);
331 add_symbol_to_partition (partition
, node
);
334 /* If the cgraph is empty, create one cgraph node set so that there is still
335 an output file for any variables that need to be exported in a DSO. */
337 new_partition ("empty");
341 /* Maximal partitioning. Put every new symbol into new partition if possible. */
347 ltrans_partition partition
;
350 FOR_EACH_SYMBOL (node
)
352 if (node
->get_partitioning_class () != SYMBOL_PARTITION
353 || symbol_partitioned_p (node
))
355 partition
= new_partition (node
->asm_name ());
356 add_symbol_to_partition (partition
, node
);
360 new_partition ("empty");
363 /* Helper function for qsort; sort nodes by order. noreorder functions must have
364 been removed earlier. */
366 node_cmp (const void *pa
, const void *pb
)
368 const struct cgraph_node
*a
= *(const struct cgraph_node
* const *) pa
;
369 const struct cgraph_node
*b
= *(const struct cgraph_node
* const *) pb
;
371 /* Profile reorder flag enables function reordering based on first execution
372 of a function. All functions with profile are placed in ascending
373 order at the beginning. */
375 if (flag_profile_reorder_functions
)
377 /* Functions with time profile are sorted in ascending order. */
378 if (a
->tp_first_run
&& b
->tp_first_run
)
379 return a
->tp_first_run
!= b
->tp_first_run
380 ? a
->tp_first_run
- b
->tp_first_run
381 : a
->order
- b
->order
;
383 /* Functions with time profile are sorted before the functions
384 that do not have the profile. */
385 if (a
->tp_first_run
|| b
->tp_first_run
)
386 return b
->tp_first_run
- a
->tp_first_run
;
389 return b
->order
- a
->order
;
392 /* Helper function for qsort; sort nodes by order. */
394 varpool_node_cmp (const void *pa
, const void *pb
)
396 const symtab_node
*a
= *static_cast<const symtab_node
* const *> (pa
);
397 const symtab_node
*b
= *static_cast<const symtab_node
* const *> (pb
);
398 return b
->order
- a
->order
;
401 /* Add all symtab nodes from NEXT_NODE to PARTITION in order. */
404 add_sorted_nodes (vec
<symtab_node
*> &next_nodes
, ltrans_partition partition
)
409 next_nodes
.qsort (varpool_node_cmp
);
410 FOR_EACH_VEC_ELT (next_nodes
, i
, node
)
411 if (!symbol_partitioned_p (node
))
412 add_symbol_to_partition (partition
, node
);
416 /* Group cgraph nodes into equally-sized partitions.
418 The partitioning algorithm is simple: nodes are taken in predefined order.
419 The order corresponds to the order we want functions to have in the final
420 output. In the future this will be given by function reordering pass, but
421 at the moment we use the topological order, which is a good approximation.
423 The goal is to partition this linear order into intervals (partitions) so
424 that all the partitions have approximately the same size and the number of
425 callgraph or IPA reference edges crossing boundaries is minimal.
427 This is a lot faster (O(n) in size of callgraph) than algorithms doing
428 priority-based graph clustering that are generally O(n^2) and, since
429 WHOPR is designed to make things go well across partitions, it leads
432 We compute the expected size of a partition as:
434 max (total_size / lto_partitions, min_partition_size)
436 We use dynamic expected size of partition so small programs are partitioned
437 into enough partitions to allow use of multiple CPUs, while large programs
438 are not partitioned too much. Creating too many partitions significantly
439 increases the streaming overhead.
441 In the future, we would like to bound the maximal size of partitions so as
442 to prevent the LTRANS stage from consuming too much memory. At the moment,
443 however, the WPA stage is the most memory intensive for large benchmarks,
444 since too many types and declarations are read into memory.
446 The function implements a simple greedy algorithm. Nodes are being added
447 to the current partition until after 3/4 of the expected partition size is
448 reached. Past this threshold, we keep track of boundary size (number of
449 edges going to other partitions) and continue adding functions until after
450 the current partition has grown to twice the expected partition size. Then
451 the process is undone to the point where the minimal ratio of boundary size
452 and in-partition calls was reached. */
455 lto_balanced_map (int n_lto_partitions
)
458 int n_varpool_nodes
= 0, varpool_pos
= 0, best_varpool_pos
= 0;
459 struct cgraph_node
**order
= XNEWVEC (cgraph_node
*, symtab
->cgraph_max_uid
);
460 auto_vec
<cgraph_node
*> noreorder
;
461 auto_vec
<varpool_node
*> varpool_order
;
463 struct cgraph_node
*node
;
464 int total_size
= 0, best_total_size
= 0;
466 ltrans_partition partition
;
467 int last_visited_node
= 0;
469 int cost
= 0, internal
= 0;
470 int best_n_nodes
= 0, best_i
= 0, best_cost
=
471 INT_MAX
, best_internal
= 0;
473 int current_order
= -1;
474 int noreorder_pos
= 0;
476 FOR_EACH_VARIABLE (vnode
)
477 gcc_assert (!vnode
->aux
);
479 FOR_EACH_DEFINED_FUNCTION (node
)
480 if (node
->get_partitioning_class () == SYMBOL_PARTITION
)
482 if (node
->no_reorder
)
483 noreorder
.safe_push (node
);
485 order
[n_nodes
++] = node
;
487 total_size
+= inline_summaries
->get (node
)->size
;
490 /* Streaming works best when the source units do not cross partition
491 boundaries much. This is because importing function from a source
492 unit tends to import a lot of global trees defined there. We should
493 get better about minimizing the function bounday, but until that
494 things works smoother if we order in source order. */
495 qsort (order
, n_nodes
, sizeof (struct cgraph_node
*), node_cmp
);
496 noreorder
.qsort (node_cmp
);
498 if (symtab
->dump_file
)
500 for(i
= 0; i
< n_nodes
; i
++)
501 fprintf (symtab
->dump_file
, "Balanced map symbol order:%s:%u\n",
502 order
[i
]->name (), order
[i
]->tp_first_run
);
503 for(i
= 0; i
< (int)noreorder
.length(); i
++)
504 fprintf (symtab
->dump_file
, "Balanced map symbol no_reorder:%s:%u\n",
505 noreorder
[i
]->name (), noreorder
[i
]->tp_first_run
);
508 /* Collect all variables that should not be reordered. */
509 FOR_EACH_VARIABLE (vnode
)
510 if (vnode
->get_partitioning_class () == SYMBOL_PARTITION
511 && (!flag_toplevel_reorder
|| vnode
->no_reorder
))
512 varpool_order
.safe_push (vnode
);
513 n_varpool_nodes
= varpool_order
.length ();
514 varpool_order
.qsort (varpool_node_cmp
);
516 /* Compute partition size and create the first partition. */
517 partition_size
= total_size
/ n_lto_partitions
;
518 if (partition_size
< PARAM_VALUE (MIN_PARTITION_SIZE
))
519 partition_size
= PARAM_VALUE (MIN_PARTITION_SIZE
);
521 partition
= new_partition ("");
522 if (symtab
->dump_file
)
523 fprintf (symtab
->dump_file
, "Total unit size: %i, partition size: %i\n",
524 total_size
, partition_size
);
526 auto_vec
<symtab_node
*> next_nodes
;
528 for (i
= 0; i
< n_nodes
; i
++)
530 if (symbol_partitioned_p (order
[i
]))
533 current_order
= order
[i
]->order
;
535 /* Output noreorder and varpool in program order first. */
536 next_nodes
.truncate (0);
537 while (varpool_pos
< n_varpool_nodes
538 && varpool_order
[varpool_pos
]->order
< current_order
)
539 next_nodes
.safe_push (varpool_order
[varpool_pos
++]);
540 while (noreorder_pos
< (int)noreorder
.length ()
541 && noreorder
[noreorder_pos
]->order
< current_order
)
543 if (!noreorder
[noreorder_pos
]->alias
)
544 total_size
-= inline_summaries
->get (noreorder
[noreorder_pos
])->size
;
545 next_nodes
.safe_push (noreorder
[noreorder_pos
++]);
547 add_sorted_nodes (next_nodes
, partition
);
549 add_symbol_to_partition (partition
, order
[i
]);
550 if (!order
[i
]->alias
)
551 total_size
-= inline_summaries
->get (order
[i
])->size
;
554 /* Once we added a new node to the partition, we also want to add
555 all referenced variables unless they was already added into some
557 add_symbol_to_partition adds possibly multiple nodes and
558 variables that are needed to satisfy needs of ORDER[i].
559 We remember last visited cgraph and varpool node from last iteration
560 of outer loop that allows us to process every new addition.
562 At the same time we compute size of the boundary into COST. Every
563 callgraph or IPA reference edge leaving the partition contributes into
564 COST. Every edge inside partition was earlier computed as one leaving
565 it and thus we need to subtract it from COST. */
566 while (last_visited_node
< lto_symtab_encoder_size (partition
->encoder
))
568 symtab_node
*refs_node
;
570 struct ipa_ref
*ref
= NULL
;
571 symtab_node
*snode
= lto_symtab_encoder_deref (partition
->encoder
,
574 if (cgraph_node
*node
= dyn_cast
<cgraph_node
*> (snode
))
576 struct cgraph_edge
*edge
;
582 gcc_assert (node
->definition
|| node
->weakref
);
584 /* Compute boundary cost of callgraph edges. */
585 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
586 if (edge
->callee
->definition
)
588 int edge_cost
= edge
->frequency
;
593 gcc_assert (edge_cost
> 0);
594 index
= lto_symtab_encoder_lookup (partition
->encoder
,
596 if (index
!= LCC_NOT_FOUND
597 && index
< last_visited_node
- 1)
598 cost
-= edge_cost
, internal
+= edge_cost
;
602 for (edge
= node
->callers
; edge
; edge
= edge
->next_caller
)
604 int edge_cost
= edge
->frequency
;
607 gcc_assert (edge
->caller
->definition
);
610 gcc_assert (edge_cost
> 0);
611 index
= lto_symtab_encoder_lookup (partition
->encoder
,
613 if (index
!= LCC_NOT_FOUND
614 && index
< last_visited_node
- 1)
626 /* Compute boundary cost of IPA REF edges and at the same time look into
627 variables referenced from current partition and try to add them. */
628 for (j
= 0; refs_node
->iterate_reference (j
, ref
); j
++)
629 if (is_a
<varpool_node
*> (ref
->referred
))
633 vnode
= dyn_cast
<varpool_node
*> (ref
->referred
);
634 if (!vnode
->definition
)
636 if (!symbol_partitioned_p (vnode
) && flag_toplevel_reorder
637 && !vnode
->no_reorder
638 && vnode
->get_partitioning_class () == SYMBOL_PARTITION
)
639 add_symbol_to_partition (partition
, vnode
);
640 index
= lto_symtab_encoder_lookup (partition
->encoder
,
642 if (index
!= LCC_NOT_FOUND
643 && index
< last_visited_node
- 1)
652 node
= dyn_cast
<cgraph_node
*> (ref
->referred
);
653 if (!node
->definition
)
655 index
= lto_symtab_encoder_lookup (partition
->encoder
,
657 if (index
!= LCC_NOT_FOUND
658 && index
< last_visited_node
- 1)
663 for (j
= 0; refs_node
->iterate_referring (j
, ref
); j
++)
664 if (is_a
<varpool_node
*> (ref
->referring
))
668 vnode
= dyn_cast
<varpool_node
*> (ref
->referring
);
669 gcc_assert (vnode
->definition
);
670 /* It is better to couple variables with their users, because it allows them
671 to be removed. Coupling with objects they refer to only helps to reduce
672 number of symbols promoted to hidden. */
673 if (!symbol_partitioned_p (vnode
) && flag_toplevel_reorder
674 && !vnode
->no_reorder
675 && !vnode
->can_remove_if_no_refs_p ()
676 && vnode
->get_partitioning_class () == SYMBOL_PARTITION
)
677 add_symbol_to_partition (partition
, vnode
);
678 index
= lto_symtab_encoder_lookup (partition
->encoder
,
680 if (index
!= LCC_NOT_FOUND
681 && index
< last_visited_node
- 1)
690 node
= dyn_cast
<cgraph_node
*> (ref
->referring
);
691 gcc_assert (node
->definition
);
692 index
= lto_symtab_encoder_lookup (partition
->encoder
,
694 if (index
!= LCC_NOT_FOUND
695 && index
< last_visited_node
- 1)
702 /* If the partition is large enough, start looking for smallest boundary cost. */
703 if (partition
->insns
< partition_size
* 3 / 4
704 || best_cost
== INT_MAX
706 || (best_internal
* (HOST_WIDE_INT
) cost
707 > (internal
* (HOST_WIDE_INT
)best_cost
)))
708 && partition
->insns
< partition_size
* 5 / 4))
711 best_internal
= internal
;
713 best_n_nodes
= lto_symtab_encoder_size (partition
->encoder
);
714 best_total_size
= total_size
;
715 best_varpool_pos
= varpool_pos
;
717 if (symtab
->dump_file
)
718 fprintf (symtab
->dump_file
, "Step %i: added %s/%i, size %i, cost %i/%i "
719 "best %i/%i, step %i\n", i
,
720 order
[i
]->name (), order
[i
]->order
,
721 partition
->insns
, cost
, internal
,
722 best_cost
, best_internal
, best_i
);
723 /* Partition is too large, unwind into step when best cost was reached and
724 start new partition. */
725 if (partition
->insns
> 2 * partition_size
)
729 if (symtab
->dump_file
)
730 fprintf (symtab
->dump_file
, "Unwinding %i insertions to step %i\n",
732 undo_partition (partition
, best_n_nodes
);
733 varpool_pos
= best_varpool_pos
;
736 /* When we are finished, avoid creating empty partition. */
737 while (i
< n_nodes
- 1 && symbol_partitioned_p (order
[i
+ 1]))
739 if (i
== n_nodes
- 1)
741 partition
= new_partition ("");
742 last_visited_node
= 0;
743 total_size
= best_total_size
;
746 if (symtab
->dump_file
)
747 fprintf (symtab
->dump_file
, "New partition\n");
751 /* Since the size of partitions is just approximate, update the size after
752 we finished current one. */
753 if (npartitions
< n_lto_partitions
)
754 partition_size
= total_size
/ (n_lto_partitions
- npartitions
);
756 partition_size
= INT_MAX
;
758 if (partition_size
< PARAM_VALUE (MIN_PARTITION_SIZE
))
759 partition_size
= PARAM_VALUE (MIN_PARTITION_SIZE
);
764 next_nodes
.truncate (0);
766 /* Varables that are not reachable from the code go into last partition. */
767 if (flag_toplevel_reorder
)
769 FOR_EACH_VARIABLE (vnode
)
770 if (vnode
->get_partitioning_class () == SYMBOL_PARTITION
771 && !symbol_partitioned_p (vnode
)
772 && !vnode
->no_reorder
)
773 next_nodes
.safe_push (vnode
);
776 /* Output remaining ordered symbols. */
777 while (varpool_pos
< n_varpool_nodes
)
778 next_nodes
.safe_push (varpool_order
[varpool_pos
++]);
779 while (noreorder_pos
< (int)noreorder
.length ())
780 next_nodes
.safe_push (noreorder
[noreorder_pos
++]);
781 add_sorted_nodes (next_nodes
, partition
);
786 /* Mangle NODE symbol name into a local name.
787 This is necessary to do
788 1) if two or more static vars of same assembler name
789 are merged into single ltrans unit.
790 2) if prevoiusly static var was promoted hidden to avoid possible conflict
791 with symbols defined out of the LTO world.
795 privatize_symbol_name (symtab_node
*node
)
797 tree decl
= node
->decl
;
798 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
801 /* If we want to privatize instrumentation clone
802 then we need to change original function name
803 which is used via transparent alias chain. */
804 if (cnode
&& cnode
->instrumentation_clone
)
805 decl
= cnode
->orig_decl
;
807 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
809 /* Our renaming machinery do not handle more than one change of assembler name.
810 We should not need more than one anyway. */
811 if (node
->lto_file_data
812 && lto_get_decl_name_mapping (node
->lto_file_data
, name
) != name
)
814 if (symtab
->dump_file
)
815 fprintf (symtab
->dump_file
,
816 "Not privatizing symbol name: %s. It privatized already.\n",
820 /* Avoid mangling of already mangled clones.
821 ??? should have a flag whether a symbol has a 'private' name already,
822 since we produce some symbols like that i.e. for global constructors
823 that are not really clones. */
824 if (node
->unique_name
)
826 if (symtab
->dump_file
)
827 fprintf (symtab
->dump_file
,
828 "Not privatizing symbol name: %s. Has unique name.\n",
832 symtab
->change_decl_assembler_name (decl
,
833 clone_function_name (decl
, "lto_priv"));
834 if (node
->lto_file_data
)
835 lto_record_renamed_decl (node
->lto_file_data
, name
,
837 (DECL_ASSEMBLER_NAME (decl
)));
838 /* We could change name which is a target of transparent alias
839 chain of instrumented function name. Fix alias chain if so .*/
842 tree iname
= NULL_TREE
;
843 if (cnode
->instrumentation_clone
)
844 iname
= DECL_ASSEMBLER_NAME (cnode
->decl
);
845 else if (cnode
->instrumented_version
846 && cnode
->instrumented_version
->orig_decl
== decl
)
847 iname
= DECL_ASSEMBLER_NAME (cnode
->instrumented_version
->decl
);
851 gcc_assert (IDENTIFIER_TRANSPARENT_ALIAS (iname
));
852 TREE_CHAIN (iname
) = DECL_ASSEMBLER_NAME (decl
);
855 if (symtab
->dump_file
)
856 fprintf (symtab
->dump_file
,
857 "Privatizing symbol name: %s -> %s\n",
858 name
, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
)));
862 /* Promote variable VNODE to be static. */
865 promote_symbol (symtab_node
*node
)
867 /* We already promoted ... */
868 if (DECL_VISIBILITY (node
->decl
) == VISIBILITY_HIDDEN
869 && DECL_VISIBILITY_SPECIFIED (node
->decl
)
870 && TREE_PUBLIC (node
->decl
))
873 gcc_checking_assert (!TREE_PUBLIC (node
->decl
)
874 && !DECL_EXTERNAL (node
->decl
));
875 /* Be sure that newly public symbol does not conflict with anything already
876 defined by the non-LTO part. */
877 privatize_symbol_name (node
);
878 TREE_PUBLIC (node
->decl
) = 1;
879 DECL_VISIBILITY (node
->decl
) = VISIBILITY_HIDDEN
;
880 DECL_VISIBILITY_SPECIFIED (node
->decl
) = true;
881 if (symtab
->dump_file
)
882 fprintf (symtab
->dump_file
,
883 "Promoting as hidden: %s\n", node
->name ());
886 /* Return true if NODE needs named section even if it won't land in the partition
888 FIXME: we should really not use named sections for inline clones and master clones. */
891 may_need_named_section_p (lto_symtab_encoder_t encoder
, symtab_node
*node
)
893 struct cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
896 if (node
->real_symbol_p ())
899 || (lto_symtab_encoder_lookup (encoder
, node
) != LCC_NOT_FOUND
900 && lto_symtab_encoder_encode_body_p (encoder
,
904 /* If NODE represents a static variable. See if there are other variables
905 of the same name in partition ENCODER (or in whole compilation unit if
906 ENCODER is NULL) and if so, mangle the statics. Always mangle all
907 conflicting statics, so we reduce changes of silently miscompiling
908 asm statements referring to them by symbol name. */
911 rename_statics (lto_symtab_encoder_t encoder
, symtab_node
*node
)
913 tree decl
= node
->decl
;
915 tree name
= DECL_ASSEMBLER_NAME (decl
);
917 /* See if this is static symbol. */
918 if ((node
->externally_visible
919 /* FIXME: externally_visible is somewhat illogically not set for
920 external symbols (i.e. those not defined). Remove this test
921 once this is fixed. */
922 || DECL_EXTERNAL (node
->decl
)
923 || !node
->real_symbol_p ())
924 && !may_need_named_section_p (encoder
, node
))
927 /* Now walk symbols sharing the same name and see if there are any conflicts.
928 (all types of symbols counts here, since we can not have static of the
929 same name as external or public symbol.) */
930 for (s
= symtab_node::get_for_asmname (name
);
931 s
; s
= s
->next_sharing_asm_name
)
932 if ((s
->real_symbol_p () || may_need_named_section_p (encoder
, s
))
933 && s
->decl
!= node
->decl
935 || lto_symtab_encoder_lookup (encoder
, s
) != LCC_NOT_FOUND
))
938 /* OK, no confict, so we have nothing to do. */
942 if (symtab
->dump_file
)
943 fprintf (symtab
->dump_file
,
944 "Renaming statics with asm name: %s\n", node
->name ());
946 /* Assign every symbol in the set that shares the same ASM name an unique
948 for (s
= symtab_node::get_for_asmname (name
); s
;)
949 if (!s
->externally_visible
950 && ((s
->real_symbol_p ()
951 && !DECL_EXTERNAL (node
->decl
)
952 && !TREE_PUBLIC (node
->decl
))
953 || may_need_named_section_p (encoder
, s
))
955 || lto_symtab_encoder_lookup (encoder
, s
) != LCC_NOT_FOUND
))
957 if (privatize_symbol_name (s
))
958 /* Re-start from beginning since we do not know how many symbols changed a name. */
959 s
= symtab_node::get_for_asmname (name
);
960 else s
= s
->next_sharing_asm_name
;
962 else s
= s
->next_sharing_asm_name
;
965 /* Find out all static decls that need to be promoted to global because
966 of cross file sharing. This function must be run in the WPA mode after
967 all inlinees are added. */
970 lto_promote_cross_file_statics (void)
974 gcc_assert (flag_wpa
);
976 select_what_to_stream (false);
978 /* First compute boundaries. */
979 n_sets
= ltrans_partitions
.length ();
980 for (i
= 0; i
< n_sets
; i
++)
982 ltrans_partition part
983 = ltrans_partitions
[i
];
984 part
->encoder
= compute_ltrans_boundary (part
->encoder
);
987 /* Look at boundaries and promote symbols as needed. */
988 for (i
= 0; i
< n_sets
; i
++)
990 lto_symtab_encoder_iterator lsei
;
991 lto_symtab_encoder_t encoder
= ltrans_partitions
[i
]->encoder
;
993 for (lsei
= lsei_start (encoder
); !lsei_end_p (lsei
);
996 symtab_node
*node
= lsei_node (lsei
);
998 /* If symbol is static, rename it if its assembler name clash with
999 anything else in this unit. */
1000 rename_statics (encoder
, node
);
1002 /* No need to promote if symbol already is externally visible ... */
1003 if (node
->externally_visible
1004 /* ... or if it is part of current partition ... */
1005 || lto_symtab_encoder_in_partition_p (encoder
, node
)
1006 /* ... or if we do not partition it. This mean that it will
1007 appear in every partition refernecing it. */
1008 || node
->get_partitioning_class () != SYMBOL_PARTITION
)
1011 promote_symbol (node
);
1016 /* Rename statics in the whole unit in the case that
1017 we do -flto-partition=none. */
1020 lto_promote_statics_nonwpa (void)
1023 FOR_EACH_SYMBOL (node
)
1024 rename_statics (NULL
, node
);