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 "tree-flow.h"
74 #include "tree-inline.h"
75 #include "langhooks.h"
76 #include "pointer-set.h"
83 #include "diagnostic.h"
89 #include "tree-iterator.h"
90 #include "tree-dump.h"
91 #include "gimple-pretty-print.h"
93 #include "ipa-inline.h"
94 #include "ipa-utils.h"
95 #include "lto-streamer.h"
98 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
100 cgraph_clone_edge (struct cgraph_edge
*e
, struct cgraph_node
*n
,
101 gimple call_stmt
, unsigned stmt_uid
, gcov_type count_scale
,
102 int freq_scale
, bool update_original
)
104 struct cgraph_edge
*new_edge
;
105 gcov_type count
= apply_probability (e
->count
, count_scale
);
108 /* We do not want to ignore loop nest after frequency drops to 0. */
111 freq
= e
->frequency
* (gcov_type
) freq_scale
/ CGRAPH_FREQ_BASE
;
112 if (freq
> CGRAPH_FREQ_MAX
)
113 freq
= CGRAPH_FREQ_MAX
;
115 if (e
->indirect_unknown_callee
)
119 if (call_stmt
&& (decl
= gimple_call_fndecl (call_stmt
)))
121 struct cgraph_node
*callee
= cgraph_get_node (decl
);
122 gcc_checking_assert (callee
);
123 new_edge
= cgraph_create_edge (n
, callee
, call_stmt
, count
, freq
);
127 new_edge
= cgraph_create_indirect_edge (n
, call_stmt
,
128 e
->indirect_info
->ecf_flags
,
130 *new_edge
->indirect_info
= *e
->indirect_info
;
135 new_edge
= cgraph_create_edge (n
, e
->callee
, call_stmt
, count
, freq
);
136 if (e
->indirect_info
)
138 new_edge
->indirect_info
139 = ggc_alloc_cleared_cgraph_indirect_call_info ();
140 *new_edge
->indirect_info
= *e
->indirect_info
;
144 new_edge
->inline_failed
= e
->inline_failed
;
145 new_edge
->indirect_inlining_edge
= e
->indirect_inlining_edge
;
146 new_edge
->lto_stmt_uid
= stmt_uid
;
147 /* Clone flags that depend on call_stmt availability manually. */
148 new_edge
->can_throw_external
= e
->can_throw_external
;
149 new_edge
->call_stmt_cannot_inline_p
= e
->call_stmt_cannot_inline_p
;
152 e
->count
-= new_edge
->count
;
156 cgraph_call_edge_duplication_hooks (e
, new_edge
);
161 /* Create node representing clone of N executed COUNT times. Decrease
162 the execution counts from original node too.
163 The new clone will have decl set to DECL that may or may not be the same
166 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
167 function's profile to reflect the fact that part of execution is handled
169 When CALL_DUPLICATOIN_HOOK is true, the ipa passes are acknowledged about
170 the new clone. Otherwise the caller is responsible for doing so later.
172 If the new node is being inlined into another one, NEW_INLINED_TO should be
173 the outline function the new one is (even indirectly) inlined to. All hooks
174 will see this in node's global.inlined_to, when invoked. Can be NULL if the
175 node is not inlined. */
178 cgraph_clone_node (struct cgraph_node
*n
, tree decl
, gcov_type count
, int freq
,
179 bool update_original
,
180 vec
<cgraph_edge_p
> redirect_callers
,
181 bool call_duplication_hook
,
182 struct cgraph_node
*new_inlined_to
)
184 struct cgraph_node
*new_node
= cgraph_create_empty_node ();
185 struct cgraph_edge
*e
;
186 gcov_type count_scale
;
189 new_node
->symbol
.decl
= decl
;
190 symtab_register_node ((symtab_node
)new_node
);
191 new_node
->origin
= n
->origin
;
192 new_node
->symbol
.lto_file_data
= n
->symbol
.lto_file_data
;
193 if (new_node
->origin
)
195 new_node
->next_nested
= new_node
->origin
->nested
;
196 new_node
->origin
->nested
= new_node
;
198 new_node
->symbol
.analyzed
= n
->symbol
.analyzed
;
199 new_node
->symbol
.definition
= n
->symbol
.definition
;
200 new_node
->local
= n
->local
;
201 new_node
->symbol
.externally_visible
= false;
202 new_node
->local
.local
= true;
203 new_node
->global
= n
->global
;
204 new_node
->global
.inlined_to
= new_inlined_to
;
205 new_node
->rtl
= n
->rtl
;
206 new_node
->count
= count
;
207 new_node
->frequency
= n
->frequency
;
208 new_node
->clone
= n
->clone
;
209 new_node
->clone
.tree_map
= NULL
;
212 if (new_node
->count
> n
->count
)
213 count_scale
= REG_BR_PROB_BASE
;
215 count_scale
= GCOV_COMPUTE_SCALE (new_node
->count
, n
->count
);
226 FOR_EACH_VEC_ELT (redirect_callers
, i
, e
)
228 /* Redirect calls to the old version node to point to its new
230 cgraph_redirect_edge_callee (e
, new_node
);
234 for (e
= n
->callees
;e
; e
=e
->next_callee
)
235 cgraph_clone_edge (e
, new_node
, e
->call_stmt
, e
->lto_stmt_uid
,
236 count_scale
, freq
, update_original
);
238 for (e
= n
->indirect_calls
; e
; e
= e
->next_callee
)
239 cgraph_clone_edge (e
, new_node
, e
->call_stmt
, e
->lto_stmt_uid
,
240 count_scale
, freq
, update_original
);
241 ipa_clone_references ((symtab_node
)new_node
, &n
->symbol
.ref_list
);
243 new_node
->next_sibling_clone
= n
->clones
;
245 n
->clones
->prev_sibling_clone
= new_node
;
246 n
->clones
= new_node
;
247 new_node
->clone_of
= n
;
249 if (call_duplication_hook
)
250 cgraph_call_node_duplication_hooks (n
, new_node
);
254 /* Create a new name for clone of DECL, add SUFFIX. Returns an identifier. */
256 static GTY(()) unsigned int clone_fn_id_num
;
259 clone_function_name (tree decl
, const char *suffix
)
261 tree name
= DECL_ASSEMBLER_NAME (decl
);
262 size_t len
= IDENTIFIER_LENGTH (name
);
263 char *tmp_name
, *prefix
;
265 prefix
= XALLOCAVEC (char, len
+ strlen (suffix
) + 2);
266 memcpy (prefix
, IDENTIFIER_POINTER (name
), len
);
267 strcpy (prefix
+ len
+ 1, suffix
);
268 #ifndef NO_DOT_IN_LABEL
270 #elif !defined NO_DOLLAR_IN_LABEL
275 ASM_FORMAT_PRIVATE_NAME (tmp_name
, prefix
, clone_fn_id_num
++);
276 return get_identifier (tmp_name
);
279 /* Create callgraph node clone with new declaration. The actual body will
280 be copied later at compilation stage.
282 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
286 cgraph_create_virtual_clone (struct cgraph_node
*old_node
,
287 vec
<cgraph_edge_p
> redirect_callers
,
288 vec
<ipa_replace_map_p
, va_gc
> *tree_map
,
292 tree old_decl
= old_node
->symbol
.decl
;
293 struct cgraph_node
*new_node
= NULL
;
296 struct ipa_replace_map
*map
;
299 gcc_checking_assert (tree_versionable_function_p (old_decl
));
301 gcc_assert (old_node
->local
.can_change_signature
|| !args_to_skip
);
303 /* Make a new FUNCTION_DECL tree node */
305 new_decl
= copy_node (old_decl
);
307 new_decl
= build_function_decl_skip_args (old_decl
, args_to_skip
, false);
308 DECL_STRUCT_FUNCTION (new_decl
) = NULL
;
310 /* Generate a new name for the new version. */
311 DECL_NAME (new_decl
) = clone_function_name (old_decl
, suffix
);
312 SET_DECL_ASSEMBLER_NAME (new_decl
, DECL_NAME (new_decl
));
313 SET_DECL_RTL (new_decl
, NULL
);
315 new_node
= cgraph_clone_node (old_node
, new_decl
, old_node
->count
,
316 CGRAPH_FREQ_BASE
, false,
317 redirect_callers
, false, NULL
);
318 /* Update the properties.
319 Make clone visible only within this translation unit. Make sure
320 that is not weak also.
321 ??? We cannot use COMDAT linkage because there is no
322 ABI support for this. */
323 DECL_EXTERNAL (new_node
->symbol
.decl
) = 0;
324 if (DECL_ONE_ONLY (old_decl
))
325 DECL_SECTION_NAME (new_node
->symbol
.decl
) = NULL
;
326 DECL_COMDAT_GROUP (new_node
->symbol
.decl
) = 0;
327 TREE_PUBLIC (new_node
->symbol
.decl
) = 0;
328 DECL_COMDAT (new_node
->symbol
.decl
) = 0;
329 DECL_WEAK (new_node
->symbol
.decl
) = 0;
330 DECL_VIRTUAL_P (new_node
->symbol
.decl
) = 0;
331 DECL_STATIC_CONSTRUCTOR (new_node
->symbol
.decl
) = 0;
332 DECL_STATIC_DESTRUCTOR (new_node
->symbol
.decl
) = 0;
333 new_node
->clone
.tree_map
= tree_map
;
334 new_node
->clone
.args_to_skip
= args_to_skip
;
336 /* Clones of global symbols or symbols with unique names are unique. */
337 if ((TREE_PUBLIC (old_decl
)
338 && !DECL_EXTERNAL (old_decl
)
339 && !DECL_WEAK (old_decl
)
340 && !DECL_COMDAT (old_decl
))
342 new_node
->symbol
.unique_name
= true;
343 FOR_EACH_VEC_SAFE_ELT (tree_map
, i
, map
)
345 tree var
= map
->new_tree
;
346 symtab_node ref_node
;
349 if (TREE_CODE (var
) != ADDR_EXPR
)
351 var
= get_base_var (var
);
354 if (TREE_CODE (var
) != FUNCTION_DECL
355 && TREE_CODE (var
) != VAR_DECL
)
358 /* Record references of the future statement initializing the constant
360 ref_node
= symtab_get_node (var
);
361 gcc_checking_assert (ref_node
);
362 ipa_record_reference ((symtab_node
)new_node
, (symtab_node
)ref_node
,
366 new_node
->clone
.combined_args_to_skip
= old_node
->clone
.combined_args_to_skip
;
367 else if (old_node
->clone
.combined_args_to_skip
)
369 int newi
= 0, oldi
= 0;
371 bitmap new_args_to_skip
= BITMAP_GGC_ALLOC ();
372 struct cgraph_node
*orig_node
;
373 for (orig_node
= old_node
; orig_node
->clone_of
; orig_node
= orig_node
->clone_of
)
375 for (arg
= DECL_ARGUMENTS (orig_node
->symbol
.decl
);
376 arg
; arg
= DECL_CHAIN (arg
), oldi
++)
378 if (bitmap_bit_p (old_node
->clone
.combined_args_to_skip
, oldi
))
380 bitmap_set_bit (new_args_to_skip
, oldi
);
383 if (bitmap_bit_p (args_to_skip
, newi
))
384 bitmap_set_bit (new_args_to_skip
, oldi
);
387 new_node
->clone
.combined_args_to_skip
= new_args_to_skip
;
390 new_node
->clone
.combined_args_to_skip
= args_to_skip
;
391 new_node
->symbol
.externally_visible
= 0;
392 new_node
->local
.local
= 1;
393 new_node
->lowered
= true;
395 cgraph_call_node_duplication_hooks (old_node
, new_node
);
401 /* NODE is being removed from symbol table; see if its entry can be replaced by
402 other inline clone. */
404 cgraph_find_replacement_node (struct cgraph_node
*node
)
406 struct cgraph_node
*next_inline_clone
, *replacement
;
408 for (next_inline_clone
= node
->clones
;
410 && next_inline_clone
->symbol
.decl
!= node
->symbol
.decl
;
411 next_inline_clone
= next_inline_clone
->next_sibling_clone
)
414 /* If there is inline clone of the node being removed, we need
415 to put it into the position of removed node and reorganize all
416 other clones to be based on it. */
417 if (next_inline_clone
)
419 struct cgraph_node
*n
;
420 struct cgraph_node
*new_clones
;
422 replacement
= next_inline_clone
;
424 /* Unlink inline clone from the list of clones of removed node. */
425 if (next_inline_clone
->next_sibling_clone
)
426 next_inline_clone
->next_sibling_clone
->prev_sibling_clone
427 = next_inline_clone
->prev_sibling_clone
;
428 if (next_inline_clone
->prev_sibling_clone
)
430 gcc_assert (node
->clones
!= next_inline_clone
);
431 next_inline_clone
->prev_sibling_clone
->next_sibling_clone
432 = next_inline_clone
->next_sibling_clone
;
436 gcc_assert (node
->clones
== next_inline_clone
);
437 node
->clones
= next_inline_clone
->next_sibling_clone
;
440 new_clones
= node
->clones
;
443 /* Copy clone info. */
444 next_inline_clone
->clone
= node
->clone
;
446 /* Now place it into clone tree at same level at NODE. */
447 next_inline_clone
->clone_of
= node
->clone_of
;
448 next_inline_clone
->prev_sibling_clone
= NULL
;
449 next_inline_clone
->next_sibling_clone
= NULL
;
452 if (node
->clone_of
->clones
)
453 node
->clone_of
->clones
->prev_sibling_clone
= next_inline_clone
;
454 next_inline_clone
->next_sibling_clone
= node
->clone_of
->clones
;
455 node
->clone_of
->clones
= next_inline_clone
;
458 /* Merge the clone list. */
461 if (!next_inline_clone
->clones
)
462 next_inline_clone
->clones
= new_clones
;
465 n
= next_inline_clone
->clones
;
466 while (n
->next_sibling_clone
)
467 n
= n
->next_sibling_clone
;
468 n
->next_sibling_clone
= new_clones
;
469 new_clones
->prev_sibling_clone
= n
;
473 /* Update clone_of pointers. */
477 n
->clone_of
= next_inline_clone
;
478 n
= n
->next_sibling_clone
;
486 /* Like cgraph_set_call_stmt but walk the clone tree and update all
487 clones sharing the same function body. */
490 cgraph_set_call_stmt_including_clones (struct cgraph_node
*orig
,
491 gimple old_stmt
, gimple new_stmt
)
493 struct cgraph_node
*node
;
494 struct cgraph_edge
*edge
= cgraph_edge (orig
, old_stmt
);
497 cgraph_set_call_stmt (edge
, new_stmt
);
503 struct cgraph_edge
*edge
= cgraph_edge (node
, old_stmt
);
505 cgraph_set_call_stmt (edge
, new_stmt
);
508 else if (node
->next_sibling_clone
)
509 node
= node
->next_sibling_clone
;
512 while (node
!= orig
&& !node
->next_sibling_clone
)
513 node
= node
->clone_of
;
515 node
= node
->next_sibling_clone
;
520 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
521 same function body. If clones already have edge for OLD_STMT; only
522 update the edge same way as cgraph_set_call_stmt_including_clones does.
524 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
525 frequencies of the clones. */
528 cgraph_create_edge_including_clones (struct cgraph_node
*orig
,
529 struct cgraph_node
*callee
,
531 gimple stmt
, gcov_type count
,
533 cgraph_inline_failed_t reason
)
535 struct cgraph_node
*node
;
536 struct cgraph_edge
*edge
;
538 if (!cgraph_edge (orig
, stmt
))
540 edge
= cgraph_create_edge (orig
, callee
, stmt
, count
, freq
);
541 edge
->inline_failed
= reason
;
548 struct cgraph_edge
*edge
= cgraph_edge (node
, old_stmt
);
550 /* It is possible that clones already contain the edge while
551 master didn't. Either we promoted indirect call into direct
552 call in the clone or we are processing clones of unreachable
553 master where edges has been removed. */
555 cgraph_set_call_stmt (edge
, stmt
);
556 else if (!cgraph_edge (node
, stmt
))
558 edge
= cgraph_create_edge (node
, callee
, stmt
, count
,
560 edge
->inline_failed
= reason
;
565 else if (node
->next_sibling_clone
)
566 node
= node
->next_sibling_clone
;
569 while (node
!= orig
&& !node
->next_sibling_clone
)
570 node
= node
->clone_of
;
572 node
= node
->next_sibling_clone
;
577 /* Remove the node from cgraph and all inline clones inlined into it.
578 Skip however removal of FORBIDDEN_NODE and return true if it needs to be
579 removed. This allows to call the function from outer loop walking clone
583 cgraph_remove_node_and_inline_clones (struct cgraph_node
*node
, struct cgraph_node
*forbidden_node
)
585 struct cgraph_edge
*e
, *next
;
588 if (node
== forbidden_node
)
590 cgraph_remove_edge (node
->callers
);
593 for (e
= node
->callees
; e
; e
= next
)
595 next
= e
->next_callee
;
596 if (!e
->inline_failed
)
597 found
|= cgraph_remove_node_and_inline_clones (e
->callee
, forbidden_node
);
599 cgraph_remove_node (node
);
603 /* The edges representing the callers of the NEW_VERSION node were
604 fixed by cgraph_function_versioning (), now the call_expr in their
605 respective tree code should be updated to call the NEW_VERSION. */
608 update_call_expr (struct cgraph_node
*new_version
)
610 struct cgraph_edge
*e
;
612 gcc_assert (new_version
);
614 /* Update the call expr on the edges to call the new version. */
615 for (e
= new_version
->callers
; e
; e
= e
->next_caller
)
617 struct function
*inner_function
= DECL_STRUCT_FUNCTION (e
->caller
->symbol
.decl
);
618 gimple_call_set_fndecl (e
->call_stmt
, new_version
->symbol
.decl
);
619 maybe_clean_eh_stmt_fn (inner_function
, e
->call_stmt
);
624 /* Create a new cgraph node which is the new version of
625 OLD_VERSION node. REDIRECT_CALLERS holds the callers
626 edges which should be redirected to point to
627 NEW_VERSION. ALL the callees edges of OLD_VERSION
628 are cloned to the new version node. Return the new
631 If non-NULL BLOCK_TO_COPY determine what basic blocks
632 was copied to prevent duplications of calls that are dead
636 cgraph_copy_node_for_versioning (struct cgraph_node
*old_version
,
638 vec
<cgraph_edge_p
> redirect_callers
,
641 struct cgraph_node
*new_version
;
642 struct cgraph_edge
*e
;
645 gcc_assert (old_version
);
647 new_version
= cgraph_create_node (new_decl
);
649 new_version
->symbol
.analyzed
= old_version
->symbol
.analyzed
;
650 new_version
->symbol
.definition
= old_version
->symbol
.definition
;
651 new_version
->local
= old_version
->local
;
652 new_version
->symbol
.externally_visible
= false;
653 new_version
->local
.local
= new_version
->symbol
.definition
;
654 new_version
->global
= old_version
->global
;
655 new_version
->rtl
= old_version
->rtl
;
656 new_version
->count
= old_version
->count
;
658 for (e
= old_version
->callees
; e
; e
=e
->next_callee
)
660 || bitmap_bit_p (bbs_to_copy
, gimple_bb (e
->call_stmt
)->index
))
661 cgraph_clone_edge (e
, new_version
, e
->call_stmt
,
662 e
->lto_stmt_uid
, REG_BR_PROB_BASE
,
665 for (e
= old_version
->indirect_calls
; e
; e
=e
->next_callee
)
667 || bitmap_bit_p (bbs_to_copy
, gimple_bb (e
->call_stmt
)->index
))
668 cgraph_clone_edge (e
, new_version
, e
->call_stmt
,
669 e
->lto_stmt_uid
, REG_BR_PROB_BASE
,
672 FOR_EACH_VEC_ELT (redirect_callers
, i
, e
)
674 /* Redirect calls to the old version node to point to its new
676 cgraph_redirect_edge_callee (e
, new_version
);
679 cgraph_call_node_duplication_hooks (old_version
, new_version
);
684 /* Perform function versioning.
685 Function versioning includes copying of the tree and
686 a callgraph update (creating a new cgraph node and updating
687 its callees and callers).
689 REDIRECT_CALLERS varray includes the edges to be redirected
692 TREE_MAP is a mapping of tree nodes we want to replace with
693 new ones (according to results of prior analysis).
694 OLD_VERSION_NODE is the node that is versioned.
696 If non-NULL ARGS_TO_SKIP determine function parameters to remove
698 If SKIP_RETURN is true, the new version will return void.
699 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
700 If non_NULL NEW_ENTRY determine new entry BB of the clone.
702 Return the new version's cgraph node. */
705 cgraph_function_versioning (struct cgraph_node
*old_version_node
,
706 vec
<cgraph_edge_p
> redirect_callers
,
707 vec
<ipa_replace_map_p
, va_gc
> *tree_map
,
711 basic_block new_entry_block
,
712 const char *clone_name
)
714 tree old_decl
= old_version_node
->symbol
.decl
;
715 struct cgraph_node
*new_version_node
= NULL
;
718 if (!tree_versionable_function_p (old_decl
))
721 gcc_assert (old_version_node
->local
.can_change_signature
|| !args_to_skip
);
723 /* Make a new FUNCTION_DECL tree node for the new version. */
724 if (!args_to_skip
&& !skip_return
)
725 new_decl
= copy_node (old_decl
);
728 = build_function_decl_skip_args (old_decl
, args_to_skip
, skip_return
);
730 /* Generate a new name for the new version. */
731 DECL_NAME (new_decl
) = clone_function_name (old_decl
, clone_name
);
732 SET_DECL_ASSEMBLER_NAME (new_decl
, DECL_NAME (new_decl
));
733 SET_DECL_RTL (new_decl
, NULL
);
735 /* When the old decl was a con-/destructor make sure the clone isn't. */
736 DECL_STATIC_CONSTRUCTOR(new_decl
) = 0;
737 DECL_STATIC_DESTRUCTOR(new_decl
) = 0;
739 /* Create the new version's call-graph node.
740 and update the edges of the new node. */
742 cgraph_copy_node_for_versioning (old_version_node
, new_decl
,
743 redirect_callers
, bbs_to_copy
);
745 /* Copy the OLD_VERSION_NODE function tree to the new version. */
746 tree_function_versioning (old_decl
, new_decl
, tree_map
, false, args_to_skip
,
747 skip_return
, bbs_to_copy
, new_entry_block
);
749 /* Update the new version's properties.
750 Make The new version visible only within this translation unit. Make sure
751 that is not weak also.
752 ??? We cannot use COMDAT linkage because there is no
753 ABI support for this. */
754 symtab_make_decl_local (new_version_node
->symbol
.decl
);
755 DECL_VIRTUAL_P (new_version_node
->symbol
.decl
) = 0;
756 new_version_node
->symbol
.externally_visible
= 0;
757 new_version_node
->local
.local
= 1;
758 new_version_node
->lowered
= true;
759 /* Clones of global symbols or symbols with unique names are unique. */
760 if ((TREE_PUBLIC (old_decl
)
761 && !DECL_EXTERNAL (old_decl
)
762 && !DECL_WEAK (old_decl
)
763 && !DECL_COMDAT (old_decl
))
765 new_version_node
->symbol
.unique_name
= true;
767 /* Update the call_expr on the edges to call the new version node. */
768 update_call_expr (new_version_node
);
770 cgraph_call_function_insertion_hooks (new_version_node
);
771 return new_version_node
;
774 /* Given virtual clone, turn it into actual clone. */
777 cgraph_materialize_clone (struct cgraph_node
*node
)
779 bitmap_obstack_initialize (NULL
);
780 node
->former_clone_of
= node
->clone_of
->symbol
.decl
;
781 if (node
->clone_of
->former_clone_of
)
782 node
->former_clone_of
= node
->clone_of
->former_clone_of
;
783 /* Copy the OLD_VERSION_NODE function tree to the new version. */
784 tree_function_versioning (node
->clone_of
->symbol
.decl
, node
->symbol
.decl
,
785 node
->clone
.tree_map
, true,
786 node
->clone
.args_to_skip
, false,
788 if (cgraph_dump_file
)
790 dump_function_to_file (node
->clone_of
->symbol
.decl
, cgraph_dump_file
, dump_flags
);
791 dump_function_to_file (node
->symbol
.decl
, cgraph_dump_file
, dump_flags
);
794 /* Function is no longer clone. */
795 if (node
->next_sibling_clone
)
796 node
->next_sibling_clone
->prev_sibling_clone
= node
->prev_sibling_clone
;
797 if (node
->prev_sibling_clone
)
798 node
->prev_sibling_clone
->next_sibling_clone
= node
->next_sibling_clone
;
800 node
->clone_of
->clones
= node
->next_sibling_clone
;
801 node
->next_sibling_clone
= NULL
;
802 node
->prev_sibling_clone
= NULL
;
803 if (!node
->clone_of
->symbol
.analyzed
&& !node
->clone_of
->clones
)
805 cgraph_release_function_body (node
->clone_of
);
806 cgraph_node_remove_callees (node
->clone_of
);
807 ipa_remove_all_references (&node
->clone_of
->symbol
.ref_list
);
809 node
->clone_of
= NULL
;
810 bitmap_obstack_release (NULL
);
813 /* Once all functions from compilation unit are in memory, produce all clones
814 and update all calls. We might also do this on demand if we don't want to
815 bring all functions to memory prior compilation, but current WHOPR
816 implementation does that and it is is bit easier to keep everything right in
820 cgraph_materialize_all_clones (void)
822 struct cgraph_node
*node
;
823 bool stabilized
= false;
825 if (cgraph_dump_file
)
826 fprintf (cgraph_dump_file
, "Materializing clones\n");
827 #ifdef ENABLE_CHECKING
831 /* We can also do topological order, but number of iterations should be
832 bounded by number of IPA passes since single IPA pass is probably not
833 going to create clones of clones it created itself. */
837 FOR_EACH_FUNCTION (node
)
839 if (node
->clone_of
&& node
->symbol
.decl
!= node
->clone_of
->symbol
.decl
840 && !gimple_has_body_p (node
->symbol
.decl
))
842 if (gimple_has_body_p (node
->clone_of
->symbol
.decl
))
844 if (cgraph_dump_file
)
846 fprintf (cgraph_dump_file
, "cloning %s to %s\n",
847 xstrdup (cgraph_node_name (node
->clone_of
)),
848 xstrdup (cgraph_node_name (node
)));
849 if (node
->clone
.tree_map
)
852 fprintf (cgraph_dump_file
, " replace map: ");
854 i
< vec_safe_length (node
->clone
.tree_map
);
857 struct ipa_replace_map
*replace_info
;
858 replace_info
= (*node
->clone
.tree_map
)[i
];
859 print_generic_expr (cgraph_dump_file
, replace_info
->old_tree
, 0);
860 fprintf (cgraph_dump_file
, " -> ");
861 print_generic_expr (cgraph_dump_file
, replace_info
->new_tree
, 0);
862 fprintf (cgraph_dump_file
, "%s%s;",
863 replace_info
->replace_p
? "(replace)":"",
864 replace_info
->ref_p
? "(ref)":"");
866 fprintf (cgraph_dump_file
, "\n");
868 if (node
->clone
.args_to_skip
)
870 fprintf (cgraph_dump_file
, " args_to_skip: ");
871 dump_bitmap (cgraph_dump_file
, node
->clone
.args_to_skip
);
873 if (node
->clone
.args_to_skip
)
875 fprintf (cgraph_dump_file
, " combined_args_to_skip:");
876 dump_bitmap (cgraph_dump_file
, node
->clone
.combined_args_to_skip
);
879 cgraph_materialize_clone (node
);
885 FOR_EACH_FUNCTION (node
)
886 if (!node
->symbol
.analyzed
&& node
->callees
)
887 cgraph_node_remove_callees (node
);
888 if (cgraph_dump_file
)
889 fprintf (cgraph_dump_file
, "Materialization Call site updates done.\n");
890 #ifdef ENABLE_CHECKING
893 symtab_remove_unreachable_nodes (false, cgraph_dump_file
);
896 #include "gt-cgraphclones.h"