2 Copyright (C) 2003-2017 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 clonning functions. I.e. creating
22 new functions based on existing functions with simple modifications,
23 such as replacement of parameters.
25 To allow whole program optimization without actual presence of function
26 bodies, an additional infrastructure is provided for so-called virtual
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 gcov_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
, gcov_count
);
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
, gcov_count
);
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
.ipa () == count
&& new_edge
->count
.ipa () == new_edge
->count
)
140 count
-= new_edge
->count
;
141 else if (caller
->count
.global0 () == caller
->count
142 && !(count
== profile_count::zero ()))
143 count
= count
.global0 ();
144 symtab
->call_edge_duplication_hooks (this, new_edge
);
148 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP and the
149 return value if SKIP_RETURN is true. */
152 cgraph_build_function_type_skip_args (tree orig_type
, bitmap args_to_skip
,
155 tree new_type
= NULL
;
156 tree args
, new_args
= NULL
;
160 for (args
= TYPE_ARG_TYPES (orig_type
); args
&& args
!= void_list_node
;
161 args
= TREE_CHAIN (args
), i
++)
162 if (!args_to_skip
|| !bitmap_bit_p (args_to_skip
, i
))
163 new_args
= tree_cons (NULL_TREE
, TREE_VALUE (args
), new_args
);
165 new_reversed
= nreverse (new_args
);
169 TREE_CHAIN (new_args
) = void_list_node
;
171 new_reversed
= void_list_node
;
174 /* Use copy_node to preserve as much as possible from original type
175 (debug info, attribute lists etc.)
176 Exception is METHOD_TYPEs must have THIS argument.
177 When we are asked to remove it, we need to build new FUNCTION_TYPE
179 if (TREE_CODE (orig_type
) != METHOD_TYPE
181 || !bitmap_bit_p (args_to_skip
, 0))
183 new_type
= build_distinct_type_copy (orig_type
);
184 TYPE_ARG_TYPES (new_type
) = new_reversed
;
189 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type
),
191 TYPE_CONTEXT (new_type
) = TYPE_CONTEXT (orig_type
);
195 TREE_TYPE (new_type
) = void_type_node
;
200 /* Build variant of function decl ORIG_DECL skipping ARGS_TO_SKIP and the
201 return value if SKIP_RETURN is true.
203 Arguments from DECL_ARGUMENTS list can't be removed now, since they are
204 linked by TREE_CHAIN directly. The caller is responsible for eliminating
205 them when they are being duplicated (i.e. copy_arguments_for_versioning). */
208 build_function_decl_skip_args (tree orig_decl
, bitmap args_to_skip
,
211 tree new_decl
= copy_node (orig_decl
);
214 new_type
= TREE_TYPE (orig_decl
);
215 if (prototype_p (new_type
)
216 || (skip_return
&& !VOID_TYPE_P (TREE_TYPE (new_type
))))
218 = cgraph_build_function_type_skip_args (new_type
, args_to_skip
,
220 TREE_TYPE (new_decl
) = new_type
;
222 /* For declarations setting DECL_VINDEX (i.e. methods)
223 we expect first argument to be THIS pointer. */
224 if (args_to_skip
&& bitmap_bit_p (args_to_skip
, 0))
225 DECL_VINDEX (new_decl
) = NULL_TREE
;
227 /* When signature changes, we need to clear builtin info. */
228 if (DECL_BUILT_IN (new_decl
)
230 && !bitmap_empty_p (args_to_skip
))
232 DECL_BUILT_IN_CLASS (new_decl
) = NOT_BUILT_IN
;
233 DECL_FUNCTION_CODE (new_decl
) = (enum built_in_function
) 0;
235 /* The FE might have information and assumptions about the other
237 DECL_LANG_SPECIFIC (new_decl
) = NULL
;
241 /* Set flags of NEW_NODE and its decl. NEW_NODE is a newly created private
242 clone or its thunk. */
245 set_new_clone_decl_and_node_flags (cgraph_node
*new_node
)
247 DECL_EXTERNAL (new_node
->decl
) = 0;
248 TREE_PUBLIC (new_node
->decl
) = 0;
249 DECL_COMDAT (new_node
->decl
) = 0;
250 DECL_WEAK (new_node
->decl
) = 0;
251 DECL_VIRTUAL_P (new_node
->decl
) = 0;
252 DECL_STATIC_CONSTRUCTOR (new_node
->decl
) = 0;
253 DECL_STATIC_DESTRUCTOR (new_node
->decl
) = 0;
255 new_node
->externally_visible
= 0;
256 new_node
->local
.local
= 1;
257 new_node
->lowered
= true;
260 /* Duplicate thunk THUNK if necessary but make it to refer to NODE.
261 ARGS_TO_SKIP, if non-NULL, determines which parameters should be omitted.
262 Function can return NODE if no thunk is necessary, which can happen when
263 thunk is this_adjusting but we are removing this parameter. */
266 duplicate_thunk_for_node (cgraph_node
*thunk
, cgraph_node
*node
)
268 cgraph_node
*new_thunk
, *thunk_of
;
269 thunk_of
= thunk
->callees
->callee
->ultimate_alias_target ();
271 if (thunk_of
->thunk
.thunk_p
)
272 node
= duplicate_thunk_for_node (thunk_of
, node
);
274 if (!DECL_ARGUMENTS (thunk
->decl
))
275 thunk
->get_untransformed_body ();
278 for (cs
= node
->callers
; cs
; cs
= cs
->next_caller
)
279 if (cs
->caller
->thunk
.thunk_p
280 && cs
->caller
->thunk
.this_adjusting
== thunk
->thunk
.this_adjusting
281 && cs
->caller
->thunk
.fixed_offset
== thunk
->thunk
.fixed_offset
282 && cs
->caller
->thunk
.virtual_offset_p
== thunk
->thunk
.virtual_offset_p
283 && cs
->caller
->thunk
.virtual_value
== thunk
->thunk
.virtual_value
)
287 if (!node
->clone
.args_to_skip
)
288 new_decl
= copy_node (thunk
->decl
);
291 /* We do not need to duplicate this_adjusting thunks if we have removed
293 if (thunk
->thunk
.this_adjusting
294 && bitmap_bit_p (node
->clone
.args_to_skip
, 0))
297 new_decl
= build_function_decl_skip_args (thunk
->decl
,
298 node
->clone
.args_to_skip
,
302 tree
*link
= &DECL_ARGUMENTS (new_decl
);
304 for (tree pd
= DECL_ARGUMENTS (thunk
->decl
); pd
; pd
= DECL_CHAIN (pd
), i
++)
306 if (!node
->clone
.args_to_skip
307 || !bitmap_bit_p (node
->clone
.args_to_skip
, i
))
309 tree nd
= copy_node (pd
);
310 DECL_CONTEXT (nd
) = new_decl
;
312 link
= &DECL_CHAIN (nd
);
317 gcc_checking_assert (!DECL_STRUCT_FUNCTION (new_decl
));
318 gcc_checking_assert (!DECL_INITIAL (new_decl
));
319 gcc_checking_assert (!DECL_RESULT (new_decl
));
320 gcc_checking_assert (!DECL_RTL_SET_P (new_decl
));
322 DECL_NAME (new_decl
) = clone_function_name (thunk
->decl
, "artificial_thunk");
323 SET_DECL_ASSEMBLER_NAME (new_decl
, DECL_NAME (new_decl
));
325 new_thunk
= cgraph_node::create (new_decl
);
326 set_new_clone_decl_and_node_flags (new_thunk
);
327 new_thunk
->definition
= true;
328 new_thunk
->local
.can_change_signature
= node
->local
.can_change_signature
;
329 new_thunk
->thunk
= thunk
->thunk
;
330 new_thunk
->unique_name
= in_lto_p
;
331 new_thunk
->former_clone_of
= thunk
->decl
;
332 new_thunk
->clone
.args_to_skip
= node
->clone
.args_to_skip
;
333 new_thunk
->clone
.combined_args_to_skip
= node
->clone
.combined_args_to_skip
;
335 cgraph_edge
*e
= new_thunk
->create_edge (node
, NULL
, new_thunk
->count
);
336 symtab
->call_edge_duplication_hooks (thunk
->callees
, e
);
337 symtab
->call_cgraph_duplication_hooks (thunk
, new_thunk
);
341 /* If E does not lead to a thunk, simply redirect it to N. Otherwise create
342 one or more equivalent thunks for N and redirect E to the first in the
343 chain. Note that it is then necessary to call
344 n->expand_all_artificial_thunks once all callers are redirected. */
347 cgraph_edge::redirect_callee_duplicating_thunks (cgraph_node
*n
)
349 cgraph_node
*orig_to
= callee
->ultimate_alias_target ();
350 if (orig_to
->thunk
.thunk_p
)
351 n
= duplicate_thunk_for_node (orig_to
, n
);
356 /* Call expand_thunk on all callers that are thunks and if analyze those nodes
357 that were expanded. */
360 cgraph_node::expand_all_artificial_thunks ()
363 for (e
= callers
; e
;)
364 if (e
->caller
->thunk
.thunk_p
)
366 cgraph_node
*thunk
= e
->caller
;
369 if (thunk
->expand_thunk (false, false))
371 thunk
->thunk
.thunk_p
= false;
374 thunk
->expand_all_artificial_thunks ();
381 dump_callgraph_transformation (const cgraph_node
*original
,
382 const cgraph_node
*clone
,
385 if (symtab
->ipa_clones_dump_file
)
387 fprintf (symtab
->ipa_clones_dump_file
,
388 "Callgraph clone;%s;%d;%s;%d;%d;%s;%d;%s;%d;%d;%s\n",
389 original
->asm_name (), original
->order
,
390 DECL_SOURCE_FILE (original
->decl
),
391 DECL_SOURCE_LINE (original
->decl
),
392 DECL_SOURCE_COLUMN (original
->decl
), clone
->asm_name (),
393 clone
->order
, DECL_SOURCE_FILE (clone
->decl
),
394 DECL_SOURCE_LINE (clone
->decl
), DECL_SOURCE_COLUMN (clone
->decl
),
397 symtab
->cloned_nodes
.add (original
);
398 symtab
->cloned_nodes
.add (clone
);
402 /* Create node representing clone of N executed COUNT times. Decrease
403 the execution counts from original node too.
404 The new clone will have decl set to DECL that may or may not be the same
407 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
408 function's profile to reflect the fact that part of execution is handled
410 When CALL_DUPLICATOIN_HOOK is true, the ipa passes are acknowledged about
411 the new clone. Otherwise the caller is responsible for doing so later.
413 If the new node is being inlined into another one, NEW_INLINED_TO should be
414 the outline function the new one is (even indirectly) inlined to. All hooks
415 will see this in node's global.inlined_to, when invoked. Can be NULL if the
416 node is not inlined. */
419 cgraph_node::create_clone (tree new_decl
, profile_count prof_count
,
420 bool update_original
,
421 vec
<cgraph_edge
*> redirect_callers
,
422 bool call_duplication_hook
,
423 cgraph_node
*new_inlined_to
,
424 bitmap args_to_skip
, const char *suffix
)
426 cgraph_node
*new_node
= symtab
->create_empty ();
429 profile_count old_count
= count
;
432 dump_callgraph_transformation (this, new_inlined_to
, "inlining to");
434 if (prof_count
== profile_count::zero ()
435 && !(count
== profile_count::zero ()))
436 prof_count
= count
.global0 ();
438 new_node
->count
= prof_count
;
440 /* Update IPA profile. Local profiles need no updating in original. */
441 if (update_original
&& !(count
== profile_count::zero ())
442 && count
.ipa () == count
&& prof_count
.ipa () == prof_count
)
444 if (count
.nonzero_p ()
445 && !(count
- prof_count
).nonzero_p ())
446 count
= count
.global0 ();
450 new_node
->decl
= new_decl
;
451 new_node
->register_symbol ();
452 new_node
->origin
= origin
;
453 new_node
->lto_file_data
= lto_file_data
;
454 if (new_node
->origin
)
456 new_node
->next_nested
= new_node
->origin
->nested
;
457 new_node
->origin
->nested
= new_node
;
459 new_node
->analyzed
= analyzed
;
460 new_node
->definition
= definition
;
461 new_node
->local
= local
;
462 new_node
->externally_visible
= false;
463 new_node
->no_reorder
= no_reorder
;
464 new_node
->local
.local
= true;
465 new_node
->global
= global
;
466 new_node
->global
.inlined_to
= new_inlined_to
;
468 new_node
->frequency
= frequency
;
469 new_node
->tp_first_run
= tp_first_run
;
470 new_node
->tm_clone
= tm_clone
;
471 new_node
->icf_merged
= icf_merged
;
472 new_node
->merged_comdat
= merged_comdat
;
473 new_node
->thunk
= thunk
;
475 new_node
->clone
.tree_map
= NULL
;
476 new_node
->clone
.args_to_skip
= args_to_skip
;
477 new_node
->split_part
= split_part
;
479 new_node
->clone
.combined_args_to_skip
= clone
.combined_args_to_skip
;
480 else if (clone
.combined_args_to_skip
)
482 new_node
->clone
.combined_args_to_skip
= BITMAP_GGC_ALLOC ();
483 bitmap_ior (new_node
->clone
.combined_args_to_skip
,
484 clone
.combined_args_to_skip
, args_to_skip
);
487 new_node
->clone
.combined_args_to_skip
= args_to_skip
;
489 FOR_EACH_VEC_ELT (redirect_callers
, i
, e
)
491 /* Redirect calls to the old version node to point to its new
492 version. The only exception is when the edge was proved to
493 be unreachable during the clonning procedure. */
495 || DECL_BUILT_IN_CLASS (e
->callee
->decl
) != BUILT_IN_NORMAL
496 || DECL_FUNCTION_CODE (e
->callee
->decl
) != BUILT_IN_UNREACHABLE
)
497 e
->redirect_callee_duplicating_thunks (new_node
);
499 new_node
->expand_all_artificial_thunks ();
501 for (e
= callees
;e
; e
=e
->next_callee
)
502 e
->clone (new_node
, e
->call_stmt
, e
->lto_stmt_uid
, new_node
->count
, old_count
,
505 for (e
= indirect_calls
; e
; e
= e
->next_callee
)
506 e
->clone (new_node
, e
->call_stmt
, e
->lto_stmt_uid
,
507 new_node
->count
, old_count
, update_original
);
508 new_node
->clone_references (this);
510 new_node
->next_sibling_clone
= clones
;
512 clones
->prev_sibling_clone
= new_node
;
514 new_node
->clone_of
= this;
516 if (call_duplication_hook
)
517 symtab
->call_cgraph_duplication_hooks (this, new_node
);
520 dump_callgraph_transformation (this, new_node
, suffix
);
525 static GTY(()) unsigned int clone_fn_id_num
;
527 /* Return a new assembler name for a clone with SUFFIX of a decl named
531 clone_function_name_1 (const char *name
, const char *suffix
)
533 size_t len
= strlen (name
);
534 char *tmp_name
, *prefix
;
536 prefix
= XALLOCAVEC (char, len
+ strlen (suffix
) + 2);
537 memcpy (prefix
, name
, len
);
538 strcpy (prefix
+ len
+ 1, suffix
);
539 prefix
[len
] = symbol_table::symbol_suffix_separator ();
540 ASM_FORMAT_PRIVATE_NAME (tmp_name
, prefix
, clone_fn_id_num
++);
541 return get_identifier (tmp_name
);
544 /* Return a new assembler name for a clone of DECL with SUFFIX. */
547 clone_function_name (tree decl
, const char *suffix
)
549 tree name
= DECL_ASSEMBLER_NAME (decl
);
550 return clone_function_name_1 (IDENTIFIER_POINTER (name
), suffix
);
554 /* Create callgraph node clone with new declaration. The actual body will
555 be copied later at compilation stage.
557 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
561 cgraph_node::create_virtual_clone (vec
<cgraph_edge
*> redirect_callers
,
562 vec
<ipa_replace_map
*, va_gc
> *tree_map
,
563 bitmap args_to_skip
, const char * suffix
)
565 tree old_decl
= decl
;
566 cgraph_node
*new_node
= NULL
;
569 ipa_replace_map
*map
;
572 gcc_checking_assert (local
.versionable
);
573 gcc_assert (local
.can_change_signature
|| !args_to_skip
);
575 /* Make a new FUNCTION_DECL tree node */
577 new_decl
= copy_node (old_decl
);
579 new_decl
= build_function_decl_skip_args (old_decl
, args_to_skip
, false);
581 /* These pointers represent function body and will be populated only when clone
583 gcc_assert (new_decl
!= old_decl
);
584 DECL_STRUCT_FUNCTION (new_decl
) = NULL
;
585 DECL_ARGUMENTS (new_decl
) = NULL
;
586 DECL_INITIAL (new_decl
) = NULL
;
587 DECL_RESULT (new_decl
) = NULL
;
588 /* We can not do DECL_RESULT (new_decl) = NULL; here because of LTO partitioning
589 sometimes storing only clone decl instead of original. */
591 /* Generate a new name for the new version. */
592 len
= IDENTIFIER_LENGTH (DECL_NAME (old_decl
));
593 name
= XALLOCAVEC (char, len
+ strlen (suffix
) + 2);
594 memcpy (name
, IDENTIFIER_POINTER (DECL_NAME (old_decl
)), len
);
595 strcpy (name
+ len
+ 1, suffix
);
597 DECL_NAME (new_decl
) = get_identifier (name
);
598 SET_DECL_ASSEMBLER_NAME (new_decl
, clone_function_name (old_decl
, suffix
));
599 SET_DECL_RTL (new_decl
, NULL
);
601 new_node
= create_clone (new_decl
, count
, false,
602 redirect_callers
, false, NULL
, args_to_skip
, suffix
);
604 /* Update the properties.
605 Make clone visible only within this translation unit. Make sure
606 that is not weak also.
607 ??? We cannot use COMDAT linkage because there is no
608 ABI support for this. */
609 set_new_clone_decl_and_node_flags (new_node
);
610 new_node
->clone
.tree_map
= tree_map
;
611 if (!implicit_section
)
612 new_node
->set_section (get_section ());
614 /* Clones of global symbols or symbols with unique names are unique. */
615 if ((TREE_PUBLIC (old_decl
)
616 && !DECL_EXTERNAL (old_decl
)
617 && !DECL_WEAK (old_decl
)
618 && !DECL_COMDAT (old_decl
))
620 new_node
->unique_name
= true;
621 FOR_EACH_VEC_SAFE_ELT (tree_map
, i
, map
)
622 new_node
->maybe_create_reference (map
->new_tree
, NULL
);
624 if (ipa_transforms_to_apply
.exists ())
625 new_node
->ipa_transforms_to_apply
626 = ipa_transforms_to_apply
.copy ();
628 symtab
->call_cgraph_duplication_hooks (this, new_node
);
633 /* callgraph node being removed from symbol table; see if its entry can be
634 replaced by other inline clone. */
636 cgraph_node::find_replacement (void)
638 cgraph_node
*next_inline_clone
, *replacement
;
640 for (next_inline_clone
= clones
;
642 && next_inline_clone
->decl
!= decl
;
643 next_inline_clone
= next_inline_clone
->next_sibling_clone
)
646 /* If there is inline clone of the node being removed, we need
647 to put it into the position of removed node and reorganize all
648 other clones to be based on it. */
649 if (next_inline_clone
)
652 cgraph_node
*new_clones
;
654 replacement
= next_inline_clone
;
656 /* Unlink inline clone from the list of clones of removed node. */
657 if (next_inline_clone
->next_sibling_clone
)
658 next_inline_clone
->next_sibling_clone
->prev_sibling_clone
659 = next_inline_clone
->prev_sibling_clone
;
660 if (next_inline_clone
->prev_sibling_clone
)
662 gcc_assert (clones
!= next_inline_clone
);
663 next_inline_clone
->prev_sibling_clone
->next_sibling_clone
664 = next_inline_clone
->next_sibling_clone
;
668 gcc_assert (clones
== next_inline_clone
);
669 clones
= next_inline_clone
->next_sibling_clone
;
675 /* Copy clone info. */
676 next_inline_clone
->clone
= clone
;
678 /* Now place it into clone tree at same level at NODE. */
679 next_inline_clone
->clone_of
= clone_of
;
680 next_inline_clone
->prev_sibling_clone
= NULL
;
681 next_inline_clone
->next_sibling_clone
= NULL
;
684 if (clone_of
->clones
)
685 clone_of
->clones
->prev_sibling_clone
= next_inline_clone
;
686 next_inline_clone
->next_sibling_clone
= clone_of
->clones
;
687 clone_of
->clones
= next_inline_clone
;
690 /* Merge the clone list. */
693 if (!next_inline_clone
->clones
)
694 next_inline_clone
->clones
= new_clones
;
697 n
= next_inline_clone
->clones
;
698 while (n
->next_sibling_clone
)
699 n
= n
->next_sibling_clone
;
700 n
->next_sibling_clone
= new_clones
;
701 new_clones
->prev_sibling_clone
= n
;
705 /* Update clone_of pointers. */
709 n
->clone_of
= next_inline_clone
;
710 n
= n
->next_sibling_clone
;
718 /* Like cgraph_set_call_stmt but walk the clone tree and update all
719 clones sharing the same function body.
720 When WHOLE_SPECULATIVE_EDGES is true, all three components of
721 speculative edge gets updated. Otherwise we update only direct
725 cgraph_node::set_call_stmt_including_clones (gimple
*old_stmt
,
727 bool update_speculative
)
730 cgraph_edge
*edge
= get_edge (old_stmt
);
733 edge
->set_call_stmt (new_stmt
, update_speculative
);
739 cgraph_edge
*edge
= node
->get_edge (old_stmt
);
742 edge
->set_call_stmt (new_stmt
, update_speculative
);
743 /* If UPDATE_SPECULATIVE is false, it means that we are turning
744 speculative call into a real code sequence. Update the
746 if (edge
->speculative
&& !update_speculative
)
748 cgraph_edge
*direct
, *indirect
;
751 gcc_assert (!edge
->indirect_unknown_callee
);
752 edge
->speculative_call_info (direct
, indirect
, ref
);
753 direct
->speculative
= false;
754 indirect
->speculative
= false;
755 ref
->speculative
= false;
760 else if (node
->next_sibling_clone
)
761 node
= node
->next_sibling_clone
;
764 while (node
!= this && !node
->next_sibling_clone
)
765 node
= node
->clone_of
;
767 node
= node
->next_sibling_clone
;
772 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
773 same function body. If clones already have edge for OLD_STMT; only
774 update the edge same way as cgraph_set_call_stmt_including_clones does.
776 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
777 frequencies of the clones. */
780 cgraph_node::create_edge_including_clones (cgraph_node
*callee
,
781 gimple
*old_stmt
, gcall
*stmt
,
783 cgraph_inline_failed_t reason
)
788 if (!get_edge (stmt
))
790 edge
= create_edge (callee
, stmt
, count
);
791 edge
->inline_failed
= reason
;
797 /* Thunk clones do not get updated while copying inline function body. */
798 if (!node
->thunk
.thunk_p
)
800 cgraph_edge
*edge
= node
->get_edge (old_stmt
);
802 /* It is possible that clones already contain the edge while
803 master didn't. Either we promoted indirect call into direct
804 call in the clone or we are processing clones of unreachable
805 master where edges has been removed. */
807 edge
->set_call_stmt (stmt
);
808 else if (! node
->get_edge (stmt
))
810 edge
= node
->create_edge (callee
, stmt
, count
);
811 edge
->inline_failed
= reason
;
816 else if (node
->next_sibling_clone
)
817 node
= node
->next_sibling_clone
;
820 while (node
!= this && !node
->next_sibling_clone
)
821 node
= node
->clone_of
;
823 node
= node
->next_sibling_clone
;
828 /* Remove the node from cgraph and all inline clones inlined into it.
829 Skip however removal of FORBIDDEN_NODE and return true if it needs to be
830 removed. This allows to call the function from outer loop walking clone
834 cgraph_node::remove_symbol_and_inline_clones (cgraph_node
*forbidden_node
)
836 cgraph_edge
*e
, *next
;
839 if (this == forbidden_node
)
844 for (e
= callees
; e
; e
= next
)
846 next
= e
->next_callee
;
847 if (!e
->inline_failed
)
848 found
|= e
->callee
->remove_symbol_and_inline_clones (forbidden_node
);
854 /* The edges representing the callers of the NEW_VERSION node were
855 fixed by cgraph_function_versioning (), now the call_expr in their
856 respective tree code should be updated to call the NEW_VERSION. */
859 update_call_expr (cgraph_node
*new_version
)
863 gcc_assert (new_version
);
865 /* Update the call expr on the edges to call the new version. */
866 for (e
= new_version
->callers
; e
; e
= e
->next_caller
)
868 function
*inner_function
= DECL_STRUCT_FUNCTION (e
->caller
->decl
);
869 gimple_call_set_fndecl (e
->call_stmt
, new_version
->decl
);
870 maybe_clean_eh_stmt_fn (inner_function
, e
->call_stmt
);
875 /* Create a new cgraph node which is the new version of
876 callgraph node. REDIRECT_CALLERS holds the callers
877 edges which should be redirected to point to
878 NEW_VERSION. ALL the callees edges of the node
879 are cloned to the new version node. Return the new
882 If non-NULL BLOCK_TO_COPY determine what basic blocks
883 was copied to prevent duplications of calls that are dead
887 cgraph_node::create_version_clone (tree new_decl
,
888 vec
<cgraph_edge
*> redirect_callers
,
892 cgraph_node
*new_version
;
896 new_version
= cgraph_node::create (new_decl
);
898 new_version
->analyzed
= analyzed
;
899 new_version
->definition
= definition
;
900 new_version
->local
= local
;
901 new_version
->externally_visible
= false;
902 new_version
->no_reorder
= no_reorder
;
903 new_version
->local
.local
= new_version
->definition
;
904 new_version
->global
= global
;
905 new_version
->rtl
= rtl
;
906 new_version
->count
= count
;
908 for (e
= callees
; e
; e
=e
->next_callee
)
910 || bitmap_bit_p (bbs_to_copy
, gimple_bb (e
->call_stmt
)->index
))
911 e
->clone (new_version
, e
->call_stmt
,
912 e
->lto_stmt_uid
, count
, count
,
914 for (e
= indirect_calls
; e
; e
=e
->next_callee
)
916 || bitmap_bit_p (bbs_to_copy
, gimple_bb (e
->call_stmt
)->index
))
917 e
->clone (new_version
, e
->call_stmt
,
918 e
->lto_stmt_uid
, count
, count
,
920 FOR_EACH_VEC_ELT (redirect_callers
, i
, e
)
922 /* Redirect calls to the old version node to point to its new
924 e
->redirect_callee (new_version
);
927 symtab
->call_cgraph_duplication_hooks (this, new_version
);
929 dump_callgraph_transformation (this, new_version
, suffix
);
934 /* Perform function versioning.
935 Function versioning includes copying of the tree and
936 a callgraph update (creating a new cgraph node and updating
937 its callees and callers).
939 REDIRECT_CALLERS varray includes the edges to be redirected
942 TREE_MAP is a mapping of tree nodes we want to replace with
943 new ones (according to results of prior analysis).
945 If non-NULL ARGS_TO_SKIP determine function parameters to remove
947 If SKIP_RETURN is true, the new version will return void.
948 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
949 If non_NULL NEW_ENTRY determine new entry BB of the clone.
951 Return the new version's cgraph node. */
954 cgraph_node::create_version_clone_with_body
955 (vec
<cgraph_edge
*> redirect_callers
,
956 vec
<ipa_replace_map
*, va_gc
> *tree_map
, bitmap args_to_skip
,
957 bool skip_return
, bitmap bbs_to_copy
, basic_block new_entry_block
,
960 tree old_decl
= decl
;
961 cgraph_node
*new_version_node
= NULL
;
964 if (!tree_versionable_function_p (old_decl
))
967 gcc_assert (local
.can_change_signature
|| !args_to_skip
);
969 /* Make a new FUNCTION_DECL tree node for the new version. */
970 if (!args_to_skip
&& !skip_return
)
971 new_decl
= copy_node (old_decl
);
974 = build_function_decl_skip_args (old_decl
, args_to_skip
, skip_return
);
976 /* Generate a new name for the new version. */
977 DECL_NAME (new_decl
) = clone_function_name (old_decl
, suffix
);
978 SET_DECL_ASSEMBLER_NAME (new_decl
, DECL_NAME (new_decl
));
979 SET_DECL_RTL (new_decl
, NULL
);
981 /* When the old decl was a con-/destructor make sure the clone isn't. */
982 DECL_STATIC_CONSTRUCTOR (new_decl
) = 0;
983 DECL_STATIC_DESTRUCTOR (new_decl
) = 0;
985 /* Create the new version's call-graph node.
986 and update the edges of the new node. */
987 new_version_node
= create_version_clone (new_decl
, redirect_callers
,
988 bbs_to_copy
, suffix
);
990 if (ipa_transforms_to_apply
.exists ())
991 new_version_node
->ipa_transforms_to_apply
992 = ipa_transforms_to_apply
.copy ();
993 /* Copy the OLD_VERSION_NODE function tree to the new version. */
994 tree_function_versioning (old_decl
, new_decl
, tree_map
, false, args_to_skip
,
995 skip_return
, bbs_to_copy
, new_entry_block
);
997 /* Update the new version's properties.
998 Make The new version visible only within this translation unit. Make sure
999 that is not weak also.
1000 ??? We cannot use COMDAT linkage because there is no
1001 ABI support for this. */
1002 new_version_node
->make_decl_local ();
1003 DECL_VIRTUAL_P (new_version_node
->decl
) = 0;
1004 new_version_node
->externally_visible
= 0;
1005 new_version_node
->local
.local
= 1;
1006 new_version_node
->lowered
= true;
1007 if (!implicit_section
)
1008 new_version_node
->set_section (get_section ());
1009 /* Clones of global symbols or symbols with unique names are unique. */
1010 if ((TREE_PUBLIC (old_decl
)
1011 && !DECL_EXTERNAL (old_decl
)
1012 && !DECL_WEAK (old_decl
)
1013 && !DECL_COMDAT (old_decl
))
1015 new_version_node
->unique_name
= true;
1017 /* Update the call_expr on the edges to call the new version node. */
1018 update_call_expr (new_version_node
);
1020 symtab
->call_cgraph_insertion_hooks (this);
1021 return new_version_node
;
1024 /* Given virtual clone, turn it into actual clone. */
1027 cgraph_materialize_clone (cgraph_node
*node
)
1029 bitmap_obstack_initialize (NULL
);
1030 node
->former_clone_of
= node
->clone_of
->decl
;
1031 if (node
->clone_of
->former_clone_of
)
1032 node
->former_clone_of
= node
->clone_of
->former_clone_of
;
1033 /* Copy the OLD_VERSION_NODE function tree to the new version. */
1034 tree_function_versioning (node
->clone_of
->decl
, node
->decl
,
1035 node
->clone
.tree_map
, true,
1036 node
->clone
.args_to_skip
, false,
1038 if (symtab
->dump_file
)
1040 dump_function_to_file (node
->clone_of
->decl
, symtab
->dump_file
,
1042 dump_function_to_file (node
->decl
, symtab
->dump_file
, dump_flags
);
1045 /* Function is no longer clone. */
1046 if (node
->next_sibling_clone
)
1047 node
->next_sibling_clone
->prev_sibling_clone
= node
->prev_sibling_clone
;
1048 if (node
->prev_sibling_clone
)
1049 node
->prev_sibling_clone
->next_sibling_clone
= node
->next_sibling_clone
;
1051 node
->clone_of
->clones
= node
->next_sibling_clone
;
1052 node
->next_sibling_clone
= NULL
;
1053 node
->prev_sibling_clone
= NULL
;
1054 if (!node
->clone_of
->analyzed
&& !node
->clone_of
->clones
)
1056 node
->clone_of
->release_body ();
1057 node
->clone_of
->remove_callees ();
1058 node
->clone_of
->remove_all_references ();
1060 node
->clone_of
= NULL
;
1061 bitmap_obstack_release (NULL
);
1064 /* Once all functions from compilation unit are in memory, produce all clones
1065 and update all calls. We might also do this on demand if we don't want to
1066 bring all functions to memory prior compilation, but current WHOPR
1067 implementation does that and it is a bit easier to keep everything right in
1071 symbol_table::materialize_all_clones (void)
1074 bool stabilized
= false;
1077 if (symtab
->dump_file
)
1078 fprintf (symtab
->dump_file
, "Materializing clones\n");
1080 cgraph_node::checking_verify_cgraph_nodes ();
1082 /* We can also do topological order, but number of iterations should be
1083 bounded by number of IPA passes since single IPA pass is probably not
1084 going to create clones of clones it created itself. */
1088 FOR_EACH_FUNCTION (node
)
1090 if (node
->clone_of
&& node
->decl
!= node
->clone_of
->decl
1091 && !gimple_has_body_p (node
->decl
))
1093 if (!node
->clone_of
->clone_of
)
1094 node
->clone_of
->get_untransformed_body ();
1095 if (gimple_has_body_p (node
->clone_of
->decl
))
1097 if (symtab
->dump_file
)
1099 fprintf (symtab
->dump_file
, "cloning %s to %s\n",
1100 xstrdup_for_dump (node
->clone_of
->name ()),
1101 xstrdup_for_dump (node
->name ()));
1102 if (node
->clone
.tree_map
)
1105 fprintf (symtab
->dump_file
, " replace map: ");
1107 i
< vec_safe_length (node
->clone
.tree_map
);
1110 ipa_replace_map
*replace_info
;
1111 replace_info
= (*node
->clone
.tree_map
)[i
];
1112 print_generic_expr (symtab
->dump_file
,
1113 replace_info
->old_tree
);
1114 fprintf (symtab
->dump_file
, " -> ");
1115 print_generic_expr (symtab
->dump_file
,
1116 replace_info
->new_tree
);
1117 fprintf (symtab
->dump_file
, "%s%s;",
1118 replace_info
->replace_p
? "(replace)":"",
1119 replace_info
->ref_p
? "(ref)":"");
1121 fprintf (symtab
->dump_file
, "\n");
1123 if (node
->clone
.args_to_skip
)
1125 fprintf (symtab
->dump_file
, " args_to_skip: ");
1126 dump_bitmap (symtab
->dump_file
,
1127 node
->clone
.args_to_skip
);
1129 if (node
->clone
.args_to_skip
)
1131 fprintf (symtab
->dump_file
, " combined_args_to_skip:");
1132 dump_bitmap (symtab
->dump_file
, node
->clone
.combined_args_to_skip
);
1135 cgraph_materialize_clone (node
);
1141 FOR_EACH_FUNCTION (node
)
1142 if (!node
->analyzed
&& node
->callees
)
1144 node
->remove_callees ();
1145 node
->remove_all_references ();
1148 node
->clear_stmts_in_references ();
1149 if (symtab
->dump_file
)
1150 fprintf (symtab
->dump_file
, "Materialization Call site updates done.\n");
1152 cgraph_node::checking_verify_cgraph_nodes ();
1154 symtab
->remove_unreachable_nodes (symtab
->dump_file
);
1157 #include "gt-cgraphclones.h"