Skip -fwhole-program when merging LTO options.
[official-gcc.git] / gcc / cgraphclones.cc
blobbb4b3c5407d56c1ea23b97131a7b91fa5b362e02
1 /* Callgraph clones
2 Copyright (C) 2003-2022 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 cloning 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 "backend.h"
71 #include "target.h"
72 #include "rtl.h"
73 #include "tree.h"
74 #include "gimple.h"
75 #include "stringpool.h"
76 #include "cgraph.h"
77 #include "lto-streamer.h"
78 #include "tree-eh.h"
79 #include "tree-cfg.h"
80 #include "tree-inline.h"
81 #include "dumpfile.h"
82 #include "gimple-pretty-print.h"
83 #include "alloc-pool.h"
84 #include "symbol-summary.h"
85 #include "tree-vrp.h"
86 #include "ipa-prop.h"
87 #include "ipa-fnsummary.h"
88 #include "symtab-thunks.h"
89 #include "symtab-clones.h"
91 /* Create clone of edge in the node N represented by CALL_EXPR
92 the callgraph. */
94 cgraph_edge *
95 cgraph_edge::clone (cgraph_node *n, gcall *call_stmt, unsigned stmt_uid,
96 profile_count num, profile_count den,
97 bool update_original)
99 cgraph_edge *new_edge;
100 profile_count::adjust_for_ipa_scaling (&num, &den);
101 profile_count prof_count = count.apply_scale (num, den);
103 if (indirect_unknown_callee)
105 tree decl;
107 if (call_stmt && (decl = gimple_call_fndecl (call_stmt))
108 /* When the call is speculative, we need to resolve it
109 via cgraph_resolve_speculation and not here. */
110 && !speculative)
112 cgraph_node *callee = cgraph_node::get (decl);
113 gcc_checking_assert (callee);
114 new_edge = n->create_edge (callee, call_stmt, prof_count, true);
116 else
118 new_edge = n->create_indirect_edge (call_stmt,
119 indirect_info->ecf_flags,
120 prof_count, true);
121 *new_edge->indirect_info = *indirect_info;
124 else
126 new_edge = n->create_edge (callee, call_stmt, prof_count, true);
127 if (indirect_info)
129 new_edge->indirect_info
130 = ggc_cleared_alloc<cgraph_indirect_call_info> ();
131 *new_edge->indirect_info = *indirect_info;
135 new_edge->inline_failed = inline_failed;
136 new_edge->indirect_inlining_edge = indirect_inlining_edge;
137 if (!call_stmt)
138 new_edge->lto_stmt_uid = stmt_uid;
139 new_edge->speculative_id = speculative_id;
140 /* Clone flags that depend on call_stmt availability manually. */
141 new_edge->can_throw_external = can_throw_external;
142 new_edge->call_stmt_cannot_inline_p = call_stmt_cannot_inline_p;
143 new_edge->speculative = speculative;
144 new_edge->in_polymorphic_cdtor = in_polymorphic_cdtor;
146 /* Update IPA profile. Local profiles need no updating in original. */
147 if (update_original)
148 count = count.combine_with_ipa_count_within (count.ipa ()
149 - new_edge->count.ipa (),
150 caller->count);
151 symtab->call_edge_duplication_hooks (this, new_edge);
152 return new_edge;
155 /* Set flags of NEW_NODE and its decl. NEW_NODE is a newly created private
156 clone or its thunk. */
158 static void
159 set_new_clone_decl_and_node_flags (cgraph_node *new_node)
161 DECL_EXTERNAL (new_node->decl) = 0;
162 TREE_PUBLIC (new_node->decl) = 0;
163 DECL_COMDAT (new_node->decl) = 0;
164 DECL_WEAK (new_node->decl) = 0;
165 DECL_VIRTUAL_P (new_node->decl) = 0;
166 DECL_STATIC_CONSTRUCTOR (new_node->decl) = 0;
167 DECL_STATIC_DESTRUCTOR (new_node->decl) = 0;
168 DECL_SET_IS_OPERATOR_NEW (new_node->decl, 0);
169 DECL_SET_IS_OPERATOR_DELETE (new_node->decl, 0);
170 DECL_IS_REPLACEABLE_OPERATOR (new_node->decl) = 0;
172 new_node->externally_visible = 0;
173 new_node->local = 1;
174 new_node->lowered = true;
175 new_node->semantic_interposition = 0;
178 /* Duplicate thunk THUNK if necessary but make it to refer to NODE.
179 ARGS_TO_SKIP, if non-NULL, determines which parameters should be omitted.
180 Function can return NODE if no thunk is necessary, which can happen when
181 thunk is this_adjusting but we are removing this parameter. */
183 static cgraph_node *
184 duplicate_thunk_for_node (cgraph_node *thunk, cgraph_node *node)
186 cgraph_node *new_thunk, *thunk_of;
187 thunk_of = thunk->callees->callee->ultimate_alias_target ();
189 if (thunk_of->thunk)
190 node = duplicate_thunk_for_node (thunk_of, node);
192 if (!DECL_ARGUMENTS (thunk->decl))
193 thunk->get_untransformed_body ();
195 thunk_info *i = thunk_info::get (thunk);
196 cgraph_edge *cs;
197 for (cs = node->callers; cs; cs = cs->next_caller)
198 if (cs->caller->thunk)
200 thunk_info *i2 = thunk_info::get (cs->caller);
201 if (*i2 == *i)
202 return cs->caller;
205 tree new_decl;
206 clone_info *info = clone_info::get (node);
207 if (info && info->param_adjustments)
209 /* We do not need to duplicate this_adjusting thunks if we have removed
210 this. */
211 if (i->this_adjusting
212 && !info->param_adjustments->first_param_intact_p ())
213 return node;
215 new_decl = copy_node (thunk->decl);
216 ipa_param_body_adjustments body_adj (info->param_adjustments,
217 new_decl);
218 body_adj.modify_formal_parameters ();
220 else
221 new_decl = copy_node (thunk->decl);
223 gcc_checking_assert (!DECL_STRUCT_FUNCTION (new_decl));
224 gcc_checking_assert (!DECL_INITIAL (new_decl));
225 gcc_checking_assert (!DECL_RESULT (new_decl));
226 gcc_checking_assert (!DECL_RTL_SET_P (new_decl));
228 DECL_NAME (new_decl) = clone_function_name_numbered (thunk->decl,
229 "artificial_thunk");
230 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
232 /* We need to force DECL_IGNORED_P because the new thunk is created after
233 early debug was run. */
234 DECL_IGNORED_P (new_decl) = 1;
236 new_thunk = cgraph_node::create (new_decl);
237 set_new_clone_decl_and_node_flags (new_thunk);
238 new_thunk->definition = true;
239 new_thunk->can_change_signature = node->can_change_signature;
240 new_thunk->thunk = thunk->thunk;
241 new_thunk->unique_name = in_lto_p;
242 new_thunk->former_clone_of = thunk->decl;
243 if (info && info->param_adjustments)
244 clone_info::get_create (new_thunk)->param_adjustments
245 = info->param_adjustments;
246 new_thunk->unit_id = thunk->unit_id;
247 new_thunk->merged_comdat = thunk->merged_comdat;
248 new_thunk->merged_extern_inline = thunk->merged_extern_inline;
250 cgraph_edge *e = new_thunk->create_edge (node, NULL, new_thunk->count);
251 symtab->call_edge_duplication_hooks (thunk->callees, e);
252 symtab->call_cgraph_duplication_hooks (thunk, new_thunk);
253 return new_thunk;
256 /* If E does not lead to a thunk, simply redirect it to N. Otherwise create
257 one or more equivalent thunks for N and redirect E to the first in the
258 chain. Note that it is then necessary to call
259 n->expand_all_artificial_thunks once all callers are redirected. */
261 void
262 cgraph_edge::redirect_callee_duplicating_thunks (cgraph_node *n)
264 cgraph_node *orig_to = callee->ultimate_alias_target ();
265 if (orig_to->thunk)
266 n = duplicate_thunk_for_node (orig_to, n);
268 redirect_callee (n);
271 /* Call expand_thunk on all callers that are thunks and if analyze those nodes
272 that were expanded. */
274 void
275 cgraph_node::expand_all_artificial_thunks ()
277 cgraph_edge *e;
278 for (e = callers; e;)
279 if (e->caller->thunk)
281 cgraph_node *thunk = e->caller;
283 e = e->next_caller;
284 if (expand_thunk (thunk, false, false))
286 thunk->thunk = false;
287 thunk->analyze ();
288 ipa_analyze_node (thunk);
289 inline_analyze_function (thunk);
291 thunk->expand_all_artificial_thunks ();
293 else
294 e = e->next_caller;
297 void
298 dump_callgraph_transformation (const cgraph_node *original,
299 const cgraph_node *clone,
300 const char *suffix)
302 if (symtab->ipa_clones_dump_file)
304 fprintf (symtab->ipa_clones_dump_file,
305 "Callgraph clone;%s;%d;%s;%d;%d;%s;%d;%s;%d;%d;%s\n",
306 original->asm_name (), original->order,
307 DECL_SOURCE_FILE (original->decl),
308 DECL_SOURCE_LINE (original->decl),
309 DECL_SOURCE_COLUMN (original->decl), clone->asm_name (),
310 clone->order, DECL_SOURCE_FILE (clone->decl),
311 DECL_SOURCE_LINE (clone->decl), DECL_SOURCE_COLUMN (clone->decl),
312 suffix);
314 symtab->cloned_nodes.add (original);
315 symtab->cloned_nodes.add (clone);
319 /* Turn profile of N to local profile. */
321 static void
322 localize_profile (cgraph_node *n)
324 n->count = n->count.guessed_local ();
325 for (cgraph_edge *e = n->callees; e; e=e->next_callee)
327 e->count = e->count.guessed_local ();
328 if (!e->inline_failed)
329 localize_profile (e->callee);
331 for (cgraph_edge *e = n->indirect_calls; e; e=e->next_callee)
332 e->count = e->count.guessed_local ();
335 /* Create node representing clone of N executed COUNT times. Decrease
336 the execution counts from original node too.
337 The new clone will have decl set to DECL that may or may not be the same
338 as decl of N.
340 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
341 function's profile to reflect the fact that part of execution is handled
342 by node.
343 When CALL_DUPLICATION_HOOK is true, the ipa passes are acknowledged about
344 the new clone. Otherwise the caller is responsible for doing so later.
346 If the new node is being inlined into another one, NEW_INLINED_TO should be
347 the outline function the new one is (even indirectly) inlined to. All hooks
348 will see this in node's inlined_to, when invoked. Can be NULL if the
349 node is not inlined.
351 If PARAM_ADJUSTMENTS is non-NULL, the parameter manipulation information
352 will be overwritten by the new structure. Otherwise the new node will
353 share parameter manipulation information with the original node. */
355 cgraph_node *
356 cgraph_node::create_clone (tree new_decl, profile_count prof_count,
357 bool update_original,
358 vec<cgraph_edge *> redirect_callers,
359 bool call_duplication_hook,
360 cgraph_node *new_inlined_to,
361 ipa_param_adjustments *param_adjustments,
362 const char *suffix)
364 cgraph_node *new_node = symtab->create_empty ();
365 cgraph_edge *e;
366 unsigned i;
367 profile_count old_count = count;
368 bool nonzero = count.ipa ().nonzero_p ();
370 if (new_inlined_to)
371 dump_callgraph_transformation (this, new_inlined_to, "inlining to");
373 /* When inlining we scale precisely to prof_count, when cloning we can
374 preserve local profile. */
375 if (!new_inlined_to)
376 prof_count = count.combine_with_ipa_count (prof_count);
377 new_node->count = prof_count;
378 new_node->calls_declare_variant_alt = this->calls_declare_variant_alt;
380 /* Update IPA profile. Local profiles need no updating in original. */
381 if (update_original)
383 if (inlined_to)
384 count = count.combine_with_ipa_count_within (count.ipa ()
385 - prof_count.ipa (),
386 inlined_to->count);
387 else
388 count = count.combine_with_ipa_count (count.ipa () - prof_count.ipa ());
390 new_node->decl = new_decl;
391 new_node->register_symbol ();
392 new_node->lto_file_data = lto_file_data;
393 new_node->analyzed = analyzed;
394 new_node->definition = definition;
395 new_node->versionable = versionable;
396 new_node->can_change_signature = can_change_signature;
397 new_node->redefined_extern_inline = redefined_extern_inline;
398 new_node->semantic_interposition = semantic_interposition;
399 new_node->tm_may_enter_irr = tm_may_enter_irr;
400 new_node->externally_visible = false;
401 new_node->no_reorder = no_reorder;
402 new_node->local = true;
403 new_node->inlined_to = new_inlined_to;
404 new_node->rtl = rtl;
405 new_node->frequency = frequency;
406 new_node->tp_first_run = tp_first_run;
407 new_node->tm_clone = tm_clone;
408 new_node->icf_merged = icf_merged;
409 new_node->thunk = thunk;
410 new_node->unit_id = unit_id;
411 new_node->merged_comdat = merged_comdat;
412 new_node->merged_extern_inline = merged_extern_inline;
413 clone_info *info = clone_info::get (this);
415 if (param_adjustments)
416 clone_info::get_create (new_node)->param_adjustments = param_adjustments;
417 else if (info && info->param_adjustments)
418 clone_info::get_create (new_node)->param_adjustments
419 = info->param_adjustments;
420 new_node->split_part = split_part;
422 FOR_EACH_VEC_ELT (redirect_callers, i, e)
424 /* Redirect calls to the old version node to point to its new
425 version. The only exception is when the edge was proved to
426 be unreachable during the cloning procedure. */
427 if (!e->callee
428 || !fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE))
429 e->redirect_callee_duplicating_thunks (new_node);
431 new_node->expand_all_artificial_thunks ();
433 for (e = callees;e; e=e->next_callee)
434 e->clone (new_node, e->call_stmt, e->lto_stmt_uid, new_node->count, old_count,
435 update_original);
437 for (e = indirect_calls; e; e = e->next_callee)
438 e->clone (new_node, e->call_stmt, e->lto_stmt_uid,
439 new_node->count, old_count, update_original);
440 new_node->clone_references (this);
442 new_node->next_sibling_clone = clones;
443 if (clones)
444 clones->prev_sibling_clone = new_node;
445 clones = new_node;
446 new_node->clone_of = this;
448 if (call_duplication_hook)
449 symtab->call_cgraph_duplication_hooks (this, new_node);
450 /* With partial train run we do not want to assume that original's
451 count is zero whenever we redurect all executed edges to clone.
452 Simply drop profile to local one in this case. */
453 if (update_original
454 && opt_for_fn (decl, flag_profile_partial_training)
455 && nonzero
456 && count.ipa_p ()
457 && !count.ipa ().nonzero_p ()
458 && !inlined_to)
459 localize_profile (this);
461 if (!new_inlined_to)
462 dump_callgraph_transformation (this, new_node, suffix);
464 return new_node;
467 static GTY(()) hash_map<const char *, unsigned> *clone_fn_ids;
469 /* Return a new assembler name for a clone of decl named NAME. Apart
470 from the string SUFFIX, the new name will end with a unique (for
471 each NAME) unspecified number. If clone numbering is not needed
472 then the two argument clone_function_name should be used instead.
473 Should not be called directly except for by
474 lto-partition.cc:privatize_symbol_name_1. */
476 tree
477 clone_function_name_numbered (const char *name, const char *suffix)
479 /* Initialize the function->counter mapping the first time it's
480 needed. */
481 if (!clone_fn_ids)
482 clone_fn_ids = hash_map<const char *, unsigned int>::create_ggc (64);
483 unsigned int &suffix_counter = clone_fn_ids->get_or_insert (
484 IDENTIFIER_POINTER (get_identifier (name)));
485 return clone_function_name (name, suffix, suffix_counter++);
488 /* Return a new assembler name for a clone of DECL. Apart from string
489 SUFFIX, the new name will end with a unique (for each DECL
490 assembler name) unspecified number. If clone numbering is not
491 needed then the two argument clone_function_name should be used
492 instead. */
494 tree
495 clone_function_name_numbered (tree decl, const char *suffix)
497 tree name = DECL_ASSEMBLER_NAME (decl);
498 return clone_function_name_numbered (IDENTIFIER_POINTER (name),
499 suffix);
502 /* Return a new assembler name for a clone of decl named NAME. Apart
503 from the string SUFFIX, the new name will end with the specified
504 NUMBER. If clone numbering is not needed then the two argument
505 clone_function_name should be used instead. */
507 tree
508 clone_function_name (const char *name, const char *suffix,
509 unsigned long number)
511 size_t len = strlen (name);
512 char *tmp_name, *prefix;
514 prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
515 memcpy (prefix, name, len);
516 strcpy (prefix + len + 1, suffix);
517 prefix[len] = symbol_table::symbol_suffix_separator ();
518 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, number);
519 return get_identifier (tmp_name);
522 /* Return a new assembler name for a clone of DECL. Apart from the
523 string SUFFIX, the new name will end with the specified NUMBER. If
524 clone numbering is not needed then the two argument
525 clone_function_name should be used instead. */
527 tree
528 clone_function_name (tree decl, const char *suffix,
529 unsigned long number)
531 return clone_function_name (
532 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), suffix, number);
535 /* Return a new assembler name ending with the string SUFFIX for a
536 clone of DECL. */
538 tree
539 clone_function_name (tree decl, const char *suffix)
541 tree identifier = DECL_ASSEMBLER_NAME (decl);
542 /* For consistency this needs to behave the same way as
543 ASM_FORMAT_PRIVATE_NAME does, but without the final number
544 suffix. */
545 char *separator = XALLOCAVEC (char, 2);
546 separator[0] = symbol_table::symbol_suffix_separator ();
547 separator[1] = 0;
548 #if defined (NO_DOT_IN_LABEL) && defined (NO_DOLLAR_IN_LABEL)
549 const char *prefix = "__";
550 #else
551 const char *prefix = "";
552 #endif
553 char *result = ACONCAT ((prefix,
554 IDENTIFIER_POINTER (identifier),
555 separator,
556 suffix,
557 (char*)0));
558 return get_identifier (result);
562 /* Create callgraph node clone with new declaration. The actual body will be
563 copied later at compilation stage. The name of the new clone will be
564 constructed from the name of the original node, SUFFIX and NUM_SUFFIX.
566 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
567 bitmap interface.
569 cgraph_node *
570 cgraph_node::create_virtual_clone (const vec<cgraph_edge *> &redirect_callers,
571 vec<ipa_replace_map *, va_gc> *tree_map,
572 ipa_param_adjustments *param_adjustments,
573 const char * suffix, unsigned num_suffix)
575 tree old_decl = decl;
576 cgraph_node *new_node = NULL;
577 tree new_decl;
578 size_t len, i;
579 ipa_replace_map *map;
580 char *name;
582 gcc_checking_assert (versionable);
583 /* TODO: It would be nice if we could recognize that param_adjustments do not
584 actually perform any changes, but at the moment let's require it simply
585 does not exist. */
586 gcc_assert (can_change_signature || !param_adjustments);
588 /* Make a new FUNCTION_DECL tree node */
589 if (!param_adjustments)
590 new_decl = copy_node (old_decl);
591 else
592 new_decl = param_adjustments->adjust_decl (old_decl);
594 /* These pointers represent function body and will be populated only when clone
595 is materialized. */
596 gcc_assert (new_decl != old_decl);
597 DECL_STRUCT_FUNCTION (new_decl) = NULL;
598 DECL_ARGUMENTS (new_decl) = NULL;
599 DECL_INITIAL (new_decl) = NULL;
600 DECL_RESULT (new_decl) = NULL;
601 /* We cannot do DECL_RESULT (new_decl) = NULL; here because of LTO partitioning
602 sometimes storing only clone decl instead of original. */
604 /* Generate a new name for the new version. */
605 len = IDENTIFIER_LENGTH (DECL_NAME (old_decl));
606 name = XALLOCAVEC (char, len + strlen (suffix) + 2);
607 memcpy (name, IDENTIFIER_POINTER (DECL_NAME (old_decl)), len);
608 strcpy (name + len + 1, suffix);
609 name[len] = '.';
610 DECL_NAME (new_decl) = get_identifier (name);
611 SET_DECL_ASSEMBLER_NAME (new_decl,
612 clone_function_name (old_decl, suffix, num_suffix));
613 SET_DECL_RTL (new_decl, NULL);
615 new_node = create_clone (new_decl, count, false,
616 redirect_callers, false, NULL, param_adjustments,
617 suffix);
619 /* Update the properties.
620 Make clone visible only within this translation unit. Make sure
621 that is not weak also.
622 ??? We cannot use COMDAT linkage because there is no
623 ABI support for this. */
624 set_new_clone_decl_and_node_flags (new_node);
625 new_node->ipcp_clone = ipcp_clone;
626 if (tree_map)
627 clone_info::get_create (new_node)->tree_map = tree_map;
628 if (!implicit_section)
629 new_node->set_section (*this);
631 /* Clones of global symbols or symbols with unique names are unique. */
632 if ((TREE_PUBLIC (old_decl)
633 && !DECL_EXTERNAL (old_decl)
634 && !DECL_WEAK (old_decl)
635 && !DECL_COMDAT (old_decl))
636 || in_lto_p)
637 new_node->unique_name = true;
638 FOR_EACH_VEC_SAFE_ELT (tree_map, i, map)
640 tree repl = map->new_tree;
641 if (map->force_load_ref)
643 gcc_assert (TREE_CODE (repl) == ADDR_EXPR);
644 repl = get_base_address (TREE_OPERAND (repl, 0));
646 new_node->maybe_create_reference (repl, NULL);
649 if (ipa_transforms_to_apply.exists ())
650 new_node->ipa_transforms_to_apply
651 = ipa_transforms_to_apply.copy ();
653 symtab->call_cgraph_duplication_hooks (this, new_node);
655 return new_node;
658 /* callgraph node being removed from symbol table; see if its entry can be
659 replaced by other inline clone.
660 INFO is clone info to attach to the new root. */
661 cgraph_node *
662 cgraph_node::find_replacement (clone_info *info)
664 cgraph_node *next_inline_clone, *replacement;
666 for (next_inline_clone = clones;
667 next_inline_clone
668 && next_inline_clone->decl != decl;
669 next_inline_clone = next_inline_clone->next_sibling_clone)
672 /* If there is inline clone of the node being removed, we need
673 to put it into the position of removed node and reorganize all
674 other clones to be based on it. */
675 if (next_inline_clone)
677 cgraph_node *n;
678 cgraph_node *new_clones;
680 replacement = next_inline_clone;
682 /* Unlink inline clone from the list of clones of removed node. */
683 if (next_inline_clone->next_sibling_clone)
684 next_inline_clone->next_sibling_clone->prev_sibling_clone
685 = next_inline_clone->prev_sibling_clone;
686 if (next_inline_clone->prev_sibling_clone)
688 gcc_assert (clones != next_inline_clone);
689 next_inline_clone->prev_sibling_clone->next_sibling_clone
690 = next_inline_clone->next_sibling_clone;
692 else
694 gcc_assert (clones == next_inline_clone);
695 clones = next_inline_clone->next_sibling_clone;
698 new_clones = clones;
699 clones = NULL;
701 /* Copy clone info. */
702 if (info)
703 *clone_info::get_create (next_inline_clone) = *info;
705 /* Now place it into clone tree at same level at NODE. */
706 next_inline_clone->clone_of = clone_of;
707 next_inline_clone->prev_sibling_clone = NULL;
708 next_inline_clone->next_sibling_clone = NULL;
709 if (clone_of)
711 if (clone_of->clones)
712 clone_of->clones->prev_sibling_clone = next_inline_clone;
713 next_inline_clone->next_sibling_clone = clone_of->clones;
714 clone_of->clones = next_inline_clone;
717 /* Merge the clone list. */
718 if (new_clones)
720 if (!next_inline_clone->clones)
721 next_inline_clone->clones = new_clones;
722 else
724 n = next_inline_clone->clones;
725 while (n->next_sibling_clone)
726 n = n->next_sibling_clone;
727 n->next_sibling_clone = new_clones;
728 new_clones->prev_sibling_clone = n;
732 /* Update clone_of pointers. */
733 n = new_clones;
734 while (n)
736 n->clone_of = next_inline_clone;
737 n = n->next_sibling_clone;
740 /* Update order in order to be able to find a LTO section
741 with function body. */
742 replacement->order = order;
744 return replacement;
746 else
747 return NULL;
750 /* Like cgraph_set_call_stmt but walk the clone tree and update all
751 clones sharing the same function body.
752 When WHOLE_SPECULATIVE_EDGES is true, all three components of
753 speculative edge gets updated. Otherwise we update only direct
754 call. */
756 void
757 cgraph_node::set_call_stmt_including_clones (gimple *old_stmt,
758 gcall *new_stmt,
759 bool update_speculative)
761 cgraph_node *node;
762 cgraph_edge *master_edge = get_edge (old_stmt);
764 if (master_edge)
765 cgraph_edge::set_call_stmt (master_edge, new_stmt, update_speculative);
767 node = clones;
768 if (node)
769 while (node != this)
771 cgraph_edge *edge = node->get_edge (old_stmt);
772 if (edge)
774 edge = cgraph_edge::set_call_stmt (edge, new_stmt,
775 update_speculative);
776 /* If UPDATE_SPECULATIVE is false, it means that we are turning
777 speculative call into a real code sequence. Update the
778 callgraph edges. */
779 if (edge->speculative && !update_speculative)
781 cgraph_edge *indirect = edge->speculative_call_indirect_edge ();
783 for (cgraph_edge *next, *direct
784 = edge->first_speculative_call_target ();
785 direct;
786 direct = next)
788 next = direct->next_speculative_call_target ();
789 direct->speculative_call_target_ref ()->speculative = false;
790 direct->speculative = false;
792 indirect->speculative = false;
795 if (node->clones)
796 node = node->clones;
797 else if (node->next_sibling_clone)
798 node = node->next_sibling_clone;
799 else
801 while (node != this && !node->next_sibling_clone)
802 node = node->clone_of;
803 if (node != this)
804 node = node->next_sibling_clone;
809 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
810 same function body. If clones already have edge for OLD_STMT; only
811 update the edge same way as cgraph_set_call_stmt_including_clones does.
813 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
814 frequencies of the clones. */
816 void
817 cgraph_node::create_edge_including_clones (cgraph_node *callee,
818 gimple *old_stmt, gcall *stmt,
819 profile_count count,
820 cgraph_inline_failed_t reason)
822 cgraph_node *node;
824 if (!get_edge (stmt))
826 cgraph_edge *edge = create_edge (callee, stmt, count);
827 edge->inline_failed = reason;
830 node = clones;
831 if (node)
832 while (node != this)
833 /* Thunk clones do not get updated while copying inline function body. */
834 if (!node->thunk)
836 cgraph_edge *edge = node->get_edge (old_stmt);
838 /* It is possible that clones already contain the edge while
839 master didn't. Either we promoted indirect call into direct
840 call in the clone or we are processing clones of unreachable
841 master where edges has been removed. */
842 if (edge)
843 edge = cgraph_edge::set_call_stmt (edge, stmt);
844 else if (! node->get_edge (stmt))
846 edge = node->create_edge (callee, stmt, count);
847 edge->inline_failed = reason;
850 if (node->clones)
851 node = node->clones;
852 else if (node->next_sibling_clone)
853 node = node->next_sibling_clone;
854 else
856 while (node != this && !node->next_sibling_clone)
857 node = node->clone_of;
858 if (node != this)
859 node = node->next_sibling_clone;
864 /* Remove the node from cgraph and all inline clones inlined into it.
865 Skip however removal of FORBIDDEN_NODE and return true if it needs to be
866 removed. This allows to call the function from outer loop walking clone
867 tree. */
869 bool
870 cgraph_node::remove_symbol_and_inline_clones (cgraph_node *forbidden_node)
872 cgraph_edge *e, *next;
873 bool found = false;
875 if (this == forbidden_node)
877 cgraph_edge::remove (callers);
878 return true;
880 for (e = callees; e; e = next)
882 next = e->next_callee;
883 if (!e->inline_failed)
884 found |= e->callee->remove_symbol_and_inline_clones (forbidden_node);
886 remove ();
887 return found;
890 /* The edges representing the callers of the NEW_VERSION node were
891 fixed by cgraph_function_versioning (), now the call_expr in their
892 respective tree code should be updated to call the NEW_VERSION. */
894 static void
895 update_call_expr (cgraph_node *new_version)
897 cgraph_edge *e;
899 gcc_assert (new_version);
901 /* Update the call expr on the edges to call the new version. */
902 for (e = new_version->callers; e; e = e->next_caller)
904 function *inner_function = DECL_STRUCT_FUNCTION (e->caller->decl);
905 gimple_call_set_fndecl (e->call_stmt, new_version->decl);
906 maybe_clean_eh_stmt_fn (inner_function, e->call_stmt);
911 /* Create a new cgraph node which is the new version of
912 callgraph node. REDIRECT_CALLERS holds the callers
913 edges which should be redirected to point to
914 NEW_VERSION. ALL the callees edges of the node
915 are cloned to the new version node. Return the new
916 version node.
918 If non-NULL BLOCK_TO_COPY determine what basic blocks
919 was copied to prevent duplications of calls that are dead
920 in the clone. */
922 cgraph_node *
923 cgraph_node::create_version_clone (tree new_decl,
924 vec<cgraph_edge *> redirect_callers,
925 bitmap bbs_to_copy,
926 const char *suffix)
928 cgraph_node *new_version;
929 cgraph_edge *e;
930 unsigned i;
932 new_version = cgraph_node::create (new_decl);
934 new_version->analyzed = analyzed;
935 new_version->definition = definition;
936 new_version->local = local;
937 new_version->externally_visible = false;
938 new_version->no_reorder = no_reorder;
939 new_version->local = new_version->definition;
940 new_version->inlined_to = inlined_to;
941 new_version->rtl = rtl;
942 new_version->count = count;
943 new_version->unit_id = unit_id;
944 new_version->merged_comdat = merged_comdat;
945 new_version->merged_extern_inline = merged_extern_inline;
947 for (e = callees; e; e=e->next_callee)
948 if (!bbs_to_copy
949 || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
950 e->clone (new_version, e->call_stmt,
951 e->lto_stmt_uid, count, count,
952 true);
953 for (e = indirect_calls; e; e=e->next_callee)
954 if (!bbs_to_copy
955 || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
956 e->clone (new_version, e->call_stmt,
957 e->lto_stmt_uid, count, count,
958 true);
959 FOR_EACH_VEC_ELT (redirect_callers, i, e)
961 /* Redirect calls to the old version node to point to its new
962 version. */
963 e->redirect_callee (new_version);
966 dump_callgraph_transformation (this, new_version, suffix);
968 return new_version;
971 /* Perform function versioning.
972 Function versioning includes copying of the tree and
973 a callgraph update (creating a new cgraph node and updating
974 its callees and callers).
976 REDIRECT_CALLERS varray includes the edges to be redirected
977 to the new version.
979 TREE_MAP is a mapping of tree nodes we want to replace with
980 new ones (according to results of prior analysis).
982 If non-NULL ARGS_TO_SKIP determine function parameters to remove
983 from new version.
984 If SKIP_RETURN is true, the new version will return void.
985 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
986 If non_NULL NEW_ENTRY determine new entry BB of the clone.
988 If TARGET_ATTRIBUTES is non-null, when creating a new declaration,
989 add the attributes to DECL_ATTRIBUTES. And call valid_attribute_p
990 that will promote value of the attribute DECL_FUNCTION_SPECIFIC_TARGET
991 of the declaration.
993 If VERSION_DECL is set true, use clone_function_name_numbered for the
994 function clone. Otherwise, use clone_function_name.
996 Return the new version's cgraph node. */
998 cgraph_node *
999 cgraph_node::create_version_clone_with_body
1000 (vec<cgraph_edge *> redirect_callers,
1001 vec<ipa_replace_map *, va_gc> *tree_map,
1002 ipa_param_adjustments *param_adjustments,
1003 bitmap bbs_to_copy, basic_block new_entry_block, const char *suffix,
1004 tree target_attributes, bool version_decl)
1006 tree old_decl = decl;
1007 cgraph_node *new_version_node = NULL;
1008 tree new_decl;
1010 if (!tree_versionable_function_p (old_decl))
1011 return NULL;
1013 /* TODO: Restore an assert that we do not change signature if
1014 can_change_signature is false. We cannot just check that
1015 param_adjustments is NULL because unfortunately ipa-split removes return
1016 values from such functions. */
1018 /* Make a new FUNCTION_DECL tree node for the new version. */
1019 if (param_adjustments)
1020 new_decl = param_adjustments->adjust_decl (old_decl);
1021 else
1022 new_decl = copy_node (old_decl);
1024 /* Generate a new name for the new version. */
1025 tree fnname = (version_decl ? clone_function_name_numbered (old_decl, suffix)
1026 : clone_function_name (old_decl, suffix));
1027 DECL_NAME (new_decl) = fnname;
1028 SET_DECL_ASSEMBLER_NAME (new_decl, fnname);
1029 SET_DECL_RTL (new_decl, NULL);
1031 DECL_VIRTUAL_P (new_decl) = 0;
1033 if (target_attributes)
1035 DECL_ATTRIBUTES (new_decl) = target_attributes;
1037 location_t saved_loc = input_location;
1038 tree v = TREE_VALUE (target_attributes);
1039 input_location = DECL_SOURCE_LOCATION (new_decl);
1040 bool r = targetm.target_option.valid_attribute_p (new_decl, NULL, v, 1);
1041 input_location = saved_loc;
1042 if (!r)
1043 return NULL;
1046 /* When the old decl was a con-/destructor make sure the clone isn't. */
1047 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1048 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1049 DECL_SET_IS_OPERATOR_NEW (new_decl, 0);
1050 DECL_SET_IS_OPERATOR_DELETE (new_decl, 0);
1051 DECL_IS_REPLACEABLE_OPERATOR (new_decl) = 0;
1053 /* Create the new version's call-graph node.
1054 and update the edges of the new node. */
1055 new_version_node = create_version_clone (new_decl, redirect_callers,
1056 bbs_to_copy, suffix);
1058 if (ipa_transforms_to_apply.exists ())
1059 new_version_node->ipa_transforms_to_apply
1060 = ipa_transforms_to_apply.copy ();
1061 /* Copy the OLD_VERSION_NODE function tree to the new version. */
1062 tree_function_versioning (old_decl, new_decl, tree_map, param_adjustments,
1063 false, bbs_to_copy, new_entry_block);
1065 /* Update the new version's properties.
1066 Make The new version visible only within this translation unit. Make sure
1067 that is not weak also.
1068 ??? We cannot use COMDAT linkage because there is no
1069 ABI support for this. */
1070 new_version_node->make_decl_local ();
1071 DECL_VIRTUAL_P (new_version_node->decl) = 0;
1072 new_version_node->externally_visible = 0;
1073 new_version_node->local = 1;
1074 new_version_node->lowered = true;
1075 if (!implicit_section)
1076 new_version_node->set_section (*this);
1077 /* Clones of global symbols or symbols with unique names are unique. */
1078 if ((TREE_PUBLIC (old_decl)
1079 && !DECL_EXTERNAL (old_decl)
1080 && !DECL_WEAK (old_decl)
1081 && !DECL_COMDAT (old_decl))
1082 || in_lto_p)
1083 new_version_node->unique_name = true;
1085 /* Update the call_expr on the edges to call the new version node. */
1086 update_call_expr (new_version_node);
1088 symtab->call_cgraph_insertion_hooks (new_version_node);
1089 return new_version_node;
1092 /* Remove the node from the tree of virtual and inline clones and make it a
1093 standalone node - not a clone any more. */
1095 void cgraph_node::remove_from_clone_tree ()
1097 if (next_sibling_clone)
1098 next_sibling_clone->prev_sibling_clone = prev_sibling_clone;
1099 if (prev_sibling_clone)
1100 prev_sibling_clone->next_sibling_clone = next_sibling_clone;
1101 else
1102 clone_of->clones = next_sibling_clone;
1103 next_sibling_clone = NULL;
1104 prev_sibling_clone = NULL;
1105 clone_of = NULL;
1108 /* Given virtual clone, turn it into actual clone. */
1110 void
1111 cgraph_node::materialize_clone ()
1113 clone_info *info = clone_info::get (this);
1114 clone_of->get_untransformed_body ();
1115 former_clone_of = clone_of->decl;
1116 if (clone_of->former_clone_of)
1117 former_clone_of = clone_of->former_clone_of;
1118 if (symtab->dump_file)
1120 fprintf (symtab->dump_file, "cloning %s to %s\n",
1121 clone_of->dump_name (),
1122 dump_name ());
1123 if (info && info->tree_map)
1125 fprintf (symtab->dump_file, " replace map:");
1126 for (unsigned int i = 0;
1127 i < vec_safe_length (info->tree_map);
1128 i++)
1130 ipa_replace_map *replace_info;
1131 replace_info = (*info->tree_map)[i];
1132 fprintf (symtab->dump_file, "%s %i -> ",
1133 i ? "," : "", replace_info->parm_num);
1134 print_generic_expr (symtab->dump_file,
1135 replace_info->new_tree);
1137 fprintf (symtab->dump_file, "\n");
1139 if (info && info->param_adjustments)
1140 info->param_adjustments->dump (symtab->dump_file);
1142 clear_stmts_in_references ();
1143 /* Copy the OLD_VERSION_NODE function tree to the new version. */
1144 tree_function_versioning (clone_of->decl, decl,
1145 info ? info->tree_map : NULL,
1146 info ? info->param_adjustments : NULL,
1147 true, NULL, NULL);
1148 if (symtab->dump_file)
1150 dump_function_to_file (clone_of->decl, symtab->dump_file,
1151 dump_flags);
1152 dump_function_to_file (decl, symtab->dump_file, dump_flags);
1155 cgraph_node *this_clone_of = clone_of;
1156 /* Function is no longer clone. */
1157 remove_from_clone_tree ();
1158 if (!this_clone_of->analyzed && !this_clone_of->clones)
1159 this_clone_of->release_body ();
1162 #include "gt-cgraphclones.h"