[RS6000] dg-do !compile and scan-assembler
[official-gcc.git] / gcc / ipa-inline-transform.c
blob4df1b7fb9eebb447463607e2f4e26065c786d706
1 /* Callgraph transformations to handle inlining
2 Copyright (C) 2003-2020 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 "function.h"
36 #include "tree.h"
37 #include "alloc-pool.h"
38 #include "tree-pass.h"
39 #include "cgraph.h"
40 #include "tree-cfg.h"
41 #include "symbol-summary.h"
42 #include "tree-vrp.h"
43 #include "ipa-prop.h"
44 #include "ipa-fnsummary.h"
45 #include "ipa-inline.h"
46 #include "tree-inline.h"
47 #include "function.h"
48 #include "cfg.h"
49 #include "basic-block.h"
50 #include "ipa-utils.h"
51 #include "ipa-modref-tree.h"
52 #include "ipa-modref.h"
53 #include "symtab-thunks.h"
55 int ncalls_inlined;
56 int nfunctions_inlined;
58 /* Scale counts of NODE edges by NUM/DEN. */
60 static void
61 update_noncloned_counts (struct cgraph_node *node,
62 profile_count num, profile_count den)
64 struct cgraph_edge *e;
66 profile_count::adjust_for_ipa_scaling (&num, &den);
68 for (e = node->callees; e; e = e->next_callee)
70 if (!e->inline_failed)
71 update_noncloned_counts (e->callee, num, den);
72 e->count = e->count.apply_scale (num, den);
74 for (e = node->indirect_calls; e; e = e->next_callee)
75 e->count = e->count.apply_scale (num, den);
76 node->count = node->count.apply_scale (num, den);
79 /* We removed or are going to remove the last call to NODE.
80 Return true if we can and want proactively remove the NODE now.
81 This is important to do, since we want inliner to know when offline
82 copy of function was removed. */
84 static bool
85 can_remove_node_now_p_1 (struct cgraph_node *node, struct cgraph_edge *e)
87 ipa_ref *ref;
89 FOR_EACH_ALIAS (node, ref)
91 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
92 if ((alias->callers && alias->callers != e)
93 || !can_remove_node_now_p_1 (alias, e))
94 return false;
96 /* FIXME: When address is taken of DECL_EXTERNAL function we still
97 can remove its offline copy, but we would need to keep unanalyzed node in
98 the callgraph so references can point to it.
100 Also for comdat group we can ignore references inside a group as we
101 want to prove the group as a whole to be dead. */
102 return (!node->address_taken
103 && node->can_remove_if_no_direct_calls_and_refs_p ()
104 /* Inlining might enable more devirtualizing, so we want to remove
105 those only after all devirtualizable virtual calls are processed.
106 Lacking may edges in callgraph we just preserve them post
107 inlining. */
108 && (!DECL_VIRTUAL_P (node->decl)
109 || !opt_for_fn (node->decl, flag_devirtualize))
110 /* During early inlining some unanalyzed cgraph nodes might be in the
111 callgraph and they might refer the function in question. */
112 && !cgraph_new_nodes.exists ());
115 /* We are going to eliminate last direct call to NODE (or alias of it) via edge E.
116 Verify that the NODE can be removed from unit and if it is contained in comdat
117 group that the whole comdat group is removable. */
119 static bool
120 can_remove_node_now_p (struct cgraph_node *node, struct cgraph_edge *e)
122 struct cgraph_node *next;
123 if (!can_remove_node_now_p_1 (node, e))
124 return false;
126 /* When we see same comdat group, we need to be sure that all
127 items can be removed. */
128 if (!node->same_comdat_group || !node->externally_visible)
129 return true;
130 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
131 next != node; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
133 if (next->alias)
134 continue;
135 if ((next->callers && next->callers != e)
136 || !can_remove_node_now_p_1 (next, e))
137 return false;
139 return true;
142 /* Return true if NODE is a master clone with non-inline clones. */
144 static bool
145 master_clone_with_noninline_clones_p (struct cgraph_node *node)
147 if (node->clone_of)
148 return false;
150 for (struct cgraph_node *n = node->clones; n; n = n->next_sibling_clone)
151 if (n->decl != node->decl)
152 return true;
154 return false;
157 /* E is expected to be an edge being inlined. Clone destination node of
158 the edge and redirect it to the new clone.
159 DUPLICATE is used for bookkeeping on whether we are actually creating new
160 clones or re-using node originally representing out-of-line function call.
161 By default the offline copy is removed, when it appears dead after inlining.
162 UPDATE_ORIGINAL prevents this transformation.
163 If OVERALL_SIZE is non-NULL, the size is updated to reflect the
164 transformation. */
166 void
167 clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
168 bool update_original, int *overall_size)
170 struct cgraph_node *inlining_into;
171 struct cgraph_edge *next;
173 if (e->caller->inlined_to)
174 inlining_into = e->caller->inlined_to;
175 else
176 inlining_into = e->caller;
178 if (duplicate)
180 /* We may eliminate the need for out-of-line copy to be output.
181 In that case just go ahead and re-use it. This is not just an
182 memory optimization. Making offline copy of function disappear
183 from the program will improve future decisions on inlining. */
184 if (!e->callee->callers->next_caller
185 /* Recursive inlining never wants the master clone to
186 be overwritten. */
187 && update_original
188 && can_remove_node_now_p (e->callee, e)
189 /* We cannot overwrite a master clone with non-inline clones
190 until after these clones are materialized. */
191 && !master_clone_with_noninline_clones_p (e->callee))
193 /* TODO: When callee is in a comdat group, we could remove all of it,
194 including all inline clones inlined into it. That would however
195 need small function inlining to register edge removal hook to
196 maintain the priority queue.
198 For now we keep the other functions in the group in program until
199 cgraph_remove_unreachable_functions gets rid of them. */
200 gcc_assert (!e->callee->inlined_to);
201 e->callee->remove_from_same_comdat_group ();
202 if (e->callee->definition
203 && inline_account_function_p (e->callee))
205 gcc_assert (!e->callee->alias);
206 if (overall_size)
207 *overall_size -= ipa_size_summaries->get (e->callee)->size;
208 nfunctions_inlined++;
210 duplicate = false;
211 e->callee->externally_visible = false;
212 update_noncloned_counts (e->callee, e->count, e->callee->count);
214 dump_callgraph_transformation (e->callee, inlining_into,
215 "inlining to");
217 else
219 struct cgraph_node *n;
221 n = e->callee->create_clone (e->callee->decl,
222 e->count,
223 update_original, vNULL, true,
224 inlining_into,
225 NULL);
226 n->used_as_abstract_origin = e->callee->used_as_abstract_origin;
227 e->redirect_callee (n);
230 else
231 e->callee->remove_from_same_comdat_group ();
233 e->callee->inlined_to = inlining_into;
234 if (e->callee->ipa_transforms_to_apply.length ())
236 e->callee->ipa_transforms_to_apply.release ();
237 e->callee->ipa_transforms_to_apply = vNULL;
240 /* Recursively clone all bodies. */
241 for (e = e->callee->callees; e; e = next)
243 next = e->next_callee;
244 if (!e->inline_failed)
245 clone_inlined_nodes (e, duplicate, update_original, overall_size);
249 /* Check all speculations in N and if any seem useless, resolve them. When a
250 first edge is resolved, pop all edges from NEW_EDGES and insert them to
251 EDGE_SET. Then remove each resolved edge from EDGE_SET, if it is there. */
253 static bool
254 check_speculations_1 (cgraph_node *n, vec<cgraph_edge *> *new_edges,
255 hash_set <cgraph_edge *> *edge_set)
257 bool speculation_removed = false;
258 cgraph_edge *next;
260 for (cgraph_edge *e = n->callees; e; e = next)
262 next = e->next_callee;
263 if (e->speculative && !speculation_useful_p (e, true))
265 while (new_edges && !new_edges->is_empty ())
266 edge_set->add (new_edges->pop ());
267 edge_set->remove (e);
269 cgraph_edge::resolve_speculation (e, NULL);
270 speculation_removed = true;
272 else if (!e->inline_failed)
273 speculation_removed |= check_speculations_1 (e->callee, new_edges,
274 edge_set);
276 return speculation_removed;
279 /* Push E to NEW_EDGES. Called from hash_set traverse method, which
280 unfortunately means this function has to have external linkage, otherwise
281 the code will not compile with gcc 4.8. */
283 bool
284 push_all_edges_in_set_to_vec (cgraph_edge * const &e,
285 vec<cgraph_edge *> *new_edges)
287 new_edges->safe_push (e);
288 return true;
291 /* Check all speculations in N and if any seem useless, resolve them and remove
292 them from NEW_EDGES. */
294 static bool
295 check_speculations (cgraph_node *n, vec<cgraph_edge *> *new_edges)
297 hash_set <cgraph_edge *> edge_set;
298 bool res = check_speculations_1 (n, new_edges, &edge_set);
299 if (!edge_set.is_empty ())
300 edge_set.traverse <vec<cgraph_edge *> *,
301 push_all_edges_in_set_to_vec> (new_edges);
302 return res;
305 /* Mark all call graph edges coming out of NODE and all nodes that have been
306 inlined to it as in_polymorphic_cdtor. */
308 static void
309 mark_all_inlined_calls_cdtor (cgraph_node *node)
311 for (cgraph_edge *cs = node->callees; cs; cs = cs->next_callee)
313 cs->in_polymorphic_cdtor = true;
314 if (!cs->inline_failed)
315 mark_all_inlined_calls_cdtor (cs->callee);
317 for (cgraph_edge *cs = node->indirect_calls; cs; cs = cs->next_callee)
318 cs->in_polymorphic_cdtor = true;
322 /* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL
323 specify whether profile of original function should be updated. If any new
324 indirect edges are discovered in the process, add them to NEW_EDGES, unless
325 it is NULL. If UPDATE_OVERALL_SUMMARY is false, do not bother to recompute overall
326 size of caller after inlining. Caller is required to eventually do it via
327 ipa_update_overall_fn_summary.
328 If callee_removed is non-NULL, set it to true if we removed callee node.
330 Return true iff any new callgraph edges were discovered as a
331 result of inlining. */
333 bool
334 inline_call (struct cgraph_edge *e, bool update_original,
335 vec<cgraph_edge *> *new_edges,
336 int *overall_size, bool update_overall_summary,
337 bool *callee_removed)
339 int old_size = 0, new_size = 0;
340 struct cgraph_node *to = NULL;
341 struct cgraph_edge *curr = e;
342 bool comdat_local = e->callee->comdat_local_p ();
343 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
344 bool new_edges_found = false;
346 int estimated_growth = 0;
347 if (! update_overall_summary)
348 estimated_growth = estimate_edge_growth (e);
349 /* This is used only for assert bellow. */
350 #if 0
351 bool predicated = inline_edge_summary (e)->predicate != NULL;
352 #endif
354 /* Don't inline inlined edges. */
355 gcc_assert (e->inline_failed);
356 /* Don't even think of inlining inline clone. */
357 gcc_assert (!callee->inlined_to);
359 to = e->caller;
360 if (to->inlined_to)
361 to = to->inlined_to;
362 if (to->thunk)
364 struct cgraph_node *target = to->callees->callee;
365 thunk_expansion = true;
367 /* Remove all annotations, but keep thunk info. */
368 thunk_info info = *thunk_info::get (to);
369 symtab->call_cgraph_removal_hooks (to);
370 *thunk_info::get_create (to) = info;
371 if (in_lto_p)
372 to->get_untransformed_body ();
373 expand_thunk (to, false, true);
374 /* When thunk is instrumented we may have multiple callees. */
375 for (e = to->callees; e && e->callee != target; e = e->next_callee)
377 symtab->call_cgraph_insertion_hooks (to);
378 thunk_expansion = false;
379 gcc_assert (e);
383 e->inline_failed = CIF_OK;
384 DECL_POSSIBLY_INLINED (callee->decl) = true;
386 if (DECL_FUNCTION_PERSONALITY (callee->decl))
387 DECL_FUNCTION_PERSONALITY (to->decl)
388 = DECL_FUNCTION_PERSONALITY (callee->decl);
390 bool reload_optimization_node = false;
391 if (!opt_for_fn (callee->decl, flag_strict_aliasing)
392 && opt_for_fn (to->decl, flag_strict_aliasing))
394 struct gcc_options opts = global_options;
395 struct gcc_options opts_set = global_options_set;
397 cl_optimization_restore (&opts, &opts_set, opts_for_fn (to->decl));
398 opts.x_flag_strict_aliasing = false;
399 if (dump_file)
400 fprintf (dump_file, "Dropping flag_strict_aliasing on %s\n",
401 to->dump_name ());
402 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (to->decl)
403 = build_optimization_node (&opts, &opts_set);
404 reload_optimization_node = true;
407 ipa_fn_summary *caller_info = ipa_fn_summaries->get (to);
408 ipa_fn_summary *callee_info = ipa_fn_summaries->get (callee);
409 if (!caller_info->fp_expressions && callee_info->fp_expressions)
411 caller_info->fp_expressions = true;
412 if (opt_for_fn (callee->decl, flag_rounding_math)
413 != opt_for_fn (to->decl, flag_rounding_math)
414 || opt_for_fn (callee->decl, flag_trapping_math)
415 != opt_for_fn (to->decl, flag_trapping_math)
416 || opt_for_fn (callee->decl, flag_unsafe_math_optimizations)
417 != opt_for_fn (to->decl, flag_unsafe_math_optimizations)
418 || opt_for_fn (callee->decl, flag_finite_math_only)
419 != opt_for_fn (to->decl, flag_finite_math_only)
420 || opt_for_fn (callee->decl, flag_signaling_nans)
421 != opt_for_fn (to->decl, flag_signaling_nans)
422 || opt_for_fn (callee->decl, flag_cx_limited_range)
423 != opt_for_fn (to->decl, flag_cx_limited_range)
424 || opt_for_fn (callee->decl, flag_signed_zeros)
425 != opt_for_fn (to->decl, flag_signed_zeros)
426 || opt_for_fn (callee->decl, flag_associative_math)
427 != opt_for_fn (to->decl, flag_associative_math)
428 || opt_for_fn (callee->decl, flag_reciprocal_math)
429 != opt_for_fn (to->decl, flag_reciprocal_math)
430 || opt_for_fn (callee->decl, flag_fp_int_builtin_inexact)
431 != opt_for_fn (to->decl, flag_fp_int_builtin_inexact)
432 || opt_for_fn (callee->decl, flag_errno_math)
433 != opt_for_fn (to->decl, flag_errno_math))
435 struct gcc_options opts = global_options;
436 struct gcc_options opts_set = global_options_set;
438 cl_optimization_restore (&opts, &opts_set, opts_for_fn (to->decl));
439 opts.x_flag_rounding_math
440 = opt_for_fn (callee->decl, flag_rounding_math);
441 opts.x_flag_trapping_math
442 = opt_for_fn (callee->decl, flag_trapping_math);
443 opts.x_flag_unsafe_math_optimizations
444 = opt_for_fn (callee->decl, flag_unsafe_math_optimizations);
445 opts.x_flag_finite_math_only
446 = opt_for_fn (callee->decl, flag_finite_math_only);
447 opts.x_flag_signaling_nans
448 = opt_for_fn (callee->decl, flag_signaling_nans);
449 opts.x_flag_cx_limited_range
450 = opt_for_fn (callee->decl, flag_cx_limited_range);
451 opts.x_flag_signed_zeros
452 = opt_for_fn (callee->decl, flag_signed_zeros);
453 opts.x_flag_associative_math
454 = opt_for_fn (callee->decl, flag_associative_math);
455 opts.x_flag_reciprocal_math
456 = opt_for_fn (callee->decl, flag_reciprocal_math);
457 opts.x_flag_fp_int_builtin_inexact
458 = opt_for_fn (callee->decl, flag_fp_int_builtin_inexact);
459 opts.x_flag_errno_math
460 = opt_for_fn (callee->decl, flag_errno_math);
461 if (dump_file)
462 fprintf (dump_file, "Copying FP flags from %s to %s\n",
463 callee->dump_name (), to->dump_name ());
464 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (to->decl)
465 = build_optimization_node (&opts, &opts_set);
466 reload_optimization_node = true;
470 /* Reload global optimization flags. */
471 if (reload_optimization_node && DECL_STRUCT_FUNCTION (to->decl) == cfun)
472 set_cfun (cfun, true);
474 /* If aliases are involved, redirect edge to the actual destination and
475 possibly remove the aliases. */
476 if (e->callee != callee)
478 struct cgraph_node *alias = e->callee, *next_alias;
479 e->redirect_callee (callee);
480 while (alias && alias != callee)
482 if (!alias->callers
483 && can_remove_node_now_p (alias,
484 !e->next_caller && !e->prev_caller ? e : NULL))
486 next_alias = alias->get_alias_target ();
487 alias->remove ();
488 if (callee_removed)
489 *callee_removed = true;
490 alias = next_alias;
492 else
493 break;
497 clone_inlined_nodes (e, true, update_original, overall_size);
499 gcc_assert (curr->callee->inlined_to == to);
501 old_size = ipa_size_summaries->get (to)->size;
502 ipa_merge_modref_summary_after_inlining (e);
503 ipa_merge_fn_summary_after_inlining (e);
504 if (e->in_polymorphic_cdtor)
505 mark_all_inlined_calls_cdtor (e->callee);
506 if (opt_for_fn (e->caller->decl, optimize))
507 new_edges_found = ipa_propagate_indirect_call_infos (curr, new_edges);
508 bool removed_p = check_speculations (e->callee, new_edges);
509 if (update_overall_summary)
510 ipa_update_overall_fn_summary (to, new_edges_found || removed_p);
511 else
512 /* Update self size by the estimate so overall function growth limits
513 work for further inlining into this function. Before inlining
514 the function we inlined to again we expect the caller to update
515 the overall summary. */
516 ipa_size_summaries->get (to)->size += estimated_growth;
517 new_size = ipa_size_summaries->get (to)->size;
519 if (callee->calls_comdat_local)
520 to->calls_comdat_local = true;
521 else if (to->calls_comdat_local && comdat_local)
522 to->calls_comdat_local = to->check_calls_comdat_local_p ();
524 /* FIXME: This assert suffers from roundoff errors, disable it for GCC 5
525 and revisit it after conversion to sreals in GCC 6.
526 See PR 65654. */
527 #if 0
528 /* Verify that estimated growth match real growth. Allow off-by-one
529 error due to ipa_fn_summary::size_scale roudoff errors. */
530 gcc_assert (!update_overall_summary || !overall_size || new_edges_found
531 || abs (estimated_growth - (new_size - old_size)) <= 1
532 || speculation_removed
533 /* FIXME: a hack. Edges with false predicate are accounted
534 wrong, we should remove them from callgraph. */
535 || predicated);
536 #endif
538 /* Account the change of overall unit size; external functions will be
539 removed and are thus not accounted. */
540 if (overall_size && inline_account_function_p (to))
541 *overall_size += new_size - old_size;
542 ncalls_inlined++;
544 /* This must happen after ipa_merge_fn_summary_after_inlining that rely on jump
545 functions of callee to not be updated. */
546 return new_edges_found;
549 /* For each node that was made the holder of function body by
550 save_inline_function_body, this summary contains pointer to the previous
551 holder of the body. */
553 function_summary <tree *> *ipa_saved_clone_sources;
555 /* Copy function body of NODE and redirect all inline clones to it.
556 This is done before inline plan is applied to NODE when there are
557 still some inline clones if it.
559 This is necessary because inline decisions are not really transitive
560 and the other inline clones may have different bodies. */
562 static struct cgraph_node *
563 save_inline_function_body (struct cgraph_node *node)
565 struct cgraph_node *first_clone, *n;
567 if (dump_file)
568 fprintf (dump_file, "\nSaving body of %s for later reuse\n",
569 node->dump_name ());
571 gcc_assert (node == cgraph_node::get (node->decl));
573 /* first_clone will be turned into real function. */
574 first_clone = node->clones;
576 /* Arrange first clone to not be thunk as those do not have bodies. */
577 if (first_clone->thunk)
579 while (first_clone->thunk)
580 first_clone = first_clone->next_sibling_clone;
581 first_clone->prev_sibling_clone->next_sibling_clone
582 = first_clone->next_sibling_clone;
583 if (first_clone->next_sibling_clone)
584 first_clone->next_sibling_clone->prev_sibling_clone
585 = first_clone->prev_sibling_clone;
586 first_clone->next_sibling_clone = node->clones;
587 first_clone->prev_sibling_clone = NULL;
588 node->clones->prev_sibling_clone = first_clone;
589 node->clones = first_clone;
591 first_clone->decl = copy_node (node->decl);
592 first_clone->decl->decl_with_vis.symtab_node = first_clone;
593 gcc_assert (first_clone == cgraph_node::get (first_clone->decl));
595 /* Now reshape the clone tree, so all other clones descends from
596 first_clone. */
597 if (first_clone->next_sibling_clone)
599 for (n = first_clone->next_sibling_clone; n->next_sibling_clone;
600 n = n->next_sibling_clone)
601 n->clone_of = first_clone;
602 n->clone_of = first_clone;
603 n->next_sibling_clone = first_clone->clones;
604 if (first_clone->clones)
605 first_clone->clones->prev_sibling_clone = n;
606 first_clone->clones = first_clone->next_sibling_clone;
607 first_clone->next_sibling_clone->prev_sibling_clone = NULL;
608 first_clone->next_sibling_clone = NULL;
609 gcc_assert (!first_clone->prev_sibling_clone);
612 tree prev_body_holder = node->decl;
613 if (!ipa_saved_clone_sources)
615 ipa_saved_clone_sources = new function_summary <tree *> (symtab);
616 ipa_saved_clone_sources->disable_insertion_hook ();
618 else
620 tree *p = ipa_saved_clone_sources->get (node);
621 if (p)
623 prev_body_holder = *p;
624 gcc_assert (prev_body_holder);
627 *ipa_saved_clone_sources->get_create (first_clone) = prev_body_holder;
628 first_clone->former_clone_of
629 = node->former_clone_of ? node->former_clone_of : node->decl;
630 first_clone->clone_of = NULL;
632 /* Now node in question has no clones. */
633 node->clones = NULL;
635 /* Inline clones share decl with the function they are cloned
636 from. Walk the whole clone tree and redirect them all to the
637 new decl. */
638 if (first_clone->clones)
639 for (n = first_clone->clones; n != first_clone;)
641 gcc_assert (n->decl == node->decl);
642 n->decl = first_clone->decl;
643 if (n->clones)
644 n = n->clones;
645 else if (n->next_sibling_clone)
646 n = n->next_sibling_clone;
647 else
649 while (n != first_clone && !n->next_sibling_clone)
650 n = n->clone_of;
651 if (n != first_clone)
652 n = n->next_sibling_clone;
656 /* Copy the OLD_VERSION_NODE function tree to the new version. */
657 tree_function_versioning (node->decl, first_clone->decl,
658 NULL, NULL, true, NULL, NULL);
660 /* The function will be short lived and removed after we inline all the
661 clones, but make it internal so we won't confuse ourself. */
662 DECL_EXTERNAL (first_clone->decl) = 0;
663 TREE_PUBLIC (first_clone->decl) = 0;
664 DECL_COMDAT (first_clone->decl) = 0;
665 first_clone->ipa_transforms_to_apply.release ();
667 /* When doing recursive inlining, the clone may become unnecessary.
668 This is possible i.e. in the case when the recursive function is proved to
669 be non-throwing and the recursion happens only in the EH landing pad.
670 We cannot remove the clone until we are done with saving the body.
671 Remove it now. */
672 if (!first_clone->callers)
674 first_clone->remove_symbol_and_inline_clones ();
675 first_clone = NULL;
677 else if (flag_checking)
678 first_clone->verify ();
680 return first_clone;
683 /* Return true when function body of DECL still needs to be kept around
684 for later re-use. */
685 static bool
686 preserve_function_body_p (struct cgraph_node *node)
688 gcc_assert (symtab->global_info_ready);
689 gcc_assert (!node->alias && !node->thunk);
691 /* Look if there is any non-thunk clone around. */
692 for (node = node->clones; node; node = node->next_sibling_clone)
693 if (!node->thunk)
694 return true;
695 return false;
698 /* Apply inline plan to function. */
700 unsigned int
701 inline_transform (struct cgraph_node *node)
703 unsigned int todo = 0;
704 struct cgraph_edge *e, *next;
705 bool has_inline = false;
707 /* FIXME: Currently the pass manager is adding inline transform more than
708 once to some clones. This needs revisiting after WPA cleanups. */
709 if (cfun->after_inlining)
710 return 0;
712 cgraph_node *next_clone;
713 for (cgraph_node *n = node->clones; n; n = next_clone)
715 next_clone = n->next_sibling_clone;
716 if (n->decl != node->decl)
717 n->materialize_clone ();
719 node->clear_stmts_in_references ();
721 /* We might need the body of this function so that we can expand
722 it inline somewhere else. */
723 if (preserve_function_body_p (node))
724 save_inline_function_body (node);
726 profile_count num = node->count;
727 profile_count den = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
728 bool scale = num.initialized_p () && !(num == den);
729 if (scale)
731 profile_count::adjust_for_ipa_scaling (&num, &den);
732 if (dump_file)
734 fprintf (dump_file, "Applying count scale ");
735 num.dump (dump_file);
736 fprintf (dump_file, "/");
737 den.dump (dump_file);
738 fprintf (dump_file, "\n");
741 basic_block bb;
742 cfun->cfg->count_max = profile_count::uninitialized ();
743 FOR_ALL_BB_FN (bb, cfun)
745 bb->count = bb->count.apply_scale (num, den);
746 cfun->cfg->count_max = cfun->cfg->count_max.max (bb->count);
748 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = node->count;
751 for (e = node->callees; e; e = next)
753 if (!e->inline_failed)
754 has_inline = true;
755 next = e->next_callee;
756 cgraph_edge::redirect_call_stmt_to_callee (e);
758 node->remove_all_references ();
760 timevar_push (TV_INTEGRATION);
761 if (node->callees && (opt_for_fn (node->decl, optimize) || has_inline))
763 todo = optimize_inline_calls (current_function_decl);
765 timevar_pop (TV_INTEGRATION);
767 cfun->always_inline_functions_inlined = true;
768 cfun->after_inlining = true;
769 todo |= execute_fixup_cfg ();
771 if (!(todo & TODO_update_ssa_any))
772 /* Redirecting edges might lead to a need for vops to be recomputed. */
773 todo |= TODO_update_ssa_only_virtuals;
775 return todo;