2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ipa-inline-transform.c
blob43cc4977dc29630a24fa146cef6acccf679c1b5a
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 "input.h"
36 #include "alias.h"
37 #include "symtab.h"
38 #include "tree.h"
39 #include "langhooks.h"
40 #include "intl.h"
41 #include "coverage.h"
42 #include "tree-cfg.h"
43 #include "is-a.h"
44 #include "plugin-api.h"
45 #include "hard-reg-set.h"
46 #include "input.h"
47 #include "function.h"
48 #include "ipa-ref.h"
49 #include "cgraph.h"
50 #include "alloc-pool.h"
51 #include "symbol-summary.h"
52 #include "ipa-prop.h"
53 #include "ipa-inline.h"
54 #include "tree-inline.h"
55 #include "tree-pass.h"
57 int ncalls_inlined;
58 int nfunctions_inlined;
60 /* Scale frequency of NODE edges by FREQ_SCALE. */
62 static void
63 update_noncloned_frequencies (struct cgraph_node *node,
64 int freq_scale)
66 struct cgraph_edge *e;
68 /* We do not want to ignore high loop nest after freq drops to 0. */
69 if (!freq_scale)
70 freq_scale = 1;
71 for (e = node->callees; e; e = e->next_callee)
73 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
74 if (e->frequency > CGRAPH_FREQ_MAX)
75 e->frequency = CGRAPH_FREQ_MAX;
76 if (!e->inline_failed)
77 update_noncloned_frequencies (e->callee, freq_scale);
79 for (e = node->indirect_calls; e; e = e->next_callee)
81 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
82 if (e->frequency > CGRAPH_FREQ_MAX)
83 e->frequency = CGRAPH_FREQ_MAX;
87 /* We removed or are going to remove the last call to NODE.
88 Return true if we can and want proactively remove the NODE now.
89 This is important to do, since we want inliner to know when offline
90 copy of function was removed. */
92 static bool
93 can_remove_node_now_p_1 (struct cgraph_node *node, struct cgraph_edge *e)
95 ipa_ref *ref;
97 FOR_EACH_ALIAS (node, ref)
99 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
100 if ((alias->callers && alias->callers != e)
101 || !can_remove_node_now_p_1 (alias, e))
102 return false;
104 /* FIXME: When address is taken of DECL_EXTERNAL function we still
105 can remove its offline copy, but we would need to keep unanalyzed node in
106 the callgraph so references can point to it.
108 Also for comdat group we can ignore references inside a group as we
109 want to prove the group as a whole to be dead. */
110 return (!node->address_taken
111 && node->can_remove_if_no_direct_calls_and_refs_p ()
112 /* Inlining might enable more devirtualizing, so we want to remove
113 those only after all devirtualizable virtual calls are processed.
114 Lacking may edges in callgraph we just preserve them post
115 inlining. */
116 && (!DECL_VIRTUAL_P (node->decl)
117 || !opt_for_fn (node->decl, flag_devirtualize))
118 /* During early inlining some unanalyzed cgraph nodes might be in the
119 callgraph and they might reffer the function in question. */
120 && !cgraph_new_nodes.exists ());
123 /* We are going to eliminate last direct call to NODE (or alias of it) via edge E.
124 Verify that the NODE can be removed from unit and if it is contained in comdat
125 group that the whole comdat group is removable. */
127 static bool
128 can_remove_node_now_p (struct cgraph_node *node, struct cgraph_edge *e)
130 struct cgraph_node *next;
131 if (!can_remove_node_now_p_1 (node, e))
132 return false;
134 /* When we see same comdat group, we need to be sure that all
135 items can be removed. */
136 if (!node->same_comdat_group || !node->externally_visible)
137 return true;
138 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
139 next != node; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
141 if (next->alias)
142 continue;
143 if ((next->callers && next->callers != e)
144 || !can_remove_node_now_p_1 (next, e))
145 return false;
147 return true;
150 /* Return true if NODE is a master clone with non-inline clones. */
152 static bool
153 master_clone_with_noninline_clones_p (struct cgraph_node *node)
155 if (node->clone_of)
156 return false;
158 for (struct cgraph_node *n = node->clones; n; n = n->next_sibling_clone)
159 if (n->decl != node->decl)
160 return true;
162 return false;
165 /* E is expected to be an edge being inlined. Clone destination node of
166 the edge and redirect it to the new clone.
167 DUPLICATE is used for bookkeeping on whether we are actually creating new
168 clones or re-using node originally representing out-of-line function call.
169 By default the offline copy is removed, when it appears dead after inlining.
170 UPDATE_ORIGINAL prevents this transformation.
171 If OVERALL_SIZE is non-NULL, the size is updated to reflect the
172 transformation.
173 FREQ_SCALE specify the scaling of frequencies of call sites. */
175 void
176 clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
177 bool update_original, int *overall_size, int freq_scale)
179 struct cgraph_node *inlining_into;
180 struct cgraph_edge *next;
182 if (e->caller->global.inlined_to)
183 inlining_into = e->caller->global.inlined_to;
184 else
185 inlining_into = e->caller;
187 if (duplicate)
189 /* We may eliminate the need for out-of-line copy to be output.
190 In that case just go ahead and re-use it. This is not just an
191 memory optimization. Making offline copy of fuction disappear
192 from the program will improve future decisions on inlining. */
193 if (!e->callee->callers->next_caller
194 /* Recursive inlining never wants the master clone to
195 be overwritten. */
196 && update_original
197 && can_remove_node_now_p (e->callee, e)
198 /* We cannot overwrite a master clone with non-inline clones
199 until after these clones are materialized. */
200 && !master_clone_with_noninline_clones_p (e->callee))
202 /* TODO: When callee is in a comdat group, we could remove all of it,
203 including all inline clones inlined into it. That would however
204 need small function inlining to register edge removal hook to
205 maintain the priority queue.
207 For now we keep the ohter functions in the group in program until
208 cgraph_remove_unreachable_functions gets rid of them. */
209 gcc_assert (!e->callee->global.inlined_to);
210 e->callee->remove_from_same_comdat_group ();
211 if (e->callee->definition
212 && inline_account_function_p (e->callee))
214 gcc_assert (!e->callee->alias);
215 if (overall_size)
216 *overall_size -= inline_summaries->get (e->callee)->size;
217 nfunctions_inlined++;
219 duplicate = false;
220 e->callee->externally_visible = false;
221 update_noncloned_frequencies (e->callee, e->frequency);
223 else
225 struct cgraph_node *n;
227 if (freq_scale == -1)
228 freq_scale = e->frequency;
229 n = e->callee->create_clone (e->callee->decl,
230 MIN (e->count, e->callee->count),
231 freq_scale,
232 update_original, vNULL, true,
233 inlining_into,
234 NULL);
235 n->used_as_abstract_origin = e->callee->used_as_abstract_origin;
236 e->redirect_callee (n);
239 else
240 e->callee->remove_from_same_comdat_group ();
242 e->callee->global.inlined_to = inlining_into;
244 /* Recursively clone all bodies. */
245 for (e = e->callee->callees; e; e = next)
247 next = e->next_callee;
248 if (!e->inline_failed)
249 clone_inlined_nodes (e, duplicate, update_original, overall_size, freq_scale);
253 /* Check all speculations in N and resolve them if they seems useless. */
255 static bool
256 check_speculations (cgraph_node *n)
258 bool speculation_removed = false;
259 cgraph_edge *next;
261 for (cgraph_edge *e = n->callees; e; e = next)
263 next = e->next_callee;
264 if (e->speculative && !speculation_useful_p (e, true))
266 e->resolve_speculation (NULL);
267 speculation_removed = true;
269 else if (!e->inline_failed)
270 speculation_removed |= check_speculations (e->callee);
272 return speculation_removed;
275 /* Mark all call graph edges coming out of NODE and all nodes that have been
276 inlined to it as in_polymorphic_cdtor. */
278 static void
279 mark_all_inlined_calls_cdtor (cgraph_node *node)
281 for (cgraph_edge *cs = node->callees; cs; cs = cs->next_callee)
283 cs->in_polymorphic_cdtor = true;
284 if (!cs->inline_failed)
285 mark_all_inlined_calls_cdtor (cs->callee);
287 for (cgraph_edge *cs = node->indirect_calls; cs; cs = cs->next_callee)
288 cs->in_polymorphic_cdtor = true;
292 /* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL
293 specify whether profile of original function should be updated. If any new
294 indirect edges are discovered in the process, add them to NEW_EDGES, unless
295 it is NULL. If UPDATE_OVERALL_SUMMARY is false, do not bother to recompute overall
296 size of caller after inlining. Caller is required to eventually do it via
297 inline_update_overall_summary.
298 If callee_removed is non-NULL, set it to true if we removed callee node.
300 Return true iff any new callgraph edges were discovered as a
301 result of inlining. */
303 bool
304 inline_call (struct cgraph_edge *e, bool update_original,
305 vec<cgraph_edge *> *new_edges,
306 int *overall_size, bool update_overall_summary,
307 bool *callee_removed)
309 int old_size = 0, new_size = 0;
310 struct cgraph_node *to = NULL;
311 struct cgraph_edge *curr = e;
312 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
313 bool new_edges_found = false;
315 /* This is used only for assert bellow. */
316 #if 0
317 int estimated_growth = estimate_edge_growth (e);
318 bool predicated = inline_edge_summary (e)->predicate != NULL;
319 #endif
321 /* Don't inline inlined edges. */
322 gcc_assert (e->inline_failed);
323 /* Don't even think of inlining inline clone. */
324 gcc_assert (!callee->global.inlined_to);
326 e->inline_failed = CIF_OK;
327 DECL_POSSIBLY_INLINED (callee->decl) = true;
329 to = e->caller;
330 if (to->global.inlined_to)
331 to = to->global.inlined_to;
333 if (DECL_FUNCTION_PERSONALITY (callee->decl))
334 DECL_FUNCTION_PERSONALITY (to->decl)
335 = DECL_FUNCTION_PERSONALITY (callee->decl);
337 /* If aliases are involved, redirect edge to the actual destination and
338 possibly remove the aliases. */
339 if (e->callee != callee)
341 struct cgraph_node *alias = e->callee, *next_alias;
342 e->redirect_callee (callee);
343 while (alias && alias != callee)
345 if (!alias->callers
346 && can_remove_node_now_p (alias,
347 !e->next_caller && !e->prev_caller ? e : NULL))
349 next_alias = alias->get_alias_target ();
350 alias->remove ();
351 if (callee_removed)
352 *callee_removed = true;
353 alias = next_alias;
355 else
356 break;
360 clone_inlined_nodes (e, true, update_original, overall_size, e->frequency);
362 gcc_assert (curr->callee->global.inlined_to == to);
364 old_size = inline_summaries->get (to)->size;
365 inline_merge_summary (e);
366 if (e->in_polymorphic_cdtor)
367 mark_all_inlined_calls_cdtor (e->callee);
368 if (opt_for_fn (e->caller->decl, optimize))
369 new_edges_found = ipa_propagate_indirect_call_infos (curr, new_edges);
370 check_speculations (e->callee);
371 if (update_overall_summary)
372 inline_update_overall_summary (to);
373 new_size = inline_summaries->get (to)->size;
375 if (callee->calls_comdat_local)
376 to->calls_comdat_local = true;
377 else if (to->calls_comdat_local && callee->comdat_local_p ())
379 struct cgraph_edge *se = to->callees;
380 for (; se; se = se->next_callee)
381 if (se->inline_failed && se->callee->comdat_local_p ())
382 break;
383 if (se == NULL)
384 to->calls_comdat_local = false;
387 /* FIXME: This assert suffers from roundoff errors, disable it for GCC 5
388 and revisit it after conversion to sreals in GCC 6.
389 See PR 65654. */
390 #if 0
391 /* Verify that estimated growth match real growth. Allow off-by-one
392 error due to INLINE_SIZE_SCALE roudoff errors. */
393 gcc_assert (!update_overall_summary || !overall_size || new_edges_found
394 || abs (estimated_growth - (new_size - old_size)) <= 1
395 || speculation_removed
396 /* FIXME: a hack. Edges with false predicate are accounted
397 wrong, we should remove them from callgraph. */
398 || predicated);
399 #endif
401 /* Account the change of overall unit size; external functions will be
402 removed and are thus not accounted. */
403 if (overall_size && inline_account_function_p (to))
404 *overall_size += new_size - old_size;
405 ncalls_inlined++;
407 /* This must happen after inline_merge_summary that rely on jump
408 functions of callee to not be updated. */
409 return new_edges_found;
413 /* Copy function body of NODE and redirect all inline clones to it.
414 This is done before inline plan is applied to NODE when there are
415 still some inline clones if it.
417 This is necessary because inline decisions are not really transitive
418 and the other inline clones may have different bodies. */
420 static struct cgraph_node *
421 save_inline_function_body (struct cgraph_node *node)
423 struct cgraph_node *first_clone, *n;
425 if (dump_file)
426 fprintf (dump_file, "\nSaving body of %s for later reuse\n",
427 node->name ());
429 gcc_assert (node == cgraph_node::get (node->decl));
431 /* first_clone will be turned into real function. */
432 first_clone = node->clones;
433 first_clone->decl = copy_node (node->decl);
434 first_clone->decl->decl_with_vis.symtab_node = first_clone;
435 gcc_assert (first_clone == cgraph_node::get (first_clone->decl));
437 /* Now reshape the clone tree, so all other clones descends from
438 first_clone. */
439 if (first_clone->next_sibling_clone)
441 for (n = first_clone->next_sibling_clone; n->next_sibling_clone; n = n->next_sibling_clone)
442 n->clone_of = first_clone;
443 n->clone_of = first_clone;
444 n->next_sibling_clone = first_clone->clones;
445 if (first_clone->clones)
446 first_clone->clones->prev_sibling_clone = n;
447 first_clone->clones = first_clone->next_sibling_clone;
448 first_clone->next_sibling_clone->prev_sibling_clone = NULL;
449 first_clone->next_sibling_clone = NULL;
450 gcc_assert (!first_clone->prev_sibling_clone);
452 first_clone->clone_of = NULL;
454 /* Now node in question has no clones. */
455 node->clones = NULL;
457 /* Inline clones share decl with the function they are cloned
458 from. Walk the whole clone tree and redirect them all to the
459 new decl. */
460 if (first_clone->clones)
461 for (n = first_clone->clones; n != first_clone;)
463 gcc_assert (n->decl == node->decl);
464 n->decl = first_clone->decl;
465 if (n->clones)
466 n = n->clones;
467 else if (n->next_sibling_clone)
468 n = n->next_sibling_clone;
469 else
471 while (n != first_clone && !n->next_sibling_clone)
472 n = n->clone_of;
473 if (n != first_clone)
474 n = n->next_sibling_clone;
478 /* Copy the OLD_VERSION_NODE function tree to the new version. */
479 tree_function_versioning (node->decl, first_clone->decl,
480 NULL, true, NULL, false,
481 NULL, NULL);
483 /* The function will be short lived and removed after we inline all the clones,
484 but make it internal so we won't confuse ourself. */
485 DECL_EXTERNAL (first_clone->decl) = 0;
486 TREE_PUBLIC (first_clone->decl) = 0;
487 DECL_COMDAT (first_clone->decl) = 0;
488 first_clone->ipa_transforms_to_apply.release ();
490 /* When doing recursive inlining, the clone may become unnecessary.
491 This is possible i.e. in the case when the recursive function is proved to be
492 non-throwing and the recursion happens only in the EH landing pad.
493 We can not remove the clone until we are done with saving the body.
494 Remove it now. */
495 if (!first_clone->callers)
497 first_clone->remove_symbol_and_inline_clones ();
498 first_clone = NULL;
500 #ifdef ENABLE_CHECKING
501 else
502 first_clone->verify ();
503 #endif
504 return first_clone;
507 /* Return true when function body of DECL still needs to be kept around
508 for later re-use. */
509 static bool
510 preserve_function_body_p (struct cgraph_node *node)
512 gcc_assert (symtab->global_info_ready);
513 gcc_assert (!node->alias && !node->thunk.thunk_p);
515 /* Look if there is any clone around. */
516 if (node->clones)
517 return true;
518 return false;
521 /* Apply inline plan to function. */
523 unsigned int
524 inline_transform (struct cgraph_node *node)
526 unsigned int todo = 0;
527 struct cgraph_edge *e, *next;
528 bool has_inline = false;
530 /* FIXME: Currently the pass manager is adding inline transform more than
531 once to some clones. This needs revisiting after WPA cleanups. */
532 if (cfun->after_inlining)
533 return 0;
535 /* We might need the body of this function so that we can expand
536 it inline somewhere else. */
537 if (preserve_function_body_p (node))
538 save_inline_function_body (node);
540 for (e = node->callees; e; e = next)
542 if (!e->inline_failed)
543 has_inline = true;
544 next = e->next_callee;
545 e->redirect_call_stmt_to_callee ();
547 node->remove_all_references ();
549 timevar_push (TV_INTEGRATION);
550 if (node->callees && (opt_for_fn (node->decl, optimize) || has_inline))
551 todo = optimize_inline_calls (current_function_decl);
552 timevar_pop (TV_INTEGRATION);
554 cfun->always_inline_functions_inlined = true;
555 cfun->after_inlining = true;
556 todo |= execute_fixup_cfg ();
558 if (!(todo & TODO_update_ssa_any))
559 /* Redirecting edges might lead to a need for vops to be recomputed. */
560 todo |= TODO_update_ssa_only_virtuals;
562 return todo;