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