Small ChangeLog tweak.
[official-gcc.git] / gcc / ipa-inline.c
blob0ebe1477f6cbd28a6bd7c59a30769af47817113d
1 /* Inlining decision heuristics.
2 Copyright (C) 2003-2017 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 /* Inlining decision heuristics
23 The implementation of inliner is organized as follows:
25 inlining heuristics limits
27 can_inline_edge_p allow to check that particular inlining is allowed
28 by the limits specified by user (allowed function growth, growth and so
29 on).
31 Functions are inlined when it is obvious the result is profitable (such
32 as functions called once or when inlining reduce code size).
33 In addition to that we perform inlining of small functions and recursive
34 inlining.
36 inlining heuristics
38 The inliner itself is split into two passes:
40 pass_early_inlining
42 Simple local inlining pass inlining callees into current function.
43 This pass makes no use of whole unit analysis and thus it can do only
44 very simple decisions based on local properties.
46 The strength of the pass is that it is run in topological order
47 (reverse postorder) on the callgraph. Functions are converted into SSA
48 form just before this pass and optimized subsequently. As a result, the
49 callees of the function seen by the early inliner was already optimized
50 and results of early inlining adds a lot of optimization opportunities
51 for the local optimization.
53 The pass handle the obvious inlining decisions within the compilation
54 unit - inlining auto inline functions, inlining for size and
55 flattening.
57 main strength of the pass is the ability to eliminate abstraction
58 penalty in C++ code (via combination of inlining and early
59 optimization) and thus improve quality of analysis done by real IPA
60 optimizers.
62 Because of lack of whole unit knowledge, the pass can not really make
63 good code size/performance tradeoffs. It however does very simple
64 speculative inlining allowing code size to grow by
65 EARLY_INLINING_INSNS when callee is leaf function. In this case the
66 optimizations performed later are very likely to eliminate the cost.
68 pass_ipa_inline
70 This is the real inliner able to handle inlining with whole program
71 knowledge. It performs following steps:
73 1) inlining of small functions. This is implemented by greedy
74 algorithm ordering all inlinable cgraph edges by their badness and
75 inlining them in this order as long as inline limits allows doing so.
77 This heuristics is not very good on inlining recursive calls. Recursive
78 calls can be inlined with results similar to loop unrolling. To do so,
79 special purpose recursive inliner is executed on function when
80 recursive edge is met as viable candidate.
82 2) Unreachable functions are removed from callgraph. Inlining leads
83 to devirtualization and other modification of callgraph so functions
84 may become unreachable during the process. Also functions declared as
85 extern inline or virtual functions are removed, since after inlining
86 we no longer need the offline bodies.
88 3) Functions called once and not exported from the unit are inlined.
89 This should almost always lead to reduction of code size by eliminating
90 the need for offline copy of the function. */
92 #include "config.h"
93 #include "system.h"
94 #include "coretypes.h"
95 #include "backend.h"
96 #include "target.h"
97 #include "rtl.h"
98 #include "tree.h"
99 #include "gimple.h"
100 #include "alloc-pool.h"
101 #include "tree-pass.h"
102 #include "gimple-ssa.h"
103 #include "cgraph.h"
104 #include "lto-streamer.h"
105 #include "trans-mem.h"
106 #include "calls.h"
107 #include "tree-inline.h"
108 #include "params.h"
109 #include "profile.h"
110 #include "symbol-summary.h"
111 #include "tree-vrp.h"
112 #include "ipa-prop.h"
113 #include "ipa-fnsummary.h"
114 #include "ipa-inline.h"
115 #include "ipa-utils.h"
116 #include "sreal.h"
117 #include "auto-profile.h"
118 #include "builtins.h"
119 #include "fibonacci_heap.h"
121 typedef fibonacci_heap <sreal, cgraph_edge> edge_heap_t;
122 typedef fibonacci_node <sreal, cgraph_edge> edge_heap_node_t;
124 /* Statistics we collect about inlining algorithm. */
125 static int overall_size;
126 static gcov_type max_count;
127 static gcov_type spec_rem;
129 /* Pre-computed constants 1/CGRAPH_FREQ_BASE and 1/100. */
130 static sreal cgraph_freq_base_rec, percent_rec;
132 /* Return false when inlining edge E would lead to violating
133 limits on function unit growth or stack usage growth.
135 The relative function body growth limit is present generally
136 to avoid problems with non-linear behavior of the compiler.
137 To allow inlining huge functions into tiny wrapper, the limit
138 is always based on the bigger of the two functions considered.
140 For stack growth limits we always base the growth in stack usage
141 of the callers. We want to prevent applications from segfaulting
142 on stack overflow when functions with huge stack frames gets
143 inlined. */
145 static bool
146 caller_growth_limits (struct cgraph_edge *e)
148 struct cgraph_node *to = e->caller;
149 struct cgraph_node *what = e->callee->ultimate_alias_target ();
150 int newsize;
151 int limit = 0;
152 HOST_WIDE_INT stack_size_limit = 0, inlined_stack;
153 ipa_fn_summary *info, *what_info, *outer_info = ipa_fn_summaries->get (to);
155 /* Look for function e->caller is inlined to. While doing
156 so work out the largest function body on the way. As
157 described above, we want to base our function growth
158 limits based on that. Not on the self size of the
159 outer function, not on the self size of inline code
160 we immediately inline to. This is the most relaxed
161 interpretation of the rule "do not grow large functions
162 too much in order to prevent compiler from exploding". */
163 while (true)
165 info = ipa_fn_summaries->get (to);
166 if (limit < info->self_size)
167 limit = info->self_size;
168 if (stack_size_limit < info->estimated_self_stack_size)
169 stack_size_limit = info->estimated_self_stack_size;
170 if (to->global.inlined_to)
171 to = to->callers->caller;
172 else
173 break;
176 what_info = ipa_fn_summaries->get (what);
178 if (limit < what_info->self_size)
179 limit = what_info->self_size;
181 limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
183 /* Check the size after inlining against the function limits. But allow
184 the function to shrink if it went over the limits by forced inlining. */
185 newsize = estimate_size_after_inlining (to, e);
186 if (newsize >= info->size
187 && newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
188 && newsize > limit)
190 e->inline_failed = CIF_LARGE_FUNCTION_GROWTH_LIMIT;
191 return false;
194 if (!what_info->estimated_stack_size)
195 return true;
197 /* FIXME: Stack size limit often prevents inlining in Fortran programs
198 due to large i/o datastructures used by the Fortran front-end.
199 We ought to ignore this limit when we know that the edge is executed
200 on every invocation of the caller (i.e. its call statement dominates
201 exit block). We do not track this information, yet. */
202 stack_size_limit += ((gcov_type)stack_size_limit
203 * PARAM_VALUE (PARAM_STACK_FRAME_GROWTH) / 100);
205 inlined_stack = (outer_info->stack_frame_offset
206 + outer_info->estimated_self_stack_size
207 + what_info->estimated_stack_size);
208 /* Check new stack consumption with stack consumption at the place
209 stack is used. */
210 if (inlined_stack > stack_size_limit
211 /* If function already has large stack usage from sibling
212 inline call, we can inline, too.
213 This bit overoptimistically assume that we are good at stack
214 packing. */
215 && inlined_stack > info->estimated_stack_size
216 && inlined_stack > PARAM_VALUE (PARAM_LARGE_STACK_FRAME))
218 e->inline_failed = CIF_LARGE_STACK_FRAME_GROWTH_LIMIT;
219 return false;
221 return true;
224 /* Dump info about why inlining has failed. */
226 static void
227 report_inline_failed_reason (struct cgraph_edge *e)
229 if (dump_file)
231 fprintf (dump_file, " not inlinable: %s -> %s, %s\n",
232 e->caller->dump_name (),
233 e->callee->dump_name (),
234 cgraph_inline_failed_string (e->inline_failed));
235 if ((e->inline_failed == CIF_TARGET_OPTION_MISMATCH
236 || e->inline_failed == CIF_OPTIMIZATION_MISMATCH)
237 && e->caller->lto_file_data
238 && e->callee->ultimate_alias_target ()->lto_file_data)
240 fprintf (dump_file, " LTO objects: %s, %s\n",
241 e->caller->lto_file_data->file_name,
242 e->callee->ultimate_alias_target ()->lto_file_data->file_name);
244 if (e->inline_failed == CIF_TARGET_OPTION_MISMATCH)
245 cl_target_option_print_diff
246 (dump_file, 2, target_opts_for_fn (e->caller->decl),
247 target_opts_for_fn (e->callee->ultimate_alias_target ()->decl));
248 if (e->inline_failed == CIF_OPTIMIZATION_MISMATCH)
249 cl_optimization_print_diff
250 (dump_file, 2, opts_for_fn (e->caller->decl),
251 opts_for_fn (e->callee->ultimate_alias_target ()->decl));
255 /* Decide whether sanitizer-related attributes allow inlining. */
257 static bool
258 sanitize_attrs_match_for_inline_p (const_tree caller, const_tree callee)
260 /* Don't care if sanitizer is disabled */
261 if (!(flag_sanitize & SANITIZE_ADDRESS))
262 return true;
264 if (!caller || !callee)
265 return true;
267 return !!lookup_attribute ("no_sanitize_address",
268 DECL_ATTRIBUTES (caller)) ==
269 !!lookup_attribute ("no_sanitize_address",
270 DECL_ATTRIBUTES (callee));
273 /* Used for flags where it is safe to inline when caller's value is
274 grater than callee's. */
275 #define check_maybe_up(flag) \
276 (opts_for_fn (caller->decl)->x_##flag \
277 != opts_for_fn (callee->decl)->x_##flag \
278 && (!always_inline \
279 || opts_for_fn (caller->decl)->x_##flag \
280 < opts_for_fn (callee->decl)->x_##flag))
281 /* Used for flags where it is safe to inline when caller's value is
282 smaller than callee's. */
283 #define check_maybe_down(flag) \
284 (opts_for_fn (caller->decl)->x_##flag \
285 != opts_for_fn (callee->decl)->x_##flag \
286 && (!always_inline \
287 || opts_for_fn (caller->decl)->x_##flag \
288 > opts_for_fn (callee->decl)->x_##flag))
289 /* Used for flags where exact match is needed for correctness. */
290 #define check_match(flag) \
291 (opts_for_fn (caller->decl)->x_##flag \
292 != opts_for_fn (callee->decl)->x_##flag)
294 /* Decide if we can inline the edge and possibly update
295 inline_failed reason.
296 We check whether inlining is possible at all and whether
297 caller growth limits allow doing so.
299 if REPORT is true, output reason to the dump file.
301 if DISREGARD_LIMITS is true, ignore size limits.*/
303 static bool
304 can_inline_edge_p (struct cgraph_edge *e, bool report,
305 bool disregard_limits = false, bool early = false)
307 gcc_checking_assert (e->inline_failed);
309 if (cgraph_inline_failed_type (e->inline_failed) == CIF_FINAL_ERROR)
311 if (report)
312 report_inline_failed_reason (e);
313 return false;
316 bool inlinable = true;
317 enum availability avail;
318 cgraph_node *caller = e->caller->global.inlined_to
319 ? e->caller->global.inlined_to : e->caller;
320 cgraph_node *callee = e->callee->ultimate_alias_target (&avail, caller);
321 tree caller_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (caller->decl);
322 tree callee_tree
323 = callee ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (callee->decl) : NULL;
325 if (!callee->definition)
327 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
328 inlinable = false;
330 else if (callee->calls_comdat_local)
332 e->inline_failed = CIF_USES_COMDAT_LOCAL;
333 inlinable = false;
335 else if (avail <= AVAIL_INTERPOSABLE)
337 e->inline_failed = CIF_OVERWRITABLE;
338 inlinable = false;
340 /* All edges with call_stmt_cannot_inline_p should have inline_failed
341 initialized to one of FINAL_ERROR reasons. */
342 else if (e->call_stmt_cannot_inline_p)
343 gcc_unreachable ();
344 /* Don't inline if the functions have different EH personalities. */
345 else if (DECL_FUNCTION_PERSONALITY (caller->decl)
346 && DECL_FUNCTION_PERSONALITY (callee->decl)
347 && (DECL_FUNCTION_PERSONALITY (caller->decl)
348 != DECL_FUNCTION_PERSONALITY (callee->decl)))
350 e->inline_failed = CIF_EH_PERSONALITY;
351 inlinable = false;
353 /* TM pure functions should not be inlined into non-TM_pure
354 functions. */
355 else if (is_tm_pure (callee->decl) && !is_tm_pure (caller->decl))
357 e->inline_failed = CIF_UNSPECIFIED;
358 inlinable = false;
360 /* Check compatibility of target optimization options. */
361 else if (!targetm.target_option.can_inline_p (caller->decl,
362 callee->decl))
364 e->inline_failed = CIF_TARGET_OPTION_MISMATCH;
365 inlinable = false;
367 else if (!ipa_fn_summaries->get (callee)->inlinable)
369 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
370 inlinable = false;
372 /* Don't inline a function with mismatched sanitization attributes. */
373 else if (!sanitize_attrs_match_for_inline_p (caller->decl, callee->decl))
375 e->inline_failed = CIF_ATTRIBUTE_MISMATCH;
376 inlinable = false;
378 /* Check if caller growth allows the inlining. */
379 else if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl)
380 && !disregard_limits
381 && !lookup_attribute ("flatten",
382 DECL_ATTRIBUTES (caller->decl))
383 && !caller_growth_limits (e))
384 inlinable = false;
385 /* Don't inline a function with a higher optimization level than the
386 caller. FIXME: this is really just tip of iceberg of handling
387 optimization attribute. */
388 else if (caller_tree != callee_tree)
390 bool always_inline =
391 (DECL_DISREGARD_INLINE_LIMITS (callee->decl)
392 && lookup_attribute ("always_inline",
393 DECL_ATTRIBUTES (callee->decl)));
394 ipa_fn_summary *caller_info = ipa_fn_summaries->get (caller);
395 ipa_fn_summary *callee_info = ipa_fn_summaries->get (callee);
397 /* Until GCC 4.9 we did not check the semantics alterning flags
398 bellow and inline across optimization boundry.
399 Enabling checks bellow breaks several packages by refusing
400 to inline library always_inline functions. See PR65873.
401 Disable the check for early inlining for now until better solution
402 is found. */
403 if (always_inline && early)
405 /* There are some options that change IL semantics which means
406 we cannot inline in these cases for correctness reason.
407 Not even for always_inline declared functions. */
408 else if (check_match (flag_wrapv)
409 || check_match (flag_trapv)
410 /* When caller or callee does FP math, be sure FP codegen flags
411 compatible. */
412 || ((caller_info->fp_expressions && callee_info->fp_expressions)
413 && (check_maybe_up (flag_rounding_math)
414 || check_maybe_up (flag_trapping_math)
415 || check_maybe_down (flag_unsafe_math_optimizations)
416 || check_maybe_down (flag_finite_math_only)
417 || check_maybe_up (flag_signaling_nans)
418 || check_maybe_down (flag_cx_limited_range)
419 || check_maybe_up (flag_signed_zeros)
420 || check_maybe_down (flag_associative_math)
421 || check_maybe_down (flag_reciprocal_math)
422 || check_maybe_down (flag_fp_int_builtin_inexact)
423 /* Strictly speaking only when the callee contains function
424 calls that may end up setting errno. */
425 || check_maybe_up (flag_errno_math)))
426 /* We do not want to make code compiled with exceptions to be
427 brought into a non-EH function unless we know that the callee
428 does not throw.
429 This is tracked by DECL_FUNCTION_PERSONALITY. */
430 || (check_maybe_up (flag_non_call_exceptions)
431 && DECL_FUNCTION_PERSONALITY (callee->decl))
432 || (check_maybe_up (flag_exceptions)
433 && DECL_FUNCTION_PERSONALITY (callee->decl))
434 /* When devirtualization is diabled for callee, it is not safe
435 to inline it as we possibly mangled the type info.
436 Allow early inlining of always inlines. */
437 || (!early && check_maybe_down (flag_devirtualize)))
439 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
440 inlinable = false;
442 /* gcc.dg/pr43564.c. Apply user-forced inline even at -O0. */
443 else if (always_inline)
445 /* When user added an attribute to the callee honor it. */
446 else if (lookup_attribute ("optimize", DECL_ATTRIBUTES (callee->decl))
447 && opts_for_fn (caller->decl) != opts_for_fn (callee->decl))
449 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
450 inlinable = false;
452 /* If explicit optimize attribute are not used, the mismatch is caused
453 by different command line options used to build different units.
454 Do not care about COMDAT functions - those are intended to be
455 optimized with the optimization flags of module they are used in.
456 Also do not care about mixing up size/speed optimization when
457 DECL_DISREGARD_INLINE_LIMITS is set. */
458 else if ((callee->merged_comdat
459 && !lookup_attribute ("optimize",
460 DECL_ATTRIBUTES (caller->decl)))
461 || DECL_DISREGARD_INLINE_LIMITS (callee->decl))
463 /* If mismatch is caused by merging two LTO units with different
464 optimizationflags we want to be bit nicer. However never inline
465 if one of functions is not optimized at all. */
466 else if (!opt_for_fn (callee->decl, optimize)
467 || !opt_for_fn (caller->decl, optimize))
469 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
470 inlinable = false;
472 /* If callee is optimized for size and caller is not, allow inlining if
473 code shrinks or we are in MAX_INLINE_INSNS_SINGLE limit and callee
474 is inline (and thus likely an unified comdat). This will allow caller
475 to run faster. */
476 else if (opt_for_fn (callee->decl, optimize_size)
477 > opt_for_fn (caller->decl, optimize_size))
479 int growth = estimate_edge_growth (e);
480 if (growth > 0
481 && (!DECL_DECLARED_INLINE_P (callee->decl)
482 && growth >= MAX (MAX_INLINE_INSNS_SINGLE,
483 MAX_INLINE_INSNS_AUTO)))
485 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
486 inlinable = false;
489 /* If callee is more aggressively optimized for performance than caller,
490 we generally want to inline only cheap (runtime wise) functions. */
491 else if (opt_for_fn (callee->decl, optimize_size)
492 < opt_for_fn (caller->decl, optimize_size)
493 || (opt_for_fn (callee->decl, optimize)
494 > opt_for_fn (caller->decl, optimize)))
496 if (estimate_edge_time (e)
497 >= 20 + ipa_call_summaries->get (e)->call_stmt_time)
499 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
500 inlinable = false;
506 if (!inlinable && report)
507 report_inline_failed_reason (e);
508 return inlinable;
512 /* Return true if the edge E is inlinable during early inlining. */
514 static bool
515 can_early_inline_edge_p (struct cgraph_edge *e)
517 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
518 /* Early inliner might get called at WPA stage when IPA pass adds new
519 function. In this case we can not really do any of early inlining
520 because function bodies are missing. */
521 if (cgraph_inline_failed_type (e->inline_failed) == CIF_FINAL_ERROR)
522 return false;
523 if (!gimple_has_body_p (callee->decl))
525 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
526 return false;
528 /* In early inliner some of callees may not be in SSA form yet
529 (i.e. the callgraph is cyclic and we did not process
530 the callee by early inliner, yet). We don't have CIF code for this
531 case; later we will re-do the decision in the real inliner. */
532 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->caller->decl))
533 || !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
535 if (dump_file)
536 fprintf (dump_file, " edge not inlinable: not in SSA form\n");
537 return false;
539 if (!can_inline_edge_p (e, true, false, true))
540 return false;
541 return true;
545 /* Return number of calls in N. Ignore cheap builtins. */
547 static int
548 num_calls (struct cgraph_node *n)
550 struct cgraph_edge *e;
551 int num = 0;
553 for (e = n->callees; e; e = e->next_callee)
554 if (!is_inexpensive_builtin (e->callee->decl))
555 num++;
556 return num;
560 /* Return true if we are interested in inlining small function. */
562 static bool
563 want_early_inline_function_p (struct cgraph_edge *e)
565 bool want_inline = true;
566 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
568 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
570 /* For AutoFDO, we need to make sure that before profile summary, all
571 hot paths' IR look exactly the same as profiled binary. As a result,
572 in einliner, we will disregard size limit and inline those callsites
573 that are:
574 * inlined in the profiled binary, and
575 * the cloned callee has enough samples to be considered "hot". */
576 else if (flag_auto_profile && afdo_callsite_hot_enough_for_early_inline (e))
578 else if (!DECL_DECLARED_INLINE_P (callee->decl)
579 && !opt_for_fn (e->caller->decl, flag_inline_small_functions))
581 e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
582 report_inline_failed_reason (e);
583 want_inline = false;
585 else
587 int growth = estimate_edge_growth (e);
588 int n;
590 if (growth <= 0)
592 else if (!e->maybe_hot_p ()
593 && growth > 0)
595 if (dump_file)
596 fprintf (dump_file, " will not early inline: %s->%s, "
597 "call is cold and code would grow by %i\n",
598 e->caller->dump_name (),
599 callee->dump_name (),
600 growth);
601 want_inline = false;
603 else if (growth > PARAM_VALUE (PARAM_EARLY_INLINING_INSNS))
605 if (dump_file)
606 fprintf (dump_file, " will not early inline: %s->%s, "
607 "growth %i exceeds --param early-inlining-insns\n",
608 e->caller->dump_name (),
609 callee->dump_name (),
610 growth);
611 want_inline = false;
613 else if ((n = num_calls (callee)) != 0
614 && growth * (n + 1) > PARAM_VALUE (PARAM_EARLY_INLINING_INSNS))
616 if (dump_file)
617 fprintf (dump_file, " will not early inline: %s->%s, "
618 "growth %i exceeds --param early-inlining-insns "
619 "divided by number of calls\n",
620 e->caller->dump_name (),
621 callee->dump_name (),
622 growth);
623 want_inline = false;
626 return want_inline;
629 /* Compute time of the edge->caller + edge->callee execution when inlining
630 does not happen. */
632 inline sreal
633 compute_uninlined_call_time (struct cgraph_edge *edge,
634 sreal uninlined_call_time)
636 cgraph_node *caller = (edge->caller->global.inlined_to
637 ? edge->caller->global.inlined_to
638 : edge->caller);
640 if (edge->count && caller->count)
641 uninlined_call_time *= (sreal)edge->count / caller->count;
642 if (edge->frequency)
643 uninlined_call_time *= cgraph_freq_base_rec * edge->frequency;
644 else
645 uninlined_call_time = uninlined_call_time >> 11;
647 sreal caller_time = ipa_fn_summaries->get (caller)->time;
648 return uninlined_call_time + caller_time;
651 /* Same as compute_uinlined_call_time but compute time when inlining
652 does happen. */
654 inline sreal
655 compute_inlined_call_time (struct cgraph_edge *edge,
656 sreal time)
658 cgraph_node *caller = (edge->caller->global.inlined_to
659 ? edge->caller->global.inlined_to
660 : edge->caller);
661 sreal caller_time = ipa_fn_summaries->get (caller)->time;
663 if (edge->count && caller->count)
664 time *= (sreal)edge->count / caller->count;
665 if (edge->frequency)
666 time *= cgraph_freq_base_rec * edge->frequency;
667 else
668 time = time >> 11;
670 /* This calculation should match one in ipa-inline-analysis.c
671 (estimate_edge_size_and_time). */
672 time -= (sreal) edge->frequency
673 * ipa_call_summaries->get (edge)->call_stmt_time / CGRAPH_FREQ_BASE;
674 time += caller_time;
675 if (time <= 0)
676 time = ((sreal) 1) >> 8;
677 gcc_checking_assert (time >= 0);
678 return time;
681 /* Return true if the speedup for inlining E is bigger than
682 PARAM_MAX_INLINE_MIN_SPEEDUP. */
684 static bool
685 big_speedup_p (struct cgraph_edge *e)
687 sreal unspec_time;
688 sreal spec_time = estimate_edge_time (e, &unspec_time);
689 sreal time = compute_uninlined_call_time (e, unspec_time);
690 sreal inlined_time = compute_inlined_call_time (e, spec_time);
692 if (time - inlined_time
693 > (sreal) (time * PARAM_VALUE (PARAM_INLINE_MIN_SPEEDUP))
694 * percent_rec)
695 return true;
696 return false;
699 /* Return true if we are interested in inlining small function.
700 When REPORT is true, report reason to dump file. */
702 static bool
703 want_inline_small_function_p (struct cgraph_edge *e, bool report)
705 bool want_inline = true;
706 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
708 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
710 else if (!DECL_DECLARED_INLINE_P (callee->decl)
711 && !opt_for_fn (e->caller->decl, flag_inline_small_functions))
713 e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
714 want_inline = false;
716 /* Do fast and conservative check if the function can be good
717 inline candidate. At the moment we allow inline hints to
718 promote non-inline functions to inline and we increase
719 MAX_INLINE_INSNS_SINGLE 16-fold for inline functions. */
720 else if ((!DECL_DECLARED_INLINE_P (callee->decl)
721 && (!e->count || !e->maybe_hot_p ()))
722 && ipa_fn_summaries->get (callee)->min_size
723 - ipa_call_summaries->get (e)->call_stmt_size
724 > MAX (MAX_INLINE_INSNS_SINGLE, MAX_INLINE_INSNS_AUTO))
726 e->inline_failed = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
727 want_inline = false;
729 else if ((DECL_DECLARED_INLINE_P (callee->decl) || e->count)
730 && ipa_fn_summaries->get (callee)->min_size
731 - ipa_call_summaries->get (e)->call_stmt_size
732 > 16 * MAX_INLINE_INSNS_SINGLE)
734 e->inline_failed = (DECL_DECLARED_INLINE_P (callee->decl)
735 ? CIF_MAX_INLINE_INSNS_SINGLE_LIMIT
736 : CIF_MAX_INLINE_INSNS_AUTO_LIMIT);
737 want_inline = false;
739 else
741 int growth = estimate_edge_growth (e);
742 ipa_hints hints = estimate_edge_hints (e);
743 bool big_speedup = big_speedup_p (e);
745 if (growth <= 0)
747 /* Apply MAX_INLINE_INSNS_SINGLE limit. Do not do so when
748 hints suggests that inlining given function is very profitable. */
749 else if (DECL_DECLARED_INLINE_P (callee->decl)
750 && growth >= MAX_INLINE_INSNS_SINGLE
751 && ((!big_speedup
752 && !(hints & (INLINE_HINT_indirect_call
753 | INLINE_HINT_known_hot
754 | INLINE_HINT_loop_iterations
755 | INLINE_HINT_array_index
756 | INLINE_HINT_loop_stride)))
757 || growth >= MAX_INLINE_INSNS_SINGLE * 16))
759 e->inline_failed = CIF_MAX_INLINE_INSNS_SINGLE_LIMIT;
760 want_inline = false;
762 else if (!DECL_DECLARED_INLINE_P (callee->decl)
763 && !opt_for_fn (e->caller->decl, flag_inline_functions))
765 /* growth_likely_positive is expensive, always test it last. */
766 if (growth >= MAX_INLINE_INSNS_SINGLE
767 || growth_likely_positive (callee, growth))
769 e->inline_failed = CIF_NOT_DECLARED_INLINED;
770 want_inline = false;
773 /* Apply MAX_INLINE_INSNS_AUTO limit for functions not declared inline
774 Upgrade it to MAX_INLINE_INSNS_SINGLE when hints suggests that
775 inlining given function is very profitable. */
776 else if (!DECL_DECLARED_INLINE_P (callee->decl)
777 && !big_speedup
778 && !(hints & INLINE_HINT_known_hot)
779 && growth >= ((hints & (INLINE_HINT_indirect_call
780 | INLINE_HINT_loop_iterations
781 | INLINE_HINT_array_index
782 | INLINE_HINT_loop_stride))
783 ? MAX (MAX_INLINE_INSNS_AUTO,
784 MAX_INLINE_INSNS_SINGLE)
785 : MAX_INLINE_INSNS_AUTO))
787 /* growth_likely_positive is expensive, always test it last. */
788 if (growth >= MAX_INLINE_INSNS_SINGLE
789 || growth_likely_positive (callee, growth))
791 e->inline_failed = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
792 want_inline = false;
795 /* If call is cold, do not inline when function body would grow. */
796 else if (!e->maybe_hot_p ()
797 && (growth >= MAX_INLINE_INSNS_SINGLE
798 || growth_likely_positive (callee, growth)))
800 e->inline_failed = CIF_UNLIKELY_CALL;
801 want_inline = false;
804 if (!want_inline && report)
805 report_inline_failed_reason (e);
806 return want_inline;
809 /* EDGE is self recursive edge.
810 We hand two cases - when function A is inlining into itself
811 or when function A is being inlined into another inliner copy of function
812 A within function B.
814 In first case OUTER_NODE points to the toplevel copy of A, while
815 in the second case OUTER_NODE points to the outermost copy of A in B.
817 In both cases we want to be extra selective since
818 inlining the call will just introduce new recursive calls to appear. */
820 static bool
821 want_inline_self_recursive_call_p (struct cgraph_edge *edge,
822 struct cgraph_node *outer_node,
823 bool peeling,
824 int depth)
826 char const *reason = NULL;
827 bool want_inline = true;
828 int caller_freq = CGRAPH_FREQ_BASE;
829 int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO);
831 if (DECL_DECLARED_INLINE_P (edge->caller->decl))
832 max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH);
834 if (!edge->maybe_hot_p ())
836 reason = "recursive call is cold";
837 want_inline = false;
839 else if (max_count && !outer_node->count)
841 reason = "not executed in profile";
842 want_inline = false;
844 else if (depth > max_depth)
846 reason = "--param max-inline-recursive-depth exceeded.";
847 want_inline = false;
850 if (outer_node->global.inlined_to)
851 caller_freq = outer_node->callers->frequency;
853 if (!caller_freq)
855 reason = "function is inlined and unlikely";
856 want_inline = false;
859 if (!want_inline)
861 /* Inlining of self recursive function into copy of itself within other function
862 is transformation similar to loop peeling.
864 Peeling is profitable if we can inline enough copies to make probability
865 of actual call to the self recursive function very small. Be sure that
866 the probability of recursion is small.
868 We ensure that the frequency of recursing is at most 1 - (1/max_depth).
869 This way the expected number of recision is at most max_depth. */
870 else if (peeling)
872 int max_prob = CGRAPH_FREQ_BASE - ((CGRAPH_FREQ_BASE + max_depth - 1)
873 / max_depth);
874 int i;
875 for (i = 1; i < depth; i++)
876 max_prob = max_prob * max_prob / CGRAPH_FREQ_BASE;
877 if (max_count
878 && (edge->count * CGRAPH_FREQ_BASE / outer_node->count
879 >= max_prob))
881 reason = "profile of recursive call is too large";
882 want_inline = false;
884 if (!max_count
885 && (edge->frequency * CGRAPH_FREQ_BASE / caller_freq
886 >= max_prob))
888 reason = "frequency of recursive call is too large";
889 want_inline = false;
892 /* Recursive inlining, i.e. equivalent of unrolling, is profitable if recursion
893 depth is large. We reduce function call overhead and increase chances that
894 things fit in hardware return predictor.
896 Recursive inlining might however increase cost of stack frame setup
897 actually slowing down functions whose recursion tree is wide rather than
898 deep.
900 Deciding reliably on when to do recursive inlining without profile feedback
901 is tricky. For now we disable recursive inlining when probability of self
902 recursion is low.
904 Recursive inlining of self recursive call within loop also results in large loop
905 depths that generally optimize badly. We may want to throttle down inlining
906 in those cases. In particular this seems to happen in one of libstdc++ rb tree
907 methods. */
908 else
910 if (max_count
911 && (edge->count * 100 / outer_node->count
912 <= PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY)))
914 reason = "profile of recursive call is too small";
915 want_inline = false;
917 else if (!max_count
918 && (edge->frequency * 100 / caller_freq
919 <= PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY)))
921 reason = "frequency of recursive call is too small";
922 want_inline = false;
925 if (!want_inline && dump_file)
926 fprintf (dump_file, " not inlining recursively: %s\n", reason);
927 return want_inline;
930 /* Return true when NODE has uninlinable caller;
931 set HAS_HOT_CALL if it has hot call.
932 Worker for cgraph_for_node_and_aliases. */
934 static bool
935 check_callers (struct cgraph_node *node, void *has_hot_call)
937 struct cgraph_edge *e;
938 for (e = node->callers; e; e = e->next_caller)
940 if (!opt_for_fn (e->caller->decl, flag_inline_functions_called_once))
941 return true;
942 if (!can_inline_edge_p (e, true))
943 return true;
944 if (e->recursive_p ())
945 return true;
946 if (!(*(bool *)has_hot_call) && e->maybe_hot_p ())
947 *(bool *)has_hot_call = true;
949 return false;
952 /* If NODE has a caller, return true. */
954 static bool
955 has_caller_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
957 if (node->callers)
958 return true;
959 return false;
962 /* Decide if inlining NODE would reduce unit size by eliminating
963 the offline copy of function.
964 When COLD is true the cold calls are considered, too. */
966 static bool
967 want_inline_function_to_all_callers_p (struct cgraph_node *node, bool cold)
969 bool has_hot_call = false;
971 /* Aliases gets inlined along with the function they alias. */
972 if (node->alias)
973 return false;
974 /* Already inlined? */
975 if (node->global.inlined_to)
976 return false;
977 /* Does it have callers? */
978 if (!node->call_for_symbol_and_aliases (has_caller_p, NULL, true))
979 return false;
980 /* Inlining into all callers would increase size? */
981 if (estimate_growth (node) > 0)
982 return false;
983 /* All inlines must be possible. */
984 if (node->call_for_symbol_and_aliases (check_callers, &has_hot_call,
985 true))
986 return false;
987 if (!cold && !has_hot_call)
988 return false;
989 return true;
992 /* A cost model driving the inlining heuristics in a way so the edges with
993 smallest badness are inlined first. After each inlining is performed
994 the costs of all caller edges of nodes affected are recomputed so the
995 metrics may accurately depend on values such as number of inlinable callers
996 of the function or function body size. */
998 static sreal
999 edge_badness (struct cgraph_edge *edge, bool dump)
1001 sreal badness;
1002 int growth;
1003 sreal edge_time, unspec_edge_time;
1004 struct cgraph_node *callee = edge->callee->ultimate_alias_target ();
1005 struct ipa_fn_summary *callee_info = ipa_fn_summaries->get (callee);
1006 ipa_hints hints;
1007 cgraph_node *caller = (edge->caller->global.inlined_to
1008 ? edge->caller->global.inlined_to
1009 : edge->caller);
1011 growth = estimate_edge_growth (edge);
1012 edge_time = estimate_edge_time (edge, &unspec_edge_time);
1013 hints = estimate_edge_hints (edge);
1014 gcc_checking_assert (edge_time >= 0);
1015 /* Check that inlined time is better, but tolerate some roundoff issues. */
1016 gcc_checking_assert ((edge_time - callee_info->time).to_int () <= 0);
1017 gcc_checking_assert (growth <= callee_info->size);
1019 if (dump)
1021 fprintf (dump_file, " Badness calculation for %s -> %s\n",
1022 edge->caller->dump_name (),
1023 edge->callee->dump_name ());
1024 fprintf (dump_file, " size growth %i, time %f unspec %f ",
1025 growth,
1026 edge_time.to_double (),
1027 unspec_edge_time.to_double ());
1028 ipa_dump_hints (dump_file, hints);
1029 if (big_speedup_p (edge))
1030 fprintf (dump_file, " big_speedup");
1031 fprintf (dump_file, "\n");
1034 /* Always prefer inlining saving code size. */
1035 if (growth <= 0)
1037 badness = (sreal) (-SREAL_MIN_SIG + growth) << (SREAL_MAX_EXP / 256);
1038 if (dump)
1039 fprintf (dump_file, " %f: Growth %d <= 0\n", badness.to_double (),
1040 growth);
1042 /* Inlining into EXTERNAL functions is not going to change anything unless
1043 they are themselves inlined. */
1044 else if (DECL_EXTERNAL (caller->decl))
1046 if (dump)
1047 fprintf (dump_file, " max: function is external\n");
1048 return sreal::max ();
1050 /* When profile is available. Compute badness as:
1052 time_saved * caller_count
1053 goodness = -------------------------------------------------
1054 growth_of_caller * overall_growth * combined_size
1056 badness = - goodness
1058 Again use negative value to make calls with profile appear hotter
1059 then calls without.
1061 else if (opt_for_fn (caller->decl, flag_guess_branch_prob) || caller->count)
1063 sreal numerator, denominator;
1064 int overall_growth;
1065 sreal inlined_time = compute_inlined_call_time (edge, edge_time);
1067 numerator = (compute_uninlined_call_time (edge, unspec_edge_time)
1068 - inlined_time);
1069 if (numerator == 0)
1070 numerator = ((sreal) 1 >> 8);
1071 if (caller->count)
1072 numerator *= caller->count;
1073 else if (opt_for_fn (caller->decl, flag_branch_probabilities))
1074 numerator = numerator >> 11;
1075 denominator = growth;
1077 overall_growth = callee_info->growth;
1079 /* Look for inliner wrappers of the form:
1081 inline_caller ()
1083 do_fast_job...
1084 if (need_more_work)
1085 noninline_callee ();
1087 Withhout panilizing this case, we usually inline noninline_callee
1088 into the inline_caller because overall_growth is small preventing
1089 further inlining of inline_caller.
1091 Penalize only callgraph edges to functions with small overall
1092 growth ...
1094 if (growth > overall_growth
1095 /* ... and having only one caller which is not inlined ... */
1096 && callee_info->single_caller
1097 && !edge->caller->global.inlined_to
1098 /* ... and edges executed only conditionally ... */
1099 && edge->frequency < CGRAPH_FREQ_BASE
1100 /* ... consider case where callee is not inline but caller is ... */
1101 && ((!DECL_DECLARED_INLINE_P (edge->callee->decl)
1102 && DECL_DECLARED_INLINE_P (caller->decl))
1103 /* ... or when early optimizers decided to split and edge
1104 frequency still indicates splitting is a win ... */
1105 || (callee->split_part && !caller->split_part
1106 && edge->frequency
1107 < CGRAPH_FREQ_BASE
1108 * PARAM_VALUE
1109 (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100
1110 /* ... and do not overwrite user specified hints. */
1111 && (!DECL_DECLARED_INLINE_P (edge->callee->decl)
1112 || DECL_DECLARED_INLINE_P (caller->decl)))))
1114 struct ipa_fn_summary *caller_info = ipa_fn_summaries->get (caller);
1115 int caller_growth = caller_info->growth;
1117 /* Only apply the penalty when caller looks like inline candidate,
1118 and it is not called once and. */
1119 if (!caller_info->single_caller && overall_growth < caller_growth
1120 && caller_info->inlinable
1121 && caller_info->size
1122 < (DECL_DECLARED_INLINE_P (caller->decl)
1123 ? MAX_INLINE_INSNS_SINGLE : MAX_INLINE_INSNS_AUTO))
1125 if (dump)
1126 fprintf (dump_file,
1127 " Wrapper penalty. Increasing growth %i to %i\n",
1128 overall_growth, caller_growth);
1129 overall_growth = caller_growth;
1132 if (overall_growth > 0)
1134 /* Strongly preffer functions with few callers that can be inlined
1135 fully. The square root here leads to smaller binaries at average.
1136 Watch however for extreme cases and return to linear function
1137 when growth is large. */
1138 if (overall_growth < 256)
1139 overall_growth *= overall_growth;
1140 else
1141 overall_growth += 256 * 256 - 256;
1142 denominator *= overall_growth;
1144 denominator *= inlined_time;
1146 badness = - numerator / denominator;
1148 if (dump)
1150 fprintf (dump_file,
1151 " %f: guessed profile. frequency %f, count %" PRId64
1152 " caller count %" PRId64
1153 " time w/o inlining %f, time with inlining %f"
1154 " overall growth %i (current) %i (original)"
1155 " %i (compensated)\n",
1156 badness.to_double (),
1157 (double)edge->frequency / CGRAPH_FREQ_BASE,
1158 edge->count, caller->count,
1159 compute_uninlined_call_time (edge,
1160 unspec_edge_time).to_double (),
1161 compute_inlined_call_time (edge, edge_time).to_double (),
1162 estimate_growth (callee),
1163 callee_info->growth, overall_growth);
1166 /* When function local profile is not available or it does not give
1167 useful information (ie frequency is zero), base the cost on
1168 loop nest and overall size growth, so we optimize for overall number
1169 of functions fully inlined in program. */
1170 else
1172 int nest = MIN (ipa_call_summaries->get (edge)->loop_depth, 8);
1173 badness = growth;
1175 /* Decrease badness if call is nested. */
1176 if (badness > 0)
1177 badness = badness >> nest;
1178 else
1179 badness = badness << nest;
1180 if (dump)
1181 fprintf (dump_file, " %f: no profile. nest %i\n",
1182 badness.to_double (), nest);
1184 gcc_checking_assert (badness != 0);
1186 if (edge->recursive_p ())
1187 badness = badness.shift (badness > 0 ? 4 : -4);
1188 if ((hints & (INLINE_HINT_indirect_call
1189 | INLINE_HINT_loop_iterations
1190 | INLINE_HINT_array_index
1191 | INLINE_HINT_loop_stride))
1192 || callee_info->growth <= 0)
1193 badness = badness.shift (badness > 0 ? -2 : 2);
1194 if (hints & (INLINE_HINT_same_scc))
1195 badness = badness.shift (badness > 0 ? 3 : -3);
1196 else if (hints & (INLINE_HINT_in_scc))
1197 badness = badness.shift (badness > 0 ? 2 : -2);
1198 else if (hints & (INLINE_HINT_cross_module))
1199 badness = badness.shift (badness > 0 ? 1 : -1);
1200 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
1201 badness = badness.shift (badness > 0 ? -4 : 4);
1202 else if ((hints & INLINE_HINT_declared_inline))
1203 badness = badness.shift (badness > 0 ? -3 : 3);
1204 if (dump)
1205 fprintf (dump_file, " Adjusted by hints %f\n", badness.to_double ());
1206 return badness;
1209 /* Recompute badness of EDGE and update its key in HEAP if needed. */
1210 static inline void
1211 update_edge_key (edge_heap_t *heap, struct cgraph_edge *edge)
1213 sreal badness = edge_badness (edge, false);
1214 if (edge->aux)
1216 edge_heap_node_t *n = (edge_heap_node_t *) edge->aux;
1217 gcc_checking_assert (n->get_data () == edge);
1219 /* fibonacci_heap::replace_key does busy updating of the
1220 heap that is unnecesarily expensive.
1221 We do lazy increases: after extracting minimum if the key
1222 turns out to be out of date, it is re-inserted into heap
1223 with correct value. */
1224 if (badness < n->get_key ())
1226 if (dump_file && (dump_flags & TDF_DETAILS))
1228 fprintf (dump_file,
1229 " decreasing badness %s -> %s, %f to %f\n",
1230 edge->caller->dump_name (),
1231 edge->callee->dump_name (),
1232 n->get_key ().to_double (),
1233 badness.to_double ());
1235 heap->decrease_key (n, badness);
1238 else
1240 if (dump_file && (dump_flags & TDF_DETAILS))
1242 fprintf (dump_file,
1243 " enqueuing call %s -> %s, badness %f\n",
1244 edge->caller->dump_name (),
1245 edge->callee->dump_name (),
1246 badness.to_double ());
1248 edge->aux = heap->insert (badness, edge);
1253 /* NODE was inlined.
1254 All caller edges needs to be resetted because
1255 size estimates change. Similarly callees needs reset
1256 because better context may be known. */
1258 static void
1259 reset_edge_caches (struct cgraph_node *node)
1261 struct cgraph_edge *edge;
1262 struct cgraph_edge *e = node->callees;
1263 struct cgraph_node *where = node;
1264 struct ipa_ref *ref;
1266 if (where->global.inlined_to)
1267 where = where->global.inlined_to;
1269 for (edge = where->callers; edge; edge = edge->next_caller)
1270 if (edge->inline_failed)
1271 reset_edge_growth_cache (edge);
1273 FOR_EACH_ALIAS (where, ref)
1274 reset_edge_caches (dyn_cast <cgraph_node *> (ref->referring));
1276 if (!e)
1277 return;
1279 while (true)
1280 if (!e->inline_failed && e->callee->callees)
1281 e = e->callee->callees;
1282 else
1284 if (e->inline_failed)
1285 reset_edge_growth_cache (e);
1286 if (e->next_callee)
1287 e = e->next_callee;
1288 else
1292 if (e->caller == node)
1293 return;
1294 e = e->caller->callers;
1296 while (!e->next_callee);
1297 e = e->next_callee;
1302 /* Recompute HEAP nodes for each of caller of NODE.
1303 UPDATED_NODES track nodes we already visited, to avoid redundant work.
1304 When CHECK_INLINABLITY_FOR is set, re-check for specified edge that
1305 it is inlinable. Otherwise check all edges. */
1307 static void
1308 update_caller_keys (edge_heap_t *heap, struct cgraph_node *node,
1309 bitmap updated_nodes,
1310 struct cgraph_edge *check_inlinablity_for)
1312 struct cgraph_edge *edge;
1313 struct ipa_ref *ref;
1315 if ((!node->alias && !ipa_fn_summaries->get (node)->inlinable)
1316 || node->global.inlined_to)
1317 return;
1318 if (!bitmap_set_bit (updated_nodes, node->uid))
1319 return;
1321 FOR_EACH_ALIAS (node, ref)
1323 struct cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1324 update_caller_keys (heap, alias, updated_nodes, check_inlinablity_for);
1327 for (edge = node->callers; edge; edge = edge->next_caller)
1328 if (edge->inline_failed)
1330 if (!check_inlinablity_for
1331 || check_inlinablity_for == edge)
1333 if (can_inline_edge_p (edge, false)
1334 && want_inline_small_function_p (edge, false))
1335 update_edge_key (heap, edge);
1336 else if (edge->aux)
1338 report_inline_failed_reason (edge);
1339 heap->delete_node ((edge_heap_node_t *) edge->aux);
1340 edge->aux = NULL;
1343 else if (edge->aux)
1344 update_edge_key (heap, edge);
1348 /* Recompute HEAP nodes for each uninlined call in NODE.
1349 This is used when we know that edge badnesses are going only to increase
1350 (we introduced new call site) and thus all we need is to insert newly
1351 created edges into heap. */
1353 static void
1354 update_callee_keys (edge_heap_t *heap, struct cgraph_node *node,
1355 bitmap updated_nodes)
1357 struct cgraph_edge *e = node->callees;
1359 if (!e)
1360 return;
1361 while (true)
1362 if (!e->inline_failed && e->callee->callees)
1363 e = e->callee->callees;
1364 else
1366 enum availability avail;
1367 struct cgraph_node *callee;
1368 /* We do not reset callee growth cache here. Since we added a new call,
1369 growth chould have just increased and consequentely badness metric
1370 don't need updating. */
1371 if (e->inline_failed
1372 && (callee = e->callee->ultimate_alias_target (&avail, e->caller))
1373 && ipa_fn_summaries->get (callee)->inlinable
1374 && avail >= AVAIL_AVAILABLE
1375 && !bitmap_bit_p (updated_nodes, callee->uid))
1377 if (can_inline_edge_p (e, false)
1378 && want_inline_small_function_p (e, false))
1379 update_edge_key (heap, e);
1380 else if (e->aux)
1382 report_inline_failed_reason (e);
1383 heap->delete_node ((edge_heap_node_t *) e->aux);
1384 e->aux = NULL;
1387 if (e->next_callee)
1388 e = e->next_callee;
1389 else
1393 if (e->caller == node)
1394 return;
1395 e = e->caller->callers;
1397 while (!e->next_callee);
1398 e = e->next_callee;
1403 /* Enqueue all recursive calls from NODE into priority queue depending on
1404 how likely we want to recursively inline the call. */
1406 static void
1407 lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
1408 edge_heap_t *heap)
1410 struct cgraph_edge *e;
1411 enum availability avail;
1413 for (e = where->callees; e; e = e->next_callee)
1414 if (e->callee == node
1415 || (e->callee->ultimate_alias_target (&avail, e->caller) == node
1416 && avail > AVAIL_INTERPOSABLE))
1418 /* When profile feedback is available, prioritize by expected number
1419 of calls. */
1420 heap->insert (!max_count ? -e->frequency
1421 : -(e->count / ((max_count + (1<<24) - 1) / (1<<24))),
1424 for (e = where->callees; e; e = e->next_callee)
1425 if (!e->inline_failed)
1426 lookup_recursive_calls (node, e->callee, heap);
1429 /* Decide on recursive inlining: in the case function has recursive calls,
1430 inline until body size reaches given argument. If any new indirect edges
1431 are discovered in the process, add them to *NEW_EDGES, unless NEW_EDGES
1432 is NULL. */
1434 static bool
1435 recursive_inlining (struct cgraph_edge *edge,
1436 vec<cgraph_edge *> *new_edges)
1438 int limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO);
1439 edge_heap_t heap (sreal::min ());
1440 struct cgraph_node *node;
1441 struct cgraph_edge *e;
1442 struct cgraph_node *master_clone = NULL, *next;
1443 int depth = 0;
1444 int n = 0;
1446 node = edge->caller;
1447 if (node->global.inlined_to)
1448 node = node->global.inlined_to;
1450 if (DECL_DECLARED_INLINE_P (node->decl))
1451 limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE);
1453 /* Make sure that function is small enough to be considered for inlining. */
1454 if (estimate_size_after_inlining (node, edge) >= limit)
1455 return false;
1456 lookup_recursive_calls (node, node, &heap);
1457 if (heap.empty ())
1458 return false;
1460 if (dump_file)
1461 fprintf (dump_file,
1462 " Performing recursive inlining on %s\n",
1463 node->name ());
1465 /* Do the inlining and update list of recursive call during process. */
1466 while (!heap.empty ())
1468 struct cgraph_edge *curr = heap.extract_min ();
1469 struct cgraph_node *cnode, *dest = curr->callee;
1471 if (!can_inline_edge_p (curr, true))
1472 continue;
1474 /* MASTER_CLONE is produced in the case we already started modified
1475 the function. Be sure to redirect edge to the original body before
1476 estimating growths otherwise we will be seeing growths after inlining
1477 the already modified body. */
1478 if (master_clone)
1480 curr->redirect_callee (master_clone);
1481 reset_edge_growth_cache (curr);
1484 if (estimate_size_after_inlining (node, curr) > limit)
1486 curr->redirect_callee (dest);
1487 reset_edge_growth_cache (curr);
1488 break;
1491 depth = 1;
1492 for (cnode = curr->caller;
1493 cnode->global.inlined_to; cnode = cnode->callers->caller)
1494 if (node->decl
1495 == curr->callee->ultimate_alias_target ()->decl)
1496 depth++;
1498 if (!want_inline_self_recursive_call_p (curr, node, false, depth))
1500 curr->redirect_callee (dest);
1501 reset_edge_growth_cache (curr);
1502 continue;
1505 if (dump_file)
1507 fprintf (dump_file,
1508 " Inlining call of depth %i", depth);
1509 if (node->count)
1511 fprintf (dump_file, " called approx. %.2f times per call",
1512 (double)curr->count / node->count);
1514 fprintf (dump_file, "\n");
1516 if (!master_clone)
1518 /* We need original clone to copy around. */
1519 master_clone = node->create_clone (node->decl, node->count,
1520 CGRAPH_FREQ_BASE, false, vNULL,
1521 true, NULL, NULL);
1522 for (e = master_clone->callees; e; e = e->next_callee)
1523 if (!e->inline_failed)
1524 clone_inlined_nodes (e, true, false, NULL, CGRAPH_FREQ_BASE);
1525 curr->redirect_callee (master_clone);
1526 reset_edge_growth_cache (curr);
1529 inline_call (curr, false, new_edges, &overall_size, true);
1530 lookup_recursive_calls (node, curr->callee, &heap);
1531 n++;
1534 if (!heap.empty () && dump_file)
1535 fprintf (dump_file, " Recursive inlining growth limit met.\n");
1537 if (!master_clone)
1538 return false;
1540 if (dump_file)
1541 fprintf (dump_file,
1542 "\n Inlined %i times, "
1543 "body grown from size %i to %i, time %f to %f\n", n,
1544 ipa_fn_summaries->get (master_clone)->size,
1545 ipa_fn_summaries->get (node)->size,
1546 ipa_fn_summaries->get (master_clone)->time.to_double (),
1547 ipa_fn_summaries->get (node)->time.to_double ());
1549 /* Remove master clone we used for inlining. We rely that clones inlined
1550 into master clone gets queued just before master clone so we don't
1551 need recursion. */
1552 for (node = symtab->first_function (); node != master_clone;
1553 node = next)
1555 next = symtab->next_function (node);
1556 if (node->global.inlined_to == master_clone)
1557 node->remove ();
1559 master_clone->remove ();
1560 return true;
1564 /* Given whole compilation unit estimate of INSNS, compute how large we can
1565 allow the unit to grow. */
1567 static int
1568 compute_max_insns (int insns)
1570 int max_insns = insns;
1571 if (max_insns < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
1572 max_insns = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
1574 return ((int64_t) max_insns
1575 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
1579 /* Compute badness of all edges in NEW_EDGES and add them to the HEAP. */
1581 static void
1582 add_new_edges_to_heap (edge_heap_t *heap, vec<cgraph_edge *> new_edges)
1584 while (new_edges.length () > 0)
1586 struct cgraph_edge *edge = new_edges.pop ();
1588 gcc_assert (!edge->aux);
1589 if (edge->inline_failed
1590 && can_inline_edge_p (edge, true)
1591 && want_inline_small_function_p (edge, true))
1592 edge->aux = heap->insert (edge_badness (edge, false), edge);
1596 /* Remove EDGE from the fibheap. */
1598 static void
1599 heap_edge_removal_hook (struct cgraph_edge *e, void *data)
1601 if (e->aux)
1603 ((edge_heap_t *)data)->delete_node ((edge_heap_node_t *)e->aux);
1604 e->aux = NULL;
1608 /* Return true if speculation of edge E seems useful.
1609 If ANTICIPATE_INLINING is true, be conservative and hope that E
1610 may get inlined. */
1612 bool
1613 speculation_useful_p (struct cgraph_edge *e, bool anticipate_inlining)
1615 enum availability avail;
1616 struct cgraph_node *target = e->callee->ultimate_alias_target (&avail,
1617 e->caller);
1618 struct cgraph_edge *direct, *indirect;
1619 struct ipa_ref *ref;
1621 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1623 if (!e->maybe_hot_p ())
1624 return false;
1626 /* See if IP optimizations found something potentially useful about the
1627 function. For now we look only for CONST/PURE flags. Almost everything
1628 else we propagate is useless. */
1629 if (avail >= AVAIL_AVAILABLE)
1631 int ecf_flags = flags_from_decl_or_type (target->decl);
1632 if (ecf_flags & ECF_CONST)
1634 e->speculative_call_info (direct, indirect, ref);
1635 if (!(indirect->indirect_info->ecf_flags & ECF_CONST))
1636 return true;
1638 else if (ecf_flags & ECF_PURE)
1640 e->speculative_call_info (direct, indirect, ref);
1641 if (!(indirect->indirect_info->ecf_flags & ECF_PURE))
1642 return true;
1645 /* If we did not managed to inline the function nor redirect
1646 to an ipa-cp clone (that are seen by having local flag set),
1647 it is probably pointless to inline it unless hardware is missing
1648 indirect call predictor. */
1649 if (!anticipate_inlining && e->inline_failed && !target->local.local)
1650 return false;
1651 /* For overwritable targets there is not much to do. */
1652 if (e->inline_failed && !can_inline_edge_p (e, false, true))
1653 return false;
1654 /* OK, speculation seems interesting. */
1655 return true;
1658 /* We know that EDGE is not going to be inlined.
1659 See if we can remove speculation. */
1661 static void
1662 resolve_noninline_speculation (edge_heap_t *edge_heap, struct cgraph_edge *edge)
1664 if (edge->speculative && !speculation_useful_p (edge, false))
1666 struct cgraph_node *node = edge->caller;
1667 struct cgraph_node *where = node->global.inlined_to
1668 ? node->global.inlined_to : node;
1669 auto_bitmap updated_nodes;
1671 spec_rem += edge->count;
1672 edge->resolve_speculation ();
1673 reset_edge_caches (where);
1674 ipa_update_overall_fn_summary (where);
1675 update_caller_keys (edge_heap, where,
1676 updated_nodes, NULL);
1677 update_callee_keys (edge_heap, where,
1678 updated_nodes);
1682 /* Return true if NODE should be accounted for overall size estimate.
1683 Skip all nodes optimized for size so we can measure the growth of hot
1684 part of program no matter of the padding. */
1686 bool
1687 inline_account_function_p (struct cgraph_node *node)
1689 return (!DECL_EXTERNAL (node->decl)
1690 && !opt_for_fn (node->decl, optimize_size)
1691 && node->frequency != NODE_FREQUENCY_UNLIKELY_EXECUTED);
1694 /* Count number of callers of NODE and store it into DATA (that
1695 points to int. Worker for cgraph_for_node_and_aliases. */
1697 static bool
1698 sum_callers (struct cgraph_node *node, void *data)
1700 struct cgraph_edge *e;
1701 int *num_calls = (int *)data;
1703 for (e = node->callers; e; e = e->next_caller)
1704 (*num_calls)++;
1705 return false;
1708 /* We use greedy algorithm for inlining of small functions:
1709 All inline candidates are put into prioritized heap ordered in
1710 increasing badness.
1712 The inlining of small functions is bounded by unit growth parameters. */
1714 static void
1715 inline_small_functions (void)
1717 struct cgraph_node *node;
1718 struct cgraph_edge *edge;
1719 edge_heap_t edge_heap (sreal::min ());
1720 auto_bitmap updated_nodes;
1721 int min_size, max_size;
1722 auto_vec<cgraph_edge *> new_indirect_edges;
1723 int initial_size = 0;
1724 struct cgraph_node **order = XCNEWVEC (cgraph_node *, symtab->cgraph_count);
1725 struct cgraph_edge_hook_list *edge_removal_hook_holder;
1726 new_indirect_edges.create (8);
1728 edge_removal_hook_holder
1729 = symtab->add_edge_removal_hook (&heap_edge_removal_hook, &edge_heap);
1731 /* Compute overall unit size and other global parameters used by badness
1732 metrics. */
1734 max_count = 0;
1735 ipa_reduced_postorder (order, true, true, NULL);
1736 free (order);
1738 FOR_EACH_DEFINED_FUNCTION (node)
1739 if (!node->global.inlined_to)
1741 if (!node->alias && node->analyzed
1742 && (node->has_gimple_body_p () || node->thunk.thunk_p))
1744 struct ipa_fn_summary *info = ipa_fn_summaries->get (node);
1745 struct ipa_dfs_info *dfs = (struct ipa_dfs_info *) node->aux;
1747 /* Do not account external functions, they will be optimized out
1748 if not inlined. Also only count the non-cold portion of program. */
1749 if (inline_account_function_p (node))
1750 initial_size += info->size;
1751 info->growth = estimate_growth (node);
1753 int num_calls = 0;
1754 node->call_for_symbol_and_aliases (sum_callers, &num_calls,
1755 true);
1756 if (num_calls == 1)
1757 info->single_caller = true;
1758 if (dfs && dfs->next_cycle)
1760 struct cgraph_node *n2;
1761 int id = dfs->scc_no + 1;
1762 for (n2 = node; n2;
1763 n2 = ((struct ipa_dfs_info *) node->aux)->next_cycle)
1765 struct ipa_fn_summary *info2 = ipa_fn_summaries->get (n2);
1766 if (info2->scc_no)
1767 break;
1768 info2->scc_no = id;
1773 for (edge = node->callers; edge; edge = edge->next_caller)
1774 if (max_count < edge->count)
1775 max_count = edge->count;
1777 ipa_free_postorder_info ();
1778 initialize_growth_caches ();
1780 if (dump_file)
1781 fprintf (dump_file,
1782 "\nDeciding on inlining of small functions. Starting with size %i.\n",
1783 initial_size);
1785 overall_size = initial_size;
1786 max_size = compute_max_insns (overall_size);
1787 min_size = overall_size;
1789 /* Populate the heap with all edges we might inline. */
1791 FOR_EACH_DEFINED_FUNCTION (node)
1793 bool update = false;
1794 struct cgraph_edge *next = NULL;
1795 bool has_speculative = false;
1797 if (dump_file)
1798 fprintf (dump_file, "Enqueueing calls in %s.\n", node->dump_name ());
1800 for (edge = node->callees; edge; edge = next)
1802 next = edge->next_callee;
1803 if (edge->inline_failed
1804 && !edge->aux
1805 && can_inline_edge_p (edge, true)
1806 && want_inline_small_function_p (edge, true)
1807 && edge->inline_failed)
1809 gcc_assert (!edge->aux);
1810 update_edge_key (&edge_heap, edge);
1812 if (edge->speculative)
1813 has_speculative = true;
1815 if (has_speculative)
1816 for (edge = node->callees; edge; edge = next)
1817 if (edge->speculative && !speculation_useful_p (edge,
1818 edge->aux != NULL))
1820 edge->resolve_speculation ();
1821 update = true;
1823 if (update)
1825 struct cgraph_node *where = node->global.inlined_to
1826 ? node->global.inlined_to : node;
1827 ipa_update_overall_fn_summary (where);
1828 reset_edge_caches (where);
1829 update_caller_keys (&edge_heap, where,
1830 updated_nodes, NULL);
1831 update_callee_keys (&edge_heap, where,
1832 updated_nodes);
1833 bitmap_clear (updated_nodes);
1837 gcc_assert (in_lto_p
1838 || !max_count
1839 || (profile_info && flag_branch_probabilities));
1841 while (!edge_heap.empty ())
1843 int old_size = overall_size;
1844 struct cgraph_node *where, *callee;
1845 sreal badness = edge_heap.min_key ();
1846 sreal current_badness;
1847 int growth;
1849 edge = edge_heap.extract_min ();
1850 gcc_assert (edge->aux);
1851 edge->aux = NULL;
1852 if (!edge->inline_failed || !edge->callee->analyzed)
1853 continue;
1855 #if CHECKING_P
1856 /* Be sure that caches are maintained consistent. */
1857 sreal cached_badness = edge_badness (edge, false);
1859 int old_size_est = estimate_edge_size (edge);
1860 sreal old_time_est = estimate_edge_time (edge);
1861 int old_hints_est = estimate_edge_hints (edge);
1863 reset_edge_growth_cache (edge);
1864 gcc_assert (old_size_est == estimate_edge_size (edge));
1865 gcc_assert (old_time_est == estimate_edge_time (edge));
1866 /* FIXME:
1868 gcc_assert (old_hints_est == estimate_edge_hints (edge));
1870 fails with profile feedback because some hints depends on
1871 maybe_hot_edge_p predicate and because callee gets inlined to other
1872 calls, the edge may become cold.
1873 This ought to be fixed by computing relative probabilities
1874 for given invocation but that will be better done once whole
1875 code is converted to sreals. Disable for now and revert to "wrong"
1876 value so enable/disable checking paths agree. */
1877 edge_growth_cache[edge->uid].hints = old_hints_est + 1;
1879 /* When updating the edge costs, we only decrease badness in the keys.
1880 Increases of badness are handled lazilly; when we see key with out
1881 of date value on it, we re-insert it now. */
1882 current_badness = edge_badness (edge, false);
1883 /* Disable checking for profile because roundoff errors may cause slight
1884 deviations in the order. */
1885 gcc_assert (max_count || cached_badness == current_badness);
1886 gcc_assert (current_badness >= badness);
1887 #else
1888 current_badness = edge_badness (edge, false);
1889 #endif
1890 if (current_badness != badness)
1892 if (edge_heap.min () && current_badness > edge_heap.min_key ())
1894 edge->aux = edge_heap.insert (current_badness, edge);
1895 continue;
1897 else
1898 badness = current_badness;
1901 if (!can_inline_edge_p (edge, true))
1903 resolve_noninline_speculation (&edge_heap, edge);
1904 continue;
1907 callee = edge->callee->ultimate_alias_target ();
1908 growth = estimate_edge_growth (edge);
1909 if (dump_file)
1911 fprintf (dump_file,
1912 "\nConsidering %s with %i size\n",
1913 callee->dump_name (),
1914 ipa_fn_summaries->get (callee)->size);
1915 fprintf (dump_file,
1916 " to be inlined into %s in %s:%i\n"
1917 " Estimated badness is %f, frequency %.2f.\n",
1918 edge->caller->dump_name (),
1919 edge->call_stmt
1920 && (LOCATION_LOCUS (gimple_location ((const gimple *)
1921 edge->call_stmt))
1922 > BUILTINS_LOCATION)
1923 ? gimple_filename ((const gimple *) edge->call_stmt)
1924 : "unknown",
1925 edge->call_stmt
1926 ? gimple_lineno ((const gimple *) edge->call_stmt)
1927 : -1,
1928 badness.to_double (),
1929 edge->frequency / (double)CGRAPH_FREQ_BASE);
1930 if (edge->count)
1931 fprintf (dump_file," Called %" PRId64"x\n",
1932 edge->count);
1933 if (dump_flags & TDF_DETAILS)
1934 edge_badness (edge, true);
1937 if (overall_size + growth > max_size
1938 && !DECL_DISREGARD_INLINE_LIMITS (callee->decl))
1940 edge->inline_failed = CIF_INLINE_UNIT_GROWTH_LIMIT;
1941 report_inline_failed_reason (edge);
1942 resolve_noninline_speculation (&edge_heap, edge);
1943 continue;
1946 if (!want_inline_small_function_p (edge, true))
1948 resolve_noninline_speculation (&edge_heap, edge);
1949 continue;
1952 /* Heuristics for inlining small functions work poorly for
1953 recursive calls where we do effects similar to loop unrolling.
1954 When inlining such edge seems profitable, leave decision on
1955 specific inliner. */
1956 if (edge->recursive_p ())
1958 where = edge->caller;
1959 if (where->global.inlined_to)
1960 where = where->global.inlined_to;
1961 if (!recursive_inlining (edge,
1962 opt_for_fn (edge->caller->decl,
1963 flag_indirect_inlining)
1964 ? &new_indirect_edges : NULL))
1966 edge->inline_failed = CIF_RECURSIVE_INLINING;
1967 resolve_noninline_speculation (&edge_heap, edge);
1968 continue;
1970 reset_edge_caches (where);
1971 /* Recursive inliner inlines all recursive calls of the function
1972 at once. Consequently we need to update all callee keys. */
1973 if (opt_for_fn (edge->caller->decl, flag_indirect_inlining))
1974 add_new_edges_to_heap (&edge_heap, new_indirect_edges);
1975 update_callee_keys (&edge_heap, where, updated_nodes);
1976 bitmap_clear (updated_nodes);
1978 else
1980 struct cgraph_node *outer_node = NULL;
1981 int depth = 0;
1983 /* Consider the case where self recursive function A is inlined
1984 into B. This is desired optimization in some cases, since it
1985 leads to effect similar of loop peeling and we might completely
1986 optimize out the recursive call. However we must be extra
1987 selective. */
1989 where = edge->caller;
1990 while (where->global.inlined_to)
1992 if (where->decl == callee->decl)
1993 outer_node = where, depth++;
1994 where = where->callers->caller;
1996 if (outer_node
1997 && !want_inline_self_recursive_call_p (edge, outer_node,
1998 true, depth))
2000 edge->inline_failed
2001 = (DECL_DISREGARD_INLINE_LIMITS (edge->callee->decl)
2002 ? CIF_RECURSIVE_INLINING : CIF_UNSPECIFIED);
2003 resolve_noninline_speculation (&edge_heap, edge);
2004 continue;
2006 else if (depth && dump_file)
2007 fprintf (dump_file, " Peeling recursion with depth %i\n", depth);
2009 gcc_checking_assert (!callee->global.inlined_to);
2010 inline_call (edge, true, &new_indirect_edges, &overall_size, true);
2011 add_new_edges_to_heap (&edge_heap, new_indirect_edges);
2013 reset_edge_caches (edge->callee);
2015 update_callee_keys (&edge_heap, where, updated_nodes);
2017 where = edge->caller;
2018 if (where->global.inlined_to)
2019 where = where->global.inlined_to;
2021 /* Our profitability metric can depend on local properties
2022 such as number of inlinable calls and size of the function body.
2023 After inlining these properties might change for the function we
2024 inlined into (since it's body size changed) and for the functions
2025 called by function we inlined (since number of it inlinable callers
2026 might change). */
2027 update_caller_keys (&edge_heap, where, updated_nodes, NULL);
2028 /* Offline copy count has possibly changed, recompute if profile is
2029 available. */
2030 if (max_count)
2032 struct cgraph_node *n = cgraph_node::get (edge->callee->decl);
2033 if (n != edge->callee && n->analyzed)
2034 update_callee_keys (&edge_heap, n, updated_nodes);
2036 bitmap_clear (updated_nodes);
2038 if (dump_file)
2040 fprintf (dump_file,
2041 " Inlined %s into %s which now has time %f and size %i, "
2042 "net change of %+i.\n",
2043 edge->callee->name (),
2044 edge->caller->name (),
2045 ipa_fn_summaries->get (edge->caller)->time.to_double (),
2046 ipa_fn_summaries->get (edge->caller)->size,
2047 overall_size - old_size);
2049 if (min_size > overall_size)
2051 min_size = overall_size;
2052 max_size = compute_max_insns (min_size);
2054 if (dump_file)
2055 fprintf (dump_file, "New minimal size reached: %i\n", min_size);
2059 free_growth_caches ();
2060 if (dump_file)
2061 fprintf (dump_file,
2062 "Unit growth for small function inlining: %i->%i (%i%%)\n",
2063 initial_size, overall_size,
2064 initial_size ? overall_size * 100 / (initial_size) - 100: 0);
2065 symtab->remove_edge_removal_hook (edge_removal_hook_holder);
2068 /* Flatten NODE. Performed both during early inlining and
2069 at IPA inlining time. */
2071 static void
2072 flatten_function (struct cgraph_node *node, bool early)
2074 struct cgraph_edge *e;
2076 /* We shouldn't be called recursively when we are being processed. */
2077 gcc_assert (node->aux == NULL);
2079 node->aux = (void *) node;
2081 for (e = node->callees; e; e = e->next_callee)
2083 struct cgraph_node *orig_callee;
2084 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
2086 /* We've hit cycle? It is time to give up. */
2087 if (callee->aux)
2089 if (dump_file)
2090 fprintf (dump_file,
2091 "Not inlining %s into %s to avoid cycle.\n",
2092 xstrdup_for_dump (callee->name ()),
2093 xstrdup_for_dump (e->caller->name ()));
2094 e->inline_failed = CIF_RECURSIVE_INLINING;
2095 continue;
2098 /* When the edge is already inlined, we just need to recurse into
2099 it in order to fully flatten the leaves. */
2100 if (!e->inline_failed)
2102 flatten_function (callee, early);
2103 continue;
2106 /* Flatten attribute needs to be processed during late inlining. For
2107 extra code quality we however do flattening during early optimization,
2108 too. */
2109 if (!early
2110 ? !can_inline_edge_p (e, true)
2111 : !can_early_inline_edge_p (e))
2112 continue;
2114 if (e->recursive_p ())
2116 if (dump_file)
2117 fprintf (dump_file, "Not inlining: recursive call.\n");
2118 continue;
2121 if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
2122 != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
2124 if (dump_file)
2125 fprintf (dump_file, "Not inlining: SSA form does not match.\n");
2126 continue;
2129 /* Inline the edge and flatten the inline clone. Avoid
2130 recursing through the original node if the node was cloned. */
2131 if (dump_file)
2132 fprintf (dump_file, " Inlining %s into %s.\n",
2133 xstrdup_for_dump (callee->name ()),
2134 xstrdup_for_dump (e->caller->name ()));
2135 orig_callee = callee;
2136 inline_call (e, true, NULL, NULL, false);
2137 if (e->callee != orig_callee)
2138 orig_callee->aux = (void *) node;
2139 flatten_function (e->callee, early);
2140 if (e->callee != orig_callee)
2141 orig_callee->aux = NULL;
2144 node->aux = NULL;
2145 if (!node->global.inlined_to)
2146 ipa_update_overall_fn_summary (node);
2149 /* Inline NODE to all callers. Worker for cgraph_for_node_and_aliases.
2150 DATA points to number of calls originally found so we avoid infinite
2151 recursion. */
2153 static bool
2154 inline_to_all_callers_1 (struct cgraph_node *node, void *data,
2155 hash_set<cgraph_node *> *callers)
2157 int *num_calls = (int *)data;
2158 bool callee_removed = false;
2160 while (node->callers && !node->global.inlined_to)
2162 struct cgraph_node *caller = node->callers->caller;
2164 if (!can_inline_edge_p (node->callers, true)
2165 || node->callers->recursive_p ())
2167 if (dump_file)
2168 fprintf (dump_file, "Uninlinable call found; giving up.\n");
2169 *num_calls = 0;
2170 return false;
2173 if (dump_file)
2175 fprintf (dump_file,
2176 "\nInlining %s size %i.\n",
2177 node->name (),
2178 ipa_fn_summaries->get (node)->size);
2179 fprintf (dump_file,
2180 " Called once from %s %i insns.\n",
2181 node->callers->caller->name (),
2182 ipa_fn_summaries->get (node->callers->caller)->size);
2185 /* Remember which callers we inlined to, delaying updating the
2186 overall summary. */
2187 callers->add (node->callers->caller);
2188 inline_call (node->callers, true, NULL, NULL, false, &callee_removed);
2189 if (dump_file)
2190 fprintf (dump_file,
2191 " Inlined into %s which now has %i size\n",
2192 caller->name (),
2193 ipa_fn_summaries->get (caller)->size);
2194 if (!(*num_calls)--)
2196 if (dump_file)
2197 fprintf (dump_file, "New calls found; giving up.\n");
2198 return callee_removed;
2200 if (callee_removed)
2201 return true;
2203 return false;
2206 /* Wrapper around inline_to_all_callers_1 doing delayed overall summary
2207 update. */
2209 static bool
2210 inline_to_all_callers (struct cgraph_node *node, void *data)
2212 hash_set<cgraph_node *> callers;
2213 bool res = inline_to_all_callers_1 (node, data, &callers);
2214 /* Perform the delayed update of the overall summary of all callers
2215 processed. This avoids quadratic behavior in the cases where
2216 we have a lot of calls to the same function. */
2217 for (hash_set<cgraph_node *>::iterator i = callers.begin ();
2218 i != callers.end (); ++i)
2219 ipa_update_overall_fn_summary (*i);
2220 return res;
2223 /* Output overall time estimate. */
2224 static void
2225 dump_overall_stats (void)
2227 sreal sum_weighted = 0, sum = 0;
2228 struct cgraph_node *node;
2230 FOR_EACH_DEFINED_FUNCTION (node)
2231 if (!node->global.inlined_to
2232 && !node->alias)
2234 sreal time = ipa_fn_summaries->get (node)->time;
2235 sum += time;
2236 sum_weighted += time * node->count;
2238 fprintf (dump_file, "Overall time estimate: "
2239 "%f weighted by profile: "
2240 "%f\n", sum.to_double (), sum_weighted.to_double ());
2243 /* Output some useful stats about inlining. */
2245 static void
2246 dump_inline_stats (void)
2248 int64_t inlined_cnt = 0, inlined_indir_cnt = 0;
2249 int64_t inlined_virt_cnt = 0, inlined_virt_indir_cnt = 0;
2250 int64_t noninlined_cnt = 0, noninlined_indir_cnt = 0;
2251 int64_t noninlined_virt_cnt = 0, noninlined_virt_indir_cnt = 0;
2252 int64_t inlined_speculative = 0, inlined_speculative_ply = 0;
2253 int64_t indirect_poly_cnt = 0, indirect_cnt = 0;
2254 int64_t reason[CIF_N_REASONS][3];
2255 int i;
2256 struct cgraph_node *node;
2258 memset (reason, 0, sizeof (reason));
2259 FOR_EACH_DEFINED_FUNCTION (node)
2261 struct cgraph_edge *e;
2262 for (e = node->callees; e; e = e->next_callee)
2264 if (e->inline_failed)
2266 reason[(int) e->inline_failed][0] += e->count;
2267 reason[(int) e->inline_failed][1] += e->frequency;
2268 reason[(int) e->inline_failed][2] ++;
2269 if (DECL_VIRTUAL_P (e->callee->decl))
2271 if (e->indirect_inlining_edge)
2272 noninlined_virt_indir_cnt += e->count;
2273 else
2274 noninlined_virt_cnt += e->count;
2276 else
2278 if (e->indirect_inlining_edge)
2279 noninlined_indir_cnt += e->count;
2280 else
2281 noninlined_cnt += e->count;
2284 else
2286 if (e->speculative)
2288 if (DECL_VIRTUAL_P (e->callee->decl))
2289 inlined_speculative_ply += e->count;
2290 else
2291 inlined_speculative += e->count;
2293 else if (DECL_VIRTUAL_P (e->callee->decl))
2295 if (e->indirect_inlining_edge)
2296 inlined_virt_indir_cnt += e->count;
2297 else
2298 inlined_virt_cnt += e->count;
2300 else
2302 if (e->indirect_inlining_edge)
2303 inlined_indir_cnt += e->count;
2304 else
2305 inlined_cnt += e->count;
2309 for (e = node->indirect_calls; e; e = e->next_callee)
2310 if (e->indirect_info->polymorphic)
2311 indirect_poly_cnt += e->count;
2312 else
2313 indirect_cnt += e->count;
2315 if (max_count)
2317 fprintf (dump_file,
2318 "Inlined %" PRId64 " + speculative "
2319 "%" PRId64 " + speculative polymorphic "
2320 "%" PRId64 " + previously indirect "
2321 "%" PRId64 " + virtual "
2322 "%" PRId64 " + virtual and previously indirect "
2323 "%" PRId64 "\n" "Not inlined "
2324 "%" PRId64 " + previously indirect "
2325 "%" PRId64 " + virtual "
2326 "%" PRId64 " + virtual and previously indirect "
2327 "%" PRId64 " + stil indirect "
2328 "%" PRId64 " + still indirect polymorphic "
2329 "%" PRId64 "\n", inlined_cnt,
2330 inlined_speculative, inlined_speculative_ply,
2331 inlined_indir_cnt, inlined_virt_cnt, inlined_virt_indir_cnt,
2332 noninlined_cnt, noninlined_indir_cnt, noninlined_virt_cnt,
2333 noninlined_virt_indir_cnt, indirect_cnt, indirect_poly_cnt);
2334 fprintf (dump_file,
2335 "Removed speculations %" PRId64 "\n",
2336 spec_rem);
2338 dump_overall_stats ();
2339 fprintf (dump_file, "\nWhy inlining failed?\n");
2340 for (i = 0; i < CIF_N_REASONS; i++)
2341 if (reason[i][2])
2342 fprintf (dump_file, "%-50s: %8i calls, %8i freq, %" PRId64" count\n",
2343 cgraph_inline_failed_string ((cgraph_inline_failed_t) i),
2344 (int) reason[i][2], (int) reason[i][1], reason[i][0]);
2347 /* Decide on the inlining. We do so in the topological order to avoid
2348 expenses on updating data structures. */
2350 static unsigned int
2351 ipa_inline (void)
2353 struct cgraph_node *node;
2354 int nnodes;
2355 struct cgraph_node **order;
2356 int i;
2357 int cold;
2358 bool remove_functions = false;
2360 if (!optimize)
2361 return 0;
2363 cgraph_freq_base_rec = (sreal) 1 / (sreal) CGRAPH_FREQ_BASE;
2364 percent_rec = (sreal) 1 / (sreal) 100;
2366 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2368 if (dump_file)
2369 ipa_dump_fn_summaries (dump_file);
2371 nnodes = ipa_reverse_postorder (order);
2373 FOR_EACH_FUNCTION (node)
2375 node->aux = 0;
2377 /* Recompute the default reasons for inlining because they may have
2378 changed during merging. */
2379 if (in_lto_p)
2381 for (cgraph_edge *e = node->callees; e; e = e->next_callee)
2383 gcc_assert (e->inline_failed);
2384 initialize_inline_failed (e);
2386 for (cgraph_edge *e = node->indirect_calls; e; e = e->next_callee)
2387 initialize_inline_failed (e);
2391 if (dump_file)
2392 fprintf (dump_file, "\nFlattening functions:\n");
2394 /* In the first pass handle functions to be flattened. Do this with
2395 a priority so none of our later choices will make this impossible. */
2396 for (i = nnodes - 1; i >= 0; i--)
2398 node = order[i];
2400 /* Handle nodes to be flattened.
2401 Ideally when processing callees we stop inlining at the
2402 entry of cycles, possibly cloning that entry point and
2403 try to flatten itself turning it into a self-recursive
2404 function. */
2405 if (lookup_attribute ("flatten",
2406 DECL_ATTRIBUTES (node->decl)) != NULL)
2408 if (dump_file)
2409 fprintf (dump_file,
2410 "Flattening %s\n", node->name ());
2411 flatten_function (node, false);
2414 if (dump_file)
2415 dump_overall_stats ();
2417 inline_small_functions ();
2419 gcc_assert (symtab->state == IPA_SSA);
2420 symtab->state = IPA_SSA_AFTER_INLINING;
2421 /* Do first after-inlining removal. We want to remove all "stale" extern
2422 inline functions and virtual functions so we really know what is called
2423 once. */
2424 symtab->remove_unreachable_nodes (dump_file);
2425 free (order);
2427 /* Inline functions with a property that after inlining into all callers the
2428 code size will shrink because the out-of-line copy is eliminated.
2429 We do this regardless on the callee size as long as function growth limits
2430 are met. */
2431 if (dump_file)
2432 fprintf (dump_file,
2433 "\nDeciding on functions to be inlined into all callers and "
2434 "removing useless speculations:\n");
2436 /* Inlining one function called once has good chance of preventing
2437 inlining other function into the same callee. Ideally we should
2438 work in priority order, but probably inlining hot functions first
2439 is good cut without the extra pain of maintaining the queue.
2441 ??? this is not really fitting the bill perfectly: inlining function
2442 into callee often leads to better optimization of callee due to
2443 increased context for optimization.
2444 For example if main() function calls a function that outputs help
2445 and then function that does the main optmization, we should inline
2446 the second with priority even if both calls are cold by themselves.
2448 We probably want to implement new predicate replacing our use of
2449 maybe_hot_edge interpreted as maybe_hot_edge || callee is known
2450 to be hot. */
2451 for (cold = 0; cold <= 1; cold ++)
2453 FOR_EACH_DEFINED_FUNCTION (node)
2455 struct cgraph_edge *edge, *next;
2456 bool update=false;
2458 for (edge = node->callees; edge; edge = next)
2460 next = edge->next_callee;
2461 if (edge->speculative && !speculation_useful_p (edge, false))
2463 edge->resolve_speculation ();
2464 spec_rem += edge->count;
2465 update = true;
2466 remove_functions = true;
2469 if (update)
2471 struct cgraph_node *where = node->global.inlined_to
2472 ? node->global.inlined_to : node;
2473 reset_edge_caches (where);
2474 ipa_update_overall_fn_summary (where);
2476 if (want_inline_function_to_all_callers_p (node, cold))
2478 int num_calls = 0;
2479 node->call_for_symbol_and_aliases (sum_callers, &num_calls,
2480 true);
2481 while (node->call_for_symbol_and_aliases
2482 (inline_to_all_callers, &num_calls, true))
2484 remove_functions = true;
2489 /* Free ipa-prop structures if they are no longer needed. */
2490 if (optimize)
2491 ipa_free_all_structures_after_iinln ();
2493 if (dump_file)
2495 fprintf (dump_file,
2496 "\nInlined %i calls, eliminated %i functions\n\n",
2497 ncalls_inlined, nfunctions_inlined);
2498 dump_inline_stats ();
2501 if (dump_file)
2502 ipa_dump_fn_summaries (dump_file);
2503 /* In WPA we use inline summaries for partitioning process. */
2504 if (!flag_wpa)
2505 ipa_free_fn_summary ();
2506 return remove_functions ? TODO_remove_functions : 0;
2509 /* Inline always-inline function calls in NODE. */
2511 static bool
2512 inline_always_inline_functions (struct cgraph_node *node)
2514 struct cgraph_edge *e;
2515 bool inlined = false;
2517 for (e = node->callees; e; e = e->next_callee)
2519 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
2520 if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl))
2521 continue;
2523 if (e->recursive_p ())
2525 if (dump_file)
2526 fprintf (dump_file, " Not inlining recursive call to %s.\n",
2527 e->callee->name ());
2528 e->inline_failed = CIF_RECURSIVE_INLINING;
2529 continue;
2532 if (!can_early_inline_edge_p (e))
2534 /* Set inlined to true if the callee is marked "always_inline" but
2535 is not inlinable. This will allow flagging an error later in
2536 expand_call_inline in tree-inline.c. */
2537 if (lookup_attribute ("always_inline",
2538 DECL_ATTRIBUTES (callee->decl)) != NULL)
2539 inlined = true;
2540 continue;
2543 if (dump_file)
2544 fprintf (dump_file, " Inlining %s into %s (always_inline).\n",
2545 xstrdup_for_dump (e->callee->name ()),
2546 xstrdup_for_dump (e->caller->name ()));
2547 inline_call (e, true, NULL, NULL, false);
2548 inlined = true;
2550 if (inlined)
2551 ipa_update_overall_fn_summary (node);
2553 return inlined;
2556 /* Decide on the inlining. We do so in the topological order to avoid
2557 expenses on updating data structures. */
2559 static bool
2560 early_inline_small_functions (struct cgraph_node *node)
2562 struct cgraph_edge *e;
2563 bool inlined = false;
2565 for (e = node->callees; e; e = e->next_callee)
2567 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
2568 if (!ipa_fn_summaries->get (callee)->inlinable
2569 || !e->inline_failed)
2570 continue;
2572 /* Do not consider functions not declared inline. */
2573 if (!DECL_DECLARED_INLINE_P (callee->decl)
2574 && !opt_for_fn (node->decl, flag_inline_small_functions)
2575 && !opt_for_fn (node->decl, flag_inline_functions))
2576 continue;
2578 if (dump_file)
2579 fprintf (dump_file, "Considering inline candidate %s.\n",
2580 callee->name ());
2582 if (!can_early_inline_edge_p (e))
2583 continue;
2585 if (e->recursive_p ())
2587 if (dump_file)
2588 fprintf (dump_file, " Not inlining: recursive call.\n");
2589 continue;
2592 if (!want_early_inline_function_p (e))
2593 continue;
2595 if (dump_file)
2596 fprintf (dump_file, " Inlining %s into %s.\n",
2597 xstrdup_for_dump (callee->name ()),
2598 xstrdup_for_dump (e->caller->name ()));
2599 inline_call (e, true, NULL, NULL, false);
2600 inlined = true;
2603 if (inlined)
2604 ipa_update_overall_fn_summary (node);
2606 return inlined;
2609 unsigned int
2610 early_inliner (function *fun)
2612 struct cgraph_node *node = cgraph_node::get (current_function_decl);
2613 struct cgraph_edge *edge;
2614 unsigned int todo = 0;
2615 int iterations = 0;
2616 bool inlined = false;
2618 if (seen_error ())
2619 return 0;
2621 /* Do nothing if datastructures for ipa-inliner are already computed. This
2622 happens when some pass decides to construct new function and
2623 cgraph_add_new_function calls lowering passes and early optimization on
2624 it. This may confuse ourself when early inliner decide to inline call to
2625 function clone, because function clones don't have parameter list in
2626 ipa-prop matching their signature. */
2627 if (ipa_node_params_sum)
2628 return 0;
2630 if (flag_checking)
2631 node->verify ();
2632 node->remove_all_references ();
2634 /* Rebuild this reference because it dosn't depend on
2635 function's body and it's required to pass cgraph_node
2636 verification. */
2637 if (node->instrumented_version
2638 && !node->instrumentation_clone)
2639 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
2641 /* Even when not optimizing or not inlining inline always-inline
2642 functions. */
2643 inlined = inline_always_inline_functions (node);
2645 if (!optimize
2646 || flag_no_inline
2647 || !flag_early_inlining
2648 /* Never inline regular functions into always-inline functions
2649 during incremental inlining. This sucks as functions calling
2650 always inline functions will get less optimized, but at the
2651 same time inlining of functions calling always inline
2652 function into an always inline function might introduce
2653 cycles of edges to be always inlined in the callgraph.
2655 We might want to be smarter and just avoid this type of inlining. */
2656 || (DECL_DISREGARD_INLINE_LIMITS (node->decl)
2657 && lookup_attribute ("always_inline",
2658 DECL_ATTRIBUTES (node->decl))))
2660 else if (lookup_attribute ("flatten",
2661 DECL_ATTRIBUTES (node->decl)) != NULL)
2663 /* When the function is marked to be flattened, recursively inline
2664 all calls in it. */
2665 if (dump_file)
2666 fprintf (dump_file,
2667 "Flattening %s\n", node->name ());
2668 flatten_function (node, true);
2669 inlined = true;
2671 else
2673 /* If some always_inline functions was inlined, apply the changes.
2674 This way we will not account always inline into growth limits and
2675 moreover we will inline calls from always inlines that we skipped
2676 previously because of conditional above. */
2677 if (inlined)
2679 timevar_push (TV_INTEGRATION);
2680 todo |= optimize_inline_calls (current_function_decl);
2681 /* optimize_inline_calls call above might have introduced new
2682 statements that don't have inline parameters computed. */
2683 for (edge = node->callees; edge; edge = edge->next_callee)
2685 struct ipa_call_summary *es = ipa_call_summaries->get (edge);
2686 es->call_stmt_size
2687 = estimate_num_insns (edge->call_stmt, &eni_size_weights);
2688 es->call_stmt_time
2689 = estimate_num_insns (edge->call_stmt, &eni_time_weights);
2691 ipa_update_overall_fn_summary (node);
2692 inlined = false;
2693 timevar_pop (TV_INTEGRATION);
2695 /* We iterate incremental inlining to get trivial cases of indirect
2696 inlining. */
2697 while (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS)
2698 && early_inline_small_functions (node))
2700 timevar_push (TV_INTEGRATION);
2701 todo |= optimize_inline_calls (current_function_decl);
2703 /* Technically we ought to recompute inline parameters so the new
2704 iteration of early inliner works as expected. We however have
2705 values approximately right and thus we only need to update edge
2706 info that might be cleared out for newly discovered edges. */
2707 for (edge = node->callees; edge; edge = edge->next_callee)
2709 /* We have no summary for new bound store calls yet. */
2710 struct ipa_call_summary *es = ipa_call_summaries->get (edge);
2711 es->call_stmt_size
2712 = estimate_num_insns (edge->call_stmt, &eni_size_weights);
2713 es->call_stmt_time
2714 = estimate_num_insns (edge->call_stmt, &eni_time_weights);
2716 if (edge->callee->decl
2717 && !gimple_check_call_matching_types (
2718 edge->call_stmt, edge->callee->decl, false))
2720 edge->inline_failed = CIF_MISMATCHED_ARGUMENTS;
2721 edge->call_stmt_cannot_inline_p = true;
2724 if (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS) - 1)
2725 ipa_update_overall_fn_summary (node);
2726 timevar_pop (TV_INTEGRATION);
2727 iterations++;
2728 inlined = false;
2730 if (dump_file)
2731 fprintf (dump_file, "Iterations: %i\n", iterations);
2734 if (inlined)
2736 timevar_push (TV_INTEGRATION);
2737 todo |= optimize_inline_calls (current_function_decl);
2738 timevar_pop (TV_INTEGRATION);
2741 fun->always_inline_functions_inlined = true;
2743 return todo;
2746 /* Do inlining of small functions. Doing so early helps profiling and other
2747 passes to be somewhat more effective and avoids some code duplication in
2748 later real inlining pass for testcases with very many function calls. */
2750 namespace {
2752 const pass_data pass_data_early_inline =
2754 GIMPLE_PASS, /* type */
2755 "einline", /* name */
2756 OPTGROUP_INLINE, /* optinfo_flags */
2757 TV_EARLY_INLINING, /* tv_id */
2758 PROP_ssa, /* properties_required */
2759 0, /* properties_provided */
2760 0, /* properties_destroyed */
2761 0, /* todo_flags_start */
2762 0, /* todo_flags_finish */
2765 class pass_early_inline : public gimple_opt_pass
2767 public:
2768 pass_early_inline (gcc::context *ctxt)
2769 : gimple_opt_pass (pass_data_early_inline, ctxt)
2772 /* opt_pass methods: */
2773 virtual unsigned int execute (function *);
2775 }; // class pass_early_inline
2777 unsigned int
2778 pass_early_inline::execute (function *fun)
2780 return early_inliner (fun);
2783 } // anon namespace
2785 gimple_opt_pass *
2786 make_pass_early_inline (gcc::context *ctxt)
2788 return new pass_early_inline (ctxt);
2791 namespace {
2793 const pass_data pass_data_ipa_inline =
2795 IPA_PASS, /* type */
2796 "inline", /* name */
2797 OPTGROUP_INLINE, /* optinfo_flags */
2798 TV_IPA_INLINING, /* tv_id */
2799 0, /* properties_required */
2800 0, /* properties_provided */
2801 0, /* properties_destroyed */
2802 0, /* todo_flags_start */
2803 ( TODO_dump_symtab ), /* todo_flags_finish */
2806 class pass_ipa_inline : public ipa_opt_pass_d
2808 public:
2809 pass_ipa_inline (gcc::context *ctxt)
2810 : ipa_opt_pass_d (pass_data_ipa_inline, ctxt,
2811 NULL, /* generate_summary */
2812 NULL, /* write_summary */
2813 NULL, /* read_summary */
2814 NULL, /* write_optimization_summary */
2815 NULL, /* read_optimization_summary */
2816 NULL, /* stmt_fixup */
2817 0, /* function_transform_todo_flags_start */
2818 inline_transform, /* function_transform */
2819 NULL) /* variable_transform */
2822 /* opt_pass methods: */
2823 virtual unsigned int execute (function *) { return ipa_inline (); }
2825 }; // class pass_ipa_inline
2827 } // anon namespace
2829 ipa_opt_pass_d *
2830 make_pass_ipa_inline (gcc::context *ctxt)
2832 return new pass_ipa_inline (ctxt);