gcc/
[official-gcc.git] / gcc / ipa-inline-transform.c
blob6cb9a9e98b60cffdd8389eb34f1dbcc2d684f206
1 /* Callgraph transformations to handle inlining
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
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.
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.
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/>. */
21 /* The inline decisions are stored in callgraph in "inline plan" and
22 applied later.
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.
29 The inline plan is applied on given function body by inline_transform. */
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "alias.h"
36 #include "symtab.h"
37 #include "tree.h"
38 #include "langhooks.h"
39 #include "intl.h"
40 #include "coverage.h"
41 #include "tree-cfg.h"
42 #include "hard-reg-set.h"
43 #include "function.h"
44 #include "cgraph.h"
45 #include "alloc-pool.h"
46 #include "symbol-summary.h"
47 #include "ipa-prop.h"
48 #include "ipa-inline.h"
49 #include "tree-inline.h"
50 #include "tree-pass.h"
52 int ncalls_inlined;
53 int nfunctions_inlined;
55 /* Scale frequency of NODE edges by FREQ_SCALE. */
57 static void
58 update_noncloned_frequencies (struct cgraph_node *node,
59 int freq_scale)
61 struct cgraph_edge *e;
63 /* We do not want to ignore high loop nest after freq drops to 0. */
64 if (!freq_scale)
65 freq_scale = 1;
66 for (e = node->callees; e; e = e->next_callee)
68 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
69 if (e->frequency > CGRAPH_FREQ_MAX)
70 e->frequency = CGRAPH_FREQ_MAX;
71 if (!e->inline_failed)
72 update_noncloned_frequencies (e->callee, freq_scale);
74 for (e = node->indirect_calls; e; e = e->next_callee)
76 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
77 if (e->frequency > CGRAPH_FREQ_MAX)
78 e->frequency = CGRAPH_FREQ_MAX;
82 /* We removed or are going to remove the last call to NODE.
83 Return true if we can and want proactively remove the NODE now.
84 This is important to do, since we want inliner to know when offline
85 copy of function was removed. */
87 static bool
88 can_remove_node_now_p_1 (struct cgraph_node *node, struct cgraph_edge *e)
90 ipa_ref *ref;
92 FOR_EACH_ALIAS (node, ref)
94 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
95 if ((alias->callers && alias->callers != e)
96 || !can_remove_node_now_p_1 (alias, e))
97 return false;
99 /* FIXME: When address is taken of DECL_EXTERNAL function we still
100 can remove its offline copy, but we would need to keep unanalyzed node in
101 the callgraph so references can point to it.
103 Also for comdat group we can ignore references inside a group as we
104 want to prove the group as a whole to be dead. */
105 return (!node->address_taken
106 && node->can_remove_if_no_direct_calls_and_refs_p ()
107 /* Inlining might enable more devirtualizing, so we want to remove
108 those only after all devirtualizable virtual calls are processed.
109 Lacking may edges in callgraph we just preserve them post
110 inlining. */
111 && (!DECL_VIRTUAL_P (node->decl)
112 || !opt_for_fn (node->decl, flag_devirtualize))
113 /* During early inlining some unanalyzed cgraph nodes might be in the
114 callgraph and they might reffer the function in question. */
115 && !cgraph_new_nodes.exists ());
118 /* We are going to eliminate last direct call to NODE (or alias of it) via edge E.
119 Verify that the NODE can be removed from unit and if it is contained in comdat
120 group that the whole comdat group is removable. */
122 static bool
123 can_remove_node_now_p (struct cgraph_node *node, struct cgraph_edge *e)
125 struct cgraph_node *next;
126 if (!can_remove_node_now_p_1 (node, e))
127 return false;
129 /* When we see same comdat group, we need to be sure that all
130 items can be removed. */
131 if (!node->same_comdat_group || !node->externally_visible)
132 return true;
133 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
134 next != node; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
136 if (next->alias)
137 continue;
138 if ((next->callers && next->callers != e)
139 || !can_remove_node_now_p_1 (next, e))
140 return false;
142 return true;
145 /* Return true if NODE is a master clone with non-inline clones. */
147 static bool
148 master_clone_with_noninline_clones_p (struct cgraph_node *node)
150 if (node->clone_of)
151 return false;
153 for (struct cgraph_node *n = node->clones; n; n = n->next_sibling_clone)
154 if (n->decl != node->decl)
155 return true;
157 return false;
160 /* E is expected to be an edge being inlined. Clone destination node of
161 the edge and redirect it to the new clone.
162 DUPLICATE is used for bookkeeping on whether we are actually creating new
163 clones or re-using node originally representing out-of-line function call.
164 By default the offline copy is removed, when it appears dead after inlining.
165 UPDATE_ORIGINAL prevents this transformation.
166 If OVERALL_SIZE is non-NULL, the size is updated to reflect the
167 transformation.
168 FREQ_SCALE specify the scaling of frequencies of call sites. */
170 void
171 clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
172 bool update_original, int *overall_size, int freq_scale)
174 struct cgraph_node *inlining_into;
175 struct cgraph_edge *next;
177 if (e->caller->global.inlined_to)
178 inlining_into = e->caller->global.inlined_to;
179 else
180 inlining_into = e->caller;
182 if (duplicate)
184 /* We may eliminate the need for out-of-line copy to be output.
185 In that case just go ahead and re-use it. This is not just an
186 memory optimization. Making offline copy of fuction disappear
187 from the program will improve future decisions on inlining. */
188 if (!e->callee->callers->next_caller
189 /* Recursive inlining never wants the master clone to
190 be overwritten. */
191 && update_original
192 && can_remove_node_now_p (e->callee, e)
193 /* We cannot overwrite a master clone with non-inline clones
194 until after these clones are materialized. */
195 && !master_clone_with_noninline_clones_p (e->callee))
197 /* TODO: When callee is in a comdat group, we could remove all of it,
198 including all inline clones inlined into it. That would however
199 need small function inlining to register edge removal hook to
200 maintain the priority queue.
202 For now we keep the ohter functions in the group in program until
203 cgraph_remove_unreachable_functions gets rid of them. */
204 gcc_assert (!e->callee->global.inlined_to);
205 e->callee->remove_from_same_comdat_group ();
206 if (e->callee->definition
207 && inline_account_function_p (e->callee))
209 gcc_assert (!e->callee->alias);
210 if (overall_size)
211 *overall_size -= inline_summaries->get (e->callee)->size;
212 nfunctions_inlined++;
214 duplicate = false;
215 e->callee->externally_visible = false;
216 update_noncloned_frequencies (e->callee, e->frequency);
218 else
220 struct cgraph_node *n;
222 if (freq_scale == -1)
223 freq_scale = e->frequency;
224 n = e->callee->create_clone (e->callee->decl,
225 MIN (e->count, e->callee->count),
226 freq_scale,
227 update_original, vNULL, true,
228 inlining_into,
229 NULL);
230 n->used_as_abstract_origin = e->callee->used_as_abstract_origin;
231 e->redirect_callee (n);
234 else
235 e->callee->remove_from_same_comdat_group ();
237 e->callee->global.inlined_to = inlining_into;
239 /* Recursively clone all bodies. */
240 for (e = e->callee->callees; e; e = next)
242 next = e->next_callee;
243 if (!e->inline_failed)
244 clone_inlined_nodes (e, duplicate, update_original, overall_size, freq_scale);
248 /* Check all speculations in N and resolve them if they seems useless. */
250 static bool
251 check_speculations (cgraph_node *n)
253 bool speculation_removed = false;
254 cgraph_edge *next;
256 for (cgraph_edge *e = n->callees; e; e = next)
258 next = e->next_callee;
259 if (e->speculative && !speculation_useful_p (e, true))
261 e->resolve_speculation (NULL);
262 speculation_removed = true;
264 else if (!e->inline_failed)
265 speculation_removed |= check_speculations (e->callee);
267 return speculation_removed;
270 /* Mark all call graph edges coming out of NODE and all nodes that have been
271 inlined to it as in_polymorphic_cdtor. */
273 static void
274 mark_all_inlined_calls_cdtor (cgraph_node *node)
276 for (cgraph_edge *cs = node->callees; cs; cs = cs->next_callee)
278 cs->in_polymorphic_cdtor = true;
279 if (!cs->inline_failed)
280 mark_all_inlined_calls_cdtor (cs->callee);
282 for (cgraph_edge *cs = node->indirect_calls; cs; cs = cs->next_callee)
283 cs->in_polymorphic_cdtor = true;
287 /* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL
288 specify whether profile of original function should be updated. If any new
289 indirect edges are discovered in the process, add them to NEW_EDGES, unless
290 it is NULL. If UPDATE_OVERALL_SUMMARY is false, do not bother to recompute overall
291 size of caller after inlining. Caller is required to eventually do it via
292 inline_update_overall_summary.
293 If callee_removed is non-NULL, set it to true if we removed callee node.
295 Return true iff any new callgraph edges were discovered as a
296 result of inlining. */
298 bool
299 inline_call (struct cgraph_edge *e, bool update_original,
300 vec<cgraph_edge *> *new_edges,
301 int *overall_size, bool update_overall_summary,
302 bool *callee_removed)
304 int old_size = 0, new_size = 0;
305 struct cgraph_node *to = NULL;
306 struct cgraph_edge *curr = e;
307 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
308 bool new_edges_found = false;
310 /* This is used only for assert bellow. */
311 #if 0
312 int estimated_growth = estimate_edge_growth (e);
313 bool predicated = inline_edge_summary (e)->predicate != NULL;
314 #endif
316 /* Don't inline inlined edges. */
317 gcc_assert (e->inline_failed);
318 /* Don't even think of inlining inline clone. */
319 gcc_assert (!callee->global.inlined_to);
321 e->inline_failed = CIF_OK;
322 DECL_POSSIBLY_INLINED (callee->decl) = true;
324 to = e->caller;
325 if (to->global.inlined_to)
326 to = to->global.inlined_to;
328 if (DECL_FUNCTION_PERSONALITY (callee->decl))
329 DECL_FUNCTION_PERSONALITY (to->decl)
330 = DECL_FUNCTION_PERSONALITY (callee->decl);
332 /* If aliases are involved, redirect edge to the actual destination and
333 possibly remove the aliases. */
334 if (e->callee != callee)
336 struct cgraph_node *alias = e->callee, *next_alias;
337 e->redirect_callee (callee);
338 while (alias && alias != callee)
340 if (!alias->callers
341 && can_remove_node_now_p (alias,
342 !e->next_caller && !e->prev_caller ? e : NULL))
344 next_alias = alias->get_alias_target ();
345 alias->remove ();
346 if (callee_removed)
347 *callee_removed = true;
348 alias = next_alias;
350 else
351 break;
355 clone_inlined_nodes (e, true, update_original, overall_size, e->frequency);
357 gcc_assert (curr->callee->global.inlined_to == to);
359 old_size = inline_summaries->get (to)->size;
360 inline_merge_summary (e);
361 if (e->in_polymorphic_cdtor)
362 mark_all_inlined_calls_cdtor (e->callee);
363 if (opt_for_fn (e->caller->decl, optimize))
364 new_edges_found = ipa_propagate_indirect_call_infos (curr, new_edges);
365 check_speculations (e->callee);
366 if (update_overall_summary)
367 inline_update_overall_summary (to);
368 new_size = inline_summaries->get (to)->size;
370 if (callee->calls_comdat_local)
371 to->calls_comdat_local = true;
372 else if (to->calls_comdat_local && callee->comdat_local_p ())
374 struct cgraph_edge *se = to->callees;
375 for (; se; se = se->next_callee)
376 if (se->inline_failed && se->callee->comdat_local_p ())
377 break;
378 if (se == NULL)
379 to->calls_comdat_local = false;
382 /* FIXME: This assert suffers from roundoff errors, disable it for GCC 5
383 and revisit it after conversion to sreals in GCC 6.
384 See PR 65654. */
385 #if 0
386 /* Verify that estimated growth match real growth. Allow off-by-one
387 error due to INLINE_SIZE_SCALE roudoff errors. */
388 gcc_assert (!update_overall_summary || !overall_size || new_edges_found
389 || abs (estimated_growth - (new_size - old_size)) <= 1
390 || speculation_removed
391 /* FIXME: a hack. Edges with false predicate are accounted
392 wrong, we should remove them from callgraph. */
393 || predicated);
394 #endif
396 /* Account the change of overall unit size; external functions will be
397 removed and are thus not accounted. */
398 if (overall_size && inline_account_function_p (to))
399 *overall_size += new_size - old_size;
400 ncalls_inlined++;
402 /* This must happen after inline_merge_summary that rely on jump
403 functions of callee to not be updated. */
404 return new_edges_found;
408 /* Copy function body of NODE and redirect all inline clones to it.
409 This is done before inline plan is applied to NODE when there are
410 still some inline clones if it.
412 This is necessary because inline decisions are not really transitive
413 and the other inline clones may have different bodies. */
415 static struct cgraph_node *
416 save_inline_function_body (struct cgraph_node *node)
418 struct cgraph_node *first_clone, *n;
420 if (dump_file)
421 fprintf (dump_file, "\nSaving body of %s for later reuse\n",
422 node->name ());
424 gcc_assert (node == cgraph_node::get (node->decl));
426 /* first_clone will be turned into real function. */
427 first_clone = node->clones;
428 first_clone->decl = copy_node (node->decl);
429 first_clone->decl->decl_with_vis.symtab_node = first_clone;
430 gcc_assert (first_clone == cgraph_node::get (first_clone->decl));
432 /* Now reshape the clone tree, so all other clones descends from
433 first_clone. */
434 if (first_clone->next_sibling_clone)
436 for (n = first_clone->next_sibling_clone; n->next_sibling_clone; n = n->next_sibling_clone)
437 n->clone_of = first_clone;
438 n->clone_of = first_clone;
439 n->next_sibling_clone = first_clone->clones;
440 if (first_clone->clones)
441 first_clone->clones->prev_sibling_clone = n;
442 first_clone->clones = first_clone->next_sibling_clone;
443 first_clone->next_sibling_clone->prev_sibling_clone = NULL;
444 first_clone->next_sibling_clone = NULL;
445 gcc_assert (!first_clone->prev_sibling_clone);
447 first_clone->clone_of = NULL;
449 /* Now node in question has no clones. */
450 node->clones = NULL;
452 /* Inline clones share decl with the function they are cloned
453 from. Walk the whole clone tree and redirect them all to the
454 new decl. */
455 if (first_clone->clones)
456 for (n = first_clone->clones; n != first_clone;)
458 gcc_assert (n->decl == node->decl);
459 n->decl = first_clone->decl;
460 if (n->clones)
461 n = n->clones;
462 else if (n->next_sibling_clone)
463 n = n->next_sibling_clone;
464 else
466 while (n != first_clone && !n->next_sibling_clone)
467 n = n->clone_of;
468 if (n != first_clone)
469 n = n->next_sibling_clone;
473 /* Copy the OLD_VERSION_NODE function tree to the new version. */
474 tree_function_versioning (node->decl, first_clone->decl,
475 NULL, true, NULL, false,
476 NULL, NULL);
478 /* The function will be short lived and removed after we inline all the clones,
479 but make it internal so we won't confuse ourself. */
480 DECL_EXTERNAL (first_clone->decl) = 0;
481 TREE_PUBLIC (first_clone->decl) = 0;
482 DECL_COMDAT (first_clone->decl) = 0;
483 first_clone->ipa_transforms_to_apply.release ();
485 /* When doing recursive inlining, the clone may become unnecessary.
486 This is possible i.e. in the case when the recursive function is proved to be
487 non-throwing and the recursion happens only in the EH landing pad.
488 We can not remove the clone until we are done with saving the body.
489 Remove it now. */
490 if (!first_clone->callers)
492 first_clone->remove_symbol_and_inline_clones ();
493 first_clone = NULL;
495 #ifdef ENABLE_CHECKING
496 else
497 first_clone->verify ();
498 #endif
499 return first_clone;
502 /* Return true when function body of DECL still needs to be kept around
503 for later re-use. */
504 static bool
505 preserve_function_body_p (struct cgraph_node *node)
507 gcc_assert (symtab->global_info_ready);
508 gcc_assert (!node->alias && !node->thunk.thunk_p);
510 /* Look if there is any clone around. */
511 if (node->clones)
512 return true;
513 return false;
516 /* Apply inline plan to function. */
518 unsigned int
519 inline_transform (struct cgraph_node *node)
521 unsigned int todo = 0;
522 struct cgraph_edge *e, *next;
523 bool has_inline = false;
525 /* FIXME: Currently the pass manager is adding inline transform more than
526 once to some clones. This needs revisiting after WPA cleanups. */
527 if (cfun->after_inlining)
528 return 0;
530 /* We might need the body of this function so that we can expand
531 it inline somewhere else. */
532 if (preserve_function_body_p (node))
533 save_inline_function_body (node);
535 for (e = node->callees; e; e = next)
537 if (!e->inline_failed)
538 has_inline = true;
539 next = e->next_callee;
540 e->redirect_call_stmt_to_callee ();
542 node->remove_all_references ();
544 timevar_push (TV_INTEGRATION);
545 if (node->callees && (opt_for_fn (node->decl, optimize) || has_inline))
546 todo = optimize_inline_calls (current_function_decl);
547 timevar_pop (TV_INTEGRATION);
549 cfun->always_inline_functions_inlined = true;
550 cfun->after_inlining = true;
551 todo |= execute_fixup_cfg ();
553 if (!(todo & TODO_update_ssa_any))
554 /* Redirecting edges might lead to a need for vops to be recomputed. */
555 todo |= TODO_update_ssa_only_virtuals;
557 return todo;