]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cgraphclones.cc
Update copyright years.
[thirdparty/gcc.git] / gcc / cgraphclones.cc
CommitLineData
564fe867 1/* Callgraph clones
a945c346 2 Copyright (C) 2003-2024 Free Software Foundation, Inc.
564fe867
JH
3 Contributed by Jan Hubicka
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
dfea3d6f 21/* This module provide facilities for cloning functions. I.e. creating
564fe867
JH
22 new functions based on existing functions with simple modifications,
23 such as replacement of parameters.
24
25 To allow whole program optimization without actual presence of function
26 bodies, an additional infrastructure is provided for so-called virtual
27 clones
28
29 A virtual clone in the callgraph is a function that has no
30 associated body, just a description of how to create its body based
31 on a different function (which itself may be a virtual clone).
32
33 The description of function modifications includes adjustments to
34 the function's signature (which allows, for example, removing or
35 adding function arguments), substitutions to perform on the
36 function body, and, for inlined functions, a pointer to the
37 function that it will be inlined into.
38
39 It is also possible to redirect any edge of the callgraph from a
40 function to its virtual clone. This implies updating of the call
41 site to adjust for the new function signature.
42
43 Most of the transformations performed by inter-procedural
44 optimizations can be represented via virtual clones. For
45 instance, a constant propagation pass can produce a virtual clone
46 of the function which replaces one of its arguments by a
47 constant. The inliner can represent its decisions by producing a
48 clone of a function whose body will be later integrated into
49 a given function.
50
51 Using virtual clones, the program can be easily updated
52 during the Execute stage, solving most of pass interactions
53 problems that would otherwise occur during Transform.
54
55 Virtual clones are later materialized in the LTRANS stage and
56 turned into real functions. Passes executed after the virtual
57 clone were introduced also perform their Transform stage
58 on new functions, so for a pass there is no significant
59 difference between operating on a real function or a virtual
60 clone introduced before its Execute stage.
61
62 Optimization passes then work on virtual clones introduced before
63 their Execute stage as if they were real functions. The
64 only difference is that clones are not visible during the
65 Generate Summary stage. */
66
67#include "config.h"
68#include "system.h"
69#include "coretypes.h"
c7131fb2 70#include "backend.h"
957060b5
AM
71#include "target.h"
72#include "rtl.h"
c7131fb2
AM
73#include "tree.h"
74#include "gimple.h"
40e23961 75#include "stringpool.h"
957060b5 76#include "cgraph.h"
957060b5 77#include "lto-streamer.h"
2fb9a547 78#include "tree-eh.h"
442b4905 79#include "tree-cfg.h"
564fe867 80#include "tree-inline.h"
79891c4c 81#include "attribs.h"
c24e924f 82#include "dumpfile.h"
564fe867 83#include "gimple-pretty-print.h"
68188fff
MJ
84#include "alloc-pool.h"
85#include "symbol-summary.h"
86#include "tree-vrp.h"
87#include "ipa-prop.h"
88#include "ipa-fnsummary.h"
67f3791f 89#include "symtab-thunks.h"
ae7a23a3 90#include "symtab-clones.h"
564fe867 91
3dafb85c
ML
92/* Create clone of edge in the node N represented by CALL_EXPR
93 the callgraph. */
94
95cgraph_edge *
538dd0b7 96cgraph_edge::clone (cgraph_node *n, gcall *call_stmt, unsigned stmt_uid,
1511c8c0 97 profile_count num, profile_count den,
1bad9c18 98 bool update_original)
564fe867 99{
3dafb85c 100 cgraph_edge *new_edge;
1bad9c18 101 profile_count::adjust_for_ipa_scaling (&num, &den);
517048ce 102 profile_count prof_count = count.apply_scale (num, den);
564fe867 103
3dafb85c 104 if (indirect_unknown_callee)
564fe867
JH
105 {
106 tree decl;
107
e57872ee
JH
108 if (call_stmt && (decl = gimple_call_fndecl (call_stmt))
109 /* When the call is speculative, we need to resolve it
110 via cgraph_resolve_speculation and not here. */
3dafb85c 111 && !speculative)
564fe867 112 {
3dafb85c 113 cgraph_node *callee = cgraph_node::get (decl);
564fe867 114 gcc_checking_assert (callee);
3187c8a5 115 new_edge = n->create_edge (callee, call_stmt, prof_count, true);
564fe867
JH
116 }
117 else
118 {
d52f5295 119 new_edge = n->create_indirect_edge (call_stmt,
3dafb85c 120 indirect_info->ecf_flags,
3187c8a5 121 prof_count, true);
3dafb85c 122 *new_edge->indirect_info = *indirect_info;
564fe867
JH
123 }
124 }
125 else
126 {
3187c8a5 127 new_edge = n->create_edge (callee, call_stmt, prof_count, true);
3dafb85c 128 if (indirect_info)
564fe867
JH
129 {
130 new_edge->indirect_info
766090c2 131 = ggc_cleared_alloc<cgraph_indirect_call_info> ();
3dafb85c 132 *new_edge->indirect_info = *indirect_info;
564fe867
JH
133 }
134 }
135
3dafb85c
ML
136 new_edge->inline_failed = inline_failed;
137 new_edge->indirect_inlining_edge = indirect_inlining_edge;
118aa5e3
JH
138 if (!call_stmt)
139 new_edge->lto_stmt_uid = stmt_uid;
f1ba88b1 140 new_edge->speculative_id = speculative_id;
564fe867 141 /* Clone flags that depend on call_stmt availability manually. */
3dafb85c
ML
142 new_edge->can_throw_external = can_throw_external;
143 new_edge->call_stmt_cannot_inline_p = call_stmt_cannot_inline_p;
144 new_edge->speculative = speculative;
f9bb202b 145 new_edge->in_polymorphic_cdtor = in_polymorphic_cdtor;
1bad9c18
JH
146
147 /* Update IPA profile. Local profiles need no updating in original. */
517048ce 148 if (update_original)
b49d29d7
JH
149 count = count.combine_with_ipa_count_within (count.ipa ()
150 - new_edge->count.ipa (),
151 caller->count);
3dafb85c 152 symtab->call_edge_duplication_hooks (this, new_edge);
564fe867
JH
153 return new_edge;
154}
155
610c8ef0
MJ
156/* Set flags of NEW_NODE and its decl. NEW_NODE is a newly created private
157 clone or its thunk. */
158
159static void
160set_new_clone_decl_and_node_flags (cgraph_node *new_node)
161{
162 DECL_EXTERNAL (new_node->decl) = 0;
610c8ef0
MJ
163 TREE_PUBLIC (new_node->decl) = 0;
164 DECL_COMDAT (new_node->decl) = 0;
165 DECL_WEAK (new_node->decl) = 0;
166 DECL_VIRTUAL_P (new_node->decl) = 0;
167 DECL_STATIC_CONSTRUCTOR (new_node->decl) = 0;
168 DECL_STATIC_DESTRUCTOR (new_node->decl) = 0;
fe8e21fd
ML
169 DECL_SET_IS_OPERATOR_NEW (new_node->decl, 0);
170 DECL_SET_IS_OPERATOR_DELETE (new_node->decl, 0);
d7a65edb 171 DECL_IS_REPLACEABLE_OPERATOR (new_node->decl) = 0;
610c8ef0
MJ
172
173 new_node->externally_visible = 0;
87f94429 174 new_node->local = 1;
610c8ef0 175 new_node->lowered = true;
75ac95f6 176 new_node->semantic_interposition = 0;
610c8ef0
MJ
177}
178
179/* Duplicate thunk THUNK if necessary but make it to refer to NODE.
180 ARGS_TO_SKIP, if non-NULL, determines which parameters should be omitted.
181 Function can return NODE if no thunk is necessary, which can happen when
182 thunk is this_adjusting but we are removing this parameter. */
183
184static cgraph_node *
d284e1b8 185duplicate_thunk_for_node (cgraph_node *thunk, cgraph_node *node)
610c8ef0
MJ
186{
187 cgraph_node *new_thunk, *thunk_of;
d52f5295 188 thunk_of = thunk->callees->callee->ultimate_alias_target ();
610c8ef0 189
67f3791f 190 if (thunk_of->thunk)
d284e1b8 191 node = duplicate_thunk_for_node (thunk_of, node);
610c8ef0 192
48fb6d40 193 if (!DECL_ARGUMENTS (thunk->decl))
70486010 194 thunk->get_untransformed_body ();
48fb6d40 195
67f3791f 196 thunk_info *i = thunk_info::get (thunk);
3dafb85c 197 cgraph_edge *cs;
610c8ef0 198 for (cs = node->callers; cs; cs = cs->next_caller)
67f3791f
JH
199 if (cs->caller->thunk)
200 {
201 thunk_info *i2 = thunk_info::get (cs->caller);
202 if (*i2 == *i)
203 return cs->caller;
204 }
610c8ef0
MJ
205
206 tree new_decl;
ae7a23a3
JH
207 clone_info *info = clone_info::get (node);
208 if (info && info->param_adjustments)
610c8ef0
MJ
209 {
210 /* We do not need to duplicate this_adjusting thunks if we have removed
211 this. */
67f3791f 212 if (i->this_adjusting
ae7a23a3 213 && !info->param_adjustments->first_param_intact_p ())
610c8ef0
MJ
214 return node;
215
ff6686d2 216 new_decl = copy_node (thunk->decl);
ae7a23a3 217 ipa_param_body_adjustments body_adj (info->param_adjustments,
ff6686d2
MJ
218 new_decl);
219 body_adj.modify_formal_parameters ();
bec63208 220 }
ff6686d2 221 else
2f1691be
JJ
222 {
223 new_decl = copy_node (thunk->decl);
224 for (tree *arg = &DECL_ARGUMENTS (new_decl);
225 *arg; arg = &DECL_CHAIN (*arg))
226 {
227 tree next = DECL_CHAIN (*arg);
228 *arg = copy_node (*arg);
229 DECL_CONTEXT (*arg) = new_decl;
230 DECL_CHAIN (*arg) = next;
231 }
232 }
bec63208 233
610c8ef0
MJ
234 gcc_checking_assert (!DECL_STRUCT_FUNCTION (new_decl));
235 gcc_checking_assert (!DECL_INITIAL (new_decl));
236 gcc_checking_assert (!DECL_RESULT (new_decl));
237 gcc_checking_assert (!DECL_RTL_SET_P (new_decl));
238
7958186b
MP
239 DECL_NAME (new_decl) = clone_function_name_numbered (thunk->decl,
240 "artificial_thunk");
610c8ef0 241 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
610c8ef0 242
67124cb6
EB
243 /* We need to force DECL_IGNORED_P because the new thunk is created after
244 early debug was run. */
245 DECL_IGNORED_P (new_decl) = 1;
246
d52f5295 247 new_thunk = cgraph_node::create (new_decl);
610c8ef0
MJ
248 set_new_clone_decl_and_node_flags (new_thunk);
249 new_thunk->definition = true;
87f94429 250 new_thunk->can_change_signature = node->can_change_signature;
610c8ef0
MJ
251 new_thunk->thunk = thunk->thunk;
252 new_thunk->unique_name = in_lto_p;
253 new_thunk->former_clone_of = thunk->decl;
a2058f58 254 if (info && info->param_adjustments)
ae7a23a3
JH
255 clone_info::get_create (new_thunk)->param_adjustments
256 = info->param_adjustments;
b74d8dc4
JH
257 new_thunk->unit_id = thunk->unit_id;
258 new_thunk->merged_comdat = thunk->merged_comdat;
259 new_thunk->merged_extern_inline = thunk->merged_extern_inline;
610c8ef0 260
1bad9c18 261 cgraph_edge *e = new_thunk->create_edge (node, NULL, new_thunk->count);
3dafb85c 262 symtab->call_edge_duplication_hooks (thunk->callees, e);
3dafb85c 263 symtab->call_cgraph_duplication_hooks (thunk, new_thunk);
610c8ef0
MJ
264 return new_thunk;
265}
266
267/* If E does not lead to a thunk, simply redirect it to N. Otherwise create
268 one or more equivalent thunks for N and redirect E to the first in the
6a4bad95
MJ
269 chain. Note that it is then necessary to call
270 n->expand_all_artificial_thunks once all callers are redirected. */
610c8ef0
MJ
271
272void
6a4bad95 273cgraph_edge::redirect_callee_duplicating_thunks (cgraph_node *n)
610c8ef0 274{
6a4bad95 275 cgraph_node *orig_to = callee->ultimate_alias_target ();
67f3791f 276 if (orig_to->thunk)
d284e1b8 277 n = duplicate_thunk_for_node (orig_to, n);
610c8ef0 278
6a4bad95
MJ
279 redirect_callee (n);
280}
281
282/* Call expand_thunk on all callers that are thunks and if analyze those nodes
283 that were expanded. */
284
285void
286cgraph_node::expand_all_artificial_thunks ()
287{
288 cgraph_edge *e;
289 for (e = callers; e;)
67f3791f 290 if (e->caller->thunk)
6a4bad95
MJ
291 {
292 cgraph_node *thunk = e->caller;
293
294 e = e->next_caller;
67f3791f 295 if (expand_thunk (thunk, false, false))
6a4bad95 296 {
67f3791f 297 thunk->thunk = false;
6a4bad95 298 thunk->analyze ();
68188fff
MJ
299 ipa_analyze_node (thunk);
300 inline_analyze_function (thunk);
6a4bad95
MJ
301 }
302 thunk->expand_all_artificial_thunks ();
303 }
304 else
305 e = e->next_caller;
610c8ef0 306}
564fe867 307
0bdad123
ML
308void
309dump_callgraph_transformation (const cgraph_node *original,
310 const cgraph_node *clone,
311 const char *suffix)
312{
313 if (symtab->ipa_clones_dump_file)
314 {
315 fprintf (symtab->ipa_clones_dump_file,
316 "Callgraph clone;%s;%d;%s;%d;%d;%s;%d;%s;%d;%d;%s\n",
317 original->asm_name (), original->order,
318 DECL_SOURCE_FILE (original->decl),
319 DECL_SOURCE_LINE (original->decl),
320 DECL_SOURCE_COLUMN (original->decl), clone->asm_name (),
321 clone->order, DECL_SOURCE_FILE (clone->decl),
322 DECL_SOURCE_LINE (clone->decl), DECL_SOURCE_COLUMN (clone->decl),
323 suffix);
324
325 symtab->cloned_nodes.add (original);
326 symtab->cloned_nodes.add (clone);
327 }
328}
329
34fbe3f0
JH
330/* Turn profile of N to local profile. */
331
332static void
333localize_profile (cgraph_node *n)
334{
335 n->count = n->count.guessed_local ();
336 for (cgraph_edge *e = n->callees; e; e=e->next_callee)
337 {
338 e->count = e->count.guessed_local ();
339 if (!e->inline_failed)
340 localize_profile (e->callee);
341 }
342 for (cgraph_edge *e = n->indirect_calls; e; e=e->next_callee)
343 e->count = e->count.guessed_local ();
344}
345
564fe867
JH
346/* Create node representing clone of N executed COUNT times. Decrease
347 the execution counts from original node too.
348 The new clone will have decl set to DECL that may or may not be the same
349 as decl of N.
350
351 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
352 function's profile to reflect the fact that part of execution is handled
353 by node.
dfea3d6f 354 When CALL_DUPLICATION_HOOK is true, the ipa passes are acknowledged about
44a60244
MJ
355 the new clone. Otherwise the caller is responsible for doing so later.
356
357 If the new node is being inlined into another one, NEW_INLINED_TO should be
358 the outline function the new one is (even indirectly) inlined to. All hooks
a62bfab5 359 will see this in node's inlined_to, when invoked. Can be NULL if the
ff6686d2
MJ
360 node is not inlined.
361
362 If PARAM_ADJUSTMENTS is non-NULL, the parameter manipulation information
363 will be overwritten by the new structure. Otherwise the new node will
364 share parameter manipulation information with the original node. */
564fe867 365
d52f5295 366cgraph_node *
1bad9c18 367cgraph_node::create_clone (tree new_decl, profile_count prof_count,
d52f5295
ML
368 bool update_original,
369 vec<cgraph_edge *> redirect_callers,
370 bool call_duplication_hook,
3dafb85c 371 cgraph_node *new_inlined_to,
ff6686d2
MJ
372 ipa_param_adjustments *param_adjustments,
373 const char *suffix)
564fe867 374{
3dafb85c
ML
375 cgraph_node *new_node = symtab->create_empty ();
376 cgraph_edge *e;
564fe867 377 unsigned i;
1bad9c18 378 profile_count old_count = count;
34fbe3f0 379 bool nonzero = count.ipa ().nonzero_p ();
564fe867 380
0bdad123
ML
381 if (new_inlined_to)
382 dump_callgraph_transformation (this, new_inlined_to, "inlining to");
383
8f58dbd1
JH
384 /* When inlining we scale precisely to prof_count, when cloning we can
385 preserve local profile. */
386 if (!new_inlined_to)
387 prof_count = count.combine_with_ipa_count (prof_count);
3995f3a2 388 new_node->count = prof_count;
178ac530 389 new_node->calls_declare_variant_alt = this->calls_declare_variant_alt;
1bad9c18
JH
390
391 /* Update IPA profile. Local profiles need no updating in original. */
517048ce 392 if (update_original)
b49d29d7
JH
393 {
394 if (inlined_to)
395 count = count.combine_with_ipa_count_within (count.ipa ()
396 - prof_count.ipa (),
397 inlined_to->count);
398 else
399 count = count.combine_with_ipa_count (count.ipa () - prof_count.ipa ());
400 }
ed6ef490 401 new_node->decl = new_decl;
d52f5295 402 new_node->register_symbol ();
d52f5295 403 new_node->lto_file_data = lto_file_data;
d52f5295
ML
404 new_node->analyzed = analyzed;
405 new_node->definition = definition;
87f94429
ML
406 new_node->versionable = versionable;
407 new_node->can_change_signature = can_change_signature;
408 new_node->redefined_extern_inline = redefined_extern_inline;
2a6d372b 409 new_node->semantic_interposition = semantic_interposition;
87f94429 410 new_node->tm_may_enter_irr = tm_may_enter_irr;
67348ccc 411 new_node->externally_visible = false;
7861b648 412 new_node->no_reorder = no_reorder;
87f94429 413 new_node->local = true;
a62bfab5 414 new_node->inlined_to = new_inlined_to;
d52f5295 415 new_node->rtl = rtl;
d52f5295
ML
416 new_node->frequency = frequency;
417 new_node->tp_first_run = tp_first_run;
9d6171dc 418 new_node->tm_clone = tm_clone;
0a7246ee 419 new_node->icf_merged = icf_merged;
6bbf39b7 420 new_node->thunk = thunk;
b74d8dc4
JH
421 new_node->unit_id = unit_id;
422 new_node->merged_comdat = merged_comdat;
423 new_node->merged_extern_inline = merged_extern_inline;
ae7a23a3 424 clone_info *info = clone_info::get (this);
d284e1b8 425
ff6686d2 426 if (param_adjustments)
ae7a23a3
JH
427 clone_info::get_create (new_node)->param_adjustments = param_adjustments;
428 else if (info && info->param_adjustments)
429 clone_info::get_create (new_node)->param_adjustments
430 = info->param_adjustments;
ed6ef490 431 new_node->split_part = split_part;
d284e1b8 432
9771b263 433 FOR_EACH_VEC_ELT (redirect_callers, i, e)
564fe867
JH
434 {
435 /* Redirect calls to the old version node to point to its new
9de6f6c3 436 version. The only exception is when the edge was proved to
dfea3d6f 437 be unreachable during the cloning procedure. */
9de6f6c3 438 if (!e->callee
1edcb2ea
JJ
439 || !fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE,
440 BUILT_IN_UNREACHABLE_TRAP))
6a4bad95 441 e->redirect_callee_duplicating_thunks (new_node);
564fe867 442 }
6a4bad95 443 new_node->expand_all_artificial_thunks ();
564fe867 444
d52f5295 445 for (e = callees;e; e=e->next_callee)
1bad9c18
JH
446 e->clone (new_node, e->call_stmt, e->lto_stmt_uid, new_node->count, old_count,
447 update_original);
564fe867 448
d52f5295 449 for (e = indirect_calls; e; e = e->next_callee)
3dafb85c 450 e->clone (new_node, e->call_stmt, e->lto_stmt_uid,
1bad9c18 451 new_node->count, old_count, update_original);
d52f5295 452 new_node->clone_references (this);
564fe867 453
d52f5295
ML
454 new_node->next_sibling_clone = clones;
455 if (clones)
456 clones->prev_sibling_clone = new_node;
457 clones = new_node;
458 new_node->clone_of = this;
564fe867
JH
459
460 if (call_duplication_hook)
3dafb85c 461 symtab->call_cgraph_duplication_hooks (this, new_node);
34fbe3f0
JH
462 /* With partial train run we do not want to assume that original's
463 count is zero whenever we redurect all executed edges to clone.
464 Simply drop profile to local one in this case. */
465 if (update_original
466 && opt_for_fn (decl, flag_profile_partial_training)
467 && nonzero
468 && count.ipa_p ()
f7f6be76
JH
469 && !count.ipa ().nonzero_p ()
470 && !inlined_to)
34fbe3f0 471 localize_profile (this);
0bdad123
ML
472
473 if (!new_inlined_to)
474 dump_callgraph_transformation (this, new_node, suffix);
475
564fe867
JH
476 return new_node;
477}
478
b75255a9 479static GTY(()) hash_map<const char *, unsigned> *clone_fn_ids;
564fe867 480
7958186b
MP
481/* Return a new assembler name for a clone of decl named NAME. Apart
482 from the string SUFFIX, the new name will end with a unique (for
483 each NAME) unspecified number. If clone numbering is not needed
484 then the two argument clone_function_name should be used instead.
485 Should not be called directly except for by
e53b6e56 486 lto-partition.cc:privatize_symbol_name_1. */
9816367c 487
564fe867 488tree
7958186b
MP
489clone_function_name_numbered (const char *name, const char *suffix)
490{
b75255a9
MP
491 /* Initialize the function->counter mapping the first time it's
492 needed. */
493 if (!clone_fn_ids)
494 clone_fn_ids = hash_map<const char *, unsigned int>::create_ggc (64);
495 unsigned int &suffix_counter = clone_fn_ids->get_or_insert (
496 IDENTIFIER_POINTER (get_identifier (name)));
497 return clone_function_name (name, suffix, suffix_counter++);
7958186b
MP
498}
499
500/* Return a new assembler name for a clone of DECL. Apart from string
501 SUFFIX, the new name will end with a unique (for each DECL
502 assembler name) unspecified number. If clone numbering is not
503 needed then the two argument clone_function_name should be used
504 instead. */
505
506tree
507clone_function_name_numbered (tree decl, const char *suffix)
508{
509 tree name = DECL_ASSEMBLER_NAME (decl);
510 return clone_function_name_numbered (IDENTIFIER_POINTER (name),
511 suffix);
512}
513
514/* Return a new assembler name for a clone of decl named NAME. Apart
515 from the string SUFFIX, the new name will end with the specified
516 NUMBER. If clone numbering is not needed then the two argument
517 clone_function_name should be used instead. */
518
519tree
520clone_function_name (const char *name, const char *suffix,
521 unsigned long number)
564fe867 522{
9816367c 523 size_t len = strlen (name);
564fe867
JH
524 char *tmp_name, *prefix;
525
526 prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
9816367c 527 memcpy (prefix, name, len);
564fe867 528 strcpy (prefix + len + 1, suffix);
f8a1abf8 529 prefix[len] = symbol_table::symbol_suffix_separator ();
7958186b 530 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, number);
564fe867
JH
531 return get_identifier (tmp_name);
532}
533
53aedcce
MP
534/* Return a new assembler name for a clone of DECL. Apart from the
535 string SUFFIX, the new name will end with the specified NUMBER. If
536 clone numbering is not needed then the two argument
537 clone_function_name should be used instead. */
538
539tree
540clone_function_name (tree decl, const char *suffix,
541 unsigned long number)
542{
543 return clone_function_name (
544 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), suffix, number);
545}
546
7958186b
MP
547/* Return a new assembler name ending with the string SUFFIX for a
548 clone of DECL. */
9816367c
BS
549
550tree
551clone_function_name (tree decl, const char *suffix)
552{
7958186b
MP
553 tree identifier = DECL_ASSEMBLER_NAME (decl);
554 /* For consistency this needs to behave the same way as
555 ASM_FORMAT_PRIVATE_NAME does, but without the final number
556 suffix. */
557 char *separator = XALLOCAVEC (char, 2);
558 separator[0] = symbol_table::symbol_suffix_separator ();
559 separator[1] = 0;
560#if defined (NO_DOT_IN_LABEL) && defined (NO_DOLLAR_IN_LABEL)
561 const char *prefix = "__";
562#else
563 const char *prefix = "";
564#endif
565 char *result = ACONCAT ((prefix,
566 IDENTIFIER_POINTER (identifier),
567 separator,
568 suffix,
569 (char*)0));
570 return get_identifier (result);
9816367c
BS
571}
572
573
53aedcce
MP
574/* Create callgraph node clone with new declaration. The actual body will be
575 copied later at compilation stage. The name of the new clone will be
576 constructed from the name of the original node, SUFFIX and NUM_SUFFIX.
564fe867
JH
577
578 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
579 bitmap interface.
580 */
3dafb85c 581cgraph_node *
00dcc88a 582cgraph_node::create_virtual_clone (const vec<cgraph_edge *> &redirect_callers,
d52f5295 583 vec<ipa_replace_map *, va_gc> *tree_map,
ff6686d2
MJ
584 ipa_param_adjustments *param_adjustments,
585 const char * suffix, unsigned num_suffix)
564fe867 586{
d52f5295 587 tree old_decl = decl;
3dafb85c 588 cgraph_node *new_node = NULL;
564fe867 589 tree new_decl;
440a5082 590 size_t len, i;
3dafb85c 591 ipa_replace_map *map;
440a5082 592 char *name;
564fe867 593
87f94429 594 gcc_checking_assert (versionable);
ff6686d2
MJ
595 /* TODO: It would be nice if we could recognize that param_adjustments do not
596 actually perform any changes, but at the moment let's require it simply
597 does not exist. */
87f94429 598 gcc_assert (can_change_signature || !param_adjustments);
564fe867
JH
599
600 /* Make a new FUNCTION_DECL tree node */
ff6686d2 601 if (!param_adjustments)
564fe867
JH
602 new_decl = copy_node (old_decl);
603 else
ff6686d2 604 new_decl = param_adjustments->adjust_decl (old_decl);
49bde175
JH
605
606 /* These pointers represent function body and will be populated only when clone
607 is materialized. */
608 gcc_assert (new_decl != old_decl);
564fe867 609 DECL_STRUCT_FUNCTION (new_decl) = NULL;
49bde175
JH
610 DECL_ARGUMENTS (new_decl) = NULL;
611 DECL_INITIAL (new_decl) = NULL;
612 DECL_RESULT (new_decl) = NULL;
67914693 613 /* We cannot do DECL_RESULT (new_decl) = NULL; here because of LTO partitioning
49bde175 614 sometimes storing only clone decl instead of original. */
564fe867
JH
615
616 /* Generate a new name for the new version. */
440a5082
EB
617 len = IDENTIFIER_LENGTH (DECL_NAME (old_decl));
618 name = XALLOCAVEC (char, len + strlen (suffix) + 2);
619 memcpy (name, IDENTIFIER_POINTER (DECL_NAME (old_decl)), len);
620 strcpy (name + len + 1, suffix);
621 name[len] = '.';
622 DECL_NAME (new_decl) = get_identifier (name);
53aedcce
MP
623 SET_DECL_ASSEMBLER_NAME (new_decl,
624 clone_function_name (old_decl, suffix, num_suffix));
564fe867
JH
625 SET_DECL_RTL (new_decl, NULL);
626
1bad9c18 627 new_node = create_clone (new_decl, count, false,
ff6686d2
MJ
628 redirect_callers, false, NULL, param_adjustments,
629 suffix);
d52f5295 630
564fe867
JH
631 /* Update the properties.
632 Make clone visible only within this translation unit. Make sure
633 that is not weak also.
634 ??? We cannot use COMDAT linkage because there is no
635 ABI support for this. */
610c8ef0 636 set_new_clone_decl_and_node_flags (new_node);
6cf67b62 637 new_node->ipcp_clone = ipcp_clone;
ae7a23a3
JH
638 if (tree_map)
639 clone_info::get_create (new_node)->tree_map = tree_map;
4ab26ee0 640 if (!implicit_section)
8b9a92f7 641 new_node->set_section (*this);
702d8703
JH
642
643 /* Clones of global symbols or symbols with unique names are unique. */
644 if ((TREE_PUBLIC (old_decl)
645 && !DECL_EXTERNAL (old_decl)
646 && !DECL_WEAK (old_decl)
647 && !DECL_COMDAT (old_decl))
648 || in_lto_p)
67348ccc 649 new_node->unique_name = true;
9771b263 650 FOR_EACH_VEC_SAFE_ELT (tree_map, i, map)
13586172
MJ
651 {
652 tree repl = map->new_tree;
653 if (map->force_load_ref)
654 {
655 gcc_assert (TREE_CODE (repl) == ADDR_EXPR);
656 repl = get_base_address (TREE_OPERAND (repl, 0));
657 }
658 new_node->maybe_create_reference (repl, NULL);
659 }
d284e1b8 660
d52f5295 661 if (ipa_transforms_to_apply.exists ())
ca860d03 662 new_node->ipa_transforms_to_apply
d52f5295 663 = ipa_transforms_to_apply.copy ();
564fe867 664
3dafb85c 665 symtab->call_cgraph_duplication_hooks (this, new_node);
564fe867
JH
666
667 return new_node;
668}
669
d52f5295 670/* callgraph node being removed from symbol table; see if its entry can be
ab4664eb
JH
671 replaced by other inline clone.
672 INFO is clone info to attach to the new root. */
d52f5295 673cgraph_node *
ab4664eb 674cgraph_node::find_replacement (clone_info *info)
564fe867 675{
3dafb85c 676 cgraph_node *next_inline_clone, *replacement;
564fe867 677
d52f5295 678 for (next_inline_clone = clones;
564fe867 679 next_inline_clone
d52f5295 680 && next_inline_clone->decl != decl;
564fe867
JH
681 next_inline_clone = next_inline_clone->next_sibling_clone)
682 ;
683
684 /* If there is inline clone of the node being removed, we need
685 to put it into the position of removed node and reorganize all
686 other clones to be based on it. */
687 if (next_inline_clone)
688 {
3dafb85c
ML
689 cgraph_node *n;
690 cgraph_node *new_clones;
564fe867
JH
691
692 replacement = next_inline_clone;
693
694 /* Unlink inline clone from the list of clones of removed node. */
695 if (next_inline_clone->next_sibling_clone)
696 next_inline_clone->next_sibling_clone->prev_sibling_clone
697 = next_inline_clone->prev_sibling_clone;
698 if (next_inline_clone->prev_sibling_clone)
699 {
d52f5295 700 gcc_assert (clones != next_inline_clone);
564fe867
JH
701 next_inline_clone->prev_sibling_clone->next_sibling_clone
702 = next_inline_clone->next_sibling_clone;
703 }
704 else
705 {
d52f5295
ML
706 gcc_assert (clones == next_inline_clone);
707 clones = next_inline_clone->next_sibling_clone;
564fe867
JH
708 }
709
d52f5295
ML
710 new_clones = clones;
711 clones = NULL;
564fe867
JH
712
713 /* Copy clone info. */
ae7a23a3
JH
714 if (info)
715 *clone_info::get_create (next_inline_clone) = *info;
564fe867
JH
716
717 /* Now place it into clone tree at same level at NODE. */
d52f5295 718 next_inline_clone->clone_of = clone_of;
564fe867
JH
719 next_inline_clone->prev_sibling_clone = NULL;
720 next_inline_clone->next_sibling_clone = NULL;
d52f5295 721 if (clone_of)
564fe867 722 {
d52f5295
ML
723 if (clone_of->clones)
724 clone_of->clones->prev_sibling_clone = next_inline_clone;
725 next_inline_clone->next_sibling_clone = clone_of->clones;
726 clone_of->clones = next_inline_clone;
564fe867
JH
727 }
728
729 /* Merge the clone list. */
730 if (new_clones)
731 {
732 if (!next_inline_clone->clones)
733 next_inline_clone->clones = new_clones;
734 else
735 {
736 n = next_inline_clone->clones;
737 while (n->next_sibling_clone)
d52f5295 738 n = n->next_sibling_clone;
564fe867
JH
739 n->next_sibling_clone = new_clones;
740 new_clones->prev_sibling_clone = n;
741 }
742 }
743
744 /* Update clone_of pointers. */
745 n = new_clones;
746 while (n)
747 {
748 n->clone_of = next_inline_clone;
749 n = n->next_sibling_clone;
750 }
3c56d8d8
ML
751
752 /* Update order in order to be able to find a LTO section
753 with function body. */
754 replacement->order = order;
755
564fe867
JH
756 return replacement;
757 }
758 else
759 return NULL;
760}
761
762/* Like cgraph_set_call_stmt but walk the clone tree and update all
042ae7d2
JH
763 clones sharing the same function body.
764 When WHOLE_SPECULATIVE_EDGES is true, all three components of
765 speculative edge gets updated. Otherwise we update only direct
766 call. */
564fe867
JH
767
768void
355fe088 769cgraph_node::set_call_stmt_including_clones (gimple *old_stmt,
538dd0b7 770 gcall *new_stmt,
d52f5295 771 bool update_speculative)
564fe867 772{
3dafb85c 773 cgraph_node *node;
27c5a177 774 cgraph_edge *master_edge = get_edge (old_stmt);
564fe867 775
27c5a177
MJ
776 if (master_edge)
777 cgraph_edge::set_call_stmt (master_edge, new_stmt, update_speculative);
564fe867 778
d52f5295 779 node = clones;
564fe867 780 if (node)
d52f5295 781 while (node != this)
564fe867 782 {
3dafb85c 783 cgraph_edge *edge = node->get_edge (old_stmt);
564fe867 784 if (edge)
042ae7d2 785 {
27c5a177
MJ
786 edge = cgraph_edge::set_call_stmt (edge, new_stmt,
787 update_speculative);
042ae7d2
JH
788 /* If UPDATE_SPECULATIVE is false, it means that we are turning
789 speculative call into a real code sequence. Update the
790 callgraph edges. */
791 if (edge->speculative && !update_speculative)
792 {
845bb366
JH
793 cgraph_edge *indirect = edge->speculative_call_indirect_edge ();
794
795 for (cgraph_edge *next, *direct
796 = edge->first_speculative_call_target ();
797 direct;
798 direct = next)
799 {
800 next = direct->next_speculative_call_target ();
801 direct->speculative_call_target_ref ()->speculative = false;
802 direct->speculative = false;
803 }
042ae7d2 804 indirect->speculative = false;
042ae7d2
JH
805 }
806 }
564fe867
JH
807 if (node->clones)
808 node = node->clones;
809 else if (node->next_sibling_clone)
810 node = node->next_sibling_clone;
811 else
812 {
d52f5295 813 while (node != this && !node->next_sibling_clone)
564fe867 814 node = node->clone_of;
d52f5295 815 if (node != this)
564fe867
JH
816 node = node->next_sibling_clone;
817 }
818 }
819}
820
821/* Like cgraph_create_edge walk the clone tree and update all clones sharing
822 same function body. If clones already have edge for OLD_STMT; only
823 update the edge same way as cgraph_set_call_stmt_including_clones does.
824
825 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
826 frequencies of the clones. */
827
828void
3dafb85c 829cgraph_node::create_edge_including_clones (cgraph_node *callee,
355fe088 830 gimple *old_stmt, gcall *stmt,
3995f3a2 831 profile_count count,
d52f5295 832 cgraph_inline_failed_t reason)
564fe867 833{
3dafb85c 834 cgraph_node *node;
564fe867 835
d52f5295 836 if (!get_edge (stmt))
564fe867 837 {
27c5a177 838 cgraph_edge *edge = create_edge (callee, stmt, count);
564fe867
JH
839 edge->inline_failed = reason;
840 }
841
d52f5295 842 node = clones;
564fe867 843 if (node)
d52f5295 844 while (node != this)
ec6a1e35 845 /* Thunk clones do not get updated while copying inline function body. */
67f3791f 846 if (!node->thunk)
ec6a1e35
JH
847 {
848 cgraph_edge *edge = node->get_edge (old_stmt);
849
850 /* It is possible that clones already contain the edge while
851 master didn't. Either we promoted indirect call into direct
852 call in the clone or we are processing clones of unreachable
853 master where edges has been removed. */
854 if (edge)
27c5a177 855 edge = cgraph_edge::set_call_stmt (edge, stmt);
ec6a1e35
JH
856 else if (! node->get_edge (stmt))
857 {
1bad9c18 858 edge = node->create_edge (callee, stmt, count);
ec6a1e35
JH
859 edge->inline_failed = reason;
860 }
564fe867 861
ec6a1e35
JH
862 if (node->clones)
863 node = node->clones;
864 else if (node->next_sibling_clone)
865 node = node->next_sibling_clone;
866 else
867 {
868 while (node != this && !node->next_sibling_clone)
869 node = node->clone_of;
870 if (node != this)
871 node = node->next_sibling_clone;
872 }
873 }
564fe867
JH
874}
875
876/* Remove the node from cgraph and all inline clones inlined into it.
877 Skip however removal of FORBIDDEN_NODE and return true if it needs to be
878 removed. This allows to call the function from outer loop walking clone
879 tree. */
880
881bool
d52f5295 882cgraph_node::remove_symbol_and_inline_clones (cgraph_node *forbidden_node)
564fe867 883{
3dafb85c 884 cgraph_edge *e, *next;
564fe867
JH
885 bool found = false;
886
d52f5295 887 if (this == forbidden_node)
39f9719e 888 {
27c5a177 889 cgraph_edge::remove (callers);
39f9719e
JH
890 return true;
891 }
d52f5295 892 for (e = callees; e; e = next)
564fe867
JH
893 {
894 next = e->next_callee;
895 if (!e->inline_failed)
d52f5295 896 found |= e->callee->remove_symbol_and_inline_clones (forbidden_node);
564fe867 897 }
d52f5295 898 remove ();
564fe867
JH
899 return found;
900}
901
902/* The edges representing the callers of the NEW_VERSION node were
903 fixed by cgraph_function_versioning (), now the call_expr in their
904 respective tree code should be updated to call the NEW_VERSION. */
905
906static void
3dafb85c 907update_call_expr (cgraph_node *new_version)
564fe867 908{
3dafb85c 909 cgraph_edge *e;
564fe867
JH
910
911 gcc_assert (new_version);
912
913 /* Update the call expr on the edges to call the new version. */
914 for (e = new_version->callers; e; e = e->next_caller)
915 {
3dafb85c 916 function *inner_function = DECL_STRUCT_FUNCTION (e->caller->decl);
67348ccc 917 gimple_call_set_fndecl (e->call_stmt, new_version->decl);
564fe867
JH
918 maybe_clean_eh_stmt_fn (inner_function, e->call_stmt);
919 }
920}
921
922
923/* Create a new cgraph node which is the new version of
d52f5295 924 callgraph node. REDIRECT_CALLERS holds the callers
564fe867 925 edges which should be redirected to point to
d52f5295 926 NEW_VERSION. ALL the callees edges of the node
564fe867
JH
927 are cloned to the new version node. Return the new
928 version node.
929
930 If non-NULL BLOCK_TO_COPY determine what basic blocks
931 was copied to prevent duplications of calls that are dead
932 in the clone. */
933
d52f5295
ML
934cgraph_node *
935cgraph_node::create_version_clone (tree new_decl,
936 vec<cgraph_edge *> redirect_callers,
0bdad123
ML
937 bitmap bbs_to_copy,
938 const char *suffix)
564fe867 939 {
3dafb85c
ML
940 cgraph_node *new_version;
941 cgraph_edge *e;
564fe867
JH
942 unsigned i;
943
d52f5295 944 new_version = cgraph_node::create (new_decl);
564fe867 945
d52f5295
ML
946 new_version->analyzed = analyzed;
947 new_version->definition = definition;
948 new_version->local = local;
67348ccc 949 new_version->externally_visible = false;
7861b648 950 new_version->no_reorder = no_reorder;
87f94429 951 new_version->local = new_version->definition;
a62bfab5 952 new_version->inlined_to = inlined_to;
d52f5295
ML
953 new_version->rtl = rtl;
954 new_version->count = count;
b74d8dc4
JH
955 new_version->unit_id = unit_id;
956 new_version->merged_comdat = merged_comdat;
957 new_version->merged_extern_inline = merged_extern_inline;
564fe867 958
d52f5295 959 for (e = callees; e; e=e->next_callee)
564fe867
JH
960 if (!bbs_to_copy
961 || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
3dafb85c 962 e->clone (new_version, e->call_stmt,
1511c8c0 963 e->lto_stmt_uid, count, count,
3dafb85c 964 true);
d52f5295 965 for (e = indirect_calls; e; e=e->next_callee)
564fe867
JH
966 if (!bbs_to_copy
967 || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
3dafb85c 968 e->clone (new_version, e->call_stmt,
1511c8c0 969 e->lto_stmt_uid, count, count,
3dafb85c 970 true);
9771b263 971 FOR_EACH_VEC_ELT (redirect_callers, i, e)
564fe867
JH
972 {
973 /* Redirect calls to the old version node to point to its new
974 version. */
3dafb85c 975 e->redirect_callee (new_version);
564fe867
JH
976 }
977
0bdad123
ML
978 dump_callgraph_transformation (this, new_version, suffix);
979
564fe867
JH
980 return new_version;
981 }
982
983/* Perform function versioning.
984 Function versioning includes copying of the tree and
985 a callgraph update (creating a new cgraph node and updating
986 its callees and callers).
987
988 REDIRECT_CALLERS varray includes the edges to be redirected
989 to the new version.
990
991 TREE_MAP is a mapping of tree nodes we want to replace with
992 new ones (according to results of prior analysis).
564fe867
JH
993
994 If non-NULL ARGS_TO_SKIP determine function parameters to remove
995 from new version.
996 If SKIP_RETURN is true, the new version will return void.
997 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
998 If non_NULL NEW_ENTRY determine new entry BB of the clone.
999
5928bc2e
ML
1000 If TARGET_ATTRIBUTES is non-null, when creating a new declaration,
1001 add the attributes to DECL_ATTRIBUTES. And call valid_attribute_p
1002 that will promote value of the attribute DECL_FUNCTION_SPECIFIC_TARGET
1003 of the declaration.
1004
bfc9250e
ML
1005 If VERSION_DECL is set true, use clone_function_name_numbered for the
1006 function clone. Otherwise, use clone_function_name.
1007
564fe867
JH
1008 Return the new version's cgraph node. */
1009
d52f5295
ML
1010cgraph_node *
1011cgraph_node::create_version_clone_with_body
1012 (vec<cgraph_edge *> redirect_callers,
ff6686d2
MJ
1013 vec<ipa_replace_map *, va_gc> *tree_map,
1014 ipa_param_adjustments *param_adjustments,
1015 bitmap bbs_to_copy, basic_block new_entry_block, const char *suffix,
bfc9250e 1016 tree target_attributes, bool version_decl)
564fe867 1017{
d52f5295 1018 tree old_decl = decl;
3dafb85c 1019 cgraph_node *new_version_node = NULL;
564fe867
JH
1020 tree new_decl;
1021
1022 if (!tree_versionable_function_p (old_decl))
1023 return NULL;
1024
ff6686d2 1025 /* TODO: Restore an assert that we do not change signature if
87f94429 1026 can_change_signature is false. We cannot just check that
ff6686d2
MJ
1027 param_adjustments is NULL because unfortunately ipa-split removes return
1028 values from such functions. */
564fe867
JH
1029
1030 /* Make a new FUNCTION_DECL tree node for the new version. */
ff6686d2
MJ
1031 if (param_adjustments)
1032 new_decl = param_adjustments->adjust_decl (old_decl);
564fe867 1033 else
ff6686d2 1034 new_decl = copy_node (old_decl);
564fe867
JH
1035
1036 /* Generate a new name for the new version. */
bfc9250e
ML
1037 tree fnname = (version_decl ? clone_function_name_numbered (old_decl, suffix)
1038 : clone_function_name (old_decl, suffix));
1039 DECL_NAME (new_decl) = fnname;
1040 SET_DECL_ASSEMBLER_NAME (new_decl, fnname);
564fe867
JH
1041 SET_DECL_RTL (new_decl, NULL);
1042
97ae2126
JH
1043 DECL_VIRTUAL_P (new_decl) = 0;
1044
5928bc2e
ML
1045 if (target_attributes)
1046 {
1047 DECL_ATTRIBUTES (new_decl) = target_attributes;
1048
1049 location_t saved_loc = input_location;
1050 tree v = TREE_VALUE (target_attributes);
1051 input_location = DECL_SOURCE_LOCATION (new_decl);
79891c4c
AC
1052 bool r;
1053 tree name_id = get_attribute_name (target_attributes);
1054 const char *name_str = IDENTIFIER_POINTER (name_id);
1055 if (strcmp (name_str, "target") == 0)
1056 r = targetm.target_option.valid_attribute_p (new_decl, name_id, v, 1);
1057 else if (strcmp (name_str, "target_version") == 0)
1058 r = targetm.target_option.valid_version_attribute_p (new_decl, name_id,
1059 v, 1);
1060 else
1061 gcc_unreachable();
1062
5928bc2e
ML
1063 input_location = saved_loc;
1064 if (!r)
1065 return NULL;
1066 }
1067
564fe867 1068 /* When the old decl was a con-/destructor make sure the clone isn't. */
c3284718
RS
1069 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1070 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
fe8e21fd
ML
1071 DECL_SET_IS_OPERATOR_NEW (new_decl, 0);
1072 DECL_SET_IS_OPERATOR_DELETE (new_decl, 0);
d7a65edb 1073 DECL_IS_REPLACEABLE_OPERATOR (new_decl) = 0;
564fe867
JH
1074
1075 /* Create the new version's call-graph node.
1076 and update the edges of the new node. */
d52f5295 1077 new_version_node = create_version_clone (new_decl, redirect_callers,
0bdad123 1078 bbs_to_copy, suffix);
564fe867 1079
d52f5295 1080 if (ipa_transforms_to_apply.exists ())
c6d43074 1081 new_version_node->ipa_transforms_to_apply
d52f5295 1082 = ipa_transforms_to_apply.copy ();
564fe867 1083 /* Copy the OLD_VERSION_NODE function tree to the new version. */
ff6686d2
MJ
1084 tree_function_versioning (old_decl, new_decl, tree_map, param_adjustments,
1085 false, bbs_to_copy, new_entry_block);
564fe867
JH
1086
1087 /* Update the new version's properties.
1088 Make The new version visible only within this translation unit. Make sure
1089 that is not weak also.
1090 ??? We cannot use COMDAT linkage because there is no
1091 ABI support for this. */
d52f5295 1092 new_version_node->make_decl_local ();
67348ccc
DM
1093 DECL_VIRTUAL_P (new_version_node->decl) = 0;
1094 new_version_node->externally_visible = 0;
87f94429 1095 new_version_node->local = 1;
564fe867 1096 new_version_node->lowered = true;
4ab26ee0 1097 if (!implicit_section)
8b9a92f7 1098 new_version_node->set_section (*this);
702d8703
JH
1099 /* Clones of global symbols or symbols with unique names are unique. */
1100 if ((TREE_PUBLIC (old_decl)
1101 && !DECL_EXTERNAL (old_decl)
1102 && !DECL_WEAK (old_decl)
1103 && !DECL_COMDAT (old_decl))
1104 || in_lto_p)
67348ccc 1105 new_version_node->unique_name = true;
564fe867
JH
1106
1107 /* Update the call_expr on the edges to call the new version node. */
1108 update_call_expr (new_version_node);
1109
cbb4e4ca 1110 symtab->call_cgraph_insertion_hooks (new_version_node);
564fe867
JH
1111 return new_version_node;
1112}
1113
d0b1b67a
MJ
1114/* Remove the node from the tree of virtual and inline clones and make it a
1115 standalone node - not a clone any more. */
1116
1117void cgraph_node::remove_from_clone_tree ()
1118{
1119 if (next_sibling_clone)
1120 next_sibling_clone->prev_sibling_clone = prev_sibling_clone;
1121 if (prev_sibling_clone)
1122 prev_sibling_clone->next_sibling_clone = next_sibling_clone;
1123 else
1124 clone_of->clones = next_sibling_clone;
1125 next_sibling_clone = NULL;
1126 prev_sibling_clone = NULL;
1127 clone_of = NULL;
1128}
1129
564fe867
JH
1130/* Given virtual clone, turn it into actual clone. */
1131
564fe867 1132void
0e590b68 1133cgraph_node::materialize_clone ()
564fe867 1134{
ae7a23a3 1135 clone_info *info = clone_info::get (this);
0e590b68
JH
1136 clone_of->get_untransformed_body ();
1137 former_clone_of = clone_of->decl;
1138 if (clone_of->former_clone_of)
1139 former_clone_of = clone_of->former_clone_of;
3dafb85c 1140 if (symtab->dump_file)
564fe867 1141 {
0e590b68
JH
1142 fprintf (symtab->dump_file, "cloning %s to %s\n",
1143 clone_of->dump_name (),
1144 dump_name ());
25a0d08b 1145 if (info && info->tree_map)
564fe867 1146 {
0e590b68
JH
1147 fprintf (symtab->dump_file, " replace map:");
1148 for (unsigned int i = 0;
ae7a23a3 1149 i < vec_safe_length (info->tree_map);
0e590b68 1150 i++)
564fe867 1151 {
0e590b68 1152 ipa_replace_map *replace_info;
ae7a23a3 1153 replace_info = (*info->tree_map)[i];
0e590b68
JH
1154 fprintf (symtab->dump_file, "%s %i -> ",
1155 i ? "," : "", replace_info->parm_num);
1156 print_generic_expr (symtab->dump_file,
1157 replace_info->new_tree);
564fe867 1158 }
0e590b68 1159 fprintf (symtab->dump_file, "\n");
564fe867 1160 }
25a0d08b 1161 if (info && info->param_adjustments)
ae7a23a3 1162 info->param_adjustments->dump (symtab->dump_file);
564fe867 1163 }
783dc02d 1164 clear_stmts_in_references ();
0e590b68
JH
1165 /* Copy the OLD_VERSION_NODE function tree to the new version. */
1166 tree_function_versioning (clone_of->decl, decl,
ae7a23a3
JH
1167 info ? info->tree_map : NULL,
1168 info ? info->param_adjustments : NULL,
0e590b68 1169 true, NULL, NULL);
3dafb85c 1170 if (symtab->dump_file)
0e590b68
JH
1171 {
1172 dump_function_to_file (clone_of->decl, symtab->dump_file,
1173 dump_flags);
1174 dump_function_to_file (decl, symtab->dump_file, dump_flags);
1175 }
b2b29377 1176
0e590b68
JH
1177 cgraph_node *this_clone_of = clone_of;
1178 /* Function is no longer clone. */
1179 remove_from_clone_tree ();
1180 if (!this_clone_of->analyzed && !this_clone_of->clones)
d7145b4b 1181 this_clone_of->release_body ();
564fe867
JH
1182}
1183
1184#include "gt-cgraphclones.h"