2 Copyright (C) 2003-2013 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"
73 #include "stringpool.h"
79 #include "tree-inline.h"
80 #include "langhooks.h"
81 #include "pointer-set.h"
87 #include "diagnostic.h"
92 #include "tree-iterator.h"
93 #include "tree-dump.h"
94 #include "gimple-pretty-print.h"
96 #include "ipa-inline.h"
97 #include "ipa-utils.h"
98 #include "lto-streamer.h"
101 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
103 cgraph_clone_edge (struct cgraph_edge
*e
, struct cgraph_node
*n
,
104 gimple call_stmt
, unsigned stmt_uid
, gcov_type count_scale
,
105 int freq_scale
, bool update_original
)
107 struct cgraph_edge
*new_edge
;
108 gcov_type count
= apply_probability (e
->count
, count_scale
);
111 /* We do not want to ignore loop nest after frequency drops to 0. */
114 freq
= e
->frequency
* (gcov_type
) freq_scale
/ CGRAPH_FREQ_BASE
;
115 if (freq
> CGRAPH_FREQ_MAX
)
116 freq
= CGRAPH_FREQ_MAX
;
118 if (e
->indirect_unknown_callee
)
122 if (call_stmt
&& (decl
= gimple_call_fndecl (call_stmt
)))
124 struct cgraph_node
*callee
= cgraph_get_node (decl
);
125 gcc_checking_assert (callee
);
126 new_edge
= cgraph_create_edge (n
, callee
, call_stmt
, count
, freq
);
130 new_edge
= cgraph_create_indirect_edge (n
, call_stmt
,
131 e
->indirect_info
->ecf_flags
,
133 *new_edge
->indirect_info
= *e
->indirect_info
;
138 new_edge
= cgraph_create_edge (n
, e
->callee
, call_stmt
, count
, freq
);
139 if (e
->indirect_info
)
141 new_edge
->indirect_info
142 = ggc_alloc_cleared_cgraph_indirect_call_info ();
143 *new_edge
->indirect_info
= *e
->indirect_info
;
147 new_edge
->inline_failed
= e
->inline_failed
;
148 new_edge
->indirect_inlining_edge
= e
->indirect_inlining_edge
;
149 new_edge
->lto_stmt_uid
= stmt_uid
;
150 /* Clone flags that depend on call_stmt availability manually. */
151 new_edge
->can_throw_external
= e
->can_throw_external
;
152 new_edge
->call_stmt_cannot_inline_p
= e
->call_stmt_cannot_inline_p
;
153 new_edge
->speculative
= e
->speculative
;
156 e
->count
-= new_edge
->count
;
160 cgraph_call_edge_duplication_hooks (e
, new_edge
);
165 /* Create node representing clone of N executed COUNT times. Decrease
166 the execution counts from original node too.
167 The new clone will have decl set to DECL that may or may not be the same
170 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
171 function's profile to reflect the fact that part of execution is handled
173 When CALL_DUPLICATOIN_HOOK is true, the ipa passes are acknowledged about
174 the new clone. Otherwise the caller is responsible for doing so later.
176 If the new node is being inlined into another one, NEW_INLINED_TO should be
177 the outline function the new one is (even indirectly) inlined to. All hooks
178 will see this in node's global.inlined_to, when invoked. Can be NULL if the
179 node is not inlined. */
182 cgraph_clone_node (struct cgraph_node
*n
, tree decl
, gcov_type count
, int freq
,
183 bool update_original
,
184 vec
<cgraph_edge_p
> redirect_callers
,
185 bool call_duplication_hook
,
186 struct cgraph_node
*new_inlined_to
)
188 struct cgraph_node
*new_node
= cgraph_create_empty_node ();
189 struct cgraph_edge
*e
;
190 gcov_type count_scale
;
193 new_node
->decl
= decl
;
194 symtab_register_node (new_node
);
195 new_node
->origin
= n
->origin
;
196 new_node
->lto_file_data
= n
->lto_file_data
;
197 if (new_node
->origin
)
199 new_node
->next_nested
= new_node
->origin
->nested
;
200 new_node
->origin
->nested
= new_node
;
202 new_node
->analyzed
= n
->analyzed
;
203 new_node
->definition
= n
->definition
;
204 new_node
->local
= n
->local
;
205 new_node
->externally_visible
= false;
206 new_node
->local
.local
= true;
207 new_node
->global
= n
->global
;
208 new_node
->global
.inlined_to
= new_inlined_to
;
209 new_node
->rtl
= n
->rtl
;
210 new_node
->count
= count
;
211 new_node
->frequency
= n
->frequency
;
212 new_node
->clone
= n
->clone
;
213 new_node
->clone
.tree_map
= NULL
;
214 new_node
->tp_first_run
= n
->tp_first_run
;
217 if (new_node
->count
> n
->count
)
218 count_scale
= REG_BR_PROB_BASE
;
220 count_scale
= GCOV_COMPUTE_SCALE (new_node
->count
, n
->count
);
231 FOR_EACH_VEC_ELT (redirect_callers
, i
, e
)
233 /* Redirect calls to the old version node to point to its new
235 cgraph_redirect_edge_callee (e
, new_node
);
239 for (e
= n
->callees
;e
; e
=e
->next_callee
)
240 cgraph_clone_edge (e
, new_node
, e
->call_stmt
, e
->lto_stmt_uid
,
241 count_scale
, freq
, update_original
);
243 for (e
= n
->indirect_calls
; e
; e
= e
->next_callee
)
244 cgraph_clone_edge (e
, new_node
, e
->call_stmt
, e
->lto_stmt_uid
,
245 count_scale
, freq
, update_original
);
246 ipa_clone_references (new_node
, &n
->ref_list
);
248 new_node
->next_sibling_clone
= n
->clones
;
250 n
->clones
->prev_sibling_clone
= new_node
;
251 n
->clones
= new_node
;
252 new_node
->clone_of
= n
;
254 if (call_duplication_hook
)
255 cgraph_call_node_duplication_hooks (n
, new_node
);
259 /* Return a new assembler name for a clone of DECL with SUFFIX. */
261 static GTY(()) unsigned int clone_fn_id_num
;
264 clone_function_name (tree decl
, const char *suffix
)
266 tree name
= DECL_ASSEMBLER_NAME (decl
);
267 size_t len
= IDENTIFIER_LENGTH (name
);
268 char *tmp_name
, *prefix
;
270 prefix
= XALLOCAVEC (char, len
+ strlen (suffix
) + 2);
271 memcpy (prefix
, IDENTIFIER_POINTER (name
), len
);
272 strcpy (prefix
+ len
+ 1, suffix
);
273 #ifndef NO_DOT_IN_LABEL
275 #elif !defined NO_DOLLAR_IN_LABEL
280 ASM_FORMAT_PRIVATE_NAME (tmp_name
, prefix
, clone_fn_id_num
++);
281 return get_identifier (tmp_name
);
284 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP and the
285 return value if SKIP_RETURN is true. */
288 build_function_type_skip_args (tree orig_type
, bitmap args_to_skip
,
291 tree new_type
= NULL
;
292 tree args
, new_args
= NULL
, t
;
296 for (args
= TYPE_ARG_TYPES (orig_type
); args
&& args
!= void_list_node
;
297 args
= TREE_CHAIN (args
), i
++)
298 if (!args_to_skip
|| !bitmap_bit_p (args_to_skip
, i
))
299 new_args
= tree_cons (NULL_TREE
, TREE_VALUE (args
), new_args
);
301 new_reversed
= nreverse (new_args
);
305 TREE_CHAIN (new_args
) = void_list_node
;
307 new_reversed
= void_list_node
;
310 /* Use copy_node to preserve as much as possible from original type
311 (debug info, attribute lists etc.)
312 Exception is METHOD_TYPEs must have THIS argument.
313 When we are asked to remove it, we need to build new FUNCTION_TYPE
315 if (TREE_CODE (orig_type
) != METHOD_TYPE
317 || !bitmap_bit_p (args_to_skip
, 0))
319 new_type
= build_distinct_type_copy (orig_type
);
320 TYPE_ARG_TYPES (new_type
) = new_reversed
;
325 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type
),
327 TYPE_CONTEXT (new_type
) = TYPE_CONTEXT (orig_type
);
331 TREE_TYPE (new_type
) = void_type_node
;
333 /* This is a new type, not a copy of an old type. Need to reassociate
334 variants. We can handle everything except the main variant lazily. */
335 t
= TYPE_MAIN_VARIANT (orig_type
);
338 t
= build_function_type_skip_args (t
, args_to_skip
, skip_return
);
339 TYPE_MAIN_VARIANT (new_type
) = t
;
340 TYPE_NEXT_VARIANT (new_type
) = TYPE_NEXT_VARIANT (t
);
341 TYPE_NEXT_VARIANT (t
) = new_type
;
345 TYPE_MAIN_VARIANT (new_type
) = new_type
;
346 TYPE_NEXT_VARIANT (new_type
) = NULL
;
352 /* Build variant of function decl ORIG_DECL skipping ARGS_TO_SKIP and the
353 return value if SKIP_RETURN is true.
355 Arguments from DECL_ARGUMENTS list can't be removed now, since they are
356 linked by TREE_CHAIN directly. The caller is responsible for eliminating
357 them when they are being duplicated (i.e. copy_arguments_for_versioning). */
360 build_function_decl_skip_args (tree orig_decl
, bitmap args_to_skip
,
363 tree new_decl
= copy_node (orig_decl
);
366 new_type
= TREE_TYPE (orig_decl
);
367 if (prototype_p (new_type
)
368 || (skip_return
&& !VOID_TYPE_P (TREE_TYPE (new_type
))))
370 = build_function_type_skip_args (new_type
, args_to_skip
, skip_return
);
371 TREE_TYPE (new_decl
) = new_type
;
373 /* For declarations setting DECL_VINDEX (i.e. methods)
374 we expect first argument to be THIS pointer. */
375 if (args_to_skip
&& bitmap_bit_p (args_to_skip
, 0))
376 DECL_VINDEX (new_decl
) = NULL_TREE
;
378 /* When signature changes, we need to clear builtin info. */
379 if (DECL_BUILT_IN (new_decl
)
381 && !bitmap_empty_p (args_to_skip
))
383 DECL_BUILT_IN_CLASS (new_decl
) = NOT_BUILT_IN
;
384 DECL_FUNCTION_CODE (new_decl
) = (enum built_in_function
) 0;
389 /* Create callgraph node clone with new declaration. The actual body will
390 be copied later at compilation stage.
392 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
396 cgraph_create_virtual_clone (struct cgraph_node
*old_node
,
397 vec
<cgraph_edge_p
> redirect_callers
,
398 vec
<ipa_replace_map_p
, va_gc
> *tree_map
,
402 tree old_decl
= old_node
->decl
;
403 struct cgraph_node
*new_node
= NULL
;
406 struct ipa_replace_map
*map
;
410 gcc_checking_assert (tree_versionable_function_p (old_decl
));
412 gcc_assert (old_node
->local
.can_change_signature
|| !args_to_skip
);
414 /* Make a new FUNCTION_DECL tree node */
416 new_decl
= copy_node (old_decl
);
418 new_decl
= build_function_decl_skip_args (old_decl
, args_to_skip
, false);
420 /* These pointers represent function body and will be populated only when clone
422 gcc_assert (new_decl
!= old_decl
);
423 DECL_STRUCT_FUNCTION (new_decl
) = NULL
;
424 DECL_ARGUMENTS (new_decl
) = NULL
;
425 DECL_INITIAL (new_decl
) = NULL
;
426 DECL_RESULT (new_decl
) = NULL
;
427 /* We can not do DECL_RESULT (new_decl) = NULL; here because of LTO partitioning
428 sometimes storing only clone decl instead of original. */
430 /* Generate a new name for the new version. */
431 len
= IDENTIFIER_LENGTH (DECL_NAME (old_decl
));
432 name
= XALLOCAVEC (char, len
+ strlen (suffix
) + 2);
433 memcpy (name
, IDENTIFIER_POINTER (DECL_NAME (old_decl
)), len
);
434 strcpy (name
+ len
+ 1, suffix
);
436 DECL_NAME (new_decl
) = get_identifier (name
);
437 SET_DECL_ASSEMBLER_NAME (new_decl
, clone_function_name (old_decl
, suffix
));
438 SET_DECL_RTL (new_decl
, NULL
);
440 new_node
= cgraph_clone_node (old_node
, new_decl
, old_node
->count
,
441 CGRAPH_FREQ_BASE
, false,
442 redirect_callers
, false, NULL
);
443 /* Update the properties.
444 Make clone visible only within this translation unit. Make sure
445 that is not weak also.
446 ??? We cannot use COMDAT linkage because there is no
447 ABI support for this. */
448 DECL_EXTERNAL (new_node
->decl
) = 0;
449 if (DECL_ONE_ONLY (old_decl
))
450 DECL_SECTION_NAME (new_node
->decl
) = NULL
;
451 DECL_COMDAT_GROUP (new_node
->decl
) = 0;
452 TREE_PUBLIC (new_node
->decl
) = 0;
453 DECL_COMDAT (new_node
->decl
) = 0;
454 DECL_WEAK (new_node
->decl
) = 0;
455 DECL_VIRTUAL_P (new_node
->decl
) = 0;
456 DECL_STATIC_CONSTRUCTOR (new_node
->decl
) = 0;
457 DECL_STATIC_DESTRUCTOR (new_node
->decl
) = 0;
458 new_node
->clone
.tree_map
= tree_map
;
459 new_node
->clone
.args_to_skip
= args_to_skip
;
461 /* Clones of global symbols or symbols with unique names are unique. */
462 if ((TREE_PUBLIC (old_decl
)
463 && !DECL_EXTERNAL (old_decl
)
464 && !DECL_WEAK (old_decl
)
465 && !DECL_COMDAT (old_decl
))
467 new_node
->unique_name
= true;
468 FOR_EACH_VEC_SAFE_ELT (tree_map
, i
, map
)
469 ipa_maybe_record_reference (new_node
, map
->new_tree
,
472 new_node
->clone
.combined_args_to_skip
= old_node
->clone
.combined_args_to_skip
;
473 else if (old_node
->clone
.combined_args_to_skip
)
475 int newi
= 0, oldi
= 0;
477 bitmap new_args_to_skip
= BITMAP_GGC_ALLOC ();
478 struct cgraph_node
*orig_node
;
479 for (orig_node
= old_node
; orig_node
->clone_of
; orig_node
= orig_node
->clone_of
)
481 for (arg
= DECL_ARGUMENTS (orig_node
->decl
);
482 arg
; arg
= DECL_CHAIN (arg
), oldi
++)
484 if (bitmap_bit_p (old_node
->clone
.combined_args_to_skip
, oldi
))
486 bitmap_set_bit (new_args_to_skip
, oldi
);
489 if (bitmap_bit_p (args_to_skip
, newi
))
490 bitmap_set_bit (new_args_to_skip
, oldi
);
493 new_node
->clone
.combined_args_to_skip
= new_args_to_skip
;
496 new_node
->clone
.combined_args_to_skip
= args_to_skip
;
497 new_node
->externally_visible
= 0;
498 new_node
->local
.local
= 1;
499 new_node
->lowered
= true;
501 cgraph_call_node_duplication_hooks (old_node
, new_node
);
507 /* NODE is being removed from symbol table; see if its entry can be replaced by
508 other inline clone. */
510 cgraph_find_replacement_node (struct cgraph_node
*node
)
512 struct cgraph_node
*next_inline_clone
, *replacement
;
514 for (next_inline_clone
= node
->clones
;
516 && next_inline_clone
->decl
!= node
->decl
;
517 next_inline_clone
= next_inline_clone
->next_sibling_clone
)
520 /* If there is inline clone of the node being removed, we need
521 to put it into the position of removed node and reorganize all
522 other clones to be based on it. */
523 if (next_inline_clone
)
525 struct cgraph_node
*n
;
526 struct cgraph_node
*new_clones
;
528 replacement
= next_inline_clone
;
530 /* Unlink inline clone from the list of clones of removed node. */
531 if (next_inline_clone
->next_sibling_clone
)
532 next_inline_clone
->next_sibling_clone
->prev_sibling_clone
533 = next_inline_clone
->prev_sibling_clone
;
534 if (next_inline_clone
->prev_sibling_clone
)
536 gcc_assert (node
->clones
!= next_inline_clone
);
537 next_inline_clone
->prev_sibling_clone
->next_sibling_clone
538 = next_inline_clone
->next_sibling_clone
;
542 gcc_assert (node
->clones
== next_inline_clone
);
543 node
->clones
= next_inline_clone
->next_sibling_clone
;
546 new_clones
= node
->clones
;
549 /* Copy clone info. */
550 next_inline_clone
->clone
= node
->clone
;
552 /* Now place it into clone tree at same level at NODE. */
553 next_inline_clone
->clone_of
= node
->clone_of
;
554 next_inline_clone
->prev_sibling_clone
= NULL
;
555 next_inline_clone
->next_sibling_clone
= NULL
;
558 if (node
->clone_of
->clones
)
559 node
->clone_of
->clones
->prev_sibling_clone
= next_inline_clone
;
560 next_inline_clone
->next_sibling_clone
= node
->clone_of
->clones
;
561 node
->clone_of
->clones
= next_inline_clone
;
564 /* Merge the clone list. */
567 if (!next_inline_clone
->clones
)
568 next_inline_clone
->clones
= new_clones
;
571 n
= next_inline_clone
->clones
;
572 while (n
->next_sibling_clone
)
573 n
= n
->next_sibling_clone
;
574 n
->next_sibling_clone
= new_clones
;
575 new_clones
->prev_sibling_clone
= n
;
579 /* Update clone_of pointers. */
583 n
->clone_of
= next_inline_clone
;
584 n
= n
->next_sibling_clone
;
592 /* Like cgraph_set_call_stmt but walk the clone tree and update all
593 clones sharing the same function body.
594 When WHOLE_SPECULATIVE_EDGES is true, all three components of
595 speculative edge gets updated. Otherwise we update only direct
599 cgraph_set_call_stmt_including_clones (struct cgraph_node
*orig
,
600 gimple old_stmt
, gimple new_stmt
,
601 bool update_speculative
)
603 struct cgraph_node
*node
;
604 struct cgraph_edge
*edge
= cgraph_edge (orig
, old_stmt
);
607 cgraph_set_call_stmt (edge
, new_stmt
, update_speculative
);
613 struct cgraph_edge
*edge
= cgraph_edge (node
, old_stmt
);
616 cgraph_set_call_stmt (edge
, new_stmt
, update_speculative
);
617 /* If UPDATE_SPECULATIVE is false, it means that we are turning
618 speculative call into a real code sequence. Update the
620 if (edge
->speculative
&& !update_speculative
)
622 struct cgraph_edge
*direct
, *indirect
;
625 gcc_assert (!edge
->indirect_unknown_callee
);
626 cgraph_speculative_call_info (edge
, direct
, indirect
, ref
);
627 direct
->speculative
= false;
628 indirect
->speculative
= false;
629 ref
->speculative
= false;
634 else if (node
->next_sibling_clone
)
635 node
= node
->next_sibling_clone
;
638 while (node
!= orig
&& !node
->next_sibling_clone
)
639 node
= node
->clone_of
;
641 node
= node
->next_sibling_clone
;
646 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
647 same function body. If clones already have edge for OLD_STMT; only
648 update the edge same way as cgraph_set_call_stmt_including_clones does.
650 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
651 frequencies of the clones. */
654 cgraph_create_edge_including_clones (struct cgraph_node
*orig
,
655 struct cgraph_node
*callee
,
657 gimple stmt
, gcov_type count
,
659 cgraph_inline_failed_t reason
)
661 struct cgraph_node
*node
;
662 struct cgraph_edge
*edge
;
664 if (!cgraph_edge (orig
, stmt
))
666 edge
= cgraph_create_edge (orig
, callee
, stmt
, count
, freq
);
667 edge
->inline_failed
= reason
;
674 struct cgraph_edge
*edge
= cgraph_edge (node
, old_stmt
);
676 /* It is possible that clones already contain the edge while
677 master didn't. Either we promoted indirect call into direct
678 call in the clone or we are processing clones of unreachable
679 master where edges has been removed. */
681 cgraph_set_call_stmt (edge
, stmt
);
682 else if (!cgraph_edge (node
, stmt
))
684 edge
= cgraph_create_edge (node
, callee
, stmt
, count
,
686 edge
->inline_failed
= reason
;
691 else if (node
->next_sibling_clone
)
692 node
= node
->next_sibling_clone
;
695 while (node
!= orig
&& !node
->next_sibling_clone
)
696 node
= node
->clone_of
;
698 node
= node
->next_sibling_clone
;
703 /* Remove the node from cgraph and all inline clones inlined into it.
704 Skip however removal of FORBIDDEN_NODE and return true if it needs to be
705 removed. This allows to call the function from outer loop walking clone
709 cgraph_remove_node_and_inline_clones (struct cgraph_node
*node
, struct cgraph_node
*forbidden_node
)
711 struct cgraph_edge
*e
, *next
;
714 if (node
== forbidden_node
)
716 cgraph_remove_edge (node
->callers
);
719 for (e
= node
->callees
; e
; e
= next
)
721 next
= e
->next_callee
;
722 if (!e
->inline_failed
)
723 found
|= cgraph_remove_node_and_inline_clones (e
->callee
, forbidden_node
);
725 cgraph_remove_node (node
);
729 /* The edges representing the callers of the NEW_VERSION node were
730 fixed by cgraph_function_versioning (), now the call_expr in their
731 respective tree code should be updated to call the NEW_VERSION. */
734 update_call_expr (struct cgraph_node
*new_version
)
736 struct cgraph_edge
*e
;
738 gcc_assert (new_version
);
740 /* Update the call expr on the edges to call the new version. */
741 for (e
= new_version
->callers
; e
; e
= e
->next_caller
)
743 struct function
*inner_function
= DECL_STRUCT_FUNCTION (e
->caller
->decl
);
744 gimple_call_set_fndecl (e
->call_stmt
, new_version
->decl
);
745 maybe_clean_eh_stmt_fn (inner_function
, e
->call_stmt
);
750 /* Create a new cgraph node which is the new version of
751 OLD_VERSION node. REDIRECT_CALLERS holds the callers
752 edges which should be redirected to point to
753 NEW_VERSION. ALL the callees edges of OLD_VERSION
754 are cloned to the new version node. Return the new
757 If non-NULL BLOCK_TO_COPY determine what basic blocks
758 was copied to prevent duplications of calls that are dead
762 cgraph_copy_node_for_versioning (struct cgraph_node
*old_version
,
764 vec
<cgraph_edge_p
> redirect_callers
,
767 struct cgraph_node
*new_version
;
768 struct cgraph_edge
*e
;
771 gcc_assert (old_version
);
773 new_version
= cgraph_create_node (new_decl
);
775 new_version
->analyzed
= old_version
->analyzed
;
776 new_version
->definition
= old_version
->definition
;
777 new_version
->local
= old_version
->local
;
778 new_version
->externally_visible
= false;
779 new_version
->local
.local
= new_version
->definition
;
780 new_version
->global
= old_version
->global
;
781 new_version
->rtl
= old_version
->rtl
;
782 new_version
->count
= old_version
->count
;
784 for (e
= old_version
->callees
; e
; e
=e
->next_callee
)
786 || bitmap_bit_p (bbs_to_copy
, gimple_bb (e
->call_stmt
)->index
))
787 cgraph_clone_edge (e
, new_version
, e
->call_stmt
,
788 e
->lto_stmt_uid
, REG_BR_PROB_BASE
,
791 for (e
= old_version
->indirect_calls
; e
; e
=e
->next_callee
)
793 || bitmap_bit_p (bbs_to_copy
, gimple_bb (e
->call_stmt
)->index
))
794 cgraph_clone_edge (e
, new_version
, e
->call_stmt
,
795 e
->lto_stmt_uid
, REG_BR_PROB_BASE
,
798 FOR_EACH_VEC_ELT (redirect_callers
, i
, e
)
800 /* Redirect calls to the old version node to point to its new
802 cgraph_redirect_edge_callee (e
, new_version
);
805 cgraph_call_node_duplication_hooks (old_version
, new_version
);
810 /* Perform function versioning.
811 Function versioning includes copying of the tree and
812 a callgraph update (creating a new cgraph node and updating
813 its callees and callers).
815 REDIRECT_CALLERS varray includes the edges to be redirected
818 TREE_MAP is a mapping of tree nodes we want to replace with
819 new ones (according to results of prior analysis).
820 OLD_VERSION_NODE is the node that is versioned.
822 If non-NULL ARGS_TO_SKIP determine function parameters to remove
824 If SKIP_RETURN is true, the new version will return void.
825 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
826 If non_NULL NEW_ENTRY determine new entry BB of the clone.
828 Return the new version's cgraph node. */
831 cgraph_function_versioning (struct cgraph_node
*old_version_node
,
832 vec
<cgraph_edge_p
> redirect_callers
,
833 vec
<ipa_replace_map_p
, va_gc
> *tree_map
,
837 basic_block new_entry_block
,
838 const char *clone_name
)
840 tree old_decl
= old_version_node
->decl
;
841 struct cgraph_node
*new_version_node
= NULL
;
844 if (!tree_versionable_function_p (old_decl
))
847 gcc_assert (old_version_node
->local
.can_change_signature
|| !args_to_skip
);
849 /* Make a new FUNCTION_DECL tree node for the new version. */
850 if (!args_to_skip
&& !skip_return
)
851 new_decl
= copy_node (old_decl
);
854 = build_function_decl_skip_args (old_decl
, args_to_skip
, skip_return
);
856 /* Generate a new name for the new version. */
857 DECL_NAME (new_decl
) = clone_function_name (old_decl
, clone_name
);
858 SET_DECL_ASSEMBLER_NAME (new_decl
, DECL_NAME (new_decl
));
859 SET_DECL_RTL (new_decl
, NULL
);
861 /* When the old decl was a con-/destructor make sure the clone isn't. */
862 DECL_STATIC_CONSTRUCTOR (new_decl
) = 0;
863 DECL_STATIC_DESTRUCTOR (new_decl
) = 0;
865 /* Create the new version's call-graph node.
866 and update the edges of the new node. */
868 cgraph_copy_node_for_versioning (old_version_node
, new_decl
,
869 redirect_callers
, bbs_to_copy
);
871 /* Copy the OLD_VERSION_NODE function tree to the new version. */
872 tree_function_versioning (old_decl
, new_decl
, tree_map
, false, args_to_skip
,
873 skip_return
, bbs_to_copy
, new_entry_block
);
875 /* Update the new version's properties.
876 Make The new version visible only within this translation unit. Make sure
877 that is not weak also.
878 ??? We cannot use COMDAT linkage because there is no
879 ABI support for this. */
880 symtab_make_decl_local (new_version_node
->decl
);
881 DECL_VIRTUAL_P (new_version_node
->decl
) = 0;
882 new_version_node
->externally_visible
= 0;
883 new_version_node
->local
.local
= 1;
884 new_version_node
->lowered
= true;
885 /* Clones of global symbols or symbols with unique names are unique. */
886 if ((TREE_PUBLIC (old_decl
)
887 && !DECL_EXTERNAL (old_decl
)
888 && !DECL_WEAK (old_decl
)
889 && !DECL_COMDAT (old_decl
))
891 new_version_node
->unique_name
= true;
893 /* Update the call_expr on the edges to call the new version node. */
894 update_call_expr (new_version_node
);
896 cgraph_call_function_insertion_hooks (new_version_node
);
897 return new_version_node
;
900 /* Given virtual clone, turn it into actual clone. */
903 cgraph_materialize_clone (struct cgraph_node
*node
)
905 bitmap_obstack_initialize (NULL
);
906 node
->former_clone_of
= node
->clone_of
->decl
;
907 if (node
->clone_of
->former_clone_of
)
908 node
->former_clone_of
= node
->clone_of
->former_clone_of
;
909 /* Copy the OLD_VERSION_NODE function tree to the new version. */
910 tree_function_versioning (node
->clone_of
->decl
, node
->decl
,
911 node
->clone
.tree_map
, true,
912 node
->clone
.args_to_skip
, false,
914 if (cgraph_dump_file
)
916 dump_function_to_file (node
->clone_of
->decl
, cgraph_dump_file
, dump_flags
);
917 dump_function_to_file (node
->decl
, cgraph_dump_file
, dump_flags
);
920 /* Function is no longer clone. */
921 if (node
->next_sibling_clone
)
922 node
->next_sibling_clone
->prev_sibling_clone
= node
->prev_sibling_clone
;
923 if (node
->prev_sibling_clone
)
924 node
->prev_sibling_clone
->next_sibling_clone
= node
->next_sibling_clone
;
926 node
->clone_of
->clones
= node
->next_sibling_clone
;
927 node
->next_sibling_clone
= NULL
;
928 node
->prev_sibling_clone
= NULL
;
929 if (!node
->clone_of
->analyzed
&& !node
->clone_of
->clones
)
931 cgraph_release_function_body (node
->clone_of
);
932 cgraph_node_remove_callees (node
->clone_of
);
933 ipa_remove_all_references (&node
->clone_of
->ref_list
);
935 node
->clone_of
= NULL
;
936 bitmap_obstack_release (NULL
);
939 /* Once all functions from compilation unit are in memory, produce all clones
940 and update all calls. We might also do this on demand if we don't want to
941 bring all functions to memory prior compilation, but current WHOPR
942 implementation does that and it is is bit easier to keep everything right in
946 cgraph_materialize_all_clones (void)
948 struct cgraph_node
*node
;
949 bool stabilized
= false;
952 if (cgraph_dump_file
)
953 fprintf (cgraph_dump_file
, "Materializing clones\n");
954 #ifdef ENABLE_CHECKING
958 /* We can also do topological order, but number of iterations should be
959 bounded by number of IPA passes since single IPA pass is probably not
960 going to create clones of clones it created itself. */
964 FOR_EACH_FUNCTION (node
)
966 if (node
->clone_of
&& node
->decl
!= node
->clone_of
->decl
967 && !gimple_has_body_p (node
->decl
))
969 if (!node
->clone_of
->clone_of
)
970 cgraph_get_body (node
->clone_of
);
971 if (gimple_has_body_p (node
->clone_of
->decl
))
973 if (cgraph_dump_file
)
975 fprintf (cgraph_dump_file
, "cloning %s to %s\n",
976 xstrdup (node
->clone_of
->name ()),
977 xstrdup (node
->name ()));
978 if (node
->clone
.tree_map
)
981 fprintf (cgraph_dump_file
, " replace map: ");
983 i
< vec_safe_length (node
->clone
.tree_map
);
986 struct ipa_replace_map
*replace_info
;
987 replace_info
= (*node
->clone
.tree_map
)[i
];
988 print_generic_expr (cgraph_dump_file
, replace_info
->old_tree
, 0);
989 fprintf (cgraph_dump_file
, " -> ");
990 print_generic_expr (cgraph_dump_file
, replace_info
->new_tree
, 0);
991 fprintf (cgraph_dump_file
, "%s%s;",
992 replace_info
->replace_p
? "(replace)":"",
993 replace_info
->ref_p
? "(ref)":"");
995 fprintf (cgraph_dump_file
, "\n");
997 if (node
->clone
.args_to_skip
)
999 fprintf (cgraph_dump_file
, " args_to_skip: ");
1000 dump_bitmap (cgraph_dump_file
, node
->clone
.args_to_skip
);
1002 if (node
->clone
.args_to_skip
)
1004 fprintf (cgraph_dump_file
, " combined_args_to_skip:");
1005 dump_bitmap (cgraph_dump_file
, node
->clone
.combined_args_to_skip
);
1008 cgraph_materialize_clone (node
);
1014 FOR_EACH_FUNCTION (node
)
1015 if (!node
->analyzed
&& node
->callees
)
1017 cgraph_node_remove_callees (node
);
1018 ipa_remove_all_references (&node
->ref_list
);
1021 ipa_clear_stmts_in_references (node
);
1022 if (cgraph_dump_file
)
1023 fprintf (cgraph_dump_file
, "Materialization Call site updates done.\n");
1024 #ifdef ENABLE_CHECKING
1027 symtab_remove_unreachable_nodes (false, cgraph_dump_file
);
1030 #include "gt-cgraphclones.h"