]>
Commit | Line | Data |
---|---|---|
c02159a7 | 1 | /* Callgraph clones |
2 | Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, | |
3 | 2011, 2012 Free Software Foundation, Inc. | |
4 | Contributed by Jan Hubicka | |
5 | ||
6 | This file is part of GCC. | |
7 | ||
8 | GCC is free software; you can redistribute it and/or modify it under | |
9 | the terms of the GNU General Public License as published by the Free | |
10 | Software Foundation; either version 3, or (at your option) any later | |
11 | version. | |
12 | ||
13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GCC; see the file COPYING3. If not see | |
20 | <http://www.gnu.org/licenses/>. */ | |
21 | ||
22 | /* This module provide facilities for clonning functions. I.e. creating | |
23 | new functions based on existing functions with simple modifications, | |
24 | such as replacement of parameters. | |
25 | ||
26 | To allow whole program optimization without actual presence of function | |
27 | bodies, an additional infrastructure is provided for so-called virtual | |
28 | clones | |
29 | ||
30 | A virtual clone in the callgraph is a function that has no | |
31 | associated body, just a description of how to create its body based | |
32 | on a different function (which itself may be a virtual clone). | |
33 | ||
34 | The description of function modifications includes adjustments to | |
35 | the function's signature (which allows, for example, removing or | |
36 | adding function arguments), substitutions to perform on the | |
37 | function body, and, for inlined functions, a pointer to the | |
38 | function that it will be inlined into. | |
39 | ||
40 | It is also possible to redirect any edge of the callgraph from a | |
41 | function to its virtual clone. This implies updating of the call | |
42 | site to adjust for the new function signature. | |
43 | ||
44 | Most of the transformations performed by inter-procedural | |
45 | optimizations can be represented via virtual clones. For | |
46 | instance, a constant propagation pass can produce a virtual clone | |
47 | of the function which replaces one of its arguments by a | |
48 | constant. The inliner can represent its decisions by producing a | |
49 | clone of a function whose body will be later integrated into | |
50 | a given function. | |
51 | ||
52 | Using virtual clones, the program can be easily updated | |
53 | during the Execute stage, solving most of pass interactions | |
54 | problems that would otherwise occur during Transform. | |
55 | ||
56 | Virtual clones are later materialized in the LTRANS stage and | |
57 | turned into real functions. Passes executed after the virtual | |
58 | clone were introduced also perform their Transform stage | |
59 | on new functions, so for a pass there is no significant | |
60 | difference between operating on a real function or a virtual | |
61 | clone introduced before its Execute stage. | |
62 | ||
63 | Optimization passes then work on virtual clones introduced before | |
64 | their Execute stage as if they were real functions. The | |
65 | only difference is that clones are not visible during the | |
66 | Generate Summary stage. */ | |
67 | ||
68 | #include "config.h" | |
69 | #include "system.h" | |
70 | #include "coretypes.h" | |
71 | #include "tm.h" | |
72 | #include "tree.h" | |
c02159a7 | 73 | #include "rtl.h" |
74 | #include "tree-flow.h" | |
75 | #include "tree-inline.h" | |
76 | #include "langhooks.h" | |
77 | #include "pointer-set.h" | |
78 | #include "toplev.h" | |
79 | #include "flags.h" | |
80 | #include "ggc.h" | |
81 | #include "debug.h" | |
82 | #include "target.h" | |
83 | #include "cgraph.h" | |
84 | #include "diagnostic.h" | |
85 | #include "timevar.h" | |
86 | #include "params.h" | |
87 | #include "fibheap.h" | |
88 | #include "intl.h" | |
89 | #include "function.h" | |
90 | #include "ipa-prop.h" | |
91 | #include "gimple.h" | |
92 | #include "tree-iterator.h" | |
93 | #include "tree-pass.h" | |
94 | #include "tree-dump.h" | |
95 | #include "gimple-pretty-print.h" | |
96 | #include "output.h" | |
97 | #include "coverage.h" | |
c02159a7 | 98 | #include "ipa-inline.h" |
99 | #include "ipa-utils.h" | |
100 | #include "lto-streamer.h" | |
101 | #include "except.h" | |
102 | ||
103 | /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */ | |
104 | struct cgraph_edge * | |
105 | cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n, | |
106 | gimple call_stmt, unsigned stmt_uid, gcov_type count_scale, | |
107 | int freq_scale, bool update_original) | |
108 | { | |
109 | struct cgraph_edge *new_edge; | |
110 | gcov_type count = e->count * count_scale / REG_BR_PROB_BASE; | |
111 | gcov_type freq; | |
112 | ||
113 | /* We do not want to ignore loop nest after frequency drops to 0. */ | |
114 | if (!freq_scale) | |
115 | freq_scale = 1; | |
116 | freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE; | |
117 | if (freq > CGRAPH_FREQ_MAX) | |
118 | freq = CGRAPH_FREQ_MAX; | |
119 | ||
120 | if (e->indirect_unknown_callee) | |
121 | { | |
122 | tree decl; | |
123 | ||
124 | if (call_stmt && (decl = gimple_call_fndecl (call_stmt))) | |
125 | { | |
126 | struct cgraph_node *callee = cgraph_get_node (decl); | |
127 | gcc_checking_assert (callee); | |
128 | new_edge = cgraph_create_edge (n, callee, call_stmt, count, freq); | |
129 | } | |
130 | else | |
131 | { | |
132 | new_edge = cgraph_create_indirect_edge (n, call_stmt, | |
133 | e->indirect_info->ecf_flags, | |
134 | count, freq); | |
135 | *new_edge->indirect_info = *e->indirect_info; | |
136 | } | |
137 | } | |
138 | else | |
139 | { | |
140 | new_edge = cgraph_create_edge (n, e->callee, call_stmt, count, freq); | |
141 | if (e->indirect_info) | |
142 | { | |
143 | new_edge->indirect_info | |
144 | = ggc_alloc_cleared_cgraph_indirect_call_info (); | |
145 | *new_edge->indirect_info = *e->indirect_info; | |
146 | } | |
147 | } | |
148 | ||
149 | new_edge->inline_failed = e->inline_failed; | |
150 | new_edge->indirect_inlining_edge = e->indirect_inlining_edge; | |
151 | new_edge->lto_stmt_uid = stmt_uid; | |
152 | /* Clone flags that depend on call_stmt availability manually. */ | |
153 | new_edge->can_throw_external = e->can_throw_external; | |
154 | new_edge->call_stmt_cannot_inline_p = e->call_stmt_cannot_inline_p; | |
155 | if (update_original) | |
156 | { | |
157 | e->count -= new_edge->count; | |
158 | if (e->count < 0) | |
159 | e->count = 0; | |
160 | } | |
161 | cgraph_call_edge_duplication_hooks (e, new_edge); | |
162 | return new_edge; | |
163 | } | |
164 | ||
165 | ||
166 | /* Create node representing clone of N executed COUNT times. Decrease | |
167 | the execution counts from original node too. | |
168 | The new clone will have decl set to DECL that may or may not be the same | |
169 | as decl of N. | |
170 | ||
171 | When UPDATE_ORIGINAL is true, the counts are subtracted from the original | |
172 | function's profile to reflect the fact that part of execution is handled | |
173 | by node. | |
174 | When CALL_DUPLICATOIN_HOOK is true, the ipa passes are acknowledged about | |
175 | the new clone. Otherwise the caller is responsible for doing so later. */ | |
176 | ||
177 | struct cgraph_node * | |
178 | cgraph_clone_node (struct cgraph_node *n, tree decl, gcov_type count, int freq, | |
179 | bool update_original, | |
180 | VEC(cgraph_edge_p,heap) *redirect_callers, | |
181 | bool call_duplication_hook) | |
182 | { | |
183 | struct cgraph_node *new_node = cgraph_create_empty_node (); | |
184 | struct cgraph_edge *e; | |
185 | gcov_type count_scale; | |
186 | unsigned i; | |
187 | ||
188 | new_node->symbol.decl = decl; | |
189 | symtab_register_node ((symtab_node)new_node); | |
190 | new_node->origin = n->origin; | |
191 | if (new_node->origin) | |
192 | { | |
193 | new_node->next_nested = new_node->origin->nested; | |
194 | new_node->origin->nested = new_node; | |
195 | } | |
196 | new_node->analyzed = n->analyzed; | |
197 | new_node->local = n->local; | |
198 | new_node->symbol.externally_visible = false; | |
199 | new_node->local.local = true; | |
200 | new_node->global = n->global; | |
201 | new_node->rtl = n->rtl; | |
202 | new_node->count = count; | |
203 | new_node->frequency = n->frequency; | |
204 | new_node->clone = n->clone; | |
205 | new_node->clone.tree_map = 0; | |
206 | if (n->count) | |
207 | { | |
208 | if (new_node->count > n->count) | |
209 | count_scale = REG_BR_PROB_BASE; | |
210 | else | |
211 | count_scale = new_node->count * REG_BR_PROB_BASE / n->count; | |
212 | } | |
213 | else | |
214 | count_scale = 0; | |
215 | if (update_original) | |
216 | { | |
217 | n->count -= count; | |
218 | if (n->count < 0) | |
219 | n->count = 0; | |
220 | } | |
221 | ||
222 | FOR_EACH_VEC_ELT (cgraph_edge_p, redirect_callers, i, e) | |
223 | { | |
224 | /* Redirect calls to the old version node to point to its new | |
225 | version. */ | |
226 | cgraph_redirect_edge_callee (e, new_node); | |
227 | } | |
228 | ||
229 | ||
230 | for (e = n->callees;e; e=e->next_callee) | |
231 | cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid, | |
232 | count_scale, freq, update_original); | |
233 | ||
234 | for (e = n->indirect_calls; e; e = e->next_callee) | |
235 | cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid, | |
236 | count_scale, freq, update_original); | |
237 | ipa_clone_references ((symtab_node)new_node, &n->symbol.ref_list); | |
238 | ||
239 | new_node->next_sibling_clone = n->clones; | |
240 | if (n->clones) | |
241 | n->clones->prev_sibling_clone = new_node; | |
242 | n->clones = new_node; | |
243 | new_node->clone_of = n; | |
244 | ||
245 | if (call_duplication_hook) | |
246 | cgraph_call_node_duplication_hooks (n, new_node); | |
247 | return new_node; | |
248 | } | |
249 | ||
250 | /* Create a new name for clone of DECL, add SUFFIX. Returns an identifier. */ | |
251 | ||
252 | static GTY(()) unsigned int clone_fn_id_num; | |
253 | ||
254 | tree | |
255 | clone_function_name (tree decl, const char *suffix) | |
256 | { | |
257 | tree name = DECL_ASSEMBLER_NAME (decl); | |
258 | size_t len = IDENTIFIER_LENGTH (name); | |
259 | char *tmp_name, *prefix; | |
260 | ||
261 | prefix = XALLOCAVEC (char, len + strlen (suffix) + 2); | |
262 | memcpy (prefix, IDENTIFIER_POINTER (name), len); | |
263 | strcpy (prefix + len + 1, suffix); | |
264 | #ifndef NO_DOT_IN_LABEL | |
265 | prefix[len] = '.'; | |
266 | #elif !defined NO_DOLLAR_IN_LABEL | |
267 | prefix[len] = '$'; | |
268 | #else | |
269 | prefix[len] = '_'; | |
270 | #endif | |
271 | ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++); | |
272 | return get_identifier (tmp_name); | |
273 | } | |
274 | ||
275 | /* Create callgraph node clone with new declaration. The actual body will | |
276 | be copied later at compilation stage. | |
277 | ||
278 | TODO: after merging in ipa-sra use function call notes instead of args_to_skip | |
279 | bitmap interface. | |
280 | */ | |
281 | struct cgraph_node * | |
282 | cgraph_create_virtual_clone (struct cgraph_node *old_node, | |
283 | VEC(cgraph_edge_p,heap) *redirect_callers, | |
284 | VEC(ipa_replace_map_p,gc) *tree_map, | |
285 | bitmap args_to_skip, | |
286 | const char * suffix) | |
287 | { | |
288 | tree old_decl = old_node->symbol.decl; | |
289 | struct cgraph_node *new_node = NULL; | |
290 | tree new_decl; | |
291 | size_t i; | |
292 | struct ipa_replace_map *map; | |
293 | ||
294 | if (!flag_wpa) | |
295 | gcc_checking_assert (tree_versionable_function_p (old_decl)); | |
296 | ||
297 | gcc_assert (old_node->local.can_change_signature || !args_to_skip); | |
298 | ||
299 | /* Make a new FUNCTION_DECL tree node */ | |
300 | if (!args_to_skip) | |
301 | new_decl = copy_node (old_decl); | |
302 | else | |
303 | new_decl = build_function_decl_skip_args (old_decl, args_to_skip, false); | |
304 | DECL_STRUCT_FUNCTION (new_decl) = NULL; | |
305 | ||
306 | /* Generate a new name for the new version. */ | |
307 | DECL_NAME (new_decl) = clone_function_name (old_decl, suffix); | |
308 | SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl)); | |
309 | SET_DECL_RTL (new_decl, NULL); | |
310 | ||
311 | new_node = cgraph_clone_node (old_node, new_decl, old_node->count, | |
312 | CGRAPH_FREQ_BASE, false, | |
313 | redirect_callers, false); | |
314 | /* Update the properties. | |
315 | Make clone visible only within this translation unit. Make sure | |
316 | that is not weak also. | |
317 | ??? We cannot use COMDAT linkage because there is no | |
318 | ABI support for this. */ | |
319 | DECL_EXTERNAL (new_node->symbol.decl) = 0; | |
320 | if (DECL_ONE_ONLY (old_decl)) | |
321 | DECL_SECTION_NAME (new_node->symbol.decl) = NULL; | |
322 | DECL_COMDAT_GROUP (new_node->symbol.decl) = 0; | |
323 | TREE_PUBLIC (new_node->symbol.decl) = 0; | |
324 | DECL_COMDAT (new_node->symbol.decl) = 0; | |
325 | DECL_WEAK (new_node->symbol.decl) = 0; | |
326 | DECL_STATIC_CONSTRUCTOR (new_node->symbol.decl) = 0; | |
327 | DECL_STATIC_DESTRUCTOR (new_node->symbol.decl) = 0; | |
328 | new_node->clone.tree_map = tree_map; | |
329 | new_node->clone.args_to_skip = args_to_skip; | |
330 | FOR_EACH_VEC_ELT (ipa_replace_map_p, tree_map, i, map) | |
331 | { | |
332 | tree var = map->new_tree; | |
333 | symtab_node ref_node; | |
334 | ||
335 | STRIP_NOPS (var); | |
336 | if (TREE_CODE (var) != ADDR_EXPR) | |
337 | continue; | |
338 | var = get_base_var (var); | |
339 | if (!var) | |
340 | continue; | |
341 | if (TREE_CODE (var) != FUNCTION_DECL | |
342 | && TREE_CODE (var) != VAR_DECL) | |
343 | continue; | |
344 | ||
345 | /* Record references of the future statement initializing the constant | |
346 | argument. */ | |
347 | ref_node = symtab_get_node (var); | |
348 | gcc_checking_assert (ref_node); | |
349 | ipa_record_reference ((symtab_node)new_node, (symtab_node)ref_node, | |
350 | IPA_REF_ADDR, NULL); | |
351 | } | |
352 | if (!args_to_skip) | |
353 | new_node->clone.combined_args_to_skip = old_node->clone.combined_args_to_skip; | |
354 | else if (old_node->clone.combined_args_to_skip) | |
355 | { | |
356 | int newi = 0, oldi = 0; | |
357 | tree arg; | |
358 | bitmap new_args_to_skip = BITMAP_GGC_ALLOC (); | |
359 | struct cgraph_node *orig_node; | |
360 | for (orig_node = old_node; orig_node->clone_of; orig_node = orig_node->clone_of) | |
361 | ; | |
362 | for (arg = DECL_ARGUMENTS (orig_node->symbol.decl); | |
363 | arg; arg = DECL_CHAIN (arg), oldi++) | |
364 | { | |
365 | if (bitmap_bit_p (old_node->clone.combined_args_to_skip, oldi)) | |
366 | { | |
367 | bitmap_set_bit (new_args_to_skip, oldi); | |
368 | continue; | |
369 | } | |
370 | if (bitmap_bit_p (args_to_skip, newi)) | |
371 | bitmap_set_bit (new_args_to_skip, oldi); | |
372 | newi++; | |
373 | } | |
374 | new_node->clone.combined_args_to_skip = new_args_to_skip; | |
375 | } | |
376 | else | |
377 | new_node->clone.combined_args_to_skip = args_to_skip; | |
378 | new_node->symbol.externally_visible = 0; | |
379 | new_node->local.local = 1; | |
380 | new_node->lowered = true; | |
381 | ||
382 | cgraph_call_node_duplication_hooks (old_node, new_node); | |
383 | ||
384 | ||
385 | return new_node; | |
386 | } | |
387 | ||
388 | /* NODE is being removed from symbol table; see if its entry can be replaced by | |
389 | other inline clone. */ | |
390 | struct cgraph_node * | |
391 | cgraph_find_replacement_node (struct cgraph_node *node) | |
392 | { | |
393 | struct cgraph_node *next_inline_clone, *replacement; | |
394 | ||
395 | for (next_inline_clone = node->clones; | |
396 | next_inline_clone | |
397 | && next_inline_clone->symbol.decl != node->symbol.decl; | |
398 | next_inline_clone = next_inline_clone->next_sibling_clone) | |
399 | ; | |
400 | ||
401 | /* If there is inline clone of the node being removed, we need | |
402 | to put it into the position of removed node and reorganize all | |
403 | other clones to be based on it. */ | |
404 | if (next_inline_clone) | |
405 | { | |
406 | struct cgraph_node *n; | |
407 | struct cgraph_node *new_clones; | |
408 | ||
409 | replacement = next_inline_clone; | |
410 | ||
411 | /* Unlink inline clone from the list of clones of removed node. */ | |
412 | if (next_inline_clone->next_sibling_clone) | |
413 | next_inline_clone->next_sibling_clone->prev_sibling_clone | |
414 | = next_inline_clone->prev_sibling_clone; | |
415 | if (next_inline_clone->prev_sibling_clone) | |
416 | { | |
417 | gcc_assert (node->clones != next_inline_clone); | |
418 | next_inline_clone->prev_sibling_clone->next_sibling_clone | |
419 | = next_inline_clone->next_sibling_clone; | |
420 | } | |
421 | else | |
422 | { | |
423 | gcc_assert (node->clones == next_inline_clone); | |
424 | node->clones = next_inline_clone->next_sibling_clone; | |
425 | } | |
426 | ||
427 | new_clones = node->clones; | |
428 | node->clones = NULL; | |
429 | ||
430 | /* Copy clone info. */ | |
431 | next_inline_clone->clone = node->clone; | |
432 | ||
433 | /* Now place it into clone tree at same level at NODE. */ | |
434 | next_inline_clone->clone_of = node->clone_of; | |
435 | next_inline_clone->prev_sibling_clone = NULL; | |
436 | next_inline_clone->next_sibling_clone = NULL; | |
437 | if (node->clone_of) | |
438 | { | |
439 | if (node->clone_of->clones) | |
440 | node->clone_of->clones->prev_sibling_clone = next_inline_clone; | |
441 | next_inline_clone->next_sibling_clone = node->clone_of->clones; | |
442 | node->clone_of->clones = next_inline_clone; | |
443 | } | |
444 | ||
445 | /* Merge the clone list. */ | |
446 | if (new_clones) | |
447 | { | |
448 | if (!next_inline_clone->clones) | |
449 | next_inline_clone->clones = new_clones; | |
450 | else | |
451 | { | |
452 | n = next_inline_clone->clones; | |
453 | while (n->next_sibling_clone) | |
454 | n = n->next_sibling_clone; | |
455 | n->next_sibling_clone = new_clones; | |
456 | new_clones->prev_sibling_clone = n; | |
457 | } | |
458 | } | |
459 | ||
460 | /* Update clone_of pointers. */ | |
461 | n = new_clones; | |
462 | while (n) | |
463 | { | |
464 | n->clone_of = next_inline_clone; | |
465 | n = n->next_sibling_clone; | |
466 | } | |
467 | return replacement; | |
468 | } | |
469 | else | |
470 | return NULL; | |
471 | } | |
472 | ||
473 | /* Like cgraph_set_call_stmt but walk the clone tree and update all | |
474 | clones sharing the same function body. */ | |
475 | ||
476 | void | |
477 | cgraph_set_call_stmt_including_clones (struct cgraph_node *orig, | |
478 | gimple old_stmt, gimple new_stmt) | |
479 | { | |
480 | struct cgraph_node *node; | |
481 | struct cgraph_edge *edge = cgraph_edge (orig, old_stmt); | |
482 | ||
483 | if (edge) | |
484 | cgraph_set_call_stmt (edge, new_stmt); | |
485 | ||
486 | node = orig->clones; | |
487 | if (node) | |
488 | while (node != orig) | |
489 | { | |
490 | struct cgraph_edge *edge = cgraph_edge (node, old_stmt); | |
491 | if (edge) | |
492 | cgraph_set_call_stmt (edge, new_stmt); | |
493 | if (node->clones) | |
494 | node = node->clones; | |
495 | else if (node->next_sibling_clone) | |
496 | node = node->next_sibling_clone; | |
497 | else | |
498 | { | |
499 | while (node != orig && !node->next_sibling_clone) | |
500 | node = node->clone_of; | |
501 | if (node != orig) | |
502 | node = node->next_sibling_clone; | |
503 | } | |
504 | } | |
505 | } | |
506 | ||
507 | /* Like cgraph_create_edge walk the clone tree and update all clones sharing | |
508 | same function body. If clones already have edge for OLD_STMT; only | |
509 | update the edge same way as cgraph_set_call_stmt_including_clones does. | |
510 | ||
511 | TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative | |
512 | frequencies of the clones. */ | |
513 | ||
514 | void | |
515 | cgraph_create_edge_including_clones (struct cgraph_node *orig, | |
516 | struct cgraph_node *callee, | |
517 | gimple old_stmt, | |
518 | gimple stmt, gcov_type count, | |
519 | int freq, | |
520 | cgraph_inline_failed_t reason) | |
521 | { | |
522 | struct cgraph_node *node; | |
523 | struct cgraph_edge *edge; | |
524 | ||
525 | if (!cgraph_edge (orig, stmt)) | |
526 | { | |
527 | edge = cgraph_create_edge (orig, callee, stmt, count, freq); | |
528 | edge->inline_failed = reason; | |
529 | } | |
530 | ||
531 | node = orig->clones; | |
532 | if (node) | |
533 | while (node != orig) | |
534 | { | |
535 | struct cgraph_edge *edge = cgraph_edge (node, old_stmt); | |
536 | ||
537 | /* It is possible that clones already contain the edge while | |
538 | master didn't. Either we promoted indirect call into direct | |
539 | call in the clone or we are processing clones of unreachable | |
540 | master where edges has been removed. */ | |
541 | if (edge) | |
542 | cgraph_set_call_stmt (edge, stmt); | |
543 | else if (!cgraph_edge (node, stmt)) | |
544 | { | |
545 | edge = cgraph_create_edge (node, callee, stmt, count, | |
546 | freq); | |
547 | edge->inline_failed = reason; | |
548 | } | |
549 | ||
550 | if (node->clones) | |
551 | node = node->clones; | |
552 | else if (node->next_sibling_clone) | |
553 | node = node->next_sibling_clone; | |
554 | else | |
555 | { | |
556 | while (node != orig && !node->next_sibling_clone) | |
557 | node = node->clone_of; | |
558 | if (node != orig) | |
559 | node = node->next_sibling_clone; | |
560 | } | |
561 | } | |
562 | } | |
563 | ||
564 | /* Remove the node from cgraph and all inline clones inlined into it. | |
565 | Skip however removal of FORBIDDEN_NODE and return true if it needs to be | |
566 | removed. This allows to call the function from outer loop walking clone | |
567 | tree. */ | |
568 | ||
569 | bool | |
570 | cgraph_remove_node_and_inline_clones (struct cgraph_node *node, struct cgraph_node *forbidden_node) | |
571 | { | |
572 | struct cgraph_edge *e, *next; | |
573 | bool found = false; | |
574 | ||
575 | if (node == forbidden_node) | |
576 | return true; | |
577 | for (e = node->callees; e; e = next) | |
578 | { | |
579 | next = e->next_callee; | |
580 | if (!e->inline_failed) | |
581 | found |= cgraph_remove_node_and_inline_clones (e->callee, forbidden_node); | |
582 | } | |
583 | cgraph_remove_node (node); | |
584 | return found; | |
585 | } | |
586 | ||
587 | /* The edges representing the callers of the NEW_VERSION node were | |
588 | fixed by cgraph_function_versioning (), now the call_expr in their | |
589 | respective tree code should be updated to call the NEW_VERSION. */ | |
590 | ||
591 | static void | |
592 | update_call_expr (struct cgraph_node *new_version) | |
593 | { | |
594 | struct cgraph_edge *e; | |
595 | ||
596 | gcc_assert (new_version); | |
597 | ||
598 | /* Update the call expr on the edges to call the new version. */ | |
599 | for (e = new_version->callers; e; e = e->next_caller) | |
600 | { | |
601 | struct function *inner_function = DECL_STRUCT_FUNCTION (e->caller->symbol.decl); | |
602 | gimple_call_set_fndecl (e->call_stmt, new_version->symbol.decl); | |
603 | maybe_clean_eh_stmt_fn (inner_function, e->call_stmt); | |
604 | } | |
605 | } | |
606 | ||
607 | ||
608 | /* Create a new cgraph node which is the new version of | |
609 | OLD_VERSION node. REDIRECT_CALLERS holds the callers | |
610 | edges which should be redirected to point to | |
611 | NEW_VERSION. ALL the callees edges of OLD_VERSION | |
612 | are cloned to the new version node. Return the new | |
613 | version node. | |
614 | ||
615 | If non-NULL BLOCK_TO_COPY determine what basic blocks | |
616 | was copied to prevent duplications of calls that are dead | |
617 | in the clone. */ | |
618 | ||
619 | struct cgraph_node * | |
620 | cgraph_copy_node_for_versioning (struct cgraph_node *old_version, | |
621 | tree new_decl, | |
622 | VEC(cgraph_edge_p,heap) *redirect_callers, | |
623 | bitmap bbs_to_copy) | |
624 | { | |
625 | struct cgraph_node *new_version; | |
626 | struct cgraph_edge *e; | |
627 | unsigned i; | |
628 | ||
629 | gcc_assert (old_version); | |
630 | ||
631 | new_version = cgraph_create_node (new_decl); | |
632 | ||
633 | new_version->analyzed = old_version->analyzed; | |
634 | new_version->local = old_version->local; | |
635 | new_version->symbol.externally_visible = false; | |
636 | new_version->local.local = old_version->analyzed; | |
637 | new_version->global = old_version->global; | |
638 | new_version->rtl = old_version->rtl; | |
639 | new_version->count = old_version->count; | |
640 | ||
641 | for (e = old_version->callees; e; e=e->next_callee) | |
642 | if (!bbs_to_copy | |
643 | || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index)) | |
644 | cgraph_clone_edge (e, new_version, e->call_stmt, | |
645 | e->lto_stmt_uid, REG_BR_PROB_BASE, | |
646 | CGRAPH_FREQ_BASE, | |
647 | true); | |
648 | for (e = old_version->indirect_calls; e; e=e->next_callee) | |
649 | if (!bbs_to_copy | |
650 | || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index)) | |
651 | cgraph_clone_edge (e, new_version, e->call_stmt, | |
652 | e->lto_stmt_uid, REG_BR_PROB_BASE, | |
653 | CGRAPH_FREQ_BASE, | |
654 | true); | |
655 | FOR_EACH_VEC_ELT (cgraph_edge_p, redirect_callers, i, e) | |
656 | { | |
657 | /* Redirect calls to the old version node to point to its new | |
658 | version. */ | |
659 | cgraph_redirect_edge_callee (e, new_version); | |
660 | } | |
661 | ||
662 | cgraph_call_node_duplication_hooks (old_version, new_version); | |
663 | ||
664 | return new_version; | |
665 | } | |
666 | ||
667 | /* Perform function versioning. | |
668 | Function versioning includes copying of the tree and | |
669 | a callgraph update (creating a new cgraph node and updating | |
670 | its callees and callers). | |
671 | ||
672 | REDIRECT_CALLERS varray includes the edges to be redirected | |
673 | to the new version. | |
674 | ||
675 | TREE_MAP is a mapping of tree nodes we want to replace with | |
676 | new ones (according to results of prior analysis). | |
677 | OLD_VERSION_NODE is the node that is versioned. | |
678 | ||
679 | If non-NULL ARGS_TO_SKIP determine function parameters to remove | |
680 | from new version. | |
681 | If SKIP_RETURN is true, the new version will return void. | |
682 | If non-NULL BLOCK_TO_COPY determine what basic blocks to copy. | |
683 | If non_NULL NEW_ENTRY determine new entry BB of the clone. | |
684 | ||
685 | Return the new version's cgraph node. */ | |
686 | ||
687 | struct cgraph_node * | |
688 | cgraph_function_versioning (struct cgraph_node *old_version_node, | |
689 | VEC(cgraph_edge_p,heap) *redirect_callers, | |
690 | VEC (ipa_replace_map_p,gc)* tree_map, | |
691 | bitmap args_to_skip, | |
692 | bool skip_return, | |
693 | bitmap bbs_to_copy, | |
694 | basic_block new_entry_block, | |
695 | const char *clone_name) | |
696 | { | |
697 | tree old_decl = old_version_node->symbol.decl; | |
698 | struct cgraph_node *new_version_node = NULL; | |
699 | tree new_decl; | |
700 | ||
701 | if (!tree_versionable_function_p (old_decl)) | |
702 | return NULL; | |
703 | ||
704 | gcc_assert (old_version_node->local.can_change_signature || !args_to_skip); | |
705 | ||
706 | /* Make a new FUNCTION_DECL tree node for the new version. */ | |
707 | if (!args_to_skip && !skip_return) | |
708 | new_decl = copy_node (old_decl); | |
709 | else | |
710 | new_decl | |
711 | = build_function_decl_skip_args (old_decl, args_to_skip, skip_return); | |
712 | ||
713 | /* Generate a new name for the new version. */ | |
714 | DECL_NAME (new_decl) = clone_function_name (old_decl, clone_name); | |
715 | SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl)); | |
716 | SET_DECL_RTL (new_decl, NULL); | |
717 | ||
718 | /* When the old decl was a con-/destructor make sure the clone isn't. */ | |
719 | DECL_STATIC_CONSTRUCTOR(new_decl) = 0; | |
720 | DECL_STATIC_DESTRUCTOR(new_decl) = 0; | |
721 | ||
722 | /* Create the new version's call-graph node. | |
723 | and update the edges of the new node. */ | |
724 | new_version_node = | |
725 | cgraph_copy_node_for_versioning (old_version_node, new_decl, | |
726 | redirect_callers, bbs_to_copy); | |
727 | ||
728 | /* Copy the OLD_VERSION_NODE function tree to the new version. */ | |
729 | tree_function_versioning (old_decl, new_decl, tree_map, false, args_to_skip, | |
730 | skip_return, bbs_to_copy, new_entry_block); | |
731 | ||
732 | /* Update the new version's properties. | |
733 | Make The new version visible only within this translation unit. Make sure | |
734 | that is not weak also. | |
735 | ??? We cannot use COMDAT linkage because there is no | |
736 | ABI support for this. */ | |
737 | symtab_make_decl_local (new_version_node->symbol.decl); | |
738 | DECL_VIRTUAL_P (new_version_node->symbol.decl) = 0; | |
739 | new_version_node->symbol.externally_visible = 0; | |
740 | new_version_node->local.local = 1; | |
741 | new_version_node->lowered = true; | |
742 | ||
743 | /* Update the call_expr on the edges to call the new version node. */ | |
744 | update_call_expr (new_version_node); | |
745 | ||
746 | cgraph_call_function_insertion_hooks (new_version_node); | |
747 | return new_version_node; | |
748 | } | |
749 | ||
750 | /* Given virtual clone, turn it into actual clone. */ | |
751 | ||
752 | static void | |
753 | cgraph_materialize_clone (struct cgraph_node *node) | |
754 | { | |
755 | bitmap_obstack_initialize (NULL); | |
756 | node->former_clone_of = node->clone_of->symbol.decl; | |
757 | if (node->clone_of->former_clone_of) | |
758 | node->former_clone_of = node->clone_of->former_clone_of; | |
759 | /* Copy the OLD_VERSION_NODE function tree to the new version. */ | |
760 | tree_function_versioning (node->clone_of->symbol.decl, node->symbol.decl, | |
761 | node->clone.tree_map, true, | |
762 | node->clone.args_to_skip, false, | |
763 | NULL, NULL); | |
764 | if (cgraph_dump_file) | |
765 | { | |
766 | dump_function_to_file (node->clone_of->symbol.decl, cgraph_dump_file, dump_flags); | |
767 | dump_function_to_file (node->symbol.decl, cgraph_dump_file, dump_flags); | |
768 | } | |
769 | ||
770 | /* Function is no longer clone. */ | |
771 | if (node->next_sibling_clone) | |
772 | node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone; | |
773 | if (node->prev_sibling_clone) | |
774 | node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone; | |
775 | else | |
776 | node->clone_of->clones = node->next_sibling_clone; | |
777 | node->next_sibling_clone = NULL; | |
778 | node->prev_sibling_clone = NULL; | |
779 | if (!node->clone_of->analyzed && !node->clone_of->clones) | |
780 | { | |
781 | cgraph_release_function_body (node->clone_of); | |
782 | cgraph_node_remove_callees (node->clone_of); | |
783 | ipa_remove_all_references (&node->clone_of->symbol.ref_list); | |
784 | } | |
785 | node->clone_of = NULL; | |
786 | bitmap_obstack_release (NULL); | |
787 | } | |
788 | ||
789 | /* Once all functions from compilation unit are in memory, produce all clones | |
790 | and update all calls. We might also do this on demand if we don't want to | |
791 | bring all functions to memory prior compilation, but current WHOPR | |
792 | implementation does that and it is is bit easier to keep everything right in | |
793 | this order. */ | |
794 | ||
795 | void | |
796 | cgraph_materialize_all_clones (void) | |
797 | { | |
798 | struct cgraph_node *node; | |
799 | bool stabilized = false; | |
800 | ||
801 | if (cgraph_dump_file) | |
802 | fprintf (cgraph_dump_file, "Materializing clones\n"); | |
803 | #ifdef ENABLE_CHECKING | |
804 | verify_cgraph (); | |
805 | #endif | |
806 | ||
807 | /* We can also do topological order, but number of iterations should be | |
808 | bounded by number of IPA passes since single IPA pass is probably not | |
809 | going to create clones of clones it created itself. */ | |
810 | while (!stabilized) | |
811 | { | |
812 | stabilized = true; | |
813 | FOR_EACH_FUNCTION (node) | |
814 | { | |
815 | if (node->clone_of && node->symbol.decl != node->clone_of->symbol.decl | |
816 | && !gimple_has_body_p (node->symbol.decl)) | |
817 | { | |
818 | if (gimple_has_body_p (node->clone_of->symbol.decl)) | |
819 | { | |
820 | if (cgraph_dump_file) | |
821 | { | |
822 | fprintf (cgraph_dump_file, "cloning %s to %s\n", | |
823 | xstrdup (cgraph_node_name (node->clone_of)), | |
824 | xstrdup (cgraph_node_name (node))); | |
825 | if (node->clone.tree_map) | |
826 | { | |
827 | unsigned int i; | |
828 | fprintf (cgraph_dump_file, " replace map: "); | |
829 | for (i = 0; i < VEC_length (ipa_replace_map_p, | |
830 | node->clone.tree_map); | |
831 | i++) | |
832 | { | |
833 | struct ipa_replace_map *replace_info; | |
834 | replace_info = VEC_index (ipa_replace_map_p, | |
835 | node->clone.tree_map, | |
836 | i); | |
837 | print_generic_expr (cgraph_dump_file, replace_info->old_tree, 0); | |
838 | fprintf (cgraph_dump_file, " -> "); | |
839 | print_generic_expr (cgraph_dump_file, replace_info->new_tree, 0); | |
840 | fprintf (cgraph_dump_file, "%s%s;", | |
841 | replace_info->replace_p ? "(replace)":"", | |
842 | replace_info->ref_p ? "(ref)":""); | |
843 | } | |
844 | fprintf (cgraph_dump_file, "\n"); | |
845 | } | |
846 | if (node->clone.args_to_skip) | |
847 | { | |
848 | fprintf (cgraph_dump_file, " args_to_skip: "); | |
849 | dump_bitmap (cgraph_dump_file, node->clone.args_to_skip); | |
850 | } | |
851 | if (node->clone.args_to_skip) | |
852 | { | |
853 | fprintf (cgraph_dump_file, " combined_args_to_skip:"); | |
854 | dump_bitmap (cgraph_dump_file, node->clone.combined_args_to_skip); | |
855 | } | |
856 | } | |
857 | cgraph_materialize_clone (node); | |
858 | stabilized = false; | |
859 | } | |
860 | } | |
861 | } | |
862 | } | |
863 | FOR_EACH_FUNCTION (node) | |
864 | if (!node->analyzed && node->callees) | |
865 | cgraph_node_remove_callees (node); | |
866 | if (cgraph_dump_file) | |
867 | fprintf (cgraph_dump_file, "Materialization Call site updates done.\n"); | |
868 | #ifdef ENABLE_CHECKING | |
869 | verify_cgraph (); | |
870 | #endif | |
91f0ab48 | 871 | symtab_remove_unreachable_nodes (false, cgraph_dump_file); |
c02159a7 | 872 | } |
873 | ||
874 | #include "gt-cgraphclones.h" |