* lto-cgraph.c (compute_ltrans_boundary, output_symtab): Fix handling
[official-gcc.git] / gcc / lto / lto-partition.c
blob453343a84717d010c6dbe60b178b63fa18f3908a
1 /* LTO partitioning logic routines.
2 Copyright (C) 2009-2016 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 "target.h"
24 #include "function.h"
25 #include "basic-block.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "alloc-pool.h"
29 #include "stringpool.h"
30 #include "cgraph.h"
31 #include "lto-streamer.h"
32 #include "params.h"
33 #include "symbol-summary.h"
34 #include "ipa-prop.h"
35 #include "ipa-inline.h"
36 #include "lto-partition.h"
38 vec<ltrans_partition> ltrans_partitions;
40 static void add_symbol_to_partition (ltrans_partition part, symtab_node *node);
43 /* Create new partition with name NAME. */
45 static ltrans_partition
46 new_partition (const char *name)
48 ltrans_partition part = XCNEW (struct ltrans_partition_def);
49 part->encoder = lto_symtab_encoder_new (false);
50 part->name = name;
51 part->insns = 0;
52 part->symbols = 0;
53 ltrans_partitions.safe_push (part);
54 return part;
57 /* Free memory used by ltrans datastructures. */
59 void
60 free_ltrans_partitions (void)
62 unsigned int idx;
63 ltrans_partition part;
64 for (idx = 0; ltrans_partitions.iterate (idx, &part); idx++)
66 if (part->initializers_visited)
67 delete part->initializers_visited;
68 /* Symtab encoder is freed after streaming. */
69 free (part);
71 ltrans_partitions.release ();
74 /* Return true if symbol is already in some partition. */
76 static inline bool
77 symbol_partitioned_p (symtab_node *node)
79 return node->aux;
82 /* Add references into the partition. */
83 static void
84 add_references_to_partition (ltrans_partition part, symtab_node *node)
86 int i;
87 struct ipa_ref *ref = NULL;
89 /* Add all duplicated references to the partition. */
90 for (i = 0; node->iterate_reference (i, ref); i++)
91 if (ref->referred->get_partitioning_class () == SYMBOL_DUPLICATE)
92 add_symbol_to_partition (part, ref->referred);
93 /* References to a readonly variable may be constant foled into its value.
94 Recursively look into the initializers of the constant variable and add
95 references, too. */
96 else if (is_a <varpool_node *> (ref->referred)
97 && (dyn_cast <varpool_node *> (ref->referred)
98 ->ctor_useable_for_folding_p ()
99 || POINTER_BOUNDS_P (ref->referred->decl))
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. */
112 static bool
113 add_symbol_to_partition_1 (ltrans_partition part, symtab_node *node)
115 enum symbol_partitioning_class c = node->get_partitioning_class ();
116 struct ipa_ref *ref;
117 symtab_node *node1;
119 /* If NODE is already there, we have nothing to do. */
120 if (lto_symtab_encoder_in_partition_p (part->encoder, node))
121 return true;
123 /* non-duplicated aliases or tunks of a duplicated symbol needs to be output
124 just once.
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))
130 return false;
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 part->symbols++;
139 lto_set_symtab_encoder_in_partition (part->encoder, node);
141 if (symbol_partitioned_p (node))
143 node->in_other_partition = 1;
144 if (symtab->dump_file)
145 fprintf (symtab->dump_file,
146 "Symbol node %s now used in multiple partitions\n",
147 node->name ());
149 node->aux = (void *)((size_t)node->aux + 1);
151 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
153 struct cgraph_edge *e;
154 if (!node->alias)
155 part->insns += inline_summaries->get (cnode)->self_size;
157 /* Add all inline clones and callees that are duplicated. */
158 for (e = cnode->callees; e; e = e->next_callee)
159 if (!e->inline_failed)
160 add_symbol_to_partition_1 (part, e->callee);
161 else if (e->callee->get_partitioning_class () == SYMBOL_DUPLICATE)
162 add_symbol_to_partition (part, e->callee);
164 /* Add all thunks associated with the function. */
165 for (e = cnode->callers; e; e = e->next_caller)
166 if (e->caller->thunk.thunk_p && !e->caller->global.inlined_to)
167 add_symbol_to_partition_1 (part, e->caller);
169 /* Instrumented version is actually the same function.
170 Therefore put it into the same partition. */
171 if (cnode->instrumented_version)
172 add_symbol_to_partition_1 (part, cnode->instrumented_version);
175 add_references_to_partition (part, node);
177 /* Add all aliases associated with the symbol. */
179 FOR_EACH_ALIAS (node, ref)
180 if (!ref->referring->transparent_alias)
181 add_symbol_to_partition_1 (part, ref->referring);
182 else
184 struct ipa_ref *ref2;
185 /* We do not need to add transparent aliases if they are not used.
186 However we must add aliases of transparent aliases if they exist. */
187 FOR_EACH_ALIAS (ref->referring, ref2)
189 /* Nested transparent aliases are not permitted. */
190 gcc_checking_assert (!ref2->referring->transparent_alias);
191 add_symbol_to_partition_1 (part, ref2->referring);
195 /* Ensure that SAME_COMDAT_GROUP lists all allways added in a group. */
196 if (node->same_comdat_group)
197 for (node1 = node->same_comdat_group;
198 node1 != node; node1 = node1->same_comdat_group)
199 if (!node->alias)
201 bool added = add_symbol_to_partition_1 (part, node1);
202 gcc_assert (added);
204 return true;
207 /* If symbol NODE is really part of other symbol's definition (i.e. it is
208 internal label, thunk, alias or so), return the outer symbol.
209 When add_symbol_to_partition_1 is called on the outer symbol it must
210 eventually add NODE, too. */
211 static symtab_node *
212 contained_in_symbol (symtab_node *node)
214 /* There is no need to consider transparent aliases to be part of the
215 definition: they are only useful insite the partition they are output
216 and thus we will always see an explicit reference to it. */
217 if (node->transparent_alias)
218 return node;
219 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
221 cnode = cnode->function_symbol ();
222 if (cnode->global.inlined_to)
223 cnode = cnode->global.inlined_to;
224 return cnode;
226 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
227 return vnode->ultimate_alias_target ();
228 return node;
231 /* Add symbol NODE to partition. When definition of NODE is part
232 of other symbol definition, add the other symbol, too. */
234 static void
235 add_symbol_to_partition (ltrans_partition part, symtab_node *node)
237 symtab_node *node1;
239 /* Verify that we do not try to duplicate something that can not be. */
240 gcc_checking_assert (node->get_partitioning_class () == SYMBOL_DUPLICATE
241 || !symbol_partitioned_p (node));
243 while ((node1 = contained_in_symbol (node)) != node)
244 node = node1;
246 /* If we have duplicated symbol contained in something we can not duplicate,
247 we are very badly screwed. The other way is possible, so we do not
248 assert this in add_symbol_to_partition_1.
250 Be lax about comdats; they may or may not be duplicated and we may
251 end up in need to duplicate keyed comdat because it has unkeyed alias. */
253 gcc_assert (node->get_partitioning_class () == SYMBOL_DUPLICATE
254 || DECL_COMDAT (node->decl)
255 || !symbol_partitioned_p (node));
257 add_symbol_to_partition_1 (part, node);
260 /* Undo all additions until number of cgraph nodes in PARITION is N_CGRAPH_NODES
261 and number of varpool nodes is N_VARPOOL_NODES. */
263 static void
264 undo_partition (ltrans_partition partition, unsigned int n_nodes)
266 while (lto_symtab_encoder_size (partition->encoder) > (int)n_nodes)
268 symtab_node *node = lto_symtab_encoder_deref (partition->encoder,
269 n_nodes);
270 partition->symbols--;
271 cgraph_node *cnode;
273 /* After UNDO we no longer know what was visited. */
274 if (partition->initializers_visited)
275 delete partition->initializers_visited;
276 partition->initializers_visited = NULL;
278 if (!node->alias && (cnode = dyn_cast <cgraph_node *> (node)))
279 partition->insns -= inline_summaries->get (cnode)->self_size;
280 lto_symtab_encoder_delete_node (partition->encoder, node);
281 node->aux = (void *)((size_t)node->aux - 1);
285 /* Group cgrah nodes by input files. This is used mainly for testing
286 right now. */
288 void
289 lto_1_to_1_map (void)
291 symtab_node *node;
292 struct lto_file_decl_data *file_data;
293 hash_map<lto_file_decl_data *, ltrans_partition> pmap;
294 ltrans_partition partition;
295 int npartitions = 0;
297 FOR_EACH_SYMBOL (node)
299 if (node->get_partitioning_class () != SYMBOL_PARTITION
300 || symbol_partitioned_p (node))
301 continue;
303 file_data = node->lto_file_data;
305 if (file_data)
307 ltrans_partition *slot = &pmap.get_or_insert (file_data);
308 if (*slot)
309 partition = *slot;
310 else
312 partition = new_partition (file_data->file_name);
313 *slot = partition;
314 npartitions++;
317 else if (!file_data && ltrans_partitions.length ())
318 partition = ltrans_partitions[0];
319 else
321 partition = new_partition ("");
322 pmap.put (NULL, partition);
323 npartitions++;
326 add_symbol_to_partition (partition, node);
329 /* If the cgraph is empty, create one cgraph node set so that there is still
330 an output file for any variables that need to be exported in a DSO. */
331 if (!npartitions)
332 new_partition ("empty");
336 /* Maximal partitioning. Put every new symbol into new partition if possible. */
338 void
339 lto_max_map (void)
341 symtab_node *node;
342 ltrans_partition partition;
343 int npartitions = 0;
345 FOR_EACH_SYMBOL (node)
347 if (node->get_partitioning_class () != SYMBOL_PARTITION
348 || symbol_partitioned_p (node))
349 continue;
350 partition = new_partition (node->asm_name ());
351 add_symbol_to_partition (partition, node);
352 npartitions++;
354 if (!npartitions)
355 new_partition ("empty");
358 /* Helper function for qsort; sort nodes by order. noreorder functions must have
359 been removed earlier. */
360 static int
361 node_cmp (const void *pa, const void *pb)
363 const struct cgraph_node *a = *(const struct cgraph_node * const *) pa;
364 const struct cgraph_node *b = *(const struct cgraph_node * const *) pb;
366 /* Profile reorder flag enables function reordering based on first execution
367 of a function. All functions with profile are placed in ascending
368 order at the beginning. */
370 if (flag_profile_reorder_functions)
372 /* Functions with time profile are sorted in ascending order. */
373 if (a->tp_first_run && b->tp_first_run)
374 return a->tp_first_run != b->tp_first_run
375 ? a->tp_first_run - b->tp_first_run
376 : a->order - b->order;
378 /* Functions with time profile are sorted before the functions
379 that do not have the profile. */
380 if (a->tp_first_run || b->tp_first_run)
381 return b->tp_first_run - a->tp_first_run;
384 return b->order - a->order;
387 /* Helper function for qsort; sort nodes by order. */
388 static int
389 varpool_node_cmp (const void *pa, const void *pb)
391 const symtab_node *a = *static_cast<const symtab_node * const *> (pa);
392 const symtab_node *b = *static_cast<const symtab_node * const *> (pb);
393 return b->order - a->order;
396 /* Add all symtab nodes from NEXT_NODE to PARTITION in order. */
398 static void
399 add_sorted_nodes (vec<symtab_node *> &next_nodes, ltrans_partition partition)
401 unsigned i;
402 symtab_node *node;
404 next_nodes.qsort (varpool_node_cmp);
405 FOR_EACH_VEC_ELT (next_nodes, i, node)
406 if (!symbol_partitioned_p (node))
407 add_symbol_to_partition (partition, node);
411 /* Group cgraph nodes into equally-sized partitions.
413 The partitioning algorithm is simple: nodes are taken in predefined order.
414 The order corresponds to the order we want functions to have in the final
415 output. In the future this will be given by function reordering pass, but
416 at the moment we use the topological order, which is a good approximation.
418 The goal is to partition this linear order into intervals (partitions) so
419 that all the partitions have approximately the same size and the number of
420 callgraph or IPA reference edges crossing boundaries is minimal.
422 This is a lot faster (O(n) in size of callgraph) than algorithms doing
423 priority-based graph clustering that are generally O(n^2) and, since
424 WHOPR is designed to make things go well across partitions, it leads
425 to good results.
427 We compute the expected size of a partition as:
429 max (total_size / lto_partitions, min_partition_size)
431 We use dynamic expected size of partition so small programs are partitioned
432 into enough partitions to allow use of multiple CPUs, while large programs
433 are not partitioned too much. Creating too many partitions significantly
434 increases the streaming overhead.
436 In the future, we would like to bound the maximal size of partitions so as
437 to prevent the LTRANS stage from consuming too much memory. At the moment,
438 however, the WPA stage is the most memory intensive for large benchmarks,
439 since too many types and declarations are read into memory.
441 The function implements a simple greedy algorithm. Nodes are being added
442 to the current partition until after 3/4 of the expected partition size is
443 reached. Past this threshold, we keep track of boundary size (number of
444 edges going to other partitions) and continue adding functions until after
445 the current partition has grown to twice the expected partition size. Then
446 the process is undone to the point where the minimal ratio of boundary size
447 and in-partition calls was reached. */
449 void
450 lto_balanced_map (int n_lto_partitions, int max_partition_size)
452 int n_nodes = 0;
453 int n_varpool_nodes = 0, varpool_pos = 0, best_varpool_pos = 0;
454 struct cgraph_node **order = XNEWVEC (cgraph_node *, symtab->cgraph_max_uid);
455 auto_vec<cgraph_node *> noreorder;
456 auto_vec<varpool_node *> varpool_order;
457 int i;
458 struct cgraph_node *node;
459 int original_total_size, total_size = 0, best_total_size = 0;
460 int partition_size;
461 ltrans_partition partition;
462 int last_visited_node = 0;
463 varpool_node *vnode;
464 int cost = 0, internal = 0;
465 int best_n_nodes = 0, best_i = 0, best_cost =
466 INT_MAX, best_internal = 0;
467 int npartitions;
468 int current_order = -1;
469 int noreorder_pos = 0;
471 FOR_EACH_VARIABLE (vnode)
472 gcc_assert (!vnode->aux);
474 FOR_EACH_DEFINED_FUNCTION (node)
475 if (node->get_partitioning_class () == SYMBOL_PARTITION)
477 if (node->no_reorder)
478 noreorder.safe_push (node);
479 else
480 order[n_nodes++] = node;
481 if (!node->alias)
482 total_size += inline_summaries->get (node)->size;
485 original_total_size = total_size;
487 /* Streaming works best when the source units do not cross partition
488 boundaries much. This is because importing function from a source
489 unit tends to import a lot of global trees defined there. We should
490 get better about minimizing the function bounday, but until that
491 things works smoother if we order in source order. */
492 qsort (order, n_nodes, sizeof (struct cgraph_node *), node_cmp);
493 noreorder.qsort (node_cmp);
495 if (symtab->dump_file)
497 for(i = 0; i < n_nodes; i++)
498 fprintf (symtab->dump_file, "Balanced map symbol order:%s:%u\n",
499 order[i]->name (), order[i]->tp_first_run);
500 for(i = 0; i < (int)noreorder.length(); i++)
501 fprintf (symtab->dump_file, "Balanced map symbol no_reorder:%s:%u\n",
502 noreorder[i]->name (), noreorder[i]->tp_first_run);
505 /* Collect all variables that should not be reordered. */
506 FOR_EACH_VARIABLE (vnode)
507 if (vnode->get_partitioning_class () == SYMBOL_PARTITION
508 && (!flag_toplevel_reorder || vnode->no_reorder))
509 varpool_order.safe_push (vnode);
510 n_varpool_nodes = varpool_order.length ();
511 varpool_order.qsort (varpool_node_cmp);
513 /* Compute partition size and create the first partition. */
514 if (PARAM_VALUE (MIN_PARTITION_SIZE) > max_partition_size)
515 fatal_error (input_location, "min partition size cannot be greater than max partition size");
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);
520 npartitions = 1;
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]))
531 continue;
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
556 earlier partition.
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;
569 int j;
570 struct ipa_ref *ref = NULL;
571 symtab_node *snode = lto_symtab_encoder_deref (partition->encoder,
572 last_visited_node);
574 if (cgraph_node *node = dyn_cast <cgraph_node *> (snode))
576 struct cgraph_edge *edge;
578 refs_node = node;
580 last_visited_node++;
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;
589 int index;
591 if (!edge_cost)
592 edge_cost = 1;
593 gcc_assert (edge_cost > 0);
594 index = lto_symtab_encoder_lookup (partition->encoder,
595 edge->callee);
596 if (index != LCC_NOT_FOUND
597 && index < last_visited_node - 1)
598 cost -= edge_cost, internal += edge_cost;
599 else
600 cost += edge_cost;
602 for (edge = node->callers; edge; edge = edge->next_caller)
604 int edge_cost = edge->frequency;
605 int index;
607 gcc_assert (edge->caller->definition);
608 if (!edge_cost)
609 edge_cost = 1;
610 gcc_assert (edge_cost > 0);
611 index = lto_symtab_encoder_lookup (partition->encoder,
612 edge->caller);
613 if (index != LCC_NOT_FOUND
614 && index < last_visited_node - 1)
615 cost -= edge_cost;
616 else
617 cost += edge_cost;
620 else
622 refs_node = snode;
623 last_visited_node++;
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))
631 int index;
633 vnode = dyn_cast <varpool_node *> (ref->referred);
634 if (!vnode->definition)
635 continue;
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,
641 vnode);
642 if (index != LCC_NOT_FOUND
643 && index < last_visited_node - 1)
644 cost--, internal++;
645 else
646 cost++;
648 else
650 int index;
652 node = dyn_cast <cgraph_node *> (ref->referred);
653 if (!node->definition)
654 continue;
655 index = lto_symtab_encoder_lookup (partition->encoder,
656 node);
657 if (index != LCC_NOT_FOUND
658 && index < last_visited_node - 1)
659 cost--, internal++;
660 else
661 cost++;
663 for (j = 0; refs_node->iterate_referring (j, ref); j++)
664 if (is_a <varpool_node *> (ref->referring))
666 int index;
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,
679 vnode);
680 if (index != LCC_NOT_FOUND
681 && index < last_visited_node - 1)
682 cost--;
683 else
684 cost++;
686 else
688 int index;
690 node = dyn_cast <cgraph_node *> (ref->referring);
691 gcc_assert (node->definition);
692 index = lto_symtab_encoder_lookup (partition->encoder,
693 node);
694 if (index != LCC_NOT_FOUND
695 && index < last_visited_node - 1)
696 cost--;
697 else
698 cost++;
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
705 || ((!cost
706 || (best_internal * (HOST_WIDE_INT) cost
707 > (internal * (HOST_WIDE_INT)best_cost)))
708 && partition->insns < partition_size * 5 / 4))
710 best_cost = cost;
711 best_internal = internal;
712 best_i = i;
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
726 || partition->insns > max_partition_size)
728 if (best_i != i)
730 if (symtab->dump_file)
731 fprintf (symtab->dump_file, "Unwinding %i insertions to step %i\n",
732 i - best_i, best_i);
733 undo_partition (partition, best_n_nodes);
734 varpool_pos = best_varpool_pos;
736 i = best_i;
737 /* When we are finished, avoid creating empty partition. */
738 while (i < n_nodes - 1 && symbol_partitioned_p (order[i + 1]))
739 i++;
740 if (i == n_nodes - 1)
741 break;
742 partition = new_partition ("");
743 last_visited_node = 0;
744 total_size = best_total_size;
745 cost = 0;
747 if (symtab->dump_file)
748 fprintf (symtab->dump_file, "New partition\n");
749 best_n_nodes = 0;
750 best_cost = INT_MAX;
752 /* Since the size of partitions is just approximate, update the size after
753 we finished current one. */
754 if (npartitions < n_lto_partitions)
755 partition_size = total_size / (n_lto_partitions - npartitions);
756 else
757 partition_size = INT_MAX;
759 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
760 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
761 npartitions ++;
765 next_nodes.truncate (0);
767 /* Varables that are not reachable from the code go into last partition. */
768 if (flag_toplevel_reorder)
770 FOR_EACH_VARIABLE (vnode)
771 if (vnode->get_partitioning_class () == SYMBOL_PARTITION
772 && !symbol_partitioned_p (vnode)
773 && !vnode->no_reorder)
774 next_nodes.safe_push (vnode);
777 /* Output remaining ordered symbols. */
778 while (varpool_pos < n_varpool_nodes)
779 next_nodes.safe_push (varpool_order[varpool_pos++]);
780 while (noreorder_pos < (int)noreorder.length ())
781 next_nodes.safe_push (noreorder[noreorder_pos++]);
782 add_sorted_nodes (next_nodes, partition);
784 free (order);
786 if (symtab->dump_file)
788 fprintf (symtab->dump_file, "\nPartition sizes:\n");
789 unsigned partitions = ltrans_partitions.length ();
791 for (unsigned i = 0; i < partitions ; i++)
793 ltrans_partition p = ltrans_partitions[i];
794 fprintf (symtab->dump_file, "partition %d contains %d (%2.2f%%)"
795 " symbols and %d (%2.2f%%) insns\n", i, p->symbols,
796 100.0 * p->symbols / n_nodes, p->insns,
797 100.0 * p->insns / original_total_size);
800 fprintf (symtab->dump_file, "\n");
804 /* Return true if we must not change the name of the NODE. The name as
805 extracted from the corresponding decl should be passed in NAME. */
807 static bool
808 must_not_rename (symtab_node *node, const char *name)
810 /* Our renaming machinery do not handle more than one change of assembler name.
811 We should not need more than one anyway. */
812 if (node->lto_file_data
813 && lto_get_decl_name_mapping (node->lto_file_data, name) != name)
815 if (symtab->dump_file)
816 fprintf (symtab->dump_file,
817 "Not privatizing symbol name: %s. It privatized already.\n",
818 name);
819 return true;
821 /* Avoid mangling of already mangled clones.
822 ??? should have a flag whether a symbol has a 'private' name already,
823 since we produce some symbols like that i.e. for global constructors
824 that are not really clones. */
825 if (node->unique_name)
827 if (symtab->dump_file)
828 fprintf (symtab->dump_file,
829 "Not privatizing symbol name: %s. Has unique name.\n",
830 name);
831 return true;
833 return false;
836 /* If we are an offload compiler, we may have to rewrite symbols to be
837 valid on this target. Return either PTR or a modified version of it. */
839 static const char *
840 maybe_rewrite_identifier (const char *ptr)
842 #if defined ACCEL_COMPILER && (defined NO_DOT_IN_LABEL || defined NO_DOLLAR_IN_LABEL)
843 #ifndef NO_DOT_IN_LABEL
844 char valid = '.';
845 const char reject[] = "$";
846 #elif !defined NO_DOLLAR_IN_LABEL
847 char valid = '$';
848 const char reject[] = ".";
849 #else
850 char valid = '_';
851 const char reject[] = ".$";
852 #endif
854 char *copy = NULL;
855 const char *match = ptr;
856 for (;;)
858 size_t off = strcspn (match, reject);
859 if (match[off] == '\0')
860 break;
861 if (copy == NULL)
863 copy = xstrdup (ptr);
864 match = copy;
866 copy[off] = valid;
868 return match;
869 #else
870 return ptr;
871 #endif
874 /* Ensure that the symbol in NODE is valid for the target, and if not,
875 rewrite it. */
877 static void
878 validize_symbol_for_target (symtab_node *node)
880 tree decl = node->decl;
881 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
883 if (must_not_rename (node, name))
884 return;
886 const char *name2 = maybe_rewrite_identifier (name);
887 if (name2 != name)
889 symtab->change_decl_assembler_name (decl, get_identifier (name2));
890 if (node->lto_file_data)
891 lto_record_renamed_decl (node->lto_file_data, name,
892 IDENTIFIER_POINTER
893 (DECL_ASSEMBLER_NAME (decl)));
897 /* Helper for privatize_symbol_name. Mangle NODE symbol name
898 represented by DECL. */
900 static bool
901 privatize_symbol_name_1 (symtab_node *node, tree decl)
903 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
905 if (must_not_rename (node, name))
906 return false;
908 name = maybe_rewrite_identifier (name);
909 symtab->change_decl_assembler_name (decl,
910 clone_function_name_1 (name,
911 "lto_priv"));
913 if (node->lto_file_data)
914 lto_record_renamed_decl (node->lto_file_data, name,
915 IDENTIFIER_POINTER
916 (DECL_ASSEMBLER_NAME (decl)));
918 if (symtab->dump_file)
919 fprintf (symtab->dump_file,
920 "Privatizing symbol name: %s -> %s\n",
921 name, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
923 return true;
926 /* Mangle NODE symbol name into a local name.
927 This is necessary to do
928 1) if two or more static vars of same assembler name
929 are merged into single ltrans unit.
930 2) if previously static var was promoted hidden to avoid possible conflict
931 with symbols defined out of the LTO world. */
933 static bool
934 privatize_symbol_name (symtab_node *node)
936 if (!privatize_symbol_name_1 (node, node->decl))
937 return false;
939 /* We could change name which is a target of transparent alias
940 chain of instrumented function name. Fix alias chain if so .*/
941 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
943 tree iname = NULL_TREE;
944 if (cnode->instrumentation_clone)
946 /* If we want to privatize instrumentation clone
947 then we also need to privatize original function. */
948 if (cnode->instrumented_version)
949 privatize_symbol_name (cnode->instrumented_version);
950 else
951 privatize_symbol_name_1 (cnode, cnode->orig_decl);
952 iname = DECL_ASSEMBLER_NAME (cnode->decl);
953 TREE_CHAIN (iname) = DECL_ASSEMBLER_NAME (cnode->orig_decl);
955 else if (cnode->instrumented_version
956 && cnode->instrumented_version->orig_decl == cnode->decl)
958 iname = DECL_ASSEMBLER_NAME (cnode->instrumented_version->decl);
959 TREE_CHAIN (iname) = DECL_ASSEMBLER_NAME (cnode->decl);
963 return true;
966 /* Promote variable VNODE to be static. */
968 static void
969 promote_symbol (symtab_node *node)
971 /* We already promoted ... */
972 if (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
973 && DECL_VISIBILITY_SPECIFIED (node->decl)
974 && TREE_PUBLIC (node->decl))
976 validize_symbol_for_target (node);
977 return;
980 gcc_checking_assert (!TREE_PUBLIC (node->decl)
981 && !DECL_EXTERNAL (node->decl));
982 /* Be sure that newly public symbol does not conflict with anything already
983 defined by the non-LTO part. */
984 privatize_symbol_name (node);
985 TREE_PUBLIC (node->decl) = 1;
986 DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
987 DECL_VISIBILITY_SPECIFIED (node->decl) = true;
988 ipa_ref *ref;
990 /* Promoting a symbol also promotes all trasparent aliases with exception
991 of weakref where the visibility flags are always wrong and set to
992 !PUBLIC. */
993 for (unsigned i = 0; node->iterate_direct_aliases (i, ref); i++)
995 struct symtab_node *alias = ref->referring;
996 if (alias->transparent_alias && !alias->weakref)
998 TREE_PUBLIC (alias->decl) = 1;
999 DECL_VISIBILITY (alias->decl) = VISIBILITY_HIDDEN;
1000 DECL_VISIBILITY_SPECIFIED (alias->decl) = true;
1002 gcc_assert (!alias->weakref || TREE_PUBLIC (alias->decl));
1005 if (symtab->dump_file)
1006 fprintf (symtab->dump_file,
1007 "Promoting as hidden: %s\n", node->name ());
1010 /* Return true if NODE needs named section even if it won't land in the partition
1011 symbol table.
1012 FIXME: we should really not use named sections for inline clones and master
1013 clones. */
1015 static bool
1016 may_need_named_section_p (lto_symtab_encoder_t encoder, symtab_node *node)
1018 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1019 if (!cnode)
1020 return false;
1021 if (node->real_symbol_p ())
1022 return false;
1023 return (!encoder
1024 || (lto_symtab_encoder_lookup (encoder, node) != LCC_NOT_FOUND
1025 && lto_symtab_encoder_encode_body_p (encoder,
1026 cnode)));
1029 /* If NODE represents a static variable. See if there are other variables
1030 of the same name in partition ENCODER (or in whole compilation unit if
1031 ENCODER is NULL) and if so, mangle the statics. Always mangle all
1032 conflicting statics, so we reduce changes of silently miscompiling
1033 asm statements referring to them by symbol name. */
1035 static void
1036 rename_statics (lto_symtab_encoder_t encoder, symtab_node *node)
1038 tree decl = node->decl;
1039 symtab_node *s;
1040 tree name = DECL_ASSEMBLER_NAME (decl);
1042 /* See if this is static symbol. */
1043 if (((node->externally_visible && !node->weakref)
1044 /* FIXME: externally_visible is somewhat illogically not set for
1045 external symbols (i.e. those not defined). Remove this test
1046 once this is fixed. */
1047 || DECL_EXTERNAL (node->decl)
1048 || !node->real_symbol_p ())
1049 && !may_need_named_section_p (encoder, node))
1050 return;
1052 /* Now walk symbols sharing the same name and see if there are any conflicts.
1053 (all types of symbols counts here, since we can not have static of the
1054 same name as external or public symbol.) */
1055 for (s = symtab_node::get_for_asmname (name);
1056 s; s = s->next_sharing_asm_name)
1057 if ((s->real_symbol_p () || may_need_named_section_p (encoder, s))
1058 && s->decl != node->decl
1059 && (!encoder
1060 || lto_symtab_encoder_lookup (encoder, s) != LCC_NOT_FOUND))
1061 break;
1063 /* OK, no confict, so we have nothing to do. */
1064 if (!s)
1065 return;
1067 if (symtab->dump_file)
1068 fprintf (symtab->dump_file,
1069 "Renaming statics with asm name: %s\n", node->name ());
1071 /* Assign every symbol in the set that shares the same ASM name an unique
1072 mangled name. */
1073 for (s = symtab_node::get_for_asmname (name); s;)
1074 if ((!s->externally_visible || s->weakref)
1075 /* Transparent aliases having same name as target are renamed at a
1076 time their target gets new name. Transparent aliases that use
1077 separate assembler name require the name to be unique. */
1078 && (!s->transparent_alias || !s->definition || s->weakref
1079 || !symbol_table::assembler_names_equal_p
1080 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (s->decl)),
1081 IDENTIFIER_POINTER
1082 (DECL_ASSEMBLER_NAME (s->get_alias_target()->decl))))
1083 && ((s->real_symbol_p ()
1084 && !DECL_EXTERNAL (s->decl)
1085 && !TREE_PUBLIC (s->decl))
1086 || may_need_named_section_p (encoder, s))
1087 && (!encoder
1088 || lto_symtab_encoder_lookup (encoder, s) != LCC_NOT_FOUND))
1090 if (privatize_symbol_name (s))
1091 /* Re-start from beginning since we do not know how many symbols changed a name. */
1092 s = symtab_node::get_for_asmname (name);
1093 else s = s->next_sharing_asm_name;
1095 else s = s->next_sharing_asm_name;
1098 /* Find out all static decls that need to be promoted to global because
1099 of cross file sharing. This function must be run in the WPA mode after
1100 all inlinees are added. */
1102 void
1103 lto_promote_cross_file_statics (void)
1105 unsigned i, n_sets;
1107 gcc_assert (flag_wpa);
1109 lto_stream_offload_p = false;
1110 select_what_to_stream ();
1112 /* First compute boundaries. */
1113 n_sets = ltrans_partitions.length ();
1114 for (i = 0; i < n_sets; i++)
1116 ltrans_partition part
1117 = ltrans_partitions[i];
1118 part->encoder = compute_ltrans_boundary (part->encoder);
1121 /* Look at boundaries and promote symbols as needed. */
1122 for (i = 0; i < n_sets; i++)
1124 lto_symtab_encoder_iterator lsei;
1125 lto_symtab_encoder_t encoder = ltrans_partitions[i]->encoder;
1127 for (lsei = lsei_start (encoder); !lsei_end_p (lsei);
1128 lsei_next (&lsei))
1130 symtab_node *node = lsei_node (lsei);
1132 /* If symbol is static, rename it if its assembler name clash with
1133 anything else in this unit. */
1134 rename_statics (encoder, node);
1136 /* No need to promote if symbol already is externally visible ... */
1137 if (node->externally_visible
1138 /* ... or if it is part of current partition ... */
1139 || lto_symtab_encoder_in_partition_p (encoder, node)
1140 /* ... or if we do not partition it. This mean that it will
1141 appear in every partition refernecing it. */
1142 || node->get_partitioning_class () != SYMBOL_PARTITION)
1144 validize_symbol_for_target (node);
1145 continue;
1148 promote_symbol (node);
1153 /* Rename statics in the whole unit in the case that
1154 we do -flto-partition=none. */
1156 void
1157 lto_promote_statics_nonwpa (void)
1159 symtab_node *node;
1160 FOR_EACH_SYMBOL (node)
1162 rename_statics (NULL, node);
1163 validize_symbol_for_target (node);