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