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