PR c++/56838
[official-gcc.git] / gcc / cgraphclones.c
blobfa6a9113a1e70da280989f7ea373229c6544e7fb
1 /* Callgraph clones
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
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.
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.
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/>. */
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.
25 To allow whole program optimization without actual presence of function
26 bodies, an additional infrastructure is provided for so-called virtual
27 clones
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).
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.
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.
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.
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.
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.
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. */
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"
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)
104 struct cgraph_edge *new_edge;
105 /* Update this to use GCOV_COMPUTE_SCALE. */
106 gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
107 gcov_type freq;
109 /* We do not want to ignore loop nest after frequency drops to 0. */
110 if (!freq_scale)
111 freq_scale = 1;
112 freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
113 if (freq > CGRAPH_FREQ_MAX)
114 freq = CGRAPH_FREQ_MAX;
116 if (e->indirect_unknown_callee)
118 tree decl;
120 if (call_stmt && (decl = gimple_call_fndecl (call_stmt)))
122 struct cgraph_node *callee = cgraph_get_node (decl);
123 gcc_checking_assert (callee);
124 new_edge = cgraph_create_edge (n, callee, call_stmt, count, freq);
126 else
128 new_edge = cgraph_create_indirect_edge (n, call_stmt,
129 e->indirect_info->ecf_flags,
130 count, freq);
131 *new_edge->indirect_info = *e->indirect_info;
134 else
136 new_edge = cgraph_create_edge (n, e->callee, call_stmt, count, freq);
137 if (e->indirect_info)
139 new_edge->indirect_info
140 = ggc_alloc_cleared_cgraph_indirect_call_info ();
141 *new_edge->indirect_info = *e->indirect_info;
145 new_edge->inline_failed = e->inline_failed;
146 new_edge->indirect_inlining_edge = e->indirect_inlining_edge;
147 new_edge->lto_stmt_uid = stmt_uid;
148 /* Clone flags that depend on call_stmt availability manually. */
149 new_edge->can_throw_external = e->can_throw_external;
150 new_edge->call_stmt_cannot_inline_p = e->call_stmt_cannot_inline_p;
151 if (update_original)
153 e->count -= new_edge->count;
154 if (e->count < 0)
155 e->count = 0;
157 cgraph_call_edge_duplication_hooks (e, new_edge);
158 return new_edge;
162 /* Create node representing clone of N executed COUNT times. Decrease
163 the execution counts from original node too.
164 The new clone will have decl set to DECL that may or may not be the same
165 as decl of N.
167 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
168 function's profile to reflect the fact that part of execution is handled
169 by node.
170 When CALL_DUPLICATOIN_HOOK is true, the ipa passes are acknowledged about
171 the new clone. Otherwise the caller is responsible for doing so later. */
173 struct cgraph_node *
174 cgraph_clone_node (struct cgraph_node *n, tree decl, gcov_type count, int freq,
175 bool update_original,
176 vec<cgraph_edge_p> redirect_callers,
177 bool call_duplication_hook)
179 struct cgraph_node *new_node = cgraph_create_empty_node ();
180 struct cgraph_edge *e;
181 gcov_type count_scale;
182 unsigned i;
184 new_node->symbol.decl = decl;
185 symtab_register_node ((symtab_node)new_node);
186 new_node->origin = n->origin;
187 new_node->symbol.lto_file_data = n->symbol.lto_file_data;
188 if (new_node->origin)
190 new_node->next_nested = new_node->origin->nested;
191 new_node->origin->nested = new_node;
193 new_node->analyzed = n->analyzed;
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)
205 if (new_node->count > n->count)
206 count_scale = REG_BR_PROB_BASE;
207 else
208 /* Update to use GCOV_COMPUTE_SCALE. */
209 count_scale = new_node->count * REG_BR_PROB_BASE / n->count;
211 else
212 count_scale = 0;
213 if (update_original)
215 n->count -= count;
216 if (n->count < 0)
217 n->count = 0;
220 FOR_EACH_VEC_ELT (redirect_callers, i, e)
222 /* Redirect calls to the old version node to point to its new
223 version. */
224 cgraph_redirect_edge_callee (e, new_node);
228 for (e = n->callees;e; e=e->next_callee)
229 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
230 count_scale, freq, update_original);
232 for (e = n->indirect_calls; e; e = e->next_callee)
233 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
234 count_scale, freq, update_original);
235 ipa_clone_references ((symtab_node)new_node, &n->symbol.ref_list);
237 new_node->next_sibling_clone = n->clones;
238 if (n->clones)
239 n->clones->prev_sibling_clone = new_node;
240 n->clones = new_node;
241 new_node->clone_of = n;
243 if (call_duplication_hook)
244 cgraph_call_node_duplication_hooks (n, new_node);
245 return new_node;
248 /* Create a new name for clone of DECL, add SUFFIX. Returns an identifier. */
250 static GTY(()) unsigned int clone_fn_id_num;
252 tree
253 clone_function_name (tree decl, const char *suffix)
255 tree name = DECL_ASSEMBLER_NAME (decl);
256 size_t len = IDENTIFIER_LENGTH (name);
257 char *tmp_name, *prefix;
259 prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
260 memcpy (prefix, IDENTIFIER_POINTER (name), len);
261 strcpy (prefix + len + 1, suffix);
262 #ifndef NO_DOT_IN_LABEL
263 prefix[len] = '.';
264 #elif !defined NO_DOLLAR_IN_LABEL
265 prefix[len] = '$';
266 #else
267 prefix[len] = '_';
268 #endif
269 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
270 return get_identifier (tmp_name);
273 /* Create callgraph node clone with new declaration. The actual body will
274 be copied later at compilation stage.
276 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
277 bitmap interface.
279 struct cgraph_node *
280 cgraph_create_virtual_clone (struct cgraph_node *old_node,
281 vec<cgraph_edge_p> redirect_callers,
282 vec<ipa_replace_map_p, va_gc> *tree_map,
283 bitmap args_to_skip,
284 const char * suffix)
286 tree old_decl = old_node->symbol.decl;
287 struct cgraph_node *new_node = NULL;
288 tree new_decl;
289 size_t i;
290 struct ipa_replace_map *map;
292 if (!flag_wpa)
293 gcc_checking_assert (tree_versionable_function_p (old_decl));
295 gcc_assert (old_node->local.can_change_signature || !args_to_skip);
297 /* Make a new FUNCTION_DECL tree node */
298 if (!args_to_skip)
299 new_decl = copy_node (old_decl);
300 else
301 new_decl = build_function_decl_skip_args (old_decl, args_to_skip, false);
302 DECL_STRUCT_FUNCTION (new_decl) = NULL;
304 /* Generate a new name for the new version. */
305 DECL_NAME (new_decl) = clone_function_name (old_decl, suffix);
306 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
307 SET_DECL_RTL (new_decl, NULL);
309 new_node = cgraph_clone_node (old_node, new_decl, old_node->count,
310 CGRAPH_FREQ_BASE, false,
311 redirect_callers, false);
312 /* Update the properties.
313 Make clone visible only within this translation unit. Make sure
314 that is not weak also.
315 ??? We cannot use COMDAT linkage because there is no
316 ABI support for this. */
317 DECL_EXTERNAL (new_node->symbol.decl) = 0;
318 if (DECL_ONE_ONLY (old_decl))
319 DECL_SECTION_NAME (new_node->symbol.decl) = NULL;
320 DECL_COMDAT_GROUP (new_node->symbol.decl) = 0;
321 TREE_PUBLIC (new_node->symbol.decl) = 0;
322 DECL_COMDAT (new_node->symbol.decl) = 0;
323 DECL_WEAK (new_node->symbol.decl) = 0;
324 DECL_VIRTUAL_P (new_node->symbol.decl) = 0;
325 DECL_STATIC_CONSTRUCTOR (new_node->symbol.decl) = 0;
326 DECL_STATIC_DESTRUCTOR (new_node->symbol.decl) = 0;
327 new_node->clone.tree_map = tree_map;
328 new_node->clone.args_to_skip = args_to_skip;
329 FOR_EACH_VEC_SAFE_ELT (tree_map, i, map)
331 tree var = map->new_tree;
332 symtab_node ref_node;
334 STRIP_NOPS (var);
335 if (TREE_CODE (var) != ADDR_EXPR)
336 continue;
337 var = get_base_var (var);
338 if (!var)
339 continue;
340 if (TREE_CODE (var) != FUNCTION_DECL
341 && TREE_CODE (var) != VAR_DECL)
342 continue;
344 /* Record references of the future statement initializing the constant
345 argument. */
346 ref_node = symtab_get_node (var);
347 gcc_checking_assert (ref_node);
348 ipa_record_reference ((symtab_node)new_node, (symtab_node)ref_node,
349 IPA_REF_ADDR, NULL);
351 if (!args_to_skip)
352 new_node->clone.combined_args_to_skip = old_node->clone.combined_args_to_skip;
353 else if (old_node->clone.combined_args_to_skip)
355 int newi = 0, oldi = 0;
356 tree arg;
357 bitmap new_args_to_skip = BITMAP_GGC_ALLOC ();
358 struct cgraph_node *orig_node;
359 for (orig_node = old_node; orig_node->clone_of; orig_node = orig_node->clone_of)
361 for (arg = DECL_ARGUMENTS (orig_node->symbol.decl);
362 arg; arg = DECL_CHAIN (arg), oldi++)
364 if (bitmap_bit_p (old_node->clone.combined_args_to_skip, oldi))
366 bitmap_set_bit (new_args_to_skip, oldi);
367 continue;
369 if (bitmap_bit_p (args_to_skip, newi))
370 bitmap_set_bit (new_args_to_skip, oldi);
371 newi++;
373 new_node->clone.combined_args_to_skip = new_args_to_skip;
375 else
376 new_node->clone.combined_args_to_skip = args_to_skip;
377 new_node->symbol.externally_visible = 0;
378 new_node->local.local = 1;
379 new_node->lowered = true;
381 cgraph_call_node_duplication_hooks (old_node, new_node);
384 return new_node;
387 /* NODE is being removed from symbol table; see if its entry can be replaced by
388 other inline clone. */
389 struct cgraph_node *
390 cgraph_find_replacement_node (struct cgraph_node *node)
392 struct cgraph_node *next_inline_clone, *replacement;
394 for (next_inline_clone = node->clones;
395 next_inline_clone
396 && next_inline_clone->symbol.decl != node->symbol.decl;
397 next_inline_clone = next_inline_clone->next_sibling_clone)
400 /* If there is inline clone of the node being removed, we need
401 to put it into the position of removed node and reorganize all
402 other clones to be based on it. */
403 if (next_inline_clone)
405 struct cgraph_node *n;
406 struct cgraph_node *new_clones;
408 replacement = next_inline_clone;
410 /* Unlink inline clone from the list of clones of removed node. */
411 if (next_inline_clone->next_sibling_clone)
412 next_inline_clone->next_sibling_clone->prev_sibling_clone
413 = next_inline_clone->prev_sibling_clone;
414 if (next_inline_clone->prev_sibling_clone)
416 gcc_assert (node->clones != next_inline_clone);
417 next_inline_clone->prev_sibling_clone->next_sibling_clone
418 = next_inline_clone->next_sibling_clone;
420 else
422 gcc_assert (node->clones == next_inline_clone);
423 node->clones = next_inline_clone->next_sibling_clone;
426 new_clones = node->clones;
427 node->clones = NULL;
429 /* Copy clone info. */
430 next_inline_clone->clone = node->clone;
432 /* Now place it into clone tree at same level at NODE. */
433 next_inline_clone->clone_of = node->clone_of;
434 next_inline_clone->prev_sibling_clone = NULL;
435 next_inline_clone->next_sibling_clone = NULL;
436 if (node->clone_of)
438 if (node->clone_of->clones)
439 node->clone_of->clones->prev_sibling_clone = next_inline_clone;
440 next_inline_clone->next_sibling_clone = node->clone_of->clones;
441 node->clone_of->clones = next_inline_clone;
444 /* Merge the clone list. */
445 if (new_clones)
447 if (!next_inline_clone->clones)
448 next_inline_clone->clones = new_clones;
449 else
451 n = next_inline_clone->clones;
452 while (n->next_sibling_clone)
453 n = n->next_sibling_clone;
454 n->next_sibling_clone = new_clones;
455 new_clones->prev_sibling_clone = n;
459 /* Update clone_of pointers. */
460 n = new_clones;
461 while (n)
463 n->clone_of = next_inline_clone;
464 n = n->next_sibling_clone;
466 return replacement;
468 else
469 return NULL;
472 /* Like cgraph_set_call_stmt but walk the clone tree and update all
473 clones sharing the same function body. */
475 void
476 cgraph_set_call_stmt_including_clones (struct cgraph_node *orig,
477 gimple old_stmt, gimple new_stmt)
479 struct cgraph_node *node;
480 struct cgraph_edge *edge = cgraph_edge (orig, old_stmt);
482 if (edge)
483 cgraph_set_call_stmt (edge, new_stmt);
485 node = orig->clones;
486 if (node)
487 while (node != orig)
489 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
490 if (edge)
491 cgraph_set_call_stmt (edge, new_stmt);
492 if (node->clones)
493 node = node->clones;
494 else if (node->next_sibling_clone)
495 node = node->next_sibling_clone;
496 else
498 while (node != orig && !node->next_sibling_clone)
499 node = node->clone_of;
500 if (node != orig)
501 node = node->next_sibling_clone;
506 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
507 same function body. If clones already have edge for OLD_STMT; only
508 update the edge same way as cgraph_set_call_stmt_including_clones does.
510 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
511 frequencies of the clones. */
513 void
514 cgraph_create_edge_including_clones (struct cgraph_node *orig,
515 struct cgraph_node *callee,
516 gimple old_stmt,
517 gimple stmt, gcov_type count,
518 int freq,
519 cgraph_inline_failed_t reason)
521 struct cgraph_node *node;
522 struct cgraph_edge *edge;
524 if (!cgraph_edge (orig, stmt))
526 edge = cgraph_create_edge (orig, callee, stmt, count, freq);
527 edge->inline_failed = reason;
530 node = orig->clones;
531 if (node)
532 while (node != orig)
534 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
536 /* It is possible that clones already contain the edge while
537 master didn't. Either we promoted indirect call into direct
538 call in the clone or we are processing clones of unreachable
539 master where edges has been removed. */
540 if (edge)
541 cgraph_set_call_stmt (edge, stmt);
542 else if (!cgraph_edge (node, stmt))
544 edge = cgraph_create_edge (node, callee, stmt, count,
545 freq);
546 edge->inline_failed = reason;
549 if (node->clones)
550 node = node->clones;
551 else if (node->next_sibling_clone)
552 node = node->next_sibling_clone;
553 else
555 while (node != orig && !node->next_sibling_clone)
556 node = node->clone_of;
557 if (node != orig)
558 node = node->next_sibling_clone;
563 /* Remove the node from cgraph and all inline clones inlined into it.
564 Skip however removal of FORBIDDEN_NODE and return true if it needs to be
565 removed. This allows to call the function from outer loop walking clone
566 tree. */
568 bool
569 cgraph_remove_node_and_inline_clones (struct cgraph_node *node, struct cgraph_node *forbidden_node)
571 struct cgraph_edge *e, *next;
572 bool found = false;
574 if (node == forbidden_node)
576 cgraph_remove_edge (node->callers);
577 return true;
579 for (e = node->callees; e; e = next)
581 next = e->next_callee;
582 if (!e->inline_failed)
583 found |= cgraph_remove_node_and_inline_clones (e->callee, forbidden_node);
585 cgraph_remove_node (node);
586 return found;
589 /* The edges representing the callers of the NEW_VERSION node were
590 fixed by cgraph_function_versioning (), now the call_expr in their
591 respective tree code should be updated to call the NEW_VERSION. */
593 static void
594 update_call_expr (struct cgraph_node *new_version)
596 struct cgraph_edge *e;
598 gcc_assert (new_version);
600 /* Update the call expr on the edges to call the new version. */
601 for (e = new_version->callers; e; e = e->next_caller)
603 struct function *inner_function = DECL_STRUCT_FUNCTION (e->caller->symbol.decl);
604 gimple_call_set_fndecl (e->call_stmt, new_version->symbol.decl);
605 maybe_clean_eh_stmt_fn (inner_function, e->call_stmt);
610 /* Create a new cgraph node which is the new version of
611 OLD_VERSION node. REDIRECT_CALLERS holds the callers
612 edges which should be redirected to point to
613 NEW_VERSION. ALL the callees edges of OLD_VERSION
614 are cloned to the new version node. Return the new
615 version node.
617 If non-NULL BLOCK_TO_COPY determine what basic blocks
618 was copied to prevent duplications of calls that are dead
619 in the clone. */
621 struct cgraph_node *
622 cgraph_copy_node_for_versioning (struct cgraph_node *old_version,
623 tree new_decl,
624 vec<cgraph_edge_p> redirect_callers,
625 bitmap bbs_to_copy)
627 struct cgraph_node *new_version;
628 struct cgraph_edge *e;
629 unsigned i;
631 gcc_assert (old_version);
633 new_version = cgraph_create_node (new_decl);
635 new_version->analyzed = old_version->analyzed;
636 new_version->local = old_version->local;
637 new_version->symbol.externally_visible = false;
638 new_version->local.local = old_version->analyzed;
639 new_version->global = old_version->global;
640 new_version->rtl = old_version->rtl;
641 new_version->count = old_version->count;
643 for (e = old_version->callees; e; e=e->next_callee)
644 if (!bbs_to_copy
645 || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
646 cgraph_clone_edge (e, new_version, e->call_stmt,
647 e->lto_stmt_uid, REG_BR_PROB_BASE,
648 CGRAPH_FREQ_BASE,
649 true);
650 for (e = old_version->indirect_calls; e; e=e->next_callee)
651 if (!bbs_to_copy
652 || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
653 cgraph_clone_edge (e, new_version, e->call_stmt,
654 e->lto_stmt_uid, REG_BR_PROB_BASE,
655 CGRAPH_FREQ_BASE,
656 true);
657 FOR_EACH_VEC_ELT (redirect_callers, i, e)
659 /* Redirect calls to the old version node to point to its new
660 version. */
661 cgraph_redirect_edge_callee (e, new_version);
664 cgraph_call_node_duplication_hooks (old_version, new_version);
666 return new_version;
669 /* Perform function versioning.
670 Function versioning includes copying of the tree and
671 a callgraph update (creating a new cgraph node and updating
672 its callees and callers).
674 REDIRECT_CALLERS varray includes the edges to be redirected
675 to the new version.
677 TREE_MAP is a mapping of tree nodes we want to replace with
678 new ones (according to results of prior analysis).
679 OLD_VERSION_NODE is the node that is versioned.
681 If non-NULL ARGS_TO_SKIP determine function parameters to remove
682 from new version.
683 If SKIP_RETURN is true, the new version will return void.
684 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
685 If non_NULL NEW_ENTRY determine new entry BB of the clone.
687 Return the new version's cgraph node. */
689 struct cgraph_node *
690 cgraph_function_versioning (struct cgraph_node *old_version_node,
691 vec<cgraph_edge_p> redirect_callers,
692 vec<ipa_replace_map_p, va_gc> *tree_map,
693 bitmap args_to_skip,
694 bool skip_return,
695 bitmap bbs_to_copy,
696 basic_block new_entry_block,
697 const char *clone_name)
699 tree old_decl = old_version_node->symbol.decl;
700 struct cgraph_node *new_version_node = NULL;
701 tree new_decl;
703 if (!tree_versionable_function_p (old_decl))
704 return NULL;
706 gcc_assert (old_version_node->local.can_change_signature || !args_to_skip);
708 /* Make a new FUNCTION_DECL tree node for the new version. */
709 if (!args_to_skip && !skip_return)
710 new_decl = copy_node (old_decl);
711 else
712 new_decl
713 = build_function_decl_skip_args (old_decl, args_to_skip, skip_return);
715 /* Generate a new name for the new version. */
716 DECL_NAME (new_decl) = clone_function_name (old_decl, clone_name);
717 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
718 SET_DECL_RTL (new_decl, NULL);
720 /* When the old decl was a con-/destructor make sure the clone isn't. */
721 DECL_STATIC_CONSTRUCTOR(new_decl) = 0;
722 DECL_STATIC_DESTRUCTOR(new_decl) = 0;
724 /* Create the new version's call-graph node.
725 and update the edges of the new node. */
726 new_version_node =
727 cgraph_copy_node_for_versioning (old_version_node, new_decl,
728 redirect_callers, bbs_to_copy);
730 /* Copy the OLD_VERSION_NODE function tree to the new version. */
731 tree_function_versioning (old_decl, new_decl, tree_map, false, args_to_skip,
732 skip_return, bbs_to_copy, new_entry_block);
734 /* Update the new version's properties.
735 Make The new version visible only within this translation unit. Make sure
736 that is not weak also.
737 ??? We cannot use COMDAT linkage because there is no
738 ABI support for this. */
739 symtab_make_decl_local (new_version_node->symbol.decl);
740 DECL_VIRTUAL_P (new_version_node->symbol.decl) = 0;
741 new_version_node->symbol.externally_visible = 0;
742 new_version_node->local.local = 1;
743 new_version_node->lowered = true;
745 /* Update the call_expr on the edges to call the new version node. */
746 update_call_expr (new_version_node);
748 cgraph_call_function_insertion_hooks (new_version_node);
749 return new_version_node;
752 /* Given virtual clone, turn it into actual clone. */
754 static void
755 cgraph_materialize_clone (struct cgraph_node *node)
757 bitmap_obstack_initialize (NULL);
758 node->former_clone_of = node->clone_of->symbol.decl;
759 if (node->clone_of->former_clone_of)
760 node->former_clone_of = node->clone_of->former_clone_of;
761 /* Copy the OLD_VERSION_NODE function tree to the new version. */
762 tree_function_versioning (node->clone_of->symbol.decl, node->symbol.decl,
763 node->clone.tree_map, true,
764 node->clone.args_to_skip, false,
765 NULL, NULL);
766 if (cgraph_dump_file)
768 dump_function_to_file (node->clone_of->symbol.decl, cgraph_dump_file, dump_flags);
769 dump_function_to_file (node->symbol.decl, cgraph_dump_file, dump_flags);
772 /* Function is no longer clone. */
773 if (node->next_sibling_clone)
774 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
775 if (node->prev_sibling_clone)
776 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
777 else
778 node->clone_of->clones = node->next_sibling_clone;
779 node->next_sibling_clone = NULL;
780 node->prev_sibling_clone = NULL;
781 if (!node->clone_of->analyzed && !node->clone_of->clones)
783 cgraph_release_function_body (node->clone_of);
784 cgraph_node_remove_callees (node->clone_of);
785 ipa_remove_all_references (&node->clone_of->symbol.ref_list);
787 node->clone_of = NULL;
788 bitmap_obstack_release (NULL);
791 /* Once all functions from compilation unit are in memory, produce all clones
792 and update all calls. We might also do this on demand if we don't want to
793 bring all functions to memory prior compilation, but current WHOPR
794 implementation does that and it is is bit easier to keep everything right in
795 this order. */
797 void
798 cgraph_materialize_all_clones (void)
800 struct cgraph_node *node;
801 bool stabilized = false;
803 if (cgraph_dump_file)
804 fprintf (cgraph_dump_file, "Materializing clones\n");
805 #ifdef ENABLE_CHECKING
806 verify_cgraph ();
807 #endif
809 /* We can also do topological order, but number of iterations should be
810 bounded by number of IPA passes since single IPA pass is probably not
811 going to create clones of clones it created itself. */
812 while (!stabilized)
814 stabilized = true;
815 FOR_EACH_FUNCTION (node)
817 if (node->clone_of && node->symbol.decl != node->clone_of->symbol.decl
818 && !gimple_has_body_p (node->symbol.decl))
820 if (gimple_has_body_p (node->clone_of->symbol.decl))
822 if (cgraph_dump_file)
824 fprintf (cgraph_dump_file, "cloning %s to %s\n",
825 xstrdup (cgraph_node_name (node->clone_of)),
826 xstrdup (cgraph_node_name (node)));
827 if (node->clone.tree_map)
829 unsigned int i;
830 fprintf (cgraph_dump_file, " replace map: ");
831 for (i = 0;
832 i < vec_safe_length (node->clone.tree_map);
833 i++)
835 struct ipa_replace_map *replace_info;
836 replace_info = (*node->clone.tree_map)[i];
837 print_generic_expr (cgraph_dump_file, replace_info->old_tree, 0);
838 fprintf (cgraph_dump_file, " -> ");
839 print_generic_expr (cgraph_dump_file, replace_info->new_tree, 0);
840 fprintf (cgraph_dump_file, "%s%s;",
841 replace_info->replace_p ? "(replace)":"",
842 replace_info->ref_p ? "(ref)":"");
844 fprintf (cgraph_dump_file, "\n");
846 if (node->clone.args_to_skip)
848 fprintf (cgraph_dump_file, " args_to_skip: ");
849 dump_bitmap (cgraph_dump_file, node->clone.args_to_skip);
851 if (node->clone.args_to_skip)
853 fprintf (cgraph_dump_file, " combined_args_to_skip:");
854 dump_bitmap (cgraph_dump_file, node->clone.combined_args_to_skip);
857 cgraph_materialize_clone (node);
858 stabilized = false;
863 FOR_EACH_FUNCTION (node)
864 if (!node->analyzed && node->callees)
865 cgraph_node_remove_callees (node);
866 if (cgraph_dump_file)
867 fprintf (cgraph_dump_file, "Materialization Call site updates done.\n");
868 #ifdef ENABLE_CHECKING
869 verify_cgraph ();
870 #endif
871 symtab_remove_unreachable_nodes (false, cgraph_dump_file);
874 #include "gt-cgraphclones.h"