* ipa-inline-transform.c (master_clone_with_noninline_clones_p): New.
[official-gcc.git] / gcc / ipa-inline-transform.c
blob1cb4c0520e83d7fe121b6debcd0e8c6a57bafe0a
1 /* Callgraph transformations to handle inlining
2 Copyright (C) 2003-2014 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 "tree.h"
36 #include "langhooks.h"
37 #include "intl.h"
38 #include "coverage.h"
39 #include "ggc.h"
40 #include "tree-cfg.h"
41 #include "ipa-prop.h"
42 #include "ipa-inline.h"
43 #include "tree-inline.h"
44 #include "tree-pass.h"
46 int ncalls_inlined;
47 int nfunctions_inlined;
48 bool speculation_removed;
50 /* Scale frequency of NODE edges by FREQ_SCALE. */
52 static void
53 update_noncloned_frequencies (struct cgraph_node *node,
54 int freq_scale)
56 struct cgraph_edge *e;
58 /* We do not want to ignore high loop nest after freq drops to 0. */
59 if (!freq_scale)
60 freq_scale = 1;
61 for (e = node->callees; e; e = e->next_callee)
63 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
64 if (e->frequency > CGRAPH_FREQ_MAX)
65 e->frequency = CGRAPH_FREQ_MAX;
66 if (!e->inline_failed)
67 update_noncloned_frequencies (e->callee, freq_scale);
69 for (e = node->indirect_calls; e; e = e->next_callee)
71 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
72 if (e->frequency > CGRAPH_FREQ_MAX)
73 e->frequency = CGRAPH_FREQ_MAX;
77 /* We removed or are going to remove the last call to NODE.
78 Return true if we can and want proactively remove the NODE now.
79 This is important to do, since we want inliner to know when offline
80 copy of function was removed. */
82 static bool
83 can_remove_node_now_p_1 (struct cgraph_node *node)
85 /* FIXME: When address is taken of DECL_EXTERNAL function we still
86 can remove its offline copy, but we would need to keep unanalyzed node in
87 the callgraph so references can point to it. */
88 return (!node->address_taken
89 && !node->has_aliases_p ()
90 && !node->used_as_abstract_origin
91 && node->can_remove_if_no_direct_calls_p ()
92 /* Inlining might enable more devirtualizing, so we want to remove
93 those only after all devirtualizable virtual calls are processed.
94 Lacking may edges in callgraph we just preserve them post
95 inlining. */
96 && !DECL_VIRTUAL_P (node->decl)
97 /* During early inlining some unanalyzed cgraph nodes might be in the
98 callgraph and they might reffer the function in question. */
99 && !cgraph_new_nodes.exists ());
102 /* We are going to eliminate last direct call to NODE (or alias of it) via edge E.
103 Verify that the NODE can be removed from unit and if it is contained in comdat
104 group that the whole comdat group is removable. */
106 static bool
107 can_remove_node_now_p (struct cgraph_node *node, struct cgraph_edge *e)
109 struct cgraph_node *next;
110 if (!can_remove_node_now_p_1 (node))
111 return false;
113 /* When we see same comdat group, we need to be sure that all
114 items can be removed. */
115 if (!node->same_comdat_group)
116 return true;
117 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
118 next != node; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
119 if ((next->callers && next->callers != e)
120 || !can_remove_node_now_p_1 (next))
121 return false;
122 return true;
125 /* Return true if NODE is a master clone with non-inline clones. */
127 static bool
128 master_clone_with_noninline_clones_p (struct cgraph_node *node)
130 if (node->clone_of)
131 return false;
133 for (struct cgraph_node *n = node->clones; n; n = n->next_sibling_clone)
134 if (n->decl != node->decl)
135 return true;
137 return false;
140 /* E is expected to be an edge being inlined. Clone destination node of
141 the edge and redirect it to the new clone.
142 DUPLICATE is used for bookkeeping on whether we are actually creating new
143 clones or re-using node originally representing out-of-line function call.
144 By default the offline copy is removed, when it appears dead after inlining.
145 UPDATE_ORIGINAL prevents this transformation.
146 If OVERALL_SIZE is non-NULL, the size is updated to reflect the
147 transformation.
148 FREQ_SCALE specify the scaling of frequencies of call sites. */
150 void
151 clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
152 bool update_original, int *overall_size, int freq_scale)
154 struct cgraph_node *inlining_into;
155 struct cgraph_edge *next;
157 if (e->caller->global.inlined_to)
158 inlining_into = e->caller->global.inlined_to;
159 else
160 inlining_into = e->caller;
162 if (duplicate)
164 /* We may eliminate the need for out-of-line copy to be output.
165 In that case just go ahead and re-use it. This is not just an
166 memory optimization. Making offline copy of fuction disappear
167 from the program will improve future decisions on inlining. */
168 if (!e->callee->callers->next_caller
169 /* Recursive inlining never wants the master clone to
170 be overwritten. */
171 && update_original
172 && can_remove_node_now_p (e->callee, e)
173 /* We cannot overwrite a master clone with non-inline clones
174 until after these clones are materialized. */
175 && !master_clone_with_noninline_clones_p (e->callee))
177 /* TODO: When callee is in a comdat group, we could remove all of it,
178 including all inline clones inlined into it. That would however
179 need small function inlining to register edge removal hook to
180 maintain the priority queue.
182 For now we keep the ohter functions in the group in program until
183 cgraph_remove_unreachable_functions gets rid of them. */
184 gcc_assert (!e->callee->global.inlined_to);
185 e->callee->dissolve_same_comdat_group_list ();
186 if (e->callee->definition && !DECL_EXTERNAL (e->callee->decl))
188 if (overall_size)
189 *overall_size -= inline_summary (e->callee)->size;
190 nfunctions_inlined++;
192 duplicate = false;
193 e->callee->externally_visible = false;
194 update_noncloned_frequencies (e->callee, e->frequency);
196 else
198 struct cgraph_node *n;
200 if (freq_scale == -1)
201 freq_scale = e->frequency;
202 n = e->callee->create_clone (e->callee->decl,
203 MIN (e->count, e->callee->count),
204 freq_scale,
205 update_original, vNULL, true,
206 inlining_into,
207 NULL);
208 e->redirect_callee (n);
211 else
212 e->callee->dissolve_same_comdat_group_list ();
214 e->callee->global.inlined_to = inlining_into;
216 /* Recursively clone all bodies. */
217 for (e = e->callee->callees; e; e = next)
219 next = e->next_callee;
220 if (!e->inline_failed)
221 clone_inlined_nodes (e, duplicate, update_original, overall_size, freq_scale);
222 if (e->speculative && !speculation_useful_p (e, true))
224 e->resolve_speculation (NULL);
225 speculation_removed = true;
231 /* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL
232 specify whether profile of original function should be updated. If any new
233 indirect edges are discovered in the process, add them to NEW_EDGES, unless
234 it is NULL. If UPDATE_OVERALL_SUMMARY is false, do not bother to recompute overall
235 size of caller after inlining. Caller is required to eventually do it via
236 inline_update_overall_summary.
237 If callee_removed is non-NULL, set it to true if we removed callee node.
239 Return true iff any new callgraph edges were discovered as a
240 result of inlining. */
242 bool
243 inline_call (struct cgraph_edge *e, bool update_original,
244 vec<cgraph_edge *> *new_edges,
245 int *overall_size, bool update_overall_summary,
246 bool *callee_removed)
248 int old_size = 0, new_size = 0;
249 struct cgraph_node *to = NULL;
250 struct cgraph_edge *curr = e;
251 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
252 bool new_edges_found = false;
254 #ifdef ENABLE_CHECKING
255 int estimated_growth = estimate_edge_growth (e);
256 bool predicated = inline_edge_summary (e)->predicate != NULL;
257 #endif
259 speculation_removed = false;
260 /* Don't inline inlined edges. */
261 gcc_assert (e->inline_failed);
262 /* Don't even think of inlining inline clone. */
263 gcc_assert (!callee->global.inlined_to);
265 e->inline_failed = CIF_OK;
266 DECL_POSSIBLY_INLINED (callee->decl) = true;
268 to = e->caller;
269 if (to->global.inlined_to)
270 to = to->global.inlined_to;
272 /* If aliases are involved, redirect edge to the actual destination and
273 possibly remove the aliases. */
274 if (e->callee != callee)
276 struct cgraph_node *alias = e->callee, *next_alias;
277 e->redirect_callee (callee);
278 while (alias && alias != callee)
280 if (!alias->callers
281 && can_remove_node_now_p (alias, e))
283 next_alias = alias->get_alias_target ();
284 alias->remove ();
285 if (callee_removed)
286 *callee_removed = true;
287 alias = next_alias;
289 else
290 break;
294 clone_inlined_nodes (e, true, update_original, overall_size, e->frequency);
296 gcc_assert (curr->callee->global.inlined_to == to);
298 old_size = inline_summary (to)->size;
299 inline_merge_summary (e);
300 if (optimize)
301 new_edges_found = ipa_propagate_indirect_call_infos (curr, new_edges);
302 if (update_overall_summary)
303 inline_update_overall_summary (to);
304 new_size = inline_summary (to)->size;
306 if (callee->calls_comdat_local)
307 to->calls_comdat_local = true;
308 else if (to->calls_comdat_local && callee->comdat_local_p ())
310 struct cgraph_edge *se = to->callees;
311 for (; se; se = se->next_callee)
312 if (se->inline_failed && se->callee->comdat_local_p ())
313 break;
314 if (se == NULL)
315 to->calls_comdat_local = false;
318 #ifdef ENABLE_CHECKING
319 /* Verify that estimated growth match real growth. Allow off-by-one
320 error due to INLINE_SIZE_SCALE roudoff errors. */
321 gcc_assert (!update_overall_summary || !overall_size || new_edges_found
322 || abs (estimated_growth - (new_size - old_size)) <= 1
323 || speculation_removed
324 /* FIXME: a hack. Edges with false predicate are accounted
325 wrong, we should remove them from callgraph. */
326 || predicated);
327 #endif
329 /* Account the change of overall unit size; external functions will be
330 removed and are thus not accounted. */
331 if (overall_size
332 && !DECL_EXTERNAL (to->decl))
333 *overall_size += new_size - old_size;
334 ncalls_inlined++;
336 /* This must happen after inline_merge_summary that rely on jump
337 functions of callee to not be updated. */
338 return new_edges_found;
342 /* Copy function body of NODE and redirect all inline clones to it.
343 This is done before inline plan is applied to NODE when there are
344 still some inline clones if it.
346 This is necessary because inline decisions are not really transitive
347 and the other inline clones may have different bodies. */
349 static struct cgraph_node *
350 save_inline_function_body (struct cgraph_node *node)
352 struct cgraph_node *first_clone, *n;
354 if (dump_file)
355 fprintf (dump_file, "\nSaving body of %s for later reuse\n",
356 node->name ());
358 gcc_assert (node == cgraph_node::get (node->decl));
360 /* first_clone will be turned into real function. */
361 first_clone = node->clones;
362 first_clone->decl = copy_node (node->decl);
363 first_clone->decl->decl_with_vis.symtab_node = first_clone;
364 gcc_assert (first_clone == cgraph_node::get (first_clone->decl));
366 /* Now reshape the clone tree, so all other clones descends from
367 first_clone. */
368 if (first_clone->next_sibling_clone)
370 for (n = first_clone->next_sibling_clone; n->next_sibling_clone; n = n->next_sibling_clone)
371 n->clone_of = first_clone;
372 n->clone_of = first_clone;
373 n->next_sibling_clone = first_clone->clones;
374 if (first_clone->clones)
375 first_clone->clones->prev_sibling_clone = n;
376 first_clone->clones = first_clone->next_sibling_clone;
377 first_clone->next_sibling_clone->prev_sibling_clone = NULL;
378 first_clone->next_sibling_clone = NULL;
379 gcc_assert (!first_clone->prev_sibling_clone);
381 first_clone->clone_of = NULL;
383 /* Now node in question has no clones. */
384 node->clones = NULL;
386 /* Inline clones share decl with the function they are cloned
387 from. Walk the whole clone tree and redirect them all to the
388 new decl. */
389 if (first_clone->clones)
390 for (n = first_clone->clones; n != first_clone;)
392 gcc_assert (n->decl == node->decl);
393 n->decl = first_clone->decl;
394 if (n->clones)
395 n = n->clones;
396 else if (n->next_sibling_clone)
397 n = n->next_sibling_clone;
398 else
400 while (n != first_clone && !n->next_sibling_clone)
401 n = n->clone_of;
402 if (n != first_clone)
403 n = n->next_sibling_clone;
407 /* Copy the OLD_VERSION_NODE function tree to the new version. */
408 tree_function_versioning (node->decl, first_clone->decl,
409 NULL, true, NULL, false,
410 NULL, NULL);
412 /* The function will be short lived and removed after we inline all the clones,
413 but make it internal so we won't confuse ourself. */
414 DECL_EXTERNAL (first_clone->decl) = 0;
415 TREE_PUBLIC (first_clone->decl) = 0;
416 DECL_COMDAT (first_clone->decl) = 0;
417 first_clone->ipa_transforms_to_apply.release ();
419 /* When doing recursive inlining, the clone may become unnecessary.
420 This is possible i.e. in the case when the recursive function is proved to be
421 non-throwing and the recursion happens only in the EH landing pad.
422 We can not remove the clone until we are done with saving the body.
423 Remove it now. */
424 if (!first_clone->callers)
426 first_clone->remove_symbol_and_inline_clones ();
427 first_clone = NULL;
429 #ifdef ENABLE_CHECKING
430 else
431 first_clone->verify ();
432 #endif
433 return first_clone;
436 /* Return true when function body of DECL still needs to be kept around
437 for later re-use. */
438 static bool
439 preserve_function_body_p (struct cgraph_node *node)
441 gcc_assert (symtab->global_info_ready);
442 gcc_assert (!node->alias && !node->thunk.thunk_p);
444 /* Look if there is any clone around. */
445 if (node->clones)
446 return true;
447 return false;
450 /* Apply inline plan to function. */
452 unsigned int
453 inline_transform (struct cgraph_node *node)
455 unsigned int todo = 0;
456 struct cgraph_edge *e, *next;
458 /* FIXME: Currently the pass manager is adding inline transform more than
459 once to some clones. This needs revisiting after WPA cleanups. */
460 if (cfun->after_inlining)
461 return 0;
463 /* We might need the body of this function so that we can expand
464 it inline somewhere else. */
465 if (preserve_function_body_p (node))
466 save_inline_function_body (node);
468 for (e = node->callees; e; e = next)
470 next = e->next_callee;
471 e->redirect_call_stmt_to_callee ();
473 node->remove_all_references ();
475 timevar_push (TV_INTEGRATION);
476 if (node->callees && optimize)
477 todo = optimize_inline_calls (current_function_decl);
478 timevar_pop (TV_INTEGRATION);
480 cfun->always_inline_functions_inlined = true;
481 cfun->after_inlining = true;
482 todo |= execute_fixup_cfg ();
484 if (!(todo & TODO_update_ssa_any))
485 /* Redirecting edges might lead to a need for vops to be recomputed. */
486 todo |= TODO_update_ssa_only_virtuals;
488 return todo;