Debian Codegraph @

gcc/cgraphclones.cc

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