lto-cgraph.c (get_alias_symbol): Remove weakref sanity check.
[gcc.git] / gcc / cgraphclones.c
1 /* Callgraph clones
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 /* This module provide facilities for clonning functions. I.e. creating
22 new functions based on existing functions with simple modifications,
23 such as replacement of parameters.
24
25 To allow whole program optimization without actual presence of function
26 bodies, an additional infrastructure is provided for so-called virtual
27 clones
28
29 A virtual clone in the callgraph is a function that has no
30 associated body, just a description of how to create its body based
31 on a different function (which itself may be a virtual clone).
32
33 The description of function modifications includes adjustments to
34 the function's signature (which allows, for example, removing or
35 adding function arguments), substitutions to perform on the
36 function body, and, for inlined functions, a pointer to the
37 function that it will be inlined into.
38
39 It is also possible to redirect any edge of the callgraph from a
40 function to its virtual clone. This implies updating of the call
41 site to adjust for the new function signature.
42
43 Most of the transformations performed by inter-procedural
44 optimizations can be represented via virtual clones. For
45 instance, a constant propagation pass can produce a virtual clone
46 of the function which replaces one of its arguments by a
47 constant. The inliner can represent its decisions by producing a
48 clone of a function whose body will be later integrated into
49 a given function.
50
51 Using virtual clones, the program can be easily updated
52 during the Execute stage, solving most of pass interactions
53 problems that would otherwise occur during Transform.
54
55 Virtual clones are later materialized in the LTRANS stage and
56 turned into real functions. Passes executed after the virtual
57 clone were introduced also perform their Transform stage
58 on new functions, so for a pass there is no significant
59 difference between operating on a real function or a virtual
60 clone introduced before its Execute stage.
61
62 Optimization passes then work on virtual clones introduced before
63 their Execute stage as if they were real functions. The
64 only difference is that clones are not visible during the
65 Generate Summary stage. */
66
67 #include "config.h"
68 #include "system.h"
69 #include "coretypes.h"
70 #include "tm.h"
71 #include "tree.h"
72 #include "rtl.h"
73 #include "tree-flow.h"
74 #include "tree-inline.h"
75 #include "langhooks.h"
76 #include "pointer-set.h"
77 #include "toplev.h"
78 #include "flags.h"
79 #include "ggc.h"
80 #include "debug.h"
81 #include "target.h"
82 #include "cgraph.h"
83 #include "diagnostic.h"
84 #include "params.h"
85 #include "intl.h"
86 #include "function.h"
87 #include "ipa-prop.h"
88 #include "gimple.h"
89 #include "tree-iterator.h"
90 #include "tree-dump.h"
91 #include "gimple-pretty-print.h"
92 #include "coverage.h"
93 #include "ipa-inline.h"
94 #include "ipa-utils.h"
95 #include "lto-streamer.h"
96 #include "except.h"
97
98 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
99 struct cgraph_edge *
100 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
101 gimple call_stmt, unsigned stmt_uid, gcov_type count_scale,
102 int freq_scale, bool update_original)
103 {
104 struct cgraph_edge *new_edge;
105 gcov_type count = apply_probability (e->count, count_scale);
106 gcov_type freq;
107
108 /* We do not want to ignore loop nest after frequency drops to 0. */
109 if (!freq_scale)
110 freq_scale = 1;
111 freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
112 if (freq > CGRAPH_FREQ_MAX)
113 freq = CGRAPH_FREQ_MAX;
114
115 if (e->indirect_unknown_callee)
116 {
117 tree decl;
118
119 if (call_stmt && (decl = gimple_call_fndecl (call_stmt)))
120 {
121 struct cgraph_node *callee = cgraph_get_node (decl);
122 gcc_checking_assert (callee);
123 new_edge = cgraph_create_edge (n, callee, call_stmt, count, freq);
124 }
125 else
126 {
127 new_edge = cgraph_create_indirect_edge (n, call_stmt,
128 e->indirect_info->ecf_flags,
129 count, freq);
130 *new_edge->indirect_info = *e->indirect_info;
131 }
132 }
133 else
134 {
135 new_edge = cgraph_create_edge (n, e->callee, call_stmt, count, freq);
136 if (e->indirect_info)
137 {
138 new_edge->indirect_info
139 = ggc_alloc_cleared_cgraph_indirect_call_info ();
140 *new_edge->indirect_info = *e->indirect_info;
141 }
142 }
143
144 new_edge->inline_failed = e->inline_failed;
145 new_edge->indirect_inlining_edge = e->indirect_inlining_edge;
146 new_edge->lto_stmt_uid = stmt_uid;
147 /* Clone flags that depend on call_stmt availability manually. */
148 new_edge->can_throw_external = e->can_throw_external;
149 new_edge->call_stmt_cannot_inline_p = e->call_stmt_cannot_inline_p;
150 if (update_original)
151 {
152 e->count -= new_edge->count;
153 if (e->count < 0)
154 e->count = 0;
155 }
156 cgraph_call_edge_duplication_hooks (e, new_edge);
157 return new_edge;
158 }
159
160
161 /* Create node representing clone of N executed COUNT times. Decrease
162 the execution counts from original node too.
163 The new clone will have decl set to DECL that may or may not be the same
164 as decl of N.
165
166 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
167 function's profile to reflect the fact that part of execution is handled
168 by node.
169 When CALL_DUPLICATOIN_HOOK is true, the ipa passes are acknowledged about
170 the new clone. Otherwise the caller is responsible for doing so later. */
171
172 struct cgraph_node *
173 cgraph_clone_node (struct cgraph_node *n, tree decl, gcov_type count, int freq,
174 bool update_original,
175 vec<cgraph_edge_p> redirect_callers,
176 bool call_duplication_hook)
177 {
178 struct cgraph_node *new_node = cgraph_create_empty_node ();
179 struct cgraph_edge *e;
180 gcov_type count_scale;
181 unsigned i;
182
183 new_node->symbol.decl = decl;
184 symtab_register_node ((symtab_node)new_node);
185 new_node->origin = n->origin;
186 new_node->symbol.lto_file_data = n->symbol.lto_file_data;
187 if (new_node->origin)
188 {
189 new_node->next_nested = new_node->origin->nested;
190 new_node->origin->nested = new_node;
191 }
192 new_node->symbol.analyzed = n->symbol.analyzed;
193 new_node->symbol.definition = n->symbol.definition;
194 new_node->local = n->local;
195 new_node->symbol.externally_visible = false;
196 new_node->local.local = true;
197 new_node->global = n->global;
198 new_node->rtl = n->rtl;
199 new_node->count = count;
200 new_node->frequency = n->frequency;
201 new_node->clone = n->clone;
202 new_node->clone.tree_map = NULL;
203 if (n->count)
204 {
205 if (new_node->count > n->count)
206 count_scale = REG_BR_PROB_BASE;
207 else
208 count_scale = GCOV_COMPUTE_SCALE (new_node->count, n->count);
209 }
210 else
211 count_scale = 0;
212 if (update_original)
213 {
214 n->count -= count;
215 if (n->count < 0)
216 n->count = 0;
217 }
218
219 FOR_EACH_VEC_ELT (redirect_callers, i, e)
220 {
221 /* Redirect calls to the old version node to point to its new
222 version. */
223 cgraph_redirect_edge_callee (e, new_node);
224 }
225
226
227 for (e = n->callees;e; e=e->next_callee)
228 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
229 count_scale, freq, update_original);
230
231 for (e = n->indirect_calls; e; e = e->next_callee)
232 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
233 count_scale, freq, update_original);
234 ipa_clone_references ((symtab_node)new_node, &n->symbol.ref_list);
235
236 new_node->next_sibling_clone = n->clones;
237 if (n->clones)
238 n->clones->prev_sibling_clone = new_node;
239 n->clones = new_node;
240 new_node->clone_of = n;
241
242 if (call_duplication_hook)
243 cgraph_call_node_duplication_hooks (n, new_node);
244 return new_node;
245 }
246
247 /* Create a new name for clone of DECL, add SUFFIX. Returns an identifier. */
248
249 static GTY(()) unsigned int clone_fn_id_num;
250
251 tree
252 clone_function_name (tree decl, const char *suffix)
253 {
254 tree name = DECL_ASSEMBLER_NAME (decl);
255 size_t len = IDENTIFIER_LENGTH (name);
256 char *tmp_name, *prefix;
257
258 prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
259 memcpy (prefix, IDENTIFIER_POINTER (name), len);
260 strcpy (prefix + len + 1, suffix);
261 #ifndef NO_DOT_IN_LABEL
262 prefix[len] = '.';
263 #elif !defined NO_DOLLAR_IN_LABEL
264 prefix[len] = '$';
265 #else
266 prefix[len] = '_';
267 #endif
268 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
269 return get_identifier (tmp_name);
270 }
271
272 /* Create callgraph node clone with new declaration. The actual body will
273 be copied later at compilation stage.
274
275 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
276 bitmap interface.
277 */
278 struct cgraph_node *
279 cgraph_create_virtual_clone (struct cgraph_node *old_node,
280 vec<cgraph_edge_p> redirect_callers,
281 vec<ipa_replace_map_p, va_gc> *tree_map,
282 bitmap args_to_skip,
283 const char * suffix)
284 {
285 tree old_decl = old_node->symbol.decl;
286 struct cgraph_node *new_node = NULL;
287 tree new_decl;
288 size_t i;
289 struct ipa_replace_map *map;
290
291 if (!flag_wpa)
292 gcc_checking_assert (tree_versionable_function_p (old_decl));
293
294 gcc_assert (old_node->local.can_change_signature || !args_to_skip);
295
296 /* Make a new FUNCTION_DECL tree node */
297 if (!args_to_skip)
298 new_decl = copy_node (old_decl);
299 else
300 new_decl = build_function_decl_skip_args (old_decl, args_to_skip, false);
301 DECL_STRUCT_FUNCTION (new_decl) = NULL;
302
303 /* Generate a new name for the new version. */
304 DECL_NAME (new_decl) = clone_function_name (old_decl, suffix);
305 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
306 SET_DECL_RTL (new_decl, NULL);
307
308 new_node = cgraph_clone_node (old_node, new_decl, old_node->count,
309 CGRAPH_FREQ_BASE, false,
310 redirect_callers, false);
311 /* Update the properties.
312 Make clone visible only within this translation unit. Make sure
313 that is not weak also.
314 ??? We cannot use COMDAT linkage because there is no
315 ABI support for this. */
316 DECL_EXTERNAL (new_node->symbol.decl) = 0;
317 if (DECL_ONE_ONLY (old_decl))
318 DECL_SECTION_NAME (new_node->symbol.decl) = NULL;
319 DECL_COMDAT_GROUP (new_node->symbol.decl) = 0;
320 TREE_PUBLIC (new_node->symbol.decl) = 0;
321 DECL_COMDAT (new_node->symbol.decl) = 0;
322 DECL_WEAK (new_node->symbol.decl) = 0;
323 DECL_VIRTUAL_P (new_node->symbol.decl) = 0;
324 DECL_STATIC_CONSTRUCTOR (new_node->symbol.decl) = 0;
325 DECL_STATIC_DESTRUCTOR (new_node->symbol.decl) = 0;
326 new_node->clone.tree_map = tree_map;
327 new_node->clone.args_to_skip = args_to_skip;
328
329 /* Clones of global symbols or symbols with unique names are unique. */
330 if ((TREE_PUBLIC (old_decl)
331 && !DECL_EXTERNAL (old_decl)
332 && !DECL_WEAK (old_decl)
333 && !DECL_COMDAT (old_decl))
334 || in_lto_p)
335 new_node->symbol.unique_name = true;
336 FOR_EACH_VEC_SAFE_ELT (tree_map, i, map)
337 {
338 tree var = map->new_tree;
339 symtab_node ref_node;
340
341 STRIP_NOPS (var);
342 if (TREE_CODE (var) != ADDR_EXPR)
343 continue;
344 var = get_base_var (var);
345 if (!var)
346 continue;
347 if (TREE_CODE (var) != FUNCTION_DECL
348 && TREE_CODE (var) != VAR_DECL)
349 continue;
350
351 /* Record references of the future statement initializing the constant
352 argument. */
353 ref_node = symtab_get_node (var);
354 gcc_checking_assert (ref_node);
355 ipa_record_reference ((symtab_node)new_node, (symtab_node)ref_node,
356 IPA_REF_ADDR, NULL);
357 }
358 if (!args_to_skip)
359 new_node->clone.combined_args_to_skip = old_node->clone.combined_args_to_skip;
360 else if (old_node->clone.combined_args_to_skip)
361 {
362 int newi = 0, oldi = 0;
363 tree arg;
364 bitmap new_args_to_skip = BITMAP_GGC_ALLOC ();
365 struct cgraph_node *orig_node;
366 for (orig_node = old_node; orig_node->clone_of; orig_node = orig_node->clone_of)
367 ;
368 for (arg = DECL_ARGUMENTS (orig_node->symbol.decl);
369 arg; arg = DECL_CHAIN (arg), oldi++)
370 {
371 if (bitmap_bit_p (old_node->clone.combined_args_to_skip, oldi))
372 {
373 bitmap_set_bit (new_args_to_skip, oldi);
374 continue;
375 }
376 if (bitmap_bit_p (args_to_skip, newi))
377 bitmap_set_bit (new_args_to_skip, oldi);
378 newi++;
379 }
380 new_node->clone.combined_args_to_skip = new_args_to_skip;
381 }
382 else
383 new_node->clone.combined_args_to_skip = args_to_skip;
384 new_node->symbol.externally_visible = 0;
385 new_node->local.local = 1;
386 new_node->lowered = true;
387
388 cgraph_call_node_duplication_hooks (old_node, new_node);
389
390
391 return new_node;
392 }
393
394 /* NODE is being removed from symbol table; see if its entry can be replaced by
395 other inline clone. */
396 struct cgraph_node *
397 cgraph_find_replacement_node (struct cgraph_node *node)
398 {
399 struct cgraph_node *next_inline_clone, *replacement;
400
401 for (next_inline_clone = node->clones;
402 next_inline_clone
403 && next_inline_clone->symbol.decl != node->symbol.decl;
404 next_inline_clone = next_inline_clone->next_sibling_clone)
405 ;
406
407 /* If there is inline clone of the node being removed, we need
408 to put it into the position of removed node and reorganize all
409 other clones to be based on it. */
410 if (next_inline_clone)
411 {
412 struct cgraph_node *n;
413 struct cgraph_node *new_clones;
414
415 replacement = next_inline_clone;
416
417 /* Unlink inline clone from the list of clones of removed node. */
418 if (next_inline_clone->next_sibling_clone)
419 next_inline_clone->next_sibling_clone->prev_sibling_clone
420 = next_inline_clone->prev_sibling_clone;
421 if (next_inline_clone->prev_sibling_clone)
422 {
423 gcc_assert (node->clones != next_inline_clone);
424 next_inline_clone->prev_sibling_clone->next_sibling_clone
425 = next_inline_clone->next_sibling_clone;
426 }
427 else
428 {
429 gcc_assert (node->clones == next_inline_clone);
430 node->clones = next_inline_clone->next_sibling_clone;
431 }
432
433 new_clones = node->clones;
434 node->clones = NULL;
435
436 /* Copy clone info. */
437 next_inline_clone->clone = node->clone;
438
439 /* Now place it into clone tree at same level at NODE. */
440 next_inline_clone->clone_of = node->clone_of;
441 next_inline_clone->prev_sibling_clone = NULL;
442 next_inline_clone->next_sibling_clone = NULL;
443 if (node->clone_of)
444 {
445 if (node->clone_of->clones)
446 node->clone_of->clones->prev_sibling_clone = next_inline_clone;
447 next_inline_clone->next_sibling_clone = node->clone_of->clones;
448 node->clone_of->clones = next_inline_clone;
449 }
450
451 /* Merge the clone list. */
452 if (new_clones)
453 {
454 if (!next_inline_clone->clones)
455 next_inline_clone->clones = new_clones;
456 else
457 {
458 n = next_inline_clone->clones;
459 while (n->next_sibling_clone)
460 n = n->next_sibling_clone;
461 n->next_sibling_clone = new_clones;
462 new_clones->prev_sibling_clone = n;
463 }
464 }
465
466 /* Update clone_of pointers. */
467 n = new_clones;
468 while (n)
469 {
470 n->clone_of = next_inline_clone;
471 n = n->next_sibling_clone;
472 }
473 return replacement;
474 }
475 else
476 return NULL;
477 }
478
479 /* Like cgraph_set_call_stmt but walk the clone tree and update all
480 clones sharing the same function body. */
481
482 void
483 cgraph_set_call_stmt_including_clones (struct cgraph_node *orig,
484 gimple old_stmt, gimple new_stmt)
485 {
486 struct cgraph_node *node;
487 struct cgraph_edge *edge = cgraph_edge (orig, old_stmt);
488
489 if (edge)
490 cgraph_set_call_stmt (edge, new_stmt);
491
492 node = orig->clones;
493 if (node)
494 while (node != orig)
495 {
496 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
497 if (edge)
498 cgraph_set_call_stmt (edge, new_stmt);
499 if (node->clones)
500 node = node->clones;
501 else if (node->next_sibling_clone)
502 node = node->next_sibling_clone;
503 else
504 {
505 while (node != orig && !node->next_sibling_clone)
506 node = node->clone_of;
507 if (node != orig)
508 node = node->next_sibling_clone;
509 }
510 }
511 }
512
513 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
514 same function body. If clones already have edge for OLD_STMT; only
515 update the edge same way as cgraph_set_call_stmt_including_clones does.
516
517 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
518 frequencies of the clones. */
519
520 void
521 cgraph_create_edge_including_clones (struct cgraph_node *orig,
522 struct cgraph_node *callee,
523 gimple old_stmt,
524 gimple stmt, gcov_type count,
525 int freq,
526 cgraph_inline_failed_t reason)
527 {
528 struct cgraph_node *node;
529 struct cgraph_edge *edge;
530
531 if (!cgraph_edge (orig, stmt))
532 {
533 edge = cgraph_create_edge (orig, callee, stmt, count, freq);
534 edge->inline_failed = reason;
535 }
536
537 node = orig->clones;
538 if (node)
539 while (node != orig)
540 {
541 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
542
543 /* It is possible that clones already contain the edge while
544 master didn't. Either we promoted indirect call into direct
545 call in the clone or we are processing clones of unreachable
546 master where edges has been removed. */
547 if (edge)
548 cgraph_set_call_stmt (edge, stmt);
549 else if (!cgraph_edge (node, stmt))
550 {
551 edge = cgraph_create_edge (node, callee, stmt, count,
552 freq);
553 edge->inline_failed = reason;
554 }
555
556 if (node->clones)
557 node = node->clones;
558 else if (node->next_sibling_clone)
559 node = node->next_sibling_clone;
560 else
561 {
562 while (node != orig && !node->next_sibling_clone)
563 node = node->clone_of;
564 if (node != orig)
565 node = node->next_sibling_clone;
566 }
567 }
568 }
569
570 /* Remove the node from cgraph and all inline clones inlined into it.
571 Skip however removal of FORBIDDEN_NODE and return true if it needs to be
572 removed. This allows to call the function from outer loop walking clone
573 tree. */
574
575 bool
576 cgraph_remove_node_and_inline_clones (struct cgraph_node *node, struct cgraph_node *forbidden_node)
577 {
578 struct cgraph_edge *e, *next;
579 bool found = false;
580
581 if (node == forbidden_node)
582 {
583 cgraph_remove_edge (node->callers);
584 return true;
585 }
586 for (e = node->callees; e; e = next)
587 {
588 next = e->next_callee;
589 if (!e->inline_failed)
590 found |= cgraph_remove_node_and_inline_clones (e->callee, forbidden_node);
591 }
592 cgraph_remove_node (node);
593 return found;
594 }
595
596 /* The edges representing the callers of the NEW_VERSION node were
597 fixed by cgraph_function_versioning (), now the call_expr in their
598 respective tree code should be updated to call the NEW_VERSION. */
599
600 static void
601 update_call_expr (struct cgraph_node *new_version)
602 {
603 struct cgraph_edge *e;
604
605 gcc_assert (new_version);
606
607 /* Update the call expr on the edges to call the new version. */
608 for (e = new_version->callers; e; e = e->next_caller)
609 {
610 struct function *inner_function = DECL_STRUCT_FUNCTION (e->caller->symbol.decl);
611 gimple_call_set_fndecl (e->call_stmt, new_version->symbol.decl);
612 maybe_clean_eh_stmt_fn (inner_function, e->call_stmt);
613 }
614 }
615
616
617 /* Create a new cgraph node which is the new version of
618 OLD_VERSION node. REDIRECT_CALLERS holds the callers
619 edges which should be redirected to point to
620 NEW_VERSION. ALL the callees edges of OLD_VERSION
621 are cloned to the new version node. Return the new
622 version node.
623
624 If non-NULL BLOCK_TO_COPY determine what basic blocks
625 was copied to prevent duplications of calls that are dead
626 in the clone. */
627
628 struct cgraph_node *
629 cgraph_copy_node_for_versioning (struct cgraph_node *old_version,
630 tree new_decl,
631 vec<cgraph_edge_p> redirect_callers,
632 bitmap bbs_to_copy)
633 {
634 struct cgraph_node *new_version;
635 struct cgraph_edge *e;
636 unsigned i;
637
638 gcc_assert (old_version);
639
640 new_version = cgraph_create_node (new_decl);
641
642 new_version->symbol.analyzed = old_version->symbol.analyzed;
643 new_version->symbol.definition = old_version->symbol.definition;
644 new_version->local = old_version->local;
645 new_version->symbol.externally_visible = false;
646 new_version->local.local = new_version->symbol.definition;
647 new_version->global = old_version->global;
648 new_version->rtl = old_version->rtl;
649 new_version->count = old_version->count;
650
651 for (e = old_version->callees; e; e=e->next_callee)
652 if (!bbs_to_copy
653 || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
654 cgraph_clone_edge (e, new_version, e->call_stmt,
655 e->lto_stmt_uid, REG_BR_PROB_BASE,
656 CGRAPH_FREQ_BASE,
657 true);
658 for (e = old_version->indirect_calls; e; e=e->next_callee)
659 if (!bbs_to_copy
660 || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
661 cgraph_clone_edge (e, new_version, e->call_stmt,
662 e->lto_stmt_uid, REG_BR_PROB_BASE,
663 CGRAPH_FREQ_BASE,
664 true);
665 FOR_EACH_VEC_ELT (redirect_callers, i, e)
666 {
667 /* Redirect calls to the old version node to point to its new
668 version. */
669 cgraph_redirect_edge_callee (e, new_version);
670 }
671
672 cgraph_call_node_duplication_hooks (old_version, new_version);
673
674 return new_version;
675 }
676
677 /* Perform function versioning.
678 Function versioning includes copying of the tree and
679 a callgraph update (creating a new cgraph node and updating
680 its callees and callers).
681
682 REDIRECT_CALLERS varray includes the edges to be redirected
683 to the new version.
684
685 TREE_MAP is a mapping of tree nodes we want to replace with
686 new ones (according to results of prior analysis).
687 OLD_VERSION_NODE is the node that is versioned.
688
689 If non-NULL ARGS_TO_SKIP determine function parameters to remove
690 from new version.
691 If SKIP_RETURN is true, the new version will return void.
692 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
693 If non_NULL NEW_ENTRY determine new entry BB of the clone.
694
695 Return the new version's cgraph node. */
696
697 struct cgraph_node *
698 cgraph_function_versioning (struct cgraph_node *old_version_node,
699 vec<cgraph_edge_p> redirect_callers,
700 vec<ipa_replace_map_p, va_gc> *tree_map,
701 bitmap args_to_skip,
702 bool skip_return,
703 bitmap bbs_to_copy,
704 basic_block new_entry_block,
705 const char *clone_name)
706 {
707 tree old_decl = old_version_node->symbol.decl;
708 struct cgraph_node *new_version_node = NULL;
709 tree new_decl;
710
711 if (!tree_versionable_function_p (old_decl))
712 return NULL;
713
714 gcc_assert (old_version_node->local.can_change_signature || !args_to_skip);
715
716 /* Make a new FUNCTION_DECL tree node for the new version. */
717 if (!args_to_skip && !skip_return)
718 new_decl = copy_node (old_decl);
719 else
720 new_decl
721 = build_function_decl_skip_args (old_decl, args_to_skip, skip_return);
722
723 /* Generate a new name for the new version. */
724 DECL_NAME (new_decl) = clone_function_name (old_decl, clone_name);
725 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
726 SET_DECL_RTL (new_decl, NULL);
727
728 /* When the old decl was a con-/destructor make sure the clone isn't. */
729 DECL_STATIC_CONSTRUCTOR(new_decl) = 0;
730 DECL_STATIC_DESTRUCTOR(new_decl) = 0;
731
732 /* Create the new version's call-graph node.
733 and update the edges of the new node. */
734 new_version_node =
735 cgraph_copy_node_for_versioning (old_version_node, new_decl,
736 redirect_callers, bbs_to_copy);
737
738 /* Copy the OLD_VERSION_NODE function tree to the new version. */
739 tree_function_versioning (old_decl, new_decl, tree_map, false, args_to_skip,
740 skip_return, bbs_to_copy, new_entry_block);
741
742 /* Update the new version's properties.
743 Make The new version visible only within this translation unit. Make sure
744 that is not weak also.
745 ??? We cannot use COMDAT linkage because there is no
746 ABI support for this. */
747 symtab_make_decl_local (new_version_node->symbol.decl);
748 DECL_VIRTUAL_P (new_version_node->symbol.decl) = 0;
749 new_version_node->symbol.externally_visible = 0;
750 new_version_node->local.local = 1;
751 new_version_node->lowered = true;
752 /* Clones of global symbols or symbols with unique names are unique. */
753 if ((TREE_PUBLIC (old_decl)
754 && !DECL_EXTERNAL (old_decl)
755 && !DECL_WEAK (old_decl)
756 && !DECL_COMDAT (old_decl))
757 || in_lto_p)
758 new_version_node->symbol.unique_name = true;
759
760 /* Update the call_expr on the edges to call the new version node. */
761 update_call_expr (new_version_node);
762
763 cgraph_call_function_insertion_hooks (new_version_node);
764 return new_version_node;
765 }
766
767 /* Given virtual clone, turn it into actual clone. */
768
769 static void
770 cgraph_materialize_clone (struct cgraph_node *node)
771 {
772 bitmap_obstack_initialize (NULL);
773 node->former_clone_of = node->clone_of->symbol.decl;
774 if (node->clone_of->former_clone_of)
775 node->former_clone_of = node->clone_of->former_clone_of;
776 /* Copy the OLD_VERSION_NODE function tree to the new version. */
777 tree_function_versioning (node->clone_of->symbol.decl, node->symbol.decl,
778 node->clone.tree_map, true,
779 node->clone.args_to_skip, false,
780 NULL, NULL);
781 if (cgraph_dump_file)
782 {
783 dump_function_to_file (node->clone_of->symbol.decl, cgraph_dump_file, dump_flags);
784 dump_function_to_file (node->symbol.decl, cgraph_dump_file, dump_flags);
785 }
786
787 /* Function is no longer clone. */
788 if (node->next_sibling_clone)
789 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
790 if (node->prev_sibling_clone)
791 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
792 else
793 node->clone_of->clones = node->next_sibling_clone;
794 node->next_sibling_clone = NULL;
795 node->prev_sibling_clone = NULL;
796 if (!node->clone_of->symbol.analyzed && !node->clone_of->clones)
797 {
798 cgraph_release_function_body (node->clone_of);
799 cgraph_node_remove_callees (node->clone_of);
800 ipa_remove_all_references (&node->clone_of->symbol.ref_list);
801 }
802 node->clone_of = NULL;
803 bitmap_obstack_release (NULL);
804 }
805
806 /* Once all functions from compilation unit are in memory, produce all clones
807 and update all calls. We might also do this on demand if we don't want to
808 bring all functions to memory prior compilation, but current WHOPR
809 implementation does that and it is is bit easier to keep everything right in
810 this order. */
811
812 void
813 cgraph_materialize_all_clones (void)
814 {
815 struct cgraph_node *node;
816 bool stabilized = false;
817
818 if (cgraph_dump_file)
819 fprintf (cgraph_dump_file, "Materializing clones\n");
820 #ifdef ENABLE_CHECKING
821 verify_cgraph ();
822 #endif
823
824 /* We can also do topological order, but number of iterations should be
825 bounded by number of IPA passes since single IPA pass is probably not
826 going to create clones of clones it created itself. */
827 while (!stabilized)
828 {
829 stabilized = true;
830 FOR_EACH_FUNCTION (node)
831 {
832 if (node->clone_of && node->symbol.decl != node->clone_of->symbol.decl
833 && !gimple_has_body_p (node->symbol.decl))
834 {
835 if (gimple_has_body_p (node->clone_of->symbol.decl))
836 {
837 if (cgraph_dump_file)
838 {
839 fprintf (cgraph_dump_file, "cloning %s to %s\n",
840 xstrdup (cgraph_node_name (node->clone_of)),
841 xstrdup (cgraph_node_name (node)));
842 if (node->clone.tree_map)
843 {
844 unsigned int i;
845 fprintf (cgraph_dump_file, " replace map: ");
846 for (i = 0;
847 i < vec_safe_length (node->clone.tree_map);
848 i++)
849 {
850 struct ipa_replace_map *replace_info;
851 replace_info = (*node->clone.tree_map)[i];
852 print_generic_expr (cgraph_dump_file, replace_info->old_tree, 0);
853 fprintf (cgraph_dump_file, " -> ");
854 print_generic_expr (cgraph_dump_file, replace_info->new_tree, 0);
855 fprintf (cgraph_dump_file, "%s%s;",
856 replace_info->replace_p ? "(replace)":"",
857 replace_info->ref_p ? "(ref)":"");
858 }
859 fprintf (cgraph_dump_file, "\n");
860 }
861 if (node->clone.args_to_skip)
862 {
863 fprintf (cgraph_dump_file, " args_to_skip: ");
864 dump_bitmap (cgraph_dump_file, node->clone.args_to_skip);
865 }
866 if (node->clone.args_to_skip)
867 {
868 fprintf (cgraph_dump_file, " combined_args_to_skip:");
869 dump_bitmap (cgraph_dump_file, node->clone.combined_args_to_skip);
870 }
871 }
872 cgraph_materialize_clone (node);
873 stabilized = false;
874 }
875 }
876 }
877 }
878 FOR_EACH_FUNCTION (node)
879 if (!node->symbol.analyzed && node->callees)
880 cgraph_node_remove_callees (node);
881 if (cgraph_dump_file)
882 fprintf (cgraph_dump_file, "Materialization Call site updates done.\n");
883 #ifdef ENABLE_CHECKING
884 verify_cgraph ();
885 #endif
886 symtab_remove_unreachable_nodes (false, cgraph_dump_file);
887 }
888
889 #include "gt-cgraphclones.h"