]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ipa-inline-transform.c
tree-core.h: Include symtab.h.
[thirdparty/gcc.git] / gcc / ipa-inline-transform.c
CommitLineData
fee8b6da 1/* Callgraph transformations to handle inlining
5624e564 2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
fee8b6da
JH
3 Contributed by Jan Hubicka
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along 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"
40e23961 35#include "alias.h"
fee8b6da
JH
36#include "tree.h"
37#include "langhooks.h"
fee8b6da
JH
38#include "intl.h"
39#include "coverage.h"
442b4905 40#include "tree-cfg.h"
c582198b 41#include "hard-reg-set.h"
c582198b 42#include "function.h"
c582198b
AM
43#include "cgraph.h"
44#include "alloc-pool.h"
dd912cb8 45#include "symbol-summary.h"
fee8b6da
JH
46#include "ipa-prop.h"
47#include "ipa-inline.h"
48#include "tree-inline.h"
516e0768 49#include "tree-pass.h"
fee8b6da
JH
50
51int ncalls_inlined;
52int nfunctions_inlined;
53
898b8927 54/* Scale frequency of NODE edges by FREQ_SCALE. */
fee8b6da
JH
55
56static void
57update_noncloned_frequencies (struct cgraph_node *node,
898b8927 58 int freq_scale)
fee8b6da
JH
59{
60 struct cgraph_edge *e;
61
62 /* We do not want to ignore high loop nest after freq drops to 0. */
63 if (!freq_scale)
64 freq_scale = 1;
65 for (e = node->callees; e; e = e->next_callee)
66 {
fee8b6da
JH
67 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
68 if (e->frequency > CGRAPH_FREQ_MAX)
69 e->frequency = CGRAPH_FREQ_MAX;
70 if (!e->inline_failed)
898b8927
JH
71 update_noncloned_frequencies (e->callee, freq_scale);
72 }
73 for (e = node->indirect_calls; e; e = e->next_callee)
74 {
75 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
76 if (e->frequency > CGRAPH_FREQ_MAX)
77 e->frequency = CGRAPH_FREQ_MAX;
fee8b6da
JH
78 }
79}
80
a5b1779f
JH
81/* We removed or are going to remove the last call to NODE.
82 Return true if we can and want proactively remove the NODE now.
83 This is important to do, since we want inliner to know when offline
84 copy of function was removed. */
85
86static bool
d142079a 87can_remove_node_now_p_1 (struct cgraph_node *node, struct cgraph_edge *e)
a5b1779f 88{
d142079a
JH
89 ipa_ref *ref;
90
91 FOR_EACH_ALIAS (node, ref)
92 {
93 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
94 if ((alias->callers && alias->callers != e)
95 || !can_remove_node_now_p_1 (alias, e))
96 return false;
97 }
a5b1779f
JH
98 /* FIXME: When address is taken of DECL_EXTERNAL function we still
99 can remove its offline copy, but we would need to keep unanalyzed node in
a6a543bf
JH
100 the callgraph so references can point to it.
101
102 Also for comdat group we can ignore references inside a group as we
103 want to prove the group as a whole to be dead. */
67348ccc 104 return (!node->address_taken
a6a543bf 105 && node->can_remove_if_no_direct_calls_and_refs_p ()
a5b1779f
JH
106 /* Inlining might enable more devirtualizing, so we want to remove
107 those only after all devirtualizable virtual calls are processed.
108 Lacking may edges in callgraph we just preserve them post
109 inlining. */
d142079a
JH
110 && (!DECL_VIRTUAL_P (node->decl)
111 || !opt_for_fn (node->decl, flag_devirtualize))
a5b1779f
JH
112 /* During early inlining some unanalyzed cgraph nodes might be in the
113 callgraph and they might reffer the function in question. */
31acf1bb 114 && !cgraph_new_nodes.exists ());
a5b1779f
JH
115}
116
6c69a029
JH
117/* We are going to eliminate last direct call to NODE (or alias of it) via edge E.
118 Verify that the NODE can be removed from unit and if it is contained in comdat
119 group that the whole comdat group is removable. */
120
121static bool
122can_remove_node_now_p (struct cgraph_node *node, struct cgraph_edge *e)
123{
124 struct cgraph_node *next;
d142079a 125 if (!can_remove_node_now_p_1 (node, e))
6c69a029
JH
126 return false;
127
128 /* When we see same comdat group, we need to be sure that all
129 items can be removed. */
8ccc8042 130 if (!node->same_comdat_group || !node->externally_visible)
6c69a029 131 return true;
d52f5295
ML
132 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
133 next != node; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
d142079a
JH
134 {
135 if (next->alias)
136 continue;
137 if ((next->callers && next->callers != e)
138 || !can_remove_node_now_p_1 (next, e))
139 return false;
140 }
6c69a029
JH
141 return true;
142}
143
d83fa499
EB
144/* Return true if NODE is a master clone with non-inline clones. */
145
146static bool
147master_clone_with_noninline_clones_p (struct cgraph_node *node)
148{
149 if (node->clone_of)
150 return false;
151
152 for (struct cgraph_node *n = node->clones; n; n = n->next_sibling_clone)
153 if (n->decl != node->decl)
154 return true;
155
156 return false;
157}
fee8b6da
JH
158
159/* E is expected to be an edge being inlined. Clone destination node of
160 the edge and redirect it to the new clone.
161 DUPLICATE is used for bookkeeping on whether we are actually creating new
162 clones or re-using node originally representing out-of-line function call.
bd936951
JH
163 By default the offline copy is removed, when it appears dead after inlining.
164 UPDATE_ORIGINAL prevents this transformation.
165 If OVERALL_SIZE is non-NULL, the size is updated to reflect the
166 transformation.
167 FREQ_SCALE specify the scaling of frequencies of call sites. */
fee8b6da
JH
168
169void
170clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
bd936951 171 bool update_original, int *overall_size, int freq_scale)
fee8b6da 172{
44a60244 173 struct cgraph_node *inlining_into;
09ce3660 174 struct cgraph_edge *next;
44a60244
MJ
175
176 if (e->caller->global.inlined_to)
177 inlining_into = e->caller->global.inlined_to;
178 else
179 inlining_into = e->caller;
180
fee8b6da
JH
181 if (duplicate)
182 {
183 /* We may eliminate the need for out-of-line copy to be output.
184 In that case just go ahead and re-use it. This is not just an
185 memory optimization. Making offline copy of fuction disappear
186 from the program will improve future decisions on inlining. */
187 if (!e->callee->callers->next_caller
188 /* Recursive inlining never wants the master clone to
189 be overwritten. */
190 && update_original
d83fa499
EB
191 && can_remove_node_now_p (e->callee, e)
192 /* We cannot overwrite a master clone with non-inline clones
193 until after these clones are materialized. */
194 && !master_clone_with_noninline_clones_p (e->callee))
fee8b6da 195 {
6c69a029
JH
196 /* TODO: When callee is in a comdat group, we could remove all of it,
197 including all inline clones inlined into it. That would however
198 need small function inlining to register edge removal hook to
199 maintain the priority queue.
200
201 For now we keep the ohter functions in the group in program until
202 cgraph_remove_unreachable_functions gets rid of them. */
fee8b6da 203 gcc_assert (!e->callee->global.inlined_to);
b91b562c 204 e->callee->remove_from_same_comdat_group ();
bb1e543c
JH
205 if (e->callee->definition
206 && inline_account_function_p (e->callee))
fee8b6da 207 {
bb1e543c 208 gcc_assert (!e->callee->alias);
fee8b6da 209 if (overall_size)
9a1e784a 210 *overall_size -= inline_summaries->get (e->callee)->size;
fee8b6da
JH
211 nfunctions_inlined++;
212 }
213 duplicate = false;
67348ccc 214 e->callee->externally_visible = false;
898b8927 215 update_noncloned_frequencies (e->callee, e->frequency);
fee8b6da
JH
216 }
217 else
218 {
219 struct cgraph_node *n;
bd936951
JH
220
221 if (freq_scale == -1)
222 freq_scale = e->frequency;
d52f5295
ML
223 n = e->callee->create_clone (e->callee->decl,
224 MIN (e->count, e->callee->count),
225 freq_scale,
226 update_original, vNULL, true,
227 inlining_into,
228 NULL);
4ad08ee8 229 n->used_as_abstract_origin = e->callee->used_as_abstract_origin;
3dafb85c 230 e->redirect_callee (n);
fee8b6da
JH
231 }
232 }
65d630d4 233 else
b91b562c 234 e->callee->remove_from_same_comdat_group ();
fee8b6da 235
44a60244 236 e->callee->global.inlined_to = inlining_into;
fee8b6da
JH
237
238 /* Recursively clone all bodies. */
09ce3660
JH
239 for (e = e->callee->callees; e; e = next)
240 {
241 next = e->next_callee;
242 if (!e->inline_failed)
bd936951 243 clone_inlined_nodes (e, duplicate, update_original, overall_size, freq_scale);
db66bf68
JH
244 }
245}
246
247/* Check all speculations in N and resolve them if they seems useless. */
248
249static bool
250check_speculations (cgraph_node *n)
251{
252 bool speculation_removed = false;
253 cgraph_edge *next;
254
255 for (cgraph_edge *e = n->callees; e; e = next)
256 {
257 next = e->next_callee;
09ce3660
JH
258 if (e->speculative && !speculation_useful_p (e, true))
259 {
3dafb85c 260 e->resolve_speculation (NULL);
09ce3660
JH
261 speculation_removed = true;
262 }
db66bf68
JH
263 else if (!e->inline_failed)
264 speculation_removed |= check_speculations (e->callee);
09ce3660 265 }
db66bf68 266 return speculation_removed;
fee8b6da
JH
267}
268
4fd94d1e
MJ
269/* Mark all call graph edges coming out of NODE and all nodes that have been
270 inlined to it as in_polymorphic_cdtor. */
271
272static void
273mark_all_inlined_calls_cdtor (cgraph_node *node)
274{
275 for (cgraph_edge *cs = node->callees; cs; cs = cs->next_callee)
276 {
277 cs->in_polymorphic_cdtor = true;
278 if (!cs->inline_failed)
279 mark_all_inlined_calls_cdtor (cs->callee);
280 }
281 for (cgraph_edge *cs = node->indirect_calls; cs; cs = cs->next_callee)
282 cs->in_polymorphic_cdtor = true;
283}
284
fee8b6da
JH
285
286/* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL
287 specify whether profile of original function should be updated. If any new
288 indirect edges are discovered in the process, add them to NEW_EDGES, unless
c170d40f
JH
289 it is NULL. If UPDATE_OVERALL_SUMMARY is false, do not bother to recompute overall
290 size of caller after inlining. Caller is required to eventually do it via
291 inline_update_overall_summary.
1bbb87c4 292 If callee_removed is non-NULL, set it to true if we removed callee node.
c170d40f
JH
293
294 Return true iff any new callgraph edges were discovered as a
fee8b6da
JH
295 result of inlining. */
296
297bool
298inline_call (struct cgraph_edge *e, bool update_original,
d52f5295 299 vec<cgraph_edge *> *new_edges,
1bbb87c4
JH
300 int *overall_size, bool update_overall_summary,
301 bool *callee_removed)
fee8b6da
JH
302{
303 int old_size = 0, new_size = 0;
304 struct cgraph_node *to = NULL;
305 struct cgraph_edge *curr = e;
d52f5295 306 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
0f378cb5
JH
307 bool new_edges_found = false;
308
f107227b
JH
309 /* This is used only for assert bellow. */
310#if 0
0f378cb5
JH
311 int estimated_growth = estimate_edge_growth (e);
312 bool predicated = inline_edge_summary (e)->predicate != NULL;
313#endif
fee8b6da
JH
314
315 /* Don't inline inlined edges. */
316 gcc_assert (e->inline_failed);
317 /* Don't even think of inlining inline clone. */
a5b1779f 318 gcc_assert (!callee->global.inlined_to);
fee8b6da
JH
319
320 e->inline_failed = CIF_OK;
67348ccc 321 DECL_POSSIBLY_INLINED (callee->decl) = true;
fee8b6da 322
632b4f8e
JH
323 to = e->caller;
324 if (to->global.inlined_to)
325 to = to->global.inlined_to;
632b4f8e 326
5058c037
JH
327 if (DECL_FUNCTION_PERSONALITY (callee->decl))
328 DECL_FUNCTION_PERSONALITY (to->decl)
329 = DECL_FUNCTION_PERSONALITY (callee->decl);
330
a5b1779f
JH
331 /* If aliases are involved, redirect edge to the actual destination and
332 possibly remove the aliases. */
333 if (e->callee != callee)
39e2db00
JH
334 {
335 struct cgraph_node *alias = e->callee, *next_alias;
3dafb85c 336 e->redirect_callee (callee);
39e2db00
JH
337 while (alias && alias != callee)
338 {
339 if (!alias->callers
8ccc8042
JH
340 && can_remove_node_now_p (alias,
341 !e->next_caller && !e->prev_caller ? e : NULL))
39e2db00 342 {
d52f5295
ML
343 next_alias = alias->get_alias_target ();
344 alias->remove ();
1bbb87c4
JH
345 if (callee_removed)
346 *callee_removed = true;
39e2db00
JH
347 alias = next_alias;
348 }
349 else
350 break;
351 }
352 }
a5b1779f 353
bd936951 354 clone_inlined_nodes (e, true, update_original, overall_size, e->frequency);
fee8b6da 355
fee8b6da 356 gcc_assert (curr->callee->global.inlined_to == to);
898b8927 357
9a1e784a 358 old_size = inline_summaries->get (to)->size;
898b8927 359 inline_merge_summary (e);
4fd94d1e
MJ
360 if (e->in_polymorphic_cdtor)
361 mark_all_inlined_calls_cdtor (e->callee);
bb1e543c 362 if (opt_for_fn (e->caller->decl, optimize))
0f378cb5 363 new_edges_found = ipa_propagate_indirect_call_infos (curr, new_edges);
db66bf68 364 check_speculations (e->callee);
c170d40f
JH
365 if (update_overall_summary)
366 inline_update_overall_summary (to);
9a1e784a 367 new_size = inline_summaries->get (to)->size;
d250540a 368
1f26ac87
JM
369 if (callee->calls_comdat_local)
370 to->calls_comdat_local = true;
d52f5295 371 else if (to->calls_comdat_local && callee->comdat_local_p ())
1f26ac87
JM
372 {
373 struct cgraph_edge *se = to->callees;
374 for (; se; se = se->next_callee)
d52f5295 375 if (se->inline_failed && se->callee->comdat_local_p ())
1f26ac87
JM
376 break;
377 if (se == NULL)
378 to->calls_comdat_local = false;
379 }
380
f107227b
JH
381 /* FIXME: This assert suffers from roundoff errors, disable it for GCC 5
382 and revisit it after conversion to sreals in GCC 6.
383 See PR 65654. */
384#if 0
0f378cb5
JH
385 /* Verify that estimated growth match real growth. Allow off-by-one
386 error due to INLINE_SIZE_SCALE roudoff errors. */
48b1474e 387 gcc_assert (!update_overall_summary || !overall_size || new_edges_found
0f378cb5 388 || abs (estimated_growth - (new_size - old_size)) <= 1
09ce3660 389 || speculation_removed
0f378cb5
JH
390 /* FIXME: a hack. Edges with false predicate are accounted
391 wrong, we should remove them from callgraph. */
392 || predicated);
393#endif
d250540a 394
8256d5ca
JH
395 /* Account the change of overall unit size; external functions will be
396 removed and are thus not accounted. */
bb1e543c 397 if (overall_size && inline_account_function_p (to))
fee8b6da
JH
398 *overall_size += new_size - old_size;
399 ncalls_inlined++;
400
25837a2f
JH
401 /* This must happen after inline_merge_summary that rely on jump
402 functions of callee to not be updated. */
0f378cb5 403 return new_edges_found;
fee8b6da
JH
404}
405
406
407/* Copy function body of NODE and redirect all inline clones to it.
408 This is done before inline plan is applied to NODE when there are
409 still some inline clones if it.
410
073a8998 411 This is necessary because inline decisions are not really transitive
fee8b6da
JH
412 and the other inline clones may have different bodies. */
413
414static struct cgraph_node *
415save_inline_function_body (struct cgraph_node *node)
416{
417 struct cgraph_node *first_clone, *n;
418
419 if (dump_file)
420 fprintf (dump_file, "\nSaving body of %s for later reuse\n",
fec39fa6 421 node->name ());
fee8b6da 422
d52f5295 423 gcc_assert (node == cgraph_node::get (node->decl));
fee8b6da
JH
424
425 /* first_clone will be turned into real function. */
426 first_clone = node->clones;
67348ccc 427 first_clone->decl = copy_node (node->decl);
aede2c10 428 first_clone->decl->decl_with_vis.symtab_node = first_clone;
d52f5295 429 gcc_assert (first_clone == cgraph_node::get (first_clone->decl));
fee8b6da
JH
430
431 /* Now reshape the clone tree, so all other clones descends from
432 first_clone. */
433 if (first_clone->next_sibling_clone)
434 {
435 for (n = first_clone->next_sibling_clone; n->next_sibling_clone; n = n->next_sibling_clone)
436 n->clone_of = first_clone;
437 n->clone_of = first_clone;
438 n->next_sibling_clone = first_clone->clones;
439 if (first_clone->clones)
440 first_clone->clones->prev_sibling_clone = n;
441 first_clone->clones = first_clone->next_sibling_clone;
442 first_clone->next_sibling_clone->prev_sibling_clone = NULL;
443 first_clone->next_sibling_clone = NULL;
444 gcc_assert (!first_clone->prev_sibling_clone);
445 }
446 first_clone->clone_of = NULL;
447
448 /* Now node in question has no clones. */
449 node->clones = NULL;
450
1a3118e9
JH
451 /* Inline clones share decl with the function they are cloned
452 from. Walk the whole clone tree and redirect them all to the
453 new decl. */
fee8b6da
JH
454 if (first_clone->clones)
455 for (n = first_clone->clones; n != first_clone;)
456 {
67348ccc
DM
457 gcc_assert (n->decl == node->decl);
458 n->decl = first_clone->decl;
fee8b6da
JH
459 if (n->clones)
460 n = n->clones;
461 else if (n->next_sibling_clone)
462 n = n->next_sibling_clone;
463 else
464 {
465 while (n != first_clone && !n->next_sibling_clone)
466 n = n->clone_of;
467 if (n != first_clone)
468 n = n->next_sibling_clone;
469 }
470 }
471
472 /* Copy the OLD_VERSION_NODE function tree to the new version. */
67348ccc 473 tree_function_versioning (node->decl, first_clone->decl,
9771b263
DN
474 NULL, true, NULL, false,
475 NULL, NULL);
fee8b6da 476
1a3118e9
JH
477 /* The function will be short lived and removed after we inline all the clones,
478 but make it internal so we won't confuse ourself. */
67348ccc 479 DECL_EXTERNAL (first_clone->decl) = 0;
67348ccc
DM
480 TREE_PUBLIC (first_clone->decl) = 0;
481 DECL_COMDAT (first_clone->decl) = 0;
9771b263 482 first_clone->ipa_transforms_to_apply.release ();
fee8b6da 483
b4e93f45
JH
484 /* When doing recursive inlining, the clone may become unnecessary.
485 This is possible i.e. in the case when the recursive function is proved to be
486 non-throwing and the recursion happens only in the EH landing pad.
487 We can not remove the clone until we are done with saving the body.
488 Remove it now. */
489 if (!first_clone->callers)
490 {
d52f5295 491 first_clone->remove_symbol_and_inline_clones ();
b4e93f45
JH
492 first_clone = NULL;
493 }
fee8b6da 494#ifdef ENABLE_CHECKING
b4e93f45 495 else
d52f5295 496 first_clone->verify ();
fee8b6da
JH
497#endif
498 return first_clone;
499}
500
9c8305f8
JH
501/* Return true when function body of DECL still needs to be kept around
502 for later re-use. */
65d630d4 503static bool
9c8305f8
JH
504preserve_function_body_p (struct cgraph_node *node)
505{
3dafb85c 506 gcc_assert (symtab->global_info_ready);
67348ccc 507 gcc_assert (!node->alias && !node->thunk.thunk_p);
9c8305f8
JH
508
509 /* Look if there is any clone around. */
510 if (node->clones)
511 return true;
512 return false;
513}
fee8b6da
JH
514
515/* Apply inline plan to function. */
516
517unsigned int
518inline_transform (struct cgraph_node *node)
519{
520 unsigned int todo = 0;
e8aec975 521 struct cgraph_edge *e, *next;
2bf86c84 522 bool has_inline = false;
c9fc06dc 523
fee8b6da
JH
524 /* FIXME: Currently the pass manager is adding inline transform more than
525 once to some clones. This needs revisiting after WPA cleanups. */
526 if (cfun->after_inlining)
527 return 0;
528
529 /* We might need the body of this function so that we can expand
530 it inline somewhere else. */
9c8305f8 531 if (preserve_function_body_p (node))
fee8b6da
JH
532 save_inline_function_body (node);
533
e8aec975
JH
534 for (e = node->callees; e; e = next)
535 {
2bf86c84
JH
536 if (!e->inline_failed)
537 has_inline = true;
e8aec975 538 next = e->next_callee;
3dafb85c 539 e->redirect_call_stmt_to_callee ();
e8aec975 540 }
d122681a 541 node->remove_all_references ();
c9fc06dc
CB
542
543 timevar_push (TV_INTEGRATION);
bb1e543c 544 if (node->callees && (opt_for_fn (node->decl, optimize) || has_inline))
55f01229 545 todo = optimize_inline_calls (current_function_decl);
c9fc06dc
CB
546 timevar_pop (TV_INTEGRATION);
547
f8698b37
RG
548 cfun->always_inline_functions_inlined = true;
549 cfun->after_inlining = true;
550 todo |= execute_fixup_cfg ();
551
55f01229
RG
552 if (!(todo & TODO_update_ssa_any))
553 /* Redirecting edges might lead to a need for vops to be recomputed. */
554 todo |= TODO_update_ssa_only_virtuals;
555
f8698b37 556 return todo;
fee8b6da 557}