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