]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ipa-inline-transform.c
Makefile.in: Add ipa-fnsummary.o and ipa-fnsummary.h
[thirdparty/gcc.git] / gcc / ipa-inline-transform.c
CommitLineData
fee8b6da 1/* Callgraph transformations to handle inlining
cbe34bb5 2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
fee8b6da
JH
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"
957060b5 35#include "function.h"
fee8b6da 36#include "tree.h"
957060b5
AM
37#include "alloc-pool.h"
38#include "tree-pass.h"
39#include "cgraph.h"
442b4905 40#include "tree-cfg.h"
dd912cb8 41#include "symbol-summary.h"
8bc5448f 42#include "tree-vrp.h"
fee8b6da 43#include "ipa-prop.h"
27d020cf 44#include "ipa-fnsummary.h"
fee8b6da
JH
45#include "ipa-inline.h"
46#include "tree-inline.h"
47
48int ncalls_inlined;
49int nfunctions_inlined;
50
898b8927 51/* Scale frequency of NODE edges by FREQ_SCALE. */
fee8b6da
JH
52
53static void
54update_noncloned_frequencies (struct cgraph_node *node,
898b8927 55 int freq_scale)
fee8b6da
JH
56{
57 struct cgraph_edge *e;
58
59 /* We do not want to ignore high loop nest after freq drops to 0. */
60 if (!freq_scale)
61 freq_scale = 1;
62 for (e = node->callees; e; e = e->next_callee)
63 {
fee8b6da
JH
64 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
65 if (e->frequency > CGRAPH_FREQ_MAX)
66 e->frequency = CGRAPH_FREQ_MAX;
67 if (!e->inline_failed)
898b8927
JH
68 update_noncloned_frequencies (e->callee, freq_scale);
69 }
70 for (e = node->indirect_calls; e; e = e->next_callee)
71 {
72 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
73 if (e->frequency > CGRAPH_FREQ_MAX)
74 e->frequency = CGRAPH_FREQ_MAX;
fee8b6da
JH
75 }
76}
77
a5b1779f
JH
78/* We removed or are going to remove the last call to NODE.
79 Return true if we can and want proactively remove the NODE now.
80 This is important to do, since we want inliner to know when offline
81 copy of function was removed. */
82
83static bool
d142079a 84can_remove_node_now_p_1 (struct cgraph_node *node, struct cgraph_edge *e)
a5b1779f 85{
d142079a
JH
86 ipa_ref *ref;
87
88 FOR_EACH_ALIAS (node, ref)
89 {
90 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
91 if ((alias->callers && alias->callers != e)
92 || !can_remove_node_now_p_1 (alias, e))
93 return false;
94 }
a5b1779f
JH
95 /* FIXME: When address is taken of DECL_EXTERNAL function we still
96 can remove its offline copy, but we would need to keep unanalyzed node in
a6a543bf
JH
97 the callgraph so references can point to it.
98
99 Also for comdat group we can ignore references inside a group as we
100 want to prove the group as a whole to be dead. */
67348ccc 101 return (!node->address_taken
a6a543bf 102 && node->can_remove_if_no_direct_calls_and_refs_p ()
a5b1779f
JH
103 /* Inlining might enable more devirtualizing, so we want to remove
104 those only after all devirtualizable virtual calls are processed.
105 Lacking may edges in callgraph we just preserve them post
106 inlining. */
d142079a
JH
107 && (!DECL_VIRTUAL_P (node->decl)
108 || !opt_for_fn (node->decl, flag_devirtualize))
a5b1779f
JH
109 /* During early inlining some unanalyzed cgraph nodes might be in the
110 callgraph and they might reffer the function in question. */
31acf1bb 111 && !cgraph_new_nodes.exists ());
a5b1779f
JH
112}
113
6c69a029
JH
114/* We are going to eliminate last direct call to NODE (or alias of it) via edge E.
115 Verify that the NODE can be removed from unit and if it is contained in comdat
116 group that the whole comdat group is removable. */
117
118static bool
119can_remove_node_now_p (struct cgraph_node *node, struct cgraph_edge *e)
120{
121 struct cgraph_node *next;
d142079a 122 if (!can_remove_node_now_p_1 (node, e))
6c69a029
JH
123 return false;
124
125 /* When we see same comdat group, we need to be sure that all
126 items can be removed. */
8ccc8042 127 if (!node->same_comdat_group || !node->externally_visible)
6c69a029 128 return true;
d52f5295
ML
129 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
130 next != node; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
d142079a
JH
131 {
132 if (next->alias)
133 continue;
134 if ((next->callers && next->callers != e)
135 || !can_remove_node_now_p_1 (next, e))
136 return false;
137 }
6c69a029
JH
138 return true;
139}
140
d83fa499
EB
141/* Return true if NODE is a master clone with non-inline clones. */
142
143static bool
144master_clone_with_noninline_clones_p (struct cgraph_node *node)
145{
146 if (node->clone_of)
147 return false;
148
149 for (struct cgraph_node *n = node->clones; n; n = n->next_sibling_clone)
150 if (n->decl != node->decl)
151 return true;
152
153 return false;
154}
fee8b6da
JH
155
156/* E is expected to be an edge being inlined. Clone destination node of
157 the edge and redirect it to the new clone.
158 DUPLICATE is used for bookkeeping on whether we are actually creating new
159 clones or re-using node originally representing out-of-line function call.
bd936951
JH
160 By default the offline copy is removed, when it appears dead after inlining.
161 UPDATE_ORIGINAL prevents this transformation.
162 If OVERALL_SIZE is non-NULL, the size is updated to reflect the
163 transformation.
164 FREQ_SCALE specify the scaling of frequencies of call sites. */
fee8b6da
JH
165
166void
167clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
bd936951 168 bool update_original, int *overall_size, int freq_scale)
fee8b6da 169{
44a60244 170 struct cgraph_node *inlining_into;
09ce3660 171 struct cgraph_edge *next;
44a60244
MJ
172
173 if (e->caller->global.inlined_to)
174 inlining_into = e->caller->global.inlined_to;
175 else
176 inlining_into = e->caller;
177
fee8b6da
JH
178 if (duplicate)
179 {
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 fuction 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
d83fa499
EB
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))
fee8b6da 192 {
6c69a029
JH
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.
197
198 For now we keep the ohter functions in the group in program until
199 cgraph_remove_unreachable_functions gets rid of them. */
fee8b6da 200 gcc_assert (!e->callee->global.inlined_to);
b91b562c 201 e->callee->remove_from_same_comdat_group ();
bb1e543c
JH
202 if (e->callee->definition
203 && inline_account_function_p (e->callee))
fee8b6da 204 {
bb1e543c 205 gcc_assert (!e->callee->alias);
fee8b6da 206 if (overall_size)
9a1e784a 207 *overall_size -= inline_summaries->get (e->callee)->size;
fee8b6da
JH
208 nfunctions_inlined++;
209 }
210 duplicate = false;
67348ccc 211 e->callee->externally_visible = false;
898b8927 212 update_noncloned_frequencies (e->callee, e->frequency);
0bdad123
ML
213
214 dump_callgraph_transformation (e->callee, inlining_into,
215 "inlining to");
fee8b6da
JH
216 }
217 else
218 {
219 struct cgraph_node *n;
bd936951
JH
220
221 if (freq_scale == -1)
222 freq_scale = e->frequency;
d52f5295
ML
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);
4ad08ee8 229 n->used_as_abstract_origin = e->callee->used_as_abstract_origin;
3dafb85c 230 e->redirect_callee (n);
fee8b6da
JH
231 }
232 }
65d630d4 233 else
b91b562c 234 e->callee->remove_from_same_comdat_group ();
fee8b6da 235
44a60244 236 e->callee->global.inlined_to = inlining_into;
fee8b6da
JH
237
238 /* Recursively clone all bodies. */
09ce3660
JH
239 for (e = e->callee->callees; e; e = next)
240 {
241 next = e->next_callee;
242 if (!e->inline_failed)
bd936951 243 clone_inlined_nodes (e, duplicate, update_original, overall_size, freq_scale);
db66bf68
JH
244 }
245}
246
247/* Check all speculations in N and resolve them if they seems useless. */
248
249static bool
250check_speculations (cgraph_node *n)
251{
252 bool speculation_removed = false;
253 cgraph_edge *next;
254
255 for (cgraph_edge *e = n->callees; e; e = next)
256 {
257 next = e->next_callee;
09ce3660
JH
258 if (e->speculative && !speculation_useful_p (e, true))
259 {
3dafb85c 260 e->resolve_speculation (NULL);
09ce3660
JH
261 speculation_removed = true;
262 }
db66bf68
JH
263 else if (!e->inline_failed)
264 speculation_removed |= check_speculations (e->callee);
09ce3660 265 }
db66bf68 266 return speculation_removed;
fee8b6da
JH
267}
268
4fd94d1e
MJ
269/* Mark all call graph edges coming out of NODE and all nodes that have been
270 inlined to it as in_polymorphic_cdtor. */
271
272static void
273mark_all_inlined_calls_cdtor (cgraph_node *node)
274{
275 for (cgraph_edge *cs = node->callees; cs; cs = cs->next_callee)
276 {
277 cs->in_polymorphic_cdtor = true;
278 if (!cs->inline_failed)
279 mark_all_inlined_calls_cdtor (cs->callee);
280 }
281 for (cgraph_edge *cs = node->indirect_calls; cs; cs = cs->next_callee)
282 cs->in_polymorphic_cdtor = true;
283}
284
fee8b6da
JH
285
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
c170d40f
JH
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.
1bbb87c4 292 If callee_removed is non-NULL, set it to true if we removed callee node.
c170d40f
JH
293
294 Return true iff any new callgraph edges were discovered as a
fee8b6da
JH
295 result of inlining. */
296
297bool
298inline_call (struct cgraph_edge *e, bool update_original,
d52f5295 299 vec<cgraph_edge *> *new_edges,
1bbb87c4
JH
300 int *overall_size, bool update_overall_summary,
301 bool *callee_removed)
fee8b6da
JH
302{
303 int old_size = 0, new_size = 0;
304 struct cgraph_node *to = NULL;
305 struct cgraph_edge *curr = e;
d52f5295 306 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
0f378cb5
JH
307 bool new_edges_found = false;
308
bddead15
RB
309 int estimated_growth = 0;
310 if (! update_overall_summary)
311 estimated_growth = estimate_edge_growth (e);
f107227b
JH
312 /* This is used only for assert bellow. */
313#if 0
0f378cb5
JH
314 bool predicated = inline_edge_summary (e)->predicate != NULL;
315#endif
fee8b6da
JH
316
317 /* Don't inline inlined edges. */
318 gcc_assert (e->inline_failed);
319 /* Don't even think of inlining inline clone. */
a5b1779f 320 gcc_assert (!callee->global.inlined_to);
fee8b6da 321
632b4f8e
JH
322 to = e->caller;
323 if (to->global.inlined_to)
324 to = to->global.inlined_to;
0b9004ed
JH
325 if (to->thunk.thunk_p)
326 {
c7ed8938 327 struct cgraph_node *target = to->callees->callee;
0b9004ed
JH
328 if (in_lto_p)
329 to->get_untransformed_body ();
330 to->expand_thunk (false, true);
c7ed8938
IE
331 /* When thunk is instrumented we may have multiple callees. */
332 for (e = to->callees; e && e->callee != target; e = e->next_callee)
333 ;
334 gcc_assert (e);
0b9004ed
JH
335 }
336
337
338 e->inline_failed = CIF_OK;
339 DECL_POSSIBLY_INLINED (callee->decl) = true;
632b4f8e 340
5058c037
JH
341 if (DECL_FUNCTION_PERSONALITY (callee->decl))
342 DECL_FUNCTION_PERSONALITY (to->decl)
343 = DECL_FUNCTION_PERSONALITY (callee->decl);
77719b06
ML
344
345 bool reload_optimization_node = false;
45285060
JH
346 if (!opt_for_fn (callee->decl, flag_strict_aliasing)
347 && opt_for_fn (to->decl, flag_strict_aliasing))
348 {
349 struct gcc_options opts = global_options;
350
f7f32acd 351 cl_optimization_restore (&opts, opts_for_fn (to->decl));
45285060
JH
352 opts.x_flag_strict_aliasing = false;
353 if (dump_file)
354 fprintf (dump_file, "Dropping flag_strict_aliasing on %s:%i\n",
355 to->name (), to->order);
45285060
JH
356 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (to->decl)
357 = build_optimization_node (&opts);
77719b06 358 reload_optimization_node = true;
45285060 359 }
5c846a81 360
818b88a7
JH
361 inline_summary *caller_info = inline_summaries->get (to);
362 inline_summary *callee_info = inline_summaries->get (callee);
363 if (!caller_info->fp_expressions && callee_info->fp_expressions)
364 {
365 caller_info->fp_expressions = true;
366 if (opt_for_fn (callee->decl, flag_rounding_math)
367 != opt_for_fn (to->decl, flag_rounding_math)
368 || opt_for_fn (callee->decl, flag_trapping_math)
369 != opt_for_fn (to->decl, flag_trapping_math)
370 || opt_for_fn (callee->decl, flag_unsafe_math_optimizations)
371 != opt_for_fn (to->decl, flag_unsafe_math_optimizations)
372 || opt_for_fn (callee->decl, flag_finite_math_only)
373 != opt_for_fn (to->decl, flag_finite_math_only)
374 || opt_for_fn (callee->decl, flag_signaling_nans)
375 != opt_for_fn (to->decl, flag_signaling_nans)
376 || opt_for_fn (callee->decl, flag_cx_limited_range)
377 != opt_for_fn (to->decl, flag_cx_limited_range)
378 || opt_for_fn (callee->decl, flag_signed_zeros)
379 != opt_for_fn (to->decl, flag_signed_zeros)
380 || opt_for_fn (callee->decl, flag_associative_math)
381 != opt_for_fn (to->decl, flag_associative_math)
382 || opt_for_fn (callee->decl, flag_reciprocal_math)
383 != opt_for_fn (to->decl, flag_reciprocal_math)
0d2f700f
JM
384 || opt_for_fn (callee->decl, flag_fp_int_builtin_inexact)
385 != opt_for_fn (to->decl, flag_fp_int_builtin_inexact)
818b88a7
JH
386 || opt_for_fn (callee->decl, flag_errno_math)
387 != opt_for_fn (to->decl, flag_errno_math))
388 {
389 struct gcc_options opts = global_options;
390
391 cl_optimization_restore (&opts, opts_for_fn (to->decl));
392 opts.x_flag_rounding_math
393 = opt_for_fn (callee->decl, flag_rounding_math);
394 opts.x_flag_trapping_math
395 = opt_for_fn (callee->decl, flag_trapping_math);
396 opts.x_flag_unsafe_math_optimizations
397 = opt_for_fn (callee->decl, flag_unsafe_math_optimizations);
398 opts.x_flag_finite_math_only
399 = opt_for_fn (callee->decl, flag_finite_math_only);
400 opts.x_flag_signaling_nans
401 = opt_for_fn (callee->decl, flag_signaling_nans);
402 opts.x_flag_cx_limited_range
403 = opt_for_fn (callee->decl, flag_cx_limited_range);
404 opts.x_flag_signed_zeros
405 = opt_for_fn (callee->decl, flag_signed_zeros);
406 opts.x_flag_associative_math
407 = opt_for_fn (callee->decl, flag_associative_math);
408 opts.x_flag_reciprocal_math
409 = opt_for_fn (callee->decl, flag_reciprocal_math);
0d2f700f
JM
410 opts.x_flag_fp_int_builtin_inexact
411 = opt_for_fn (callee->decl, flag_fp_int_builtin_inexact);
818b88a7
JH
412 opts.x_flag_errno_math
413 = opt_for_fn (callee->decl, flag_errno_math);
414 if (dump_file)
415 fprintf (dump_file, "Copying FP flags from %s:%i to %s:%i\n",
416 callee->name (), callee->order, to->name (), to->order);
818b88a7
JH
417 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (to->decl)
418 = build_optimization_node (&opts);
77719b06 419 reload_optimization_node = true;
818b88a7
JH
420 }
421 }
5058c037 422
77719b06
ML
423 /* Reload global optimization flags. */
424 if (reload_optimization_node && DECL_STRUCT_FUNCTION (to->decl) == cfun)
425 set_cfun (cfun, true);
426
a5b1779f
JH
427 /* If aliases are involved, redirect edge to the actual destination and
428 possibly remove the aliases. */
429 if (e->callee != callee)
39e2db00
JH
430 {
431 struct cgraph_node *alias = e->callee, *next_alias;
3dafb85c 432 e->redirect_callee (callee);
39e2db00
JH
433 while (alias && alias != callee)
434 {
435 if (!alias->callers
8ccc8042
JH
436 && can_remove_node_now_p (alias,
437 !e->next_caller && !e->prev_caller ? e : NULL))
39e2db00 438 {
d52f5295
ML
439 next_alias = alias->get_alias_target ();
440 alias->remove ();
1bbb87c4
JH
441 if (callee_removed)
442 *callee_removed = true;
39e2db00
JH
443 alias = next_alias;
444 }
445 else
446 break;
447 }
448 }
a5b1779f 449
bd936951 450 clone_inlined_nodes (e, true, update_original, overall_size, e->frequency);
fee8b6da 451
fee8b6da 452 gcc_assert (curr->callee->global.inlined_to == to);
898b8927 453
9a1e784a 454 old_size = inline_summaries->get (to)->size;
898b8927 455 inline_merge_summary (e);
4fd94d1e
MJ
456 if (e->in_polymorphic_cdtor)
457 mark_all_inlined_calls_cdtor (e->callee);
bb1e543c 458 if (opt_for_fn (e->caller->decl, optimize))
0f378cb5 459 new_edges_found = ipa_propagate_indirect_call_infos (curr, new_edges);
db66bf68 460 check_speculations (e->callee);
c170d40f 461 if (update_overall_summary)
bddead15
RB
462 inline_update_overall_summary (to);
463 else
464 /* Update self size by the estimate so overall function growth limits
465 work for further inlining into this function. Before inlining
466 the function we inlined to again we expect the caller to update
467 the overall summary. */
468 inline_summaries->get (to)->size += estimated_growth;
9a1e784a 469 new_size = inline_summaries->get (to)->size;
d250540a 470
1f26ac87
JM
471 if (callee->calls_comdat_local)
472 to->calls_comdat_local = true;
d52f5295 473 else if (to->calls_comdat_local && callee->comdat_local_p ())
1f26ac87
JM
474 {
475 struct cgraph_edge *se = to->callees;
476 for (; se; se = se->next_callee)
d52f5295 477 if (se->inline_failed && se->callee->comdat_local_p ())
1f26ac87
JM
478 break;
479 if (se == NULL)
480 to->calls_comdat_local = false;
481 }
482
f107227b
JH
483 /* FIXME: This assert suffers from roundoff errors, disable it for GCC 5
484 and revisit it after conversion to sreals in GCC 6.
485 See PR 65654. */
486#if 0
0f378cb5
JH
487 /* Verify that estimated growth match real growth. Allow off-by-one
488 error due to INLINE_SIZE_SCALE roudoff errors. */
48b1474e 489 gcc_assert (!update_overall_summary || !overall_size || new_edges_found
0f378cb5 490 || abs (estimated_growth - (new_size - old_size)) <= 1
09ce3660 491 || speculation_removed
0f378cb5
JH
492 /* FIXME: a hack. Edges with false predicate are accounted
493 wrong, we should remove them from callgraph. */
494 || predicated);
495#endif
d250540a 496
8256d5ca
JH
497 /* Account the change of overall unit size; external functions will be
498 removed and are thus not accounted. */
bb1e543c 499 if (overall_size && inline_account_function_p (to))
fee8b6da
JH
500 *overall_size += new_size - old_size;
501 ncalls_inlined++;
502
25837a2f
JH
503 /* This must happen after inline_merge_summary that rely on jump
504 functions of callee to not be updated. */
0f378cb5 505 return new_edges_found;
fee8b6da
JH
506}
507
508
509/* Copy function body of NODE and redirect all inline clones to it.
510 This is done before inline plan is applied to NODE when there are
511 still some inline clones if it.
512
073a8998 513 This is necessary because inline decisions are not really transitive
fee8b6da
JH
514 and the other inline clones may have different bodies. */
515
516static struct cgraph_node *
517save_inline_function_body (struct cgraph_node *node)
518{
519 struct cgraph_node *first_clone, *n;
520
521 if (dump_file)
522 fprintf (dump_file, "\nSaving body of %s for later reuse\n",
fec39fa6 523 node->name ());
fee8b6da 524
d52f5295 525 gcc_assert (node == cgraph_node::get (node->decl));
fee8b6da
JH
526
527 /* first_clone will be turned into real function. */
528 first_clone = node->clones;
ec6a1e35
JH
529
530 /* Arrange first clone to not be thunk as those do not have bodies. */
531 if (first_clone->thunk.thunk_p)
532 {
533 while (first_clone->thunk.thunk_p)
534 first_clone = first_clone->next_sibling_clone;
535 first_clone->prev_sibling_clone->next_sibling_clone
536 = first_clone->next_sibling_clone;
537 if (first_clone->next_sibling_clone)
538 first_clone->next_sibling_clone->prev_sibling_clone
539 = first_clone->prev_sibling_clone;
540 first_clone->next_sibling_clone = node->clones;
541 first_clone->prev_sibling_clone = NULL;
542 node->clones->prev_sibling_clone = first_clone;
543 node->clones = first_clone;
544 }
67348ccc 545 first_clone->decl = copy_node (node->decl);
aede2c10 546 first_clone->decl->decl_with_vis.symtab_node = first_clone;
d52f5295 547 gcc_assert (first_clone == cgraph_node::get (first_clone->decl));
fee8b6da
JH
548
549 /* Now reshape the clone tree, so all other clones descends from
550 first_clone. */
551 if (first_clone->next_sibling_clone)
552 {
ec6a1e35
JH
553 for (n = first_clone->next_sibling_clone; n->next_sibling_clone;
554 n = n->next_sibling_clone)
fee8b6da
JH
555 n->clone_of = first_clone;
556 n->clone_of = first_clone;
557 n->next_sibling_clone = first_clone->clones;
558 if (first_clone->clones)
559 first_clone->clones->prev_sibling_clone = n;
560 first_clone->clones = first_clone->next_sibling_clone;
561 first_clone->next_sibling_clone->prev_sibling_clone = NULL;
562 first_clone->next_sibling_clone = NULL;
563 gcc_assert (!first_clone->prev_sibling_clone);
564 }
565 first_clone->clone_of = NULL;
566
567 /* Now node in question has no clones. */
568 node->clones = NULL;
569
1a3118e9
JH
570 /* Inline clones share decl with the function they are cloned
571 from. Walk the whole clone tree and redirect them all to the
572 new decl. */
fee8b6da
JH
573 if (first_clone->clones)
574 for (n = first_clone->clones; n != first_clone;)
575 {
67348ccc
DM
576 gcc_assert (n->decl == node->decl);
577 n->decl = first_clone->decl;
fee8b6da
JH
578 if (n->clones)
579 n = n->clones;
580 else if (n->next_sibling_clone)
581 n = n->next_sibling_clone;
582 else
583 {
584 while (n != first_clone && !n->next_sibling_clone)
585 n = n->clone_of;
586 if (n != first_clone)
587 n = n->next_sibling_clone;
588 }
589 }
590
591 /* Copy the OLD_VERSION_NODE function tree to the new version. */
67348ccc 592 tree_function_versioning (node->decl, first_clone->decl,
9771b263
DN
593 NULL, true, NULL, false,
594 NULL, NULL);
fee8b6da 595
1a3118e9
JH
596 /* The function will be short lived and removed after we inline all the clones,
597 but make it internal so we won't confuse ourself. */
67348ccc 598 DECL_EXTERNAL (first_clone->decl) = 0;
67348ccc
DM
599 TREE_PUBLIC (first_clone->decl) = 0;
600 DECL_COMDAT (first_clone->decl) = 0;
9771b263 601 first_clone->ipa_transforms_to_apply.release ();
fee8b6da 602
b4e93f45
JH
603 /* When doing recursive inlining, the clone may become unnecessary.
604 This is possible i.e. in the case when the recursive function is proved to be
605 non-throwing and the recursion happens only in the EH landing pad.
606 We can not remove the clone until we are done with saving the body.
607 Remove it now. */
608 if (!first_clone->callers)
609 {
d52f5295 610 first_clone->remove_symbol_and_inline_clones ();
b4e93f45
JH
611 first_clone = NULL;
612 }
b2b29377 613 else if (flag_checking)
d52f5295 614 first_clone->verify ();
b2b29377 615
fee8b6da
JH
616 return first_clone;
617}
618
9c8305f8
JH
619/* Return true when function body of DECL still needs to be kept around
620 for later re-use. */
65d630d4 621static bool
9c8305f8
JH
622preserve_function_body_p (struct cgraph_node *node)
623{
3dafb85c 624 gcc_assert (symtab->global_info_ready);
67348ccc 625 gcc_assert (!node->alias && !node->thunk.thunk_p);
9c8305f8 626
ec6a1e35
JH
627 /* Look if there is any non-thunk clone around. */
628 for (node = node->clones; node; node = node->next_sibling_clone)
629 if (!node->thunk.thunk_p)
630 return true;
9c8305f8
JH
631 return false;
632}
fee8b6da
JH
633
634/* Apply inline plan to function. */
635
636unsigned int
637inline_transform (struct cgraph_node *node)
638{
639 unsigned int todo = 0;
e8aec975 640 struct cgraph_edge *e, *next;
2bf86c84 641 bool has_inline = false;
c9fc06dc 642
fee8b6da
JH
643 /* FIXME: Currently the pass manager is adding inline transform more than
644 once to some clones. This needs revisiting after WPA cleanups. */
645 if (cfun->after_inlining)
646 return 0;
647
648 /* We might need the body of this function so that we can expand
649 it inline somewhere else. */
9c8305f8 650 if (preserve_function_body_p (node))
fee8b6da
JH
651 save_inline_function_body (node);
652
e8aec975
JH
653 for (e = node->callees; e; e = next)
654 {
2bf86c84
JH
655 if (!e->inline_failed)
656 has_inline = true;
e8aec975 657 next = e->next_callee;
3dafb85c 658 e->redirect_call_stmt_to_callee ();
e8aec975 659 }
d122681a 660 node->remove_all_references ();
c9fc06dc
CB
661
662 timevar_push (TV_INTEGRATION);
bb1e543c 663 if (node->callees && (opt_for_fn (node->decl, optimize) || has_inline))
55f01229 664 todo = optimize_inline_calls (current_function_decl);
c9fc06dc
CB
665 timevar_pop (TV_INTEGRATION);
666
f8698b37
RG
667 cfun->always_inline_functions_inlined = true;
668 cfun->after_inlining = true;
669 todo |= execute_fixup_cfg ();
670
55f01229
RG
671 if (!(todo & TODO_update_ssa_any))
672 /* Redirecting edges might lead to a need for vops to be recomputed. */
673 todo |= TODO_update_ssa_only_virtuals;
674
f8698b37 675 return todo;
fee8b6da 676}