re PR fortran/77506 (F2008 Standard does not allow CHARACTER(LEN=*) in array constructor)
[official-gcc.git] / gcc / ipa-inline-transform.c
blob98c7f96fbb69e4f8d401062493284f20ad1dc43e
1 /* Callgraph transformations to handle inlining
2 Copyright (C) 2003-2016 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 "ipa-prop.h"
43 #include "ipa-inline.h"
44 #include "tree-inline.h"
46 int ncalls_inlined;
47 int nfunctions_inlined;
49 /* Scale frequency of NODE edges by FREQ_SCALE. */
51 static void
52 update_noncloned_frequencies (struct cgraph_node *node,
53 int freq_scale)
55 struct cgraph_edge *e;
57 /* We do not want to ignore high loop nest after freq drops to 0. */
58 if (!freq_scale)
59 freq_scale = 1;
60 for (e = node->callees; e; e = e->next_callee)
62 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
63 if (e->frequency > CGRAPH_FREQ_MAX)
64 e->frequency = CGRAPH_FREQ_MAX;
65 if (!e->inline_failed)
66 update_noncloned_frequencies (e->callee, freq_scale);
68 for (e = node->indirect_calls; e; e = e->next_callee)
70 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
71 if (e->frequency > CGRAPH_FREQ_MAX)
72 e->frequency = CGRAPH_FREQ_MAX;
76 /* We removed or are going to remove the last call to NODE.
77 Return true if we can and want proactively remove the NODE now.
78 This is important to do, since we want inliner to know when offline
79 copy of function was removed. */
81 static bool
82 can_remove_node_now_p_1 (struct cgraph_node *node, struct cgraph_edge *e)
84 ipa_ref *ref;
86 FOR_EACH_ALIAS (node, ref)
88 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
89 if ((alias->callers && alias->callers != e)
90 || !can_remove_node_now_p_1 (alias, e))
91 return false;
93 /* FIXME: When address is taken of DECL_EXTERNAL function we still
94 can remove its offline copy, but we would need to keep unanalyzed node in
95 the callgraph so references can point to it.
97 Also for comdat group we can ignore references inside a group as we
98 want to prove the group as a whole to be dead. */
99 return (!node->address_taken
100 && node->can_remove_if_no_direct_calls_and_refs_p ()
101 /* Inlining might enable more devirtualizing, so we want to remove
102 those only after all devirtualizable virtual calls are processed.
103 Lacking may edges in callgraph we just preserve them post
104 inlining. */
105 && (!DECL_VIRTUAL_P (node->decl)
106 || !opt_for_fn (node->decl, flag_devirtualize))
107 /* During early inlining some unanalyzed cgraph nodes might be in the
108 callgraph and they might reffer the function in question. */
109 && !cgraph_new_nodes.exists ());
112 /* We are going to eliminate last direct call to NODE (or alias of it) via edge E.
113 Verify that the NODE can be removed from unit and if it is contained in comdat
114 group that the whole comdat group is removable. */
116 static bool
117 can_remove_node_now_p (struct cgraph_node *node, struct cgraph_edge *e)
119 struct cgraph_node *next;
120 if (!can_remove_node_now_p_1 (node, e))
121 return false;
123 /* When we see same comdat group, we need to be sure that all
124 items can be removed. */
125 if (!node->same_comdat_group || !node->externally_visible)
126 return true;
127 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
128 next != node; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
130 if (next->alias)
131 continue;
132 if ((next->callers && next->callers != e)
133 || !can_remove_node_now_p_1 (next, e))
134 return false;
136 return true;
139 /* Return true if NODE is a master clone with non-inline clones. */
141 static bool
142 master_clone_with_noninline_clones_p (struct cgraph_node *node)
144 if (node->clone_of)
145 return false;
147 for (struct cgraph_node *n = node->clones; n; n = n->next_sibling_clone)
148 if (n->decl != node->decl)
149 return true;
151 return false;
154 /* E is expected to be an edge being inlined. Clone destination node of
155 the edge and redirect it to the new clone.
156 DUPLICATE is used for bookkeeping on whether we are actually creating new
157 clones or re-using node originally representing out-of-line function call.
158 By default the offline copy is removed, when it appears dead after inlining.
159 UPDATE_ORIGINAL prevents this transformation.
160 If OVERALL_SIZE is non-NULL, the size is updated to reflect the
161 transformation.
162 FREQ_SCALE specify the scaling of frequencies of call sites. */
164 void
165 clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
166 bool update_original, int *overall_size, int freq_scale)
168 struct cgraph_node *inlining_into;
169 struct cgraph_edge *next;
171 if (e->caller->global.inlined_to)
172 inlining_into = e->caller->global.inlined_to;
173 else
174 inlining_into = e->caller;
176 if (duplicate)
178 /* We may eliminate the need for out-of-line copy to be output.
179 In that case just go ahead and re-use it. This is not just an
180 memory optimization. Making offline copy of fuction disappear
181 from the program will improve future decisions on inlining. */
182 if (!e->callee->callers->next_caller
183 /* Recursive inlining never wants the master clone to
184 be overwritten. */
185 && update_original
186 && can_remove_node_now_p (e->callee, e)
187 /* We cannot overwrite a master clone with non-inline clones
188 until after these clones are materialized. */
189 && !master_clone_with_noninline_clones_p (e->callee))
191 /* TODO: When callee is in a comdat group, we could remove all of it,
192 including all inline clones inlined into it. That would however
193 need small function inlining to register edge removal hook to
194 maintain the priority queue.
196 For now we keep the ohter functions in the group in program until
197 cgraph_remove_unreachable_functions gets rid of them. */
198 gcc_assert (!e->callee->global.inlined_to);
199 e->callee->remove_from_same_comdat_group ();
200 if (e->callee->definition
201 && inline_account_function_p (e->callee))
203 gcc_assert (!e->callee->alias);
204 if (overall_size)
205 *overall_size -= inline_summaries->get (e->callee)->size;
206 nfunctions_inlined++;
208 duplicate = false;
209 e->callee->externally_visible = false;
210 update_noncloned_frequencies (e->callee, e->frequency);
212 else
214 struct cgraph_node *n;
216 if (freq_scale == -1)
217 freq_scale = e->frequency;
218 n = e->callee->create_clone (e->callee->decl,
219 MIN (e->count, e->callee->count),
220 freq_scale,
221 update_original, vNULL, true,
222 inlining_into,
223 NULL);
224 n->used_as_abstract_origin = e->callee->used_as_abstract_origin;
225 e->redirect_callee (n);
228 else
229 e->callee->remove_from_same_comdat_group ();
231 e->callee->global.inlined_to = inlining_into;
233 /* Recursively clone all bodies. */
234 for (e = e->callee->callees; e; e = next)
236 next = e->next_callee;
237 if (!e->inline_failed)
238 clone_inlined_nodes (e, duplicate, update_original, overall_size, freq_scale);
242 /* Check all speculations in N and resolve them if they seems useless. */
244 static bool
245 check_speculations (cgraph_node *n)
247 bool speculation_removed = false;
248 cgraph_edge *next;
250 for (cgraph_edge *e = n->callees; e; e = next)
252 next = e->next_callee;
253 if (e->speculative && !speculation_useful_p (e, true))
255 e->resolve_speculation (NULL);
256 speculation_removed = true;
258 else if (!e->inline_failed)
259 speculation_removed |= check_speculations (e->callee);
261 return speculation_removed;
264 /* Mark all call graph edges coming out of NODE and all nodes that have been
265 inlined to it as in_polymorphic_cdtor. */
267 static void
268 mark_all_inlined_calls_cdtor (cgraph_node *node)
270 for (cgraph_edge *cs = node->callees; cs; cs = cs->next_callee)
272 cs->in_polymorphic_cdtor = true;
273 if (!cs->inline_failed)
274 mark_all_inlined_calls_cdtor (cs->callee);
276 for (cgraph_edge *cs = node->indirect_calls; cs; cs = cs->next_callee)
277 cs->in_polymorphic_cdtor = true;
281 /* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL
282 specify whether profile of original function should be updated. If any new
283 indirect edges are discovered in the process, add them to NEW_EDGES, unless
284 it is NULL. If UPDATE_OVERALL_SUMMARY is false, do not bother to recompute overall
285 size of caller after inlining. Caller is required to eventually do it via
286 inline_update_overall_summary.
287 If callee_removed is non-NULL, set it to true if we removed callee node.
289 Return true iff any new callgraph edges were discovered as a
290 result of inlining. */
292 bool
293 inline_call (struct cgraph_edge *e, bool update_original,
294 vec<cgraph_edge *> *new_edges,
295 int *overall_size, bool update_overall_summary,
296 bool *callee_removed)
298 int old_size = 0, new_size = 0;
299 struct cgraph_node *to = NULL;
300 struct cgraph_edge *curr = e;
301 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
302 bool new_edges_found = false;
304 int estimated_growth = 0;
305 if (! update_overall_summary)
306 estimated_growth = estimate_edge_growth (e);
307 /* This is used only for assert bellow. */
308 #if 0
309 bool predicated = inline_edge_summary (e)->predicate != NULL;
310 #endif
312 /* Don't inline inlined edges. */
313 gcc_assert (e->inline_failed);
314 /* Don't even think of inlining inline clone. */
315 gcc_assert (!callee->global.inlined_to);
317 to = e->caller;
318 if (to->global.inlined_to)
319 to = to->global.inlined_to;
320 if (to->thunk.thunk_p)
322 struct cgraph_node *target = to->callees->callee;
323 if (in_lto_p)
324 to->get_untransformed_body ();
325 to->expand_thunk (false, true);
326 /* When thunk is instrumented we may have multiple callees. */
327 for (e = to->callees; e && e->callee != target; e = e->next_callee)
329 gcc_assert (e);
333 e->inline_failed = CIF_OK;
334 DECL_POSSIBLY_INLINED (callee->decl) = true;
336 if (DECL_FUNCTION_PERSONALITY (callee->decl))
337 DECL_FUNCTION_PERSONALITY (to->decl)
338 = DECL_FUNCTION_PERSONALITY (callee->decl);
339 if (!opt_for_fn (callee->decl, flag_strict_aliasing)
340 && opt_for_fn (to->decl, flag_strict_aliasing))
342 struct gcc_options opts = global_options;
344 cl_optimization_restore (&opts, opts_for_fn (to->decl));
345 opts.x_flag_strict_aliasing = false;
346 if (dump_file)
347 fprintf (dump_file, "Dropping flag_strict_aliasing on %s:%i\n",
348 to->name (), to->order);
349 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (to->decl)
350 = build_optimization_node (&opts);
353 inline_summary *caller_info = inline_summaries->get (to);
354 inline_summary *callee_info = inline_summaries->get (callee);
355 if (!caller_info->fp_expressions && callee_info->fp_expressions)
357 caller_info->fp_expressions = true;
358 if (opt_for_fn (callee->decl, flag_rounding_math)
359 != opt_for_fn (to->decl, flag_rounding_math)
360 || opt_for_fn (callee->decl, flag_trapping_math)
361 != opt_for_fn (to->decl, flag_trapping_math)
362 || opt_for_fn (callee->decl, flag_unsafe_math_optimizations)
363 != opt_for_fn (to->decl, flag_unsafe_math_optimizations)
364 || opt_for_fn (callee->decl, flag_finite_math_only)
365 != opt_for_fn (to->decl, flag_finite_math_only)
366 || opt_for_fn (callee->decl, flag_signaling_nans)
367 != opt_for_fn (to->decl, flag_signaling_nans)
368 || opt_for_fn (callee->decl, flag_cx_limited_range)
369 != opt_for_fn (to->decl, flag_cx_limited_range)
370 || opt_for_fn (callee->decl, flag_signed_zeros)
371 != opt_for_fn (to->decl, flag_signed_zeros)
372 || opt_for_fn (callee->decl, flag_associative_math)
373 != opt_for_fn (to->decl, flag_associative_math)
374 || opt_for_fn (callee->decl, flag_reciprocal_math)
375 != opt_for_fn (to->decl, flag_reciprocal_math)
376 || opt_for_fn (callee->decl, flag_fp_int_builtin_inexact)
377 != opt_for_fn (to->decl, flag_fp_int_builtin_inexact)
378 || opt_for_fn (callee->decl, flag_errno_math)
379 != opt_for_fn (to->decl, flag_errno_math))
381 struct gcc_options opts = global_options;
383 cl_optimization_restore (&opts, opts_for_fn (to->decl));
384 opts.x_flag_rounding_math
385 = opt_for_fn (callee->decl, flag_rounding_math);
386 opts.x_flag_trapping_math
387 = opt_for_fn (callee->decl, flag_trapping_math);
388 opts.x_flag_unsafe_math_optimizations
389 = opt_for_fn (callee->decl, flag_unsafe_math_optimizations);
390 opts.x_flag_finite_math_only
391 = opt_for_fn (callee->decl, flag_finite_math_only);
392 opts.x_flag_signaling_nans
393 = opt_for_fn (callee->decl, flag_signaling_nans);
394 opts.x_flag_cx_limited_range
395 = opt_for_fn (callee->decl, flag_cx_limited_range);
396 opts.x_flag_signed_zeros
397 = opt_for_fn (callee->decl, flag_signed_zeros);
398 opts.x_flag_associative_math
399 = opt_for_fn (callee->decl, flag_associative_math);
400 opts.x_flag_reciprocal_math
401 = opt_for_fn (callee->decl, flag_reciprocal_math);
402 opts.x_flag_fp_int_builtin_inexact
403 = opt_for_fn (callee->decl, flag_fp_int_builtin_inexact);
404 opts.x_flag_errno_math
405 = opt_for_fn (callee->decl, flag_errno_math);
406 if (dump_file)
407 fprintf (dump_file, "Copying FP flags from %s:%i to %s:%i\n",
408 callee->name (), callee->order, to->name (), to->order);
409 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (to->decl)
410 = build_optimization_node (&opts);
414 /* If aliases are involved, redirect edge to the actual destination and
415 possibly remove the aliases. */
416 if (e->callee != callee)
418 struct cgraph_node *alias = e->callee, *next_alias;
419 e->redirect_callee (callee);
420 while (alias && alias != callee)
422 if (!alias->callers
423 && can_remove_node_now_p (alias,
424 !e->next_caller && !e->prev_caller ? e : NULL))
426 next_alias = alias->get_alias_target ();
427 alias->remove ();
428 if (callee_removed)
429 *callee_removed = true;
430 alias = next_alias;
432 else
433 break;
437 clone_inlined_nodes (e, true, update_original, overall_size, e->frequency);
439 gcc_assert (curr->callee->global.inlined_to == to);
441 old_size = inline_summaries->get (to)->size;
442 inline_merge_summary (e);
443 if (e->in_polymorphic_cdtor)
444 mark_all_inlined_calls_cdtor (e->callee);
445 if (opt_for_fn (e->caller->decl, optimize))
446 new_edges_found = ipa_propagate_indirect_call_infos (curr, new_edges);
447 check_speculations (e->callee);
448 if (update_overall_summary)
449 inline_update_overall_summary (to);
450 else
451 /* Update self size by the estimate so overall function growth limits
452 work for further inlining into this function. Before inlining
453 the function we inlined to again we expect the caller to update
454 the overall summary. */
455 inline_summaries->get (to)->size += estimated_growth;
456 new_size = inline_summaries->get (to)->size;
458 if (callee->calls_comdat_local)
459 to->calls_comdat_local = true;
460 else if (to->calls_comdat_local && callee->comdat_local_p ())
462 struct cgraph_edge *se = to->callees;
463 for (; se; se = se->next_callee)
464 if (se->inline_failed && se->callee->comdat_local_p ())
465 break;
466 if (se == NULL)
467 to->calls_comdat_local = false;
470 /* FIXME: This assert suffers from roundoff errors, disable it for GCC 5
471 and revisit it after conversion to sreals in GCC 6.
472 See PR 65654. */
473 #if 0
474 /* Verify that estimated growth match real growth. Allow off-by-one
475 error due to INLINE_SIZE_SCALE roudoff errors. */
476 gcc_assert (!update_overall_summary || !overall_size || new_edges_found
477 || abs (estimated_growth - (new_size - old_size)) <= 1
478 || speculation_removed
479 /* FIXME: a hack. Edges with false predicate are accounted
480 wrong, we should remove them from callgraph. */
481 || predicated);
482 #endif
484 /* Account the change of overall unit size; external functions will be
485 removed and are thus not accounted. */
486 if (overall_size && inline_account_function_p (to))
487 *overall_size += new_size - old_size;
488 ncalls_inlined++;
490 /* This must happen after inline_merge_summary that rely on jump
491 functions of callee to not be updated. */
492 return new_edges_found;
496 /* Copy function body of NODE and redirect all inline clones to it.
497 This is done before inline plan is applied to NODE when there are
498 still some inline clones if it.
500 This is necessary because inline decisions are not really transitive
501 and the other inline clones may have different bodies. */
503 static struct cgraph_node *
504 save_inline_function_body (struct cgraph_node *node)
506 struct cgraph_node *first_clone, *n;
508 if (dump_file)
509 fprintf (dump_file, "\nSaving body of %s for later reuse\n",
510 node->name ());
512 gcc_assert (node == cgraph_node::get (node->decl));
514 /* first_clone will be turned into real function. */
515 first_clone = node->clones;
517 /* Arrange first clone to not be thunk as those do not have bodies. */
518 if (first_clone->thunk.thunk_p)
520 while (first_clone->thunk.thunk_p)
521 first_clone = first_clone->next_sibling_clone;
522 first_clone->prev_sibling_clone->next_sibling_clone
523 = first_clone->next_sibling_clone;
524 if (first_clone->next_sibling_clone)
525 first_clone->next_sibling_clone->prev_sibling_clone
526 = first_clone->prev_sibling_clone;
527 first_clone->next_sibling_clone = node->clones;
528 first_clone->prev_sibling_clone = NULL;
529 node->clones->prev_sibling_clone = first_clone;
530 node->clones = first_clone;
532 first_clone->decl = copy_node (node->decl);
533 first_clone->decl->decl_with_vis.symtab_node = first_clone;
534 gcc_assert (first_clone == cgraph_node::get (first_clone->decl));
536 /* Now reshape the clone tree, so all other clones descends from
537 first_clone. */
538 if (first_clone->next_sibling_clone)
540 for (n = first_clone->next_sibling_clone; n->next_sibling_clone;
541 n = n->next_sibling_clone)
542 n->clone_of = first_clone;
543 n->clone_of = first_clone;
544 n->next_sibling_clone = first_clone->clones;
545 if (first_clone->clones)
546 first_clone->clones->prev_sibling_clone = n;
547 first_clone->clones = first_clone->next_sibling_clone;
548 first_clone->next_sibling_clone->prev_sibling_clone = NULL;
549 first_clone->next_sibling_clone = NULL;
550 gcc_assert (!first_clone->prev_sibling_clone);
552 first_clone->clone_of = NULL;
554 /* Now node in question has no clones. */
555 node->clones = NULL;
557 /* Inline clones share decl with the function they are cloned
558 from. Walk the whole clone tree and redirect them all to the
559 new decl. */
560 if (first_clone->clones)
561 for (n = first_clone->clones; n != first_clone;)
563 gcc_assert (n->decl == node->decl);
564 n->decl = first_clone->decl;
565 if (n->clones)
566 n = n->clones;
567 else if (n->next_sibling_clone)
568 n = n->next_sibling_clone;
569 else
571 while (n != first_clone && !n->next_sibling_clone)
572 n = n->clone_of;
573 if (n != first_clone)
574 n = n->next_sibling_clone;
578 /* Copy the OLD_VERSION_NODE function tree to the new version. */
579 tree_function_versioning (node->decl, first_clone->decl,
580 NULL, true, NULL, false,
581 NULL, NULL);
583 /* The function will be short lived and removed after we inline all the clones,
584 but make it internal so we won't confuse ourself. */
585 DECL_EXTERNAL (first_clone->decl) = 0;
586 TREE_PUBLIC (first_clone->decl) = 0;
587 DECL_COMDAT (first_clone->decl) = 0;
588 first_clone->ipa_transforms_to_apply.release ();
590 /* When doing recursive inlining, the clone may become unnecessary.
591 This is possible i.e. in the case when the recursive function is proved to be
592 non-throwing and the recursion happens only in the EH landing pad.
593 We can not remove the clone until we are done with saving the body.
594 Remove it now. */
595 if (!first_clone->callers)
597 first_clone->remove_symbol_and_inline_clones ();
598 first_clone = NULL;
600 else if (flag_checking)
601 first_clone->verify ();
603 return first_clone;
606 /* Return true when function body of DECL still needs to be kept around
607 for later re-use. */
608 static bool
609 preserve_function_body_p (struct cgraph_node *node)
611 gcc_assert (symtab->global_info_ready);
612 gcc_assert (!node->alias && !node->thunk.thunk_p);
614 /* Look if there is any non-thunk clone around. */
615 for (node = node->clones; node; node = node->next_sibling_clone)
616 if (!node->thunk.thunk_p)
617 return true;
618 return false;
621 /* Apply inline plan to function. */
623 unsigned int
624 inline_transform (struct cgraph_node *node)
626 unsigned int todo = 0;
627 struct cgraph_edge *e, *next;
628 bool has_inline = false;
630 /* FIXME: Currently the pass manager is adding inline transform more than
631 once to some clones. This needs revisiting after WPA cleanups. */
632 if (cfun->after_inlining)
633 return 0;
635 /* We might need the body of this function so that we can expand
636 it inline somewhere else. */
637 if (preserve_function_body_p (node))
638 save_inline_function_body (node);
640 for (e = node->callees; e; e = next)
642 if (!e->inline_failed)
643 has_inline = true;
644 next = e->next_callee;
645 e->redirect_call_stmt_to_callee ();
647 node->remove_all_references ();
649 timevar_push (TV_INTEGRATION);
650 if (node->callees && (opt_for_fn (node->decl, optimize) || has_inline))
651 todo = optimize_inline_calls (current_function_decl);
652 timevar_pop (TV_INTEGRATION);
654 cfun->always_inline_functions_inlined = true;
655 cfun->after_inlining = true;
656 todo |= execute_fixup_cfg ();
658 if (!(todo & TODO_update_ssa_any))
659 /* Redirecting edges might lead to a need for vops to be recomputed. */
660 todo |= TODO_update_ssa_only_virtuals;
662 return todo;