cgraph.h (struct cgraph_node): Add ipcp_clone flag.
[gcc.git] / gcc / cgraphclones.c
1 /* Callgraph clones
2 Copyright (C) 2003-2019 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21 /* This module provide facilities for cloning 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 "backend.h"
71 #include "target.h"
72 #include "rtl.h"
73 #include "tree.h"
74 #include "gimple.h"
75 #include "stringpool.h"
76 #include "cgraph.h"
77 #include "lto-streamer.h"
78 #include "tree-eh.h"
79 #include "tree-cfg.h"
80 #include "tree-inline.h"
81 #include "dumpfile.h"
82 #include "gimple-pretty-print.h"
83
84 /* Create clone of edge in the node N represented by CALL_EXPR
85 the callgraph. */
86
87 cgraph_edge *
88 cgraph_edge::clone (cgraph_node *n, gcall *call_stmt, unsigned stmt_uid,
89 profile_count num, profile_count den,
90 bool update_original)
91 {
92 cgraph_edge *new_edge;
93 profile_count::adjust_for_ipa_scaling (&num, &den);
94 profile_count prof_count = count.apply_scale (num, den);
95
96 if (indirect_unknown_callee)
97 {
98 tree decl;
99
100 if (call_stmt && (decl = gimple_call_fndecl (call_stmt))
101 /* When the call is speculative, we need to resolve it
102 via cgraph_resolve_speculation and not here. */
103 && !speculative)
104 {
105 cgraph_node *callee = cgraph_node::get (decl);
106 gcc_checking_assert (callee);
107 new_edge = n->create_edge (callee, call_stmt, prof_count, true);
108 }
109 else
110 {
111 new_edge = n->create_indirect_edge (call_stmt,
112 indirect_info->ecf_flags,
113 prof_count, true);
114 *new_edge->indirect_info = *indirect_info;
115 }
116 }
117 else
118 {
119 new_edge = n->create_edge (callee, call_stmt, prof_count, true);
120 if (indirect_info)
121 {
122 new_edge->indirect_info
123 = ggc_cleared_alloc<cgraph_indirect_call_info> ();
124 *new_edge->indirect_info = *indirect_info;
125 }
126 }
127
128 new_edge->inline_failed = inline_failed;
129 new_edge->indirect_inlining_edge = indirect_inlining_edge;
130 new_edge->lto_stmt_uid = stmt_uid;
131 /* Clone flags that depend on call_stmt availability manually. */
132 new_edge->can_throw_external = can_throw_external;
133 new_edge->call_stmt_cannot_inline_p = call_stmt_cannot_inline_p;
134 new_edge->speculative = speculative;
135 new_edge->in_polymorphic_cdtor = in_polymorphic_cdtor;
136
137 /* Update IPA profile. Local profiles need no updating in original. */
138 if (update_original)
139 count = count.combine_with_ipa_count (count.ipa ()
140 - new_edge->count.ipa ());
141 symtab->call_edge_duplication_hooks (this, new_edge);
142 return new_edge;
143 }
144
145 /* Set flags of NEW_NODE and its decl. NEW_NODE is a newly created private
146 clone or its thunk. */
147
148 static void
149 set_new_clone_decl_and_node_flags (cgraph_node *new_node)
150 {
151 DECL_EXTERNAL (new_node->decl) = 0;
152 TREE_PUBLIC (new_node->decl) = 0;
153 DECL_COMDAT (new_node->decl) = 0;
154 DECL_WEAK (new_node->decl) = 0;
155 DECL_VIRTUAL_P (new_node->decl) = 0;
156 DECL_STATIC_CONSTRUCTOR (new_node->decl) = 0;
157 DECL_STATIC_DESTRUCTOR (new_node->decl) = 0;
158 DECL_SET_IS_OPERATOR_NEW (new_node->decl, 0);
159 DECL_SET_IS_OPERATOR_DELETE (new_node->decl, 0);
160
161 new_node->externally_visible = 0;
162 new_node->local = 1;
163 new_node->lowered = true;
164 }
165
166 /* Duplicate thunk THUNK if necessary but make it to refer to NODE.
167 ARGS_TO_SKIP, if non-NULL, determines which parameters should be omitted.
168 Function can return NODE if no thunk is necessary, which can happen when
169 thunk is this_adjusting but we are removing this parameter. */
170
171 static cgraph_node *
172 duplicate_thunk_for_node (cgraph_node *thunk, cgraph_node *node)
173 {
174 cgraph_node *new_thunk, *thunk_of;
175 thunk_of = thunk->callees->callee->ultimate_alias_target ();
176
177 if (thunk_of->thunk.thunk_p)
178 node = duplicate_thunk_for_node (thunk_of, node);
179
180 if (!DECL_ARGUMENTS (thunk->decl))
181 thunk->get_untransformed_body ();
182
183 cgraph_edge *cs;
184 for (cs = node->callers; cs; cs = cs->next_caller)
185 if (cs->caller->thunk.thunk_p
186 && cs->caller->thunk.fixed_offset == thunk->thunk.fixed_offset
187 && cs->caller->thunk.virtual_value == thunk->thunk.virtual_value
188 && cs->caller->thunk.indirect_offset == thunk->thunk.indirect_offset
189 && cs->caller->thunk.this_adjusting == thunk->thunk.this_adjusting
190 && cs->caller->thunk.virtual_offset_p == thunk->thunk.virtual_offset_p)
191 return cs->caller;
192
193 tree new_decl;
194 if (node->clone.param_adjustments)
195 {
196 /* We do not need to duplicate this_adjusting thunks if we have removed
197 this. */
198 if (thunk->thunk.this_adjusting
199 && !node->clone.param_adjustments->first_param_intact_p ())
200 return node;
201
202 new_decl = copy_node (thunk->decl);
203 ipa_param_body_adjustments body_adj (node->clone.param_adjustments,
204 new_decl);
205 body_adj.modify_formal_parameters ();
206 }
207 else
208 new_decl = copy_node (thunk->decl);
209
210 gcc_checking_assert (!DECL_STRUCT_FUNCTION (new_decl));
211 gcc_checking_assert (!DECL_INITIAL (new_decl));
212 gcc_checking_assert (!DECL_RESULT (new_decl));
213 gcc_checking_assert (!DECL_RTL_SET_P (new_decl));
214
215 DECL_NAME (new_decl) = clone_function_name_numbered (thunk->decl,
216 "artificial_thunk");
217 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
218
219 /* We need to force DECL_IGNORED_P because the new thunk is created after
220 early debug was run. */
221 DECL_IGNORED_P (new_decl) = 1;
222
223 new_thunk = cgraph_node::create (new_decl);
224 set_new_clone_decl_and_node_flags (new_thunk);
225 new_thunk->definition = true;
226 new_thunk->can_change_signature = node->can_change_signature;
227 new_thunk->thunk = thunk->thunk;
228 new_thunk->unique_name = in_lto_p;
229 new_thunk->former_clone_of = thunk->decl;
230 new_thunk->clone.param_adjustments = node->clone.param_adjustments;
231
232 cgraph_edge *e = new_thunk->create_edge (node, NULL, new_thunk->count);
233 symtab->call_edge_duplication_hooks (thunk->callees, e);
234 symtab->call_cgraph_duplication_hooks (thunk, new_thunk);
235 return new_thunk;
236 }
237
238 /* If E does not lead to a thunk, simply redirect it to N. Otherwise create
239 one or more equivalent thunks for N and redirect E to the first in the
240 chain. Note that it is then necessary to call
241 n->expand_all_artificial_thunks once all callers are redirected. */
242
243 void
244 cgraph_edge::redirect_callee_duplicating_thunks (cgraph_node *n)
245 {
246 cgraph_node *orig_to = callee->ultimate_alias_target ();
247 if (orig_to->thunk.thunk_p)
248 n = duplicate_thunk_for_node (orig_to, n);
249
250 redirect_callee (n);
251 }
252
253 /* Call expand_thunk on all callers that are thunks and if analyze those nodes
254 that were expanded. */
255
256 void
257 cgraph_node::expand_all_artificial_thunks ()
258 {
259 cgraph_edge *e;
260 for (e = callers; e;)
261 if (e->caller->thunk.thunk_p)
262 {
263 cgraph_node *thunk = e->caller;
264
265 e = e->next_caller;
266 if (thunk->expand_thunk (false, false))
267 {
268 thunk->thunk.thunk_p = false;
269 thunk->analyze ();
270 }
271 thunk->expand_all_artificial_thunks ();
272 }
273 else
274 e = e->next_caller;
275 }
276
277 void
278 dump_callgraph_transformation (const cgraph_node *original,
279 const cgraph_node *clone,
280 const char *suffix)
281 {
282 if (symtab->ipa_clones_dump_file)
283 {
284 fprintf (symtab->ipa_clones_dump_file,
285 "Callgraph clone;%s;%d;%s;%d;%d;%s;%d;%s;%d;%d;%s\n",
286 original->asm_name (), original->order,
287 DECL_SOURCE_FILE (original->decl),
288 DECL_SOURCE_LINE (original->decl),
289 DECL_SOURCE_COLUMN (original->decl), clone->asm_name (),
290 clone->order, DECL_SOURCE_FILE (clone->decl),
291 DECL_SOURCE_LINE (clone->decl), DECL_SOURCE_COLUMN (clone->decl),
292 suffix);
293
294 symtab->cloned_nodes.add (original);
295 symtab->cloned_nodes.add (clone);
296 }
297 }
298
299 /* Create node representing clone of N executed COUNT times. Decrease
300 the execution counts from original node too.
301 The new clone will have decl set to DECL that may or may not be the same
302 as decl of N.
303
304 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
305 function's profile to reflect the fact that part of execution is handled
306 by node.
307 When CALL_DUPLICATION_HOOK is true, the ipa passes are acknowledged about
308 the new clone. Otherwise the caller is responsible for doing so later.
309
310 If the new node is being inlined into another one, NEW_INLINED_TO should be
311 the outline function the new one is (even indirectly) inlined to. All hooks
312 will see this in node's inlined_to, when invoked. Can be NULL if the
313 node is not inlined.
314
315 If PARAM_ADJUSTMENTS is non-NULL, the parameter manipulation information
316 will be overwritten by the new structure. Otherwise the new node will
317 share parameter manipulation information with the original node. */
318
319 cgraph_node *
320 cgraph_node::create_clone (tree new_decl, profile_count prof_count,
321 bool update_original,
322 vec<cgraph_edge *> redirect_callers,
323 bool call_duplication_hook,
324 cgraph_node *new_inlined_to,
325 ipa_param_adjustments *param_adjustments,
326 const char *suffix)
327 {
328 cgraph_node *new_node = symtab->create_empty ();
329 cgraph_edge *e;
330 unsigned i;
331 profile_count old_count = count;
332
333 if (new_inlined_to)
334 dump_callgraph_transformation (this, new_inlined_to, "inlining to");
335
336 /* When inlining we scale precisely to prof_count, when cloning we can
337 preserve local profile. */
338 if (!new_inlined_to)
339 prof_count = count.combine_with_ipa_count (prof_count);
340 new_node->count = prof_count;
341
342 /* Update IPA profile. Local profiles need no updating in original. */
343 if (update_original)
344 count = count.combine_with_ipa_count (count.ipa () - prof_count.ipa ());
345 new_node->decl = new_decl;
346 new_node->register_symbol ();
347 new_node->origin = origin;
348 new_node->lto_file_data = lto_file_data;
349 if (new_node->origin)
350 {
351 new_node->next_nested = new_node->origin->nested;
352 new_node->origin->nested = new_node;
353 }
354 new_node->analyzed = analyzed;
355 new_node->definition = definition;
356 new_node->versionable = versionable;
357 new_node->can_change_signature = can_change_signature;
358 new_node->redefined_extern_inline = redefined_extern_inline;
359 new_node->tm_may_enter_irr = tm_may_enter_irr;
360 new_node->externally_visible = false;
361 new_node->no_reorder = no_reorder;
362 new_node->local = true;
363 new_node->inlined_to = new_inlined_to;
364 new_node->rtl = rtl;
365 new_node->frequency = frequency;
366 new_node->tp_first_run = tp_first_run;
367 new_node->tm_clone = tm_clone;
368 new_node->icf_merged = icf_merged;
369 new_node->merged_comdat = merged_comdat;
370 new_node->thunk = thunk;
371
372 if (param_adjustments)
373 new_node->clone.param_adjustments = param_adjustments;
374 else
375 new_node->clone.param_adjustments = clone.param_adjustments;
376 new_node->clone.tree_map = NULL;
377 new_node->clone.performed_splits = vec_safe_copy (clone.performed_splits);
378 new_node->split_part = split_part;
379
380 FOR_EACH_VEC_ELT (redirect_callers, i, e)
381 {
382 /* Redirect calls to the old version node to point to its new
383 version. The only exception is when the edge was proved to
384 be unreachable during the cloning procedure. */
385 if (!e->callee
386 || !fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE))
387 e->redirect_callee_duplicating_thunks (new_node);
388 }
389 new_node->expand_all_artificial_thunks ();
390
391 for (e = callees;e; e=e->next_callee)
392 e->clone (new_node, e->call_stmt, e->lto_stmt_uid, new_node->count, old_count,
393 update_original);
394
395 for (e = indirect_calls; e; e = e->next_callee)
396 e->clone (new_node, e->call_stmt, e->lto_stmt_uid,
397 new_node->count, old_count, update_original);
398 new_node->clone_references (this);
399
400 new_node->next_sibling_clone = clones;
401 if (clones)
402 clones->prev_sibling_clone = new_node;
403 clones = new_node;
404 new_node->clone_of = this;
405
406 if (call_duplication_hook)
407 symtab->call_cgraph_duplication_hooks (this, new_node);
408
409 if (!new_inlined_to)
410 dump_callgraph_transformation (this, new_node, suffix);
411
412 return new_node;
413 }
414
415 static GTY(()) hash_map<const char *, unsigned> *clone_fn_ids;
416
417 /* Return a new assembler name for a clone of decl named NAME. Apart
418 from the string SUFFIX, the new name will end with a unique (for
419 each NAME) unspecified number. If clone numbering is not needed
420 then the two argument clone_function_name should be used instead.
421 Should not be called directly except for by
422 lto-partition.c:privatize_symbol_name_1. */
423
424 tree
425 clone_function_name_numbered (const char *name, const char *suffix)
426 {
427 /* Initialize the function->counter mapping the first time it's
428 needed. */
429 if (!clone_fn_ids)
430 clone_fn_ids = hash_map<const char *, unsigned int>::create_ggc (64);
431 unsigned int &suffix_counter = clone_fn_ids->get_or_insert (
432 IDENTIFIER_POINTER (get_identifier (name)));
433 return clone_function_name (name, suffix, suffix_counter++);
434 }
435
436 /* Return a new assembler name for a clone of DECL. Apart from string
437 SUFFIX, the new name will end with a unique (for each DECL
438 assembler name) unspecified number. If clone numbering is not
439 needed then the two argument clone_function_name should be used
440 instead. */
441
442 tree
443 clone_function_name_numbered (tree decl, const char *suffix)
444 {
445 tree name = DECL_ASSEMBLER_NAME (decl);
446 return clone_function_name_numbered (IDENTIFIER_POINTER (name),
447 suffix);
448 }
449
450 /* Return a new assembler name for a clone of decl named NAME. Apart
451 from the string SUFFIX, the new name will end with the specified
452 NUMBER. If clone numbering is not needed then the two argument
453 clone_function_name should be used instead. */
454
455 tree
456 clone_function_name (const char *name, const char *suffix,
457 unsigned long number)
458 {
459 size_t len = strlen (name);
460 char *tmp_name, *prefix;
461
462 prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
463 memcpy (prefix, name, len);
464 strcpy (prefix + len + 1, suffix);
465 prefix[len] = symbol_table::symbol_suffix_separator ();
466 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, number);
467 return get_identifier (tmp_name);
468 }
469
470 /* Return a new assembler name for a clone of DECL. Apart from the
471 string SUFFIX, the new name will end with the specified NUMBER. If
472 clone numbering is not needed then the two argument
473 clone_function_name should be used instead. */
474
475 tree
476 clone_function_name (tree decl, const char *suffix,
477 unsigned long number)
478 {
479 return clone_function_name (
480 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), suffix, number);
481 }
482
483 /* Return a new assembler name ending with the string SUFFIX for a
484 clone of DECL. */
485
486 tree
487 clone_function_name (tree decl, const char *suffix)
488 {
489 tree identifier = DECL_ASSEMBLER_NAME (decl);
490 /* For consistency this needs to behave the same way as
491 ASM_FORMAT_PRIVATE_NAME does, but without the final number
492 suffix. */
493 char *separator = XALLOCAVEC (char, 2);
494 separator[0] = symbol_table::symbol_suffix_separator ();
495 separator[1] = 0;
496 #if defined (NO_DOT_IN_LABEL) && defined (NO_DOLLAR_IN_LABEL)
497 const char *prefix = "__";
498 #else
499 const char *prefix = "";
500 #endif
501 char *result = ACONCAT ((prefix,
502 IDENTIFIER_POINTER (identifier),
503 separator,
504 suffix,
505 (char*)0));
506 return get_identifier (result);
507 }
508
509
510 /* Create callgraph node clone with new declaration. The actual body will be
511 copied later at compilation stage. The name of the new clone will be
512 constructed from the name of the original node, SUFFIX and NUM_SUFFIX.
513
514 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
515 bitmap interface.
516 */
517 cgraph_node *
518 cgraph_node::create_virtual_clone (vec<cgraph_edge *> redirect_callers,
519 vec<ipa_replace_map *, va_gc> *tree_map,
520 ipa_param_adjustments *param_adjustments,
521 const char * suffix, unsigned num_suffix)
522 {
523 tree old_decl = decl;
524 cgraph_node *new_node = NULL;
525 tree new_decl;
526 size_t len, i;
527 ipa_replace_map *map;
528 char *name;
529
530 gcc_checking_assert (versionable);
531 /* TODO: It would be nice if we could recognize that param_adjustments do not
532 actually perform any changes, but at the moment let's require it simply
533 does not exist. */
534 gcc_assert (can_change_signature || !param_adjustments);
535
536 /* Make a new FUNCTION_DECL tree node */
537 if (!param_adjustments)
538 new_decl = copy_node (old_decl);
539 else
540 new_decl = param_adjustments->adjust_decl (old_decl);
541
542 /* These pointers represent function body and will be populated only when clone
543 is materialized. */
544 gcc_assert (new_decl != old_decl);
545 DECL_STRUCT_FUNCTION (new_decl) = NULL;
546 DECL_ARGUMENTS (new_decl) = NULL;
547 DECL_INITIAL (new_decl) = NULL;
548 DECL_RESULT (new_decl) = NULL;
549 /* We cannot do DECL_RESULT (new_decl) = NULL; here because of LTO partitioning
550 sometimes storing only clone decl instead of original. */
551
552 /* Generate a new name for the new version. */
553 len = IDENTIFIER_LENGTH (DECL_NAME (old_decl));
554 name = XALLOCAVEC (char, len + strlen (suffix) + 2);
555 memcpy (name, IDENTIFIER_POINTER (DECL_NAME (old_decl)), len);
556 strcpy (name + len + 1, suffix);
557 name[len] = '.';
558 DECL_NAME (new_decl) = get_identifier (name);
559 SET_DECL_ASSEMBLER_NAME (new_decl,
560 clone_function_name (old_decl, suffix, num_suffix));
561 SET_DECL_RTL (new_decl, NULL);
562
563 new_node = create_clone (new_decl, count, false,
564 redirect_callers, false, NULL, param_adjustments,
565 suffix);
566
567 /* Update the properties.
568 Make clone visible only within this translation unit. Make sure
569 that is not weak also.
570 ??? We cannot use COMDAT linkage because there is no
571 ABI support for this. */
572 set_new_clone_decl_and_node_flags (new_node);
573 new_node->ipcp_clone = ipcp_clone;
574 new_node->clone.tree_map = tree_map;
575 if (!implicit_section)
576 new_node->set_section (get_section ());
577
578 /* Clones of global symbols or symbols with unique names are unique. */
579 if ((TREE_PUBLIC (old_decl)
580 && !DECL_EXTERNAL (old_decl)
581 && !DECL_WEAK (old_decl)
582 && !DECL_COMDAT (old_decl))
583 || in_lto_p)
584 new_node->unique_name = true;
585 FOR_EACH_VEC_SAFE_ELT (tree_map, i, map)
586 new_node->maybe_create_reference (map->new_tree, NULL);
587
588 if (ipa_transforms_to_apply.exists ())
589 new_node->ipa_transforms_to_apply
590 = ipa_transforms_to_apply.copy ();
591
592 symtab->call_cgraph_duplication_hooks (this, new_node);
593
594 return new_node;
595 }
596
597 /* callgraph node being removed from symbol table; see if its entry can be
598 replaced by other inline clone. */
599 cgraph_node *
600 cgraph_node::find_replacement (void)
601 {
602 cgraph_node *next_inline_clone, *replacement;
603
604 for (next_inline_clone = clones;
605 next_inline_clone
606 && next_inline_clone->decl != decl;
607 next_inline_clone = next_inline_clone->next_sibling_clone)
608 ;
609
610 /* If there is inline clone of the node being removed, we need
611 to put it into the position of removed node and reorganize all
612 other clones to be based on it. */
613 if (next_inline_clone)
614 {
615 cgraph_node *n;
616 cgraph_node *new_clones;
617
618 replacement = next_inline_clone;
619
620 /* Unlink inline clone from the list of clones of removed node. */
621 if (next_inline_clone->next_sibling_clone)
622 next_inline_clone->next_sibling_clone->prev_sibling_clone
623 = next_inline_clone->prev_sibling_clone;
624 if (next_inline_clone->prev_sibling_clone)
625 {
626 gcc_assert (clones != next_inline_clone);
627 next_inline_clone->prev_sibling_clone->next_sibling_clone
628 = next_inline_clone->next_sibling_clone;
629 }
630 else
631 {
632 gcc_assert (clones == next_inline_clone);
633 clones = next_inline_clone->next_sibling_clone;
634 }
635
636 new_clones = clones;
637 clones = NULL;
638
639 /* Copy clone info. */
640 next_inline_clone->clone = clone;
641
642 /* Now place it into clone tree at same level at NODE. */
643 next_inline_clone->clone_of = clone_of;
644 next_inline_clone->prev_sibling_clone = NULL;
645 next_inline_clone->next_sibling_clone = NULL;
646 if (clone_of)
647 {
648 if (clone_of->clones)
649 clone_of->clones->prev_sibling_clone = next_inline_clone;
650 next_inline_clone->next_sibling_clone = clone_of->clones;
651 clone_of->clones = next_inline_clone;
652 }
653
654 /* Merge the clone list. */
655 if (new_clones)
656 {
657 if (!next_inline_clone->clones)
658 next_inline_clone->clones = new_clones;
659 else
660 {
661 n = next_inline_clone->clones;
662 while (n->next_sibling_clone)
663 n = n->next_sibling_clone;
664 n->next_sibling_clone = new_clones;
665 new_clones->prev_sibling_clone = n;
666 }
667 }
668
669 /* Update clone_of pointers. */
670 n = new_clones;
671 while (n)
672 {
673 n->clone_of = next_inline_clone;
674 n = n->next_sibling_clone;
675 }
676
677 /* Update order in order to be able to find a LTO section
678 with function body. */
679 replacement->order = order;
680
681 return replacement;
682 }
683 else
684 return NULL;
685 }
686
687 /* Like cgraph_set_call_stmt but walk the clone tree and update all
688 clones sharing the same function body.
689 When WHOLE_SPECULATIVE_EDGES is true, all three components of
690 speculative edge gets updated. Otherwise we update only direct
691 call. */
692
693 void
694 cgraph_node::set_call_stmt_including_clones (gimple *old_stmt,
695 gcall *new_stmt,
696 bool update_speculative)
697 {
698 cgraph_node *node;
699 cgraph_edge *edge = get_edge (old_stmt);
700
701 if (edge)
702 edge->set_call_stmt (new_stmt, update_speculative);
703
704 node = clones;
705 if (node)
706 while (node != this)
707 {
708 cgraph_edge *edge = node->get_edge (old_stmt);
709 if (edge)
710 {
711 edge->set_call_stmt (new_stmt, update_speculative);
712 /* If UPDATE_SPECULATIVE is false, it means that we are turning
713 speculative call into a real code sequence. Update the
714 callgraph edges. */
715 if (edge->speculative && !update_speculative)
716 {
717 cgraph_edge *direct, *indirect;
718 ipa_ref *ref;
719
720 gcc_assert (!edge->indirect_unknown_callee);
721 edge->speculative_call_info (direct, indirect, ref);
722 direct->speculative = false;
723 indirect->speculative = false;
724 ref->speculative = false;
725 }
726 }
727 if (node->clones)
728 node = node->clones;
729 else if (node->next_sibling_clone)
730 node = node->next_sibling_clone;
731 else
732 {
733 while (node != this && !node->next_sibling_clone)
734 node = node->clone_of;
735 if (node != this)
736 node = node->next_sibling_clone;
737 }
738 }
739 }
740
741 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
742 same function body. If clones already have edge for OLD_STMT; only
743 update the edge same way as cgraph_set_call_stmt_including_clones does.
744
745 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
746 frequencies of the clones. */
747
748 void
749 cgraph_node::create_edge_including_clones (cgraph_node *callee,
750 gimple *old_stmt, gcall *stmt,
751 profile_count count,
752 cgraph_inline_failed_t reason)
753 {
754 cgraph_node *node;
755 cgraph_edge *edge;
756
757 if (!get_edge (stmt))
758 {
759 edge = create_edge (callee, stmt, count);
760 edge->inline_failed = reason;
761 }
762
763 node = clones;
764 if (node)
765 while (node != this)
766 /* Thunk clones do not get updated while copying inline function body. */
767 if (!node->thunk.thunk_p)
768 {
769 cgraph_edge *edge = node->get_edge (old_stmt);
770
771 /* It is possible that clones already contain the edge while
772 master didn't. Either we promoted indirect call into direct
773 call in the clone or we are processing clones of unreachable
774 master where edges has been removed. */
775 if (edge)
776 edge->set_call_stmt (stmt);
777 else if (! node->get_edge (stmt))
778 {
779 edge = node->create_edge (callee, stmt, count);
780 edge->inline_failed = reason;
781 }
782
783 if (node->clones)
784 node = node->clones;
785 else if (node->next_sibling_clone)
786 node = node->next_sibling_clone;
787 else
788 {
789 while (node != this && !node->next_sibling_clone)
790 node = node->clone_of;
791 if (node != this)
792 node = node->next_sibling_clone;
793 }
794 }
795 }
796
797 /* Remove the node from cgraph and all inline clones inlined into it.
798 Skip however removal of FORBIDDEN_NODE and return true if it needs to be
799 removed. This allows to call the function from outer loop walking clone
800 tree. */
801
802 bool
803 cgraph_node::remove_symbol_and_inline_clones (cgraph_node *forbidden_node)
804 {
805 cgraph_edge *e, *next;
806 bool found = false;
807
808 if (this == forbidden_node)
809 {
810 callers->remove ();
811 return true;
812 }
813 for (e = callees; e; e = next)
814 {
815 next = e->next_callee;
816 if (!e->inline_failed)
817 found |= e->callee->remove_symbol_and_inline_clones (forbidden_node);
818 }
819 remove ();
820 return found;
821 }
822
823 /* The edges representing the callers of the NEW_VERSION node were
824 fixed by cgraph_function_versioning (), now the call_expr in their
825 respective tree code should be updated to call the NEW_VERSION. */
826
827 static void
828 update_call_expr (cgraph_node *new_version)
829 {
830 cgraph_edge *e;
831
832 gcc_assert (new_version);
833
834 /* Update the call expr on the edges to call the new version. */
835 for (e = new_version->callers; e; e = e->next_caller)
836 {
837 function *inner_function = DECL_STRUCT_FUNCTION (e->caller->decl);
838 gimple_call_set_fndecl (e->call_stmt, new_version->decl);
839 maybe_clean_eh_stmt_fn (inner_function, e->call_stmt);
840 }
841 }
842
843
844 /* Create a new cgraph node which is the new version of
845 callgraph node. REDIRECT_CALLERS holds the callers
846 edges which should be redirected to point to
847 NEW_VERSION. ALL the callees edges of the node
848 are cloned to the new version node. Return the new
849 version node.
850
851 If non-NULL BLOCK_TO_COPY determine what basic blocks
852 was copied to prevent duplications of calls that are dead
853 in the clone. */
854
855 cgraph_node *
856 cgraph_node::create_version_clone (tree new_decl,
857 vec<cgraph_edge *> redirect_callers,
858 bitmap bbs_to_copy,
859 const char *suffix)
860 {
861 cgraph_node *new_version;
862 cgraph_edge *e;
863 unsigned i;
864
865 new_version = cgraph_node::create (new_decl);
866
867 new_version->analyzed = analyzed;
868 new_version->definition = definition;
869 new_version->local = local;
870 new_version->externally_visible = false;
871 new_version->no_reorder = no_reorder;
872 new_version->local = new_version->definition;
873 new_version->inlined_to = inlined_to;
874 new_version->rtl = rtl;
875 new_version->count = count;
876
877 for (e = callees; e; e=e->next_callee)
878 if (!bbs_to_copy
879 || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
880 e->clone (new_version, e->call_stmt,
881 e->lto_stmt_uid, count, count,
882 true);
883 for (e = indirect_calls; e; e=e->next_callee)
884 if (!bbs_to_copy
885 || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
886 e->clone (new_version, e->call_stmt,
887 e->lto_stmt_uid, count, count,
888 true);
889 FOR_EACH_VEC_ELT (redirect_callers, i, e)
890 {
891 /* Redirect calls to the old version node to point to its new
892 version. */
893 e->redirect_callee (new_version);
894 }
895
896 dump_callgraph_transformation (this, new_version, suffix);
897
898 return new_version;
899 }
900
901 /* Perform function versioning.
902 Function versioning includes copying of the tree and
903 a callgraph update (creating a new cgraph node and updating
904 its callees and callers).
905
906 REDIRECT_CALLERS varray includes the edges to be redirected
907 to the new version.
908
909 TREE_MAP is a mapping of tree nodes we want to replace with
910 new ones (according to results of prior analysis).
911
912 If non-NULL ARGS_TO_SKIP determine function parameters to remove
913 from new version.
914 If SKIP_RETURN is true, the new version will return void.
915 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
916 If non_NULL NEW_ENTRY determine new entry BB of the clone.
917
918 If TARGET_ATTRIBUTES is non-null, when creating a new declaration,
919 add the attributes to DECL_ATTRIBUTES. And call valid_attribute_p
920 that will promote value of the attribute DECL_FUNCTION_SPECIFIC_TARGET
921 of the declaration.
922
923 Return the new version's cgraph node. */
924
925 cgraph_node *
926 cgraph_node::create_version_clone_with_body
927 (vec<cgraph_edge *> redirect_callers,
928 vec<ipa_replace_map *, va_gc> *tree_map,
929 ipa_param_adjustments *param_adjustments,
930 bitmap bbs_to_copy, basic_block new_entry_block, const char *suffix,
931 tree target_attributes)
932 {
933 tree old_decl = decl;
934 cgraph_node *new_version_node = NULL;
935 tree new_decl;
936
937 if (!tree_versionable_function_p (old_decl))
938 return NULL;
939
940 /* TODO: Restore an assert that we do not change signature if
941 can_change_signature is false. We cannot just check that
942 param_adjustments is NULL because unfortunately ipa-split removes return
943 values from such functions. */
944
945 /* Make a new FUNCTION_DECL tree node for the new version. */
946 if (param_adjustments)
947 new_decl = param_adjustments->adjust_decl (old_decl);
948 else
949 new_decl = copy_node (old_decl);
950
951 /* Generate a new name for the new version. */
952 DECL_NAME (new_decl) = clone_function_name_numbered (old_decl, suffix);
953 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
954 SET_DECL_RTL (new_decl, NULL);
955
956 DECL_VIRTUAL_P (new_decl) = 0;
957
958 if (target_attributes)
959 {
960 DECL_ATTRIBUTES (new_decl) = target_attributes;
961
962 location_t saved_loc = input_location;
963 tree v = TREE_VALUE (target_attributes);
964 input_location = DECL_SOURCE_LOCATION (new_decl);
965 bool r = targetm.target_option.valid_attribute_p (new_decl, NULL, v, 1);
966 input_location = saved_loc;
967 if (!r)
968 return NULL;
969 }
970
971 /* When the old decl was a con-/destructor make sure the clone isn't. */
972 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
973 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
974 DECL_SET_IS_OPERATOR_NEW (new_decl, 0);
975 DECL_SET_IS_OPERATOR_DELETE (new_decl, 0);
976
977 /* Create the new version's call-graph node.
978 and update the edges of the new node. */
979 new_version_node = create_version_clone (new_decl, redirect_callers,
980 bbs_to_copy, suffix);
981
982 if (ipa_transforms_to_apply.exists ())
983 new_version_node->ipa_transforms_to_apply
984 = ipa_transforms_to_apply.copy ();
985 /* Copy the OLD_VERSION_NODE function tree to the new version. */
986 tree_function_versioning (old_decl, new_decl, tree_map, param_adjustments,
987 false, bbs_to_copy, new_entry_block);
988
989 /* Update the new version's properties.
990 Make The new version visible only within this translation unit. Make sure
991 that is not weak also.
992 ??? We cannot use COMDAT linkage because there is no
993 ABI support for this. */
994 new_version_node->make_decl_local ();
995 DECL_VIRTUAL_P (new_version_node->decl) = 0;
996 new_version_node->externally_visible = 0;
997 new_version_node->local = 1;
998 new_version_node->lowered = true;
999 if (!implicit_section)
1000 new_version_node->set_section (get_section ());
1001 /* Clones of global symbols or symbols with unique names are unique. */
1002 if ((TREE_PUBLIC (old_decl)
1003 && !DECL_EXTERNAL (old_decl)
1004 && !DECL_WEAK (old_decl)
1005 && !DECL_COMDAT (old_decl))
1006 || in_lto_p)
1007 new_version_node->unique_name = true;
1008
1009 /* Update the call_expr on the edges to call the new version node. */
1010 update_call_expr (new_version_node);
1011
1012 symtab->call_cgraph_insertion_hooks (new_version_node);
1013 return new_version_node;
1014 }
1015
1016 /* Given virtual clone, turn it into actual clone. */
1017
1018 static void
1019 cgraph_materialize_clone (cgraph_node *node)
1020 {
1021 bitmap_obstack_initialize (NULL);
1022 node->former_clone_of = node->clone_of->decl;
1023 if (node->clone_of->former_clone_of)
1024 node->former_clone_of = node->clone_of->former_clone_of;
1025 /* Copy the OLD_VERSION_NODE function tree to the new version. */
1026 tree_function_versioning (node->clone_of->decl, node->decl,
1027 node->clone.tree_map, node->clone.param_adjustments,
1028 true, NULL, NULL);
1029 if (symtab->dump_file)
1030 {
1031 dump_function_to_file (node->clone_of->decl, symtab->dump_file,
1032 dump_flags);
1033 dump_function_to_file (node->decl, symtab->dump_file, dump_flags);
1034 }
1035
1036 /* Function is no longer clone. */
1037 if (node->next_sibling_clone)
1038 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1039 if (node->prev_sibling_clone)
1040 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1041 else
1042 node->clone_of->clones = node->next_sibling_clone;
1043 node->next_sibling_clone = NULL;
1044 node->prev_sibling_clone = NULL;
1045 if (!node->clone_of->analyzed && !node->clone_of->clones)
1046 {
1047 node->clone_of->release_body ();
1048 node->clone_of->remove_callees ();
1049 node->clone_of->remove_all_references ();
1050 }
1051 node->clone_of = NULL;
1052 bitmap_obstack_release (NULL);
1053 }
1054
1055 /* Once all functions from compilation unit are in memory, produce all clones
1056 and update all calls. We might also do this on demand if we don't want to
1057 bring all functions to memory prior compilation, but current WHOPR
1058 implementation does that and it is a bit easier to keep everything right in
1059 this order. */
1060
1061 void
1062 symbol_table::materialize_all_clones (void)
1063 {
1064 cgraph_node *node;
1065 bool stabilized = false;
1066
1067
1068 if (symtab->dump_file)
1069 fprintf (symtab->dump_file, "Materializing clones\n");
1070
1071 cgraph_node::checking_verify_cgraph_nodes ();
1072
1073 /* We can also do topological order, but number of iterations should be
1074 bounded by number of IPA passes since single IPA pass is probably not
1075 going to create clones of clones it created itself. */
1076 while (!stabilized)
1077 {
1078 stabilized = true;
1079 FOR_EACH_FUNCTION (node)
1080 {
1081 if (node->clone_of && node->decl != node->clone_of->decl
1082 && !gimple_has_body_p (node->decl))
1083 {
1084 if (!node->clone_of->clone_of)
1085 node->clone_of->get_untransformed_body ();
1086 if (gimple_has_body_p (node->clone_of->decl))
1087 {
1088 if (symtab->dump_file)
1089 {
1090 fprintf (symtab->dump_file, "cloning %s to %s\n",
1091 xstrdup_for_dump (node->clone_of->name ()),
1092 xstrdup_for_dump (node->name ()));
1093 if (node->clone.tree_map)
1094 {
1095 unsigned int i;
1096 fprintf (symtab->dump_file, " replace map: ");
1097 for (i = 0;
1098 i < vec_safe_length (node->clone.tree_map);
1099 i++)
1100 {
1101 ipa_replace_map *replace_info;
1102 replace_info = (*node->clone.tree_map)[i];
1103 fprintf (symtab->dump_file, "%i -> ",
1104 (*node->clone.tree_map)[i]->parm_num);
1105 print_generic_expr (symtab->dump_file,
1106 replace_info->new_tree);
1107 }
1108 fprintf (symtab->dump_file, "\n");
1109 }
1110 if (node->clone.param_adjustments)
1111 node->clone.param_adjustments->dump (symtab->dump_file);
1112 }
1113 cgraph_materialize_clone (node);
1114 stabilized = false;
1115 }
1116 }
1117 }
1118 }
1119 FOR_EACH_FUNCTION (node)
1120 if (!node->analyzed && node->callees)
1121 {
1122 node->remove_callees ();
1123 node->remove_all_references ();
1124 }
1125 else
1126 node->clear_stmts_in_references ();
1127 if (symtab->dump_file)
1128 fprintf (symtab->dump_file, "Materialization Call site updates done.\n");
1129
1130 cgraph_node::checking_verify_cgraph_nodes ();
1131
1132 symtab->remove_unreachable_nodes (symtab->dump_file);
1133 }
1134
1135 #include "gt-cgraphclones.h"