Implement -mmemcpy-strategy= and -mmemset-strategy= options
[official-gcc.git] / gcc / cgraphclones.c
blobae26a026d6453fdd4ed43dd632e4ee2d09b6a90b
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 gcov_type count = apply_probability (e->count, count_scale);
106 gcov_type freq;
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;
115 if (e->indirect_unknown_callee)
117 tree decl;
119 if (call_stmt && (decl = gimple_call_fndecl (call_stmt)))
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);
125 else
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;
133 else
135 new_edge = cgraph_create_edge (n, e->callee, call_stmt, count, freq);
136 if (e->indirect_info)
138 new_edge->indirect_info
139 = ggc_alloc_cleared_cgraph_indirect_call_info ();
140 *new_edge->indirect_info = *e->indirect_info;
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 new_edge->speculative = e->speculative;
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 If the new node is being inlined into another one, NEW_INLINED_TO should be
174 the outline function the new one is (even indirectly) inlined to. All hooks
175 will see this in node's global.inlined_to, when invoked. Can be NULL if the
176 node is not inlined. */
178 struct cgraph_node *
179 cgraph_clone_node (struct cgraph_node *n, tree decl, gcov_type count, int freq,
180 bool update_original,
181 vec<cgraph_edge_p> redirect_callers,
182 bool call_duplication_hook,
183 struct cgraph_node *new_inlined_to)
185 struct cgraph_node *new_node = cgraph_create_empty_node ();
186 struct cgraph_edge *e;
187 gcov_type count_scale;
188 unsigned i;
190 new_node->symbol.decl = decl;
191 symtab_register_node ((symtab_node)new_node);
192 new_node->origin = n->origin;
193 new_node->symbol.lto_file_data = n->symbol.lto_file_data;
194 if (new_node->origin)
196 new_node->next_nested = new_node->origin->nested;
197 new_node->origin->nested = new_node;
199 new_node->symbol.analyzed = n->symbol.analyzed;
200 new_node->symbol.definition = n->symbol.definition;
201 new_node->local = n->local;
202 new_node->symbol.externally_visible = false;
203 new_node->local.local = true;
204 new_node->global = n->global;
205 new_node->global.inlined_to = new_inlined_to;
206 new_node->rtl = n->rtl;
207 new_node->count = count;
208 new_node->frequency = n->frequency;
209 new_node->clone = n->clone;
210 new_node->clone.tree_map = NULL;
211 if (n->count)
213 if (new_node->count > n->count)
214 count_scale = REG_BR_PROB_BASE;
215 else
216 count_scale = GCOV_COMPUTE_SCALE (new_node->count, n->count);
218 else
219 count_scale = 0;
220 if (update_original)
222 n->count -= count;
223 if (n->count < 0)
224 n->count = 0;
227 FOR_EACH_VEC_ELT (redirect_callers, i, e)
229 /* Redirect calls to the old version node to point to its new
230 version. */
231 cgraph_redirect_edge_callee (e, new_node);
235 for (e = n->callees;e; e=e->next_callee)
236 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
237 count_scale, freq, update_original);
239 for (e = n->indirect_calls; e; e = e->next_callee)
240 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
241 count_scale, freq, update_original);
242 ipa_clone_references ((symtab_node)new_node, &n->symbol.ref_list);
244 new_node->next_sibling_clone = n->clones;
245 if (n->clones)
246 n->clones->prev_sibling_clone = new_node;
247 n->clones = new_node;
248 new_node->clone_of = n;
250 if (call_duplication_hook)
251 cgraph_call_node_duplication_hooks (n, new_node);
252 return new_node;
255 /* Create a new name for clone of DECL, add SUFFIX. Returns an identifier. */
257 static GTY(()) unsigned int clone_fn_id_num;
259 tree
260 clone_function_name (tree decl, const char *suffix)
262 tree name = DECL_ASSEMBLER_NAME (decl);
263 size_t len = IDENTIFIER_LENGTH (name);
264 char *tmp_name, *prefix;
266 prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
267 memcpy (prefix, IDENTIFIER_POINTER (name), len);
268 strcpy (prefix + len + 1, suffix);
269 #ifndef NO_DOT_IN_LABEL
270 prefix[len] = '.';
271 #elif !defined NO_DOLLAR_IN_LABEL
272 prefix[len] = '$';
273 #else
274 prefix[len] = '_';
275 #endif
276 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
277 return get_identifier (tmp_name);
280 /* Create callgraph node clone with new declaration. The actual body will
281 be copied later at compilation stage.
283 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
284 bitmap interface.
286 struct cgraph_node *
287 cgraph_create_virtual_clone (struct cgraph_node *old_node,
288 vec<cgraph_edge_p> redirect_callers,
289 vec<ipa_replace_map_p, va_gc> *tree_map,
290 bitmap args_to_skip,
291 const char * suffix)
293 tree old_decl = old_node->symbol.decl;
294 struct cgraph_node *new_node = NULL;
295 tree new_decl;
296 size_t i;
297 struct ipa_replace_map *map;
299 if (!in_lto_p)
300 gcc_checking_assert (tree_versionable_function_p (old_decl));
302 gcc_assert (old_node->local.can_change_signature || !args_to_skip);
304 /* Make a new FUNCTION_DECL tree node */
305 if (!args_to_skip)
306 new_decl = copy_node (old_decl);
307 else
308 new_decl = build_function_decl_skip_args (old_decl, args_to_skip, false);
310 /* These pointers represent function body and will be populated only when clone
311 is materialized. */
312 gcc_assert (new_decl != old_decl);
313 DECL_STRUCT_FUNCTION (new_decl) = NULL;
314 DECL_ARGUMENTS (new_decl) = NULL;
315 DECL_INITIAL (new_decl) = NULL;
316 DECL_RESULT (new_decl) = NULL;
317 /* We can not do DECL_RESULT (new_decl) = NULL; here because of LTO partitioning
318 sometimes storing only clone decl instead of original. */
320 /* Generate a new name for the new version. */
321 DECL_NAME (new_decl) = clone_function_name (old_decl, suffix);
322 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
323 SET_DECL_RTL (new_decl, NULL);
325 new_node = cgraph_clone_node (old_node, new_decl, old_node->count,
326 CGRAPH_FREQ_BASE, false,
327 redirect_callers, false, NULL);
328 /* Update the properties.
329 Make clone visible only within this translation unit. Make sure
330 that is not weak also.
331 ??? We cannot use COMDAT linkage because there is no
332 ABI support for this. */
333 DECL_EXTERNAL (new_node->symbol.decl) = 0;
334 if (DECL_ONE_ONLY (old_decl))
335 DECL_SECTION_NAME (new_node->symbol.decl) = NULL;
336 DECL_COMDAT_GROUP (new_node->symbol.decl) = 0;
337 TREE_PUBLIC (new_node->symbol.decl) = 0;
338 DECL_COMDAT (new_node->symbol.decl) = 0;
339 DECL_WEAK (new_node->symbol.decl) = 0;
340 DECL_VIRTUAL_P (new_node->symbol.decl) = 0;
341 DECL_STATIC_CONSTRUCTOR (new_node->symbol.decl) = 0;
342 DECL_STATIC_DESTRUCTOR (new_node->symbol.decl) = 0;
343 new_node->clone.tree_map = tree_map;
344 new_node->clone.args_to_skip = args_to_skip;
346 /* Clones of global symbols or symbols with unique names are unique. */
347 if ((TREE_PUBLIC (old_decl)
348 && !DECL_EXTERNAL (old_decl)
349 && !DECL_WEAK (old_decl)
350 && !DECL_COMDAT (old_decl))
351 || in_lto_p)
352 new_node->symbol.unique_name = true;
353 FOR_EACH_VEC_SAFE_ELT (tree_map, i, map)
354 ipa_maybe_record_reference ((symtab_node) new_node, map->new_tree,
355 IPA_REF_ADDR, NULL);
356 if (!args_to_skip)
357 new_node->clone.combined_args_to_skip = old_node->clone.combined_args_to_skip;
358 else if (old_node->clone.combined_args_to_skip)
360 int newi = 0, oldi = 0;
361 tree arg;
362 bitmap new_args_to_skip = BITMAP_GGC_ALLOC ();
363 struct cgraph_node *orig_node;
364 for (orig_node = old_node; orig_node->clone_of; orig_node = orig_node->clone_of)
366 for (arg = DECL_ARGUMENTS (orig_node->symbol.decl);
367 arg; arg = DECL_CHAIN (arg), oldi++)
369 if (bitmap_bit_p (old_node->clone.combined_args_to_skip, oldi))
371 bitmap_set_bit (new_args_to_skip, oldi);
372 continue;
374 if (bitmap_bit_p (args_to_skip, newi))
375 bitmap_set_bit (new_args_to_skip, oldi);
376 newi++;
378 new_node->clone.combined_args_to_skip = new_args_to_skip;
380 else
381 new_node->clone.combined_args_to_skip = args_to_skip;
382 new_node->symbol.externally_visible = 0;
383 new_node->local.local = 1;
384 new_node->lowered = true;
386 cgraph_call_node_duplication_hooks (old_node, new_node);
389 return new_node;
392 /* NODE is being removed from symbol table; see if its entry can be replaced by
393 other inline clone. */
394 struct cgraph_node *
395 cgraph_find_replacement_node (struct cgraph_node *node)
397 struct cgraph_node *next_inline_clone, *replacement;
399 for (next_inline_clone = node->clones;
400 next_inline_clone
401 && next_inline_clone->symbol.decl != node->symbol.decl;
402 next_inline_clone = next_inline_clone->next_sibling_clone)
405 /* If there is inline clone of the node being removed, we need
406 to put it into the position of removed node and reorganize all
407 other clones to be based on it. */
408 if (next_inline_clone)
410 struct cgraph_node *n;
411 struct cgraph_node *new_clones;
413 replacement = next_inline_clone;
415 /* Unlink inline clone from the list of clones of removed node. */
416 if (next_inline_clone->next_sibling_clone)
417 next_inline_clone->next_sibling_clone->prev_sibling_clone
418 = next_inline_clone->prev_sibling_clone;
419 if (next_inline_clone->prev_sibling_clone)
421 gcc_assert (node->clones != next_inline_clone);
422 next_inline_clone->prev_sibling_clone->next_sibling_clone
423 = next_inline_clone->next_sibling_clone;
425 else
427 gcc_assert (node->clones == next_inline_clone);
428 node->clones = next_inline_clone->next_sibling_clone;
431 new_clones = node->clones;
432 node->clones = NULL;
434 /* Copy clone info. */
435 next_inline_clone->clone = node->clone;
437 /* Now place it into clone tree at same level at NODE. */
438 next_inline_clone->clone_of = node->clone_of;
439 next_inline_clone->prev_sibling_clone = NULL;
440 next_inline_clone->next_sibling_clone = NULL;
441 if (node->clone_of)
443 if (node->clone_of->clones)
444 node->clone_of->clones->prev_sibling_clone = next_inline_clone;
445 next_inline_clone->next_sibling_clone = node->clone_of->clones;
446 node->clone_of->clones = next_inline_clone;
449 /* Merge the clone list. */
450 if (new_clones)
452 if (!next_inline_clone->clones)
453 next_inline_clone->clones = new_clones;
454 else
456 n = next_inline_clone->clones;
457 while (n->next_sibling_clone)
458 n = n->next_sibling_clone;
459 n->next_sibling_clone = new_clones;
460 new_clones->prev_sibling_clone = n;
464 /* Update clone_of pointers. */
465 n = new_clones;
466 while (n)
468 n->clone_of = next_inline_clone;
469 n = n->next_sibling_clone;
471 return replacement;
473 else
474 return NULL;
477 /* Like cgraph_set_call_stmt but walk the clone tree and update all
478 clones sharing the same function body.
479 When WHOLE_SPECULATIVE_EDGES is true, all three components of
480 speculative edge gets updated. Otherwise we update only direct
481 call. */
483 void
484 cgraph_set_call_stmt_including_clones (struct cgraph_node *orig,
485 gimple old_stmt, gimple new_stmt,
486 bool update_speculative)
488 struct cgraph_node *node;
489 struct cgraph_edge *edge = cgraph_edge (orig, old_stmt);
491 if (edge)
492 cgraph_set_call_stmt (edge, new_stmt, update_speculative);
494 node = orig->clones;
495 if (node)
496 while (node != orig)
498 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
499 if (edge)
501 cgraph_set_call_stmt (edge, new_stmt, update_speculative);
502 /* If UPDATE_SPECULATIVE is false, it means that we are turning
503 speculative call into a real code sequence. Update the
504 callgraph edges. */
505 if (edge->speculative && !update_speculative)
507 struct cgraph_edge *direct, *indirect;
508 struct ipa_ref *ref;
510 gcc_assert (!edge->indirect_unknown_callee);
511 cgraph_speculative_call_info (edge, direct, indirect, ref);
512 direct->speculative = false;
513 indirect->speculative = false;
514 ref->speculative = false;
517 if (node->clones)
518 node = node->clones;
519 else if (node->next_sibling_clone)
520 node = node->next_sibling_clone;
521 else
523 while (node != orig && !node->next_sibling_clone)
524 node = node->clone_of;
525 if (node != orig)
526 node = node->next_sibling_clone;
531 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
532 same function body. If clones already have edge for OLD_STMT; only
533 update the edge same way as cgraph_set_call_stmt_including_clones does.
535 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
536 frequencies of the clones. */
538 void
539 cgraph_create_edge_including_clones (struct cgraph_node *orig,
540 struct cgraph_node *callee,
541 gimple old_stmt,
542 gimple stmt, gcov_type count,
543 int freq,
544 cgraph_inline_failed_t reason)
546 struct cgraph_node *node;
547 struct cgraph_edge *edge;
549 if (!cgraph_edge (orig, stmt))
551 edge = cgraph_create_edge (orig, callee, stmt, count, freq);
552 edge->inline_failed = reason;
555 node = orig->clones;
556 if (node)
557 while (node != orig)
559 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
561 /* It is possible that clones already contain the edge while
562 master didn't. Either we promoted indirect call into direct
563 call in the clone or we are processing clones of unreachable
564 master where edges has been removed. */
565 if (edge)
566 cgraph_set_call_stmt (edge, stmt);
567 else if (!cgraph_edge (node, stmt))
569 edge = cgraph_create_edge (node, callee, stmt, count,
570 freq);
571 edge->inline_failed = reason;
574 if (node->clones)
575 node = node->clones;
576 else if (node->next_sibling_clone)
577 node = node->next_sibling_clone;
578 else
580 while (node != orig && !node->next_sibling_clone)
581 node = node->clone_of;
582 if (node != orig)
583 node = node->next_sibling_clone;
588 /* Remove the node from cgraph and all inline clones inlined into it.
589 Skip however removal of FORBIDDEN_NODE and return true if it needs to be
590 removed. This allows to call the function from outer loop walking clone
591 tree. */
593 bool
594 cgraph_remove_node_and_inline_clones (struct cgraph_node *node, struct cgraph_node *forbidden_node)
596 struct cgraph_edge *e, *next;
597 bool found = false;
599 if (node == forbidden_node)
601 cgraph_remove_edge (node->callers);
602 return true;
604 for (e = node->callees; e; e = next)
606 next = e->next_callee;
607 if (!e->inline_failed)
608 found |= cgraph_remove_node_and_inline_clones (e->callee, forbidden_node);
610 cgraph_remove_node (node);
611 return found;
614 /* The edges representing the callers of the NEW_VERSION node were
615 fixed by cgraph_function_versioning (), now the call_expr in their
616 respective tree code should be updated to call the NEW_VERSION. */
618 static void
619 update_call_expr (struct cgraph_node *new_version)
621 struct cgraph_edge *e;
623 gcc_assert (new_version);
625 /* Update the call expr on the edges to call the new version. */
626 for (e = new_version->callers; e; e = e->next_caller)
628 struct function *inner_function = DECL_STRUCT_FUNCTION (e->caller->symbol.decl);
629 gimple_call_set_fndecl (e->call_stmt, new_version->symbol.decl);
630 maybe_clean_eh_stmt_fn (inner_function, e->call_stmt);
635 /* Create a new cgraph node which is the new version of
636 OLD_VERSION node. REDIRECT_CALLERS holds the callers
637 edges which should be redirected to point to
638 NEW_VERSION. ALL the callees edges of OLD_VERSION
639 are cloned to the new version node. Return the new
640 version node.
642 If non-NULL BLOCK_TO_COPY determine what basic blocks
643 was copied to prevent duplications of calls that are dead
644 in the clone. */
646 struct cgraph_node *
647 cgraph_copy_node_for_versioning (struct cgraph_node *old_version,
648 tree new_decl,
649 vec<cgraph_edge_p> redirect_callers,
650 bitmap bbs_to_copy)
652 struct cgraph_node *new_version;
653 struct cgraph_edge *e;
654 unsigned i;
656 gcc_assert (old_version);
658 new_version = cgraph_create_node (new_decl);
660 new_version->symbol.analyzed = old_version->symbol.analyzed;
661 new_version->symbol.definition = old_version->symbol.definition;
662 new_version->local = old_version->local;
663 new_version->symbol.externally_visible = false;
664 new_version->local.local = new_version->symbol.definition;
665 new_version->global = old_version->global;
666 new_version->rtl = old_version->rtl;
667 new_version->count = old_version->count;
669 for (e = old_version->callees; e; e=e->next_callee)
670 if (!bbs_to_copy
671 || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
672 cgraph_clone_edge (e, new_version, e->call_stmt,
673 e->lto_stmt_uid, REG_BR_PROB_BASE,
674 CGRAPH_FREQ_BASE,
675 true);
676 for (e = old_version->indirect_calls; e; e=e->next_callee)
677 if (!bbs_to_copy
678 || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
679 cgraph_clone_edge (e, new_version, e->call_stmt,
680 e->lto_stmt_uid, REG_BR_PROB_BASE,
681 CGRAPH_FREQ_BASE,
682 true);
683 FOR_EACH_VEC_ELT (redirect_callers, i, e)
685 /* Redirect calls to the old version node to point to its new
686 version. */
687 cgraph_redirect_edge_callee (e, new_version);
690 cgraph_call_node_duplication_hooks (old_version, new_version);
692 return new_version;
695 /* Perform function versioning.
696 Function versioning includes copying of the tree and
697 a callgraph update (creating a new cgraph node and updating
698 its callees and callers).
700 REDIRECT_CALLERS varray includes the edges to be redirected
701 to the new version.
703 TREE_MAP is a mapping of tree nodes we want to replace with
704 new ones (according to results of prior analysis).
705 OLD_VERSION_NODE is the node that is versioned.
707 If non-NULL ARGS_TO_SKIP determine function parameters to remove
708 from new version.
709 If SKIP_RETURN is true, the new version will return void.
710 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
711 If non_NULL NEW_ENTRY determine new entry BB of the clone.
713 Return the new version's cgraph node. */
715 struct cgraph_node *
716 cgraph_function_versioning (struct cgraph_node *old_version_node,
717 vec<cgraph_edge_p> redirect_callers,
718 vec<ipa_replace_map_p, va_gc> *tree_map,
719 bitmap args_to_skip,
720 bool skip_return,
721 bitmap bbs_to_copy,
722 basic_block new_entry_block,
723 const char *clone_name)
725 tree old_decl = old_version_node->symbol.decl;
726 struct cgraph_node *new_version_node = NULL;
727 tree new_decl;
729 if (!tree_versionable_function_p (old_decl))
730 return NULL;
732 gcc_assert (old_version_node->local.can_change_signature || !args_to_skip);
734 /* Make a new FUNCTION_DECL tree node for the new version. */
735 if (!args_to_skip && !skip_return)
736 new_decl = copy_node (old_decl);
737 else
738 new_decl
739 = build_function_decl_skip_args (old_decl, args_to_skip, skip_return);
741 /* Generate a new name for the new version. */
742 DECL_NAME (new_decl) = clone_function_name (old_decl, clone_name);
743 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
744 SET_DECL_RTL (new_decl, NULL);
746 /* When the old decl was a con-/destructor make sure the clone isn't. */
747 DECL_STATIC_CONSTRUCTOR(new_decl) = 0;
748 DECL_STATIC_DESTRUCTOR(new_decl) = 0;
750 /* Create the new version's call-graph node.
751 and update the edges of the new node. */
752 new_version_node =
753 cgraph_copy_node_for_versioning (old_version_node, new_decl,
754 redirect_callers, bbs_to_copy);
756 /* Copy the OLD_VERSION_NODE function tree to the new version. */
757 tree_function_versioning (old_decl, new_decl, tree_map, false, args_to_skip,
758 skip_return, bbs_to_copy, new_entry_block);
760 /* Update the new version's properties.
761 Make The new version visible only within this translation unit. Make sure
762 that is not weak also.
763 ??? We cannot use COMDAT linkage because there is no
764 ABI support for this. */
765 symtab_make_decl_local (new_version_node->symbol.decl);
766 DECL_VIRTUAL_P (new_version_node->symbol.decl) = 0;
767 new_version_node->symbol.externally_visible = 0;
768 new_version_node->local.local = 1;
769 new_version_node->lowered = true;
770 /* Clones of global symbols or symbols with unique names are unique. */
771 if ((TREE_PUBLIC (old_decl)
772 && !DECL_EXTERNAL (old_decl)
773 && !DECL_WEAK (old_decl)
774 && !DECL_COMDAT (old_decl))
775 || in_lto_p)
776 new_version_node->symbol.unique_name = true;
778 /* Update the call_expr on the edges to call the new version node. */
779 update_call_expr (new_version_node);
781 cgraph_call_function_insertion_hooks (new_version_node);
782 return new_version_node;
785 /* Given virtual clone, turn it into actual clone. */
787 static void
788 cgraph_materialize_clone (struct cgraph_node *node)
790 bitmap_obstack_initialize (NULL);
791 node->former_clone_of = node->clone_of->symbol.decl;
792 if (node->clone_of->former_clone_of)
793 node->former_clone_of = node->clone_of->former_clone_of;
794 /* Copy the OLD_VERSION_NODE function tree to the new version. */
795 tree_function_versioning (node->clone_of->symbol.decl, node->symbol.decl,
796 node->clone.tree_map, true,
797 node->clone.args_to_skip, false,
798 NULL, NULL);
799 if (cgraph_dump_file)
801 dump_function_to_file (node->clone_of->symbol.decl, cgraph_dump_file, dump_flags);
802 dump_function_to_file (node->symbol.decl, cgraph_dump_file, dump_flags);
805 /* Function is no longer clone. */
806 if (node->next_sibling_clone)
807 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
808 if (node->prev_sibling_clone)
809 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
810 else
811 node->clone_of->clones = node->next_sibling_clone;
812 node->next_sibling_clone = NULL;
813 node->prev_sibling_clone = NULL;
814 if (!node->clone_of->symbol.analyzed && !node->clone_of->clones)
816 cgraph_release_function_body (node->clone_of);
817 cgraph_node_remove_callees (node->clone_of);
818 ipa_remove_all_references (&node->clone_of->symbol.ref_list);
820 node->clone_of = NULL;
821 bitmap_obstack_release (NULL);
824 /* Once all functions from compilation unit are in memory, produce all clones
825 and update all calls. We might also do this on demand if we don't want to
826 bring all functions to memory prior compilation, but current WHOPR
827 implementation does that and it is is bit easier to keep everything right in
828 this order. */
830 void
831 cgraph_materialize_all_clones (void)
833 struct cgraph_node *node;
834 bool stabilized = false;
837 if (cgraph_dump_file)
838 fprintf (cgraph_dump_file, "Materializing clones\n");
839 #ifdef ENABLE_CHECKING
840 verify_cgraph ();
841 #endif
843 /* We can also do topological order, but number of iterations should be
844 bounded by number of IPA passes since single IPA pass is probably not
845 going to create clones of clones it created itself. */
846 while (!stabilized)
848 stabilized = true;
849 FOR_EACH_FUNCTION (node)
851 if (node->clone_of && node->symbol.decl != node->clone_of->symbol.decl
852 && !gimple_has_body_p (node->symbol.decl))
854 if (!node->clone_of->clone_of)
855 cgraph_get_body (node->clone_of);
856 if (gimple_has_body_p (node->clone_of->symbol.decl))
858 if (cgraph_dump_file)
860 fprintf (cgraph_dump_file, "cloning %s to %s\n",
861 xstrdup (cgraph_node_name (node->clone_of)),
862 xstrdup (cgraph_node_name (node)));
863 if (node->clone.tree_map)
865 unsigned int i;
866 fprintf (cgraph_dump_file, " replace map: ");
867 for (i = 0;
868 i < vec_safe_length (node->clone.tree_map);
869 i++)
871 struct ipa_replace_map *replace_info;
872 replace_info = (*node->clone.tree_map)[i];
873 print_generic_expr (cgraph_dump_file, replace_info->old_tree, 0);
874 fprintf (cgraph_dump_file, " -> ");
875 print_generic_expr (cgraph_dump_file, replace_info->new_tree, 0);
876 fprintf (cgraph_dump_file, "%s%s;",
877 replace_info->replace_p ? "(replace)":"",
878 replace_info->ref_p ? "(ref)":"");
880 fprintf (cgraph_dump_file, "\n");
882 if (node->clone.args_to_skip)
884 fprintf (cgraph_dump_file, " args_to_skip: ");
885 dump_bitmap (cgraph_dump_file, node->clone.args_to_skip);
887 if (node->clone.args_to_skip)
889 fprintf (cgraph_dump_file, " combined_args_to_skip:");
890 dump_bitmap (cgraph_dump_file, node->clone.combined_args_to_skip);
893 cgraph_materialize_clone (node);
894 stabilized = false;
899 FOR_EACH_FUNCTION (node)
900 if (!node->symbol.analyzed && node->callees)
902 cgraph_node_remove_callees (node);
903 ipa_remove_all_references (&node->symbol.ref_list);
905 else
906 ipa_clear_stmts_in_references ((symtab_node)node);
907 if (cgraph_dump_file)
908 fprintf (cgraph_dump_file, "Materialization Call site updates done.\n");
909 #ifdef ENABLE_CHECKING
910 verify_cgraph ();
911 #endif
912 symtab_remove_unreachable_nodes (false, cgraph_dump_file);
915 #include "gt-cgraphclones.h"