]>
Commit | Line | Data |
---|---|---|
564fe867 | 1 | /* Callgraph clones |
a5544970 | 2 | Copyright (C) 2003-2019 Free Software Foundation, Inc. |
564fe867 JH |
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 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" | |
c7131fb2 | 70 | #include "backend.h" |
957060b5 AM |
71 | #include "target.h" |
72 | #include "rtl.h" | |
c7131fb2 AM |
73 | #include "tree.h" |
74 | #include "gimple.h" | |
40e23961 | 75 | #include "stringpool.h" |
957060b5 | 76 | #include "cgraph.h" |
957060b5 | 77 | #include "lto-streamer.h" |
2fb9a547 | 78 | #include "tree-eh.h" |
442b4905 | 79 | #include "tree-cfg.h" |
564fe867 | 80 | #include "tree-inline.h" |
c24e924f | 81 | #include "dumpfile.h" |
564fe867 | 82 | #include "gimple-pretty-print.h" |
564fe867 | 83 | |
3dafb85c ML |
84 | /* Create clone of edge in the node N represented by CALL_EXPR |
85 | the callgraph. */ | |
86 | ||
87 | cgraph_edge * | |
538dd0b7 | 88 | cgraph_edge::clone (cgraph_node *n, gcall *call_stmt, unsigned stmt_uid, |
1511c8c0 | 89 | profile_count num, profile_count den, |
1bad9c18 | 90 | bool update_original) |
564fe867 | 91 | { |
3dafb85c | 92 | cgraph_edge *new_edge; |
1bad9c18 | 93 | profile_count::adjust_for_ipa_scaling (&num, &den); |
517048ce | 94 | profile_count prof_count = count.apply_scale (num, den); |
564fe867 | 95 | |
3dafb85c | 96 | if (indirect_unknown_callee) |
564fe867 JH |
97 | { |
98 | tree decl; | |
99 | ||
e57872ee JH |
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. */ | |
3dafb85c | 103 | && !speculative) |
564fe867 | 104 | { |
3dafb85c | 105 | cgraph_node *callee = cgraph_node::get (decl); |
564fe867 | 106 | gcc_checking_assert (callee); |
3187c8a5 | 107 | new_edge = n->create_edge (callee, call_stmt, prof_count, true); |
564fe867 JH |
108 | } |
109 | else | |
110 | { | |
d52f5295 | 111 | new_edge = n->create_indirect_edge (call_stmt, |
3dafb85c | 112 | indirect_info->ecf_flags, |
3187c8a5 | 113 | prof_count, true); |
3dafb85c | 114 | *new_edge->indirect_info = *indirect_info; |
564fe867 JH |
115 | } |
116 | } | |
117 | else | |
118 | { | |
3187c8a5 | 119 | new_edge = n->create_edge (callee, call_stmt, prof_count, true); |
3dafb85c | 120 | if (indirect_info) |
564fe867 JH |
121 | { |
122 | new_edge->indirect_info | |
766090c2 | 123 | = ggc_cleared_alloc<cgraph_indirect_call_info> (); |
3dafb85c | 124 | *new_edge->indirect_info = *indirect_info; |
564fe867 JH |
125 | } |
126 | } | |
127 | ||
3dafb85c ML |
128 | new_edge->inline_failed = inline_failed; |
129 | new_edge->indirect_inlining_edge = indirect_inlining_edge; | |
564fe867 JH |
130 | new_edge->lto_stmt_uid = stmt_uid; |
131 | /* Clone flags that depend on call_stmt availability manually. */ | |
3dafb85c ML |
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; | |
f9bb202b | 135 | new_edge->in_polymorphic_cdtor = in_polymorphic_cdtor; |
1bad9c18 JH |
136 | |
137 | /* Update IPA profile. Local profiles need no updating in original. */ | |
517048ce JH |
138 | if (update_original) |
139 | count = count.combine_with_ipa_count (count.ipa () | |
140 | - new_edge->count.ipa ()); | |
3dafb85c | 141 | symtab->call_edge_duplication_hooks (this, new_edge); |
564fe867 JH |
142 | return new_edge; |
143 | } | |
144 | ||
610c8ef0 MJ |
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; | |
610c8ef0 MJ |
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; | |
fe8e21fd ML |
158 | DECL_SET_IS_OPERATOR_NEW (new_node->decl, 0); |
159 | DECL_SET_IS_OPERATOR_DELETE (new_node->decl, 0); | |
610c8ef0 MJ |
160 | |
161 | new_node->externally_visible = 0; | |
87f94429 | 162 | new_node->local = 1; |
610c8ef0 MJ |
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 * | |
d284e1b8 | 172 | duplicate_thunk_for_node (cgraph_node *thunk, cgraph_node *node) |
610c8ef0 MJ |
173 | { |
174 | cgraph_node *new_thunk, *thunk_of; | |
d52f5295 | 175 | thunk_of = thunk->callees->callee->ultimate_alias_target (); |
610c8ef0 MJ |
176 | |
177 | if (thunk_of->thunk.thunk_p) | |
d284e1b8 | 178 | node = duplicate_thunk_for_node (thunk_of, node); |
610c8ef0 | 179 | |
48fb6d40 | 180 | if (!DECL_ARGUMENTS (thunk->decl)) |
70486010 | 181 | thunk->get_untransformed_body (); |
48fb6d40 | 182 | |
3dafb85c | 183 | cgraph_edge *cs; |
610c8ef0 MJ |
184 | for (cs = node->callers; cs; cs = cs->next_caller) |
185 | if (cs->caller->thunk.thunk_p | |
610c8ef0 | 186 | && cs->caller->thunk.fixed_offset == thunk->thunk.fixed_offset |
44662f68 EB |
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) | |
610c8ef0 MJ |
191 | return cs->caller; |
192 | ||
193 | tree new_decl; | |
ff6686d2 | 194 | if (node->clone.param_adjustments) |
610c8ef0 MJ |
195 | { |
196 | /* We do not need to duplicate this_adjusting thunks if we have removed | |
197 | this. */ | |
198 | if (thunk->thunk.this_adjusting | |
ff6686d2 | 199 | && !node->clone.param_adjustments->first_param_intact_p ()) |
610c8ef0 MJ |
200 | return node; |
201 | ||
ff6686d2 MJ |
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 (); | |
bec63208 | 206 | } |
ff6686d2 MJ |
207 | else |
208 | new_decl = copy_node (thunk->decl); | |
bec63208 | 209 | |
610c8ef0 MJ |
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 | ||
7958186b MP |
215 | DECL_NAME (new_decl) = clone_function_name_numbered (thunk->decl, |
216 | "artificial_thunk"); | |
610c8ef0 | 217 | SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl)); |
610c8ef0 | 218 | |
67124cb6 EB |
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 | ||
d52f5295 | 223 | new_thunk = cgraph_node::create (new_decl); |
610c8ef0 MJ |
224 | set_new_clone_decl_and_node_flags (new_thunk); |
225 | new_thunk->definition = true; | |
87f94429 | 226 | new_thunk->can_change_signature = node->can_change_signature; |
610c8ef0 MJ |
227 | new_thunk->thunk = thunk->thunk; |
228 | new_thunk->unique_name = in_lto_p; | |
229 | new_thunk->former_clone_of = thunk->decl; | |
ff6686d2 | 230 | new_thunk->clone.param_adjustments = node->clone.param_adjustments; |
610c8ef0 | 231 | |
1bad9c18 | 232 | cgraph_edge *e = new_thunk->create_edge (node, NULL, new_thunk->count); |
3dafb85c | 233 | symtab->call_edge_duplication_hooks (thunk->callees, e); |
3dafb85c | 234 | symtab->call_cgraph_duplication_hooks (thunk, new_thunk); |
610c8ef0 MJ |
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 | |
6a4bad95 MJ |
240 | chain. Note that it is then necessary to call |
241 | n->expand_all_artificial_thunks once all callers are redirected. */ | |
610c8ef0 MJ |
242 | |
243 | void | |
6a4bad95 | 244 | cgraph_edge::redirect_callee_duplicating_thunks (cgraph_node *n) |
610c8ef0 | 245 | { |
6a4bad95 | 246 | cgraph_node *orig_to = callee->ultimate_alias_target (); |
610c8ef0 | 247 | if (orig_to->thunk.thunk_p) |
d284e1b8 | 248 | n = duplicate_thunk_for_node (orig_to, n); |
610c8ef0 | 249 | |
6a4bad95 MJ |
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; | |
610c8ef0 | 275 | } |
564fe867 | 276 | |
0bdad123 ML |
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 | ||
564fe867 JH |
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_DUPLICATOIN_HOOK is true, the ipa passes are acknowledged about | |
44a60244 MJ |
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 | |
a62bfab5 | 312 | will see this in node's inlined_to, when invoked. Can be NULL if the |
ff6686d2 MJ |
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. */ | |
564fe867 | 318 | |
d52f5295 | 319 | cgraph_node * |
1bad9c18 | 320 | cgraph_node::create_clone (tree new_decl, profile_count prof_count, |
d52f5295 ML |
321 | bool update_original, |
322 | vec<cgraph_edge *> redirect_callers, | |
323 | bool call_duplication_hook, | |
3dafb85c | 324 | cgraph_node *new_inlined_to, |
ff6686d2 MJ |
325 | ipa_param_adjustments *param_adjustments, |
326 | const char *suffix) | |
564fe867 | 327 | { |
3dafb85c ML |
328 | cgraph_node *new_node = symtab->create_empty (); |
329 | cgraph_edge *e; | |
564fe867 | 330 | unsigned i; |
1bad9c18 | 331 | profile_count old_count = count; |
564fe867 | 332 | |
0bdad123 ML |
333 | if (new_inlined_to) |
334 | dump_callgraph_transformation (this, new_inlined_to, "inlining to"); | |
335 | ||
8f58dbd1 JH |
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); | |
3995f3a2 | 340 | new_node->count = prof_count; |
1bad9c18 JH |
341 | |
342 | /* Update IPA profile. Local profiles need no updating in original. */ | |
517048ce JH |
343 | if (update_original) |
344 | count = count.combine_with_ipa_count (count.ipa () - prof_count.ipa ()); | |
ed6ef490 | 345 | new_node->decl = new_decl; |
d52f5295 ML |
346 | new_node->register_symbol (); |
347 | new_node->origin = origin; | |
348 | new_node->lto_file_data = lto_file_data; | |
564fe867 JH |
349 | if (new_node->origin) |
350 | { | |
351 | new_node->next_nested = new_node->origin->nested; | |
352 | new_node->origin->nested = new_node; | |
353 | } | |
d52f5295 ML |
354 | new_node->analyzed = analyzed; |
355 | new_node->definition = definition; | |
87f94429 ML |
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; | |
67348ccc | 360 | new_node->externally_visible = false; |
7861b648 | 361 | new_node->no_reorder = no_reorder; |
87f94429 | 362 | new_node->local = true; |
a62bfab5 | 363 | new_node->inlined_to = new_inlined_to; |
d52f5295 | 364 | new_node->rtl = rtl; |
d52f5295 ML |
365 | new_node->frequency = frequency; |
366 | new_node->tp_first_run = tp_first_run; | |
9d6171dc | 367 | new_node->tm_clone = tm_clone; |
0a7246ee | 368 | new_node->icf_merged = icf_merged; |
88636b62 | 369 | new_node->merged_comdat = merged_comdat; |
6bbf39b7 | 370 | new_node->thunk = thunk; |
d284e1b8 | 371 | |
ff6686d2 MJ |
372 | if (param_adjustments) |
373 | new_node->clone.param_adjustments = param_adjustments; | |
374 | else | |
375 | new_node->clone.param_adjustments = clone.param_adjustments; | |
d284e1b8 | 376 | new_node->clone.tree_map = NULL; |
ff6686d2 | 377 | new_node->clone.performed_splits = vec_safe_copy (clone.performed_splits); |
ed6ef490 | 378 | new_node->split_part = split_part; |
d284e1b8 | 379 | |
9771b263 | 380 | FOR_EACH_VEC_ELT (redirect_callers, i, e) |
564fe867 JH |
381 | { |
382 | /* Redirect calls to the old version node to point to its new | |
9de6f6c3 JH |
383 | version. The only exception is when the edge was proved to |
384 | be unreachable during the clonning procedure. */ | |
385 | if (!e->callee | |
3d78e008 | 386 | || !fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE)) |
6a4bad95 | 387 | e->redirect_callee_duplicating_thunks (new_node); |
564fe867 | 388 | } |
6a4bad95 | 389 | new_node->expand_all_artificial_thunks (); |
564fe867 | 390 | |
d52f5295 | 391 | for (e = callees;e; e=e->next_callee) |
1bad9c18 JH |
392 | e->clone (new_node, e->call_stmt, e->lto_stmt_uid, new_node->count, old_count, |
393 | update_original); | |
564fe867 | 394 | |
d52f5295 | 395 | for (e = indirect_calls; e; e = e->next_callee) |
3dafb85c | 396 | e->clone (new_node, e->call_stmt, e->lto_stmt_uid, |
1bad9c18 | 397 | new_node->count, old_count, update_original); |
d52f5295 | 398 | new_node->clone_references (this); |
564fe867 | 399 | |
d52f5295 ML |
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; | |
564fe867 JH |
405 | |
406 | if (call_duplication_hook) | |
3dafb85c | 407 | symtab->call_cgraph_duplication_hooks (this, new_node); |
0bdad123 ML |
408 | |
409 | if (!new_inlined_to) | |
410 | dump_callgraph_transformation (this, new_node, suffix); | |
411 | ||
564fe867 JH |
412 | return new_node; |
413 | } | |
414 | ||
b75255a9 | 415 | static GTY(()) hash_map<const char *, unsigned> *clone_fn_ids; |
564fe867 | 416 | |
7958186b MP |
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. */ | |
9816367c | 423 | |
564fe867 | 424 | tree |
7958186b MP |
425 | clone_function_name_numbered (const char *name, const char *suffix) |
426 | { | |
b75255a9 MP |
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++); | |
7958186b MP |
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) | |
564fe867 | 458 | { |
9816367c | 459 | size_t len = strlen (name); |
564fe867 JH |
460 | char *tmp_name, *prefix; |
461 | ||
462 | prefix = XALLOCAVEC (char, len + strlen (suffix) + 2); | |
9816367c | 463 | memcpy (prefix, name, len); |
564fe867 | 464 | strcpy (prefix + len + 1, suffix); |
f8a1abf8 | 465 | prefix[len] = symbol_table::symbol_suffix_separator (); |
7958186b | 466 | ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, number); |
564fe867 JH |
467 | return get_identifier (tmp_name); |
468 | } | |
469 | ||
53aedcce MP |
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 | ||
7958186b MP |
483 | /* Return a new assembler name ending with the string SUFFIX for a |
484 | clone of DECL. */ | |
9816367c BS |
485 | |
486 | tree | |
487 | clone_function_name (tree decl, const char *suffix) | |
488 | { | |
7958186b MP |
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); | |
9816367c BS |
507 | } |
508 | ||
509 | ||
53aedcce MP |
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. | |
564fe867 JH |
513 | |
514 | TODO: after merging in ipa-sra use function call notes instead of args_to_skip | |
515 | bitmap interface. | |
516 | */ | |
3dafb85c | 517 | cgraph_node * |
d52f5295 ML |
518 | cgraph_node::create_virtual_clone (vec<cgraph_edge *> redirect_callers, |
519 | vec<ipa_replace_map *, va_gc> *tree_map, | |
ff6686d2 MJ |
520 | ipa_param_adjustments *param_adjustments, |
521 | const char * suffix, unsigned num_suffix) | |
564fe867 | 522 | { |
d52f5295 | 523 | tree old_decl = decl; |
3dafb85c | 524 | cgraph_node *new_node = NULL; |
564fe867 | 525 | tree new_decl; |
440a5082 | 526 | size_t len, i; |
3dafb85c | 527 | ipa_replace_map *map; |
440a5082 | 528 | char *name; |
564fe867 | 529 | |
87f94429 | 530 | gcc_checking_assert (versionable); |
ff6686d2 MJ |
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. */ | |
87f94429 | 534 | gcc_assert (can_change_signature || !param_adjustments); |
564fe867 JH |
535 | |
536 | /* Make a new FUNCTION_DECL tree node */ | |
ff6686d2 | 537 | if (!param_adjustments) |
564fe867 JH |
538 | new_decl = copy_node (old_decl); |
539 | else | |
ff6686d2 | 540 | new_decl = param_adjustments->adjust_decl (old_decl); |
49bde175 JH |
541 | |
542 | /* These pointers represent function body and will be populated only when clone | |
543 | is materialized. */ | |
544 | gcc_assert (new_decl != old_decl); | |
564fe867 | 545 | DECL_STRUCT_FUNCTION (new_decl) = NULL; |
49bde175 JH |
546 | DECL_ARGUMENTS (new_decl) = NULL; |
547 | DECL_INITIAL (new_decl) = NULL; | |
548 | DECL_RESULT (new_decl) = NULL; | |
67914693 | 549 | /* We cannot do DECL_RESULT (new_decl) = NULL; here because of LTO partitioning |
49bde175 | 550 | sometimes storing only clone decl instead of original. */ |
564fe867 JH |
551 | |
552 | /* Generate a new name for the new version. */ | |
440a5082 EB |
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); | |
53aedcce MP |
559 | SET_DECL_ASSEMBLER_NAME (new_decl, |
560 | clone_function_name (old_decl, suffix, num_suffix)); | |
564fe867 JH |
561 | SET_DECL_RTL (new_decl, NULL); |
562 | ||
1bad9c18 | 563 | new_node = create_clone (new_decl, count, false, |
ff6686d2 MJ |
564 | redirect_callers, false, NULL, param_adjustments, |
565 | suffix); | |
d52f5295 | 566 | |
564fe867 JH |
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. */ | |
610c8ef0 | 572 | set_new_clone_decl_and_node_flags (new_node); |
564fe867 | 573 | new_node->clone.tree_map = tree_map; |
4ab26ee0 | 574 | if (!implicit_section) |
ed89033d | 575 | new_node->set_section (get_section ()); |
702d8703 JH |
576 | |
577 | /* Clones of global symbols or symbols with unique names are unique. */ | |
578 | if ((TREE_PUBLIC (old_decl) | |
579 | && !DECL_EXTERNAL (old_decl) | |
580 | && !DECL_WEAK (old_decl) | |
581 | && !DECL_COMDAT (old_decl)) | |
582 | || in_lto_p) | |
67348ccc | 583 | new_node->unique_name = true; |
9771b263 | 584 | FOR_EACH_VEC_SAFE_ELT (tree_map, i, map) |
2d8d3ae2 | 585 | new_node->maybe_create_reference (map->new_tree, NULL); |
d284e1b8 | 586 | |
d52f5295 | 587 | if (ipa_transforms_to_apply.exists ()) |
ca860d03 | 588 | new_node->ipa_transforms_to_apply |
d52f5295 | 589 | = ipa_transforms_to_apply.copy (); |
564fe867 | 590 | |
3dafb85c | 591 | symtab->call_cgraph_duplication_hooks (this, new_node); |
564fe867 JH |
592 | |
593 | return new_node; | |
594 | } | |
595 | ||
d52f5295 ML |
596 | /* callgraph node being removed from symbol table; see if its entry can be |
597 | replaced by other inline clone. */ | |
598 | cgraph_node * | |
599 | cgraph_node::find_replacement (void) | |
564fe867 | 600 | { |
3dafb85c | 601 | cgraph_node *next_inline_clone, *replacement; |
564fe867 | 602 | |
d52f5295 | 603 | for (next_inline_clone = clones; |
564fe867 | 604 | next_inline_clone |
d52f5295 | 605 | && next_inline_clone->decl != decl; |
564fe867 JH |
606 | next_inline_clone = next_inline_clone->next_sibling_clone) |
607 | ; | |
608 | ||
609 | /* If there is inline clone of the node being removed, we need | |
610 | to put it into the position of removed node and reorganize all | |
611 | other clones to be based on it. */ | |
612 | if (next_inline_clone) | |
613 | { | |
3dafb85c ML |
614 | cgraph_node *n; |
615 | cgraph_node *new_clones; | |
564fe867 JH |
616 | |
617 | replacement = next_inline_clone; | |
618 | ||
619 | /* Unlink inline clone from the list of clones of removed node. */ | |
620 | if (next_inline_clone->next_sibling_clone) | |
621 | next_inline_clone->next_sibling_clone->prev_sibling_clone | |
622 | = next_inline_clone->prev_sibling_clone; | |
623 | if (next_inline_clone->prev_sibling_clone) | |
624 | { | |
d52f5295 | 625 | gcc_assert (clones != next_inline_clone); |
564fe867 JH |
626 | next_inline_clone->prev_sibling_clone->next_sibling_clone |
627 | = next_inline_clone->next_sibling_clone; | |
628 | } | |
629 | else | |
630 | { | |
d52f5295 ML |
631 | gcc_assert (clones == next_inline_clone); |
632 | clones = next_inline_clone->next_sibling_clone; | |
564fe867 JH |
633 | } |
634 | ||
d52f5295 ML |
635 | new_clones = clones; |
636 | clones = NULL; | |
564fe867 JH |
637 | |
638 | /* Copy clone info. */ | |
d52f5295 | 639 | next_inline_clone->clone = clone; |
564fe867 JH |
640 | |
641 | /* Now place it into clone tree at same level at NODE. */ | |
d52f5295 | 642 | next_inline_clone->clone_of = clone_of; |
564fe867 JH |
643 | next_inline_clone->prev_sibling_clone = NULL; |
644 | next_inline_clone->next_sibling_clone = NULL; | |
d52f5295 | 645 | if (clone_of) |
564fe867 | 646 | { |
d52f5295 ML |
647 | if (clone_of->clones) |
648 | clone_of->clones->prev_sibling_clone = next_inline_clone; | |
649 | next_inline_clone->next_sibling_clone = clone_of->clones; | |
650 | clone_of->clones = next_inline_clone; | |
564fe867 JH |
651 | } |
652 | ||
653 | /* Merge the clone list. */ | |
654 | if (new_clones) | |
655 | { | |
656 | if (!next_inline_clone->clones) | |
657 | next_inline_clone->clones = new_clones; | |
658 | else | |
659 | { | |
660 | n = next_inline_clone->clones; | |
661 | while (n->next_sibling_clone) | |
d52f5295 | 662 | n = n->next_sibling_clone; |
564fe867 JH |
663 | n->next_sibling_clone = new_clones; |
664 | new_clones->prev_sibling_clone = n; | |
665 | } | |
666 | } | |
667 | ||
668 | /* Update clone_of pointers. */ | |
669 | n = new_clones; | |
670 | while (n) | |
671 | { | |
672 | n->clone_of = next_inline_clone; | |
673 | n = n->next_sibling_clone; | |
674 | } | |
3c56d8d8 ML |
675 | |
676 | /* Update order in order to be able to find a LTO section | |
677 | with function body. */ | |
678 | replacement->order = order; | |
679 | ||
564fe867 JH |
680 | return replacement; |
681 | } | |
682 | else | |
683 | return NULL; | |
684 | } | |
685 | ||
686 | /* Like cgraph_set_call_stmt but walk the clone tree and update all | |
042ae7d2 JH |
687 | clones sharing the same function body. |
688 | When WHOLE_SPECULATIVE_EDGES is true, all three components of | |
689 | speculative edge gets updated. Otherwise we update only direct | |
690 | call. */ | |
564fe867 JH |
691 | |
692 | void | |
355fe088 | 693 | cgraph_node::set_call_stmt_including_clones (gimple *old_stmt, |
538dd0b7 | 694 | gcall *new_stmt, |
d52f5295 | 695 | bool update_speculative) |
564fe867 | 696 | { |
3dafb85c ML |
697 | cgraph_node *node; |
698 | cgraph_edge *edge = get_edge (old_stmt); | |
564fe867 JH |
699 | |
700 | if (edge) | |
3dafb85c | 701 | edge->set_call_stmt (new_stmt, update_speculative); |
564fe867 | 702 | |
d52f5295 | 703 | node = clones; |
564fe867 | 704 | if (node) |
d52f5295 | 705 | while (node != this) |
564fe867 | 706 | { |
3dafb85c | 707 | cgraph_edge *edge = node->get_edge (old_stmt); |
564fe867 | 708 | if (edge) |
042ae7d2 | 709 | { |
3dafb85c | 710 | edge->set_call_stmt (new_stmt, update_speculative); |
042ae7d2 JH |
711 | /* If UPDATE_SPECULATIVE is false, it means that we are turning |
712 | speculative call into a real code sequence. Update the | |
713 | callgraph edges. */ | |
714 | if (edge->speculative && !update_speculative) | |
715 | { | |
3dafb85c ML |
716 | cgraph_edge *direct, *indirect; |
717 | ipa_ref *ref; | |
042ae7d2 JH |
718 | |
719 | gcc_assert (!edge->indirect_unknown_callee); | |
3dafb85c | 720 | edge->speculative_call_info (direct, indirect, ref); |
042ae7d2 JH |
721 | direct->speculative = false; |
722 | indirect->speculative = false; | |
723 | ref->speculative = false; | |
724 | } | |
725 | } | |
564fe867 JH |
726 | if (node->clones) |
727 | node = node->clones; | |
728 | else if (node->next_sibling_clone) | |
729 | node = node->next_sibling_clone; | |
730 | else | |
731 | { | |
d52f5295 | 732 | while (node != this && !node->next_sibling_clone) |
564fe867 | 733 | node = node->clone_of; |
d52f5295 | 734 | if (node != this) |
564fe867 JH |
735 | node = node->next_sibling_clone; |
736 | } | |
737 | } | |
738 | } | |
739 | ||
740 | /* Like cgraph_create_edge walk the clone tree and update all clones sharing | |
741 | same function body. If clones already have edge for OLD_STMT; only | |
742 | update the edge same way as cgraph_set_call_stmt_including_clones does. | |
743 | ||
744 | TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative | |
745 | frequencies of the clones. */ | |
746 | ||
747 | void | |
3dafb85c | 748 | cgraph_node::create_edge_including_clones (cgraph_node *callee, |
355fe088 | 749 | gimple *old_stmt, gcall *stmt, |
3995f3a2 | 750 | profile_count count, |
d52f5295 | 751 | cgraph_inline_failed_t reason) |
564fe867 | 752 | { |
3dafb85c ML |
753 | cgraph_node *node; |
754 | cgraph_edge *edge; | |
564fe867 | 755 | |
d52f5295 | 756 | if (!get_edge (stmt)) |
564fe867 | 757 | { |
1bad9c18 | 758 | edge = create_edge (callee, stmt, count); |
564fe867 JH |
759 | edge->inline_failed = reason; |
760 | } | |
761 | ||
d52f5295 | 762 | node = clones; |
564fe867 | 763 | if (node) |
d52f5295 | 764 | while (node != this) |
ec6a1e35 JH |
765 | /* Thunk clones do not get updated while copying inline function body. */ |
766 | if (!node->thunk.thunk_p) | |
767 | { | |
768 | cgraph_edge *edge = node->get_edge (old_stmt); | |
769 | ||
770 | /* It is possible that clones already contain the edge while | |
771 | master didn't. Either we promoted indirect call into direct | |
772 | call in the clone or we are processing clones of unreachable | |
773 | master where edges has been removed. */ | |
774 | if (edge) | |
775 | edge->set_call_stmt (stmt); | |
776 | else if (! node->get_edge (stmt)) | |
777 | { | |
1bad9c18 | 778 | edge = node->create_edge (callee, stmt, count); |
ec6a1e35 JH |
779 | edge->inline_failed = reason; |
780 | } | |
564fe867 | 781 | |
ec6a1e35 JH |
782 | if (node->clones) |
783 | node = node->clones; | |
784 | else if (node->next_sibling_clone) | |
785 | node = node->next_sibling_clone; | |
786 | else | |
787 | { | |
788 | while (node != this && !node->next_sibling_clone) | |
789 | node = node->clone_of; | |
790 | if (node != this) | |
791 | node = node->next_sibling_clone; | |
792 | } | |
793 | } | |
564fe867 JH |
794 | } |
795 | ||
796 | /* Remove the node from cgraph and all inline clones inlined into it. | |
797 | Skip however removal of FORBIDDEN_NODE and return true if it needs to be | |
798 | removed. This allows to call the function from outer loop walking clone | |
799 | tree. */ | |
800 | ||
801 | bool | |
d52f5295 | 802 | cgraph_node::remove_symbol_and_inline_clones (cgraph_node *forbidden_node) |
564fe867 | 803 | { |
3dafb85c | 804 | cgraph_edge *e, *next; |
564fe867 JH |
805 | bool found = false; |
806 | ||
d52f5295 | 807 | if (this == forbidden_node) |
39f9719e | 808 | { |
3dafb85c | 809 | callers->remove (); |
39f9719e JH |
810 | return true; |
811 | } | |
d52f5295 | 812 | for (e = callees; e; e = next) |
564fe867 JH |
813 | { |
814 | next = e->next_callee; | |
815 | if (!e->inline_failed) | |
d52f5295 | 816 | found |= e->callee->remove_symbol_and_inline_clones (forbidden_node); |
564fe867 | 817 | } |
d52f5295 | 818 | remove (); |
564fe867 JH |
819 | return found; |
820 | } | |
821 | ||
822 | /* The edges representing the callers of the NEW_VERSION node were | |
823 | fixed by cgraph_function_versioning (), now the call_expr in their | |
824 | respective tree code should be updated to call the NEW_VERSION. */ | |
825 | ||
826 | static void | |
3dafb85c | 827 | update_call_expr (cgraph_node *new_version) |
564fe867 | 828 | { |
3dafb85c | 829 | cgraph_edge *e; |
564fe867 JH |
830 | |
831 | gcc_assert (new_version); | |
832 | ||
833 | /* Update the call expr on the edges to call the new version. */ | |
834 | for (e = new_version->callers; e; e = e->next_caller) | |
835 | { | |
3dafb85c | 836 | function *inner_function = DECL_STRUCT_FUNCTION (e->caller->decl); |
67348ccc | 837 | gimple_call_set_fndecl (e->call_stmt, new_version->decl); |
564fe867 JH |
838 | maybe_clean_eh_stmt_fn (inner_function, e->call_stmt); |
839 | } | |
840 | } | |
841 | ||
842 | ||
843 | /* Create a new cgraph node which is the new version of | |
d52f5295 | 844 | callgraph node. REDIRECT_CALLERS holds the callers |
564fe867 | 845 | edges which should be redirected to point to |
d52f5295 | 846 | NEW_VERSION. ALL the callees edges of the node |
564fe867 JH |
847 | are cloned to the new version node. Return the new |
848 | version node. | |
849 | ||
850 | If non-NULL BLOCK_TO_COPY determine what basic blocks | |
851 | was copied to prevent duplications of calls that are dead | |
852 | in the clone. */ | |
853 | ||
d52f5295 ML |
854 | cgraph_node * |
855 | cgraph_node::create_version_clone (tree new_decl, | |
856 | vec<cgraph_edge *> redirect_callers, | |
0bdad123 ML |
857 | bitmap bbs_to_copy, |
858 | const char *suffix) | |
564fe867 | 859 | { |
3dafb85c ML |
860 | cgraph_node *new_version; |
861 | cgraph_edge *e; | |
564fe867 JH |
862 | unsigned i; |
863 | ||
d52f5295 | 864 | new_version = cgraph_node::create (new_decl); |
564fe867 | 865 | |
d52f5295 ML |
866 | new_version->analyzed = analyzed; |
867 | new_version->definition = definition; | |
868 | new_version->local = local; | |
67348ccc | 869 | new_version->externally_visible = false; |
7861b648 | 870 | new_version->no_reorder = no_reorder; |
87f94429 | 871 | new_version->local = new_version->definition; |
a62bfab5 | 872 | new_version->inlined_to = inlined_to; |
d52f5295 ML |
873 | new_version->rtl = rtl; |
874 | new_version->count = count; | |
564fe867 | 875 | |
d52f5295 | 876 | for (e = callees; e; e=e->next_callee) |
564fe867 JH |
877 | if (!bbs_to_copy |
878 | || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index)) | |
3dafb85c | 879 | e->clone (new_version, e->call_stmt, |
1511c8c0 | 880 | e->lto_stmt_uid, count, count, |
3dafb85c | 881 | true); |
d52f5295 | 882 | for (e = indirect_calls; e; e=e->next_callee) |
564fe867 JH |
883 | if (!bbs_to_copy |
884 | || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index)) | |
3dafb85c | 885 | e->clone (new_version, e->call_stmt, |
1511c8c0 | 886 | e->lto_stmt_uid, count, count, |
3dafb85c | 887 | true); |
9771b263 | 888 | FOR_EACH_VEC_ELT (redirect_callers, i, e) |
564fe867 JH |
889 | { |
890 | /* Redirect calls to the old version node to point to its new | |
891 | version. */ | |
3dafb85c | 892 | e->redirect_callee (new_version); |
564fe867 JH |
893 | } |
894 | ||
0bdad123 ML |
895 | dump_callgraph_transformation (this, new_version, suffix); |
896 | ||
564fe867 JH |
897 | return new_version; |
898 | } | |
899 | ||
900 | /* Perform function versioning. | |
901 | Function versioning includes copying of the tree and | |
902 | a callgraph update (creating a new cgraph node and updating | |
903 | its callees and callers). | |
904 | ||
905 | REDIRECT_CALLERS varray includes the edges to be redirected | |
906 | to the new version. | |
907 | ||
908 | TREE_MAP is a mapping of tree nodes we want to replace with | |
909 | new ones (according to results of prior analysis). | |
564fe867 JH |
910 | |
911 | If non-NULL ARGS_TO_SKIP determine function parameters to remove | |
912 | from new version. | |
913 | If SKIP_RETURN is true, the new version will return void. | |
914 | If non-NULL BLOCK_TO_COPY determine what basic blocks to copy. | |
915 | If non_NULL NEW_ENTRY determine new entry BB of the clone. | |
916 | ||
5928bc2e ML |
917 | If TARGET_ATTRIBUTES is non-null, when creating a new declaration, |
918 | add the attributes to DECL_ATTRIBUTES. And call valid_attribute_p | |
919 | that will promote value of the attribute DECL_FUNCTION_SPECIFIC_TARGET | |
920 | of the declaration. | |
921 | ||
564fe867 JH |
922 | Return the new version's cgraph node. */ |
923 | ||
d52f5295 ML |
924 | cgraph_node * |
925 | cgraph_node::create_version_clone_with_body | |
926 | (vec<cgraph_edge *> redirect_callers, | |
ff6686d2 MJ |
927 | vec<ipa_replace_map *, va_gc> *tree_map, |
928 | ipa_param_adjustments *param_adjustments, | |
929 | bitmap bbs_to_copy, basic_block new_entry_block, const char *suffix, | |
930 | tree target_attributes) | |
564fe867 | 931 | { |
d52f5295 | 932 | tree old_decl = decl; |
3dafb85c | 933 | cgraph_node *new_version_node = NULL; |
564fe867 JH |
934 | tree new_decl; |
935 | ||
936 | if (!tree_versionable_function_p (old_decl)) | |
937 | return NULL; | |
938 | ||
ff6686d2 | 939 | /* TODO: Restore an assert that we do not change signature if |
87f94429 | 940 | can_change_signature is false. We cannot just check that |
ff6686d2 MJ |
941 | param_adjustments is NULL because unfortunately ipa-split removes return |
942 | values from such functions. */ | |
564fe867 JH |
943 | |
944 | /* Make a new FUNCTION_DECL tree node for the new version. */ | |
ff6686d2 MJ |
945 | if (param_adjustments) |
946 | new_decl = param_adjustments->adjust_decl (old_decl); | |
564fe867 | 947 | else |
ff6686d2 | 948 | new_decl = copy_node (old_decl); |
564fe867 JH |
949 | |
950 | /* Generate a new name for the new version. */ | |
7958186b | 951 | DECL_NAME (new_decl) = clone_function_name_numbered (old_decl, suffix); |
564fe867 JH |
952 | SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl)); |
953 | SET_DECL_RTL (new_decl, NULL); | |
954 | ||
97ae2126 JH |
955 | DECL_VIRTUAL_P (new_decl) = 0; |
956 | ||
5928bc2e ML |
957 | if (target_attributes) |
958 | { | |
959 | DECL_ATTRIBUTES (new_decl) = target_attributes; | |
960 | ||
961 | location_t saved_loc = input_location; | |
962 | tree v = TREE_VALUE (target_attributes); | |
963 | input_location = DECL_SOURCE_LOCATION (new_decl); | |
cc2a672a | 964 | bool r = targetm.target_option.valid_attribute_p (new_decl, NULL, v, 1); |
5928bc2e ML |
965 | input_location = saved_loc; |
966 | if (!r) | |
967 | return NULL; | |
968 | } | |
969 | ||
564fe867 | 970 | /* When the old decl was a con-/destructor make sure the clone isn't. */ |
c3284718 RS |
971 | DECL_STATIC_CONSTRUCTOR (new_decl) = 0; |
972 | DECL_STATIC_DESTRUCTOR (new_decl) = 0; | |
fe8e21fd ML |
973 | DECL_SET_IS_OPERATOR_NEW (new_decl, 0); |
974 | DECL_SET_IS_OPERATOR_DELETE (new_decl, 0); | |
564fe867 JH |
975 | |
976 | /* Create the new version's call-graph node. | |
977 | and update the edges of the new node. */ | |
d52f5295 | 978 | new_version_node = create_version_clone (new_decl, redirect_callers, |
0bdad123 | 979 | bbs_to_copy, suffix); |
564fe867 | 980 | |
d52f5295 | 981 | if (ipa_transforms_to_apply.exists ()) |
c6d43074 | 982 | new_version_node->ipa_transforms_to_apply |
d52f5295 | 983 | = ipa_transforms_to_apply.copy (); |
564fe867 | 984 | /* Copy the OLD_VERSION_NODE function tree to the new version. */ |
ff6686d2 MJ |
985 | tree_function_versioning (old_decl, new_decl, tree_map, param_adjustments, |
986 | false, bbs_to_copy, new_entry_block); | |
564fe867 JH |
987 | |
988 | /* Update the new version's properties. | |
989 | Make The new version visible only within this translation unit. Make sure | |
990 | that is not weak also. | |
991 | ??? We cannot use COMDAT linkage because there is no | |
992 | ABI support for this. */ | |
d52f5295 | 993 | new_version_node->make_decl_local (); |
67348ccc DM |
994 | DECL_VIRTUAL_P (new_version_node->decl) = 0; |
995 | new_version_node->externally_visible = 0; | |
87f94429 | 996 | new_version_node->local = 1; |
564fe867 | 997 | new_version_node->lowered = true; |
4ab26ee0 JJ |
998 | if (!implicit_section) |
999 | new_version_node->set_section (get_section ()); | |
702d8703 JH |
1000 | /* Clones of global symbols or symbols with unique names are unique. */ |
1001 | if ((TREE_PUBLIC (old_decl) | |
1002 | && !DECL_EXTERNAL (old_decl) | |
1003 | && !DECL_WEAK (old_decl) | |
1004 | && !DECL_COMDAT (old_decl)) | |
1005 | || in_lto_p) | |
67348ccc | 1006 | new_version_node->unique_name = true; |
564fe867 JH |
1007 | |
1008 | /* Update the call_expr on the edges to call the new version node. */ | |
1009 | update_call_expr (new_version_node); | |
1010 | ||
cbb4e4ca | 1011 | symtab->call_cgraph_insertion_hooks (new_version_node); |
564fe867 JH |
1012 | return new_version_node; |
1013 | } | |
1014 | ||
1015 | /* Given virtual clone, turn it into actual clone. */ | |
1016 | ||
1017 | static void | |
3dafb85c | 1018 | cgraph_materialize_clone (cgraph_node *node) |
564fe867 JH |
1019 | { |
1020 | bitmap_obstack_initialize (NULL); | |
67348ccc | 1021 | node->former_clone_of = node->clone_of->decl; |
564fe867 JH |
1022 | if (node->clone_of->former_clone_of) |
1023 | node->former_clone_of = node->clone_of->former_clone_of; | |
1024 | /* Copy the OLD_VERSION_NODE function tree to the new version. */ | |
67348ccc | 1025 | tree_function_versioning (node->clone_of->decl, node->decl, |
ff6686d2 MJ |
1026 | node->clone.tree_map, node->clone.param_adjustments, |
1027 | true, NULL, NULL); | |
3dafb85c | 1028 | if (symtab->dump_file) |
564fe867 | 1029 | { |
3dafb85c ML |
1030 | dump_function_to_file (node->clone_of->decl, symtab->dump_file, |
1031 | dump_flags); | |
1032 | dump_function_to_file (node->decl, symtab->dump_file, dump_flags); | |
564fe867 JH |
1033 | } |
1034 | ||
1035 | /* Function is no longer clone. */ | |
1036 | if (node->next_sibling_clone) | |
1037 | node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone; | |
1038 | if (node->prev_sibling_clone) | |
1039 | node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone; | |
1040 | else | |
1041 | node->clone_of->clones = node->next_sibling_clone; | |
1042 | node->next_sibling_clone = NULL; | |
1043 | node->prev_sibling_clone = NULL; | |
67348ccc | 1044 | if (!node->clone_of->analyzed && !node->clone_of->clones) |
564fe867 | 1045 | { |
d52f5295 ML |
1046 | node->clone_of->release_body (); |
1047 | node->clone_of->remove_callees (); | |
d122681a | 1048 | node->clone_of->remove_all_references (); |
564fe867 JH |
1049 | } |
1050 | node->clone_of = NULL; | |
1051 | bitmap_obstack_release (NULL); | |
1052 | } | |
1053 | ||
1054 | /* Once all functions from compilation unit are in memory, produce all clones | |
1055 | and update all calls. We might also do this on demand if we don't want to | |
1056 | bring all functions to memory prior compilation, but current WHOPR | |
026c3cfd | 1057 | implementation does that and it is a bit easier to keep everything right in |
564fe867 JH |
1058 | this order. */ |
1059 | ||
1060 | void | |
3dafb85c | 1061 | symbol_table::materialize_all_clones (void) |
564fe867 | 1062 | { |
3dafb85c | 1063 | cgraph_node *node; |
564fe867 | 1064 | bool stabilized = false; |
042ae7d2 | 1065 | |
564fe867 | 1066 | |
3dafb85c ML |
1067 | if (symtab->dump_file) |
1068 | fprintf (symtab->dump_file, "Materializing clones\n"); | |
b2b29377 MM |
1069 | |
1070 | cgraph_node::checking_verify_cgraph_nodes (); | |
564fe867 JH |
1071 | |
1072 | /* We can also do topological order, but number of iterations should be | |
1073 | bounded by number of IPA passes since single IPA pass is probably not | |
1074 | going to create clones of clones it created itself. */ | |
1075 | while (!stabilized) | |
1076 | { | |
1077 | stabilized = true; | |
1078 | FOR_EACH_FUNCTION (node) | |
1079 | { | |
67348ccc DM |
1080 | if (node->clone_of && node->decl != node->clone_of->decl |
1081 | && !gimple_has_body_p (node->decl)) | |
564fe867 | 1082 | { |
a2e2a668 | 1083 | if (!node->clone_of->clone_of) |
70486010 | 1084 | node->clone_of->get_untransformed_body (); |
67348ccc | 1085 | if (gimple_has_body_p (node->clone_of->decl)) |
564fe867 | 1086 | { |
3dafb85c | 1087 | if (symtab->dump_file) |
564fe867 | 1088 | { |
3dafb85c | 1089 | fprintf (symtab->dump_file, "cloning %s to %s\n", |
2a72a953 DM |
1090 | xstrdup_for_dump (node->clone_of->name ()), |
1091 | xstrdup_for_dump (node->name ())); | |
564fe867 JH |
1092 | if (node->clone.tree_map) |
1093 | { | |
1094 | unsigned int i; | |
3dafb85c | 1095 | fprintf (symtab->dump_file, " replace map: "); |
9771b263 DN |
1096 | for (i = 0; |
1097 | i < vec_safe_length (node->clone.tree_map); | |
1098 | i++) | |
564fe867 | 1099 | { |
3dafb85c | 1100 | ipa_replace_map *replace_info; |
9771b263 | 1101 | replace_info = (*node->clone.tree_map)[i]; |
ff6686d2 MJ |
1102 | fprintf (symtab->dump_file, "%i -> ", |
1103 | (*node->clone.tree_map)[i]->parm_num); | |
ef6cb4c7 ML |
1104 | print_generic_expr (symtab->dump_file, |
1105 | replace_info->new_tree); | |
564fe867 | 1106 | } |
3dafb85c | 1107 | fprintf (symtab->dump_file, "\n"); |
564fe867 | 1108 | } |
ff6686d2 MJ |
1109 | if (node->clone.param_adjustments) |
1110 | node->clone.param_adjustments->dump (symtab->dump_file); | |
564fe867 JH |
1111 | } |
1112 | cgraph_materialize_clone (node); | |
1113 | stabilized = false; | |
1114 | } | |
1115 | } | |
1116 | } | |
1117 | } | |
1118 | FOR_EACH_FUNCTION (node) | |
67348ccc | 1119 | if (!node->analyzed && node->callees) |
71cafea9 | 1120 | { |
d52f5295 | 1121 | node->remove_callees (); |
d122681a | 1122 | node->remove_all_references (); |
71cafea9 JH |
1123 | } |
1124 | else | |
d122681a | 1125 | node->clear_stmts_in_references (); |
3dafb85c ML |
1126 | if (symtab->dump_file) |
1127 | fprintf (symtab->dump_file, "Materialization Call site updates done.\n"); | |
b2b29377 MM |
1128 | |
1129 | cgraph_node::checking_verify_cgraph_nodes (); | |
1130 | ||
17e0fc92 | 1131 | symtab->remove_unreachable_nodes (symtab->dump_file); |
564fe867 JH |
1132 | } |
1133 | ||
1134 | #include "gt-cgraphclones.h" |