2 Copyright (C) 2003-2019 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
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
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
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
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. */
69 #include "coretypes.h"
75 #include "stringpool.h"
77 #include "lto-streamer.h"
80 #include "tree-inline.h"
82 #include "gimple-pretty-print.h"
84 /* Create clone of edge in the node N represented by CALL_EXPR
88 cgraph_edge::clone (cgraph_node
*n
, gcall
*call_stmt
, unsigned stmt_uid
,
89 profile_count num
, profile_count den
,
92 cgraph_edge
*new_edge
;
93 profile_count::adjust_for_ipa_scaling (&num
, &den
);
94 profile_count prof_count
= count
.apply_scale (num
, den
);
96 if (indirect_unknown_callee
)
100 if (call_stmt
&& (decl
= gimple_call_fndecl (call_stmt
))
101 /* When the call is speculative, we need to resolve it
102 via cgraph_resolve_speculation and not here. */
105 cgraph_node
*callee
= cgraph_node::get (decl
);
106 gcc_checking_assert (callee
);
107 new_edge
= n
->create_edge (callee
, call_stmt
, prof_count
, true);
111 new_edge
= n
->create_indirect_edge (call_stmt
,
112 indirect_info
->ecf_flags
,
114 *new_edge
->indirect_info
= *indirect_info
;
119 new_edge
= n
->create_edge (callee
, call_stmt
, prof_count
, true);
122 new_edge
->indirect_info
123 = ggc_cleared_alloc
<cgraph_indirect_call_info
> ();
124 *new_edge
->indirect_info
= *indirect_info
;
128 new_edge
->inline_failed
= inline_failed
;
129 new_edge
->indirect_inlining_edge
= indirect_inlining_edge
;
130 new_edge
->lto_stmt_uid
= stmt_uid
;
131 /* Clone flags that depend on call_stmt availability manually. */
132 new_edge
->can_throw_external
= can_throw_external
;
133 new_edge
->call_stmt_cannot_inline_p
= call_stmt_cannot_inline_p
;
134 new_edge
->speculative
= speculative
;
135 new_edge
->in_polymorphic_cdtor
= in_polymorphic_cdtor
;
137 /* Update IPA profile. Local profiles need no updating in original. */
139 count
= count
.combine_with_ipa_count (count
.ipa ()
140 - new_edge
->count
.ipa ());
141 symtab
->call_edge_duplication_hooks (this, new_edge
);
145 /* Set flags of NEW_NODE and its decl. NEW_NODE is a newly created private
146 clone or its thunk. */
149 set_new_clone_decl_and_node_flags (cgraph_node
*new_node
)
151 DECL_EXTERNAL (new_node
->decl
) = 0;
152 TREE_PUBLIC (new_node
->decl
) = 0;
153 DECL_COMDAT (new_node
->decl
) = 0;
154 DECL_WEAK (new_node
->decl
) = 0;
155 DECL_VIRTUAL_P (new_node
->decl
) = 0;
156 DECL_STATIC_CONSTRUCTOR (new_node
->decl
) = 0;
157 DECL_STATIC_DESTRUCTOR (new_node
->decl
) = 0;
158 DECL_SET_IS_OPERATOR_NEW (new_node
->decl
, 0);
159 DECL_SET_IS_OPERATOR_DELETE (new_node
->decl
, 0);
161 new_node
->externally_visible
= 0;
163 new_node
->lowered
= true;
166 /* Duplicate thunk THUNK if necessary but make it to refer to NODE.
167 ARGS_TO_SKIP, if non-NULL, determines which parameters should be omitted.
168 Function can return NODE if no thunk is necessary, which can happen when
169 thunk is this_adjusting but we are removing this parameter. */
172 duplicate_thunk_for_node (cgraph_node
*thunk
, cgraph_node
*node
)
174 cgraph_node
*new_thunk
, *thunk_of
;
175 thunk_of
= thunk
->callees
->callee
->ultimate_alias_target ();
177 if (thunk_of
->thunk
.thunk_p
)
178 node
= duplicate_thunk_for_node (thunk_of
, node
);
180 if (!DECL_ARGUMENTS (thunk
->decl
))
181 thunk
->get_untransformed_body ();
184 for (cs
= node
->callers
; cs
; cs
= cs
->next_caller
)
185 if (cs
->caller
->thunk
.thunk_p
186 && cs
->caller
->thunk
.fixed_offset
== thunk
->thunk
.fixed_offset
187 && cs
->caller
->thunk
.virtual_value
== thunk
->thunk
.virtual_value
188 && cs
->caller
->thunk
.indirect_offset
== thunk
->thunk
.indirect_offset
189 && cs
->caller
->thunk
.this_adjusting
== thunk
->thunk
.this_adjusting
190 && cs
->caller
->thunk
.virtual_offset_p
== thunk
->thunk
.virtual_offset_p
)
194 if (node
->clone
.param_adjustments
)
196 /* We do not need to duplicate this_adjusting thunks if we have removed
198 if (thunk
->thunk
.this_adjusting
199 && !node
->clone
.param_adjustments
->first_param_intact_p ())
202 new_decl
= copy_node (thunk
->decl
);
203 ipa_param_body_adjustments
body_adj (node
->clone
.param_adjustments
,
205 body_adj
.modify_formal_parameters ();
208 new_decl
= copy_node (thunk
->decl
);
210 gcc_checking_assert (!DECL_STRUCT_FUNCTION (new_decl
));
211 gcc_checking_assert (!DECL_INITIAL (new_decl
));
212 gcc_checking_assert (!DECL_RESULT (new_decl
));
213 gcc_checking_assert (!DECL_RTL_SET_P (new_decl
));
215 DECL_NAME (new_decl
) = clone_function_name_numbered (thunk
->decl
,
217 SET_DECL_ASSEMBLER_NAME (new_decl
, DECL_NAME (new_decl
));
219 /* We need to force DECL_IGNORED_P because the new thunk is created after
220 early debug was run. */
221 DECL_IGNORED_P (new_decl
) = 1;
223 new_thunk
= cgraph_node::create (new_decl
);
224 set_new_clone_decl_and_node_flags (new_thunk
);
225 new_thunk
->definition
= true;
226 new_thunk
->can_change_signature
= node
->can_change_signature
;
227 new_thunk
->thunk
= thunk
->thunk
;
228 new_thunk
->unique_name
= in_lto_p
;
229 new_thunk
->former_clone_of
= thunk
->decl
;
230 new_thunk
->clone
.param_adjustments
= node
->clone
.param_adjustments
;
232 cgraph_edge
*e
= new_thunk
->create_edge (node
, NULL
, new_thunk
->count
);
233 symtab
->call_edge_duplication_hooks (thunk
->callees
, e
);
234 symtab
->call_cgraph_duplication_hooks (thunk
, new_thunk
);
238 /* If E does not lead to a thunk, simply redirect it to N. Otherwise create
239 one or more equivalent thunks for N and redirect E to the first in the
240 chain. Note that it is then necessary to call
241 n->expand_all_artificial_thunks once all callers are redirected. */
244 cgraph_edge::redirect_callee_duplicating_thunks (cgraph_node
*n
)
246 cgraph_node
*orig_to
= callee
->ultimate_alias_target ();
247 if (orig_to
->thunk
.thunk_p
)
248 n
= duplicate_thunk_for_node (orig_to
, n
);
253 /* Call expand_thunk on all callers that are thunks and if analyze those nodes
254 that were expanded. */
257 cgraph_node::expand_all_artificial_thunks ()
260 for (e
= callers
; e
;)
261 if (e
->caller
->thunk
.thunk_p
)
263 cgraph_node
*thunk
= e
->caller
;
266 if (thunk
->expand_thunk (false, false))
268 thunk
->thunk
.thunk_p
= false;
271 thunk
->expand_all_artificial_thunks ();
278 dump_callgraph_transformation (const cgraph_node
*original
,
279 const cgraph_node
*clone
,
282 if (symtab
->ipa_clones_dump_file
)
284 fprintf (symtab
->ipa_clones_dump_file
,
285 "Callgraph clone;%s;%d;%s;%d;%d;%s;%d;%s;%d;%d;%s\n",
286 original
->asm_name (), original
->order
,
287 DECL_SOURCE_FILE (original
->decl
),
288 DECL_SOURCE_LINE (original
->decl
),
289 DECL_SOURCE_COLUMN (original
->decl
), clone
->asm_name (),
290 clone
->order
, DECL_SOURCE_FILE (clone
->decl
),
291 DECL_SOURCE_LINE (clone
->decl
), DECL_SOURCE_COLUMN (clone
->decl
),
294 symtab
->cloned_nodes
.add (original
);
295 symtab
->cloned_nodes
.add (clone
);
299 /* Create node representing clone of N executed COUNT times. Decrease
300 the execution counts from original node too.
301 The new clone will have decl set to DECL that may or may not be the same
304 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
305 function's profile to reflect the fact that part of execution is handled
307 When CALL_DUPLICATION_HOOK is true, the ipa passes are acknowledged about
308 the new clone. Otherwise the caller is responsible for doing so later.
310 If the new node is being inlined into another one, NEW_INLINED_TO should be
311 the outline function the new one is (even indirectly) inlined to. All hooks
312 will see this in node's inlined_to, when invoked. Can be NULL if the
315 If PARAM_ADJUSTMENTS is non-NULL, the parameter manipulation information
316 will be overwritten by the new structure. Otherwise the new node will
317 share parameter manipulation information with the original node. */
320 cgraph_node::create_clone (tree new_decl
, profile_count prof_count
,
321 bool update_original
,
322 vec
<cgraph_edge
*> redirect_callers
,
323 bool call_duplication_hook
,
324 cgraph_node
*new_inlined_to
,
325 ipa_param_adjustments
*param_adjustments
,
328 cgraph_node
*new_node
= symtab
->create_empty ();
331 profile_count old_count
= count
;
334 dump_callgraph_transformation (this, new_inlined_to
, "inlining to");
336 /* When inlining we scale precisely to prof_count, when cloning we can
337 preserve local profile. */
339 prof_count
= count
.combine_with_ipa_count (prof_count
);
340 new_node
->count
= prof_count
;
342 /* Update IPA profile. Local profiles need no updating in original. */
344 count
= count
.combine_with_ipa_count (count
.ipa () - prof_count
.ipa ());
345 new_node
->decl
= new_decl
;
346 new_node
->register_symbol ();
347 new_node
->origin
= origin
;
348 new_node
->lto_file_data
= lto_file_data
;
349 if (new_node
->origin
)
351 new_node
->next_nested
= new_node
->origin
->nested
;
352 new_node
->origin
->nested
= new_node
;
354 new_node
->analyzed
= analyzed
;
355 new_node
->definition
= definition
;
356 new_node
->versionable
= versionable
;
357 new_node
->can_change_signature
= can_change_signature
;
358 new_node
->redefined_extern_inline
= redefined_extern_inline
;
359 new_node
->tm_may_enter_irr
= tm_may_enter_irr
;
360 new_node
->externally_visible
= false;
361 new_node
->no_reorder
= no_reorder
;
362 new_node
->local
= true;
363 new_node
->inlined_to
= new_inlined_to
;
365 new_node
->frequency
= frequency
;
366 new_node
->tp_first_run
= tp_first_run
;
367 new_node
->tm_clone
= tm_clone
;
368 new_node
->icf_merged
= icf_merged
;
369 new_node
->merged_comdat
= merged_comdat
;
370 new_node
->thunk
= thunk
;
372 if (param_adjustments
)
373 new_node
->clone
.param_adjustments
= param_adjustments
;
375 new_node
->clone
.param_adjustments
= clone
.param_adjustments
;
376 new_node
->clone
.tree_map
= NULL
;
377 new_node
->clone
.performed_splits
= vec_safe_copy (clone
.performed_splits
);
378 new_node
->split_part
= split_part
;
380 FOR_EACH_VEC_ELT (redirect_callers
, i
, e
)
382 /* Redirect calls to the old version node to point to its new
383 version. The only exception is when the edge was proved to
384 be unreachable during the cloning procedure. */
386 || !fndecl_built_in_p (e
->callee
->decl
, BUILT_IN_UNREACHABLE
))
387 e
->redirect_callee_duplicating_thunks (new_node
);
389 new_node
->expand_all_artificial_thunks ();
391 for (e
= callees
;e
; e
=e
->next_callee
)
392 e
->clone (new_node
, e
->call_stmt
, e
->lto_stmt_uid
, new_node
->count
, old_count
,
395 for (e
= indirect_calls
; e
; e
= e
->next_callee
)
396 e
->clone (new_node
, e
->call_stmt
, e
->lto_stmt_uid
,
397 new_node
->count
, old_count
, update_original
);
398 new_node
->clone_references (this);
400 new_node
->next_sibling_clone
= clones
;
402 clones
->prev_sibling_clone
= new_node
;
404 new_node
->clone_of
= this;
406 if (call_duplication_hook
)
407 symtab
->call_cgraph_duplication_hooks (this, new_node
);
410 dump_callgraph_transformation (this, new_node
, suffix
);
415 static GTY(()) hash_map
<const char *, unsigned> *clone_fn_ids
;
417 /* Return a new assembler name for a clone of decl named NAME. Apart
418 from the string SUFFIX, the new name will end with a unique (for
419 each NAME) unspecified number. If clone numbering is not needed
420 then the two argument clone_function_name should be used instead.
421 Should not be called directly except for by
422 lto-partition.c:privatize_symbol_name_1. */
425 clone_function_name_numbered (const char *name
, const char *suffix
)
427 /* Initialize the function->counter mapping the first time it's
430 clone_fn_ids
= hash_map
<const char *, unsigned int>::create_ggc (64);
431 unsigned int &suffix_counter
= clone_fn_ids
->get_or_insert (
432 IDENTIFIER_POINTER (get_identifier (name
)));
433 return clone_function_name (name
, suffix
, suffix_counter
++);
436 /* Return a new assembler name for a clone of DECL. Apart from string
437 SUFFIX, the new name will end with a unique (for each DECL
438 assembler name) unspecified number. If clone numbering is not
439 needed then the two argument clone_function_name should be used
443 clone_function_name_numbered (tree decl
, const char *suffix
)
445 tree name
= DECL_ASSEMBLER_NAME (decl
);
446 return clone_function_name_numbered (IDENTIFIER_POINTER (name
),
450 /* Return a new assembler name for a clone of decl named NAME. Apart
451 from the string SUFFIX, the new name will end with the specified
452 NUMBER. If clone numbering is not needed then the two argument
453 clone_function_name should be used instead. */
456 clone_function_name (const char *name
, const char *suffix
,
457 unsigned long number
)
459 size_t len
= strlen (name
);
460 char *tmp_name
, *prefix
;
462 prefix
= XALLOCAVEC (char, len
+ strlen (suffix
) + 2);
463 memcpy (prefix
, name
, len
);
464 strcpy (prefix
+ len
+ 1, suffix
);
465 prefix
[len
] = symbol_table::symbol_suffix_separator ();
466 ASM_FORMAT_PRIVATE_NAME (tmp_name
, prefix
, number
);
467 return get_identifier (tmp_name
);
470 /* Return a new assembler name for a clone of DECL. Apart from the
471 string SUFFIX, the new name will end with the specified NUMBER. If
472 clone numbering is not needed then the two argument
473 clone_function_name should be used instead. */
476 clone_function_name (tree decl
, const char *suffix
,
477 unsigned long number
)
479 return clone_function_name (
480 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
)), suffix
, number
);
483 /* Return a new assembler name ending with the string SUFFIX for a
487 clone_function_name (tree decl
, const char *suffix
)
489 tree identifier
= DECL_ASSEMBLER_NAME (decl
);
490 /* For consistency this needs to behave the same way as
491 ASM_FORMAT_PRIVATE_NAME does, but without the final number
493 char *separator
= XALLOCAVEC (char, 2);
494 separator
[0] = symbol_table::symbol_suffix_separator ();
496 #if defined (NO_DOT_IN_LABEL) && defined (NO_DOLLAR_IN_LABEL)
497 const char *prefix
= "__";
499 const char *prefix
= "";
501 char *result
= ACONCAT ((prefix
,
502 IDENTIFIER_POINTER (identifier
),
506 return get_identifier (result
);
510 /* Create callgraph node clone with new declaration. The actual body will be
511 copied later at compilation stage. The name of the new clone will be
512 constructed from the name of the original node, SUFFIX and NUM_SUFFIX.
514 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
518 cgraph_node::create_virtual_clone (vec
<cgraph_edge
*> redirect_callers
,
519 vec
<ipa_replace_map
*, va_gc
> *tree_map
,
520 ipa_param_adjustments
*param_adjustments
,
521 const char * suffix
, unsigned num_suffix
)
523 tree old_decl
= decl
;
524 cgraph_node
*new_node
= NULL
;
527 ipa_replace_map
*map
;
530 gcc_checking_assert (versionable
);
531 /* TODO: It would be nice if we could recognize that param_adjustments do not
532 actually perform any changes, but at the moment let's require it simply
534 gcc_assert (can_change_signature
|| !param_adjustments
);
536 /* Make a new FUNCTION_DECL tree node */
537 if (!param_adjustments
)
538 new_decl
= copy_node (old_decl
);
540 new_decl
= param_adjustments
->adjust_decl (old_decl
);
542 /* These pointers represent function body and will be populated only when clone
544 gcc_assert (new_decl
!= old_decl
);
545 DECL_STRUCT_FUNCTION (new_decl
) = NULL
;
546 DECL_ARGUMENTS (new_decl
) = NULL
;
547 DECL_INITIAL (new_decl
) = NULL
;
548 DECL_RESULT (new_decl
) = NULL
;
549 /* We cannot do DECL_RESULT (new_decl) = NULL; here because of LTO partitioning
550 sometimes storing only clone decl instead of original. */
552 /* Generate a new name for the new version. */
553 len
= IDENTIFIER_LENGTH (DECL_NAME (old_decl
));
554 name
= XALLOCAVEC (char, len
+ strlen (suffix
) + 2);
555 memcpy (name
, IDENTIFIER_POINTER (DECL_NAME (old_decl
)), len
);
556 strcpy (name
+ len
+ 1, suffix
);
558 DECL_NAME (new_decl
) = get_identifier (name
);
559 SET_DECL_ASSEMBLER_NAME (new_decl
,
560 clone_function_name (old_decl
, suffix
, num_suffix
));
561 SET_DECL_RTL (new_decl
, NULL
);
563 new_node
= create_clone (new_decl
, count
, false,
564 redirect_callers
, false, NULL
, param_adjustments
,
567 /* Update the properties.
568 Make clone visible only within this translation unit. Make sure
569 that is not weak also.
570 ??? We cannot use COMDAT linkage because there is no
571 ABI support for this. */
572 set_new_clone_decl_and_node_flags (new_node
);
573 new_node
->ipcp_clone
= ipcp_clone
;
574 new_node
->clone
.tree_map
= tree_map
;
575 if (!implicit_section
)
576 new_node
->set_section (get_section ());
578 /* Clones of global symbols or symbols with unique names are unique. */
579 if ((TREE_PUBLIC (old_decl
)
580 && !DECL_EXTERNAL (old_decl
)
581 && !DECL_WEAK (old_decl
)
582 && !DECL_COMDAT (old_decl
))
584 new_node
->unique_name
= true;
585 FOR_EACH_VEC_SAFE_ELT (tree_map
, i
, map
)
586 new_node
->maybe_create_reference (map
->new_tree
, NULL
);
588 if (ipa_transforms_to_apply
.exists ())
589 new_node
->ipa_transforms_to_apply
590 = ipa_transforms_to_apply
.copy ();
592 symtab
->call_cgraph_duplication_hooks (this, new_node
);
597 /* callgraph node being removed from symbol table; see if its entry can be
598 replaced by other inline clone. */
600 cgraph_node::find_replacement (void)
602 cgraph_node
*next_inline_clone
, *replacement
;
604 for (next_inline_clone
= clones
;
606 && next_inline_clone
->decl
!= decl
;
607 next_inline_clone
= next_inline_clone
->next_sibling_clone
)
610 /* If there is inline clone of the node being removed, we need
611 to put it into the position of removed node and reorganize all
612 other clones to be based on it. */
613 if (next_inline_clone
)
616 cgraph_node
*new_clones
;
618 replacement
= next_inline_clone
;
620 /* Unlink inline clone from the list of clones of removed node. */
621 if (next_inline_clone
->next_sibling_clone
)
622 next_inline_clone
->next_sibling_clone
->prev_sibling_clone
623 = next_inline_clone
->prev_sibling_clone
;
624 if (next_inline_clone
->prev_sibling_clone
)
626 gcc_assert (clones
!= next_inline_clone
);
627 next_inline_clone
->prev_sibling_clone
->next_sibling_clone
628 = next_inline_clone
->next_sibling_clone
;
632 gcc_assert (clones
== next_inline_clone
);
633 clones
= next_inline_clone
->next_sibling_clone
;
639 /* Copy clone info. */
640 next_inline_clone
->clone
= clone
;
642 /* Now place it into clone tree at same level at NODE. */
643 next_inline_clone
->clone_of
= clone_of
;
644 next_inline_clone
->prev_sibling_clone
= NULL
;
645 next_inline_clone
->next_sibling_clone
= NULL
;
648 if (clone_of
->clones
)
649 clone_of
->clones
->prev_sibling_clone
= next_inline_clone
;
650 next_inline_clone
->next_sibling_clone
= clone_of
->clones
;
651 clone_of
->clones
= next_inline_clone
;
654 /* Merge the clone list. */
657 if (!next_inline_clone
->clones
)
658 next_inline_clone
->clones
= new_clones
;
661 n
= next_inline_clone
->clones
;
662 while (n
->next_sibling_clone
)
663 n
= n
->next_sibling_clone
;
664 n
->next_sibling_clone
= new_clones
;
665 new_clones
->prev_sibling_clone
= n
;
669 /* Update clone_of pointers. */
673 n
->clone_of
= next_inline_clone
;
674 n
= n
->next_sibling_clone
;
677 /* Update order in order to be able to find a LTO section
678 with function body. */
679 replacement
->order
= order
;
687 /* Like cgraph_set_call_stmt but walk the clone tree and update all
688 clones sharing the same function body.
689 When WHOLE_SPECULATIVE_EDGES is true, all three components of
690 speculative edge gets updated. Otherwise we update only direct
694 cgraph_node::set_call_stmt_including_clones (gimple
*old_stmt
,
696 bool update_speculative
)
699 cgraph_edge
*edge
= get_edge (old_stmt
);
702 edge
->set_call_stmt (new_stmt
, update_speculative
);
708 cgraph_edge
*edge
= node
->get_edge (old_stmt
);
711 edge
->set_call_stmt (new_stmt
, update_speculative
);
712 /* If UPDATE_SPECULATIVE is false, it means that we are turning
713 speculative call into a real code sequence. Update the
715 if (edge
->speculative
&& !update_speculative
)
717 cgraph_edge
*direct
, *indirect
;
720 gcc_assert (!edge
->indirect_unknown_callee
);
721 edge
->speculative_call_info (direct
, indirect
, ref
);
722 direct
->speculative
= false;
723 indirect
->speculative
= false;
724 ref
->speculative
= false;
729 else if (node
->next_sibling_clone
)
730 node
= node
->next_sibling_clone
;
733 while (node
!= this && !node
->next_sibling_clone
)
734 node
= node
->clone_of
;
736 node
= node
->next_sibling_clone
;
741 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
742 same function body. If clones already have edge for OLD_STMT; only
743 update the edge same way as cgraph_set_call_stmt_including_clones does.
745 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
746 frequencies of the clones. */
749 cgraph_node::create_edge_including_clones (cgraph_node
*callee
,
750 gimple
*old_stmt
, gcall
*stmt
,
752 cgraph_inline_failed_t reason
)
757 if (!get_edge (stmt
))
759 edge
= create_edge (callee
, stmt
, count
);
760 edge
->inline_failed
= reason
;
766 /* Thunk clones do not get updated while copying inline function body. */
767 if (!node
->thunk
.thunk_p
)
769 cgraph_edge
*edge
= node
->get_edge (old_stmt
);
771 /* It is possible that clones already contain the edge while
772 master didn't. Either we promoted indirect call into direct
773 call in the clone or we are processing clones of unreachable
774 master where edges has been removed. */
776 edge
->set_call_stmt (stmt
);
777 else if (! node
->get_edge (stmt
))
779 edge
= node
->create_edge (callee
, stmt
, count
);
780 edge
->inline_failed
= reason
;
785 else if (node
->next_sibling_clone
)
786 node
= node
->next_sibling_clone
;
789 while (node
!= this && !node
->next_sibling_clone
)
790 node
= node
->clone_of
;
792 node
= node
->next_sibling_clone
;
797 /* Remove the node from cgraph and all inline clones inlined into it.
798 Skip however removal of FORBIDDEN_NODE and return true if it needs to be
799 removed. This allows to call the function from outer loop walking clone
803 cgraph_node::remove_symbol_and_inline_clones (cgraph_node
*forbidden_node
)
805 cgraph_edge
*e
, *next
;
808 if (this == forbidden_node
)
813 for (e
= callees
; e
; e
= next
)
815 next
= e
->next_callee
;
816 if (!e
->inline_failed
)
817 found
|= e
->callee
->remove_symbol_and_inline_clones (forbidden_node
);
823 /* The edges representing the callers of the NEW_VERSION node were
824 fixed by cgraph_function_versioning (), now the call_expr in their
825 respective tree code should be updated to call the NEW_VERSION. */
828 update_call_expr (cgraph_node
*new_version
)
832 gcc_assert (new_version
);
834 /* Update the call expr on the edges to call the new version. */
835 for (e
= new_version
->callers
; e
; e
= e
->next_caller
)
837 function
*inner_function
= DECL_STRUCT_FUNCTION (e
->caller
->decl
);
838 gimple_call_set_fndecl (e
->call_stmt
, new_version
->decl
);
839 maybe_clean_eh_stmt_fn (inner_function
, e
->call_stmt
);
844 /* Create a new cgraph node which is the new version of
845 callgraph node. REDIRECT_CALLERS holds the callers
846 edges which should be redirected to point to
847 NEW_VERSION. ALL the callees edges of the node
848 are cloned to the new version node. Return the new
851 If non-NULL BLOCK_TO_COPY determine what basic blocks
852 was copied to prevent duplications of calls that are dead
856 cgraph_node::create_version_clone (tree new_decl
,
857 vec
<cgraph_edge
*> redirect_callers
,
861 cgraph_node
*new_version
;
865 new_version
= cgraph_node::create (new_decl
);
867 new_version
->analyzed
= analyzed
;
868 new_version
->definition
= definition
;
869 new_version
->local
= local
;
870 new_version
->externally_visible
= false;
871 new_version
->no_reorder
= no_reorder
;
872 new_version
->local
= new_version
->definition
;
873 new_version
->inlined_to
= inlined_to
;
874 new_version
->rtl
= rtl
;
875 new_version
->count
= count
;
877 for (e
= callees
; e
; e
=e
->next_callee
)
879 || bitmap_bit_p (bbs_to_copy
, gimple_bb (e
->call_stmt
)->index
))
880 e
->clone (new_version
, e
->call_stmt
,
881 e
->lto_stmt_uid
, count
, count
,
883 for (e
= indirect_calls
; e
; e
=e
->next_callee
)
885 || bitmap_bit_p (bbs_to_copy
, gimple_bb (e
->call_stmt
)->index
))
886 e
->clone (new_version
, e
->call_stmt
,
887 e
->lto_stmt_uid
, count
, count
,
889 FOR_EACH_VEC_ELT (redirect_callers
, i
, e
)
891 /* Redirect calls to the old version node to point to its new
893 e
->redirect_callee (new_version
);
896 dump_callgraph_transformation (this, new_version
, suffix
);
901 /* Perform function versioning.
902 Function versioning includes copying of the tree and
903 a callgraph update (creating a new cgraph node and updating
904 its callees and callers).
906 REDIRECT_CALLERS varray includes the edges to be redirected
909 TREE_MAP is a mapping of tree nodes we want to replace with
910 new ones (according to results of prior analysis).
912 If non-NULL ARGS_TO_SKIP determine function parameters to remove
914 If SKIP_RETURN is true, the new version will return void.
915 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
916 If non_NULL NEW_ENTRY determine new entry BB of the clone.
918 If TARGET_ATTRIBUTES is non-null, when creating a new declaration,
919 add the attributes to DECL_ATTRIBUTES. And call valid_attribute_p
920 that will promote value of the attribute DECL_FUNCTION_SPECIFIC_TARGET
923 Return the new version's cgraph node. */
926 cgraph_node::create_version_clone_with_body
927 (vec
<cgraph_edge
*> redirect_callers
,
928 vec
<ipa_replace_map
*, va_gc
> *tree_map
,
929 ipa_param_adjustments
*param_adjustments
,
930 bitmap bbs_to_copy
, basic_block new_entry_block
, const char *suffix
,
931 tree target_attributes
)
933 tree old_decl
= decl
;
934 cgraph_node
*new_version_node
= NULL
;
937 if (!tree_versionable_function_p (old_decl
))
940 /* TODO: Restore an assert that we do not change signature if
941 can_change_signature is false. We cannot just check that
942 param_adjustments is NULL because unfortunately ipa-split removes return
943 values from such functions. */
945 /* Make a new FUNCTION_DECL tree node for the new version. */
946 if (param_adjustments
)
947 new_decl
= param_adjustments
->adjust_decl (old_decl
);
949 new_decl
= copy_node (old_decl
);
951 /* Generate a new name for the new version. */
952 DECL_NAME (new_decl
) = clone_function_name_numbered (old_decl
, suffix
);
953 SET_DECL_ASSEMBLER_NAME (new_decl
, DECL_NAME (new_decl
));
954 SET_DECL_RTL (new_decl
, NULL
);
956 DECL_VIRTUAL_P (new_decl
) = 0;
958 if (target_attributes
)
960 DECL_ATTRIBUTES (new_decl
) = target_attributes
;
962 location_t saved_loc
= input_location
;
963 tree v
= TREE_VALUE (target_attributes
);
964 input_location
= DECL_SOURCE_LOCATION (new_decl
);
965 bool r
= targetm
.target_option
.valid_attribute_p (new_decl
, NULL
, v
, 1);
966 input_location
= saved_loc
;
971 /* When the old decl was a con-/destructor make sure the clone isn't. */
972 DECL_STATIC_CONSTRUCTOR (new_decl
) = 0;
973 DECL_STATIC_DESTRUCTOR (new_decl
) = 0;
974 DECL_SET_IS_OPERATOR_NEW (new_decl
, 0);
975 DECL_SET_IS_OPERATOR_DELETE (new_decl
, 0);
977 /* Create the new version's call-graph node.
978 and update the edges of the new node. */
979 new_version_node
= create_version_clone (new_decl
, redirect_callers
,
980 bbs_to_copy
, suffix
);
982 if (ipa_transforms_to_apply
.exists ())
983 new_version_node
->ipa_transforms_to_apply
984 = ipa_transforms_to_apply
.copy ();
985 /* Copy the OLD_VERSION_NODE function tree to the new version. */
986 tree_function_versioning (old_decl
, new_decl
, tree_map
, param_adjustments
,
987 false, bbs_to_copy
, new_entry_block
);
989 /* Update the new version's properties.
990 Make The new version visible only within this translation unit. Make sure
991 that is not weak also.
992 ??? We cannot use COMDAT linkage because there is no
993 ABI support for this. */
994 new_version_node
->make_decl_local ();
995 DECL_VIRTUAL_P (new_version_node
->decl
) = 0;
996 new_version_node
->externally_visible
= 0;
997 new_version_node
->local
= 1;
998 new_version_node
->lowered
= true;
999 if (!implicit_section
)
1000 new_version_node
->set_section (get_section ());
1001 /* Clones of global symbols or symbols with unique names are unique. */
1002 if ((TREE_PUBLIC (old_decl
)
1003 && !DECL_EXTERNAL (old_decl
)
1004 && !DECL_WEAK (old_decl
)
1005 && !DECL_COMDAT (old_decl
))
1007 new_version_node
->unique_name
= true;
1009 /* Update the call_expr on the edges to call the new version node. */
1010 update_call_expr (new_version_node
);
1012 symtab
->call_cgraph_insertion_hooks (new_version_node
);
1013 return new_version_node
;
1016 /* Given virtual clone, turn it into actual clone. */
1019 cgraph_materialize_clone (cgraph_node
*node
)
1021 bitmap_obstack_initialize (NULL
);
1022 node
->former_clone_of
= node
->clone_of
->decl
;
1023 if (node
->clone_of
->former_clone_of
)
1024 node
->former_clone_of
= node
->clone_of
->former_clone_of
;
1025 /* Copy the OLD_VERSION_NODE function tree to the new version. */
1026 tree_function_versioning (node
->clone_of
->decl
, node
->decl
,
1027 node
->clone
.tree_map
, node
->clone
.param_adjustments
,
1029 if (symtab
->dump_file
)
1031 dump_function_to_file (node
->clone_of
->decl
, symtab
->dump_file
,
1033 dump_function_to_file (node
->decl
, symtab
->dump_file
, dump_flags
);
1036 /* Function is no longer clone. */
1037 if (node
->next_sibling_clone
)
1038 node
->next_sibling_clone
->prev_sibling_clone
= node
->prev_sibling_clone
;
1039 if (node
->prev_sibling_clone
)
1040 node
->prev_sibling_clone
->next_sibling_clone
= node
->next_sibling_clone
;
1042 node
->clone_of
->clones
= node
->next_sibling_clone
;
1043 node
->next_sibling_clone
= NULL
;
1044 node
->prev_sibling_clone
= NULL
;
1045 if (!node
->clone_of
->analyzed
&& !node
->clone_of
->clones
)
1047 node
->clone_of
->release_body ();
1048 node
->clone_of
->remove_callees ();
1049 node
->clone_of
->remove_all_references ();
1051 node
->clone_of
= NULL
;
1052 bitmap_obstack_release (NULL
);
1055 /* Once all functions from compilation unit are in memory, produce all clones
1056 and update all calls. We might also do this on demand if we don't want to
1057 bring all functions to memory prior compilation, but current WHOPR
1058 implementation does that and it is a bit easier to keep everything right in
1062 symbol_table::materialize_all_clones (void)
1065 bool stabilized
= false;
1068 if (symtab
->dump_file
)
1069 fprintf (symtab
->dump_file
, "Materializing clones\n");
1071 cgraph_node::checking_verify_cgraph_nodes ();
1073 /* We can also do topological order, but number of iterations should be
1074 bounded by number of IPA passes since single IPA pass is probably not
1075 going to create clones of clones it created itself. */
1079 FOR_EACH_FUNCTION (node
)
1081 if (node
->clone_of
&& node
->decl
!= node
->clone_of
->decl
1082 && !gimple_has_body_p (node
->decl
))
1084 if (!node
->clone_of
->clone_of
)
1085 node
->clone_of
->get_untransformed_body ();
1086 if (gimple_has_body_p (node
->clone_of
->decl
))
1088 if (symtab
->dump_file
)
1090 fprintf (symtab
->dump_file
, "cloning %s to %s\n",
1091 xstrdup_for_dump (node
->clone_of
->name ()),
1092 xstrdup_for_dump (node
->name ()));
1093 if (node
->clone
.tree_map
)
1096 fprintf (symtab
->dump_file
, " replace map: ");
1098 i
< vec_safe_length (node
->clone
.tree_map
);
1101 ipa_replace_map
*replace_info
;
1102 replace_info
= (*node
->clone
.tree_map
)[i
];
1103 fprintf (symtab
->dump_file
, "%i -> ",
1104 (*node
->clone
.tree_map
)[i
]->parm_num
);
1105 print_generic_expr (symtab
->dump_file
,
1106 replace_info
->new_tree
);
1108 fprintf (symtab
->dump_file
, "\n");
1110 if (node
->clone
.param_adjustments
)
1111 node
->clone
.param_adjustments
->dump (symtab
->dump_file
);
1113 cgraph_materialize_clone (node
);
1119 FOR_EACH_FUNCTION (node
)
1120 if (!node
->analyzed
&& node
->callees
)
1122 node
->remove_callees ();
1123 node
->remove_all_references ();
1126 node
->clear_stmts_in_references ();
1127 if (symtab
->dump_file
)
1128 fprintf (symtab
->dump_file
, "Materialization Call site updates done.\n");
1130 cgraph_node::checking_verify_cgraph_nodes ();
1132 symtab
->remove_unreachable_nodes (symtab
->dump_file
);
1135 #include "gt-cgraphclones.h"