re PR other/63387 (Optimize pairs of isnan() calls into a single isunordered())
[official-gcc.git] / gcc / ipa-inline.c
blob4533ea46be7588ee72fcda70b40bfaff60d3f606
1 /* Inlining decision heuristics.
2 Copyright (C) 2003-2015 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 "tm.h"
96 #include "hash-set.h"
97 #include "machmode.h"
98 #include "vec.h"
99 #include "double-int.h"
100 #include "input.h"
101 #include "alias.h"
102 #include "symtab.h"
103 #include "wide-int.h"
104 #include "inchash.h"
105 #include "tree.h"
106 #include "fold-const.h"
107 #include "trans-mem.h"
108 #include "calls.h"
109 #include "tree-inline.h"
110 #include "langhooks.h"
111 #include "flags.h"
112 #include "diagnostic.h"
113 #include "gimple-pretty-print.h"
114 #include "params.h"
115 #include "intl.h"
116 #include "tree-pass.h"
117 #include "coverage.h"
118 #include "rtl.h"
119 #include "bitmap.h"
120 #include "profile.h"
121 #include "predict.h"
122 #include "hard-reg-set.h"
123 #include "input.h"
124 #include "function.h"
125 #include "basic-block.h"
126 #include "tree-ssa-alias.h"
127 #include "internal-fn.h"
128 #include "gimple-expr.h"
129 #include "is-a.h"
130 #include "gimple.h"
131 #include "gimple-ssa.h"
132 #include "hash-map.h"
133 #include "plugin-api.h"
134 #include "ipa-ref.h"
135 #include "cgraph.h"
136 #include "alloc-pool.h"
137 #include "symbol-summary.h"
138 #include "ipa-prop.h"
139 #include "except.h"
140 #include "target.h"
141 #include "ipa-inline.h"
142 #include "ipa-utils.h"
143 #include "sreal.h"
144 #include "auto-profile.h"
145 #include "builtins.h"
146 #include "fibonacci_heap.h"
147 #include "lto-streamer.h"
149 typedef fibonacci_heap <sreal, cgraph_edge> edge_heap_t;
150 typedef fibonacci_node <sreal, cgraph_edge> edge_heap_node_t;
152 /* Statistics we collect about inlining algorithm. */
153 static int overall_size;
154 static gcov_type max_count;
155 static gcov_type spec_rem;
157 /* Pre-computed constants 1/CGRAPH_FREQ_BASE and 1/100. */
158 static sreal cgraph_freq_base_rec, percent_rec;
160 /* Return false when inlining edge E would lead to violating
161 limits on function unit growth or stack usage growth.
163 The relative function body growth limit is present generally
164 to avoid problems with non-linear behavior of the compiler.
165 To allow inlining huge functions into tiny wrapper, the limit
166 is always based on the bigger of the two functions considered.
168 For stack growth limits we always base the growth in stack usage
169 of the callers. We want to prevent applications from segfaulting
170 on stack overflow when functions with huge stack frames gets
171 inlined. */
173 static bool
174 caller_growth_limits (struct cgraph_edge *e)
176 struct cgraph_node *to = e->caller;
177 struct cgraph_node *what = e->callee->ultimate_alias_target ();
178 int newsize;
179 int limit = 0;
180 HOST_WIDE_INT stack_size_limit = 0, inlined_stack;
181 inline_summary *info, *what_info, *outer_info = inline_summaries->get (to);
183 /* Look for function e->caller is inlined to. While doing
184 so work out the largest function body on the way. As
185 described above, we want to base our function growth
186 limits based on that. Not on the self size of the
187 outer function, not on the self size of inline code
188 we immediately inline to. This is the most relaxed
189 interpretation of the rule "do not grow large functions
190 too much in order to prevent compiler from exploding". */
191 while (true)
193 info = inline_summaries->get (to);
194 if (limit < info->self_size)
195 limit = info->self_size;
196 if (stack_size_limit < info->estimated_self_stack_size)
197 stack_size_limit = info->estimated_self_stack_size;
198 if (to->global.inlined_to)
199 to = to->callers->caller;
200 else
201 break;
204 what_info = inline_summaries->get (what);
206 if (limit < what_info->self_size)
207 limit = what_info->self_size;
209 limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
211 /* Check the size after inlining against the function limits. But allow
212 the function to shrink if it went over the limits by forced inlining. */
213 newsize = estimate_size_after_inlining (to, e);
214 if (newsize >= info->size
215 && newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
216 && newsize > limit)
218 e->inline_failed = CIF_LARGE_FUNCTION_GROWTH_LIMIT;
219 return false;
222 if (!what_info->estimated_stack_size)
223 return true;
225 /* FIXME: Stack size limit often prevents inlining in Fortran programs
226 due to large i/o datastructures used by the Fortran front-end.
227 We ought to ignore this limit when we know that the edge is executed
228 on every invocation of the caller (i.e. its call statement dominates
229 exit block). We do not track this information, yet. */
230 stack_size_limit += ((gcov_type)stack_size_limit
231 * PARAM_VALUE (PARAM_STACK_FRAME_GROWTH) / 100);
233 inlined_stack = (outer_info->stack_frame_offset
234 + outer_info->estimated_self_stack_size
235 + what_info->estimated_stack_size);
236 /* Check new stack consumption with stack consumption at the place
237 stack is used. */
238 if (inlined_stack > stack_size_limit
239 /* If function already has large stack usage from sibling
240 inline call, we can inline, too.
241 This bit overoptimistically assume that we are good at stack
242 packing. */
243 && inlined_stack > info->estimated_stack_size
244 && inlined_stack > PARAM_VALUE (PARAM_LARGE_STACK_FRAME))
246 e->inline_failed = CIF_LARGE_STACK_FRAME_GROWTH_LIMIT;
247 return false;
249 return true;
252 /* Dump info about why inlining has failed. */
254 static void
255 report_inline_failed_reason (struct cgraph_edge *e)
257 if (dump_file)
259 fprintf (dump_file, " not inlinable: %s/%i -> %s/%i, %s\n",
260 xstrdup_for_dump (e->caller->name ()), e->caller->order,
261 xstrdup_for_dump (e->callee->name ()), e->callee->order,
262 cgraph_inline_failed_string (e->inline_failed));
263 if ((e->inline_failed == CIF_TARGET_OPTION_MISMATCH
264 || e->inline_failed == CIF_OPTIMIZATION_MISMATCH)
265 && e->caller->lto_file_data
266 && e->callee->function_symbol ()->lto_file_data)
268 fprintf (dump_file, " LTO objects: %s, %s\n",
269 e->caller->lto_file_data->file_name,
270 e->callee->function_symbol ()->lto_file_data->file_name);
272 if (e->inline_failed == CIF_TARGET_OPTION_MISMATCH)
273 cl_target_option_print_diff
274 (dump_file, 2, target_opts_for_fn (e->caller->decl),
275 target_opts_for_fn (e->callee->ultimate_alias_target ()->decl));
276 if (e->inline_failed == CIF_OPTIMIZATION_MISMATCH)
277 cl_optimization_print_diff
278 (dump_file, 2, opts_for_fn (e->caller->decl),
279 opts_for_fn (e->callee->ultimate_alias_target ()->decl));
283 /* Decide whether sanitizer-related attributes allow inlining. */
285 static bool
286 sanitize_attrs_match_for_inline_p (const_tree caller, const_tree callee)
288 /* Don't care if sanitizer is disabled */
289 if (!(flag_sanitize & SANITIZE_ADDRESS))
290 return true;
292 if (!caller || !callee)
293 return true;
295 return !!lookup_attribute ("no_sanitize_address",
296 DECL_ATTRIBUTES (caller)) ==
297 !!lookup_attribute ("no_sanitize_address",
298 DECL_ATTRIBUTES (callee));
301 /* Used for flags where it is safe to inline when caller's value is
302 grater than callee's. */
303 #define check_maybe_up(flag) \
304 (opts_for_fn (caller->decl)->x_##flag \
305 != opts_for_fn (callee->decl)->x_##flag \
306 && (!always_inline \
307 || opts_for_fn (caller->decl)->x_##flag \
308 < opts_for_fn (callee->decl)->x_##flag))
309 /* Used for flags where it is safe to inline when caller's value is
310 smaller than callee's. */
311 #define check_maybe_down(flag) \
312 (opts_for_fn (caller->decl)->x_##flag \
313 != opts_for_fn (callee->decl)->x_##flag \
314 && (!always_inline \
315 || opts_for_fn (caller->decl)->x_##flag \
316 > opts_for_fn (callee->decl)->x_##flag))
317 /* Used for flags where exact match is needed for correctness. */
318 #define check_match(flag) \
319 (opts_for_fn (caller->decl)->x_##flag \
320 != opts_for_fn (callee->decl)->x_##flag)
322 /* Decide if we can inline the edge and possibly update
323 inline_failed reason.
324 We check whether inlining is possible at all and whether
325 caller growth limits allow doing so.
327 if REPORT is true, output reason to the dump file.
329 if DISREGARD_LIMITS is true, ignore size limits.*/
331 static bool
332 can_inline_edge_p (struct cgraph_edge *e, bool report,
333 bool disregard_limits = false, bool early = false)
335 gcc_checking_assert (e->inline_failed);
337 if (cgraph_inline_failed_type (e->inline_failed) == CIF_FINAL_ERROR)
339 if (report)
340 report_inline_failed_reason (e);
341 return false;
344 bool inlinable = true;
345 enum availability avail;
346 cgraph_node *callee = e->callee->ultimate_alias_target (&avail);
347 cgraph_node *caller = e->caller->global.inlined_to
348 ? e->caller->global.inlined_to : e->caller;
349 tree caller_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (caller->decl);
350 tree callee_tree
351 = callee ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (callee->decl) : NULL;
353 if (!callee->definition)
355 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
356 inlinable = false;
358 else if (callee->calls_comdat_local)
360 e->inline_failed = CIF_USES_COMDAT_LOCAL;
361 inlinable = false;
363 else if (avail <= AVAIL_INTERPOSABLE)
365 e->inline_failed = CIF_OVERWRITABLE;
366 inlinable = false;
368 else if (e->call_stmt_cannot_inline_p)
370 if (e->inline_failed != CIF_FUNCTION_NOT_OPTIMIZED)
371 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
372 inlinable = false;
374 /* Don't inline if the functions have different EH personalities. */
375 else if (DECL_FUNCTION_PERSONALITY (caller->decl)
376 && DECL_FUNCTION_PERSONALITY (callee->decl)
377 && (DECL_FUNCTION_PERSONALITY (caller->decl)
378 != DECL_FUNCTION_PERSONALITY (callee->decl)))
380 e->inline_failed = CIF_EH_PERSONALITY;
381 inlinable = false;
383 /* TM pure functions should not be inlined into non-TM_pure
384 functions. */
385 else if (is_tm_pure (callee->decl) && !is_tm_pure (caller->decl))
387 e->inline_failed = CIF_UNSPECIFIED;
388 inlinable = false;
390 /* Check compatibility of target optimization options. */
391 else if (!targetm.target_option.can_inline_p (caller->decl,
392 callee->decl))
394 e->inline_failed = CIF_TARGET_OPTION_MISMATCH;
395 inlinable = false;
397 else if (!inline_summaries->get (callee)->inlinable)
399 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
400 inlinable = false;
402 else if (inline_summaries->get (caller)->contains_cilk_spawn)
404 e->inline_failed = CIF_CILK_SPAWN;
405 inlinable = false;
407 /* Don't inline a function with mismatched sanitization attributes. */
408 else if (!sanitize_attrs_match_for_inline_p (caller->decl, callee->decl))
410 e->inline_failed = CIF_ATTRIBUTE_MISMATCH;
411 inlinable = false;
413 /* Check if caller growth allows the inlining. */
414 else if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl)
415 && !disregard_limits
416 && !lookup_attribute ("flatten",
417 DECL_ATTRIBUTES (caller->decl))
418 && !caller_growth_limits (e))
419 inlinable = false;
420 /* Don't inline a function with a higher optimization level than the
421 caller. FIXME: this is really just tip of iceberg of handling
422 optimization attribute. */
423 else if (caller_tree != callee_tree)
425 bool always_inline =
426 (DECL_DISREGARD_INLINE_LIMITS (callee->decl)
427 && lookup_attribute ("always_inline",
428 DECL_ATTRIBUTES (callee->decl)));
430 /* There are some options that change IL semantics which means
431 we cannot inline in these cases for correctness reason.
432 Not even for always_inline declared functions. */
433 /* Strictly speaking only when the callee contains signed integer
434 math where overflow is undefined. */
435 if ((check_maybe_up (flag_strict_overflow)
436 /* this flag is set by optimize. Allow inlining across
437 optimize boundary. */
438 && (!opt_for_fn (caller->decl, optimize)
439 == !opt_for_fn (callee->decl, optimize) || !always_inline))
440 || check_match (flag_wrapv)
441 || check_match (flag_trapv)
442 /* Strictly speaking only when the callee contains memory
443 accesses that are not using alias-set zero anyway. */
444 || check_maybe_down (flag_strict_aliasing)
445 /* Strictly speaking only when the callee uses FP math. */
446 || check_maybe_up (flag_rounding_math)
447 || check_maybe_up (flag_trapping_math)
448 || check_maybe_down (flag_unsafe_math_optimizations)
449 || check_maybe_down (flag_finite_math_only)
450 || check_maybe_up (flag_signaling_nans)
451 || check_maybe_down (flag_cx_limited_range)
452 || check_maybe_up (flag_signed_zeros)
453 || check_maybe_down (flag_associative_math)
454 || check_maybe_down (flag_reciprocal_math)
455 /* We do not want to make code compiled with exceptions to be brought
456 into a non-EH function unless we know that the callee does not
457 throw. This is tracked by DECL_FUNCTION_PERSONALITY. */
458 || (check_match (flag_non_call_exceptions)
459 /* TODO: We also may allow bringing !flag_non_call_exceptions
460 to flag_non_call_exceptions function, but that may need
461 extra work in tree-inline to add the extra EH edges. */
462 && (!opt_for_fn (callee->decl, flag_non_call_exceptions)
463 || DECL_FUNCTION_PERSONALITY (callee->decl)))
464 || (check_maybe_up (flag_exceptions)
465 && DECL_FUNCTION_PERSONALITY (callee->decl))
466 /* Strictly speaking only when the callee contains function
467 calls that may end up setting errno. */
468 || check_maybe_up (flag_errno_math)
469 /* When devirtualization is diabled for callee, it is not safe
470 to inline it as we possibly mangled the type info.
471 Allow early inlining of always inlines. */
472 || (!early && check_maybe_down (flag_devirtualize)))
474 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
475 inlinable = false;
477 /* gcc.dg/pr43564.c. Apply user-forced inline even at -O0. */
478 else if (always_inline)
480 /* When user added an attribute to the callee honor it. */
481 else if (lookup_attribute ("optimize", DECL_ATTRIBUTES (callee->decl))
482 && opts_for_fn (caller->decl) != opts_for_fn (callee->decl))
484 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
485 inlinable = false;
487 /* If mismatch is caused by merging two LTO units with different
488 optimizationflags we want to be bit nicer. However never inline
489 if one of functions is not optimized at all. */
490 else if (!opt_for_fn (callee->decl, optimize)
491 || !opt_for_fn (caller->decl, optimize))
493 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
494 inlinable = false;
496 /* If callee is optimized for size and caller is not, allow inlining if
497 code shrinks or we are in MAX_INLINE_INSNS_SINGLE limit and callee
498 is inline (and thus likely an unified comdat). This will allow caller
499 to run faster. */
500 else if (opt_for_fn (callee->decl, optimize_size)
501 > opt_for_fn (caller->decl, optimize_size))
503 int growth = estimate_edge_growth (e);
504 if (growth > 0
505 && (!DECL_DECLARED_INLINE_P (callee->decl)
506 && growth >= MAX (MAX_INLINE_INSNS_SINGLE,
507 MAX_INLINE_INSNS_AUTO)))
509 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
510 inlinable = false;
513 /* If callee is more aggressively optimized for performance than caller,
514 we generally want to inline only cheap (runtime wise) functions. */
515 else if (opt_for_fn (callee->decl, optimize_size)
516 < opt_for_fn (caller->decl, optimize_size)
517 || (opt_for_fn (callee->decl, optimize)
518 >= opt_for_fn (caller->decl, optimize)))
520 if (estimate_edge_time (e)
521 >= 20 + inline_edge_summary (e)->call_stmt_time)
523 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
524 inlinable = false;
530 if (!inlinable && report)
531 report_inline_failed_reason (e);
532 return inlinable;
536 /* Return true if the edge E is inlinable during early inlining. */
538 static bool
539 can_early_inline_edge_p (struct cgraph_edge *e)
541 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
542 /* Early inliner might get called at WPA stage when IPA pass adds new
543 function. In this case we can not really do any of early inlining
544 because function bodies are missing. */
545 if (!gimple_has_body_p (callee->decl))
547 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
548 return false;
550 /* In early inliner some of callees may not be in SSA form yet
551 (i.e. the callgraph is cyclic and we did not process
552 the callee by early inliner, yet). We don't have CIF code for this
553 case; later we will re-do the decision in the real inliner. */
554 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->caller->decl))
555 || !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
557 if (dump_file)
558 fprintf (dump_file, " edge not inlinable: not in SSA form\n");
559 return false;
561 if (!can_inline_edge_p (e, true, false, true))
562 return false;
563 return true;
567 /* Return number of calls in N. Ignore cheap builtins. */
569 static int
570 num_calls (struct cgraph_node *n)
572 struct cgraph_edge *e;
573 int num = 0;
575 for (e = n->callees; e; e = e->next_callee)
576 if (!is_inexpensive_builtin (e->callee->decl))
577 num++;
578 return num;
582 /* Return true if we are interested in inlining small function. */
584 static bool
585 want_early_inline_function_p (struct cgraph_edge *e)
587 bool want_inline = true;
588 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
590 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
592 /* For AutoFDO, we need to make sure that before profile summary, all
593 hot paths' IR look exactly the same as profiled binary. As a result,
594 in einliner, we will disregard size limit and inline those callsites
595 that are:
596 * inlined in the profiled binary, and
597 * the cloned callee has enough samples to be considered "hot". */
598 else if (flag_auto_profile && afdo_callsite_hot_enough_for_early_inline (e))
600 else if (!DECL_DECLARED_INLINE_P (callee->decl)
601 && !opt_for_fn (e->caller->decl, flag_inline_small_functions))
603 e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
604 report_inline_failed_reason (e);
605 want_inline = false;
607 else
609 int growth = estimate_edge_growth (e);
610 int n;
612 if (growth <= 0)
614 else if (!e->maybe_hot_p ()
615 && growth > 0)
617 if (dump_file)
618 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
619 "call is cold and code would grow by %i\n",
620 xstrdup_for_dump (e->caller->name ()),
621 e->caller->order,
622 xstrdup_for_dump (callee->name ()), callee->order,
623 growth);
624 want_inline = false;
626 else if (growth > PARAM_VALUE (PARAM_EARLY_INLINING_INSNS))
628 if (dump_file)
629 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
630 "growth %i exceeds --param early-inlining-insns\n",
631 xstrdup_for_dump (e->caller->name ()),
632 e->caller->order,
633 xstrdup_for_dump (callee->name ()), callee->order,
634 growth);
635 want_inline = false;
637 else if ((n = num_calls (callee)) != 0
638 && growth * (n + 1) > PARAM_VALUE (PARAM_EARLY_INLINING_INSNS))
640 if (dump_file)
641 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
642 "growth %i exceeds --param early-inlining-insns "
643 "divided by number of calls\n",
644 xstrdup_for_dump (e->caller->name ()),
645 e->caller->order,
646 xstrdup_for_dump (callee->name ()), callee->order,
647 growth);
648 want_inline = false;
651 return want_inline;
654 /* Compute time of the edge->caller + edge->callee execution when inlining
655 does not happen. */
657 inline sreal
658 compute_uninlined_call_time (struct inline_summary *callee_info,
659 struct cgraph_edge *edge)
661 sreal uninlined_call_time = (sreal)callee_info->time;
662 cgraph_node *caller = (edge->caller->global.inlined_to
663 ? edge->caller->global.inlined_to
664 : edge->caller);
666 if (edge->count && caller->count)
667 uninlined_call_time *= (sreal)edge->count / caller->count;
668 if (edge->frequency)
669 uninlined_call_time *= cgraph_freq_base_rec * edge->frequency;
670 else
671 uninlined_call_time = uninlined_call_time >> 11;
673 int caller_time = inline_summaries->get (caller)->time;
674 return uninlined_call_time + caller_time;
677 /* Same as compute_uinlined_call_time but compute time when inlining
678 does happen. */
680 inline sreal
681 compute_inlined_call_time (struct cgraph_edge *edge,
682 int edge_time)
684 cgraph_node *caller = (edge->caller->global.inlined_to
685 ? edge->caller->global.inlined_to
686 : edge->caller);
687 int caller_time = inline_summaries->get (caller)->time;
688 sreal time = edge_time;
690 if (edge->count && caller->count)
691 time *= (sreal)edge->count / caller->count;
692 if (edge->frequency)
693 time *= cgraph_freq_base_rec * edge->frequency;
694 else
695 time = time >> 11;
697 /* This calculation should match one in ipa-inline-analysis.
698 FIXME: Once ipa-inline-analysis is converted to sreal this can be
699 simplified. */
700 time -= (sreal) ((gcov_type) edge->frequency
701 * inline_edge_summary (edge)->call_stmt_time
702 * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE)) / INLINE_TIME_SCALE;
703 time += caller_time;
704 if (time <= 0)
705 time = ((sreal) 1) >> 8;
706 gcc_checking_assert (time >= 0);
707 return time;
710 /* Return true if the speedup for inlining E is bigger than
711 PARAM_MAX_INLINE_MIN_SPEEDUP. */
713 static bool
714 big_speedup_p (struct cgraph_edge *e)
716 sreal time = compute_uninlined_call_time (inline_summaries->get (e->callee),
718 sreal inlined_time = compute_inlined_call_time (e, estimate_edge_time (e));
720 if (time - inlined_time
721 > (sreal) time * PARAM_VALUE (PARAM_INLINE_MIN_SPEEDUP)
722 * percent_rec)
723 return true;
724 return false;
727 /* Return true if we are interested in inlining small function.
728 When REPORT is true, report reason to dump file. */
730 static bool
731 want_inline_small_function_p (struct cgraph_edge *e, bool report)
733 bool want_inline = true;
734 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
736 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
738 else if (!DECL_DECLARED_INLINE_P (callee->decl)
739 && !opt_for_fn (e->caller->decl, flag_inline_small_functions))
741 e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
742 want_inline = false;
744 /* Do fast and conservative check if the function can be good
745 inline candidate. At the moment we allow inline hints to
746 promote non-inline functions to inline and we increase
747 MAX_INLINE_INSNS_SINGLE 16-fold for inline functions. */
748 else if ((!DECL_DECLARED_INLINE_P (callee->decl)
749 && (!e->count || !e->maybe_hot_p ()))
750 && inline_summaries->get (callee)->min_size
751 - inline_edge_summary (e)->call_stmt_size
752 > MAX (MAX_INLINE_INSNS_SINGLE, MAX_INLINE_INSNS_AUTO))
754 e->inline_failed = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
755 want_inline = false;
757 else if ((DECL_DECLARED_INLINE_P (callee->decl) || e->count)
758 && inline_summaries->get (callee)->min_size
759 - inline_edge_summary (e)->call_stmt_size
760 > 16 * MAX_INLINE_INSNS_SINGLE)
762 e->inline_failed = (DECL_DECLARED_INLINE_P (callee->decl)
763 ? CIF_MAX_INLINE_INSNS_SINGLE_LIMIT
764 : CIF_MAX_INLINE_INSNS_AUTO_LIMIT);
765 want_inline = false;
767 else
769 int growth = estimate_edge_growth (e);
770 inline_hints hints = estimate_edge_hints (e);
771 bool big_speedup = big_speedup_p (e);
773 if (growth <= 0)
775 /* Apply MAX_INLINE_INSNS_SINGLE limit. Do not do so when
776 hints suggests that inlining given function is very profitable. */
777 else if (DECL_DECLARED_INLINE_P (callee->decl)
778 && growth >= MAX_INLINE_INSNS_SINGLE
779 && ((!big_speedup
780 && !(hints & (INLINE_HINT_indirect_call
781 | INLINE_HINT_known_hot
782 | INLINE_HINT_loop_iterations
783 | INLINE_HINT_array_index
784 | INLINE_HINT_loop_stride)))
785 || growth >= MAX_INLINE_INSNS_SINGLE * 16))
787 e->inline_failed = CIF_MAX_INLINE_INSNS_SINGLE_LIMIT;
788 want_inline = false;
790 else if (!DECL_DECLARED_INLINE_P (callee->decl)
791 && !opt_for_fn (e->caller->decl, flag_inline_functions))
793 /* growth_likely_positive is expensive, always test it last. */
794 if (growth >= MAX_INLINE_INSNS_SINGLE
795 || growth_likely_positive (callee, growth))
797 e->inline_failed = CIF_NOT_DECLARED_INLINED;
798 want_inline = false;
801 /* Apply MAX_INLINE_INSNS_AUTO limit for functions not declared inline
802 Upgrade it to MAX_INLINE_INSNS_SINGLE when hints suggests that
803 inlining given function is very profitable. */
804 else if (!DECL_DECLARED_INLINE_P (callee->decl)
805 && !big_speedup
806 && !(hints & INLINE_HINT_known_hot)
807 && growth >= ((hints & (INLINE_HINT_indirect_call
808 | INLINE_HINT_loop_iterations
809 | INLINE_HINT_array_index
810 | INLINE_HINT_loop_stride))
811 ? MAX (MAX_INLINE_INSNS_AUTO,
812 MAX_INLINE_INSNS_SINGLE)
813 : MAX_INLINE_INSNS_AUTO))
815 /* growth_likely_positive is expensive, always test it last. */
816 if (growth >= MAX_INLINE_INSNS_SINGLE
817 || growth_likely_positive (callee, growth))
819 e->inline_failed = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
820 want_inline = false;
823 /* If call is cold, do not inline when function body would grow. */
824 else if (!e->maybe_hot_p ()
825 && (growth >= MAX_INLINE_INSNS_SINGLE
826 || growth_likely_positive (callee, growth)))
828 e->inline_failed = CIF_UNLIKELY_CALL;
829 want_inline = false;
832 if (!want_inline && report)
833 report_inline_failed_reason (e);
834 return want_inline;
837 /* EDGE is self recursive edge.
838 We hand two cases - when function A is inlining into itself
839 or when function A is being inlined into another inliner copy of function
840 A within function B.
842 In first case OUTER_NODE points to the toplevel copy of A, while
843 in the second case OUTER_NODE points to the outermost copy of A in B.
845 In both cases we want to be extra selective since
846 inlining the call will just introduce new recursive calls to appear. */
848 static bool
849 want_inline_self_recursive_call_p (struct cgraph_edge *edge,
850 struct cgraph_node *outer_node,
851 bool peeling,
852 int depth)
854 char const *reason = NULL;
855 bool want_inline = true;
856 int caller_freq = CGRAPH_FREQ_BASE;
857 int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO);
859 if (DECL_DECLARED_INLINE_P (edge->caller->decl))
860 max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH);
862 if (!edge->maybe_hot_p ())
864 reason = "recursive call is cold";
865 want_inline = false;
867 else if (max_count && !outer_node->count)
869 reason = "not executed in profile";
870 want_inline = false;
872 else if (depth > max_depth)
874 reason = "--param max-inline-recursive-depth exceeded.";
875 want_inline = false;
878 if (outer_node->global.inlined_to)
879 caller_freq = outer_node->callers->frequency;
881 if (!caller_freq)
883 reason = "function is inlined and unlikely";
884 want_inline = false;
887 if (!want_inline)
889 /* Inlining of self recursive function into copy of itself within other function
890 is transformation similar to loop peeling.
892 Peeling is profitable if we can inline enough copies to make probability
893 of actual call to the self recursive function very small. Be sure that
894 the probability of recursion is small.
896 We ensure that the frequency of recursing is at most 1 - (1/max_depth).
897 This way the expected number of recision is at most max_depth. */
898 else if (peeling)
900 int max_prob = CGRAPH_FREQ_BASE - ((CGRAPH_FREQ_BASE + max_depth - 1)
901 / max_depth);
902 int i;
903 for (i = 1; i < depth; i++)
904 max_prob = max_prob * max_prob / CGRAPH_FREQ_BASE;
905 if (max_count
906 && (edge->count * CGRAPH_FREQ_BASE / outer_node->count
907 >= max_prob))
909 reason = "profile of recursive call is too large";
910 want_inline = false;
912 if (!max_count
913 && (edge->frequency * CGRAPH_FREQ_BASE / caller_freq
914 >= max_prob))
916 reason = "frequency of recursive call is too large";
917 want_inline = false;
920 /* Recursive inlining, i.e. equivalent of unrolling, is profitable if recursion
921 depth is large. We reduce function call overhead and increase chances that
922 things fit in hardware return predictor.
924 Recursive inlining might however increase cost of stack frame setup
925 actually slowing down functions whose recursion tree is wide rather than
926 deep.
928 Deciding reliably on when to do recursive inlining without profile feedback
929 is tricky. For now we disable recursive inlining when probability of self
930 recursion is low.
932 Recursive inlining of self recursive call within loop also results in large loop
933 depths that generally optimize badly. We may want to throttle down inlining
934 in those cases. In particular this seems to happen in one of libstdc++ rb tree
935 methods. */
936 else
938 if (max_count
939 && (edge->count * 100 / outer_node->count
940 <= PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY)))
942 reason = "profile of recursive call is too small";
943 want_inline = false;
945 else if (!max_count
946 && (edge->frequency * 100 / caller_freq
947 <= PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY)))
949 reason = "frequency of recursive call is too small";
950 want_inline = false;
953 if (!want_inline && dump_file)
954 fprintf (dump_file, " not inlining recursively: %s\n", reason);
955 return want_inline;
958 /* Return true when NODE has uninlinable caller;
959 set HAS_HOT_CALL if it has hot call.
960 Worker for cgraph_for_node_and_aliases. */
962 static bool
963 check_callers (struct cgraph_node *node, void *has_hot_call)
965 struct cgraph_edge *e;
966 for (e = node->callers; e; e = e->next_caller)
968 if (!opt_for_fn (e->caller->decl, flag_inline_functions_called_once))
969 return true;
970 if (!can_inline_edge_p (e, true))
971 return true;
972 if (e->recursive_p ())
973 return true;
974 if (!(*(bool *)has_hot_call) && e->maybe_hot_p ())
975 *(bool *)has_hot_call = true;
977 return false;
980 /* If NODE has a caller, return true. */
982 static bool
983 has_caller_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
985 if (node->callers)
986 return true;
987 return false;
990 /* Decide if inlining NODE would reduce unit size by eliminating
991 the offline copy of function.
992 When COLD is true the cold calls are considered, too. */
994 static bool
995 want_inline_function_to_all_callers_p (struct cgraph_node *node, bool cold)
997 bool has_hot_call = false;
999 /* Aliases gets inlined along with the function they alias. */
1000 if (node->alias)
1001 return false;
1002 /* Already inlined? */
1003 if (node->global.inlined_to)
1004 return false;
1005 /* Does it have callers? */
1006 if (!node->call_for_symbol_and_aliases (has_caller_p, NULL, true))
1007 return false;
1008 /* Inlining into all callers would increase size? */
1009 if (estimate_growth (node) > 0)
1010 return false;
1011 /* All inlines must be possible. */
1012 if (node->call_for_symbol_and_aliases (check_callers, &has_hot_call,
1013 true))
1014 return false;
1015 if (!cold && !has_hot_call)
1016 return false;
1017 return true;
1020 /* A cost model driving the inlining heuristics in a way so the edges with
1021 smallest badness are inlined first. After each inlining is performed
1022 the costs of all caller edges of nodes affected are recomputed so the
1023 metrics may accurately depend on values such as number of inlinable callers
1024 of the function or function body size. */
1026 static sreal
1027 edge_badness (struct cgraph_edge *edge, bool dump)
1029 sreal badness;
1030 int growth, edge_time;
1031 struct cgraph_node *callee = edge->callee->ultimate_alias_target ();
1032 struct inline_summary *callee_info = inline_summaries->get (callee);
1033 inline_hints hints;
1034 cgraph_node *caller = (edge->caller->global.inlined_to
1035 ? edge->caller->global.inlined_to
1036 : edge->caller);
1038 growth = estimate_edge_growth (edge);
1039 edge_time = estimate_edge_time (edge);
1040 hints = estimate_edge_hints (edge);
1041 gcc_checking_assert (edge_time >= 0);
1042 gcc_checking_assert (edge_time <= callee_info->time);
1043 gcc_checking_assert (growth <= callee_info->size);
1045 if (dump)
1047 fprintf (dump_file, " Badness calculation for %s/%i -> %s/%i\n",
1048 xstrdup_for_dump (edge->caller->name ()),
1049 edge->caller->order,
1050 xstrdup_for_dump (callee->name ()),
1051 edge->callee->order);
1052 fprintf (dump_file, " size growth %i, time %i ",
1053 growth,
1054 edge_time);
1055 dump_inline_hints (dump_file, hints);
1056 if (big_speedup_p (edge))
1057 fprintf (dump_file, " big_speedup");
1058 fprintf (dump_file, "\n");
1061 /* Always prefer inlining saving code size. */
1062 if (growth <= 0)
1064 badness = (sreal) (-SREAL_MIN_SIG + growth) << (SREAL_MAX_EXP / 256);
1065 if (dump)
1066 fprintf (dump_file, " %f: Growth %d <= 0\n", badness.to_double (),
1067 growth);
1069 /* Inlining into EXTERNAL functions is not going to change anything unless
1070 they are themselves inlined. */
1071 else if (DECL_EXTERNAL (caller->decl))
1073 if (dump)
1074 fprintf (dump_file, " max: function is external\n");
1075 return sreal::max ();
1077 /* When profile is available. Compute badness as:
1079 time_saved * caller_count
1080 goodness = -------------------------------------------------
1081 growth_of_caller * overall_growth * combined_size
1083 badness = - goodness
1085 Again use negative value to make calls with profile appear hotter
1086 then calls without.
1088 else if (opt_for_fn (caller->decl, flag_guess_branch_prob) || caller->count)
1090 sreal numerator, denominator;
1091 int overall_growth;
1093 numerator = (compute_uninlined_call_time (callee_info, edge)
1094 - compute_inlined_call_time (edge, edge_time));
1095 if (numerator == 0)
1096 numerator = ((sreal) 1 >> 8);
1097 if (caller->count)
1098 numerator *= caller->count;
1099 else if (opt_for_fn (caller->decl, flag_branch_probabilities))
1100 numerator = numerator >> 11;
1101 denominator = growth;
1103 overall_growth = callee_info->growth;
1105 /* Look for inliner wrappers of the form:
1107 inline_caller ()
1109 do_fast_job...
1110 if (need_more_work)
1111 noninline_callee ();
1113 Withhout panilizing this case, we usually inline noninline_callee
1114 into the inline_caller because overall_growth is small preventing
1115 further inlining of inline_caller.
1117 Penalize only callgraph edges to functions with small overall
1118 growth ...
1120 if (growth > overall_growth
1121 /* ... and having only one caller which is not inlined ... */
1122 && callee_info->single_caller
1123 && !edge->caller->global.inlined_to
1124 /* ... and edges executed only conditionally ... */
1125 && edge->frequency < CGRAPH_FREQ_BASE
1126 /* ... consider case where callee is not inline but caller is ... */
1127 && ((!DECL_DECLARED_INLINE_P (edge->callee->decl)
1128 && DECL_DECLARED_INLINE_P (caller->decl))
1129 /* ... or when early optimizers decided to split and edge
1130 frequency still indicates splitting is a win ... */
1131 || (callee->split_part && !caller->split_part
1132 && edge->frequency
1133 < CGRAPH_FREQ_BASE
1134 * PARAM_VALUE
1135 (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100
1136 /* ... and do not overwrite user specified hints. */
1137 && (!DECL_DECLARED_INLINE_P (edge->callee->decl)
1138 || DECL_DECLARED_INLINE_P (caller->decl)))))
1140 struct inline_summary *caller_info = inline_summaries->get (caller);
1141 int caller_growth = caller_info->growth;
1143 /* Only apply the penalty when caller looks like inline candidate,
1144 and it is not called once and. */
1145 if (!caller_info->single_caller && overall_growth < caller_growth
1146 && caller_info->inlinable
1147 && caller_info->size
1148 < (DECL_DECLARED_INLINE_P (caller->decl)
1149 ? MAX_INLINE_INSNS_SINGLE : MAX_INLINE_INSNS_AUTO))
1151 if (dump)
1152 fprintf (dump_file,
1153 " Wrapper penalty. Increasing growth %i to %i\n",
1154 overall_growth, caller_growth);
1155 overall_growth = caller_growth;
1158 if (overall_growth > 0)
1160 /* Strongly preffer functions with few callers that can be inlined
1161 fully. The square root here leads to smaller binaries at average.
1162 Watch however for extreme cases and return to linear function
1163 when growth is large. */
1164 if (overall_growth < 256)
1165 overall_growth *= overall_growth;
1166 else
1167 overall_growth += 256 * 256 - 256;
1168 denominator *= overall_growth;
1170 denominator *= inline_summaries->get (caller)->self_size + growth;
1172 badness = - numerator / denominator;
1174 if (dump)
1176 fprintf (dump_file,
1177 " %f: guessed profile. frequency %f, count %"PRId64
1178 " caller count %"PRId64
1179 " time w/o inlining %f, time w inlining %f"
1180 " overall growth %i (current) %i (original)"
1181 " %i (compensated)\n",
1182 badness.to_double (),
1183 (double)edge->frequency / CGRAPH_FREQ_BASE,
1184 edge->count, caller->count,
1185 compute_uninlined_call_time (callee_info, edge).to_double (),
1186 compute_inlined_call_time (edge, edge_time).to_double (),
1187 estimate_growth (callee),
1188 callee_info->growth, overall_growth);
1191 /* When function local profile is not available or it does not give
1192 useful information (ie frequency is zero), base the cost on
1193 loop nest and overall size growth, so we optimize for overall number
1194 of functions fully inlined in program. */
1195 else
1197 int nest = MIN (inline_edge_summary (edge)->loop_depth, 8);
1198 badness = growth;
1200 /* Decrease badness if call is nested. */
1201 if (badness > 0)
1202 badness = badness >> nest;
1203 else
1204 badness = badness << nest;
1205 if (dump)
1206 fprintf (dump_file, " %f: no profile. nest %i\n",
1207 badness.to_double (), nest);
1209 gcc_checking_assert (badness != 0);
1211 if (edge->recursive_p ())
1212 badness = badness.shift (badness > 0 ? 4 : -4);
1213 if ((hints & (INLINE_HINT_indirect_call
1214 | INLINE_HINT_loop_iterations
1215 | INLINE_HINT_array_index
1216 | INLINE_HINT_loop_stride))
1217 || callee_info->growth <= 0)
1218 badness = badness.shift (badness > 0 ? -2 : 2);
1219 if (hints & (INLINE_HINT_same_scc))
1220 badness = badness.shift (badness > 0 ? 3 : -3);
1221 else if (hints & (INLINE_HINT_in_scc))
1222 badness = badness.shift (badness > 0 ? 2 : -2);
1223 else if (hints & (INLINE_HINT_cross_module))
1224 badness = badness.shift (badness > 0 ? 1 : -1);
1225 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
1226 badness = badness.shift (badness > 0 ? -4 : 4);
1227 else if ((hints & INLINE_HINT_declared_inline))
1228 badness = badness.shift (badness > 0 ? -3 : 3);
1229 if (dump)
1230 fprintf (dump_file, " Adjusted by hints %f\n", badness.to_double ());
1231 return badness;
1234 /* Recompute badness of EDGE and update its key in HEAP if needed. */
1235 static inline void
1236 update_edge_key (edge_heap_t *heap, struct cgraph_edge *edge)
1238 sreal badness = edge_badness (edge, false);
1239 if (edge->aux)
1241 edge_heap_node_t *n = (edge_heap_node_t *) edge->aux;
1242 gcc_checking_assert (n->get_data () == edge);
1244 /* fibonacci_heap::replace_key does busy updating of the
1245 heap that is unnecesarily expensive.
1246 We do lazy increases: after extracting minimum if the key
1247 turns out to be out of date, it is re-inserted into heap
1248 with correct value. */
1249 if (badness < n->get_key ())
1251 if (dump_file && (dump_flags & TDF_DETAILS))
1253 fprintf (dump_file,
1254 " decreasing badness %s/%i -> %s/%i, %f"
1255 " to %f\n",
1256 xstrdup_for_dump (edge->caller->name ()),
1257 edge->caller->order,
1258 xstrdup_for_dump (edge->callee->name ()),
1259 edge->callee->order,
1260 n->get_key ().to_double (),
1261 badness.to_double ());
1263 heap->decrease_key (n, badness);
1266 else
1268 if (dump_file && (dump_flags & TDF_DETAILS))
1270 fprintf (dump_file,
1271 " enqueuing call %s/%i -> %s/%i, badness %f\n",
1272 xstrdup_for_dump (edge->caller->name ()),
1273 edge->caller->order,
1274 xstrdup_for_dump (edge->callee->name ()),
1275 edge->callee->order,
1276 badness.to_double ());
1278 edge->aux = heap->insert (badness, edge);
1283 /* NODE was inlined.
1284 All caller edges needs to be resetted because
1285 size estimates change. Similarly callees needs reset
1286 because better context may be known. */
1288 static void
1289 reset_edge_caches (struct cgraph_node *node)
1291 struct cgraph_edge *edge;
1292 struct cgraph_edge *e = node->callees;
1293 struct cgraph_node *where = node;
1294 struct ipa_ref *ref;
1296 if (where->global.inlined_to)
1297 where = where->global.inlined_to;
1299 for (edge = where->callers; edge; edge = edge->next_caller)
1300 if (edge->inline_failed)
1301 reset_edge_growth_cache (edge);
1303 FOR_EACH_ALIAS (where, ref)
1304 reset_edge_caches (dyn_cast <cgraph_node *> (ref->referring));
1306 if (!e)
1307 return;
1309 while (true)
1310 if (!e->inline_failed && e->callee->callees)
1311 e = e->callee->callees;
1312 else
1314 if (e->inline_failed)
1315 reset_edge_growth_cache (e);
1316 if (e->next_callee)
1317 e = e->next_callee;
1318 else
1322 if (e->caller == node)
1323 return;
1324 e = e->caller->callers;
1326 while (!e->next_callee);
1327 e = e->next_callee;
1332 /* Recompute HEAP nodes for each of caller of NODE.
1333 UPDATED_NODES track nodes we already visited, to avoid redundant work.
1334 When CHECK_INLINABLITY_FOR is set, re-check for specified edge that
1335 it is inlinable. Otherwise check all edges. */
1337 static void
1338 update_caller_keys (edge_heap_t *heap, struct cgraph_node *node,
1339 bitmap updated_nodes,
1340 struct cgraph_edge *check_inlinablity_for)
1342 struct cgraph_edge *edge;
1343 struct ipa_ref *ref;
1345 if ((!node->alias && !inline_summaries->get (node)->inlinable)
1346 || node->global.inlined_to)
1347 return;
1348 if (!bitmap_set_bit (updated_nodes, node->uid))
1349 return;
1351 FOR_EACH_ALIAS (node, ref)
1353 struct cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1354 update_caller_keys (heap, alias, updated_nodes, check_inlinablity_for);
1357 for (edge = node->callers; edge; edge = edge->next_caller)
1358 if (edge->inline_failed)
1360 if (!check_inlinablity_for
1361 || check_inlinablity_for == edge)
1363 if (can_inline_edge_p (edge, false)
1364 && want_inline_small_function_p (edge, false))
1365 update_edge_key (heap, edge);
1366 else if (edge->aux)
1368 report_inline_failed_reason (edge);
1369 heap->delete_node ((edge_heap_node_t *) edge->aux);
1370 edge->aux = NULL;
1373 else if (edge->aux)
1374 update_edge_key (heap, edge);
1378 /* Recompute HEAP nodes for each uninlined call in NODE.
1379 This is used when we know that edge badnesses are going only to increase
1380 (we introduced new call site) and thus all we need is to insert newly
1381 created edges into heap. */
1383 static void
1384 update_callee_keys (edge_heap_t *heap, struct cgraph_node *node,
1385 bitmap updated_nodes)
1387 struct cgraph_edge *e = node->callees;
1389 if (!e)
1390 return;
1391 while (true)
1392 if (!e->inline_failed && e->callee->callees)
1393 e = e->callee->callees;
1394 else
1396 enum availability avail;
1397 struct cgraph_node *callee;
1398 /* We do not reset callee growth cache here. Since we added a new call,
1399 growth chould have just increased and consequentely badness metric
1400 don't need updating. */
1401 if (e->inline_failed
1402 && (callee = e->callee->ultimate_alias_target (&avail))
1403 && inline_summaries->get (callee)->inlinable
1404 && avail >= AVAIL_AVAILABLE
1405 && !bitmap_bit_p (updated_nodes, callee->uid))
1407 if (can_inline_edge_p (e, false)
1408 && want_inline_small_function_p (e, false))
1409 update_edge_key (heap, e);
1410 else if (e->aux)
1412 report_inline_failed_reason (e);
1413 heap->delete_node ((edge_heap_node_t *) e->aux);
1414 e->aux = NULL;
1417 if (e->next_callee)
1418 e = e->next_callee;
1419 else
1423 if (e->caller == node)
1424 return;
1425 e = e->caller->callers;
1427 while (!e->next_callee);
1428 e = e->next_callee;
1433 /* Enqueue all recursive calls from NODE into priority queue depending on
1434 how likely we want to recursively inline the call. */
1436 static void
1437 lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
1438 edge_heap_t *heap)
1440 struct cgraph_edge *e;
1441 enum availability avail;
1443 for (e = where->callees; e; e = e->next_callee)
1444 if (e->callee == node
1445 || (e->callee->ultimate_alias_target (&avail) == node
1446 && avail > AVAIL_INTERPOSABLE))
1448 /* When profile feedback is available, prioritize by expected number
1449 of calls. */
1450 heap->insert (!max_count ? -e->frequency
1451 : -(e->count / ((max_count + (1<<24) - 1) / (1<<24))),
1454 for (e = where->callees; e; e = e->next_callee)
1455 if (!e->inline_failed)
1456 lookup_recursive_calls (node, e->callee, heap);
1459 /* Decide on recursive inlining: in the case function has recursive calls,
1460 inline until body size reaches given argument. If any new indirect edges
1461 are discovered in the process, add them to *NEW_EDGES, unless NEW_EDGES
1462 is NULL. */
1464 static bool
1465 recursive_inlining (struct cgraph_edge *edge,
1466 vec<cgraph_edge *> *new_edges)
1468 int limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO);
1469 edge_heap_t heap (sreal::min ());
1470 struct cgraph_node *node;
1471 struct cgraph_edge *e;
1472 struct cgraph_node *master_clone = NULL, *next;
1473 int depth = 0;
1474 int n = 0;
1476 node = edge->caller;
1477 if (node->global.inlined_to)
1478 node = node->global.inlined_to;
1480 if (DECL_DECLARED_INLINE_P (node->decl))
1481 limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE);
1483 /* Make sure that function is small enough to be considered for inlining. */
1484 if (estimate_size_after_inlining (node, edge) >= limit)
1485 return false;
1486 lookup_recursive_calls (node, node, &heap);
1487 if (heap.empty ())
1488 return false;
1490 if (dump_file)
1491 fprintf (dump_file,
1492 " Performing recursive inlining on %s\n",
1493 node->name ());
1495 /* Do the inlining and update list of recursive call during process. */
1496 while (!heap.empty ())
1498 struct cgraph_edge *curr = heap.extract_min ();
1499 struct cgraph_node *cnode, *dest = curr->callee;
1501 if (!can_inline_edge_p (curr, true))
1502 continue;
1504 /* MASTER_CLONE is produced in the case we already started modified
1505 the function. Be sure to redirect edge to the original body before
1506 estimating growths otherwise we will be seeing growths after inlining
1507 the already modified body. */
1508 if (master_clone)
1510 curr->redirect_callee (master_clone);
1511 reset_edge_growth_cache (curr);
1514 if (estimate_size_after_inlining (node, curr) > limit)
1516 curr->redirect_callee (dest);
1517 reset_edge_growth_cache (curr);
1518 break;
1521 depth = 1;
1522 for (cnode = curr->caller;
1523 cnode->global.inlined_to; cnode = cnode->callers->caller)
1524 if (node->decl
1525 == curr->callee->ultimate_alias_target ()->decl)
1526 depth++;
1528 if (!want_inline_self_recursive_call_p (curr, node, false, depth))
1530 curr->redirect_callee (dest);
1531 reset_edge_growth_cache (curr);
1532 continue;
1535 if (dump_file)
1537 fprintf (dump_file,
1538 " Inlining call of depth %i", depth);
1539 if (node->count)
1541 fprintf (dump_file, " called approx. %.2f times per call",
1542 (double)curr->count / node->count);
1544 fprintf (dump_file, "\n");
1546 if (!master_clone)
1548 /* We need original clone to copy around. */
1549 master_clone = node->create_clone (node->decl, node->count,
1550 CGRAPH_FREQ_BASE, false, vNULL,
1551 true, NULL, NULL);
1552 for (e = master_clone->callees; e; e = e->next_callee)
1553 if (!e->inline_failed)
1554 clone_inlined_nodes (e, true, false, NULL, CGRAPH_FREQ_BASE);
1555 curr->redirect_callee (master_clone);
1556 reset_edge_growth_cache (curr);
1559 inline_call (curr, false, new_edges, &overall_size, true);
1560 lookup_recursive_calls (node, curr->callee, &heap);
1561 n++;
1564 if (!heap.empty () && dump_file)
1565 fprintf (dump_file, " Recursive inlining growth limit met.\n");
1567 if (!master_clone)
1568 return false;
1570 if (dump_file)
1571 fprintf (dump_file,
1572 "\n Inlined %i times, "
1573 "body grown from size %i to %i, time %i to %i\n", n,
1574 inline_summaries->get (master_clone)->size, inline_summaries->get (node)->size,
1575 inline_summaries->get (master_clone)->time, inline_summaries->get (node)->time);
1577 /* Remove master clone we used for inlining. We rely that clones inlined
1578 into master clone gets queued just before master clone so we don't
1579 need recursion. */
1580 for (node = symtab->first_function (); node != master_clone;
1581 node = next)
1583 next = symtab->next_function (node);
1584 if (node->global.inlined_to == master_clone)
1585 node->remove ();
1587 master_clone->remove ();
1588 return true;
1592 /* Given whole compilation unit estimate of INSNS, compute how large we can
1593 allow the unit to grow. */
1595 static int
1596 compute_max_insns (int insns)
1598 int max_insns = insns;
1599 if (max_insns < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
1600 max_insns = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
1602 return ((int64_t) max_insns
1603 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
1607 /* Compute badness of all edges in NEW_EDGES and add them to the HEAP. */
1609 static void
1610 add_new_edges_to_heap (edge_heap_t *heap, vec<cgraph_edge *> new_edges)
1612 while (new_edges.length () > 0)
1614 struct cgraph_edge *edge = new_edges.pop ();
1616 gcc_assert (!edge->aux);
1617 if (edge->inline_failed
1618 && can_inline_edge_p (edge, true)
1619 && want_inline_small_function_p (edge, true))
1620 edge->aux = heap->insert (edge_badness (edge, false), edge);
1624 /* Remove EDGE from the fibheap. */
1626 static void
1627 heap_edge_removal_hook (struct cgraph_edge *e, void *data)
1629 if (e->aux)
1631 ((edge_heap_t *)data)->delete_node ((edge_heap_node_t *)e->aux);
1632 e->aux = NULL;
1636 /* Return true if speculation of edge E seems useful.
1637 If ANTICIPATE_INLINING is true, be conservative and hope that E
1638 may get inlined. */
1640 bool
1641 speculation_useful_p (struct cgraph_edge *e, bool anticipate_inlining)
1643 enum availability avail;
1644 struct cgraph_node *target = e->callee->ultimate_alias_target (&avail);
1645 struct cgraph_edge *direct, *indirect;
1646 struct ipa_ref *ref;
1648 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1650 if (!e->maybe_hot_p ())
1651 return false;
1653 /* See if IP optimizations found something potentially useful about the
1654 function. For now we look only for CONST/PURE flags. Almost everything
1655 else we propagate is useless. */
1656 if (avail >= AVAIL_AVAILABLE)
1658 int ecf_flags = flags_from_decl_or_type (target->decl);
1659 if (ecf_flags & ECF_CONST)
1661 e->speculative_call_info (direct, indirect, ref);
1662 if (!(indirect->indirect_info->ecf_flags & ECF_CONST))
1663 return true;
1665 else if (ecf_flags & ECF_PURE)
1667 e->speculative_call_info (direct, indirect, ref);
1668 if (!(indirect->indirect_info->ecf_flags & ECF_PURE))
1669 return true;
1672 /* If we did not managed to inline the function nor redirect
1673 to an ipa-cp clone (that are seen by having local flag set),
1674 it is probably pointless to inline it unless hardware is missing
1675 indirect call predictor. */
1676 if (!anticipate_inlining && e->inline_failed && !target->local.local)
1677 return false;
1678 /* For overwritable targets there is not much to do. */
1679 if (e->inline_failed && !can_inline_edge_p (e, false, true))
1680 return false;
1681 /* OK, speculation seems interesting. */
1682 return true;
1685 /* We know that EDGE is not going to be inlined.
1686 See if we can remove speculation. */
1688 static void
1689 resolve_noninline_speculation (edge_heap_t *edge_heap, struct cgraph_edge *edge)
1691 if (edge->speculative && !speculation_useful_p (edge, false))
1693 struct cgraph_node *node = edge->caller;
1694 struct cgraph_node *where = node->global.inlined_to
1695 ? node->global.inlined_to : node;
1696 bitmap updated_nodes = BITMAP_ALLOC (NULL);
1698 spec_rem += edge->count;
1699 edge->resolve_speculation ();
1700 reset_edge_caches (where);
1701 inline_update_overall_summary (where);
1702 update_caller_keys (edge_heap, where,
1703 updated_nodes, NULL);
1704 update_callee_keys (edge_heap, where,
1705 updated_nodes);
1706 BITMAP_FREE (updated_nodes);
1710 /* Return true if NODE should be accounted for overall size estimate.
1711 Skip all nodes optimized for size so we can measure the growth of hot
1712 part of program no matter of the padding. */
1714 bool
1715 inline_account_function_p (struct cgraph_node *node)
1717 return (!DECL_EXTERNAL (node->decl)
1718 && !opt_for_fn (node->decl, optimize_size)
1719 && node->frequency != NODE_FREQUENCY_UNLIKELY_EXECUTED);
1722 /* Count number of callers of NODE and store it into DATA (that
1723 points to int. Worker for cgraph_for_node_and_aliases. */
1725 static bool
1726 sum_callers (struct cgraph_node *node, void *data)
1728 struct cgraph_edge *e;
1729 int *num_calls = (int *)data;
1731 for (e = node->callers; e; e = e->next_caller)
1732 (*num_calls)++;
1733 return false;
1736 /* We use greedy algorithm for inlining of small functions:
1737 All inline candidates are put into prioritized heap ordered in
1738 increasing badness.
1740 The inlining of small functions is bounded by unit growth parameters. */
1742 static void
1743 inline_small_functions (void)
1745 struct cgraph_node *node;
1746 struct cgraph_edge *edge;
1747 edge_heap_t edge_heap (sreal::min ());
1748 bitmap updated_nodes = BITMAP_ALLOC (NULL);
1749 int min_size, max_size;
1750 auto_vec<cgraph_edge *> new_indirect_edges;
1751 int initial_size = 0;
1752 struct cgraph_node **order = XCNEWVEC (cgraph_node *, symtab->cgraph_count);
1753 struct cgraph_edge_hook_list *edge_removal_hook_holder;
1754 new_indirect_edges.create (8);
1756 edge_removal_hook_holder
1757 = symtab->add_edge_removal_hook (&heap_edge_removal_hook, &edge_heap);
1759 /* Compute overall unit size and other global parameters used by badness
1760 metrics. */
1762 max_count = 0;
1763 ipa_reduced_postorder (order, true, true, NULL);
1764 free (order);
1766 FOR_EACH_DEFINED_FUNCTION (node)
1767 if (!node->global.inlined_to)
1769 if (!node->alias && node->analyzed
1770 && (node->has_gimple_body_p () || node->thunk.thunk_p))
1772 struct inline_summary *info = inline_summaries->get (node);
1773 struct ipa_dfs_info *dfs = (struct ipa_dfs_info *) node->aux;
1775 /* Do not account external functions, they will be optimized out
1776 if not inlined. Also only count the non-cold portion of program. */
1777 if (inline_account_function_p (node))
1778 initial_size += info->size;
1779 info->growth = estimate_growth (node);
1781 int num_calls = 0;
1782 node->call_for_symbol_and_aliases (sum_callers, &num_calls,
1783 true);
1784 if (num_calls == 1)
1785 info->single_caller = true;
1786 if (dfs && dfs->next_cycle)
1788 struct cgraph_node *n2;
1789 int id = dfs->scc_no + 1;
1790 for (n2 = node; n2;
1791 n2 = ((struct ipa_dfs_info *) node->aux)->next_cycle)
1793 struct inline_summary *info2 = inline_summaries->get (n2);
1794 if (info2->scc_no)
1795 break;
1796 info2->scc_no = id;
1801 for (edge = node->callers; edge; edge = edge->next_caller)
1802 if (max_count < edge->count)
1803 max_count = edge->count;
1805 ipa_free_postorder_info ();
1806 initialize_growth_caches ();
1808 if (dump_file)
1809 fprintf (dump_file,
1810 "\nDeciding on inlining of small functions. Starting with size %i.\n",
1811 initial_size);
1813 overall_size = initial_size;
1814 max_size = compute_max_insns (overall_size);
1815 min_size = overall_size;
1817 /* Populate the heap with all edges we might inline. */
1819 FOR_EACH_DEFINED_FUNCTION (node)
1821 bool update = false;
1822 struct cgraph_edge *next = NULL;
1823 bool has_speculative = false;
1825 if (dump_file)
1826 fprintf (dump_file, "Enqueueing calls in %s/%i.\n",
1827 node->name (), node->order);
1829 for (edge = node->callees; edge; edge = next)
1831 next = edge->next_callee;
1832 if (edge->inline_failed
1833 && !edge->aux
1834 && can_inline_edge_p (edge, true)
1835 && want_inline_small_function_p (edge, true)
1836 && edge->inline_failed)
1838 gcc_assert (!edge->aux);
1839 update_edge_key (&edge_heap, edge);
1841 if (edge->speculative)
1842 has_speculative = true;
1844 if (has_speculative)
1845 for (edge = node->callees; edge; edge = next)
1846 if (edge->speculative && !speculation_useful_p (edge,
1847 edge->aux != NULL))
1849 edge->resolve_speculation ();
1850 update = true;
1852 if (update)
1854 struct cgraph_node *where = node->global.inlined_to
1855 ? node->global.inlined_to : node;
1856 inline_update_overall_summary (where);
1857 reset_edge_caches (where);
1858 update_caller_keys (&edge_heap, where,
1859 updated_nodes, NULL);
1860 update_callee_keys (&edge_heap, where,
1861 updated_nodes);
1862 bitmap_clear (updated_nodes);
1866 gcc_assert (in_lto_p
1867 || !max_count
1868 || (profile_info && flag_branch_probabilities));
1870 while (!edge_heap.empty ())
1872 int old_size = overall_size;
1873 struct cgraph_node *where, *callee;
1874 sreal badness = edge_heap.min_key ();
1875 sreal current_badness;
1876 int growth;
1878 edge = edge_heap.extract_min ();
1879 gcc_assert (edge->aux);
1880 edge->aux = NULL;
1881 if (!edge->inline_failed || !edge->callee->analyzed)
1882 continue;
1884 #ifdef ENABLE_CHECKING
1885 /* Be sure that caches are maintained consistent. */
1886 sreal cached_badness = edge_badness (edge, false);
1888 int old_size_est = estimate_edge_size (edge);
1889 int old_time_est = estimate_edge_time (edge);
1890 int old_hints_est = estimate_edge_hints (edge);
1892 reset_edge_growth_cache (edge);
1893 gcc_assert (old_size_est == estimate_edge_size (edge));
1894 gcc_assert (old_time_est == estimate_edge_time (edge));
1895 /* FIXME:
1897 gcc_assert (old_hints_est == estimate_edge_hints (edge));
1899 fails with profile feedback because some hints depends on
1900 maybe_hot_edge_p predicate and because callee gets inlined to other
1901 calls, the edge may become cold.
1902 This ought to be fixed by computing relative probabilities
1903 for given invocation but that will be better done once whole
1904 code is converted to sreals. Disable for now and revert to "wrong"
1905 value so enable/disable checking paths agree. */
1906 edge_growth_cache[edge->uid].hints = old_hints_est + 1;
1908 /* When updating the edge costs, we only decrease badness in the keys.
1909 Increases of badness are handled lazilly; when we see key with out
1910 of date value on it, we re-insert it now. */
1911 current_badness = edge_badness (edge, false);
1912 /* Disable checking for profile because roundoff errors may cause slight
1913 deviations in the order. */
1914 gcc_assert (max_count || cached_badness == current_badness);
1915 gcc_assert (current_badness >= badness);
1916 #else
1917 current_badness = edge_badness (edge, false);
1918 #endif
1919 if (current_badness != badness)
1921 if (edge_heap.min () && current_badness > edge_heap.min_key ())
1923 edge->aux = edge_heap.insert (current_badness, edge);
1924 continue;
1926 else
1927 badness = current_badness;
1930 if (!can_inline_edge_p (edge, true))
1932 resolve_noninline_speculation (&edge_heap, edge);
1933 continue;
1936 callee = edge->callee->ultimate_alias_target ();
1937 growth = estimate_edge_growth (edge);
1938 if (dump_file)
1940 fprintf (dump_file,
1941 "\nConsidering %s/%i with %i size\n",
1942 callee->name (), callee->order,
1943 inline_summaries->get (callee)->size);
1944 fprintf (dump_file,
1945 " to be inlined into %s/%i in %s:%i\n"
1946 " Estimated badness is %f, frequency %.2f.\n",
1947 edge->caller->name (), edge->caller->order,
1948 edge->call_stmt
1949 && (LOCATION_LOCUS (gimple_location ((const_gimple)
1950 edge->call_stmt))
1951 > BUILTINS_LOCATION)
1952 ? gimple_filename ((const_gimple) edge->call_stmt)
1953 : "unknown",
1954 edge->call_stmt
1955 ? gimple_lineno ((const_gimple) edge->call_stmt)
1956 : -1,
1957 badness.to_double (),
1958 edge->frequency / (double)CGRAPH_FREQ_BASE);
1959 if (edge->count)
1960 fprintf (dump_file," Called %"PRId64"x\n",
1961 edge->count);
1962 if (dump_flags & TDF_DETAILS)
1963 edge_badness (edge, true);
1966 if (overall_size + growth > max_size
1967 && !DECL_DISREGARD_INLINE_LIMITS (callee->decl))
1969 edge->inline_failed = CIF_INLINE_UNIT_GROWTH_LIMIT;
1970 report_inline_failed_reason (edge);
1971 resolve_noninline_speculation (&edge_heap, edge);
1972 continue;
1975 if (!want_inline_small_function_p (edge, true))
1977 resolve_noninline_speculation (&edge_heap, edge);
1978 continue;
1981 /* Heuristics for inlining small functions work poorly for
1982 recursive calls where we do effects similar to loop unrolling.
1983 When inlining such edge seems profitable, leave decision on
1984 specific inliner. */
1985 if (edge->recursive_p ())
1987 where = edge->caller;
1988 if (where->global.inlined_to)
1989 where = where->global.inlined_to;
1990 if (!recursive_inlining (edge,
1991 opt_for_fn (edge->caller->decl,
1992 flag_indirect_inlining)
1993 ? &new_indirect_edges : NULL))
1995 edge->inline_failed = CIF_RECURSIVE_INLINING;
1996 resolve_noninline_speculation (&edge_heap, edge);
1997 continue;
1999 reset_edge_caches (where);
2000 /* Recursive inliner inlines all recursive calls of the function
2001 at once. Consequently we need to update all callee keys. */
2002 if (opt_for_fn (edge->caller->decl, flag_indirect_inlining))
2003 add_new_edges_to_heap (&edge_heap, new_indirect_edges);
2004 update_callee_keys (&edge_heap, where, updated_nodes);
2005 bitmap_clear (updated_nodes);
2007 else
2009 struct cgraph_node *outer_node = NULL;
2010 int depth = 0;
2012 /* Consider the case where self recursive function A is inlined
2013 into B. This is desired optimization in some cases, since it
2014 leads to effect similar of loop peeling and we might completely
2015 optimize out the recursive call. However we must be extra
2016 selective. */
2018 where = edge->caller;
2019 while (where->global.inlined_to)
2021 if (where->decl == callee->decl)
2022 outer_node = where, depth++;
2023 where = where->callers->caller;
2025 if (outer_node
2026 && !want_inline_self_recursive_call_p (edge, outer_node,
2027 true, depth))
2029 edge->inline_failed
2030 = (DECL_DISREGARD_INLINE_LIMITS (edge->callee->decl)
2031 ? CIF_RECURSIVE_INLINING : CIF_UNSPECIFIED);
2032 resolve_noninline_speculation (&edge_heap, edge);
2033 continue;
2035 else if (depth && dump_file)
2036 fprintf (dump_file, " Peeling recursion with depth %i\n", depth);
2038 gcc_checking_assert (!callee->global.inlined_to);
2039 inline_call (edge, true, &new_indirect_edges, &overall_size, true);
2040 add_new_edges_to_heap (&edge_heap, new_indirect_edges);
2042 reset_edge_caches (edge->callee->function_symbol ());
2044 update_callee_keys (&edge_heap, where, updated_nodes);
2046 where = edge->caller;
2047 if (where->global.inlined_to)
2048 where = where->global.inlined_to;
2050 /* Our profitability metric can depend on local properties
2051 such as number of inlinable calls and size of the function body.
2052 After inlining these properties might change for the function we
2053 inlined into (since it's body size changed) and for the functions
2054 called by function we inlined (since number of it inlinable callers
2055 might change). */
2056 update_caller_keys (&edge_heap, where, updated_nodes, NULL);
2057 /* Offline copy count has possibly changed, recompute if profile is
2058 available. */
2059 if (max_count)
2061 struct cgraph_node *n = cgraph_node::get (edge->callee->decl);
2062 if (n != edge->callee && n->analyzed)
2063 update_callee_keys (&edge_heap, n, updated_nodes);
2065 bitmap_clear (updated_nodes);
2067 if (dump_file)
2069 fprintf (dump_file,
2070 " Inlined into %s which now has time %i and size %i,"
2071 "net change of %+i.\n",
2072 edge->caller->name (),
2073 inline_summaries->get (edge->caller)->time,
2074 inline_summaries->get (edge->caller)->size,
2075 overall_size - old_size);
2077 if (min_size > overall_size)
2079 min_size = overall_size;
2080 max_size = compute_max_insns (min_size);
2082 if (dump_file)
2083 fprintf (dump_file, "New minimal size reached: %i\n", min_size);
2087 free_growth_caches ();
2088 if (dump_file)
2089 fprintf (dump_file,
2090 "Unit growth for small function inlining: %i->%i (%i%%)\n",
2091 initial_size, overall_size,
2092 initial_size ? overall_size * 100 / (initial_size) - 100: 0);
2093 BITMAP_FREE (updated_nodes);
2094 symtab->remove_edge_removal_hook (edge_removal_hook_holder);
2097 /* Flatten NODE. Performed both during early inlining and
2098 at IPA inlining time. */
2100 static void
2101 flatten_function (struct cgraph_node *node, bool early)
2103 struct cgraph_edge *e;
2105 /* We shouldn't be called recursively when we are being processed. */
2106 gcc_assert (node->aux == NULL);
2108 node->aux = (void *) node;
2110 for (e = node->callees; e; e = e->next_callee)
2112 struct cgraph_node *orig_callee;
2113 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
2115 /* We've hit cycle? It is time to give up. */
2116 if (callee->aux)
2118 if (dump_file)
2119 fprintf (dump_file,
2120 "Not inlining %s into %s to avoid cycle.\n",
2121 xstrdup_for_dump (callee->name ()),
2122 xstrdup_for_dump (e->caller->name ()));
2123 e->inline_failed = CIF_RECURSIVE_INLINING;
2124 continue;
2127 /* When the edge is already inlined, we just need to recurse into
2128 it in order to fully flatten the leaves. */
2129 if (!e->inline_failed)
2131 flatten_function (callee, early);
2132 continue;
2135 /* Flatten attribute needs to be processed during late inlining. For
2136 extra code quality we however do flattening during early optimization,
2137 too. */
2138 if (!early
2139 ? !can_inline_edge_p (e, true)
2140 : !can_early_inline_edge_p (e))
2141 continue;
2143 if (e->recursive_p ())
2145 if (dump_file)
2146 fprintf (dump_file, "Not inlining: recursive call.\n");
2147 continue;
2150 if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
2151 != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
2153 if (dump_file)
2154 fprintf (dump_file, "Not inlining: SSA form does not match.\n");
2155 continue;
2158 /* Inline the edge and flatten the inline clone. Avoid
2159 recursing through the original node if the node was cloned. */
2160 if (dump_file)
2161 fprintf (dump_file, " Inlining %s into %s.\n",
2162 xstrdup_for_dump (callee->name ()),
2163 xstrdup_for_dump (e->caller->name ()));
2164 orig_callee = callee;
2165 inline_call (e, true, NULL, NULL, false);
2166 if (e->callee != orig_callee)
2167 orig_callee->aux = (void *) node;
2168 flatten_function (e->callee, early);
2169 if (e->callee != orig_callee)
2170 orig_callee->aux = NULL;
2173 node->aux = NULL;
2174 if (!node->global.inlined_to)
2175 inline_update_overall_summary (node);
2178 /* Inline NODE to all callers. Worker for cgraph_for_node_and_aliases.
2179 DATA points to number of calls originally found so we avoid infinite
2180 recursion. */
2182 static bool
2183 inline_to_all_callers (struct cgraph_node *node, void *data)
2185 int *num_calls = (int *)data;
2186 bool callee_removed = false;
2188 while (node->callers && !node->global.inlined_to)
2190 struct cgraph_node *caller = node->callers->caller;
2192 if (!can_inline_edge_p (node->callers, true)
2193 || node->callers->recursive_p ())
2195 if (dump_file)
2196 fprintf (dump_file, "Uninlinable call found; giving up.\n");
2197 *num_calls = 0;
2198 return false;
2201 if (dump_file)
2203 fprintf (dump_file,
2204 "\nInlining %s size %i.\n",
2205 node->name (),
2206 inline_summaries->get (node)->size);
2207 fprintf (dump_file,
2208 " Called once from %s %i insns.\n",
2209 node->callers->caller->name (),
2210 inline_summaries->get (node->callers->caller)->size);
2213 inline_call (node->callers, true, NULL, NULL, true, &callee_removed);
2214 if (dump_file)
2215 fprintf (dump_file,
2216 " Inlined into %s which now has %i size\n",
2217 caller->name (),
2218 inline_summaries->get (caller)->size);
2219 if (!(*num_calls)--)
2221 if (dump_file)
2222 fprintf (dump_file, "New calls found; giving up.\n");
2223 return callee_removed;
2225 if (callee_removed)
2226 return true;
2228 return false;
2231 /* Output overall time estimate. */
2232 static void
2233 dump_overall_stats (void)
2235 int64_t sum_weighted = 0, sum = 0;
2236 struct cgraph_node *node;
2238 FOR_EACH_DEFINED_FUNCTION (node)
2239 if (!node->global.inlined_to
2240 && !node->alias)
2242 int time = inline_summaries->get (node)->time;
2243 sum += time;
2244 sum_weighted += time * node->count;
2246 fprintf (dump_file, "Overall time estimate: "
2247 "%"PRId64" weighted by profile: "
2248 "%"PRId64"\n", sum, sum_weighted);
2251 /* Output some useful stats about inlining. */
2253 static void
2254 dump_inline_stats (void)
2256 int64_t inlined_cnt = 0, inlined_indir_cnt = 0;
2257 int64_t inlined_virt_cnt = 0, inlined_virt_indir_cnt = 0;
2258 int64_t noninlined_cnt = 0, noninlined_indir_cnt = 0;
2259 int64_t noninlined_virt_cnt = 0, noninlined_virt_indir_cnt = 0;
2260 int64_t inlined_speculative = 0, inlined_speculative_ply = 0;
2261 int64_t indirect_poly_cnt = 0, indirect_cnt = 0;
2262 int64_t reason[CIF_N_REASONS][3];
2263 int i;
2264 struct cgraph_node *node;
2266 memset (reason, 0, sizeof (reason));
2267 FOR_EACH_DEFINED_FUNCTION (node)
2269 struct cgraph_edge *e;
2270 for (e = node->callees; e; e = e->next_callee)
2272 if (e->inline_failed)
2274 reason[(int) e->inline_failed][0] += e->count;
2275 reason[(int) e->inline_failed][1] += e->frequency;
2276 reason[(int) e->inline_failed][2] ++;
2277 if (DECL_VIRTUAL_P (e->callee->decl))
2279 if (e->indirect_inlining_edge)
2280 noninlined_virt_indir_cnt += e->count;
2281 else
2282 noninlined_virt_cnt += e->count;
2284 else
2286 if (e->indirect_inlining_edge)
2287 noninlined_indir_cnt += e->count;
2288 else
2289 noninlined_cnt += e->count;
2292 else
2294 if (e->speculative)
2296 if (DECL_VIRTUAL_P (e->callee->decl))
2297 inlined_speculative_ply += e->count;
2298 else
2299 inlined_speculative += e->count;
2301 else if (DECL_VIRTUAL_P (e->callee->decl))
2303 if (e->indirect_inlining_edge)
2304 inlined_virt_indir_cnt += e->count;
2305 else
2306 inlined_virt_cnt += e->count;
2308 else
2310 if (e->indirect_inlining_edge)
2311 inlined_indir_cnt += e->count;
2312 else
2313 inlined_cnt += e->count;
2317 for (e = node->indirect_calls; e; e = e->next_callee)
2318 if (e->indirect_info->polymorphic)
2319 indirect_poly_cnt += e->count;
2320 else
2321 indirect_cnt += e->count;
2323 if (max_count)
2325 fprintf (dump_file,
2326 "Inlined %"PRId64 " + speculative "
2327 "%"PRId64 " + speculative polymorphic "
2328 "%"PRId64 " + previously indirect "
2329 "%"PRId64 " + virtual "
2330 "%"PRId64 " + virtual and previously indirect "
2331 "%"PRId64 "\n" "Not inlined "
2332 "%"PRId64 " + previously indirect "
2333 "%"PRId64 " + virtual "
2334 "%"PRId64 " + virtual and previously indirect "
2335 "%"PRId64 " + stil indirect "
2336 "%"PRId64 " + still indirect polymorphic "
2337 "%"PRId64 "\n", inlined_cnt,
2338 inlined_speculative, inlined_speculative_ply,
2339 inlined_indir_cnt, inlined_virt_cnt, inlined_virt_indir_cnt,
2340 noninlined_cnt, noninlined_indir_cnt, noninlined_virt_cnt,
2341 noninlined_virt_indir_cnt, indirect_cnt, indirect_poly_cnt);
2342 fprintf (dump_file,
2343 "Removed speculations %"PRId64 "\n",
2344 spec_rem);
2346 dump_overall_stats ();
2347 fprintf (dump_file, "\nWhy inlining failed?\n");
2348 for (i = 0; i < CIF_N_REASONS; i++)
2349 if (reason[i][2])
2350 fprintf (dump_file, "%-50s: %8i calls, %8i freq, %"PRId64" count\n",
2351 cgraph_inline_failed_string ((cgraph_inline_failed_t) i),
2352 (int) reason[i][2], (int) reason[i][1], reason[i][0]);
2355 /* Decide on the inlining. We do so in the topological order to avoid
2356 expenses on updating data structures. */
2358 static unsigned int
2359 ipa_inline (void)
2361 struct cgraph_node *node;
2362 int nnodes;
2363 struct cgraph_node **order;
2364 int i;
2365 int cold;
2366 bool remove_functions = false;
2368 if (!optimize)
2369 return 0;
2371 cgraph_freq_base_rec = (sreal) 1 / (sreal) CGRAPH_FREQ_BASE;
2372 percent_rec = (sreal) 1 / (sreal) 100;
2374 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2376 if (in_lto_p && optimize)
2377 ipa_update_after_lto_read ();
2379 if (dump_file)
2380 dump_inline_summaries (dump_file);
2382 nnodes = ipa_reverse_postorder (order);
2384 FOR_EACH_FUNCTION (node)
2386 node->aux = 0;
2388 /* Recompute the default reasons for inlining because they may have
2389 changed during merging. */
2390 if (in_lto_p)
2392 for (cgraph_edge *e = node->callees; e; e = e->next_callee)
2394 gcc_assert (e->inline_failed);
2395 initialize_inline_failed (e);
2397 for (cgraph_edge *e = node->indirect_calls; e; e = e->next_callee)
2398 initialize_inline_failed (e);
2402 if (dump_file)
2403 fprintf (dump_file, "\nFlattening functions:\n");
2405 /* In the first pass handle functions to be flattened. Do this with
2406 a priority so none of our later choices will make this impossible. */
2407 for (i = nnodes - 1; i >= 0; i--)
2409 node = order[i];
2411 /* Handle nodes to be flattened.
2412 Ideally when processing callees we stop inlining at the
2413 entry of cycles, possibly cloning that entry point and
2414 try to flatten itself turning it into a self-recursive
2415 function. */
2416 if (lookup_attribute ("flatten",
2417 DECL_ATTRIBUTES (node->decl)) != NULL)
2419 if (dump_file)
2420 fprintf (dump_file,
2421 "Flattening %s\n", node->name ());
2422 flatten_function (node, false);
2425 if (dump_file)
2426 dump_overall_stats ();
2428 inline_small_functions ();
2430 gcc_assert (symtab->state == IPA_SSA);
2431 symtab->state = IPA_SSA_AFTER_INLINING;
2432 /* Do first after-inlining removal. We want to remove all "stale" extern
2433 inline functions and virtual functions so we really know what is called
2434 once. */
2435 symtab->remove_unreachable_nodes (dump_file);
2436 free (order);
2438 /* Inline functions with a property that after inlining into all callers the
2439 code size will shrink because the out-of-line copy is eliminated.
2440 We do this regardless on the callee size as long as function growth limits
2441 are met. */
2442 if (dump_file)
2443 fprintf (dump_file,
2444 "\nDeciding on functions to be inlined into all callers and "
2445 "removing useless speculations:\n");
2447 /* Inlining one function called once has good chance of preventing
2448 inlining other function into the same callee. Ideally we should
2449 work in priority order, but probably inlining hot functions first
2450 is good cut without the extra pain of maintaining the queue.
2452 ??? this is not really fitting the bill perfectly: inlining function
2453 into callee often leads to better optimization of callee due to
2454 increased context for optimization.
2455 For example if main() function calls a function that outputs help
2456 and then function that does the main optmization, we should inline
2457 the second with priority even if both calls are cold by themselves.
2459 We probably want to implement new predicate replacing our use of
2460 maybe_hot_edge interpreted as maybe_hot_edge || callee is known
2461 to be hot. */
2462 for (cold = 0; cold <= 1; cold ++)
2464 FOR_EACH_DEFINED_FUNCTION (node)
2466 struct cgraph_edge *edge, *next;
2467 bool update=false;
2469 for (edge = node->callees; edge; edge = next)
2471 next = edge->next_callee;
2472 if (edge->speculative && !speculation_useful_p (edge, false))
2474 edge->resolve_speculation ();
2475 spec_rem += edge->count;
2476 update = true;
2477 remove_functions = true;
2480 if (update)
2482 struct cgraph_node *where = node->global.inlined_to
2483 ? node->global.inlined_to : node;
2484 reset_edge_caches (where);
2485 inline_update_overall_summary (where);
2487 if (want_inline_function_to_all_callers_p (node, cold))
2489 int num_calls = 0;
2490 node->call_for_symbol_and_aliases (sum_callers, &num_calls,
2491 true);
2492 while (node->call_for_symbol_and_aliases
2493 (inline_to_all_callers, &num_calls, true))
2495 remove_functions = true;
2500 /* Free ipa-prop structures if they are no longer needed. */
2501 if (optimize)
2502 ipa_free_all_structures_after_iinln ();
2504 if (dump_file)
2506 fprintf (dump_file,
2507 "\nInlined %i calls, eliminated %i functions\n\n",
2508 ncalls_inlined, nfunctions_inlined);
2509 dump_inline_stats ();
2512 if (dump_file)
2513 dump_inline_summaries (dump_file);
2514 /* In WPA we use inline summaries for partitioning process. */
2515 if (!flag_wpa)
2516 inline_free_summary ();
2517 return remove_functions ? TODO_remove_functions : 0;
2520 /* Inline always-inline function calls in NODE. */
2522 static bool
2523 inline_always_inline_functions (struct cgraph_node *node)
2525 struct cgraph_edge *e;
2526 bool inlined = false;
2528 for (e = node->callees; e; e = e->next_callee)
2530 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
2531 if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl))
2532 continue;
2534 if (e->recursive_p ())
2536 if (dump_file)
2537 fprintf (dump_file, " Not inlining recursive call to %s.\n",
2538 e->callee->name ());
2539 e->inline_failed = CIF_RECURSIVE_INLINING;
2540 continue;
2543 if (!can_early_inline_edge_p (e))
2545 /* Set inlined to true if the callee is marked "always_inline" but
2546 is not inlinable. This will allow flagging an error later in
2547 expand_call_inline in tree-inline.c. */
2548 if (lookup_attribute ("always_inline",
2549 DECL_ATTRIBUTES (callee->decl)) != NULL)
2550 inlined = true;
2551 continue;
2554 if (dump_file)
2555 fprintf (dump_file, " Inlining %s into %s (always_inline).\n",
2556 xstrdup_for_dump (e->callee->name ()),
2557 xstrdup_for_dump (e->caller->name ()));
2558 inline_call (e, true, NULL, NULL, false);
2559 inlined = true;
2561 if (inlined)
2562 inline_update_overall_summary (node);
2564 return inlined;
2567 /* Decide on the inlining. We do so in the topological order to avoid
2568 expenses on updating data structures. */
2570 static bool
2571 early_inline_small_functions (struct cgraph_node *node)
2573 struct cgraph_edge *e;
2574 bool inlined = false;
2576 for (e = node->callees; e; e = e->next_callee)
2578 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
2579 if (!inline_summaries->get (callee)->inlinable
2580 || !e->inline_failed)
2581 continue;
2583 /* Do not consider functions not declared inline. */
2584 if (!DECL_DECLARED_INLINE_P (callee->decl)
2585 && !opt_for_fn (node->decl, flag_inline_small_functions)
2586 && !opt_for_fn (node->decl, flag_inline_functions))
2587 continue;
2589 if (dump_file)
2590 fprintf (dump_file, "Considering inline candidate %s.\n",
2591 callee->name ());
2593 if (!can_early_inline_edge_p (e))
2594 continue;
2596 if (e->recursive_p ())
2598 if (dump_file)
2599 fprintf (dump_file, " Not inlining: recursive call.\n");
2600 continue;
2603 if (!want_early_inline_function_p (e))
2604 continue;
2606 if (dump_file)
2607 fprintf (dump_file, " Inlining %s into %s.\n",
2608 xstrdup_for_dump (callee->name ()),
2609 xstrdup_for_dump (e->caller->name ()));
2610 inline_call (e, true, NULL, NULL, true);
2611 inlined = true;
2614 return inlined;
2617 unsigned int
2618 early_inliner (function *fun)
2620 struct cgraph_node *node = cgraph_node::get (current_function_decl);
2621 struct cgraph_edge *edge;
2622 unsigned int todo = 0;
2623 int iterations = 0;
2624 bool inlined = false;
2626 if (seen_error ())
2627 return 0;
2629 /* Do nothing if datastructures for ipa-inliner are already computed. This
2630 happens when some pass decides to construct new function and
2631 cgraph_add_new_function calls lowering passes and early optimization on
2632 it. This may confuse ourself when early inliner decide to inline call to
2633 function clone, because function clones don't have parameter list in
2634 ipa-prop matching their signature. */
2635 if (ipa_node_params_sum)
2636 return 0;
2638 #ifdef ENABLE_CHECKING
2639 node->verify ();
2640 #endif
2641 node->remove_all_references ();
2643 /* Rebuild this reference because it dosn't depend on
2644 function's body and it's required to pass cgraph_node
2645 verification. */
2646 if (node->instrumented_version
2647 && !node->instrumentation_clone)
2648 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
2650 /* Even when not optimizing or not inlining inline always-inline
2651 functions. */
2652 inlined = inline_always_inline_functions (node);
2654 if (!optimize
2655 || flag_no_inline
2656 || !flag_early_inlining
2657 /* Never inline regular functions into always-inline functions
2658 during incremental inlining. This sucks as functions calling
2659 always inline functions will get less optimized, but at the
2660 same time inlining of functions calling always inline
2661 function into an always inline function might introduce
2662 cycles of edges to be always inlined in the callgraph.
2664 We might want to be smarter and just avoid this type of inlining. */
2665 || (DECL_DISREGARD_INLINE_LIMITS (node->decl)
2666 && lookup_attribute ("always_inline",
2667 DECL_ATTRIBUTES (node->decl))))
2669 else if (lookup_attribute ("flatten",
2670 DECL_ATTRIBUTES (node->decl)) != NULL)
2672 /* When the function is marked to be flattened, recursively inline
2673 all calls in it. */
2674 if (dump_file)
2675 fprintf (dump_file,
2676 "Flattening %s\n", node->name ());
2677 flatten_function (node, true);
2678 inlined = true;
2680 else
2682 /* If some always_inline functions was inlined, apply the changes.
2683 This way we will not account always inline into growth limits and
2684 moreover we will inline calls from always inlines that we skipped
2685 previously becuase of conditional above. */
2686 if (inlined)
2688 timevar_push (TV_INTEGRATION);
2689 todo |= optimize_inline_calls (current_function_decl);
2690 /* optimize_inline_calls call above might have introduced new
2691 statements that don't have inline parameters computed. */
2692 for (edge = node->callees; edge; edge = edge->next_callee)
2694 if (inline_edge_summary_vec.length () > (unsigned) edge->uid)
2696 struct inline_edge_summary *es = inline_edge_summary (edge);
2697 es->call_stmt_size
2698 = estimate_num_insns (edge->call_stmt, &eni_size_weights);
2699 es->call_stmt_time
2700 = estimate_num_insns (edge->call_stmt, &eni_time_weights);
2703 inline_update_overall_summary (node);
2704 inlined = false;
2705 timevar_pop (TV_INTEGRATION);
2707 /* We iterate incremental inlining to get trivial cases of indirect
2708 inlining. */
2709 while (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS)
2710 && early_inline_small_functions (node))
2712 timevar_push (TV_INTEGRATION);
2713 todo |= optimize_inline_calls (current_function_decl);
2715 /* Technically we ought to recompute inline parameters so the new
2716 iteration of early inliner works as expected. We however have
2717 values approximately right and thus we only need to update edge
2718 info that might be cleared out for newly discovered edges. */
2719 for (edge = node->callees; edge; edge = edge->next_callee)
2721 /* We have no summary for new bound store calls yet. */
2722 if (inline_edge_summary_vec.length () > (unsigned)edge->uid)
2724 struct inline_edge_summary *es = inline_edge_summary (edge);
2725 es->call_stmt_size
2726 = estimate_num_insns (edge->call_stmt, &eni_size_weights);
2727 es->call_stmt_time
2728 = estimate_num_insns (edge->call_stmt, &eni_time_weights);
2730 if (edge->callee->decl
2731 && !gimple_check_call_matching_types (
2732 edge->call_stmt, edge->callee->decl, false))
2733 edge->call_stmt_cannot_inline_p = true;
2735 if (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS) - 1)
2736 inline_update_overall_summary (node);
2737 timevar_pop (TV_INTEGRATION);
2738 iterations++;
2739 inlined = false;
2741 if (dump_file)
2742 fprintf (dump_file, "Iterations: %i\n", iterations);
2745 if (inlined)
2747 timevar_push (TV_INTEGRATION);
2748 todo |= optimize_inline_calls (current_function_decl);
2749 timevar_pop (TV_INTEGRATION);
2752 fun->always_inline_functions_inlined = true;
2754 return todo;
2757 /* Do inlining of small functions. Doing so early helps profiling and other
2758 passes to be somewhat more effective and avoids some code duplication in
2759 later real inlining pass for testcases with very many function calls. */
2761 namespace {
2763 const pass_data pass_data_early_inline =
2765 GIMPLE_PASS, /* type */
2766 "einline", /* name */
2767 OPTGROUP_INLINE, /* optinfo_flags */
2768 TV_EARLY_INLINING, /* tv_id */
2769 PROP_ssa, /* properties_required */
2770 0, /* properties_provided */
2771 0, /* properties_destroyed */
2772 0, /* todo_flags_start */
2773 0, /* todo_flags_finish */
2776 class pass_early_inline : public gimple_opt_pass
2778 public:
2779 pass_early_inline (gcc::context *ctxt)
2780 : gimple_opt_pass (pass_data_early_inline, ctxt)
2783 /* opt_pass methods: */
2784 virtual unsigned int execute (function *);
2786 }; // class pass_early_inline
2788 unsigned int
2789 pass_early_inline::execute (function *fun)
2791 return early_inliner (fun);
2794 } // anon namespace
2796 gimple_opt_pass *
2797 make_pass_early_inline (gcc::context *ctxt)
2799 return new pass_early_inline (ctxt);
2802 namespace {
2804 const pass_data pass_data_ipa_inline =
2806 IPA_PASS, /* type */
2807 "inline", /* name */
2808 OPTGROUP_INLINE, /* optinfo_flags */
2809 TV_IPA_INLINING, /* tv_id */
2810 0, /* properties_required */
2811 0, /* properties_provided */
2812 0, /* properties_destroyed */
2813 0, /* todo_flags_start */
2814 ( TODO_dump_symtab ), /* todo_flags_finish */
2817 class pass_ipa_inline : public ipa_opt_pass_d
2819 public:
2820 pass_ipa_inline (gcc::context *ctxt)
2821 : ipa_opt_pass_d (pass_data_ipa_inline, ctxt,
2822 inline_generate_summary, /* generate_summary */
2823 inline_write_summary, /* write_summary */
2824 inline_read_summary, /* read_summary */
2825 NULL, /* write_optimization_summary */
2826 NULL, /* read_optimization_summary */
2827 NULL, /* stmt_fixup */
2828 0, /* function_transform_todo_flags_start */
2829 inline_transform, /* function_transform */
2830 NULL) /* variable_transform */
2833 /* opt_pass methods: */
2834 virtual unsigned int execute (function *) { return ipa_inline (); }
2836 }; // class pass_ipa_inline
2838 } // anon namespace
2840 ipa_opt_pass_d *
2841 make_pass_ipa_inline (gcc::context *ctxt)
2843 return new pass_ipa_inline (ctxt);