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