]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/lto/lto-partition.c
LTO balanced map: add stats about insns and symbols.
[thirdparty/gcc.git] / gcc / lto / lto-partition.c
1 /* LTO partitioning logic routines.
2 Copyright (C) 2009-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "toplev.h"
24 #include "hash-set.h"
25 #include "machmode.h"
26 #include "vec.h"
27 #include "double-int.h"
28 #include "input.h"
29 #include "alias.h"
30 #include "symtab.h"
31 #include "options.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "predict.h"
37 #include "tm.h"
38 #include "hard-reg-set.h"
39 #include "input.h"
40 #include "function.h"
41 #include "basic-block.h"
42 #include "tree-ssa-alias.h"
43 #include "internal-fn.h"
44 #include "gimple-expr.h"
45 #include "is-a.h"
46 #include "gimple.h"
47 #include "hash-map.h"
48 #include "plugin-api.h"
49 #include "ipa-ref.h"
50 #include "cgraph.h"
51 #include "lto-streamer.h"
52 #include "timevar.h"
53 #include "params.h"
54 #include "alloc-pool.h"
55 #include "symbol-summary.h"
56 #include "ipa-prop.h"
57 #include "ipa-inline.h"
58 #include "ipa-utils.h"
59 #include "lto-partition.h"
60 #include "stringpool.h"
61
62 vec<ltrans_partition> ltrans_partitions;
63
64 static void add_symbol_to_partition (ltrans_partition part, symtab_node *node);
65
66
67 /* Create new partition with name NAME. */
68
69 static ltrans_partition
70 new_partition (const char *name)
71 {
72 ltrans_partition part = XCNEW (struct ltrans_partition_def);
73 part->encoder = lto_symtab_encoder_new (false);
74 part->name = name;
75 part->insns = 0;
76 part->symbols = 0;
77 ltrans_partitions.safe_push (part);
78 return part;
79 }
80
81 /* Free memory used by ltrans datastructures. */
82
83 void
84 free_ltrans_partitions (void)
85 {
86 unsigned int idx;
87 ltrans_partition part;
88 for (idx = 0; ltrans_partitions.iterate (idx, &part); idx++)
89 {
90 if (part->initializers_visited)
91 delete part->initializers_visited;
92 /* Symtab encoder is freed after streaming. */
93 free (part);
94 }
95 ltrans_partitions.release ();
96 }
97
98 /* Return true if symbol is already in some partition. */
99
100 static inline bool
101 symbol_partitioned_p (symtab_node *node)
102 {
103 return node->aux;
104 }
105
106 /* Add references into the partition. */
107 static void
108 add_references_to_partition (ltrans_partition part, symtab_node *node)
109 {
110 int i;
111 struct ipa_ref *ref = NULL;
112
113 /* Add all duplicated references to the partition. */
114 for (i = 0; node->iterate_reference (i, ref); i++)
115 if (ref->referred->get_partitioning_class () == SYMBOL_DUPLICATE)
116 add_symbol_to_partition (part, ref->referred);
117 /* References to a readonly variable may be constant foled into its value.
118 Recursively look into the initializers of the constant variable and add
119 references, too. */
120 else if (is_a <varpool_node *> (ref->referred)
121 && (dyn_cast <varpool_node *> (ref->referred)
122 ->ctor_useable_for_folding_p ()
123 || POINTER_BOUNDS_P (ref->referred->decl))
124 && !lto_symtab_encoder_in_partition_p (part->encoder, ref->referred))
125 {
126 if (!part->initializers_visited)
127 part->initializers_visited = new hash_set<symtab_node *>;
128 if (!part->initializers_visited->add (ref->referred))
129 add_references_to_partition (part, ref->referred);
130 }
131 }
132
133 /* Helper function for add_symbol_to_partition doing the actual dirty work
134 of adding NODE to PART. */
135
136 static bool
137 add_symbol_to_partition_1 (ltrans_partition part, symtab_node *node)
138 {
139 enum symbol_partitioning_class c = node->get_partitioning_class ();
140 struct ipa_ref *ref;
141 symtab_node *node1;
142
143 /* If NODE is already there, we have nothing to do. */
144 if (lto_symtab_encoder_in_partition_p (part->encoder, node))
145 return true;
146
147 /* non-duplicated aliases or tunks of a duplicated symbol needs to be output
148 just once.
149
150 Be lax about comdats; they may or may not be duplicated and we may
151 end up in need to duplicate keyed comdat because it has unkeyed alias. */
152 if (c == SYMBOL_PARTITION && !DECL_COMDAT (node->decl)
153 && symbol_partitioned_p (node))
154 return false;
155
156 /* Be sure that we never try to duplicate partitioned symbol
157 or add external symbol. */
158 gcc_assert (c != SYMBOL_EXTERNAL
159 && (c == SYMBOL_DUPLICATE || !symbol_partitioned_p (node)));
160
161 part->symbols++;
162
163 lto_set_symtab_encoder_in_partition (part->encoder, node);
164
165 if (symbol_partitioned_p (node))
166 {
167 node->in_other_partition = 1;
168 if (symtab->dump_file)
169 fprintf (symtab->dump_file,
170 "Symbol node %s now used in multiple partitions\n",
171 node->name ());
172 }
173 node->aux = (void *)((size_t)node->aux + 1);
174
175 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
176 {
177 struct cgraph_edge *e;
178 if (!node->alias)
179 part->insns += inline_summaries->get (cnode)->self_size;
180
181 /* Add all inline clones and callees that are duplicated. */
182 for (e = cnode->callees; e; e = e->next_callee)
183 if (!e->inline_failed)
184 add_symbol_to_partition_1 (part, e->callee);
185 else if (e->callee->get_partitioning_class () == SYMBOL_DUPLICATE)
186 add_symbol_to_partition (part, e->callee);
187
188 /* Add all thunks associated with the function. */
189 for (e = cnode->callers; e; e = e->next_caller)
190 if (e->caller->thunk.thunk_p)
191 add_symbol_to_partition_1 (part, e->caller);
192
193 /* Instrumented version is actually the same function.
194 Therefore put it into the same partition. */
195 if (cnode->instrumented_version)
196 add_symbol_to_partition_1 (part, cnode->instrumented_version);
197 }
198
199 add_references_to_partition (part, node);
200
201 /* Add all aliases associated with the symbol. */
202
203 FOR_EACH_ALIAS (node, ref)
204 if (!node->weakref)
205 add_symbol_to_partition_1 (part, ref->referring);
206
207 /* Ensure that SAME_COMDAT_GROUP lists all allways added in a group. */
208 if (node->same_comdat_group)
209 for (node1 = node->same_comdat_group;
210 node1 != node; node1 = node1->same_comdat_group)
211 if (!node->alias)
212 {
213 bool added = add_symbol_to_partition_1 (part, node1);
214 gcc_assert (added);
215 }
216 return true;
217 }
218
219 /* If symbol NODE is really part of other symbol's definition (i.e. it is
220 internal label, thunk, alias or so), return the outer symbol.
221 When add_symbol_to_partition_1 is called on the outer symbol it must
222 eventually add NODE, too. */
223 static symtab_node *
224 contained_in_symbol (symtab_node *node)
225 {
226 /* Weakrefs are never contained in anything. */
227 if (node->weakref)
228 return node;
229 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
230 {
231 cnode = cnode->function_symbol ();
232 if (cnode->global.inlined_to)
233 cnode = cnode->global.inlined_to;
234 return cnode;
235 }
236 else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
237 return vnode->ultimate_alias_target ();
238 return node;
239 }
240
241 /* Add symbol NODE to partition. When definition of NODE is part
242 of other symbol definition, add the other symbol, too. */
243
244 static void
245 add_symbol_to_partition (ltrans_partition part, symtab_node *node)
246 {
247 symtab_node *node1;
248
249 /* Verify that we do not try to duplicate something that can not be. */
250 gcc_checking_assert (node->get_partitioning_class () == SYMBOL_DUPLICATE
251 || !symbol_partitioned_p (node));
252
253 while ((node1 = contained_in_symbol (node)) != node)
254 node = node1;
255
256 /* If we have duplicated symbol contained in something we can not duplicate,
257 we are very badly screwed. The other way is possible, so we do not
258 assert this in add_symbol_to_partition_1.
259
260 Be lax about comdats; they may or may not be duplicated and we may
261 end up in need to duplicate keyed comdat because it has unkeyed alias. */
262
263 gcc_assert (node->get_partitioning_class () == SYMBOL_DUPLICATE
264 || DECL_COMDAT (node->decl)
265 || !symbol_partitioned_p (node));
266
267 add_symbol_to_partition_1 (part, node);
268 }
269
270 /* Undo all additions until number of cgraph nodes in PARITION is N_CGRAPH_NODES
271 and number of varpool nodes is N_VARPOOL_NODES. */
272
273 static void
274 undo_partition (ltrans_partition partition, unsigned int n_nodes)
275 {
276 while (lto_symtab_encoder_size (partition->encoder) > (int)n_nodes)
277 {
278 symtab_node *node = lto_symtab_encoder_deref (partition->encoder,
279 n_nodes);
280 partition->symbols--;
281 cgraph_node *cnode;
282
283 /* After UNDO we no longer know what was visited. */
284 if (partition->initializers_visited)
285 delete partition->initializers_visited;
286 partition->initializers_visited = NULL;
287
288 if (!node->alias && (cnode = dyn_cast <cgraph_node *> (node)))
289 partition->insns -= inline_summaries->get (cnode)->self_size;
290 lto_symtab_encoder_delete_node (partition->encoder, node);
291 node->aux = (void *)((size_t)node->aux - 1);
292 }
293 }
294
295 /* Group cgrah nodes by input files. This is used mainly for testing
296 right now. */
297
298 void
299 lto_1_to_1_map (void)
300 {
301 symtab_node *node;
302 struct lto_file_decl_data *file_data;
303 hash_map<lto_file_decl_data *, ltrans_partition> pmap;
304 ltrans_partition partition;
305 int npartitions = 0;
306
307 FOR_EACH_SYMBOL (node)
308 {
309 if (node->get_partitioning_class () != SYMBOL_PARTITION
310 || symbol_partitioned_p (node))
311 continue;
312
313 file_data = node->lto_file_data;
314
315 if (file_data)
316 {
317 ltrans_partition *slot = &pmap.get_or_insert (file_data);
318 if (*slot)
319 partition = *slot;
320 else
321 {
322 partition = new_partition (file_data->file_name);
323 *slot = partition;
324 npartitions++;
325 }
326 }
327 else if (!file_data && ltrans_partitions.length ())
328 partition = ltrans_partitions[0];
329 else
330 {
331 partition = new_partition ("");
332 pmap.put (NULL, partition);
333 npartitions++;
334 }
335
336 add_symbol_to_partition (partition, node);
337 }
338
339 /* If the cgraph is empty, create one cgraph node set so that there is still
340 an output file for any variables that need to be exported in a DSO. */
341 if (!npartitions)
342 new_partition ("empty");
343
344 }
345
346 /* Maximal partitioning. Put every new symbol into new partition if possible. */
347
348 void
349 lto_max_map (void)
350 {
351 symtab_node *node;
352 ltrans_partition partition;
353 int npartitions = 0;
354
355 FOR_EACH_SYMBOL (node)
356 {
357 if (node->get_partitioning_class () != SYMBOL_PARTITION
358 || symbol_partitioned_p (node))
359 continue;
360 partition = new_partition (node->asm_name ());
361 add_symbol_to_partition (partition, node);
362 npartitions++;
363 }
364 if (!npartitions)
365 new_partition ("empty");
366 }
367
368 /* Helper function for qsort; sort nodes by order. noreorder functions must have
369 been removed earlier. */
370 static int
371 node_cmp (const void *pa, const void *pb)
372 {
373 const struct cgraph_node *a = *(const struct cgraph_node * const *) pa;
374 const struct cgraph_node *b = *(const struct cgraph_node * const *) pb;
375
376 /* Profile reorder flag enables function reordering based on first execution
377 of a function. All functions with profile are placed in ascending
378 order at the beginning. */
379
380 if (flag_profile_reorder_functions)
381 {
382 /* Functions with time profile are sorted in ascending order. */
383 if (a->tp_first_run && b->tp_first_run)
384 return a->tp_first_run != b->tp_first_run
385 ? a->tp_first_run - b->tp_first_run
386 : a->order - b->order;
387
388 /* Functions with time profile are sorted before the functions
389 that do not have the profile. */
390 if (a->tp_first_run || b->tp_first_run)
391 return b->tp_first_run - a->tp_first_run;
392 }
393
394 return b->order - a->order;
395 }
396
397 /* Helper function for qsort; sort nodes by order. */
398 static int
399 varpool_node_cmp (const void *pa, const void *pb)
400 {
401 const symtab_node *a = *static_cast<const symtab_node * const *> (pa);
402 const symtab_node *b = *static_cast<const symtab_node * const *> (pb);
403 return b->order - a->order;
404 }
405
406 /* Add all symtab nodes from NEXT_NODE to PARTITION in order. */
407
408 static void
409 add_sorted_nodes (vec<symtab_node *> &next_nodes, ltrans_partition partition)
410 {
411 unsigned i;
412 symtab_node *node;
413
414 next_nodes.qsort (varpool_node_cmp);
415 FOR_EACH_VEC_ELT (next_nodes, i, node)
416 if (!symbol_partitioned_p (node))
417 add_symbol_to_partition (partition, node);
418 }
419
420
421 /* Group cgraph nodes into equally-sized partitions.
422
423 The partitioning algorithm is simple: nodes are taken in predefined order.
424 The order corresponds to the order we want functions to have in the final
425 output. In the future this will be given by function reordering pass, but
426 at the moment we use the topological order, which is a good approximation.
427
428 The goal is to partition this linear order into intervals (partitions) so
429 that all the partitions have approximately the same size and the number of
430 callgraph or IPA reference edges crossing boundaries is minimal.
431
432 This is a lot faster (O(n) in size of callgraph) than algorithms doing
433 priority-based graph clustering that are generally O(n^2) and, since
434 WHOPR is designed to make things go well across partitions, it leads
435 to good results.
436
437 We compute the expected size of a partition as:
438
439 max (total_size / lto_partitions, min_partition_size)
440
441 We use dynamic expected size of partition so small programs are partitioned
442 into enough partitions to allow use of multiple CPUs, while large programs
443 are not partitioned too much. Creating too many partitions significantly
444 increases the streaming overhead.
445
446 In the future, we would like to bound the maximal size of partitions so as
447 to prevent the LTRANS stage from consuming too much memory. At the moment,
448 however, the WPA stage is the most memory intensive for large benchmarks,
449 since too many types and declarations are read into memory.
450
451 The function implements a simple greedy algorithm. Nodes are being added
452 to the current partition until after 3/4 of the expected partition size is
453 reached. Past this threshold, we keep track of boundary size (number of
454 edges going to other partitions) and continue adding functions until after
455 the current partition has grown to twice the expected partition size. Then
456 the process is undone to the point where the minimal ratio of boundary size
457 and in-partition calls was reached. */
458
459 void
460 lto_balanced_map (int n_lto_partitions)
461 {
462 int n_nodes = 0;
463 int n_varpool_nodes = 0, varpool_pos = 0, best_varpool_pos = 0;
464 struct cgraph_node **order = XNEWVEC (cgraph_node *, symtab->cgraph_max_uid);
465 auto_vec<cgraph_node *> noreorder;
466 auto_vec<varpool_node *> varpool_order;
467 int i;
468 struct cgraph_node *node;
469 int original_total_size, total_size = 0, best_total_size = 0;
470 int partition_size;
471 ltrans_partition partition;
472 int last_visited_node = 0;
473 varpool_node *vnode;
474 int cost = 0, internal = 0;
475 int best_n_nodes = 0, best_i = 0, best_cost =
476 INT_MAX, best_internal = 0;
477 int npartitions;
478 int current_order = -1;
479 int noreorder_pos = 0;
480
481 FOR_EACH_VARIABLE (vnode)
482 gcc_assert (!vnode->aux);
483
484 FOR_EACH_DEFINED_FUNCTION (node)
485 if (node->get_partitioning_class () == SYMBOL_PARTITION)
486 {
487 if (node->no_reorder)
488 noreorder.safe_push (node);
489 else
490 order[n_nodes++] = node;
491 if (!node->alias)
492 total_size += inline_summaries->get (node)->size;
493 }
494
495 original_total_size = total_size;
496
497 /* Streaming works best when the source units do not cross partition
498 boundaries much. This is because importing function from a source
499 unit tends to import a lot of global trees defined there. We should
500 get better about minimizing the function bounday, but until that
501 things works smoother if we order in source order. */
502 qsort (order, n_nodes, sizeof (struct cgraph_node *), node_cmp);
503 noreorder.qsort (node_cmp);
504
505 if (symtab->dump_file)
506 {
507 for(i = 0; i < n_nodes; i++)
508 fprintf (symtab->dump_file, "Balanced map symbol order:%s:%u\n",
509 order[i]->name (), order[i]->tp_first_run);
510 for(i = 0; i < (int)noreorder.length(); i++)
511 fprintf (symtab->dump_file, "Balanced map symbol no_reorder:%s:%u\n",
512 noreorder[i]->name (), noreorder[i]->tp_first_run);
513 }
514
515 /* Collect all variables that should not be reordered. */
516 FOR_EACH_VARIABLE (vnode)
517 if (vnode->get_partitioning_class () == SYMBOL_PARTITION
518 && (!flag_toplevel_reorder || vnode->no_reorder))
519 varpool_order.safe_push (vnode);
520 n_varpool_nodes = varpool_order.length ();
521 varpool_order.qsort (varpool_node_cmp);
522
523 /* Compute partition size and create the first partition. */
524 partition_size = total_size / n_lto_partitions;
525 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
526 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
527 npartitions = 1;
528 partition = new_partition ("");
529 if (symtab->dump_file)
530 fprintf (symtab->dump_file, "Total unit size: %i, partition size: %i\n",
531 total_size, partition_size);
532
533 auto_vec<symtab_node *> next_nodes;
534
535 for (i = 0; i < n_nodes; i++)
536 {
537 if (symbol_partitioned_p (order[i]))
538 continue;
539
540 current_order = order[i]->order;
541
542 /* Output noreorder and varpool in program order first. */
543 next_nodes.truncate (0);
544 while (varpool_pos < n_varpool_nodes
545 && varpool_order[varpool_pos]->order < current_order)
546 next_nodes.safe_push (varpool_order[varpool_pos++]);
547 while (noreorder_pos < (int)noreorder.length ()
548 && noreorder[noreorder_pos]->order < current_order)
549 {
550 if (!noreorder[noreorder_pos]->alias)
551 total_size -= inline_summaries->get (noreorder[noreorder_pos])->size;
552 next_nodes.safe_push (noreorder[noreorder_pos++]);
553 }
554 add_sorted_nodes (next_nodes, partition);
555
556 add_symbol_to_partition (partition, order[i]);
557 if (!order[i]->alias)
558 total_size -= inline_summaries->get (order[i])->size;
559
560
561 /* Once we added a new node to the partition, we also want to add
562 all referenced variables unless they was already added into some
563 earlier partition.
564 add_symbol_to_partition adds possibly multiple nodes and
565 variables that are needed to satisfy needs of ORDER[i].
566 We remember last visited cgraph and varpool node from last iteration
567 of outer loop that allows us to process every new addition.
568
569 At the same time we compute size of the boundary into COST. Every
570 callgraph or IPA reference edge leaving the partition contributes into
571 COST. Every edge inside partition was earlier computed as one leaving
572 it and thus we need to subtract it from COST. */
573 while (last_visited_node < lto_symtab_encoder_size (partition->encoder))
574 {
575 symtab_node *refs_node;
576 int j;
577 struct ipa_ref *ref = NULL;
578 symtab_node *snode = lto_symtab_encoder_deref (partition->encoder,
579 last_visited_node);
580
581 if (cgraph_node *node = dyn_cast <cgraph_node *> (snode))
582 {
583 struct cgraph_edge *edge;
584
585 refs_node = node;
586
587 last_visited_node++;
588
589 gcc_assert (node->definition || node->weakref);
590
591 /* Compute boundary cost of callgraph edges. */
592 for (edge = node->callees; edge; edge = edge->next_callee)
593 if (edge->callee->definition)
594 {
595 int edge_cost = edge->frequency;
596 int index;
597
598 if (!edge_cost)
599 edge_cost = 1;
600 gcc_assert (edge_cost > 0);
601 index = lto_symtab_encoder_lookup (partition->encoder,
602 edge->callee);
603 if (index != LCC_NOT_FOUND
604 && index < last_visited_node - 1)
605 cost -= edge_cost, internal += edge_cost;
606 else
607 cost += edge_cost;
608 }
609 for (edge = node->callers; edge; edge = edge->next_caller)
610 {
611 int edge_cost = edge->frequency;
612 int index;
613
614 gcc_assert (edge->caller->definition);
615 if (!edge_cost)
616 edge_cost = 1;
617 gcc_assert (edge_cost > 0);
618 index = lto_symtab_encoder_lookup (partition->encoder,
619 edge->caller);
620 if (index != LCC_NOT_FOUND
621 && index < last_visited_node - 1)
622 cost -= edge_cost;
623 else
624 cost += edge_cost;
625 }
626 }
627 else
628 {
629 refs_node = snode;
630 last_visited_node++;
631 }
632
633 /* Compute boundary cost of IPA REF edges and at the same time look into
634 variables referenced from current partition and try to add them. */
635 for (j = 0; refs_node->iterate_reference (j, ref); j++)
636 if (is_a <varpool_node *> (ref->referred))
637 {
638 int index;
639
640 vnode = dyn_cast <varpool_node *> (ref->referred);
641 if (!vnode->definition)
642 continue;
643 if (!symbol_partitioned_p (vnode) && flag_toplevel_reorder
644 && !vnode->no_reorder
645 && vnode->get_partitioning_class () == SYMBOL_PARTITION)
646 add_symbol_to_partition (partition, vnode);
647 index = lto_symtab_encoder_lookup (partition->encoder,
648 vnode);
649 if (index != LCC_NOT_FOUND
650 && index < last_visited_node - 1)
651 cost--, internal++;
652 else
653 cost++;
654 }
655 else
656 {
657 int index;
658
659 node = dyn_cast <cgraph_node *> (ref->referred);
660 if (!node->definition)
661 continue;
662 index = lto_symtab_encoder_lookup (partition->encoder,
663 node);
664 if (index != LCC_NOT_FOUND
665 && index < last_visited_node - 1)
666 cost--, internal++;
667 else
668 cost++;
669 }
670 for (j = 0; refs_node->iterate_referring (j, ref); j++)
671 if (is_a <varpool_node *> (ref->referring))
672 {
673 int index;
674
675 vnode = dyn_cast <varpool_node *> (ref->referring);
676 gcc_assert (vnode->definition);
677 /* It is better to couple variables with their users, because it allows them
678 to be removed. Coupling with objects they refer to only helps to reduce
679 number of symbols promoted to hidden. */
680 if (!symbol_partitioned_p (vnode) && flag_toplevel_reorder
681 && !vnode->no_reorder
682 && !vnode->can_remove_if_no_refs_p ()
683 && vnode->get_partitioning_class () == SYMBOL_PARTITION)
684 add_symbol_to_partition (partition, vnode);
685 index = lto_symtab_encoder_lookup (partition->encoder,
686 vnode);
687 if (index != LCC_NOT_FOUND
688 && index < last_visited_node - 1)
689 cost--;
690 else
691 cost++;
692 }
693 else
694 {
695 int index;
696
697 node = dyn_cast <cgraph_node *> (ref->referring);
698 gcc_assert (node->definition);
699 index = lto_symtab_encoder_lookup (partition->encoder,
700 node);
701 if (index != LCC_NOT_FOUND
702 && index < last_visited_node - 1)
703 cost--;
704 else
705 cost++;
706 }
707 }
708
709 /* If the partition is large enough, start looking for smallest boundary cost. */
710 if (partition->insns < partition_size * 3 / 4
711 || best_cost == INT_MAX
712 || ((!cost
713 || (best_internal * (HOST_WIDE_INT) cost
714 > (internal * (HOST_WIDE_INT)best_cost)))
715 && partition->insns < partition_size * 5 / 4))
716 {
717 best_cost = cost;
718 best_internal = internal;
719 best_i = i;
720 best_n_nodes = lto_symtab_encoder_size (partition->encoder);
721 best_total_size = total_size;
722 best_varpool_pos = varpool_pos;
723 }
724 if (symtab->dump_file)
725 fprintf (symtab->dump_file, "Step %i: added %s/%i, size %i, cost %i/%i "
726 "best %i/%i, step %i\n", i,
727 order[i]->name (), order[i]->order,
728 partition->insns, cost, internal,
729 best_cost, best_internal, best_i);
730 /* Partition is too large, unwind into step when best cost was reached and
731 start new partition. */
732 if (partition->insns > 2 * partition_size)
733 {
734 if (best_i != i)
735 {
736 if (symtab->dump_file)
737 fprintf (symtab->dump_file, "Unwinding %i insertions to step %i\n",
738 i - best_i, best_i);
739 undo_partition (partition, best_n_nodes);
740 varpool_pos = best_varpool_pos;
741 }
742 i = best_i;
743 /* When we are finished, avoid creating empty partition. */
744 while (i < n_nodes - 1 && symbol_partitioned_p (order[i + 1]))
745 i++;
746 if (i == n_nodes - 1)
747 break;
748 partition = new_partition ("");
749 last_visited_node = 0;
750 total_size = best_total_size;
751 cost = 0;
752
753 if (symtab->dump_file)
754 fprintf (symtab->dump_file, "New partition\n");
755 best_n_nodes = 0;
756 best_cost = INT_MAX;
757
758 /* Since the size of partitions is just approximate, update the size after
759 we finished current one. */
760 if (npartitions < n_lto_partitions)
761 partition_size = total_size / (n_lto_partitions - npartitions);
762 else
763 partition_size = INT_MAX;
764
765 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
766 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
767 npartitions ++;
768 }
769 }
770
771 next_nodes.truncate (0);
772
773 /* Varables that are not reachable from the code go into last partition. */
774 if (flag_toplevel_reorder)
775 {
776 FOR_EACH_VARIABLE (vnode)
777 if (vnode->get_partitioning_class () == SYMBOL_PARTITION
778 && !symbol_partitioned_p (vnode)
779 && !vnode->no_reorder)
780 next_nodes.safe_push (vnode);
781 }
782
783 /* Output remaining ordered symbols. */
784 while (varpool_pos < n_varpool_nodes)
785 next_nodes.safe_push (varpool_order[varpool_pos++]);
786 while (noreorder_pos < (int)noreorder.length ())
787 next_nodes.safe_push (noreorder[noreorder_pos++]);
788 add_sorted_nodes (next_nodes, partition);
789
790 free (order);
791
792 if (symtab->dump_file)
793 {
794 fprintf (symtab->dump_file, "\nPartition sizes:\n");
795 unsigned partitions = ltrans_partitions.length ();
796
797 for (unsigned i = 0; i < partitions ; i++)
798 {
799 ltrans_partition p = ltrans_partitions[i];
800 fprintf (symtab->dump_file, "partition %d contains %d (%2.2f%%)"
801 " symbols and %d (%2.2f%%) insns\n", i, p->symbols,
802 100.0 * p->symbols / n_nodes, p->insns,
803 100.0 * p->insns / original_total_size);
804 }
805
806 fprintf (symtab->dump_file, "\n");
807 }
808 }
809
810 /* Return true if we must not change the name of the NODE. The name as
811 extracted from the corresponding decl should be passed in NAME. */
812
813 static bool
814 must_not_rename (symtab_node *node, const char *name)
815 {
816 /* Our renaming machinery do not handle more than one change of assembler name.
817 We should not need more than one anyway. */
818 if (node->lto_file_data
819 && lto_get_decl_name_mapping (node->lto_file_data, name) != name)
820 {
821 if (symtab->dump_file)
822 fprintf (symtab->dump_file,
823 "Not privatizing symbol name: %s. It privatized already.\n",
824 name);
825 return true;
826 }
827 /* Avoid mangling of already mangled clones.
828 ??? should have a flag whether a symbol has a 'private' name already,
829 since we produce some symbols like that i.e. for global constructors
830 that are not really clones. */
831 if (node->unique_name)
832 {
833 if (symtab->dump_file)
834 fprintf (symtab->dump_file,
835 "Not privatizing symbol name: %s. Has unique name.\n",
836 name);
837 return true;
838 }
839 return false;
840 }
841
842 /* If we are an offload compiler, we may have to rewrite symbols to be
843 valid on this target. Return either PTR or a modified version of it. */
844
845 static const char *
846 maybe_rewrite_identifier (const char *ptr)
847 {
848 #if defined ACCEL_COMPILER && (defined NO_DOT_IN_LABEL || defined NO_DOLLAR_IN_LABEL)
849 #ifndef NO_DOT_IN_LABEL
850 char valid = '.';
851 const char reject[] = "$";
852 #elif !defined NO_DOLLAR_IN_LABEL
853 char valid = '$';
854 const char reject[] = ".";
855 #else
856 char valid = '_';
857 const char reject[] = ".$";
858 #endif
859
860 char *copy = NULL;
861 const char *match = ptr;
862 for (;;)
863 {
864 size_t off = strcspn (match, reject);
865 if (match[off] == '\0')
866 break;
867 if (copy == NULL)
868 {
869 copy = xstrdup (ptr);
870 match = copy;
871 }
872 copy[off] = valid;
873 }
874 return match;
875 #else
876 return ptr;
877 #endif
878 }
879
880 /* Ensure that the symbol in NODE is valid for the target, and if not,
881 rewrite it. */
882
883 static void
884 validize_symbol_for_target (symtab_node *node)
885 {
886 tree decl = node->decl;
887 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
888
889 if (must_not_rename (node, name))
890 return;
891
892 const char *name2 = maybe_rewrite_identifier (name);
893 if (name2 != name)
894 {
895 symtab->change_decl_assembler_name (decl, get_identifier (name2));
896 if (node->lto_file_data)
897 lto_record_renamed_decl (node->lto_file_data, name,
898 IDENTIFIER_POINTER
899 (DECL_ASSEMBLER_NAME (decl)));
900 }
901 }
902
903 /* Mangle NODE symbol name into a local name.
904 This is necessary to do
905 1) if two or more static vars of same assembler name
906 are merged into single ltrans unit.
907 2) if previously static var was promoted hidden to avoid possible conflict
908 with symbols defined out of the LTO world. */
909
910 static bool
911 privatize_symbol_name (symtab_node *node)
912 {
913 tree decl = node->decl;
914 const char *name;
915 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
916
917 /* If we want to privatize instrumentation clone
918 then we need to change original function name
919 which is used via transparent alias chain. */
920 if (cnode && cnode->instrumentation_clone)
921 decl = cnode->orig_decl;
922
923 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
924
925 if (must_not_rename (node, name))
926 return false;
927
928 name = maybe_rewrite_identifier (name);
929 symtab->change_decl_assembler_name (decl,
930 clone_function_name_1 (name,
931 "lto_priv"));
932 if (node->lto_file_data)
933 lto_record_renamed_decl (node->lto_file_data, name,
934 IDENTIFIER_POINTER
935 (DECL_ASSEMBLER_NAME (decl)));
936 /* We could change name which is a target of transparent alias
937 chain of instrumented function name. Fix alias chain if so .*/
938 if (cnode)
939 {
940 tree iname = NULL_TREE;
941 if (cnode->instrumentation_clone)
942 iname = DECL_ASSEMBLER_NAME (cnode->decl);
943 else if (cnode->instrumented_version
944 && cnode->instrumented_version->orig_decl == decl)
945 iname = DECL_ASSEMBLER_NAME (cnode->instrumented_version->decl);
946
947 if (iname)
948 {
949 gcc_assert (IDENTIFIER_TRANSPARENT_ALIAS (iname));
950 TREE_CHAIN (iname) = DECL_ASSEMBLER_NAME (decl);
951 }
952 }
953 if (symtab->dump_file)
954 fprintf (symtab->dump_file,
955 "Privatizing symbol name: %s -> %s\n",
956 name, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
957 return true;
958 }
959
960 /* Promote variable VNODE to be static. */
961
962 static void
963 promote_symbol (symtab_node *node)
964 {
965 /* We already promoted ... */
966 if (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
967 && DECL_VISIBILITY_SPECIFIED (node->decl)
968 && TREE_PUBLIC (node->decl))
969 {
970 validize_symbol_for_target (node);
971 return;
972 }
973
974 gcc_checking_assert (!TREE_PUBLIC (node->decl)
975 && !DECL_EXTERNAL (node->decl));
976 /* Be sure that newly public symbol does not conflict with anything already
977 defined by the non-LTO part. */
978 privatize_symbol_name (node);
979 TREE_PUBLIC (node->decl) = 1;
980 DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
981 DECL_VISIBILITY_SPECIFIED (node->decl) = true;
982 if (symtab->dump_file)
983 fprintf (symtab->dump_file,
984 "Promoting as hidden: %s\n", node->name ());
985 }
986
987 /* Return true if NODE needs named section even if it won't land in the partition
988 symbol table.
989 FIXME: we should really not use named sections for inline clones and master clones. */
990
991 static bool
992 may_need_named_section_p (lto_symtab_encoder_t encoder, symtab_node *node)
993 {
994 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
995 if (!cnode)
996 return false;
997 if (node->real_symbol_p ())
998 return false;
999 return (!encoder
1000 || (lto_symtab_encoder_lookup (encoder, node) != LCC_NOT_FOUND
1001 && lto_symtab_encoder_encode_body_p (encoder,
1002 cnode)));
1003 }
1004
1005 /* If NODE represents a static variable. See if there are other variables
1006 of the same name in partition ENCODER (or in whole compilation unit if
1007 ENCODER is NULL) and if so, mangle the statics. Always mangle all
1008 conflicting statics, so we reduce changes of silently miscompiling
1009 asm statements referring to them by symbol name. */
1010
1011 static void
1012 rename_statics (lto_symtab_encoder_t encoder, symtab_node *node)
1013 {
1014 tree decl = node->decl;
1015 symtab_node *s;
1016 tree name = DECL_ASSEMBLER_NAME (decl);
1017
1018 /* See if this is static symbol. */
1019 if ((node->externally_visible
1020 /* FIXME: externally_visible is somewhat illogically not set for
1021 external symbols (i.e. those not defined). Remove this test
1022 once this is fixed. */
1023 || DECL_EXTERNAL (node->decl)
1024 || !node->real_symbol_p ())
1025 && !may_need_named_section_p (encoder, node))
1026 return;
1027
1028 /* Now walk symbols sharing the same name and see if there are any conflicts.
1029 (all types of symbols counts here, since we can not have static of the
1030 same name as external or public symbol.) */
1031 for (s = symtab_node::get_for_asmname (name);
1032 s; s = s->next_sharing_asm_name)
1033 if ((s->real_symbol_p () || may_need_named_section_p (encoder, s))
1034 && s->decl != node->decl
1035 && (!encoder
1036 || lto_symtab_encoder_lookup (encoder, s) != LCC_NOT_FOUND))
1037 break;
1038
1039 /* OK, no confict, so we have nothing to do. */
1040 if (!s)
1041 return;
1042
1043 if (symtab->dump_file)
1044 fprintf (symtab->dump_file,
1045 "Renaming statics with asm name: %s\n", node->name ());
1046
1047 /* Assign every symbol in the set that shares the same ASM name an unique
1048 mangled name. */
1049 for (s = symtab_node::get_for_asmname (name); s;)
1050 if (!s->externally_visible
1051 && ((s->real_symbol_p ()
1052 && !DECL_EXTERNAL (node->decl)
1053 && !TREE_PUBLIC (node->decl))
1054 || may_need_named_section_p (encoder, s))
1055 && (!encoder
1056 || lto_symtab_encoder_lookup (encoder, s) != LCC_NOT_FOUND))
1057 {
1058 if (privatize_symbol_name (s))
1059 /* Re-start from beginning since we do not know how many symbols changed a name. */
1060 s = symtab_node::get_for_asmname (name);
1061 else s = s->next_sharing_asm_name;
1062 }
1063 else s = s->next_sharing_asm_name;
1064 }
1065
1066 /* Find out all static decls that need to be promoted to global because
1067 of cross file sharing. This function must be run in the WPA mode after
1068 all inlinees are added. */
1069
1070 void
1071 lto_promote_cross_file_statics (void)
1072 {
1073 unsigned i, n_sets;
1074
1075 gcc_assert (flag_wpa);
1076
1077 lto_stream_offload_p = false;
1078 select_what_to_stream ();
1079
1080 /* First compute boundaries. */
1081 n_sets = ltrans_partitions.length ();
1082 for (i = 0; i < n_sets; i++)
1083 {
1084 ltrans_partition part
1085 = ltrans_partitions[i];
1086 part->encoder = compute_ltrans_boundary (part->encoder);
1087 }
1088
1089 /* Look at boundaries and promote symbols as needed. */
1090 for (i = 0; i < n_sets; i++)
1091 {
1092 lto_symtab_encoder_iterator lsei;
1093 lto_symtab_encoder_t encoder = ltrans_partitions[i]->encoder;
1094
1095 for (lsei = lsei_start (encoder); !lsei_end_p (lsei);
1096 lsei_next (&lsei))
1097 {
1098 symtab_node *node = lsei_node (lsei);
1099
1100 /* If symbol is static, rename it if its assembler name clash with
1101 anything else in this unit. */
1102 rename_statics (encoder, node);
1103
1104 /* No need to promote if symbol already is externally visible ... */
1105 if (node->externally_visible
1106 /* ... or if it is part of current partition ... */
1107 || lto_symtab_encoder_in_partition_p (encoder, node)
1108 /* ... or if we do not partition it. This mean that it will
1109 appear in every partition refernecing it. */
1110 || node->get_partitioning_class () != SYMBOL_PARTITION)
1111 {
1112 validize_symbol_for_target (node);
1113 continue;
1114 }
1115
1116 promote_symbol (node);
1117 }
1118 }
1119 }
1120
1121 /* Rename statics in the whole unit in the case that
1122 we do -flto-partition=none. */
1123
1124 void
1125 lto_promote_statics_nonwpa (void)
1126 {
1127 symtab_node *node;
1128 FOR_EACH_SYMBOL (node)
1129 {
1130 rename_statics (NULL, node);
1131 validize_symbol_for_target (node);
1132 }
1133 }