2013-11-13 Christophe Lyon <christophe.lyon@linaro.org>
[official-gcc.git] / gcc / cgraphclones.c
blobf91fcfc6fd45bfa3f617e36f3372bfda542079c0
1 /* Callgraph clones
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
10 version.
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
15 for more details.
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
27 clones
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
49 a given function.
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. */
67 #include "config.h"
68 #include "system.h"
69 #include "coretypes.h"
70 #include "tm.h"
71 #include "tree.h"
72 #include "gimple.h"
73 #include "rtl.h"
74 #include "bitmap.h"
75 #include "tree-cfg.h"
76 #include "tree-inline.h"
77 #include "langhooks.h"
78 #include "pointer-set.h"
79 #include "toplev.h"
80 #include "flags.h"
81 #include "ggc.h"
82 #include "debug.h"
83 #include "target.h"
84 #include "diagnostic.h"
85 #include "params.h"
86 #include "intl.h"
87 #include "function.h"
88 #include "ipa-prop.h"
89 #include "tree-iterator.h"
90 #include "tree-dump.h"
91 #include "gimple-pretty-print.h"
92 #include "coverage.h"
93 #include "ipa-inline.h"
94 #include "ipa-utils.h"
95 #include "lto-streamer.h"
96 #include "except.h"
98 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
99 struct cgraph_edge *
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);
106 gcov_type freq;
108 /* We do not want to ignore loop nest after frequency drops to 0. */
109 if (!freq_scale)
110 freq_scale = 1;
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)
117 tree decl;
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);
125 else
127 new_edge = cgraph_create_indirect_edge (n, call_stmt,
128 e->indirect_info->ecf_flags,
129 count, freq);
130 *new_edge->indirect_info = *e->indirect_info;
133 else
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;
150 new_edge->speculative = e->speculative;
151 if (update_original)
153 e->count -= new_edge->count;
154 if (e->count < 0)
155 e->count = 0;
157 cgraph_call_edge_duplication_hooks (e, new_edge);
158 return new_edge;
162 /* Create node representing clone of N executed COUNT times. Decrease
163 the execution counts from original node too.
164 The new clone will have decl set to DECL that may or may not be the same
165 as decl of N.
167 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
168 function's profile to reflect the fact that part of execution is handled
169 by node.
170 When CALL_DUPLICATOIN_HOOK is true, the ipa passes are acknowledged about
171 the new clone. Otherwise the caller is responsible for doing so later.
173 If the new node is being inlined into another one, NEW_INLINED_TO should be
174 the outline function the new one is (even indirectly) inlined to. All hooks
175 will see this in node's global.inlined_to, when invoked. Can be NULL if the
176 node is not inlined. */
178 struct cgraph_node *
179 cgraph_clone_node (struct cgraph_node *n, tree decl, gcov_type count, int freq,
180 bool update_original,
181 vec<cgraph_edge_p> redirect_callers,
182 bool call_duplication_hook,
183 struct cgraph_node *new_inlined_to)
185 struct cgraph_node *new_node = cgraph_create_empty_node ();
186 struct cgraph_edge *e;
187 gcov_type count_scale;
188 unsigned i;
190 new_node->decl = decl;
191 symtab_register_node (new_node);
192 new_node->origin = n->origin;
193 new_node->lto_file_data = n->lto_file_data;
194 if (new_node->origin)
196 new_node->next_nested = new_node->origin->nested;
197 new_node->origin->nested = new_node;
199 new_node->analyzed = n->analyzed;
200 new_node->definition = n->definition;
201 new_node->local = n->local;
202 new_node->externally_visible = false;
203 new_node->local.local = true;
204 new_node->global = n->global;
205 new_node->global.inlined_to = new_inlined_to;
206 new_node->rtl = n->rtl;
207 new_node->count = count;
208 new_node->frequency = n->frequency;
209 new_node->clone = n->clone;
210 new_node->clone.tree_map = NULL;
211 new_node->tp_first_run = n->tp_first_run;
212 if (n->count)
214 if (new_node->count > n->count)
215 count_scale = REG_BR_PROB_BASE;
216 else
217 count_scale = GCOV_COMPUTE_SCALE (new_node->count, n->count);
219 else
220 count_scale = 0;
221 if (update_original)
223 n->count -= count;
224 if (n->count < 0)
225 n->count = 0;
228 FOR_EACH_VEC_ELT (redirect_callers, i, e)
230 /* Redirect calls to the old version node to point to its new
231 version. */
232 cgraph_redirect_edge_callee (e, new_node);
236 for (e = n->callees;e; e=e->next_callee)
237 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
238 count_scale, freq, update_original);
240 for (e = n->indirect_calls; e; e = e->next_callee)
241 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
242 count_scale, freq, update_original);
243 ipa_clone_references (new_node, &n->ref_list);
245 new_node->next_sibling_clone = n->clones;
246 if (n->clones)
247 n->clones->prev_sibling_clone = new_node;
248 n->clones = new_node;
249 new_node->clone_of = n;
251 if (call_duplication_hook)
252 cgraph_call_node_duplication_hooks (n, new_node);
253 return new_node;
256 /* Return a new assembler name for a clone of DECL with SUFFIX. */
258 static GTY(()) unsigned int clone_fn_id_num;
260 tree
261 clone_function_name (tree decl, const char *suffix)
263 tree name = DECL_ASSEMBLER_NAME (decl);
264 size_t len = IDENTIFIER_LENGTH (name);
265 char *tmp_name, *prefix;
267 prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
268 memcpy (prefix, IDENTIFIER_POINTER (name), len);
269 strcpy (prefix + len + 1, suffix);
270 #ifndef NO_DOT_IN_LABEL
271 prefix[len] = '.';
272 #elif !defined NO_DOLLAR_IN_LABEL
273 prefix[len] = '$';
274 #else
275 prefix[len] = '_';
276 #endif
277 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
278 return get_identifier (tmp_name);
281 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP and the
282 return value if SKIP_RETURN is true. */
284 static tree
285 build_function_type_skip_args (tree orig_type, bitmap args_to_skip,
286 bool skip_return)
288 tree new_type = NULL;
289 tree args, new_args = NULL, t;
290 tree new_reversed;
291 int i = 0;
293 for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
294 args = TREE_CHAIN (args), i++)
295 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
296 new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
298 new_reversed = nreverse (new_args);
299 if (args)
301 if (new_reversed)
302 TREE_CHAIN (new_args) = void_list_node;
303 else
304 new_reversed = void_list_node;
307 /* Use copy_node to preserve as much as possible from original type
308 (debug info, attribute lists etc.)
309 Exception is METHOD_TYPEs must have THIS argument.
310 When we are asked to remove it, we need to build new FUNCTION_TYPE
311 instead. */
312 if (TREE_CODE (orig_type) != METHOD_TYPE
313 || !args_to_skip
314 || !bitmap_bit_p (args_to_skip, 0))
316 new_type = build_distinct_type_copy (orig_type);
317 TYPE_ARG_TYPES (new_type) = new_reversed;
319 else
321 new_type
322 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
323 new_reversed));
324 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
327 if (skip_return)
328 TREE_TYPE (new_type) = void_type_node;
330 /* This is a new type, not a copy of an old type. Need to reassociate
331 variants. We can handle everything except the main variant lazily. */
332 t = TYPE_MAIN_VARIANT (orig_type);
333 if (t != orig_type)
335 t = build_function_type_skip_args (t, args_to_skip, skip_return);
336 TYPE_MAIN_VARIANT (new_type) = t;
337 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
338 TYPE_NEXT_VARIANT (t) = new_type;
340 else
342 TYPE_MAIN_VARIANT (new_type) = new_type;
343 TYPE_NEXT_VARIANT (new_type) = NULL;
346 return new_type;
349 /* Build variant of function decl ORIG_DECL skipping ARGS_TO_SKIP and the
350 return value if SKIP_RETURN is true.
352 Arguments from DECL_ARGUMENTS list can't be removed now, since they are
353 linked by TREE_CHAIN directly. The caller is responsible for eliminating
354 them when they are being duplicated (i.e. copy_arguments_for_versioning). */
356 static tree
357 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip,
358 bool skip_return)
360 tree new_decl = copy_node (orig_decl);
361 tree new_type;
363 new_type = TREE_TYPE (orig_decl);
364 if (prototype_p (new_type)
365 || (skip_return && !VOID_TYPE_P (TREE_TYPE (new_type))))
366 new_type
367 = build_function_type_skip_args (new_type, args_to_skip, skip_return);
368 TREE_TYPE (new_decl) = new_type;
370 /* For declarations setting DECL_VINDEX (i.e. methods)
371 we expect first argument to be THIS pointer. */
372 if (args_to_skip && bitmap_bit_p (args_to_skip, 0))
373 DECL_VINDEX (new_decl) = NULL_TREE;
375 /* When signature changes, we need to clear builtin info. */
376 if (DECL_BUILT_IN (new_decl)
377 && args_to_skip
378 && !bitmap_empty_p (args_to_skip))
380 DECL_BUILT_IN_CLASS (new_decl) = NOT_BUILT_IN;
381 DECL_FUNCTION_CODE (new_decl) = (enum built_in_function) 0;
383 return new_decl;
386 /* Create callgraph node clone with new declaration. The actual body will
387 be copied later at compilation stage.
389 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
390 bitmap interface.
392 struct cgraph_node *
393 cgraph_create_virtual_clone (struct cgraph_node *old_node,
394 vec<cgraph_edge_p> redirect_callers,
395 vec<ipa_replace_map_p, va_gc> *tree_map,
396 bitmap args_to_skip,
397 const char * suffix)
399 tree old_decl = old_node->decl;
400 struct cgraph_node *new_node = NULL;
401 tree new_decl;
402 size_t len, i;
403 struct ipa_replace_map *map;
404 char *name;
406 if (!in_lto_p)
407 gcc_checking_assert (tree_versionable_function_p (old_decl));
409 gcc_assert (old_node->local.can_change_signature || !args_to_skip);
411 /* Make a new FUNCTION_DECL tree node */
412 if (!args_to_skip)
413 new_decl = copy_node (old_decl);
414 else
415 new_decl = build_function_decl_skip_args (old_decl, args_to_skip, false);
417 /* These pointers represent function body and will be populated only when clone
418 is materialized. */
419 gcc_assert (new_decl != old_decl);
420 DECL_STRUCT_FUNCTION (new_decl) = NULL;
421 DECL_ARGUMENTS (new_decl) = NULL;
422 DECL_INITIAL (new_decl) = NULL;
423 DECL_RESULT (new_decl) = NULL;
424 /* We can not do DECL_RESULT (new_decl) = NULL; here because of LTO partitioning
425 sometimes storing only clone decl instead of original. */
427 /* Generate a new name for the new version. */
428 len = IDENTIFIER_LENGTH (DECL_NAME (old_decl));
429 name = XALLOCAVEC (char, len + strlen (suffix) + 2);
430 memcpy (name, IDENTIFIER_POINTER (DECL_NAME (old_decl)), len);
431 strcpy (name + len + 1, suffix);
432 name[len] = '.';
433 DECL_NAME (new_decl) = get_identifier (name);
434 SET_DECL_ASSEMBLER_NAME (new_decl, clone_function_name (old_decl, suffix));
435 SET_DECL_RTL (new_decl, NULL);
437 new_node = cgraph_clone_node (old_node, new_decl, old_node->count,
438 CGRAPH_FREQ_BASE, false,
439 redirect_callers, false, NULL);
440 /* Update the properties.
441 Make clone visible only within this translation unit. Make sure
442 that is not weak also.
443 ??? We cannot use COMDAT linkage because there is no
444 ABI support for this. */
445 DECL_EXTERNAL (new_node->decl) = 0;
446 if (DECL_ONE_ONLY (old_decl))
447 DECL_SECTION_NAME (new_node->decl) = NULL;
448 DECL_COMDAT_GROUP (new_node->decl) = 0;
449 TREE_PUBLIC (new_node->decl) = 0;
450 DECL_COMDAT (new_node->decl) = 0;
451 DECL_WEAK (new_node->decl) = 0;
452 DECL_VIRTUAL_P (new_node->decl) = 0;
453 DECL_STATIC_CONSTRUCTOR (new_node->decl) = 0;
454 DECL_STATIC_DESTRUCTOR (new_node->decl) = 0;
455 new_node->clone.tree_map = tree_map;
456 new_node->clone.args_to_skip = args_to_skip;
458 /* Clones of global symbols or symbols with unique names are unique. */
459 if ((TREE_PUBLIC (old_decl)
460 && !DECL_EXTERNAL (old_decl)
461 && !DECL_WEAK (old_decl)
462 && !DECL_COMDAT (old_decl))
463 || in_lto_p)
464 new_node->unique_name = true;
465 FOR_EACH_VEC_SAFE_ELT (tree_map, i, map)
466 ipa_maybe_record_reference (new_node, map->new_tree,
467 IPA_REF_ADDR, NULL);
468 if (!args_to_skip)
469 new_node->clone.combined_args_to_skip = old_node->clone.combined_args_to_skip;
470 else if (old_node->clone.combined_args_to_skip)
472 int newi = 0, oldi = 0;
473 tree arg;
474 bitmap new_args_to_skip = BITMAP_GGC_ALLOC ();
475 struct cgraph_node *orig_node;
476 for (orig_node = old_node; orig_node->clone_of; orig_node = orig_node->clone_of)
478 for (arg = DECL_ARGUMENTS (orig_node->decl);
479 arg; arg = DECL_CHAIN (arg), oldi++)
481 if (bitmap_bit_p (old_node->clone.combined_args_to_skip, oldi))
483 bitmap_set_bit (new_args_to_skip, oldi);
484 continue;
486 if (bitmap_bit_p (args_to_skip, newi))
487 bitmap_set_bit (new_args_to_skip, oldi);
488 newi++;
490 new_node->clone.combined_args_to_skip = new_args_to_skip;
492 else
493 new_node->clone.combined_args_to_skip = args_to_skip;
494 new_node->externally_visible = 0;
495 new_node->local.local = 1;
496 new_node->lowered = true;
498 cgraph_call_node_duplication_hooks (old_node, new_node);
501 return new_node;
504 /* NODE is being removed from symbol table; see if its entry can be replaced by
505 other inline clone. */
506 struct cgraph_node *
507 cgraph_find_replacement_node (struct cgraph_node *node)
509 struct cgraph_node *next_inline_clone, *replacement;
511 for (next_inline_clone = node->clones;
512 next_inline_clone
513 && next_inline_clone->decl != node->decl;
514 next_inline_clone = next_inline_clone->next_sibling_clone)
517 /* If there is inline clone of the node being removed, we need
518 to put it into the position of removed node and reorganize all
519 other clones to be based on it. */
520 if (next_inline_clone)
522 struct cgraph_node *n;
523 struct cgraph_node *new_clones;
525 replacement = next_inline_clone;
527 /* Unlink inline clone from the list of clones of removed node. */
528 if (next_inline_clone->next_sibling_clone)
529 next_inline_clone->next_sibling_clone->prev_sibling_clone
530 = next_inline_clone->prev_sibling_clone;
531 if (next_inline_clone->prev_sibling_clone)
533 gcc_assert (node->clones != next_inline_clone);
534 next_inline_clone->prev_sibling_clone->next_sibling_clone
535 = next_inline_clone->next_sibling_clone;
537 else
539 gcc_assert (node->clones == next_inline_clone);
540 node->clones = next_inline_clone->next_sibling_clone;
543 new_clones = node->clones;
544 node->clones = NULL;
546 /* Copy clone info. */
547 next_inline_clone->clone = node->clone;
549 /* Now place it into clone tree at same level at NODE. */
550 next_inline_clone->clone_of = node->clone_of;
551 next_inline_clone->prev_sibling_clone = NULL;
552 next_inline_clone->next_sibling_clone = NULL;
553 if (node->clone_of)
555 if (node->clone_of->clones)
556 node->clone_of->clones->prev_sibling_clone = next_inline_clone;
557 next_inline_clone->next_sibling_clone = node->clone_of->clones;
558 node->clone_of->clones = next_inline_clone;
561 /* Merge the clone list. */
562 if (new_clones)
564 if (!next_inline_clone->clones)
565 next_inline_clone->clones = new_clones;
566 else
568 n = next_inline_clone->clones;
569 while (n->next_sibling_clone)
570 n = n->next_sibling_clone;
571 n->next_sibling_clone = new_clones;
572 new_clones->prev_sibling_clone = n;
576 /* Update clone_of pointers. */
577 n = new_clones;
578 while (n)
580 n->clone_of = next_inline_clone;
581 n = n->next_sibling_clone;
583 return replacement;
585 else
586 return NULL;
589 /* Like cgraph_set_call_stmt but walk the clone tree and update all
590 clones sharing the same function body.
591 When WHOLE_SPECULATIVE_EDGES is true, all three components of
592 speculative edge gets updated. Otherwise we update only direct
593 call. */
595 void
596 cgraph_set_call_stmt_including_clones (struct cgraph_node *orig,
597 gimple old_stmt, gimple new_stmt,
598 bool update_speculative)
600 struct cgraph_node *node;
601 struct cgraph_edge *edge = cgraph_edge (orig, old_stmt);
603 if (edge)
604 cgraph_set_call_stmt (edge, new_stmt, update_speculative);
606 node = orig->clones;
607 if (node)
608 while (node != orig)
610 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
611 if (edge)
613 cgraph_set_call_stmt (edge, new_stmt, update_speculative);
614 /* If UPDATE_SPECULATIVE is false, it means that we are turning
615 speculative call into a real code sequence. Update the
616 callgraph edges. */
617 if (edge->speculative && !update_speculative)
619 struct cgraph_edge *direct, *indirect;
620 struct ipa_ref *ref;
622 gcc_assert (!edge->indirect_unknown_callee);
623 cgraph_speculative_call_info (edge, direct, indirect, ref);
624 direct->speculative = false;
625 indirect->speculative = false;
626 ref->speculative = false;
629 if (node->clones)
630 node = node->clones;
631 else if (node->next_sibling_clone)
632 node = node->next_sibling_clone;
633 else
635 while (node != orig && !node->next_sibling_clone)
636 node = node->clone_of;
637 if (node != orig)
638 node = node->next_sibling_clone;
643 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
644 same function body. If clones already have edge for OLD_STMT; only
645 update the edge same way as cgraph_set_call_stmt_including_clones does.
647 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
648 frequencies of the clones. */
650 void
651 cgraph_create_edge_including_clones (struct cgraph_node *orig,
652 struct cgraph_node *callee,
653 gimple old_stmt,
654 gimple stmt, gcov_type count,
655 int freq,
656 cgraph_inline_failed_t reason)
658 struct cgraph_node *node;
659 struct cgraph_edge *edge;
661 if (!cgraph_edge (orig, stmt))
663 edge = cgraph_create_edge (orig, callee, stmt, count, freq);
664 edge->inline_failed = reason;
667 node = orig->clones;
668 if (node)
669 while (node != orig)
671 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
673 /* It is possible that clones already contain the edge while
674 master didn't. Either we promoted indirect call into direct
675 call in the clone or we are processing clones of unreachable
676 master where edges has been removed. */
677 if (edge)
678 cgraph_set_call_stmt (edge, stmt);
679 else if (!cgraph_edge (node, stmt))
681 edge = cgraph_create_edge (node, callee, stmt, count,
682 freq);
683 edge->inline_failed = reason;
686 if (node->clones)
687 node = node->clones;
688 else if (node->next_sibling_clone)
689 node = node->next_sibling_clone;
690 else
692 while (node != orig && !node->next_sibling_clone)
693 node = node->clone_of;
694 if (node != orig)
695 node = node->next_sibling_clone;
700 /* Remove the node from cgraph and all inline clones inlined into it.
701 Skip however removal of FORBIDDEN_NODE and return true if it needs to be
702 removed. This allows to call the function from outer loop walking clone
703 tree. */
705 bool
706 cgraph_remove_node_and_inline_clones (struct cgraph_node *node, struct cgraph_node *forbidden_node)
708 struct cgraph_edge *e, *next;
709 bool found = false;
711 if (node == forbidden_node)
713 cgraph_remove_edge (node->callers);
714 return true;
716 for (e = node->callees; e; e = next)
718 next = e->next_callee;
719 if (!e->inline_failed)
720 found |= cgraph_remove_node_and_inline_clones (e->callee, forbidden_node);
722 cgraph_remove_node (node);
723 return found;
726 /* The edges representing the callers of the NEW_VERSION node were
727 fixed by cgraph_function_versioning (), now the call_expr in their
728 respective tree code should be updated to call the NEW_VERSION. */
730 static void
731 update_call_expr (struct cgraph_node *new_version)
733 struct cgraph_edge *e;
735 gcc_assert (new_version);
737 /* Update the call expr on the edges to call the new version. */
738 for (e = new_version->callers; e; e = e->next_caller)
740 struct function *inner_function = DECL_STRUCT_FUNCTION (e->caller->decl);
741 gimple_call_set_fndecl (e->call_stmt, new_version->decl);
742 maybe_clean_eh_stmt_fn (inner_function, e->call_stmt);
747 /* Create a new cgraph node which is the new version of
748 OLD_VERSION node. REDIRECT_CALLERS holds the callers
749 edges which should be redirected to point to
750 NEW_VERSION. ALL the callees edges of OLD_VERSION
751 are cloned to the new version node. Return the new
752 version node.
754 If non-NULL BLOCK_TO_COPY determine what basic blocks
755 was copied to prevent duplications of calls that are dead
756 in the clone. */
758 struct cgraph_node *
759 cgraph_copy_node_for_versioning (struct cgraph_node *old_version,
760 tree new_decl,
761 vec<cgraph_edge_p> redirect_callers,
762 bitmap bbs_to_copy)
764 struct cgraph_node *new_version;
765 struct cgraph_edge *e;
766 unsigned i;
768 gcc_assert (old_version);
770 new_version = cgraph_create_node (new_decl);
772 new_version->analyzed = old_version->analyzed;
773 new_version->definition = old_version->definition;
774 new_version->local = old_version->local;
775 new_version->externally_visible = false;
776 new_version->local.local = new_version->definition;
777 new_version->global = old_version->global;
778 new_version->rtl = old_version->rtl;
779 new_version->count = old_version->count;
781 for (e = old_version->callees; e; e=e->next_callee)
782 if (!bbs_to_copy
783 || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
784 cgraph_clone_edge (e, new_version, e->call_stmt,
785 e->lto_stmt_uid, REG_BR_PROB_BASE,
786 CGRAPH_FREQ_BASE,
787 true);
788 for (e = old_version->indirect_calls; e; e=e->next_callee)
789 if (!bbs_to_copy
790 || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
791 cgraph_clone_edge (e, new_version, e->call_stmt,
792 e->lto_stmt_uid, REG_BR_PROB_BASE,
793 CGRAPH_FREQ_BASE,
794 true);
795 FOR_EACH_VEC_ELT (redirect_callers, i, e)
797 /* Redirect calls to the old version node to point to its new
798 version. */
799 cgraph_redirect_edge_callee (e, new_version);
802 cgraph_call_node_duplication_hooks (old_version, new_version);
804 return new_version;
807 /* Perform function versioning.
808 Function versioning includes copying of the tree and
809 a callgraph update (creating a new cgraph node and updating
810 its callees and callers).
812 REDIRECT_CALLERS varray includes the edges to be redirected
813 to the new version.
815 TREE_MAP is a mapping of tree nodes we want to replace with
816 new ones (according to results of prior analysis).
817 OLD_VERSION_NODE is the node that is versioned.
819 If non-NULL ARGS_TO_SKIP determine function parameters to remove
820 from new version.
821 If SKIP_RETURN is true, the new version will return void.
822 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
823 If non_NULL NEW_ENTRY determine new entry BB of the clone.
825 Return the new version's cgraph node. */
827 struct cgraph_node *
828 cgraph_function_versioning (struct cgraph_node *old_version_node,
829 vec<cgraph_edge_p> redirect_callers,
830 vec<ipa_replace_map_p, va_gc> *tree_map,
831 bitmap args_to_skip,
832 bool skip_return,
833 bitmap bbs_to_copy,
834 basic_block new_entry_block,
835 const char *clone_name)
837 tree old_decl = old_version_node->decl;
838 struct cgraph_node *new_version_node = NULL;
839 tree new_decl;
841 if (!tree_versionable_function_p (old_decl))
842 return NULL;
844 gcc_assert (old_version_node->local.can_change_signature || !args_to_skip);
846 /* Make a new FUNCTION_DECL tree node for the new version. */
847 if (!args_to_skip && !skip_return)
848 new_decl = copy_node (old_decl);
849 else
850 new_decl
851 = build_function_decl_skip_args (old_decl, args_to_skip, skip_return);
853 /* Generate a new name for the new version. */
854 DECL_NAME (new_decl) = clone_function_name (old_decl, clone_name);
855 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
856 SET_DECL_RTL (new_decl, NULL);
858 /* When the old decl was a con-/destructor make sure the clone isn't. */
859 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
860 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
862 /* Create the new version's call-graph node.
863 and update the edges of the new node. */
864 new_version_node =
865 cgraph_copy_node_for_versioning (old_version_node, new_decl,
866 redirect_callers, bbs_to_copy);
868 /* Copy the OLD_VERSION_NODE function tree to the new version. */
869 tree_function_versioning (old_decl, new_decl, tree_map, false, args_to_skip,
870 skip_return, bbs_to_copy, new_entry_block);
872 /* Update the new version's properties.
873 Make The new version visible only within this translation unit. Make sure
874 that is not weak also.
875 ??? We cannot use COMDAT linkage because there is no
876 ABI support for this. */
877 symtab_make_decl_local (new_version_node->decl);
878 DECL_VIRTUAL_P (new_version_node->decl) = 0;
879 new_version_node->externally_visible = 0;
880 new_version_node->local.local = 1;
881 new_version_node->lowered = true;
882 /* Clones of global symbols or symbols with unique names are unique. */
883 if ((TREE_PUBLIC (old_decl)
884 && !DECL_EXTERNAL (old_decl)
885 && !DECL_WEAK (old_decl)
886 && !DECL_COMDAT (old_decl))
887 || in_lto_p)
888 new_version_node->unique_name = true;
890 /* Update the call_expr on the edges to call the new version node. */
891 update_call_expr (new_version_node);
893 cgraph_call_function_insertion_hooks (new_version_node);
894 return new_version_node;
897 /* Given virtual clone, turn it into actual clone. */
899 static void
900 cgraph_materialize_clone (struct cgraph_node *node)
902 bitmap_obstack_initialize (NULL);
903 node->former_clone_of = node->clone_of->decl;
904 if (node->clone_of->former_clone_of)
905 node->former_clone_of = node->clone_of->former_clone_of;
906 /* Copy the OLD_VERSION_NODE function tree to the new version. */
907 tree_function_versioning (node->clone_of->decl, node->decl,
908 node->clone.tree_map, true,
909 node->clone.args_to_skip, false,
910 NULL, NULL);
911 if (cgraph_dump_file)
913 dump_function_to_file (node->clone_of->decl, cgraph_dump_file, dump_flags);
914 dump_function_to_file (node->decl, cgraph_dump_file, dump_flags);
917 /* Function is no longer clone. */
918 if (node->next_sibling_clone)
919 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
920 if (node->prev_sibling_clone)
921 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
922 else
923 node->clone_of->clones = node->next_sibling_clone;
924 node->next_sibling_clone = NULL;
925 node->prev_sibling_clone = NULL;
926 if (!node->clone_of->analyzed && !node->clone_of->clones)
928 cgraph_release_function_body (node->clone_of);
929 cgraph_node_remove_callees (node->clone_of);
930 ipa_remove_all_references (&node->clone_of->ref_list);
932 node->clone_of = NULL;
933 bitmap_obstack_release (NULL);
936 /* Once all functions from compilation unit are in memory, produce all clones
937 and update all calls. We might also do this on demand if we don't want to
938 bring all functions to memory prior compilation, but current WHOPR
939 implementation does that and it is is bit easier to keep everything right in
940 this order. */
942 void
943 cgraph_materialize_all_clones (void)
945 struct cgraph_node *node;
946 bool stabilized = false;
949 if (cgraph_dump_file)
950 fprintf (cgraph_dump_file, "Materializing clones\n");
951 #ifdef ENABLE_CHECKING
952 verify_cgraph ();
953 #endif
955 /* We can also do topological order, but number of iterations should be
956 bounded by number of IPA passes since single IPA pass is probably not
957 going to create clones of clones it created itself. */
958 while (!stabilized)
960 stabilized = true;
961 FOR_EACH_FUNCTION (node)
963 if (node->clone_of && node->decl != node->clone_of->decl
964 && !gimple_has_body_p (node->decl))
966 if (!node->clone_of->clone_of)
967 cgraph_get_body (node->clone_of);
968 if (gimple_has_body_p (node->clone_of->decl))
970 if (cgraph_dump_file)
972 fprintf (cgraph_dump_file, "cloning %s to %s\n",
973 xstrdup (cgraph_node_name (node->clone_of)),
974 xstrdup (cgraph_node_name (node)));
975 if (node->clone.tree_map)
977 unsigned int i;
978 fprintf (cgraph_dump_file, " replace map: ");
979 for (i = 0;
980 i < vec_safe_length (node->clone.tree_map);
981 i++)
983 struct ipa_replace_map *replace_info;
984 replace_info = (*node->clone.tree_map)[i];
985 print_generic_expr (cgraph_dump_file, replace_info->old_tree, 0);
986 fprintf (cgraph_dump_file, " -> ");
987 print_generic_expr (cgraph_dump_file, replace_info->new_tree, 0);
988 fprintf (cgraph_dump_file, "%s%s;",
989 replace_info->replace_p ? "(replace)":"",
990 replace_info->ref_p ? "(ref)":"");
992 fprintf (cgraph_dump_file, "\n");
994 if (node->clone.args_to_skip)
996 fprintf (cgraph_dump_file, " args_to_skip: ");
997 dump_bitmap (cgraph_dump_file, node->clone.args_to_skip);
999 if (node->clone.args_to_skip)
1001 fprintf (cgraph_dump_file, " combined_args_to_skip:");
1002 dump_bitmap (cgraph_dump_file, node->clone.combined_args_to_skip);
1005 cgraph_materialize_clone (node);
1006 stabilized = false;
1011 FOR_EACH_FUNCTION (node)
1012 if (!node->analyzed && node->callees)
1014 cgraph_node_remove_callees (node);
1015 ipa_remove_all_references (&node->ref_list);
1017 else
1018 ipa_clear_stmts_in_references (node);
1019 if (cgraph_dump_file)
1020 fprintf (cgraph_dump_file, "Materialization Call site updates done.\n");
1021 #ifdef ENABLE_CHECKING
1022 verify_cgraph ();
1023 #endif
1024 symtab_remove_unreachable_nodes (false, cgraph_dump_file);
1027 #include "gt-cgraphclones.h"