]>
Commit | Line | Data |
---|---|---|
8cbc43ff | 1 | /* Callgraph transformations to handle inlining |
3aea1f79 | 2 | Copyright (C) 2003-2014 Free Software Foundation, Inc. |
8cbc43ff | 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 | /* The inline decisions are stored in callgraph in "inline plan" and | |
22 | applied later. | |
23 | ||
24 | To mark given call inline, use inline_call function. | |
25 | The function marks the edge inlinable and, if necessary, produces | |
26 | virtual clone in the callgraph representing the new copy of callee's | |
27 | function body. | |
28 | ||
29 | The inline plan is applied on given function body by inline_transform. */ | |
30 | ||
31 | #include "config.h" | |
32 | #include "system.h" | |
33 | #include "coretypes.h" | |
34 | #include "tm.h" | |
35 | #include "tree.h" | |
36 | #include "langhooks.h" | |
8cbc43ff | 37 | #include "intl.h" |
38 | #include "coverage.h" | |
39 | #include "ggc.h" | |
073c1fd5 | 40 | #include "tree-cfg.h" |
8cbc43ff | 41 | #include "ipa-prop.h" |
42 | #include "ipa-inline.h" | |
43 | #include "tree-inline.h" | |
77486e4a | 44 | #include "tree-pass.h" |
8cbc43ff | 45 | |
46 | int ncalls_inlined; | |
47 | int nfunctions_inlined; | |
12d5ae9f | 48 | bool speculation_removed; |
8cbc43ff | 49 | |
0835ad03 | 50 | /* Scale frequency of NODE edges by FREQ_SCALE. */ |
8cbc43ff | 51 | |
52 | static void | |
53 | update_noncloned_frequencies (struct cgraph_node *node, | |
0835ad03 | 54 | int freq_scale) |
8cbc43ff | 55 | { |
56 | struct cgraph_edge *e; | |
57 | ||
58 | /* We do not want to ignore high loop nest after freq drops to 0. */ | |
59 | if (!freq_scale) | |
60 | freq_scale = 1; | |
61 | for (e = node->callees; e; e = e->next_callee) | |
62 | { | |
8cbc43ff | 63 | e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE; |
64 | if (e->frequency > CGRAPH_FREQ_MAX) | |
65 | e->frequency = CGRAPH_FREQ_MAX; | |
66 | if (!e->inline_failed) | |
0835ad03 | 67 | update_noncloned_frequencies (e->callee, freq_scale); |
68 | } | |
69 | for (e = node->indirect_calls; e; e = e->next_callee) | |
70 | { | |
71 | e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE; | |
72 | if (e->frequency > CGRAPH_FREQ_MAX) | |
73 | e->frequency = CGRAPH_FREQ_MAX; | |
8cbc43ff | 74 | } |
75 | } | |
76 | ||
82626cb0 | 77 | /* We removed or are going to remove the last call to NODE. |
78 | Return true if we can and want proactively remove the NODE now. | |
79 | This is important to do, since we want inliner to know when offline | |
80 | copy of function was removed. */ | |
81 | ||
82 | static bool | |
7791b0eb | 83 | can_remove_node_now_p_1 (struct cgraph_node *node) |
82626cb0 | 84 | { |
85 | /* FIXME: When address is taken of DECL_EXTERNAL function we still | |
86 | can remove its offline copy, but we would need to keep unanalyzed node in | |
87 | the callgraph so references can point to it. */ | |
02774f2d | 88 | return (!node->address_taken |
51ce5652 | 89 | && !node->has_aliases_p () |
abb1a237 | 90 | && !node->used_as_abstract_origin |
82626cb0 | 91 | && cgraph_can_remove_if_no_direct_calls_p (node) |
92 | /* Inlining might enable more devirtualizing, so we want to remove | |
93 | those only after all devirtualizable virtual calls are processed. | |
94 | Lacking may edges in callgraph we just preserve them post | |
95 | inlining. */ | |
02774f2d | 96 | && !DECL_VIRTUAL_P (node->decl) |
82626cb0 | 97 | /* During early inlining some unanalyzed cgraph nodes might be in the |
98 | callgraph and they might reffer the function in question. */ | |
99 | && !cgraph_new_nodes); | |
100 | } | |
101 | ||
7791b0eb | 102 | /* We are going to eliminate last direct call to NODE (or alias of it) via edge E. |
103 | Verify that the NODE can be removed from unit and if it is contained in comdat | |
104 | group that the whole comdat group is removable. */ | |
105 | ||
106 | static bool | |
107 | can_remove_node_now_p (struct cgraph_node *node, struct cgraph_edge *e) | |
108 | { | |
109 | struct cgraph_node *next; | |
110 | if (!can_remove_node_now_p_1 (node)) | |
111 | return false; | |
112 | ||
113 | /* When we see same comdat group, we need to be sure that all | |
114 | items can be removed. */ | |
02774f2d | 115 | if (!node->same_comdat_group) |
7791b0eb | 116 | return true; |
02774f2d | 117 | for (next = cgraph (node->same_comdat_group); |
118 | next != node; next = cgraph (next->same_comdat_group)) | |
02d98a17 | 119 | if ((next->callers && next->callers != e) |
120 | || !can_remove_node_now_p_1 (next)) | |
7791b0eb | 121 | return false; |
122 | return true; | |
123 | } | |
124 | ||
8cbc43ff | 125 | |
126 | /* E is expected to be an edge being inlined. Clone destination node of | |
127 | the edge and redirect it to the new clone. | |
128 | DUPLICATE is used for bookkeeping on whether we are actually creating new | |
129 | clones or re-using node originally representing out-of-line function call. | |
b8731470 | 130 | By default the offline copy is removed, when it appears dead after inlining. |
131 | UPDATE_ORIGINAL prevents this transformation. | |
132 | If OVERALL_SIZE is non-NULL, the size is updated to reflect the | |
133 | transformation. | |
134 | FREQ_SCALE specify the scaling of frequencies of call sites. */ | |
8cbc43ff | 135 | |
136 | void | |
137 | clone_inlined_nodes (struct cgraph_edge *e, bool duplicate, | |
b8731470 | 138 | bool update_original, int *overall_size, int freq_scale) |
8cbc43ff | 139 | { |
48f42a9a | 140 | struct cgraph_node *inlining_into; |
12d5ae9f | 141 | struct cgraph_edge *next; |
48f42a9a | 142 | |
143 | if (e->caller->global.inlined_to) | |
144 | inlining_into = e->caller->global.inlined_to; | |
145 | else | |
146 | inlining_into = e->caller; | |
147 | ||
8cbc43ff | 148 | if (duplicate) |
149 | { | |
150 | /* We may eliminate the need for out-of-line copy to be output. | |
151 | In that case just go ahead and re-use it. This is not just an | |
152 | memory optimization. Making offline copy of fuction disappear | |
153 | from the program will improve future decisions on inlining. */ | |
154 | if (!e->callee->callers->next_caller | |
155 | /* Recursive inlining never wants the master clone to | |
156 | be overwritten. */ | |
157 | && update_original | |
7791b0eb | 158 | && can_remove_node_now_p (e->callee, e)) |
8cbc43ff | 159 | { |
7791b0eb | 160 | /* TODO: When callee is in a comdat group, we could remove all of it, |
161 | including all inline clones inlined into it. That would however | |
162 | need small function inlining to register edge removal hook to | |
163 | maintain the priority queue. | |
164 | ||
165 | For now we keep the ohter functions in the group in program until | |
166 | cgraph_remove_unreachable_functions gets rid of them. */ | |
8cbc43ff | 167 | gcc_assert (!e->callee->global.inlined_to); |
02774f2d | 168 | symtab_dissolve_same_comdat_group_list (e->callee); |
169 | if (e->callee->definition && !DECL_EXTERNAL (e->callee->decl)) | |
8cbc43ff | 170 | { |
171 | if (overall_size) | |
172 | *overall_size -= inline_summary (e->callee)->size; | |
173 | nfunctions_inlined++; | |
174 | } | |
175 | duplicate = false; | |
02774f2d | 176 | e->callee->externally_visible = false; |
0835ad03 | 177 | update_noncloned_frequencies (e->callee, e->frequency); |
8cbc43ff | 178 | } |
179 | else | |
180 | { | |
181 | struct cgraph_node *n; | |
b8731470 | 182 | |
183 | if (freq_scale == -1) | |
184 | freq_scale = e->frequency; | |
02774f2d | 185 | n = cgraph_clone_node (e->callee, e->callee->decl, |
27754c3a | 186 | MIN (e->count, e->callee->count), freq_scale, |
187 | update_original, vNULL, true, inlining_into, | |
188 | NULL); | |
8cbc43ff | 189 | cgraph_redirect_edge_callee (e, n); |
190 | } | |
191 | } | |
cf951b1a | 192 | else |
02774f2d | 193 | symtab_dissolve_same_comdat_group_list (e->callee); |
8cbc43ff | 194 | |
48f42a9a | 195 | e->callee->global.inlined_to = inlining_into; |
8cbc43ff | 196 | |
197 | /* Recursively clone all bodies. */ | |
12d5ae9f | 198 | for (e = e->callee->callees; e; e = next) |
199 | { | |
200 | next = e->next_callee; | |
201 | if (!e->inline_failed) | |
b8731470 | 202 | clone_inlined_nodes (e, duplicate, update_original, overall_size, freq_scale); |
12d5ae9f | 203 | if (e->speculative && !speculation_useful_p (e, true)) |
204 | { | |
205 | cgraph_resolve_speculation (e, NULL); | |
206 | speculation_removed = true; | |
207 | } | |
208 | } | |
8cbc43ff | 209 | } |
210 | ||
211 | ||
212 | /* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL | |
213 | specify whether profile of original function should be updated. If any new | |
214 | indirect edges are discovered in the process, add them to NEW_EDGES, unless | |
6331b6fa | 215 | it is NULL. If UPDATE_OVERALL_SUMMARY is false, do not bother to recompute overall |
216 | size of caller after inlining. Caller is required to eventually do it via | |
217 | inline_update_overall_summary. | |
7c5c01f1 | 218 | If callee_removed is non-NULL, set it to true if we removed callee node. |
6331b6fa | 219 | |
220 | Return true iff any new callgraph edges were discovered as a | |
8cbc43ff | 221 | result of inlining. */ |
222 | ||
223 | bool | |
224 | inline_call (struct cgraph_edge *e, bool update_original, | |
f1f41a6c | 225 | vec<cgraph_edge_p> *new_edges, |
7c5c01f1 | 226 | int *overall_size, bool update_overall_summary, |
227 | bool *callee_removed) | |
8cbc43ff | 228 | { |
229 | int old_size = 0, new_size = 0; | |
230 | struct cgraph_node *to = NULL; | |
231 | struct cgraph_edge *curr = e; | |
82626cb0 | 232 | struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL); |
18b64b34 | 233 | bool new_edges_found = false; |
234 | ||
12ecd4f9 | 235 | #ifdef ENABLE_CHECKING |
18b64b34 | 236 | int estimated_growth = estimate_edge_growth (e); |
237 | bool predicated = inline_edge_summary (e)->predicate != NULL; | |
238 | #endif | |
8cbc43ff | 239 | |
12d5ae9f | 240 | speculation_removed = false; |
8cbc43ff | 241 | /* Don't inline inlined edges. */ |
242 | gcc_assert (e->inline_failed); | |
243 | /* Don't even think of inlining inline clone. */ | |
82626cb0 | 244 | gcc_assert (!callee->global.inlined_to); |
8cbc43ff | 245 | |
246 | e->inline_failed = CIF_OK; | |
02774f2d | 247 | DECL_POSSIBLY_INLINED (callee->decl) = true; |
8cbc43ff | 248 | |
a41f2a28 | 249 | to = e->caller; |
250 | if (to->global.inlined_to) | |
251 | to = to->global.inlined_to; | |
a41f2a28 | 252 | |
82626cb0 | 253 | /* If aliases are involved, redirect edge to the actual destination and |
254 | possibly remove the aliases. */ | |
255 | if (e->callee != callee) | |
c70f46b0 | 256 | { |
257 | struct cgraph_node *alias = e->callee, *next_alias; | |
258 | cgraph_redirect_edge_callee (e, callee); | |
259 | while (alias && alias != callee) | |
260 | { | |
261 | if (!alias->callers | |
7791b0eb | 262 | && can_remove_node_now_p (alias, e)) |
c70f46b0 | 263 | { |
15ca8f90 | 264 | next_alias = cgraph_alias_target (alias); |
c70f46b0 | 265 | cgraph_remove_node (alias); |
7c5c01f1 | 266 | if (callee_removed) |
267 | *callee_removed = true; | |
c70f46b0 | 268 | alias = next_alias; |
269 | } | |
270 | else | |
271 | break; | |
272 | } | |
273 | } | |
82626cb0 | 274 | |
b8731470 | 275 | clone_inlined_nodes (e, true, update_original, overall_size, e->frequency); |
8cbc43ff | 276 | |
8cbc43ff | 277 | gcc_assert (curr->callee->global.inlined_to == to); |
0835ad03 | 278 | |
279 | old_size = inline_summary (to)->size; | |
280 | inline_merge_summary (e); | |
18b64b34 | 281 | if (optimize) |
282 | new_edges_found = ipa_propagate_indirect_call_infos (curr, new_edges); | |
6331b6fa | 283 | if (update_overall_summary) |
284 | inline_update_overall_summary (to); | |
0835ad03 | 285 | new_size = inline_summary (to)->size; |
12ecd4f9 | 286 | |
468088ac | 287 | if (callee->calls_comdat_local) |
288 | to->calls_comdat_local = true; | |
289 | else if (to->calls_comdat_local && symtab_comdat_local_p (callee)) | |
290 | { | |
291 | struct cgraph_edge *se = to->callees; | |
292 | for (; se; se = se->next_callee) | |
293 | if (se->inline_failed && symtab_comdat_local_p (se->callee)) | |
294 | break; | |
295 | if (se == NULL) | |
296 | to->calls_comdat_local = false; | |
297 | } | |
298 | ||
12ecd4f9 | 299 | #ifdef ENABLE_CHECKING |
18b64b34 | 300 | /* Verify that estimated growth match real growth. Allow off-by-one |
301 | error due to INLINE_SIZE_SCALE roudoff errors. */ | |
95fb3203 | 302 | gcc_assert (!update_overall_summary || !overall_size || new_edges_found |
18b64b34 | 303 | || abs (estimated_growth - (new_size - old_size)) <= 1 |
12d5ae9f | 304 | || speculation_removed |
18b64b34 | 305 | /* FIXME: a hack. Edges with false predicate are accounted |
306 | wrong, we should remove them from callgraph. */ | |
307 | || predicated); | |
308 | #endif | |
12ecd4f9 | 309 | |
d94ff7ce | 310 | /* Account the change of overall unit size; external functions will be |
311 | removed and are thus not accounted. */ | |
312 | if (overall_size | |
02774f2d | 313 | && !DECL_EXTERNAL (to->decl)) |
8cbc43ff | 314 | *overall_size += new_size - old_size; |
315 | ncalls_inlined++; | |
316 | ||
eb4ae064 | 317 | /* This must happen after inline_merge_summary that rely on jump |
318 | functions of callee to not be updated. */ | |
18b64b34 | 319 | return new_edges_found; |
8cbc43ff | 320 | } |
321 | ||
322 | ||
323 | /* Copy function body of NODE and redirect all inline clones to it. | |
324 | This is done before inline plan is applied to NODE when there are | |
325 | still some inline clones if it. | |
326 | ||
9d75589a | 327 | This is necessary because inline decisions are not really transitive |
8cbc43ff | 328 | and the other inline clones may have different bodies. */ |
329 | ||
330 | static struct cgraph_node * | |
331 | save_inline_function_body (struct cgraph_node *node) | |
332 | { | |
333 | struct cgraph_node *first_clone, *n; | |
334 | ||
335 | if (dump_file) | |
336 | fprintf (dump_file, "\nSaving body of %s for later reuse\n", | |
f1c8b4d7 | 337 | node->name ()); |
8cbc43ff | 338 | |
02774f2d | 339 | gcc_assert (node == cgraph_get_node (node->decl)); |
8cbc43ff | 340 | |
341 | /* first_clone will be turned into real function. */ | |
342 | first_clone = node->clones; | |
02774f2d | 343 | first_clone->decl = copy_node (node->decl); |
8c016392 | 344 | first_clone->decl->decl_with_vis.symtab_node = first_clone; |
02774f2d | 345 | gcc_assert (first_clone == cgraph_get_node (first_clone->decl)); |
8cbc43ff | 346 | |
347 | /* Now reshape the clone tree, so all other clones descends from | |
348 | first_clone. */ | |
349 | if (first_clone->next_sibling_clone) | |
350 | { | |
351 | for (n = first_clone->next_sibling_clone; n->next_sibling_clone; n = n->next_sibling_clone) | |
352 | n->clone_of = first_clone; | |
353 | n->clone_of = first_clone; | |
354 | n->next_sibling_clone = first_clone->clones; | |
355 | if (first_clone->clones) | |
356 | first_clone->clones->prev_sibling_clone = n; | |
357 | first_clone->clones = first_clone->next_sibling_clone; | |
358 | first_clone->next_sibling_clone->prev_sibling_clone = NULL; | |
359 | first_clone->next_sibling_clone = NULL; | |
360 | gcc_assert (!first_clone->prev_sibling_clone); | |
361 | } | |
362 | first_clone->clone_of = NULL; | |
363 | ||
364 | /* Now node in question has no clones. */ | |
365 | node->clones = NULL; | |
366 | ||
d826e131 | 367 | /* Inline clones share decl with the function they are cloned |
368 | from. Walk the whole clone tree and redirect them all to the | |
369 | new decl. */ | |
8cbc43ff | 370 | if (first_clone->clones) |
371 | for (n = first_clone->clones; n != first_clone;) | |
372 | { | |
02774f2d | 373 | gcc_assert (n->decl == node->decl); |
374 | n->decl = first_clone->decl; | |
8cbc43ff | 375 | if (n->clones) |
376 | n = n->clones; | |
377 | else if (n->next_sibling_clone) | |
378 | n = n->next_sibling_clone; | |
379 | else | |
380 | { | |
381 | while (n != first_clone && !n->next_sibling_clone) | |
382 | n = n->clone_of; | |
383 | if (n != first_clone) | |
384 | n = n->next_sibling_clone; | |
385 | } | |
386 | } | |
387 | ||
388 | /* Copy the OLD_VERSION_NODE function tree to the new version. */ | |
02774f2d | 389 | tree_function_versioning (node->decl, first_clone->decl, |
f1f41a6c | 390 | NULL, true, NULL, false, |
391 | NULL, NULL); | |
8cbc43ff | 392 | |
d826e131 | 393 | /* The function will be short lived and removed after we inline all the clones, |
394 | but make it internal so we won't confuse ourself. */ | |
02774f2d | 395 | DECL_EXTERNAL (first_clone->decl) = 0; |
02774f2d | 396 | TREE_PUBLIC (first_clone->decl) = 0; |
397 | DECL_COMDAT (first_clone->decl) = 0; | |
f1f41a6c | 398 | first_clone->ipa_transforms_to_apply.release (); |
8cbc43ff | 399 | |
41710b76 | 400 | /* When doing recursive inlining, the clone may become unnecessary. |
401 | This is possible i.e. in the case when the recursive function is proved to be | |
402 | non-throwing and the recursion happens only in the EH landing pad. | |
403 | We can not remove the clone until we are done with saving the body. | |
404 | Remove it now. */ | |
405 | if (!first_clone->callers) | |
406 | { | |
407 | cgraph_remove_node_and_inline_clones (first_clone, NULL); | |
408 | first_clone = NULL; | |
409 | } | |
8cbc43ff | 410 | #ifdef ENABLE_CHECKING |
41710b76 | 411 | else |
412 | verify_cgraph_node (first_clone); | |
8cbc43ff | 413 | #endif |
414 | return first_clone; | |
415 | } | |
416 | ||
da5e1e7c | 417 | /* Return true when function body of DECL still needs to be kept around |
418 | for later re-use. */ | |
cf951b1a | 419 | static bool |
da5e1e7c | 420 | preserve_function_body_p (struct cgraph_node *node) |
421 | { | |
422 | gcc_assert (cgraph_global_info_ready); | |
02774f2d | 423 | gcc_assert (!node->alias && !node->thunk.thunk_p); |
da5e1e7c | 424 | |
425 | /* Look if there is any clone around. */ | |
426 | if (node->clones) | |
427 | return true; | |
428 | return false; | |
429 | } | |
8cbc43ff | 430 | |
431 | /* Apply inline plan to function. */ | |
432 | ||
433 | unsigned int | |
434 | inline_transform (struct cgraph_node *node) | |
435 | { | |
436 | unsigned int todo = 0; | |
dc3d1030 | 437 | struct cgraph_edge *e, *next; |
a522e9eb | 438 | |
8cbc43ff | 439 | /* FIXME: Currently the pass manager is adding inline transform more than |
440 | once to some clones. This needs revisiting after WPA cleanups. */ | |
441 | if (cfun->after_inlining) | |
442 | return 0; | |
443 | ||
444 | /* We might need the body of this function so that we can expand | |
445 | it inline somewhere else. */ | |
da5e1e7c | 446 | if (preserve_function_body_p (node)) |
8cbc43ff | 447 | save_inline_function_body (node); |
448 | ||
dc3d1030 | 449 | for (e = node->callees; e; e = next) |
450 | { | |
451 | next = e->next_callee; | |
452 | cgraph_redirect_edge_call_stmt_to_callee (e); | |
453 | } | |
51ce5652 | 454 | node->remove_all_references (); |
a522e9eb | 455 | |
456 | timevar_push (TV_INTEGRATION); | |
ed784636 | 457 | if (node->callees && optimize) |
4b0e0420 | 458 | todo = optimize_inline_calls (current_function_decl); |
a522e9eb | 459 | timevar_pop (TV_INTEGRATION); |
460 | ||
e723655c | 461 | cfun->always_inline_functions_inlined = true; |
462 | cfun->after_inlining = true; | |
463 | todo |= execute_fixup_cfg (); | |
464 | ||
4b0e0420 | 465 | if (!(todo & TODO_update_ssa_any)) |
466 | /* Redirecting edges might lead to a need for vops to be recomputed. */ | |
467 | todo |= TODO_update_ssa_only_virtuals; | |
468 | ||
e723655c | 469 | return todo; |
8cbc43ff | 470 | } |