2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
3 2011, 2012 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This module provide facilities for clonning functions. I.e. creating
23 new functions based on existing functions with simple modifications,
24 such as replacement of parameters.
26 To allow whole program optimization without actual presence of function
27 bodies, an additional infrastructure is provided for so-called virtual
30 A virtual clone in the callgraph is a function that has no
31 associated body, just a description of how to create its body based
32 on a different function (which itself may be a virtual clone).
34 The description of function modifications includes adjustments to
35 the function's signature (which allows, for example, removing or
36 adding function arguments), substitutions to perform on the
37 function body, and, for inlined functions, a pointer to the
38 function that it will be inlined into.
40 It is also possible to redirect any edge of the callgraph from a
41 function to its virtual clone. This implies updating of the call
42 site to adjust for the new function signature.
44 Most of the transformations performed by inter-procedural
45 optimizations can be represented via virtual clones. For
46 instance, a constant propagation pass can produce a virtual clone
47 of the function which replaces one of its arguments by a
48 constant. The inliner can represent its decisions by producing a
49 clone of a function whose body will be later integrated into
52 Using virtual clones, the program can be easily updated
53 during the Execute stage, solving most of pass interactions
54 problems that would otherwise occur during Transform.
56 Virtual clones are later materialized in the LTRANS stage and
57 turned into real functions. Passes executed after the virtual
58 clone were introduced also perform their Transform stage
59 on new functions, so for a pass there is no significant
60 difference between operating on a real function or a virtual
61 clone introduced before its Execute stage.
63 Optimization passes then work on virtual clones introduced before
64 their Execute stage as if they were real functions. The
65 only difference is that clones are not visible during the
66 Generate Summary stage. */
70 #include "coretypes.h"
74 #include "tree-flow.h"
75 #include "tree-inline.h"
76 #include "langhooks.h"
77 #include "pointer-set.h"
84 #include "diagnostic.h"
92 #include "tree-iterator.h"
93 #include "tree-pass.h"
94 #include "tree-dump.h"
95 #include "gimple-pretty-print.h"
97 #include "ipa-inline.h"
98 #include "ipa-utils.h"
99 #include "lto-streamer.h"
102 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
104 cgraph_clone_edge (struct cgraph_edge
*e
, struct cgraph_node
*n
,
105 gimple call_stmt
, unsigned stmt_uid
, gcov_type count_scale
,
106 int freq_scale
, bool update_original
)
108 struct cgraph_edge
*new_edge
;
109 gcov_type count
= e
->count
* count_scale
/ REG_BR_PROB_BASE
;
112 /* We do not want to ignore loop nest after frequency drops to 0. */
115 freq
= e
->frequency
* (gcov_type
) freq_scale
/ CGRAPH_FREQ_BASE
;
116 if (freq
> CGRAPH_FREQ_MAX
)
117 freq
= CGRAPH_FREQ_MAX
;
119 if (e
->indirect_unknown_callee
)
123 if (call_stmt
&& (decl
= gimple_call_fndecl (call_stmt
)))
125 struct cgraph_node
*callee
= cgraph_get_node (decl
);
126 gcc_checking_assert (callee
);
127 new_edge
= cgraph_create_edge (n
, callee
, call_stmt
, count
, freq
);
131 new_edge
= cgraph_create_indirect_edge (n
, call_stmt
,
132 e
->indirect_info
->ecf_flags
,
134 *new_edge
->indirect_info
= *e
->indirect_info
;
139 new_edge
= cgraph_create_edge (n
, e
->callee
, call_stmt
, count
, freq
);
140 if (e
->indirect_info
)
142 new_edge
->indirect_info
143 = ggc_alloc_cleared_cgraph_indirect_call_info ();
144 *new_edge
->indirect_info
= *e
->indirect_info
;
148 new_edge
->inline_failed
= e
->inline_failed
;
149 new_edge
->indirect_inlining_edge
= e
->indirect_inlining_edge
;
150 new_edge
->lto_stmt_uid
= stmt_uid
;
151 /* Clone flags that depend on call_stmt availability manually. */
152 new_edge
->can_throw_external
= e
->can_throw_external
;
153 new_edge
->call_stmt_cannot_inline_p
= e
->call_stmt_cannot_inline_p
;
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. */
177 cgraph_clone_node (struct cgraph_node
*n
, tree decl
, gcov_type count
, int freq
,
178 bool update_original
,
179 VEC(cgraph_edge_p
,heap
) *redirect_callers
,
180 bool call_duplication_hook
)
182 struct cgraph_node
*new_node
= cgraph_create_empty_node ();
183 struct cgraph_edge
*e
;
184 gcov_type count_scale
;
187 new_node
->symbol
.decl
= decl
;
188 symtab_register_node ((symtab_node
)new_node
);
189 new_node
->origin
= n
->origin
;
190 if (new_node
->origin
)
192 new_node
->next_nested
= new_node
->origin
->nested
;
193 new_node
->origin
->nested
= new_node
;
195 new_node
->analyzed
= n
->analyzed
;
196 new_node
->local
= n
->local
;
197 new_node
->symbol
.externally_visible
= false;
198 new_node
->local
.local
= true;
199 new_node
->global
= n
->global
;
200 new_node
->rtl
= n
->rtl
;
201 new_node
->count
= count
;
202 new_node
->frequency
= n
->frequency
;
203 new_node
->clone
= n
->clone
;
204 new_node
->clone
.tree_map
= 0;
207 if (new_node
->count
> n
->count
)
208 count_scale
= REG_BR_PROB_BASE
;
210 count_scale
= new_node
->count
* REG_BR_PROB_BASE
/ n
->count
;
221 FOR_EACH_VEC_ELT (cgraph_edge_p
, redirect_callers
, i
, e
)
223 /* Redirect calls to the old version node to point to its new
225 cgraph_redirect_edge_callee (e
, new_node
);
229 for (e
= n
->callees
;e
; e
=e
->next_callee
)
230 cgraph_clone_edge (e
, new_node
, e
->call_stmt
, e
->lto_stmt_uid
,
231 count_scale
, freq
, update_original
);
233 for (e
= n
->indirect_calls
; e
; e
= e
->next_callee
)
234 cgraph_clone_edge (e
, new_node
, e
->call_stmt
, e
->lto_stmt_uid
,
235 count_scale
, freq
, update_original
);
236 ipa_clone_references ((symtab_node
)new_node
, &n
->symbol
.ref_list
);
238 new_node
->next_sibling_clone
= n
->clones
;
240 n
->clones
->prev_sibling_clone
= new_node
;
241 n
->clones
= new_node
;
242 new_node
->clone_of
= n
;
244 if (call_duplication_hook
)
245 cgraph_call_node_duplication_hooks (n
, new_node
);
249 /* Create a new name for clone of DECL, add SUFFIX. Returns an identifier. */
251 static GTY(()) unsigned int clone_fn_id_num
;
254 clone_function_name (tree decl
, const char *suffix
)
256 tree name
= DECL_ASSEMBLER_NAME (decl
);
257 size_t len
= IDENTIFIER_LENGTH (name
);
258 char *tmp_name
, *prefix
;
260 prefix
= XALLOCAVEC (char, len
+ strlen (suffix
) + 2);
261 memcpy (prefix
, IDENTIFIER_POINTER (name
), len
);
262 strcpy (prefix
+ len
+ 1, suffix
);
263 #ifndef NO_DOT_IN_LABEL
265 #elif !defined NO_DOLLAR_IN_LABEL
270 ASM_FORMAT_PRIVATE_NAME (tmp_name
, prefix
, clone_fn_id_num
++);
271 return get_identifier (tmp_name
);
274 /* Create callgraph node clone with new declaration. The actual body will
275 be copied later at compilation stage.
277 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
281 cgraph_create_virtual_clone (struct cgraph_node
*old_node
,
282 VEC(cgraph_edge_p
,heap
) *redirect_callers
,
283 VEC(ipa_replace_map_p
,gc
) *tree_map
,
287 tree old_decl
= old_node
->symbol
.decl
;
288 struct cgraph_node
*new_node
= NULL
;
291 struct ipa_replace_map
*map
;
294 gcc_checking_assert (tree_versionable_function_p (old_decl
));
296 gcc_assert (old_node
->local
.can_change_signature
|| !args_to_skip
);
298 /* Make a new FUNCTION_DECL tree node */
300 new_decl
= copy_node (old_decl
);
302 new_decl
= build_function_decl_skip_args (old_decl
, args_to_skip
, false);
303 DECL_STRUCT_FUNCTION (new_decl
) = NULL
;
305 /* Generate a new name for the new version. */
306 DECL_NAME (new_decl
) = clone_function_name (old_decl
, suffix
);
307 SET_DECL_ASSEMBLER_NAME (new_decl
, DECL_NAME (new_decl
));
308 SET_DECL_RTL (new_decl
, NULL
);
310 new_node
= cgraph_clone_node (old_node
, new_decl
, old_node
->count
,
311 CGRAPH_FREQ_BASE
, false,
312 redirect_callers
, false);
313 /* Update the properties.
314 Make clone visible only within this translation unit. Make sure
315 that is not weak also.
316 ??? We cannot use COMDAT linkage because there is no
317 ABI support for this. */
318 DECL_EXTERNAL (new_node
->symbol
.decl
) = 0;
319 if (DECL_ONE_ONLY (old_decl
))
320 DECL_SECTION_NAME (new_node
->symbol
.decl
) = NULL
;
321 DECL_COMDAT_GROUP (new_node
->symbol
.decl
) = 0;
322 TREE_PUBLIC (new_node
->symbol
.decl
) = 0;
323 DECL_COMDAT (new_node
->symbol
.decl
) = 0;
324 DECL_WEAK (new_node
->symbol
.decl
) = 0;
325 DECL_STATIC_CONSTRUCTOR (new_node
->symbol
.decl
) = 0;
326 DECL_STATIC_DESTRUCTOR (new_node
->symbol
.decl
) = 0;
327 new_node
->clone
.tree_map
= tree_map
;
328 new_node
->clone
.args_to_skip
= args_to_skip
;
329 FOR_EACH_VEC_ELT (ipa_replace_map_p
, tree_map
, i
, map
)
331 tree var
= map
->new_tree
;
332 symtab_node ref_node
;
335 if (TREE_CODE (var
) != ADDR_EXPR
)
337 var
= get_base_var (var
);
340 if (TREE_CODE (var
) != FUNCTION_DECL
341 && TREE_CODE (var
) != VAR_DECL
)
344 /* Record references of the future statement initializing the constant
346 ref_node
= symtab_get_node (var
);
347 gcc_checking_assert (ref_node
);
348 ipa_record_reference ((symtab_node
)new_node
, (symtab_node
)ref_node
,
352 new_node
->clone
.combined_args_to_skip
= old_node
->clone
.combined_args_to_skip
;
353 else if (old_node
->clone
.combined_args_to_skip
)
355 int newi
= 0, oldi
= 0;
357 bitmap new_args_to_skip
= BITMAP_GGC_ALLOC ();
358 struct cgraph_node
*orig_node
;
359 for (orig_node
= old_node
; orig_node
->clone_of
; orig_node
= orig_node
->clone_of
)
361 for (arg
= DECL_ARGUMENTS (orig_node
->symbol
.decl
);
362 arg
; arg
= DECL_CHAIN (arg
), oldi
++)
364 if (bitmap_bit_p (old_node
->clone
.combined_args_to_skip
, oldi
))
366 bitmap_set_bit (new_args_to_skip
, oldi
);
369 if (bitmap_bit_p (args_to_skip
, newi
))
370 bitmap_set_bit (new_args_to_skip
, oldi
);
373 new_node
->clone
.combined_args_to_skip
= new_args_to_skip
;
376 new_node
->clone
.combined_args_to_skip
= args_to_skip
;
377 new_node
->symbol
.externally_visible
= 0;
378 new_node
->local
.local
= 1;
379 new_node
->lowered
= true;
381 cgraph_call_node_duplication_hooks (old_node
, new_node
);
387 /* NODE is being removed from symbol table; see if its entry can be replaced by
388 other inline clone. */
390 cgraph_find_replacement_node (struct cgraph_node
*node
)
392 struct cgraph_node
*next_inline_clone
, *replacement
;
394 for (next_inline_clone
= node
->clones
;
396 && next_inline_clone
->symbol
.decl
!= node
->symbol
.decl
;
397 next_inline_clone
= next_inline_clone
->next_sibling_clone
)
400 /* If there is inline clone of the node being removed, we need
401 to put it into the position of removed node and reorganize all
402 other clones to be based on it. */
403 if (next_inline_clone
)
405 struct cgraph_node
*n
;
406 struct cgraph_node
*new_clones
;
408 replacement
= next_inline_clone
;
410 /* Unlink inline clone from the list of clones of removed node. */
411 if (next_inline_clone
->next_sibling_clone
)
412 next_inline_clone
->next_sibling_clone
->prev_sibling_clone
413 = next_inline_clone
->prev_sibling_clone
;
414 if (next_inline_clone
->prev_sibling_clone
)
416 gcc_assert (node
->clones
!= next_inline_clone
);
417 next_inline_clone
->prev_sibling_clone
->next_sibling_clone
418 = next_inline_clone
->next_sibling_clone
;
422 gcc_assert (node
->clones
== next_inline_clone
);
423 node
->clones
= next_inline_clone
->next_sibling_clone
;
426 new_clones
= node
->clones
;
429 /* Copy clone info. */
430 next_inline_clone
->clone
= node
->clone
;
432 /* Now place it into clone tree at same level at NODE. */
433 next_inline_clone
->clone_of
= node
->clone_of
;
434 next_inline_clone
->prev_sibling_clone
= NULL
;
435 next_inline_clone
->next_sibling_clone
= NULL
;
438 if (node
->clone_of
->clones
)
439 node
->clone_of
->clones
->prev_sibling_clone
= next_inline_clone
;
440 next_inline_clone
->next_sibling_clone
= node
->clone_of
->clones
;
441 node
->clone_of
->clones
= next_inline_clone
;
444 /* Merge the clone list. */
447 if (!next_inline_clone
->clones
)
448 next_inline_clone
->clones
= new_clones
;
451 n
= next_inline_clone
->clones
;
452 while (n
->next_sibling_clone
)
453 n
= n
->next_sibling_clone
;
454 n
->next_sibling_clone
= new_clones
;
455 new_clones
->prev_sibling_clone
= n
;
459 /* Update clone_of pointers. */
463 n
->clone_of
= next_inline_clone
;
464 n
= n
->next_sibling_clone
;
472 /* Like cgraph_set_call_stmt but walk the clone tree and update all
473 clones sharing the same function body. */
476 cgraph_set_call_stmt_including_clones (struct cgraph_node
*orig
,
477 gimple old_stmt
, gimple new_stmt
)
479 struct cgraph_node
*node
;
480 struct cgraph_edge
*edge
= cgraph_edge (orig
, old_stmt
);
483 cgraph_set_call_stmt (edge
, new_stmt
);
489 struct cgraph_edge
*edge
= cgraph_edge (node
, old_stmt
);
491 cgraph_set_call_stmt (edge
, new_stmt
);
494 else if (node
->next_sibling_clone
)
495 node
= node
->next_sibling_clone
;
498 while (node
!= orig
&& !node
->next_sibling_clone
)
499 node
= node
->clone_of
;
501 node
= node
->next_sibling_clone
;
506 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
507 same function body. If clones already have edge for OLD_STMT; only
508 update the edge same way as cgraph_set_call_stmt_including_clones does.
510 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
511 frequencies of the clones. */
514 cgraph_create_edge_including_clones (struct cgraph_node
*orig
,
515 struct cgraph_node
*callee
,
517 gimple stmt
, gcov_type count
,
519 cgraph_inline_failed_t reason
)
521 struct cgraph_node
*node
;
522 struct cgraph_edge
*edge
;
524 if (!cgraph_edge (orig
, stmt
))
526 edge
= cgraph_create_edge (orig
, callee
, stmt
, count
, freq
);
527 edge
->inline_failed
= reason
;
534 struct cgraph_edge
*edge
= cgraph_edge (node
, old_stmt
);
536 /* It is possible that clones already contain the edge while
537 master didn't. Either we promoted indirect call into direct
538 call in the clone or we are processing clones of unreachable
539 master where edges has been removed. */
541 cgraph_set_call_stmt (edge
, stmt
);
542 else if (!cgraph_edge (node
, stmt
))
544 edge
= cgraph_create_edge (node
, callee
, stmt
, count
,
546 edge
->inline_failed
= reason
;
551 else if (node
->next_sibling_clone
)
552 node
= node
->next_sibling_clone
;
555 while (node
!= orig
&& !node
->next_sibling_clone
)
556 node
= node
->clone_of
;
558 node
= node
->next_sibling_clone
;
563 /* Remove the node from cgraph and all inline clones inlined into it.
564 Skip however removal of FORBIDDEN_NODE and return true if it needs to be
565 removed. This allows to call the function from outer loop walking clone
569 cgraph_remove_node_and_inline_clones (struct cgraph_node
*node
, struct cgraph_node
*forbidden_node
)
571 struct cgraph_edge
*e
, *next
;
574 if (node
== forbidden_node
)
576 for (e
= node
->callees
; e
; e
= next
)
578 next
= e
->next_callee
;
579 if (!e
->inline_failed
)
580 found
|= cgraph_remove_node_and_inline_clones (e
->callee
, forbidden_node
);
582 cgraph_remove_node (node
);
586 /* The edges representing the callers of the NEW_VERSION node were
587 fixed by cgraph_function_versioning (), now the call_expr in their
588 respective tree code should be updated to call the NEW_VERSION. */
591 update_call_expr (struct cgraph_node
*new_version
)
593 struct cgraph_edge
*e
;
595 gcc_assert (new_version
);
597 /* Update the call expr on the edges to call the new version. */
598 for (e
= new_version
->callers
; e
; e
= e
->next_caller
)
600 struct function
*inner_function
= DECL_STRUCT_FUNCTION (e
->caller
->symbol
.decl
);
601 gimple_call_set_fndecl (e
->call_stmt
, new_version
->symbol
.decl
);
602 maybe_clean_eh_stmt_fn (inner_function
, e
->call_stmt
);
607 /* Create a new cgraph node which is the new version of
608 OLD_VERSION node. REDIRECT_CALLERS holds the callers
609 edges which should be redirected to point to
610 NEW_VERSION. ALL the callees edges of OLD_VERSION
611 are cloned to the new version node. Return the new
614 If non-NULL BLOCK_TO_COPY determine what basic blocks
615 was copied to prevent duplications of calls that are dead
619 cgraph_copy_node_for_versioning (struct cgraph_node
*old_version
,
621 VEC(cgraph_edge_p
,heap
) *redirect_callers
,
624 struct cgraph_node
*new_version
;
625 struct cgraph_edge
*e
;
628 gcc_assert (old_version
);
630 new_version
= cgraph_create_node (new_decl
);
632 new_version
->analyzed
= old_version
->analyzed
;
633 new_version
->local
= old_version
->local
;
634 new_version
->symbol
.externally_visible
= false;
635 new_version
->local
.local
= old_version
->analyzed
;
636 new_version
->global
= old_version
->global
;
637 new_version
->rtl
= old_version
->rtl
;
638 new_version
->count
= old_version
->count
;
640 for (e
= old_version
->callees
; e
; e
=e
->next_callee
)
642 || bitmap_bit_p (bbs_to_copy
, gimple_bb (e
->call_stmt
)->index
))
643 cgraph_clone_edge (e
, new_version
, e
->call_stmt
,
644 e
->lto_stmt_uid
, REG_BR_PROB_BASE
,
647 for (e
= old_version
->indirect_calls
; e
; e
=e
->next_callee
)
649 || bitmap_bit_p (bbs_to_copy
, gimple_bb (e
->call_stmt
)->index
))
650 cgraph_clone_edge (e
, new_version
, e
->call_stmt
,
651 e
->lto_stmt_uid
, REG_BR_PROB_BASE
,
654 FOR_EACH_VEC_ELT (cgraph_edge_p
, redirect_callers
, i
, e
)
656 /* Redirect calls to the old version node to point to its new
658 cgraph_redirect_edge_callee (e
, new_version
);
661 cgraph_call_node_duplication_hooks (old_version
, new_version
);
666 /* Perform function versioning.
667 Function versioning includes copying of the tree and
668 a callgraph update (creating a new cgraph node and updating
669 its callees and callers).
671 REDIRECT_CALLERS varray includes the edges to be redirected
674 TREE_MAP is a mapping of tree nodes we want to replace with
675 new ones (according to results of prior analysis).
676 OLD_VERSION_NODE is the node that is versioned.
678 If non-NULL ARGS_TO_SKIP determine function parameters to remove
680 If SKIP_RETURN is true, the new version will return void.
681 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
682 If non_NULL NEW_ENTRY determine new entry BB of the clone.
684 Return the new version's cgraph node. */
687 cgraph_function_versioning (struct cgraph_node
*old_version_node
,
688 VEC(cgraph_edge_p
,heap
) *redirect_callers
,
689 VEC (ipa_replace_map_p
,gc
)* tree_map
,
693 basic_block new_entry_block
,
694 const char *clone_name
)
696 tree old_decl
= old_version_node
->symbol
.decl
;
697 struct cgraph_node
*new_version_node
= NULL
;
700 if (!tree_versionable_function_p (old_decl
))
703 gcc_assert (old_version_node
->local
.can_change_signature
|| !args_to_skip
);
705 /* Make a new FUNCTION_DECL tree node for the new version. */
706 if (!args_to_skip
&& !skip_return
)
707 new_decl
= copy_node (old_decl
);
710 = build_function_decl_skip_args (old_decl
, args_to_skip
, skip_return
);
712 /* Generate a new name for the new version. */
713 DECL_NAME (new_decl
) = clone_function_name (old_decl
, clone_name
);
714 SET_DECL_ASSEMBLER_NAME (new_decl
, DECL_NAME (new_decl
));
715 SET_DECL_RTL (new_decl
, NULL
);
717 /* When the old decl was a con-/destructor make sure the clone isn't. */
718 DECL_STATIC_CONSTRUCTOR(new_decl
) = 0;
719 DECL_STATIC_DESTRUCTOR(new_decl
) = 0;
721 /* Create the new version's call-graph node.
722 and update the edges of the new node. */
724 cgraph_copy_node_for_versioning (old_version_node
, new_decl
,
725 redirect_callers
, bbs_to_copy
);
727 /* Copy the OLD_VERSION_NODE function tree to the new version. */
728 tree_function_versioning (old_decl
, new_decl
, tree_map
, false, args_to_skip
,
729 skip_return
, bbs_to_copy
, new_entry_block
);
731 /* Update the new version's properties.
732 Make The new version visible only within this translation unit. Make sure
733 that is not weak also.
734 ??? We cannot use COMDAT linkage because there is no
735 ABI support for this. */
736 symtab_make_decl_local (new_version_node
->symbol
.decl
);
737 DECL_VIRTUAL_P (new_version_node
->symbol
.decl
) = 0;
738 new_version_node
->symbol
.externally_visible
= 0;
739 new_version_node
->local
.local
= 1;
740 new_version_node
->lowered
= true;
742 /* Update the call_expr on the edges to call the new version node. */
743 update_call_expr (new_version_node
);
745 cgraph_call_function_insertion_hooks (new_version_node
);
746 return new_version_node
;
749 /* Given virtual clone, turn it into actual clone. */
752 cgraph_materialize_clone (struct cgraph_node
*node
)
754 bitmap_obstack_initialize (NULL
);
755 node
->former_clone_of
= node
->clone_of
->symbol
.decl
;
756 if (node
->clone_of
->former_clone_of
)
757 node
->former_clone_of
= node
->clone_of
->former_clone_of
;
758 /* Copy the OLD_VERSION_NODE function tree to the new version. */
759 tree_function_versioning (node
->clone_of
->symbol
.decl
, node
->symbol
.decl
,
760 node
->clone
.tree_map
, true,
761 node
->clone
.args_to_skip
, false,
763 if (cgraph_dump_file
)
765 dump_function_to_file (node
->clone_of
->symbol
.decl
, cgraph_dump_file
, dump_flags
);
766 dump_function_to_file (node
->symbol
.decl
, cgraph_dump_file
, dump_flags
);
769 /* Function is no longer clone. */
770 if (node
->next_sibling_clone
)
771 node
->next_sibling_clone
->prev_sibling_clone
= node
->prev_sibling_clone
;
772 if (node
->prev_sibling_clone
)
773 node
->prev_sibling_clone
->next_sibling_clone
= node
->next_sibling_clone
;
775 node
->clone_of
->clones
= node
->next_sibling_clone
;
776 node
->next_sibling_clone
= NULL
;
777 node
->prev_sibling_clone
= NULL
;
778 if (!node
->clone_of
->analyzed
&& !node
->clone_of
->clones
)
780 cgraph_release_function_body (node
->clone_of
);
781 cgraph_node_remove_callees (node
->clone_of
);
782 ipa_remove_all_references (&node
->clone_of
->symbol
.ref_list
);
784 node
->clone_of
= NULL
;
785 bitmap_obstack_release (NULL
);
788 /* Once all functions from compilation unit are in memory, produce all clones
789 and update all calls. We might also do this on demand if we don't want to
790 bring all functions to memory prior compilation, but current WHOPR
791 implementation does that and it is is bit easier to keep everything right in
795 cgraph_materialize_all_clones (void)
797 struct cgraph_node
*node
;
798 bool stabilized
= false;
800 if (cgraph_dump_file
)
801 fprintf (cgraph_dump_file
, "Materializing clones\n");
802 #ifdef ENABLE_CHECKING
806 /* We can also do topological order, but number of iterations should be
807 bounded by number of IPA passes since single IPA pass is probably not
808 going to create clones of clones it created itself. */
812 FOR_EACH_FUNCTION (node
)
814 if (node
->clone_of
&& node
->symbol
.decl
!= node
->clone_of
->symbol
.decl
815 && !gimple_has_body_p (node
->symbol
.decl
))
817 if (gimple_has_body_p (node
->clone_of
->symbol
.decl
))
819 if (cgraph_dump_file
)
821 fprintf (cgraph_dump_file
, "cloning %s to %s\n",
822 xstrdup (cgraph_node_name (node
->clone_of
)),
823 xstrdup (cgraph_node_name (node
)));
824 if (node
->clone
.tree_map
)
827 fprintf (cgraph_dump_file
, " replace map: ");
828 for (i
= 0; i
< VEC_length (ipa_replace_map_p
,
829 node
->clone
.tree_map
);
832 struct ipa_replace_map
*replace_info
;
833 replace_info
= VEC_index (ipa_replace_map_p
,
834 node
->clone
.tree_map
,
836 print_generic_expr (cgraph_dump_file
, replace_info
->old_tree
, 0);
837 fprintf (cgraph_dump_file
, " -> ");
838 print_generic_expr (cgraph_dump_file
, replace_info
->new_tree
, 0);
839 fprintf (cgraph_dump_file
, "%s%s;",
840 replace_info
->replace_p
? "(replace)":"",
841 replace_info
->ref_p
? "(ref)":"");
843 fprintf (cgraph_dump_file
, "\n");
845 if (node
->clone
.args_to_skip
)
847 fprintf (cgraph_dump_file
, " args_to_skip: ");
848 dump_bitmap (cgraph_dump_file
, node
->clone
.args_to_skip
);
850 if (node
->clone
.args_to_skip
)
852 fprintf (cgraph_dump_file
, " combined_args_to_skip:");
853 dump_bitmap (cgraph_dump_file
, node
->clone
.combined_args_to_skip
);
856 cgraph_materialize_clone (node
);
862 FOR_EACH_FUNCTION (node
)
863 if (!node
->analyzed
&& node
->callees
)
864 cgraph_node_remove_callees (node
);
865 if (cgraph_dump_file
)
866 fprintf (cgraph_dump_file
, "Materialization Call site updates done.\n");
867 #ifdef ENABLE_CHECKING
870 symtab_remove_unreachable_nodes (false, cgraph_dump_file
);
873 #include "gt-cgraphclones.h"