* lib/ubsan-dg.exp (check_effective_target_fsanitize_undefined):
[official-gcc.git] / gcc / cgraphclones.c
blob77d0b1181582f3326006dbfdb59b4e59c906ee1a
1 /* Callgraph clones
2 Copyright (C) 2003-2014 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 "rtl.h"
72 #include "tree.h"
73 #include "stringpool.h"
74 #include "hashtab.h"
75 #include "hash-set.h"
76 #include "vec.h"
77 #include "machmode.h"
78 #include "hard-reg-set.h"
79 #include "input.h"
80 #include "function.h"
81 #include "emit-rtl.h"
82 #include "predict.h"
83 #include "basic-block.h"
84 #include "tree-ssa-alias.h"
85 #include "internal-fn.h"
86 #include "tree-eh.h"
87 #include "gimple-expr.h"
88 #include "is-a.h"
89 #include "gimple.h"
90 #include "bitmap.h"
91 #include "tree-cfg.h"
92 #include "tree-inline.h"
93 #include "langhooks.h"
94 #include "toplev.h"
95 #include "flags.h"
96 #include "debug.h"
97 #include "target.h"
98 #include "diagnostic.h"
99 #include "params.h"
100 #include "intl.h"
101 #include "hash-map.h"
102 #include "plugin-api.h"
103 #include "ipa-ref.h"
104 #include "cgraph.h"
105 #include "alloc-pool.h"
106 #include "symbol-summary.h"
107 #include "ipa-prop.h"
108 #include "tree-iterator.h"
109 #include "tree-dump.h"
110 #include "gimple-pretty-print.h"
111 #include "coverage.h"
112 #include "ipa-inline.h"
113 #include "ipa-utils.h"
114 #include "lto-streamer.h"
115 #include "except.h"
117 /* Create clone of edge in the node N represented by CALL_EXPR
118 the callgraph. */
120 cgraph_edge *
121 cgraph_edge::clone (cgraph_node *n, gcall *call_stmt, unsigned stmt_uid,
122 gcov_type count_scale, int freq_scale, bool update_original)
124 cgraph_edge *new_edge;
125 gcov_type gcov_count = apply_probability (count, count_scale);
126 gcov_type freq;
128 /* We do not want to ignore loop nest after frequency drops to 0. */
129 if (!freq_scale)
130 freq_scale = 1;
131 freq = frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
132 if (freq > CGRAPH_FREQ_MAX)
133 freq = CGRAPH_FREQ_MAX;
135 if (indirect_unknown_callee)
137 tree decl;
139 if (call_stmt && (decl = gimple_call_fndecl (call_stmt))
140 /* When the call is speculative, we need to resolve it
141 via cgraph_resolve_speculation and not here. */
142 && !speculative)
144 cgraph_node *callee = cgraph_node::get (decl);
145 gcc_checking_assert (callee);
146 new_edge = n->create_edge (callee, call_stmt, gcov_count, freq);
148 else
150 new_edge = n->create_indirect_edge (call_stmt,
151 indirect_info->ecf_flags,
152 count, freq, false);
153 *new_edge->indirect_info = *indirect_info;
156 else
158 new_edge = n->create_edge (callee, call_stmt, gcov_count, freq);
159 if (indirect_info)
161 new_edge->indirect_info
162 = ggc_cleared_alloc<cgraph_indirect_call_info> ();
163 *new_edge->indirect_info = *indirect_info;
167 new_edge->inline_failed = inline_failed;
168 new_edge->indirect_inlining_edge = indirect_inlining_edge;
169 new_edge->lto_stmt_uid = stmt_uid;
170 /* Clone flags that depend on call_stmt availability manually. */
171 new_edge->can_throw_external = can_throw_external;
172 new_edge->call_stmt_cannot_inline_p = call_stmt_cannot_inline_p;
173 new_edge->speculative = speculative;
174 new_edge->in_polymorphic_cdtor = in_polymorphic_cdtor;
175 if (update_original)
177 count -= new_edge->count;
178 if (count < 0)
179 count = 0;
181 symtab->call_edge_duplication_hooks (this, new_edge);
182 return new_edge;
185 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP and the
186 return value if SKIP_RETURN is true. */
188 static tree
189 build_function_type_skip_args (tree orig_type, bitmap args_to_skip,
190 bool skip_return)
192 tree new_type = NULL;
193 tree args, new_args = NULL;
194 tree new_reversed;
195 int i = 0;
197 for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
198 args = TREE_CHAIN (args), i++)
199 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
200 new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
202 new_reversed = nreverse (new_args);
203 if (args)
205 if (new_reversed)
206 TREE_CHAIN (new_args) = void_list_node;
207 else
208 new_reversed = void_list_node;
211 /* Use copy_node to preserve as much as possible from original type
212 (debug info, attribute lists etc.)
213 Exception is METHOD_TYPEs must have THIS argument.
214 When we are asked to remove it, we need to build new FUNCTION_TYPE
215 instead. */
216 if (TREE_CODE (orig_type) != METHOD_TYPE
217 || !args_to_skip
218 || !bitmap_bit_p (args_to_skip, 0))
220 new_type = build_distinct_type_copy (orig_type);
221 TYPE_ARG_TYPES (new_type) = new_reversed;
223 else
225 new_type
226 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
227 new_reversed));
228 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
231 if (skip_return)
232 TREE_TYPE (new_type) = void_type_node;
234 return new_type;
237 /* Build variant of function decl ORIG_DECL skipping ARGS_TO_SKIP and the
238 return value if SKIP_RETURN is true.
240 Arguments from DECL_ARGUMENTS list can't be removed now, since they are
241 linked by TREE_CHAIN directly. The caller is responsible for eliminating
242 them when they are being duplicated (i.e. copy_arguments_for_versioning). */
244 static tree
245 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip,
246 bool skip_return)
248 tree new_decl = copy_node (orig_decl);
249 tree new_type;
251 new_type = TREE_TYPE (orig_decl);
252 if (prototype_p (new_type)
253 || (skip_return && !VOID_TYPE_P (TREE_TYPE (new_type))))
254 new_type
255 = build_function_type_skip_args (new_type, args_to_skip, skip_return);
256 TREE_TYPE (new_decl) = new_type;
258 /* For declarations setting DECL_VINDEX (i.e. methods)
259 we expect first argument to be THIS pointer. */
260 if (args_to_skip && bitmap_bit_p (args_to_skip, 0))
261 DECL_VINDEX (new_decl) = NULL_TREE;
263 /* When signature changes, we need to clear builtin info. */
264 if (DECL_BUILT_IN (new_decl)
265 && args_to_skip
266 && !bitmap_empty_p (args_to_skip))
268 DECL_BUILT_IN_CLASS (new_decl) = NOT_BUILT_IN;
269 DECL_FUNCTION_CODE (new_decl) = (enum built_in_function) 0;
271 /* The FE might have information and assumptions about the other
272 arguments. */
273 DECL_LANG_SPECIFIC (new_decl) = NULL;
274 return new_decl;
277 /* Set flags of NEW_NODE and its decl. NEW_NODE is a newly created private
278 clone or its thunk. */
280 static void
281 set_new_clone_decl_and_node_flags (cgraph_node *new_node)
283 DECL_EXTERNAL (new_node->decl) = 0;
284 TREE_PUBLIC (new_node->decl) = 0;
285 DECL_COMDAT (new_node->decl) = 0;
286 DECL_WEAK (new_node->decl) = 0;
287 DECL_VIRTUAL_P (new_node->decl) = 0;
288 DECL_STATIC_CONSTRUCTOR (new_node->decl) = 0;
289 DECL_STATIC_DESTRUCTOR (new_node->decl) = 0;
291 new_node->externally_visible = 0;
292 new_node->local.local = 1;
293 new_node->lowered = true;
296 /* Duplicate thunk THUNK if necessary but make it to refer to NODE.
297 ARGS_TO_SKIP, if non-NULL, determines which parameters should be omitted.
298 Function can return NODE if no thunk is necessary, which can happen when
299 thunk is this_adjusting but we are removing this parameter. */
301 static cgraph_node *
302 duplicate_thunk_for_node (cgraph_node *thunk, cgraph_node *node)
304 cgraph_node *new_thunk, *thunk_of;
305 thunk_of = thunk->callees->callee->ultimate_alias_target ();
307 if (thunk_of->thunk.thunk_p)
308 node = duplicate_thunk_for_node (thunk_of, node);
310 if (!DECL_ARGUMENTS (thunk->decl))
311 thunk->get_untransformed_body ();
313 cgraph_edge *cs;
314 for (cs = node->callers; cs; cs = cs->next_caller)
315 if (cs->caller->thunk.thunk_p
316 && cs->caller->thunk.this_adjusting == thunk->thunk.this_adjusting
317 && cs->caller->thunk.fixed_offset == thunk->thunk.fixed_offset
318 && cs->caller->thunk.virtual_offset_p == thunk->thunk.virtual_offset_p
319 && cs->caller->thunk.virtual_value == thunk->thunk.virtual_value)
320 return cs->caller;
322 tree new_decl;
323 if (!node->clone.args_to_skip)
324 new_decl = copy_node (thunk->decl);
325 else
327 /* We do not need to duplicate this_adjusting thunks if we have removed
328 this. */
329 if (thunk->thunk.this_adjusting
330 && bitmap_bit_p (node->clone.args_to_skip, 0))
331 return node;
333 new_decl = build_function_decl_skip_args (thunk->decl,
334 node->clone.args_to_skip,
335 false);
338 tree *link = &DECL_ARGUMENTS (new_decl);
339 int i = 0;
340 for (tree pd = DECL_ARGUMENTS (thunk->decl); pd; pd = DECL_CHAIN (pd), i++)
342 if (!node->clone.args_to_skip
343 || !bitmap_bit_p (node->clone.args_to_skip, i))
345 tree nd = copy_node (pd);
346 DECL_CONTEXT (nd) = new_decl;
347 *link = nd;
348 link = &DECL_CHAIN (nd);
351 *link = NULL_TREE;
353 gcc_checking_assert (!DECL_STRUCT_FUNCTION (new_decl));
354 gcc_checking_assert (!DECL_INITIAL (new_decl));
355 gcc_checking_assert (!DECL_RESULT (new_decl));
356 gcc_checking_assert (!DECL_RTL_SET_P (new_decl));
358 DECL_NAME (new_decl) = clone_function_name (thunk->decl, "artificial_thunk");
359 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
361 new_thunk = cgraph_node::create (new_decl);
362 set_new_clone_decl_and_node_flags (new_thunk);
363 new_thunk->definition = true;
364 new_thunk->thunk = thunk->thunk;
365 new_thunk->unique_name = in_lto_p;
366 new_thunk->former_clone_of = thunk->decl;
367 new_thunk->clone.args_to_skip = node->clone.args_to_skip;
368 new_thunk->clone.combined_args_to_skip = node->clone.combined_args_to_skip;
370 cgraph_edge *e = new_thunk->create_edge (node, NULL, 0,
371 CGRAPH_FREQ_BASE);
372 e->call_stmt_cannot_inline_p = true;
373 symtab->call_edge_duplication_hooks (thunk->callees, e);
374 symtab->call_cgraph_duplication_hooks (thunk, new_thunk);
375 return new_thunk;
378 /* If E does not lead to a thunk, simply redirect it to N. Otherwise create
379 one or more equivalent thunks for N and redirect E to the first in the
380 chain. Note that it is then necessary to call
381 n->expand_all_artificial_thunks once all callers are redirected. */
383 void
384 cgraph_edge::redirect_callee_duplicating_thunks (cgraph_node *n)
386 cgraph_node *orig_to = callee->ultimate_alias_target ();
387 if (orig_to->thunk.thunk_p)
388 n = duplicate_thunk_for_node (orig_to, n);
390 redirect_callee (n);
393 /* Call expand_thunk on all callers that are thunks and if analyze those nodes
394 that were expanded. */
396 void
397 cgraph_node::expand_all_artificial_thunks ()
399 cgraph_edge *e;
400 for (e = callers; e;)
401 if (e->caller->thunk.thunk_p)
403 cgraph_node *thunk = e->caller;
405 e = e->next_caller;
406 if (thunk->expand_thunk (false, false))
408 thunk->thunk.thunk_p = false;
409 thunk->analyze ();
411 thunk->expand_all_artificial_thunks ();
413 else
414 e = e->next_caller;
417 /* Create node representing clone of N executed COUNT times. Decrease
418 the execution counts from original node too.
419 The new clone will have decl set to DECL that may or may not be the same
420 as decl of N.
422 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
423 function's profile to reflect the fact that part of execution is handled
424 by node.
425 When CALL_DUPLICATOIN_HOOK is true, the ipa passes are acknowledged about
426 the new clone. Otherwise the caller is responsible for doing so later.
428 If the new node is being inlined into another one, NEW_INLINED_TO should be
429 the outline function the new one is (even indirectly) inlined to. All hooks
430 will see this in node's global.inlined_to, when invoked. Can be NULL if the
431 node is not inlined. */
433 cgraph_node *
434 cgraph_node::create_clone (tree decl, gcov_type gcov_count, int freq,
435 bool update_original,
436 vec<cgraph_edge *> redirect_callers,
437 bool call_duplication_hook,
438 cgraph_node *new_inlined_to,
439 bitmap args_to_skip)
441 cgraph_node *new_node = symtab->create_empty ();
442 cgraph_edge *e;
443 gcov_type count_scale;
444 unsigned i;
446 new_node->decl = decl;
447 new_node->register_symbol ();
448 new_node->origin = origin;
449 new_node->lto_file_data = lto_file_data;
450 if (new_node->origin)
452 new_node->next_nested = new_node->origin->nested;
453 new_node->origin->nested = new_node;
455 new_node->analyzed = analyzed;
456 new_node->definition = definition;
457 new_node->local = local;
458 new_node->externally_visible = false;
459 new_node->no_reorder = no_reorder;
460 new_node->local.local = true;
461 new_node->global = global;
462 new_node->global.inlined_to = new_inlined_to;
463 new_node->rtl = rtl;
464 new_node->count = count;
465 new_node->frequency = frequency;
466 new_node->tp_first_run = tp_first_run;
467 new_node->tm_clone = tm_clone;
469 new_node->clone.tree_map = NULL;
470 new_node->clone.args_to_skip = args_to_skip;
471 if (!args_to_skip)
472 new_node->clone.combined_args_to_skip = clone.combined_args_to_skip;
473 else if (clone.combined_args_to_skip)
475 new_node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
476 bitmap_ior (new_node->clone.combined_args_to_skip,
477 clone.combined_args_to_skip, args_to_skip);
479 else
480 new_node->clone.combined_args_to_skip = args_to_skip;
482 if (count)
484 if (new_node->count > count)
485 count_scale = REG_BR_PROB_BASE;
486 else
487 count_scale = GCOV_COMPUTE_SCALE (new_node->count, count);
489 else
490 count_scale = 0;
491 if (update_original)
493 count -= gcov_count;
494 if (count < 0)
495 count = 0;
498 FOR_EACH_VEC_ELT (redirect_callers, i, e)
500 /* Redirect calls to the old version node to point to its new
501 version. The only exception is when the edge was proved to
502 be unreachable during the clonning procedure. */
503 if (!e->callee
504 || DECL_BUILT_IN_CLASS (e->callee->decl) != BUILT_IN_NORMAL
505 || DECL_FUNCTION_CODE (e->callee->decl) != BUILT_IN_UNREACHABLE)
506 e->redirect_callee_duplicating_thunks (new_node);
508 new_node->expand_all_artificial_thunks ();
510 for (e = callees;e; e=e->next_callee)
511 e->clone (new_node, e->call_stmt, e->lto_stmt_uid, count_scale,
512 freq, update_original);
514 for (e = indirect_calls; e; e = e->next_callee)
515 e->clone (new_node, e->call_stmt, e->lto_stmt_uid,
516 count_scale, freq, update_original);
517 new_node->clone_references (this);
519 new_node->next_sibling_clone = clones;
520 if (clones)
521 clones->prev_sibling_clone = new_node;
522 clones = new_node;
523 new_node->clone_of = this;
525 if (call_duplication_hook)
526 symtab->call_cgraph_duplication_hooks (this, new_node);
527 return new_node;
530 /* Return a new assembler name for a clone of DECL with SUFFIX. */
532 static GTY(()) unsigned int clone_fn_id_num;
534 tree
535 clone_function_name (tree decl, const char *suffix)
537 tree name = DECL_ASSEMBLER_NAME (decl);
538 size_t len = IDENTIFIER_LENGTH (name);
539 char *tmp_name, *prefix;
541 prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
542 memcpy (prefix, IDENTIFIER_POINTER (name), len);
543 strcpy (prefix + len + 1, suffix);
544 #ifndef NO_DOT_IN_LABEL
545 prefix[len] = '.';
546 #elif !defined NO_DOLLAR_IN_LABEL
547 prefix[len] = '$';
548 #else
549 prefix[len] = '_';
550 #endif
551 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
552 return get_identifier (tmp_name);
555 /* Create callgraph node clone with new declaration. The actual body will
556 be copied later at compilation stage.
558 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
559 bitmap interface.
561 cgraph_node *
562 cgraph_node::create_virtual_clone (vec<cgraph_edge *> redirect_callers,
563 vec<ipa_replace_map *, va_gc> *tree_map,
564 bitmap args_to_skip, const char * suffix)
566 tree old_decl = decl;
567 cgraph_node *new_node = NULL;
568 tree new_decl;
569 size_t len, i;
570 ipa_replace_map *map;
571 char *name;
573 if (!in_lto_p)
574 gcc_checking_assert (tree_versionable_function_p (old_decl));
576 gcc_assert (local.can_change_signature || !args_to_skip);
578 /* Make a new FUNCTION_DECL tree node */
579 if (!args_to_skip)
580 new_decl = copy_node (old_decl);
581 else
582 new_decl = build_function_decl_skip_args (old_decl, args_to_skip, false);
584 /* These pointers represent function body and will be populated only when clone
585 is materialized. */
586 gcc_assert (new_decl != old_decl);
587 DECL_STRUCT_FUNCTION (new_decl) = NULL;
588 DECL_ARGUMENTS (new_decl) = NULL;
589 DECL_INITIAL (new_decl) = NULL;
590 DECL_RESULT (new_decl) = NULL;
591 /* We can not do DECL_RESULT (new_decl) = NULL; here because of LTO partitioning
592 sometimes storing only clone decl instead of original. */
594 /* Generate a new name for the new version. */
595 len = IDENTIFIER_LENGTH (DECL_NAME (old_decl));
596 name = XALLOCAVEC (char, len + strlen (suffix) + 2);
597 memcpy (name, IDENTIFIER_POINTER (DECL_NAME (old_decl)), len);
598 strcpy (name + len + 1, suffix);
599 name[len] = '.';
600 DECL_NAME (new_decl) = get_identifier (name);
601 SET_DECL_ASSEMBLER_NAME (new_decl, clone_function_name (old_decl, suffix));
602 SET_DECL_RTL (new_decl, NULL);
604 new_node = create_clone (new_decl, count, CGRAPH_FREQ_BASE, false,
605 redirect_callers, false, NULL, args_to_skip);
607 /* Update the properties.
608 Make clone visible only within this translation unit. Make sure
609 that is not weak also.
610 ??? We cannot use COMDAT linkage because there is no
611 ABI support for this. */
612 set_new_clone_decl_and_node_flags (new_node);
613 new_node->clone.tree_map = tree_map;
615 /* Clones of global symbols or symbols with unique names are unique. */
616 if ((TREE_PUBLIC (old_decl)
617 && !DECL_EXTERNAL (old_decl)
618 && !DECL_WEAK (old_decl)
619 && !DECL_COMDAT (old_decl))
620 || in_lto_p)
621 new_node->unique_name = true;
622 FOR_EACH_VEC_SAFE_ELT (tree_map, i, map)
623 new_node->maybe_create_reference (map->new_tree, IPA_REF_ADDR, NULL);
625 if (ipa_transforms_to_apply.exists ())
626 new_node->ipa_transforms_to_apply
627 = ipa_transforms_to_apply.copy ();
629 symtab->call_cgraph_duplication_hooks (this, new_node);
631 return new_node;
634 /* callgraph node being removed from symbol table; see if its entry can be
635 replaced by other inline clone. */
636 cgraph_node *
637 cgraph_node::find_replacement (void)
639 cgraph_node *next_inline_clone, *replacement;
641 for (next_inline_clone = clones;
642 next_inline_clone
643 && next_inline_clone->decl != decl;
644 next_inline_clone = next_inline_clone->next_sibling_clone)
647 /* If there is inline clone of the node being removed, we need
648 to put it into the position of removed node and reorganize all
649 other clones to be based on it. */
650 if (next_inline_clone)
652 cgraph_node *n;
653 cgraph_node *new_clones;
655 replacement = next_inline_clone;
657 /* Unlink inline clone from the list of clones of removed node. */
658 if (next_inline_clone->next_sibling_clone)
659 next_inline_clone->next_sibling_clone->prev_sibling_clone
660 = next_inline_clone->prev_sibling_clone;
661 if (next_inline_clone->prev_sibling_clone)
663 gcc_assert (clones != next_inline_clone);
664 next_inline_clone->prev_sibling_clone->next_sibling_clone
665 = next_inline_clone->next_sibling_clone;
667 else
669 gcc_assert (clones == next_inline_clone);
670 clones = next_inline_clone->next_sibling_clone;
673 new_clones = clones;
674 clones = NULL;
676 /* Copy clone info. */
677 next_inline_clone->clone = clone;
679 /* Now place it into clone tree at same level at NODE. */
680 next_inline_clone->clone_of = clone_of;
681 next_inline_clone->prev_sibling_clone = NULL;
682 next_inline_clone->next_sibling_clone = NULL;
683 if (clone_of)
685 if (clone_of->clones)
686 clone_of->clones->prev_sibling_clone = next_inline_clone;
687 next_inline_clone->next_sibling_clone = clone_of->clones;
688 clone_of->clones = next_inline_clone;
691 /* Merge the clone list. */
692 if (new_clones)
694 if (!next_inline_clone->clones)
695 next_inline_clone->clones = new_clones;
696 else
698 n = next_inline_clone->clones;
699 while (n->next_sibling_clone)
700 n = n->next_sibling_clone;
701 n->next_sibling_clone = new_clones;
702 new_clones->prev_sibling_clone = n;
706 /* Update clone_of pointers. */
707 n = new_clones;
708 while (n)
710 n->clone_of = next_inline_clone;
711 n = n->next_sibling_clone;
713 return replacement;
715 else
716 return NULL;
719 /* Like cgraph_set_call_stmt but walk the clone tree and update all
720 clones sharing the same function body.
721 When WHOLE_SPECULATIVE_EDGES is true, all three components of
722 speculative edge gets updated. Otherwise we update only direct
723 call. */
725 void
726 cgraph_node::set_call_stmt_including_clones (gimple old_stmt,
727 gcall *new_stmt,
728 bool update_speculative)
730 cgraph_node *node;
731 cgraph_edge *edge = get_edge (old_stmt);
733 if (edge)
734 edge->set_call_stmt (new_stmt, update_speculative);
736 node = clones;
737 if (node)
738 while (node != this)
740 cgraph_edge *edge = node->get_edge (old_stmt);
741 if (edge)
743 edge->set_call_stmt (new_stmt, update_speculative);
744 /* If UPDATE_SPECULATIVE is false, it means that we are turning
745 speculative call into a real code sequence. Update the
746 callgraph edges. */
747 if (edge->speculative && !update_speculative)
749 cgraph_edge *direct, *indirect;
750 ipa_ref *ref;
752 gcc_assert (!edge->indirect_unknown_callee);
753 edge->speculative_call_info (direct, indirect, ref);
754 direct->speculative = false;
755 indirect->speculative = false;
756 ref->speculative = false;
759 if (node->clones)
760 node = node->clones;
761 else if (node->next_sibling_clone)
762 node = node->next_sibling_clone;
763 else
765 while (node != this && !node->next_sibling_clone)
766 node = node->clone_of;
767 if (node != this)
768 node = node->next_sibling_clone;
773 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
774 same function body. If clones already have edge for OLD_STMT; only
775 update the edge same way as cgraph_set_call_stmt_including_clones does.
777 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
778 frequencies of the clones. */
780 void
781 cgraph_node::create_edge_including_clones (cgraph_node *callee,
782 gimple old_stmt, gcall *stmt,
783 gcov_type count,
784 int freq,
785 cgraph_inline_failed_t reason)
787 cgraph_node *node;
788 cgraph_edge *edge;
790 if (!get_edge (stmt))
792 edge = create_edge (callee, stmt, count, freq);
793 edge->inline_failed = reason;
796 node = clones;
797 if (node)
798 while (node != this)
800 cgraph_edge *edge = node->get_edge (old_stmt);
802 /* It is possible that clones already contain the edge while
803 master didn't. Either we promoted indirect call into direct
804 call in the clone or we are processing clones of unreachable
805 master where edges has been removed. */
806 if (edge)
807 edge->set_call_stmt (stmt);
808 else if (! node->get_edge (stmt))
810 edge = node->create_edge (callee, stmt, count, freq);
811 edge->inline_failed = reason;
814 if (node->clones)
815 node = node->clones;
816 else if (node->next_sibling_clone)
817 node = node->next_sibling_clone;
818 else
820 while (node != this && !node->next_sibling_clone)
821 node = node->clone_of;
822 if (node != this)
823 node = node->next_sibling_clone;
828 /* Remove the node from cgraph and all inline clones inlined into it.
829 Skip however removal of FORBIDDEN_NODE and return true if it needs to be
830 removed. This allows to call the function from outer loop walking clone
831 tree. */
833 bool
834 cgraph_node::remove_symbol_and_inline_clones (cgraph_node *forbidden_node)
836 cgraph_edge *e, *next;
837 bool found = false;
839 if (this == forbidden_node)
841 callers->remove ();
842 return true;
844 for (e = callees; e; e = next)
846 next = e->next_callee;
847 if (!e->inline_failed)
848 found |= e->callee->remove_symbol_and_inline_clones (forbidden_node);
850 remove ();
851 return found;
854 /* The edges representing the callers of the NEW_VERSION node were
855 fixed by cgraph_function_versioning (), now the call_expr in their
856 respective tree code should be updated to call the NEW_VERSION. */
858 static void
859 update_call_expr (cgraph_node *new_version)
861 cgraph_edge *e;
863 gcc_assert (new_version);
865 /* Update the call expr on the edges to call the new version. */
866 for (e = new_version->callers; e; e = e->next_caller)
868 function *inner_function = DECL_STRUCT_FUNCTION (e->caller->decl);
869 gimple_call_set_fndecl (e->call_stmt, new_version->decl);
870 maybe_clean_eh_stmt_fn (inner_function, e->call_stmt);
875 /* Create a new cgraph node which is the new version of
876 callgraph node. REDIRECT_CALLERS holds the callers
877 edges which should be redirected to point to
878 NEW_VERSION. ALL the callees edges of the node
879 are cloned to the new version node. Return the new
880 version node.
882 If non-NULL BLOCK_TO_COPY determine what basic blocks
883 was copied to prevent duplications of calls that are dead
884 in the clone. */
886 cgraph_node *
887 cgraph_node::create_version_clone (tree new_decl,
888 vec<cgraph_edge *> redirect_callers,
889 bitmap bbs_to_copy)
891 cgraph_node *new_version;
892 cgraph_edge *e;
893 unsigned i;
895 new_version = cgraph_node::create (new_decl);
897 new_version->analyzed = analyzed;
898 new_version->definition = definition;
899 new_version->local = local;
900 new_version->externally_visible = false;
901 new_version->no_reorder = no_reorder;
902 new_version->local.local = new_version->definition;
903 new_version->global = global;
904 new_version->rtl = rtl;
905 new_version->count = count;
907 for (e = callees; e; e=e->next_callee)
908 if (!bbs_to_copy
909 || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
910 e->clone (new_version, e->call_stmt,
911 e->lto_stmt_uid, REG_BR_PROB_BASE,
912 CGRAPH_FREQ_BASE,
913 true);
914 for (e = indirect_calls; e; e=e->next_callee)
915 if (!bbs_to_copy
916 || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
917 e->clone (new_version, e->call_stmt,
918 e->lto_stmt_uid, REG_BR_PROB_BASE,
919 CGRAPH_FREQ_BASE,
920 true);
921 FOR_EACH_VEC_ELT (redirect_callers, i, e)
923 /* Redirect calls to the old version node to point to its new
924 version. */
925 e->redirect_callee (new_version);
928 symtab->call_cgraph_duplication_hooks (this, new_version);
930 return new_version;
933 /* Perform function versioning.
934 Function versioning includes copying of the tree and
935 a callgraph update (creating a new cgraph node and updating
936 its callees and callers).
938 REDIRECT_CALLERS varray includes the edges to be redirected
939 to the new version.
941 TREE_MAP is a mapping of tree nodes we want to replace with
942 new ones (according to results of prior analysis).
944 If non-NULL ARGS_TO_SKIP determine function parameters to remove
945 from new version.
946 If SKIP_RETURN is true, the new version will return void.
947 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
948 If non_NULL NEW_ENTRY determine new entry BB of the clone.
950 Return the new version's cgraph node. */
952 cgraph_node *
953 cgraph_node::create_version_clone_with_body
954 (vec<cgraph_edge *> redirect_callers,
955 vec<ipa_replace_map *, va_gc> *tree_map, bitmap args_to_skip,
956 bool skip_return, bitmap bbs_to_copy, basic_block new_entry_block,
957 const char *clone_name)
959 tree old_decl = decl;
960 cgraph_node *new_version_node = NULL;
961 tree new_decl;
963 if (!tree_versionable_function_p (old_decl))
964 return NULL;
966 gcc_assert (local.can_change_signature || !args_to_skip);
968 /* Make a new FUNCTION_DECL tree node for the new version. */
969 if (!args_to_skip && !skip_return)
970 new_decl = copy_node (old_decl);
971 else
972 new_decl
973 = build_function_decl_skip_args (old_decl, args_to_skip, skip_return);
975 /* Generate a new name for the new version. */
976 DECL_NAME (new_decl) = clone_function_name (old_decl, clone_name);
977 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
978 SET_DECL_RTL (new_decl, NULL);
980 /* When the old decl was a con-/destructor make sure the clone isn't. */
981 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
982 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
984 /* Create the new version's call-graph node.
985 and update the edges of the new node. */
986 new_version_node = create_version_clone (new_decl, redirect_callers,
987 bbs_to_copy);
989 if (ipa_transforms_to_apply.exists ())
990 new_version_node->ipa_transforms_to_apply
991 = ipa_transforms_to_apply.copy ();
992 /* Copy the OLD_VERSION_NODE function tree to the new version. */
993 tree_function_versioning (old_decl, new_decl, tree_map, false, args_to_skip,
994 skip_return, bbs_to_copy, new_entry_block);
996 /* Update the new version's properties.
997 Make The new version visible only within this translation unit. Make sure
998 that is not weak also.
999 ??? We cannot use COMDAT linkage because there is no
1000 ABI support for this. */
1001 new_version_node->make_decl_local ();
1002 DECL_VIRTUAL_P (new_version_node->decl) = 0;
1003 new_version_node->externally_visible = 0;
1004 new_version_node->local.local = 1;
1005 new_version_node->lowered = true;
1006 /* Clones of global symbols or symbols with unique names are unique. */
1007 if ((TREE_PUBLIC (old_decl)
1008 && !DECL_EXTERNAL (old_decl)
1009 && !DECL_WEAK (old_decl)
1010 && !DECL_COMDAT (old_decl))
1011 || in_lto_p)
1012 new_version_node->unique_name = true;
1014 /* Update the call_expr on the edges to call the new version node. */
1015 update_call_expr (new_version_node);
1017 symtab->call_cgraph_insertion_hooks (this);
1018 return new_version_node;
1021 /* Given virtual clone, turn it into actual clone. */
1023 static void
1024 cgraph_materialize_clone (cgraph_node *node)
1026 bitmap_obstack_initialize (NULL);
1027 node->former_clone_of = node->clone_of->decl;
1028 if (node->clone_of->former_clone_of)
1029 node->former_clone_of = node->clone_of->former_clone_of;
1030 /* Copy the OLD_VERSION_NODE function tree to the new version. */
1031 tree_function_versioning (node->clone_of->decl, node->decl,
1032 node->clone.tree_map, true,
1033 node->clone.args_to_skip, false,
1034 NULL, NULL);
1035 if (symtab->dump_file)
1037 dump_function_to_file (node->clone_of->decl, symtab->dump_file,
1038 dump_flags);
1039 dump_function_to_file (node->decl, symtab->dump_file, dump_flags);
1042 /* Function is no longer clone. */
1043 if (node->next_sibling_clone)
1044 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1045 if (node->prev_sibling_clone)
1046 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1047 else
1048 node->clone_of->clones = node->next_sibling_clone;
1049 node->next_sibling_clone = NULL;
1050 node->prev_sibling_clone = NULL;
1051 if (!node->clone_of->analyzed && !node->clone_of->clones)
1053 node->clone_of->release_body ();
1054 node->clone_of->remove_callees ();
1055 node->clone_of->remove_all_references ();
1057 node->clone_of = NULL;
1058 bitmap_obstack_release (NULL);
1061 /* Once all functions from compilation unit are in memory, produce all clones
1062 and update all calls. We might also do this on demand if we don't want to
1063 bring all functions to memory prior compilation, but current WHOPR
1064 implementation does that and it is is bit easier to keep everything right in
1065 this order. */
1067 void
1068 symbol_table::materialize_all_clones (void)
1070 cgraph_node *node;
1071 bool stabilized = false;
1074 if (symtab->dump_file)
1075 fprintf (symtab->dump_file, "Materializing clones\n");
1076 #ifdef ENABLE_CHECKING
1077 cgraph_node::verify_cgraph_nodes ();
1078 #endif
1080 /* We can also do topological order, but number of iterations should be
1081 bounded by number of IPA passes since single IPA pass is probably not
1082 going to create clones of clones it created itself. */
1083 while (!stabilized)
1085 stabilized = true;
1086 FOR_EACH_FUNCTION (node)
1088 if (node->clone_of && node->decl != node->clone_of->decl
1089 && !gimple_has_body_p (node->decl))
1091 if (!node->clone_of->clone_of)
1092 node->clone_of->get_untransformed_body ();
1093 if (gimple_has_body_p (node->clone_of->decl))
1095 if (symtab->dump_file)
1097 fprintf (symtab->dump_file, "cloning %s to %s\n",
1098 xstrdup_for_dump (node->clone_of->name ()),
1099 xstrdup_for_dump (node->name ()));
1100 if (node->clone.tree_map)
1102 unsigned int i;
1103 fprintf (symtab->dump_file, " replace map: ");
1104 for (i = 0;
1105 i < vec_safe_length (node->clone.tree_map);
1106 i++)
1108 ipa_replace_map *replace_info;
1109 replace_info = (*node->clone.tree_map)[i];
1110 print_generic_expr (symtab->dump_file, replace_info->old_tree, 0);
1111 fprintf (symtab->dump_file, " -> ");
1112 print_generic_expr (symtab->dump_file, replace_info->new_tree, 0);
1113 fprintf (symtab->dump_file, "%s%s;",
1114 replace_info->replace_p ? "(replace)":"",
1115 replace_info->ref_p ? "(ref)":"");
1117 fprintf (symtab->dump_file, "\n");
1119 if (node->clone.args_to_skip)
1121 fprintf (symtab->dump_file, " args_to_skip: ");
1122 dump_bitmap (symtab->dump_file,
1123 node->clone.args_to_skip);
1125 if (node->clone.args_to_skip)
1127 fprintf (symtab->dump_file, " combined_args_to_skip:");
1128 dump_bitmap (symtab->dump_file, node->clone.combined_args_to_skip);
1131 cgraph_materialize_clone (node);
1132 stabilized = false;
1137 FOR_EACH_FUNCTION (node)
1138 if (!node->analyzed && node->callees)
1140 node->remove_callees ();
1141 node->remove_all_references ();
1143 else
1144 node->clear_stmts_in_references ();
1145 if (symtab->dump_file)
1146 fprintf (symtab->dump_file, "Materialization Call site updates done.\n");
1147 #ifdef ENABLE_CHECKING
1148 cgraph_node::verify_cgraph_nodes ();
1149 #endif
1150 symtab->remove_unreachable_nodes (symtab->dump_file);
1153 #include "gt-cgraphclones.h"