]> gcc.gnu.org Git - gcc.git/blame - gcc/cgraphclones.c
re PR target/59216 (ARM negdi*extendsidi regression)
[gcc.git] / gcc / cgraphclones.c
CommitLineData
564fe867 1/* Callgraph clones
d1e082c2 2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
564fe867
JH
3 Contributed by Jan Hubicka
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
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.
24
25 To allow whole program optimization without actual presence of function
26 bodies, an additional infrastructure is provided for so-called virtual
27 clones
28
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).
32
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.
38
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.
42
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.
50
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.
54
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.
61
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. */
66
67#include "config.h"
68#include "system.h"
69#include "coretypes.h"
70#include "tm.h"
d8a2d370 71#include "rtl.h"
564fe867 72#include "tree.h"
d8a2d370
DN
73#include "stringpool.h"
74#include "function.h"
75#include "emit-rtl.h"
8e9055ae 76#include "gimple.h"
442b4905
AM
77#include "bitmap.h"
78#include "tree-cfg.h"
564fe867
JH
79#include "tree-inline.h"
80#include "langhooks.h"
81#include "pointer-set.h"
82#include "toplev.h"
83#include "flags.h"
84#include "ggc.h"
85#include "debug.h"
86#include "target.h"
564fe867 87#include "diagnostic.h"
564fe867 88#include "params.h"
564fe867
JH
89#include "intl.h"
90#include "function.h"
91#include "ipa-prop.h"
564fe867 92#include "tree-iterator.h"
564fe867
JH
93#include "tree-dump.h"
94#include "gimple-pretty-print.h"
564fe867 95#include "coverage.h"
564fe867
JH
96#include "ipa-inline.h"
97#include "ipa-utils.h"
98#include "lto-streamer.h"
99#include "except.h"
100
101/* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
102struct cgraph_edge *
103cgraph_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)
106{
107 struct cgraph_edge *new_edge;
8b47039c 108 gcov_type count = apply_probability (e->count, count_scale);
564fe867
JH
109 gcov_type freq;
110
111 /* We do not want to ignore loop nest after frequency drops to 0. */
112 if (!freq_scale)
113 freq_scale = 1;
114 freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
115 if (freq > CGRAPH_FREQ_MAX)
116 freq = CGRAPH_FREQ_MAX;
117
118 if (e->indirect_unknown_callee)
119 {
120 tree decl;
121
122 if (call_stmt && (decl = gimple_call_fndecl (call_stmt)))
123 {
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);
127 }
128 else
129 {
130 new_edge = cgraph_create_indirect_edge (n, call_stmt,
131 e->indirect_info->ecf_flags,
132 count, freq);
133 *new_edge->indirect_info = *e->indirect_info;
134 }
135 }
136 else
137 {
138 new_edge = cgraph_create_edge (n, e->callee, call_stmt, count, freq);
139 if (e->indirect_info)
140 {
141 new_edge->indirect_info
142 = ggc_alloc_cleared_cgraph_indirect_call_info ();
143 *new_edge->indirect_info = *e->indirect_info;
144 }
145 }
146
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;
042ae7d2 153 new_edge->speculative = e->speculative;
564fe867
JH
154 if (update_original)
155 {
156 e->count -= new_edge->count;
157 if (e->count < 0)
158 e->count = 0;
159 }
160 cgraph_call_edge_duplication_hooks (e, new_edge);
161 return new_edge;
162}
163
164
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
168 as decl of N.
169
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
172 by node.
173 When CALL_DUPLICATOIN_HOOK is true, the ipa passes are acknowledged about
44a60244
MJ
174 the new clone. Otherwise the caller is responsible for doing so later.
175
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. */
564fe867
JH
180
181struct cgraph_node *
182cgraph_clone_node (struct cgraph_node *n, tree decl, gcov_type count, int freq,
183 bool update_original,
9771b263 184 vec<cgraph_edge_p> redirect_callers,
44a60244
MJ
185 bool call_duplication_hook,
186 struct cgraph_node *new_inlined_to)
564fe867
JH
187{
188 struct cgraph_node *new_node = cgraph_create_empty_node ();
189 struct cgraph_edge *e;
190 gcov_type count_scale;
191 unsigned i;
192
67348ccc
DM
193 new_node->decl = decl;
194 symtab_register_node (new_node);
564fe867 195 new_node->origin = n->origin;
67348ccc 196 new_node->lto_file_data = n->lto_file_data;
564fe867
JH
197 if (new_node->origin)
198 {
199 new_node->next_nested = new_node->origin->nested;
200 new_node->origin->nested = new_node;
201 }
67348ccc
DM
202 new_node->analyzed = n->analyzed;
203 new_node->definition = n->definition;
564fe867 204 new_node->local = n->local;
67348ccc 205 new_node->externally_visible = false;
564fe867
JH
206 new_node->local.local = true;
207 new_node->global = n->global;
44a60244 208 new_node->global.inlined_to = new_inlined_to;
564fe867
JH
209 new_node->rtl = n->rtl;
210 new_node->count = count;
211 new_node->frequency = n->frequency;
212 new_node->clone = n->clone;
9771b263 213 new_node->clone.tree_map = NULL;
86ce5d2f 214 new_node->tp_first_run = n->tp_first_run;
564fe867
JH
215 if (n->count)
216 {
217 if (new_node->count > n->count)
218 count_scale = REG_BR_PROB_BASE;
219 else
8b47039c 220 count_scale = GCOV_COMPUTE_SCALE (new_node->count, n->count);
564fe867
JH
221 }
222 else
223 count_scale = 0;
224 if (update_original)
225 {
226 n->count -= count;
227 if (n->count < 0)
228 n->count = 0;
229 }
230
9771b263 231 FOR_EACH_VEC_ELT (redirect_callers, i, e)
564fe867
JH
232 {
233 /* Redirect calls to the old version node to point to its new
234 version. */
235 cgraph_redirect_edge_callee (e, new_node);
236 }
237
238
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);
242
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);
67348ccc 246 ipa_clone_references (new_node, &n->ref_list);
564fe867
JH
247
248 new_node->next_sibling_clone = n->clones;
249 if (n->clones)
250 n->clones->prev_sibling_clone = new_node;
251 n->clones = new_node;
252 new_node->clone_of = n;
253
254 if (call_duplication_hook)
255 cgraph_call_node_duplication_hooks (n, new_node);
256 return new_node;
257}
258
440a5082 259/* Return a new assembler name for a clone of DECL with SUFFIX. */
564fe867
JH
260
261static GTY(()) unsigned int clone_fn_id_num;
262
263tree
264clone_function_name (tree decl, const char *suffix)
265{
266 tree name = DECL_ASSEMBLER_NAME (decl);
267 size_t len = IDENTIFIER_LENGTH (name);
268 char *tmp_name, *prefix;
269
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
274 prefix[len] = '.';
275#elif !defined NO_DOLLAR_IN_LABEL
276 prefix[len] = '$';
277#else
278 prefix[len] = '_';
279#endif
280 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
281 return get_identifier (tmp_name);
282}
283
862d0b35
DN
284/* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP and the
285 return value if SKIP_RETURN is true. */
286
287static tree
288build_function_type_skip_args (tree orig_type, bitmap args_to_skip,
289 bool skip_return)
290{
291 tree new_type = NULL;
292 tree args, new_args = NULL, t;
293 tree new_reversed;
294 int i = 0;
295
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);
300
301 new_reversed = nreverse (new_args);
302 if (args)
303 {
304 if (new_reversed)
305 TREE_CHAIN (new_args) = void_list_node;
306 else
307 new_reversed = void_list_node;
308 }
309
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
314 instead. */
315 if (TREE_CODE (orig_type) != METHOD_TYPE
316 || !args_to_skip
317 || !bitmap_bit_p (args_to_skip, 0))
318 {
319 new_type = build_distinct_type_copy (orig_type);
320 TYPE_ARG_TYPES (new_type) = new_reversed;
321 }
322 else
323 {
324 new_type
325 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
326 new_reversed));
327 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
328 }
329
330 if (skip_return)
331 TREE_TYPE (new_type) = void_type_node;
332
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);
336 if (t != orig_type)
337 {
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;
342 }
343 else
344 {
345 TYPE_MAIN_VARIANT (new_type) = new_type;
346 TYPE_NEXT_VARIANT (new_type) = NULL;
347 }
348
349 return new_type;
350}
351
352/* Build variant of function decl ORIG_DECL skipping ARGS_TO_SKIP and the
353 return value if SKIP_RETURN is true.
354
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). */
358
359static tree
360build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip,
361 bool skip_return)
362{
363 tree new_decl = copy_node (orig_decl);
364 tree new_type;
365
366 new_type = TREE_TYPE (orig_decl);
367 if (prototype_p (new_type)
368 || (skip_return && !VOID_TYPE_P (TREE_TYPE (new_type))))
369 new_type
370 = build_function_type_skip_args (new_type, args_to_skip, skip_return);
371 TREE_TYPE (new_decl) = new_type;
372
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;
377
378 /* When signature changes, we need to clear builtin info. */
379 if (DECL_BUILT_IN (new_decl)
380 && args_to_skip
381 && !bitmap_empty_p (args_to_skip))
382 {
383 DECL_BUILT_IN_CLASS (new_decl) = NOT_BUILT_IN;
384 DECL_FUNCTION_CODE (new_decl) = (enum built_in_function) 0;
385 }
386 return new_decl;
387}
388
564fe867
JH
389/* Create callgraph node clone with new declaration. The actual body will
390 be copied later at compilation stage.
391
392 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
393 bitmap interface.
394 */
395struct cgraph_node *
396cgraph_create_virtual_clone (struct cgraph_node *old_node,
9771b263
DN
397 vec<cgraph_edge_p> redirect_callers,
398 vec<ipa_replace_map_p, va_gc> *tree_map,
564fe867
JH
399 bitmap args_to_skip,
400 const char * suffix)
401{
67348ccc 402 tree old_decl = old_node->decl;
564fe867
JH
403 struct cgraph_node *new_node = NULL;
404 tree new_decl;
440a5082 405 size_t len, i;
564fe867 406 struct ipa_replace_map *map;
440a5082 407 char *name;
564fe867 408
a2e2a668 409 if (!in_lto_p)
564fe867
JH
410 gcc_checking_assert (tree_versionable_function_p (old_decl));
411
412 gcc_assert (old_node->local.can_change_signature || !args_to_skip);
413
414 /* Make a new FUNCTION_DECL tree node */
415 if (!args_to_skip)
416 new_decl = copy_node (old_decl);
417 else
418 new_decl = build_function_decl_skip_args (old_decl, args_to_skip, false);
49bde175
JH
419
420 /* These pointers represent function body and will be populated only when clone
421 is materialized. */
422 gcc_assert (new_decl != old_decl);
564fe867 423 DECL_STRUCT_FUNCTION (new_decl) = NULL;
49bde175
JH
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. */
564fe867
JH
429
430 /* Generate a new name for the new version. */
440a5082
EB
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);
435 name[len] = '.';
436 DECL_NAME (new_decl) = get_identifier (name);
437 SET_DECL_ASSEMBLER_NAME (new_decl, clone_function_name (old_decl, suffix));
564fe867
JH
438 SET_DECL_RTL (new_decl, NULL);
439
440 new_node = cgraph_clone_node (old_node, new_decl, old_node->count,
441 CGRAPH_FREQ_BASE, false,
44a60244 442 redirect_callers, false, NULL);
564fe867
JH
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. */
67348ccc 448 DECL_EXTERNAL (new_node->decl) = 0;
564fe867 449 if (DECL_ONE_ONLY (old_decl))
67348ccc
DM
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;
564fe867
JH
458 new_node->clone.tree_map = tree_map;
459 new_node->clone.args_to_skip = args_to_skip;
702d8703
JH
460
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))
466 || in_lto_p)
67348ccc 467 new_node->unique_name = true;
9771b263 468 FOR_EACH_VEC_SAFE_ELT (tree_map, i, map)
67348ccc 469 ipa_maybe_record_reference (new_node, map->new_tree,
79ee9826 470 IPA_REF_ADDR, NULL);
564fe867
JH
471 if (!args_to_skip)
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)
474 {
475 int newi = 0, oldi = 0;
476 tree arg;
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)
480 ;
67348ccc 481 for (arg = DECL_ARGUMENTS (orig_node->decl);
564fe867
JH
482 arg; arg = DECL_CHAIN (arg), oldi++)
483 {
484 if (bitmap_bit_p (old_node->clone.combined_args_to_skip, oldi))
485 {
486 bitmap_set_bit (new_args_to_skip, oldi);
487 continue;
488 }
489 if (bitmap_bit_p (args_to_skip, newi))
490 bitmap_set_bit (new_args_to_skip, oldi);
491 newi++;
492 }
493 new_node->clone.combined_args_to_skip = new_args_to_skip;
494 }
495 else
496 new_node->clone.combined_args_to_skip = args_to_skip;
67348ccc 497 new_node->externally_visible = 0;
564fe867
JH
498 new_node->local.local = 1;
499 new_node->lowered = true;
500
501 cgraph_call_node_duplication_hooks (old_node, new_node);
502
503
504 return new_node;
505}
506
507/* NODE is being removed from symbol table; see if its entry can be replaced by
508 other inline clone. */
509struct cgraph_node *
510cgraph_find_replacement_node (struct cgraph_node *node)
511{
512 struct cgraph_node *next_inline_clone, *replacement;
513
514 for (next_inline_clone = node->clones;
515 next_inline_clone
67348ccc 516 && next_inline_clone->decl != node->decl;
564fe867
JH
517 next_inline_clone = next_inline_clone->next_sibling_clone)
518 ;
519
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)
524 {
525 struct cgraph_node *n;
526 struct cgraph_node *new_clones;
527
528 replacement = next_inline_clone;
529
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)
535 {
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;
539 }
540 else
541 {
542 gcc_assert (node->clones == next_inline_clone);
543 node->clones = next_inline_clone->next_sibling_clone;
544 }
545
546 new_clones = node->clones;
547 node->clones = NULL;
548
549 /* Copy clone info. */
550 next_inline_clone->clone = node->clone;
551
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;
556 if (node->clone_of)
557 {
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;
562 }
563
564 /* Merge the clone list. */
565 if (new_clones)
566 {
567 if (!next_inline_clone->clones)
568 next_inline_clone->clones = new_clones;
569 else
570 {
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;
576 }
577 }
578
579 /* Update clone_of pointers. */
580 n = new_clones;
581 while (n)
582 {
583 n->clone_of = next_inline_clone;
584 n = n->next_sibling_clone;
585 }
586 return replacement;
587 }
588 else
589 return NULL;
590}
591
592/* Like cgraph_set_call_stmt but walk the clone tree and update all
042ae7d2
JH
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
596 call. */
564fe867
JH
597
598void
599cgraph_set_call_stmt_including_clones (struct cgraph_node *orig,
042ae7d2
JH
600 gimple old_stmt, gimple new_stmt,
601 bool update_speculative)
564fe867
JH
602{
603 struct cgraph_node *node;
604 struct cgraph_edge *edge = cgraph_edge (orig, old_stmt);
605
606 if (edge)
042ae7d2 607 cgraph_set_call_stmt (edge, new_stmt, update_speculative);
564fe867
JH
608
609 node = orig->clones;
610 if (node)
611 while (node != orig)
612 {
613 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
614 if (edge)
042ae7d2
JH
615 {
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
619 callgraph edges. */
620 if (edge->speculative && !update_speculative)
621 {
622 struct cgraph_edge *direct, *indirect;
623 struct ipa_ref *ref;
624
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;
630 }
631 }
564fe867
JH
632 if (node->clones)
633 node = node->clones;
634 else if (node->next_sibling_clone)
635 node = node->next_sibling_clone;
636 else
637 {
638 while (node != orig && !node->next_sibling_clone)
639 node = node->clone_of;
640 if (node != orig)
641 node = node->next_sibling_clone;
642 }
643 }
644}
645
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.
649
650 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
651 frequencies of the clones. */
652
653void
654cgraph_create_edge_including_clones (struct cgraph_node *orig,
655 struct cgraph_node *callee,
656 gimple old_stmt,
657 gimple stmt, gcov_type count,
658 int freq,
659 cgraph_inline_failed_t reason)
660{
661 struct cgraph_node *node;
662 struct cgraph_edge *edge;
663
664 if (!cgraph_edge (orig, stmt))
665 {
666 edge = cgraph_create_edge (orig, callee, stmt, count, freq);
667 edge->inline_failed = reason;
668 }
669
670 node = orig->clones;
671 if (node)
672 while (node != orig)
673 {
674 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
675
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. */
680 if (edge)
681 cgraph_set_call_stmt (edge, stmt);
682 else if (!cgraph_edge (node, stmt))
683 {
684 edge = cgraph_create_edge (node, callee, stmt, count,
685 freq);
686 edge->inline_failed = reason;
687 }
688
689 if (node->clones)
690 node = node->clones;
691 else if (node->next_sibling_clone)
692 node = node->next_sibling_clone;
693 else
694 {
695 while (node != orig && !node->next_sibling_clone)
696 node = node->clone_of;
697 if (node != orig)
698 node = node->next_sibling_clone;
699 }
700 }
701}
702
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
706 tree. */
707
708bool
709cgraph_remove_node_and_inline_clones (struct cgraph_node *node, struct cgraph_node *forbidden_node)
710{
711 struct cgraph_edge *e, *next;
712 bool found = false;
713
714 if (node == forbidden_node)
39f9719e
JH
715 {
716 cgraph_remove_edge (node->callers);
717 return true;
718 }
564fe867
JH
719 for (e = node->callees; e; e = next)
720 {
721 next = e->next_callee;
722 if (!e->inline_failed)
723 found |= cgraph_remove_node_and_inline_clones (e->callee, forbidden_node);
724 }
725 cgraph_remove_node (node);
726 return found;
727}
728
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. */
732
733static void
734update_call_expr (struct cgraph_node *new_version)
735{
736 struct cgraph_edge *e;
737
738 gcc_assert (new_version);
739
740 /* Update the call expr on the edges to call the new version. */
741 for (e = new_version->callers; e; e = e->next_caller)
742 {
67348ccc
DM
743 struct function *inner_function = DECL_STRUCT_FUNCTION (e->caller->decl);
744 gimple_call_set_fndecl (e->call_stmt, new_version->decl);
564fe867
JH
745 maybe_clean_eh_stmt_fn (inner_function, e->call_stmt);
746 }
747}
748
749
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
755 version node.
756
757 If non-NULL BLOCK_TO_COPY determine what basic blocks
758 was copied to prevent duplications of calls that are dead
759 in the clone. */
760
761struct cgraph_node *
762cgraph_copy_node_for_versioning (struct cgraph_node *old_version,
763 tree new_decl,
9771b263 764 vec<cgraph_edge_p> redirect_callers,
564fe867
JH
765 bitmap bbs_to_copy)
766 {
767 struct cgraph_node *new_version;
768 struct cgraph_edge *e;
769 unsigned i;
770
771 gcc_assert (old_version);
772
773 new_version = cgraph_create_node (new_decl);
774
67348ccc
DM
775 new_version->analyzed = old_version->analyzed;
776 new_version->definition = old_version->definition;
564fe867 777 new_version->local = old_version->local;
67348ccc
DM
778 new_version->externally_visible = false;
779 new_version->local.local = new_version->definition;
564fe867
JH
780 new_version->global = old_version->global;
781 new_version->rtl = old_version->rtl;
782 new_version->count = old_version->count;
783
784 for (e = old_version->callees; e; e=e->next_callee)
785 if (!bbs_to_copy
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,
789 CGRAPH_FREQ_BASE,
790 true);
791 for (e = old_version->indirect_calls; e; e=e->next_callee)
792 if (!bbs_to_copy
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,
796 CGRAPH_FREQ_BASE,
797 true);
9771b263 798 FOR_EACH_VEC_ELT (redirect_callers, i, e)
564fe867
JH
799 {
800 /* Redirect calls to the old version node to point to its new
801 version. */
802 cgraph_redirect_edge_callee (e, new_version);
803 }
804
805 cgraph_call_node_duplication_hooks (old_version, new_version);
806
807 return new_version;
808 }
809
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).
814
815 REDIRECT_CALLERS varray includes the edges to be redirected
816 to the new version.
817
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.
821
822 If non-NULL ARGS_TO_SKIP determine function parameters to remove
823 from new version.
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.
827
828 Return the new version's cgraph node. */
829
830struct cgraph_node *
831cgraph_function_versioning (struct cgraph_node *old_version_node,
9771b263
DN
832 vec<cgraph_edge_p> redirect_callers,
833 vec<ipa_replace_map_p, va_gc> *tree_map,
564fe867
JH
834 bitmap args_to_skip,
835 bool skip_return,
836 bitmap bbs_to_copy,
837 basic_block new_entry_block,
838 const char *clone_name)
839{
67348ccc 840 tree old_decl = old_version_node->decl;
564fe867
JH
841 struct cgraph_node *new_version_node = NULL;
842 tree new_decl;
843
844 if (!tree_versionable_function_p (old_decl))
845 return NULL;
846
847 gcc_assert (old_version_node->local.can_change_signature || !args_to_skip);
848
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);
852 else
853 new_decl
854 = build_function_decl_skip_args (old_decl, args_to_skip, skip_return);
855
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);
860
861 /* When the old decl was a con-/destructor make sure the clone isn't. */
c3284718
RS
862 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
863 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
564fe867
JH
864
865 /* Create the new version's call-graph node.
866 and update the edges of the new node. */
867 new_version_node =
868 cgraph_copy_node_for_versioning (old_version_node, new_decl,
869 redirect_callers, bbs_to_copy);
870
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);
874
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. */
67348ccc
DM
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;
564fe867
JH
883 new_version_node->local.local = 1;
884 new_version_node->lowered = true;
702d8703
JH
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))
890 || in_lto_p)
67348ccc 891 new_version_node->unique_name = true;
564fe867
JH
892
893 /* Update the call_expr on the edges to call the new version node. */
894 update_call_expr (new_version_node);
895
896 cgraph_call_function_insertion_hooks (new_version_node);
897 return new_version_node;
898}
899
900/* Given virtual clone, turn it into actual clone. */
901
902static void
903cgraph_materialize_clone (struct cgraph_node *node)
904{
905 bitmap_obstack_initialize (NULL);
67348ccc 906 node->former_clone_of = node->clone_of->decl;
564fe867
JH
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. */
67348ccc 910 tree_function_versioning (node->clone_of->decl, node->decl,
564fe867
JH
911 node->clone.tree_map, true,
912 node->clone.args_to_skip, false,
913 NULL, NULL);
914 if (cgraph_dump_file)
915 {
67348ccc
DM
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);
564fe867
JH
918 }
919
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;
925 else
926 node->clone_of->clones = node->next_sibling_clone;
927 node->next_sibling_clone = NULL;
928 node->prev_sibling_clone = NULL;
67348ccc 929 if (!node->clone_of->analyzed && !node->clone_of->clones)
564fe867
JH
930 {
931 cgraph_release_function_body (node->clone_of);
932 cgraph_node_remove_callees (node->clone_of);
67348ccc 933 ipa_remove_all_references (&node->clone_of->ref_list);
564fe867
JH
934 }
935 node->clone_of = NULL;
936 bitmap_obstack_release (NULL);
937}
938
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
943 this order. */
944
945void
946cgraph_materialize_all_clones (void)
947{
948 struct cgraph_node *node;
949 bool stabilized = false;
042ae7d2 950
564fe867
JH
951
952 if (cgraph_dump_file)
953 fprintf (cgraph_dump_file, "Materializing clones\n");
954#ifdef ENABLE_CHECKING
955 verify_cgraph ();
956#endif
957
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. */
961 while (!stabilized)
962 {
963 stabilized = true;
964 FOR_EACH_FUNCTION (node)
965 {
67348ccc
DM
966 if (node->clone_of && node->decl != node->clone_of->decl
967 && !gimple_has_body_p (node->decl))
564fe867 968 {
a2e2a668
JH
969 if (!node->clone_of->clone_of)
970 cgraph_get_body (node->clone_of);
67348ccc 971 if (gimple_has_body_p (node->clone_of->decl))
564fe867
JH
972 {
973 if (cgraph_dump_file)
974 {
975 fprintf (cgraph_dump_file, "cloning %s to %s\n",
fec39fa6
TS
976 xstrdup (node->clone_of->name ()),
977 xstrdup (node->name ()));
564fe867
JH
978 if (node->clone.tree_map)
979 {
980 unsigned int i;
981 fprintf (cgraph_dump_file, " replace map: ");
9771b263
DN
982 for (i = 0;
983 i < vec_safe_length (node->clone.tree_map);
984 i++)
564fe867
JH
985 {
986 struct ipa_replace_map *replace_info;
9771b263 987 replace_info = (*node->clone.tree_map)[i];
564fe867
JH
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)":"");
994 }
995 fprintf (cgraph_dump_file, "\n");
996 }
997 if (node->clone.args_to_skip)
998 {
999 fprintf (cgraph_dump_file, " args_to_skip: ");
1000 dump_bitmap (cgraph_dump_file, node->clone.args_to_skip);
1001 }
1002 if (node->clone.args_to_skip)
1003 {
1004 fprintf (cgraph_dump_file, " combined_args_to_skip:");
1005 dump_bitmap (cgraph_dump_file, node->clone.combined_args_to_skip);
1006 }
1007 }
1008 cgraph_materialize_clone (node);
1009 stabilized = false;
1010 }
1011 }
1012 }
1013 }
1014 FOR_EACH_FUNCTION (node)
67348ccc 1015 if (!node->analyzed && node->callees)
71cafea9
JH
1016 {
1017 cgraph_node_remove_callees (node);
67348ccc 1018 ipa_remove_all_references (&node->ref_list);
71cafea9
JH
1019 }
1020 else
67348ccc 1021 ipa_clear_stmts_in_references (node);
564fe867
JH
1022 if (cgraph_dump_file)
1023 fprintf (cgraph_dump_file, "Materialization Call site updates done.\n");
1024#ifdef ENABLE_CHECKING
1025 verify_cgraph ();
1026#endif
04142cc3 1027 symtab_remove_unreachable_nodes (false, cgraph_dump_file);
564fe867
JH
1028}
1029
1030#include "gt-cgraphclones.h"
This page took 0.498303 seconds and 5 git commands to generate.