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