]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ipa-inline-transform.c
2019-01-09 Sandra Loosemore <sandra@codesourcery.com>
[thirdparty/gcc.git] / gcc / ipa-inline-transform.c
CommitLineData
8cbc43ff 1/* Callgraph transformations to handle inlining
fbd26352 2 Copyright (C) 2003-2019 Free Software Foundation, Inc.
8cbc43ff 3 Contributed by Jan Hubicka
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21/* The inline decisions are stored in callgraph in "inline plan" and
22 applied later.
23
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.
28
29 The inline plan is applied on given function body by inline_transform. */
30
31#include "config.h"
32#include "system.h"
33#include "coretypes.h"
34#include "tm.h"
7c29e30e 35#include "function.h"
8cbc43ff 36#include "tree.h"
7c29e30e 37#include "alloc-pool.h"
38#include "tree-pass.h"
39#include "cgraph.h"
073c1fd5 40#include "tree-cfg.h"
2cc80ac3 41#include "symbol-summary.h"
25a8e007 42#include "tree-vrp.h"
8cbc43ff 43#include "ipa-prop.h"
b9a58fc5 44#include "ipa-fnsummary.h"
8cbc43ff 45#include "ipa-inline.h"
46#include "tree-inline.h"
6e4303bf 47#include "function.h"
48#include "cfg.h"
49#include "basic-block.h"
8cbc43ff 50
51int ncalls_inlined;
52int nfunctions_inlined;
53
151b9ff5 54/* Scale counts of NODE edges by NUM/DEN. */
8cbc43ff 55
56static void
151b9ff5 57update_noncloned_counts (struct cgraph_node *node,
58 profile_count num, profile_count den)
8cbc43ff 59{
60 struct cgraph_edge *e;
84895d28 61
371858d4 62 profile_count::adjust_for_ipa_scaling (&num, &den);
8cbc43ff 63
8cbc43ff 64 for (e = node->callees; e; e = e->next_callee)
65 {
8cbc43ff 66 if (!e->inline_failed)
151b9ff5 67 update_noncloned_counts (e->callee, num, den);
84895d28 68 e->count = e->count.apply_scale (num, den);
0835ad03 69 }
70 for (e = node->indirect_calls; e; e = e->next_callee)
151b9ff5 71 e->count = e->count.apply_scale (num, den);
84895d28 72 node->count = node->count.apply_scale (num, den);
8cbc43ff 73}
74
82626cb0 75/* We removed or are going to remove the last call to NODE.
76 Return true if we can and want proactively remove the NODE now.
77 This is important to do, since we want inliner to know when offline
78 copy of function was removed. */
79
80static bool
7fbf53b8 81can_remove_node_now_p_1 (struct cgraph_node *node, struct cgraph_edge *e)
82626cb0 82{
7fbf53b8 83 ipa_ref *ref;
84
85 FOR_EACH_ALIAS (node, ref)
86 {
87 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
88 if ((alias->callers && alias->callers != e)
89 || !can_remove_node_now_p_1 (alias, e))
90 return false;
91 }
82626cb0 92 /* FIXME: When address is taken of DECL_EXTERNAL function we still
93 can remove its offline copy, but we would need to keep unanalyzed node in
e88fecaf 94 the callgraph so references can point to it.
95
96 Also for comdat group we can ignore references inside a group as we
97 want to prove the group as a whole to be dead. */
02774f2d 98 return (!node->address_taken
e88fecaf 99 && node->can_remove_if_no_direct_calls_and_refs_p ()
82626cb0 100 /* Inlining might enable more devirtualizing, so we want to remove
101 those only after all devirtualizable virtual calls are processed.
102 Lacking may edges in callgraph we just preserve them post
103 inlining. */
7fbf53b8 104 && (!DECL_VIRTUAL_P (node->decl)
105 || !opt_for_fn (node->decl, flag_devirtualize))
82626cb0 106 /* During early inlining some unanalyzed cgraph nodes might be in the
107 callgraph and they might reffer the function in question. */
347a47cb 108 && !cgraph_new_nodes.exists ());
82626cb0 109}
110
7791b0eb 111/* We are going to eliminate last direct call to NODE (or alias of it) via edge E.
112 Verify that the NODE can be removed from unit and if it is contained in comdat
113 group that the whole comdat group is removable. */
114
115static bool
116can_remove_node_now_p (struct cgraph_node *node, struct cgraph_edge *e)
117{
118 struct cgraph_node *next;
7fbf53b8 119 if (!can_remove_node_now_p_1 (node, e))
7791b0eb 120 return false;
121
122 /* When we see same comdat group, we need to be sure that all
123 items can be removed. */
6365c927 124 if (!node->same_comdat_group || !node->externally_visible)
7791b0eb 125 return true;
415d1b9a 126 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
127 next != node; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
7fbf53b8 128 {
129 if (next->alias)
130 continue;
131 if ((next->callers && next->callers != e)
132 || !can_remove_node_now_p_1 (next, e))
133 return false;
134 }
7791b0eb 135 return true;
136}
137
0ecf4b03 138/* Return true if NODE is a master clone with non-inline clones. */
139
140static bool
141master_clone_with_noninline_clones_p (struct cgraph_node *node)
142{
143 if (node->clone_of)
144 return false;
145
146 for (struct cgraph_node *n = node->clones; n; n = n->next_sibling_clone)
147 if (n->decl != node->decl)
148 return true;
149
150 return false;
151}
8cbc43ff 152
153/* E is expected to be an edge being inlined. Clone destination node of
154 the edge and redirect it to the new clone.
155 DUPLICATE is used for bookkeeping on whether we are actually creating new
156 clones or re-using node originally representing out-of-line function call.
b8731470 157 By default the offline copy is removed, when it appears dead after inlining.
158 UPDATE_ORIGINAL prevents this transformation.
159 If OVERALL_SIZE is non-NULL, the size is updated to reflect the
151b9ff5 160 transformation. */
8cbc43ff 161
162void
163clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
151b9ff5 164 bool update_original, int *overall_size)
8cbc43ff 165{
48f42a9a 166 struct cgraph_node *inlining_into;
12d5ae9f 167 struct cgraph_edge *next;
48f42a9a 168
169 if (e->caller->global.inlined_to)
170 inlining_into = e->caller->global.inlined_to;
171 else
172 inlining_into = e->caller;
173
8cbc43ff 174 if (duplicate)
175 {
176 /* We may eliminate the need for out-of-line copy to be output.
177 In that case just go ahead and re-use it. This is not just an
178 memory optimization. Making offline copy of fuction disappear
179 from the program will improve future decisions on inlining. */
180 if (!e->callee->callers->next_caller
181 /* Recursive inlining never wants the master clone to
182 be overwritten. */
183 && update_original
0ecf4b03 184 && can_remove_node_now_p (e->callee, e)
185 /* We cannot overwrite a master clone with non-inline clones
186 until after these clones are materialized. */
187 && !master_clone_with_noninline_clones_p (e->callee))
8cbc43ff 188 {
7791b0eb 189 /* TODO: When callee is in a comdat group, we could remove all of it,
190 including all inline clones inlined into it. That would however
191 need small function inlining to register edge removal hook to
192 maintain the priority queue.
193
194 For now we keep the ohter functions in the group in program until
195 cgraph_remove_unreachable_functions gets rid of them. */
8cbc43ff 196 gcc_assert (!e->callee->global.inlined_to);
ab89cd93 197 e->callee->remove_from_same_comdat_group ();
a6d60179 198 if (e->callee->definition
199 && inline_account_function_p (e->callee))
8cbc43ff 200 {
a6d60179 201 gcc_assert (!e->callee->alias);
8cbc43ff 202 if (overall_size)
d2c2513e 203 *overall_size -= ipa_fn_summaries->get (e->callee)->size;
8cbc43ff 204 nfunctions_inlined++;
205 }
206 duplicate = false;
02774f2d 207 e->callee->externally_visible = false;
151b9ff5 208 update_noncloned_counts (e->callee, e->count, e->callee->count);
9dc70d59 209
210 dump_callgraph_transformation (e->callee, inlining_into,
211 "inlining to");
8cbc43ff 212 }
213 else
214 {
215 struct cgraph_node *n;
b8731470 216
415d1b9a 217 n = e->callee->create_clone (e->callee->decl,
151b9ff5 218 e->count,
415d1b9a 219 update_original, vNULL, true,
220 inlining_into,
221 NULL);
ca92a251 222 n->used_as_abstract_origin = e->callee->used_as_abstract_origin;
35ee1c66 223 e->redirect_callee (n);
8cbc43ff 224 }
225 }
cf951b1a 226 else
ab89cd93 227 e->callee->remove_from_same_comdat_group ();
8cbc43ff 228
48f42a9a 229 e->callee->global.inlined_to = inlining_into;
8cbc43ff 230
231 /* Recursively clone all bodies. */
12d5ae9f 232 for (e = e->callee->callees; e; e = next)
233 {
234 next = e->next_callee;
235 if (!e->inline_failed)
151b9ff5 236 clone_inlined_nodes (e, duplicate, update_original, overall_size);
7ab096e0 237 }
238}
239
240/* Check all speculations in N and resolve them if they seems useless. */
241
242static bool
243check_speculations (cgraph_node *n)
244{
245 bool speculation_removed = false;
246 cgraph_edge *next;
247
248 for (cgraph_edge *e = n->callees; e; e = next)
249 {
250 next = e->next_callee;
12d5ae9f 251 if (e->speculative && !speculation_useful_p (e, true))
252 {
35ee1c66 253 e->resolve_speculation (NULL);
12d5ae9f 254 speculation_removed = true;
255 }
7ab096e0 256 else if (!e->inline_failed)
257 speculation_removed |= check_speculations (e->callee);
12d5ae9f 258 }
7ab096e0 259 return speculation_removed;
8cbc43ff 260}
261
2bec7365 262/* Mark all call graph edges coming out of NODE and all nodes that have been
263 inlined to it as in_polymorphic_cdtor. */
264
265static void
266mark_all_inlined_calls_cdtor (cgraph_node *node)
267{
268 for (cgraph_edge *cs = node->callees; cs; cs = cs->next_callee)
269 {
270 cs->in_polymorphic_cdtor = true;
271 if (!cs->inline_failed)
6e4303bf 272 mark_all_inlined_calls_cdtor (cs->callee);
2bec7365 273 }
274 for (cgraph_edge *cs = node->indirect_calls; cs; cs = cs->next_callee)
275 cs->in_polymorphic_cdtor = true;
276}
277
8cbc43ff 278
279/* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL
280 specify whether profile of original function should be updated. If any new
281 indirect edges are discovered in the process, add them to NEW_EDGES, unless
6331b6fa 282 it is NULL. If UPDATE_OVERALL_SUMMARY is false, do not bother to recompute overall
283 size of caller after inlining. Caller is required to eventually do it via
1297cbcd 284 ipa_update_overall_fn_summary.
7c5c01f1 285 If callee_removed is non-NULL, set it to true if we removed callee node.
6331b6fa 286
287 Return true iff any new callgraph edges were discovered as a
8cbc43ff 288 result of inlining. */
289
290bool
291inline_call (struct cgraph_edge *e, bool update_original,
415d1b9a 292 vec<cgraph_edge *> *new_edges,
7c5c01f1 293 int *overall_size, bool update_overall_summary,
294 bool *callee_removed)
8cbc43ff 295{
296 int old_size = 0, new_size = 0;
297 struct cgraph_node *to = NULL;
298 struct cgraph_edge *curr = e;
415d1b9a 299 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
18b64b34 300 bool new_edges_found = false;
301
6ec9c8c5 302 int estimated_growth = 0;
303 if (! update_overall_summary)
304 estimated_growth = estimate_edge_growth (e);
6737c56a 305 /* This is used only for assert bellow. */
306#if 0
18b64b34 307 bool predicated = inline_edge_summary (e)->predicate != NULL;
308#endif
8cbc43ff 309
310 /* Don't inline inlined edges. */
311 gcc_assert (e->inline_failed);
312 /* Don't even think of inlining inline clone. */
82626cb0 313 gcc_assert (!callee->global.inlined_to);
8cbc43ff 314
a41f2a28 315 to = e->caller;
316 if (to->global.inlined_to)
317 to = to->global.inlined_to;
46729204 318 if (to->thunk.thunk_p)
319 {
839a6560 320 struct cgraph_node *target = to->callees->callee;
46729204 321 if (in_lto_p)
322 to->get_untransformed_body ();
323 to->expand_thunk (false, true);
839a6560 324 /* When thunk is instrumented we may have multiple callees. */
325 for (e = to->callees; e && e->callee != target; e = e->next_callee)
326 ;
327 gcc_assert (e);
46729204 328 }
329
330
331 e->inline_failed = CIF_OK;
332 DECL_POSSIBLY_INLINED (callee->decl) = true;
a41f2a28 333
e806c56f 334 if (DECL_FUNCTION_PERSONALITY (callee->decl))
335 DECL_FUNCTION_PERSONALITY (to->decl)
336 = DECL_FUNCTION_PERSONALITY (callee->decl);
67b5f619 337
338 bool reload_optimization_node = false;
b1d47f0c 339 if (!opt_for_fn (callee->decl, flag_strict_aliasing)
340 && opt_for_fn (to->decl, flag_strict_aliasing))
341 {
342 struct gcc_options opts = global_options;
343
42781674 344 cl_optimization_restore (&opts, opts_for_fn (to->decl));
b1d47f0c 345 opts.x_flag_strict_aliasing = false;
346 if (dump_file)
0e388735 347 fprintf (dump_file, "Dropping flag_strict_aliasing on %s\n",
348 to->dump_name ());
b1d47f0c 349 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (to->decl)
350 = build_optimization_node (&opts);
67b5f619 351 reload_optimization_node = true;
b1d47f0c 352 }
9ed0eb00 353
d2c2513e 354 ipa_fn_summary *caller_info = ipa_fn_summaries->get (to);
355 ipa_fn_summary *callee_info = ipa_fn_summaries->get (callee);
46e5dccc 356 if (!caller_info->fp_expressions && callee_info->fp_expressions)
357 {
358 caller_info->fp_expressions = true;
359 if (opt_for_fn (callee->decl, flag_rounding_math)
360 != opt_for_fn (to->decl, flag_rounding_math)
361 || opt_for_fn (callee->decl, flag_trapping_math)
362 != opt_for_fn (to->decl, flag_trapping_math)
363 || opt_for_fn (callee->decl, flag_unsafe_math_optimizations)
364 != opt_for_fn (to->decl, flag_unsafe_math_optimizations)
365 || opt_for_fn (callee->decl, flag_finite_math_only)
366 != opt_for_fn (to->decl, flag_finite_math_only)
367 || opt_for_fn (callee->decl, flag_signaling_nans)
368 != opt_for_fn (to->decl, flag_signaling_nans)
369 || opt_for_fn (callee->decl, flag_cx_limited_range)
370 != opt_for_fn (to->decl, flag_cx_limited_range)
371 || opt_for_fn (callee->decl, flag_signed_zeros)
372 != opt_for_fn (to->decl, flag_signed_zeros)
373 || opt_for_fn (callee->decl, flag_associative_math)
374 != opt_for_fn (to->decl, flag_associative_math)
375 || opt_for_fn (callee->decl, flag_reciprocal_math)
376 != opt_for_fn (to->decl, flag_reciprocal_math)
20702ea3 377 || opt_for_fn (callee->decl, flag_fp_int_builtin_inexact)
378 != opt_for_fn (to->decl, flag_fp_int_builtin_inexact)
46e5dccc 379 || opt_for_fn (callee->decl, flag_errno_math)
380 != opt_for_fn (to->decl, flag_errno_math))
381 {
382 struct gcc_options opts = global_options;
383
384 cl_optimization_restore (&opts, opts_for_fn (to->decl));
385 opts.x_flag_rounding_math
386 = opt_for_fn (callee->decl, flag_rounding_math);
387 opts.x_flag_trapping_math
388 = opt_for_fn (callee->decl, flag_trapping_math);
389 opts.x_flag_unsafe_math_optimizations
390 = opt_for_fn (callee->decl, flag_unsafe_math_optimizations);
391 opts.x_flag_finite_math_only
392 = opt_for_fn (callee->decl, flag_finite_math_only);
393 opts.x_flag_signaling_nans
394 = opt_for_fn (callee->decl, flag_signaling_nans);
395 opts.x_flag_cx_limited_range
396 = opt_for_fn (callee->decl, flag_cx_limited_range);
397 opts.x_flag_signed_zeros
398 = opt_for_fn (callee->decl, flag_signed_zeros);
399 opts.x_flag_associative_math
400 = opt_for_fn (callee->decl, flag_associative_math);
401 opts.x_flag_reciprocal_math
402 = opt_for_fn (callee->decl, flag_reciprocal_math);
20702ea3 403 opts.x_flag_fp_int_builtin_inexact
404 = opt_for_fn (callee->decl, flag_fp_int_builtin_inexact);
46e5dccc 405 opts.x_flag_errno_math
406 = opt_for_fn (callee->decl, flag_errno_math);
407 if (dump_file)
0e388735 408 fprintf (dump_file, "Copying FP flags from %s to %s\n",
409 callee->dump_name (), to->dump_name ());
46e5dccc 410 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (to->decl)
411 = build_optimization_node (&opts);
67b5f619 412 reload_optimization_node = true;
46e5dccc 413 }
414 }
e806c56f 415
67b5f619 416 /* Reload global optimization flags. */
417 if (reload_optimization_node && DECL_STRUCT_FUNCTION (to->decl) == cfun)
418 set_cfun (cfun, true);
419
82626cb0 420 /* If aliases are involved, redirect edge to the actual destination and
421 possibly remove the aliases. */
422 if (e->callee != callee)
c70f46b0 423 {
424 struct cgraph_node *alias = e->callee, *next_alias;
35ee1c66 425 e->redirect_callee (callee);
c70f46b0 426 while (alias && alias != callee)
427 {
428 if (!alias->callers
6365c927 429 && can_remove_node_now_p (alias,
430 !e->next_caller && !e->prev_caller ? e : NULL))
c70f46b0 431 {
415d1b9a 432 next_alias = alias->get_alias_target ();
433 alias->remove ();
7c5c01f1 434 if (callee_removed)
435 *callee_removed = true;
c70f46b0 436 alias = next_alias;
437 }
438 else
439 break;
440 }
441 }
82626cb0 442
151b9ff5 443 clone_inlined_nodes (e, true, update_original, overall_size);
8cbc43ff 444
8cbc43ff 445 gcc_assert (curr->callee->global.inlined_to == to);
0835ad03 446
d2c2513e 447 old_size = ipa_fn_summaries->get (to)->size;
1297cbcd 448 ipa_merge_fn_summary_after_inlining (e);
2bec7365 449 if (e->in_polymorphic_cdtor)
450 mark_all_inlined_calls_cdtor (e->callee);
a6d60179 451 if (opt_for_fn (e->caller->decl, optimize))
18b64b34 452 new_edges_found = ipa_propagate_indirect_call_infos (curr, new_edges);
7ab096e0 453 check_speculations (e->callee);
6331b6fa 454 if (update_overall_summary)
1297cbcd 455 ipa_update_overall_fn_summary (to);
6ec9c8c5 456 else
457 /* Update self size by the estimate so overall function growth limits
458 work for further inlining into this function. Before inlining
459 the function we inlined to again we expect the caller to update
460 the overall summary. */
d2c2513e 461 ipa_fn_summaries->get (to)->size += estimated_growth;
462 new_size = ipa_fn_summaries->get (to)->size;
12ecd4f9 463
468088ac 464 if (callee->calls_comdat_local)
465 to->calls_comdat_local = true;
415d1b9a 466 else if (to->calls_comdat_local && callee->comdat_local_p ())
468088ac 467 {
468 struct cgraph_edge *se = to->callees;
469 for (; se; se = se->next_callee)
415d1b9a 470 if (se->inline_failed && se->callee->comdat_local_p ())
468088ac 471 break;
472 if (se == NULL)
473 to->calls_comdat_local = false;
474 }
475
6737c56a 476 /* FIXME: This assert suffers from roundoff errors, disable it for GCC 5
477 and revisit it after conversion to sreals in GCC 6.
478 See PR 65654. */
479#if 0
18b64b34 480 /* Verify that estimated growth match real growth. Allow off-by-one
1297cbcd 481 error due to ipa_fn_summary::size_scale roudoff errors. */
95fb3203 482 gcc_assert (!update_overall_summary || !overall_size || new_edges_found
18b64b34 483 || abs (estimated_growth - (new_size - old_size)) <= 1
12d5ae9f 484 || speculation_removed
18b64b34 485 /* FIXME: a hack. Edges with false predicate are accounted
486 wrong, we should remove them from callgraph. */
487 || predicated);
488#endif
12ecd4f9 489
d94ff7ce 490 /* Account the change of overall unit size; external functions will be
491 removed and are thus not accounted. */
a6d60179 492 if (overall_size && inline_account_function_p (to))
8cbc43ff 493 *overall_size += new_size - old_size;
494 ncalls_inlined++;
495
1297cbcd 496 /* This must happen after ipa_merge_fn_summary_after_inlining that rely on jump
eb4ae064 497 functions of callee to not be updated. */
18b64b34 498 return new_edges_found;
8cbc43ff 499}
500
501
502/* Copy function body of NODE and redirect all inline clones to it.
503 This is done before inline plan is applied to NODE when there are
504 still some inline clones if it.
505
9d75589a 506 This is necessary because inline decisions are not really transitive
8cbc43ff 507 and the other inline clones may have different bodies. */
508
509static struct cgraph_node *
510save_inline_function_body (struct cgraph_node *node)
511{
512 struct cgraph_node *first_clone, *n;
513
514 if (dump_file)
515 fprintf (dump_file, "\nSaving body of %s for later reuse\n",
f1c8b4d7 516 node->name ());
8cbc43ff 517
415d1b9a 518 gcc_assert (node == cgraph_node::get (node->decl));
8cbc43ff 519
520 /* first_clone will be turned into real function. */
521 first_clone = node->clones;
6a353dda 522
523 /* Arrange first clone to not be thunk as those do not have bodies. */
524 if (first_clone->thunk.thunk_p)
525 {
526 while (first_clone->thunk.thunk_p)
527 first_clone = first_clone->next_sibling_clone;
528 first_clone->prev_sibling_clone->next_sibling_clone
529 = first_clone->next_sibling_clone;
530 if (first_clone->next_sibling_clone)
531 first_clone->next_sibling_clone->prev_sibling_clone
532 = first_clone->prev_sibling_clone;
533 first_clone->next_sibling_clone = node->clones;
534 first_clone->prev_sibling_clone = NULL;
535 node->clones->prev_sibling_clone = first_clone;
536 node->clones = first_clone;
537 }
02774f2d 538 first_clone->decl = copy_node (node->decl);
8c016392 539 first_clone->decl->decl_with_vis.symtab_node = first_clone;
415d1b9a 540 gcc_assert (first_clone == cgraph_node::get (first_clone->decl));
8cbc43ff 541
542 /* Now reshape the clone tree, so all other clones descends from
543 first_clone. */
544 if (first_clone->next_sibling_clone)
545 {
6a353dda 546 for (n = first_clone->next_sibling_clone; n->next_sibling_clone;
547 n = n->next_sibling_clone)
8cbc43ff 548 n->clone_of = first_clone;
549 n->clone_of = first_clone;
550 n->next_sibling_clone = first_clone->clones;
551 if (first_clone->clones)
552 first_clone->clones->prev_sibling_clone = n;
553 first_clone->clones = first_clone->next_sibling_clone;
554 first_clone->next_sibling_clone->prev_sibling_clone = NULL;
555 first_clone->next_sibling_clone = NULL;
556 gcc_assert (!first_clone->prev_sibling_clone);
557 }
558 first_clone->clone_of = NULL;
559
560 /* Now node in question has no clones. */
561 node->clones = NULL;
562
d826e131 563 /* Inline clones share decl with the function they are cloned
564 from. Walk the whole clone tree and redirect them all to the
565 new decl. */
8cbc43ff 566 if (first_clone->clones)
567 for (n = first_clone->clones; n != first_clone;)
568 {
02774f2d 569 gcc_assert (n->decl == node->decl);
570 n->decl = first_clone->decl;
8cbc43ff 571 if (n->clones)
572 n = n->clones;
573 else if (n->next_sibling_clone)
574 n = n->next_sibling_clone;
575 else
576 {
577 while (n != first_clone && !n->next_sibling_clone)
578 n = n->clone_of;
579 if (n != first_clone)
580 n = n->next_sibling_clone;
581 }
582 }
583
584 /* Copy the OLD_VERSION_NODE function tree to the new version. */
02774f2d 585 tree_function_versioning (node->decl, first_clone->decl,
f1f41a6c 586 NULL, true, NULL, false,
587 NULL, NULL);
8cbc43ff 588
d826e131 589 /* The function will be short lived and removed after we inline all the clones,
590 but make it internal so we won't confuse ourself. */
02774f2d 591 DECL_EXTERNAL (first_clone->decl) = 0;
02774f2d 592 TREE_PUBLIC (first_clone->decl) = 0;
593 DECL_COMDAT (first_clone->decl) = 0;
f1f41a6c 594 first_clone->ipa_transforms_to_apply.release ();
8cbc43ff 595
41710b76 596 /* When doing recursive inlining, the clone may become unnecessary.
597 This is possible i.e. in the case when the recursive function is proved to be
598 non-throwing and the recursion happens only in the EH landing pad.
f4d3c071 599 We cannot remove the clone until we are done with saving the body.
41710b76 600 Remove it now. */
601 if (!first_clone->callers)
602 {
415d1b9a 603 first_clone->remove_symbol_and_inline_clones ();
41710b76 604 first_clone = NULL;
605 }
382ecba7 606 else if (flag_checking)
415d1b9a 607 first_clone->verify ();
382ecba7 608
8cbc43ff 609 return first_clone;
610}
611
da5e1e7c 612/* Return true when function body of DECL still needs to be kept around
613 for later re-use. */
cf951b1a 614static bool
da5e1e7c 615preserve_function_body_p (struct cgraph_node *node)
616{
35ee1c66 617 gcc_assert (symtab->global_info_ready);
02774f2d 618 gcc_assert (!node->alias && !node->thunk.thunk_p);
da5e1e7c 619
6a353dda 620 /* Look if there is any non-thunk clone around. */
621 for (node = node->clones; node; node = node->next_sibling_clone)
622 if (!node->thunk.thunk_p)
623 return true;
da5e1e7c 624 return false;
625}
8cbc43ff 626
627/* Apply inline plan to function. */
628
629unsigned int
630inline_transform (struct cgraph_node *node)
631{
632 unsigned int todo = 0;
dc3d1030 633 struct cgraph_edge *e, *next;
d1f68cd8 634 bool has_inline = false;
a522e9eb 635
8cbc43ff 636 /* FIXME: Currently the pass manager is adding inline transform more than
637 once to some clones. This needs revisiting after WPA cleanups. */
638 if (cfun->after_inlining)
639 return 0;
640
641 /* We might need the body of this function so that we can expand
642 it inline somewhere else. */
da5e1e7c 643 if (preserve_function_body_p (node))
8cbc43ff 644 save_inline_function_body (node);
645
dc3d1030 646 for (e = node->callees; e; e = next)
647 {
d1f68cd8 648 if (!e->inline_failed)
649 has_inline = true;
dc3d1030 650 next = e->next_callee;
35ee1c66 651 e->redirect_call_stmt_to_callee ();
dc3d1030 652 }
51ce5652 653 node->remove_all_references ();
a522e9eb 654
655 timevar_push (TV_INTEGRATION);
a6d60179 656 if (node->callees && (opt_for_fn (node->decl, optimize) || has_inline))
6e4303bf 657 {
658 profile_count num = node->count;
659 profile_count den = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
ed0831a9 660 bool scale = num.initialized_p () && !(num == den);
6e4303bf 661 if (scale)
662 {
ed0831a9 663 profile_count::adjust_for_ipa_scaling (&num, &den);
6e4303bf 664 if (dump_file)
665 {
666 fprintf (dump_file, "Applying count scale ");
667 num.dump (dump_file);
668 fprintf (dump_file, "/");
669 den.dump (dump_file);
670 fprintf (dump_file, "\n");
671 }
672
673 basic_block bb;
ed0831a9 674 cfun->cfg->count_max = profile_count::uninitialized ();
6e4303bf 675 FOR_ALL_BB_FN (bb, cfun)
ed0831a9 676 {
84895d28 677 bb->count = bb->count.apply_scale (num, den);
ed0831a9 678 cfun->cfg->count_max = cfun->cfg->count_max.max (bb->count);
679 }
6e4303bf 680 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = node->count;
681 }
682 todo = optimize_inline_calls (current_function_decl);
683 }
a522e9eb 684 timevar_pop (TV_INTEGRATION);
685
e723655c 686 cfun->always_inline_functions_inlined = true;
687 cfun->after_inlining = true;
688 todo |= execute_fixup_cfg ();
689
4b0e0420 690 if (!(todo & TODO_update_ssa_any))
691 /* Redirecting edges might lead to a need for vops to be recomputed. */
692 todo |= TODO_update_ssa_only_virtuals;
693
e723655c 694 return todo;
8cbc43ff 695}