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