* include/bits/basic_string.h (getline): Qualify call to prevent ADL
[official-gcc.git] / gcc / ipa-inline.c
blob5f1c9b04c6a282c440e626a3ecf3d1c563680b1a
1 /* Inlining decision heuristics.
2 Copyright (C) 2003-2014 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 "tree.h"
97 #include "trans-mem.h"
98 #include "calls.h"
99 #include "tree-inline.h"
100 #include "langhooks.h"
101 #include "flags.h"
102 #include "diagnostic.h"
103 #include "gimple-pretty-print.h"
104 #include "params.h"
105 #include "fibheap.h"
106 #include "intl.h"
107 #include "tree-pass.h"
108 #include "coverage.h"
109 #include "rtl.h"
110 #include "bitmap.h"
111 #include "basic-block.h"
112 #include "tree-ssa-alias.h"
113 #include "internal-fn.h"
114 #include "gimple-expr.h"
115 #include "is-a.h"
116 #include "gimple.h"
117 #include "gimple-ssa.h"
118 #include "ipa-prop.h"
119 #include "except.h"
120 #include "target.h"
121 #include "ipa-inline.h"
122 #include "ipa-utils.h"
123 #include "sreal.h"
124 #include "cilk.h"
125 #include "builtins.h"
127 /* Statistics we collect about inlining algorithm. */
128 static int overall_size;
129 static gcov_type max_count;
130 static sreal max_count_real, max_relbenefit_real, half_int_min_real;
131 static gcov_type spec_rem;
133 /* Return false when inlining edge E would lead to violating
134 limits on function unit growth or stack usage growth.
136 The relative function body growth limit is present generally
137 to avoid problems with non-linear behavior of the compiler.
138 To allow inlining huge functions into tiny wrapper, the limit
139 is always based on the bigger of the two functions considered.
141 For stack growth limits we always base the growth in stack usage
142 of the callers. We want to prevent applications from segfaulting
143 on stack overflow when functions with huge stack frames gets
144 inlined. */
146 static bool
147 caller_growth_limits (struct cgraph_edge *e)
149 struct cgraph_node *to = e->caller;
150 struct cgraph_node *what = e->callee->ultimate_alias_target ();
151 int newsize;
152 int limit = 0;
153 HOST_WIDE_INT stack_size_limit = 0, inlined_stack;
154 struct inline_summary *info, *what_info, *outer_info = inline_summary (to);
156 /* Look for function e->caller is inlined to. While doing
157 so work out the largest function body on the way. As
158 described above, we want to base our function growth
159 limits based on that. Not on the self size of the
160 outer function, not on the self size of inline code
161 we immediately inline to. This is the most relaxed
162 interpretation of the rule "do not grow large functions
163 too much in order to prevent compiler from exploding". */
164 while (true)
166 info = inline_summary (to);
167 if (limit < info->self_size)
168 limit = info->self_size;
169 if (stack_size_limit < info->estimated_self_stack_size)
170 stack_size_limit = info->estimated_self_stack_size;
171 if (to->global.inlined_to)
172 to = to->callers->caller;
173 else
174 break;
177 what_info = inline_summary (what);
179 if (limit < what_info->self_size)
180 limit = what_info->self_size;
182 limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
184 /* Check the size after inlining against the function limits. But allow
185 the function to shrink if it went over the limits by forced inlining. */
186 newsize = estimate_size_after_inlining (to, e);
187 if (newsize >= info->size
188 && newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
189 && newsize > limit)
191 e->inline_failed = CIF_LARGE_FUNCTION_GROWTH_LIMIT;
192 return false;
195 if (!what_info->estimated_stack_size)
196 return true;
198 /* FIXME: Stack size limit often prevents inlining in Fortran programs
199 due to large i/o datastructures used by the Fortran front-end.
200 We ought to ignore this limit when we know that the edge is executed
201 on every invocation of the caller (i.e. its call statement dominates
202 exit block). We do not track this information, yet. */
203 stack_size_limit += ((gcov_type)stack_size_limit
204 * PARAM_VALUE (PARAM_STACK_FRAME_GROWTH) / 100);
206 inlined_stack = (outer_info->stack_frame_offset
207 + outer_info->estimated_self_stack_size
208 + what_info->estimated_stack_size);
209 /* Check new stack consumption with stack consumption at the place
210 stack is used. */
211 if (inlined_stack > stack_size_limit
212 /* If function already has large stack usage from sibling
213 inline call, we can inline, too.
214 This bit overoptimistically assume that we are good at stack
215 packing. */
216 && inlined_stack > info->estimated_stack_size
217 && inlined_stack > PARAM_VALUE (PARAM_LARGE_STACK_FRAME))
219 e->inline_failed = CIF_LARGE_STACK_FRAME_GROWTH_LIMIT;
220 return false;
222 return true;
225 /* Dump info about why inlining has failed. */
227 static void
228 report_inline_failed_reason (struct cgraph_edge *e)
230 if (dump_file)
232 fprintf (dump_file, " not inlinable: %s/%i -> %s/%i, %s\n",
233 xstrdup (e->caller->name ()), e->caller->order,
234 xstrdup (e->callee->name ()), e->callee->order,
235 cgraph_inline_failed_string (e->inline_failed));
239 /* Decide whether sanitizer-related attributes allow inlining. */
241 static bool
242 sanitize_attrs_match_for_inline_p (const_tree caller, const_tree callee)
244 /* Don't care if sanitizer is disabled */
245 if (!(flag_sanitize & SANITIZE_ADDRESS))
246 return true;
248 if (!caller || !callee)
249 return true;
251 return !!lookup_attribute ("no_sanitize_address",
252 DECL_ATTRIBUTES (caller)) ==
253 !!lookup_attribute ("no_sanitize_address",
254 DECL_ATTRIBUTES (callee));
257 /* Decide if we can inline the edge and possibly update
258 inline_failed reason.
259 We check whether inlining is possible at all and whether
260 caller growth limits allow doing so.
262 if REPORT is true, output reason to the dump file.
264 if DISREGARD_LIMITS is true, ignore size limits.*/
266 static bool
267 can_inline_edge_p (struct cgraph_edge *e, bool report,
268 bool disregard_limits = false)
270 bool inlinable = true;
271 enum availability avail;
272 cgraph_node *callee = e->callee->ultimate_alias_target (&avail);
273 tree caller_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (e->caller->decl);
274 tree callee_tree
275 = callee ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (callee->decl) : NULL;
276 struct function *caller_cfun = DECL_STRUCT_FUNCTION (e->caller->decl);
277 struct function *callee_cfun
278 = callee ? DECL_STRUCT_FUNCTION (callee->decl) : NULL;
280 if (!caller_cfun && e->caller->clone_of)
281 caller_cfun = DECL_STRUCT_FUNCTION (e->caller->clone_of->decl);
283 if (!callee_cfun && callee && callee->clone_of)
284 callee_cfun = DECL_STRUCT_FUNCTION (callee->clone_of->decl);
286 gcc_assert (e->inline_failed);
288 if (!callee || !callee->definition)
290 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
291 inlinable = false;
293 else if (callee->calls_comdat_local)
295 e->inline_failed = CIF_USES_COMDAT_LOCAL;
296 inlinable = false;
298 else if (!inline_summary (callee)->inlinable
299 || (caller_cfun && fn_contains_cilk_spawn_p (caller_cfun)))
301 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
302 inlinable = false;
304 else if (avail <= AVAIL_INTERPOSABLE)
306 e->inline_failed = CIF_OVERWRITABLE;
307 inlinable = false;
309 else if (e->call_stmt_cannot_inline_p)
311 if (e->inline_failed != CIF_FUNCTION_NOT_OPTIMIZED)
312 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
313 inlinable = false;
315 /* Don't inline if the functions have different EH personalities. */
316 else if (DECL_FUNCTION_PERSONALITY (e->caller->decl)
317 && DECL_FUNCTION_PERSONALITY (callee->decl)
318 && (DECL_FUNCTION_PERSONALITY (e->caller->decl)
319 != DECL_FUNCTION_PERSONALITY (callee->decl)))
321 e->inline_failed = CIF_EH_PERSONALITY;
322 inlinable = false;
324 /* TM pure functions should not be inlined into non-TM_pure
325 functions. */
326 else if (is_tm_pure (callee->decl)
327 && !is_tm_pure (e->caller->decl))
329 e->inline_failed = CIF_UNSPECIFIED;
330 inlinable = false;
332 /* Don't inline if the callee can throw non-call exceptions but the
333 caller cannot.
334 FIXME: this is obviously wrong for LTO where STRUCT_FUNCTION is missing.
335 Move the flag into cgraph node or mirror it in the inline summary. */
336 else if (callee_cfun && callee_cfun->can_throw_non_call_exceptions
337 && !(caller_cfun && caller_cfun->can_throw_non_call_exceptions))
339 e->inline_failed = CIF_NON_CALL_EXCEPTIONS;
340 inlinable = false;
342 /* Check compatibility of target optimization options. */
343 else if (!targetm.target_option.can_inline_p (e->caller->decl,
344 callee->decl))
346 e->inline_failed = CIF_TARGET_OPTION_MISMATCH;
347 inlinable = false;
349 /* Don't inline a function with mismatched sanitization attributes. */
350 else if (!sanitize_attrs_match_for_inline_p (e->caller->decl, callee->decl))
352 e->inline_failed = CIF_ATTRIBUTE_MISMATCH;
353 inlinable = false;
355 /* Check if caller growth allows the inlining. */
356 else if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl)
357 && !disregard_limits
358 && !lookup_attribute ("flatten",
359 DECL_ATTRIBUTES
360 (e->caller->global.inlined_to
361 ? e->caller->global.inlined_to->decl
362 : e->caller->decl))
363 && !caller_growth_limits (e))
364 inlinable = false;
365 /* Don't inline a function with a higher optimization level than the
366 caller. FIXME: this is really just tip of iceberg of handling
367 optimization attribute. */
368 else if (caller_tree != callee_tree)
370 struct cl_optimization *caller_opt
371 = TREE_OPTIMIZATION ((caller_tree)
372 ? caller_tree
373 : optimization_default_node);
375 struct cl_optimization *callee_opt
376 = TREE_OPTIMIZATION ((callee_tree)
377 ? callee_tree
378 : optimization_default_node);
380 if (((caller_opt->x_optimize > callee_opt->x_optimize)
381 || (caller_opt->x_optimize_size != callee_opt->x_optimize_size))
382 /* gcc.dg/pr43564.c. Look at forced inline even in -O0. */
383 && !DECL_DISREGARD_INLINE_LIMITS (e->callee->decl))
385 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
386 inlinable = false;
390 if (!inlinable && report)
391 report_inline_failed_reason (e);
392 return inlinable;
396 /* Return true if the edge E is inlinable during early inlining. */
398 static bool
399 can_early_inline_edge_p (struct cgraph_edge *e)
401 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
402 /* Early inliner might get called at WPA stage when IPA pass adds new
403 function. In this case we can not really do any of early inlining
404 because function bodies are missing. */
405 if (!gimple_has_body_p (callee->decl))
407 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
408 return false;
410 /* In early inliner some of callees may not be in SSA form yet
411 (i.e. the callgraph is cyclic and we did not process
412 the callee by early inliner, yet). We don't have CIF code for this
413 case; later we will re-do the decision in the real inliner. */
414 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->caller->decl))
415 || !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
417 if (dump_file)
418 fprintf (dump_file, " edge not inlinable: not in SSA form\n");
419 return false;
421 if (!can_inline_edge_p (e, true))
422 return false;
423 return true;
427 /* Return number of calls in N. Ignore cheap builtins. */
429 static int
430 num_calls (struct cgraph_node *n)
432 struct cgraph_edge *e;
433 int num = 0;
435 for (e = n->callees; e; e = e->next_callee)
436 if (!is_inexpensive_builtin (e->callee->decl))
437 num++;
438 return num;
442 /* Return true if we are interested in inlining small function. */
444 static bool
445 want_early_inline_function_p (struct cgraph_edge *e)
447 bool want_inline = true;
448 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
450 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
452 else if (!DECL_DECLARED_INLINE_P (callee->decl)
453 && !flag_inline_small_functions)
455 e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
456 report_inline_failed_reason (e);
457 want_inline = false;
459 else
461 int growth = estimate_edge_growth (e);
462 int n;
464 if (growth <= 0)
466 else if (!cgraph_maybe_hot_edge_p (e)
467 && growth > 0)
469 if (dump_file)
470 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
471 "call is cold and code would grow by %i\n",
472 xstrdup (e->caller->name ()),
473 e->caller->order,
474 xstrdup (callee->name ()), callee->order,
475 growth);
476 want_inline = false;
478 else if (growth > PARAM_VALUE (PARAM_EARLY_INLINING_INSNS))
480 if (dump_file)
481 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
482 "growth %i exceeds --param early-inlining-insns\n",
483 xstrdup (e->caller->name ()),
484 e->caller->order,
485 xstrdup (callee->name ()), callee->order,
486 growth);
487 want_inline = false;
489 else if ((n = num_calls (callee)) != 0
490 && growth * (n + 1) > PARAM_VALUE (PARAM_EARLY_INLINING_INSNS))
492 if (dump_file)
493 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
494 "growth %i exceeds --param early-inlining-insns "
495 "divided by number of calls\n",
496 xstrdup (e->caller->name ()),
497 e->caller->order,
498 xstrdup (callee->name ()), callee->order,
499 growth);
500 want_inline = false;
503 return want_inline;
506 /* Compute time of the edge->caller + edge->callee execution when inlining
507 does not happen. */
509 inline gcov_type
510 compute_uninlined_call_time (struct inline_summary *callee_info,
511 struct cgraph_edge *edge)
513 gcov_type uninlined_call_time =
514 RDIV ((gcov_type)callee_info->time * MAX (edge->frequency, 1),
515 CGRAPH_FREQ_BASE);
516 gcov_type caller_time = inline_summary (edge->caller->global.inlined_to
517 ? edge->caller->global.inlined_to
518 : edge->caller)->time;
519 return uninlined_call_time + caller_time;
522 /* Same as compute_uinlined_call_time but compute time when inlining
523 does happen. */
525 inline gcov_type
526 compute_inlined_call_time (struct cgraph_edge *edge,
527 int edge_time)
529 gcov_type caller_time = inline_summary (edge->caller->global.inlined_to
530 ? edge->caller->global.inlined_to
531 : edge->caller)->time;
532 gcov_type time = (caller_time
533 + RDIV (((gcov_type) edge_time
534 - inline_edge_summary (edge)->call_stmt_time)
535 * MAX (edge->frequency, 1), CGRAPH_FREQ_BASE));
536 /* Possible one roundoff error, but watch for overflows. */
537 gcc_checking_assert (time >= INT_MIN / 2);
538 if (time < 0)
539 time = 0;
540 return time;
543 /* Return true if the speedup for inlining E is bigger than
544 PARAM_MAX_INLINE_MIN_SPEEDUP. */
546 static bool
547 big_speedup_p (struct cgraph_edge *e)
549 gcov_type time = compute_uninlined_call_time (inline_summary (e->callee),
551 gcov_type inlined_time = compute_inlined_call_time (e,
552 estimate_edge_time (e));
553 if (time - inlined_time
554 > RDIV (time * PARAM_VALUE (PARAM_INLINE_MIN_SPEEDUP), 100))
555 return true;
556 return false;
559 /* Return true if we are interested in inlining small function.
560 When REPORT is true, report reason to dump file. */
562 static bool
563 want_inline_small_function_p (struct cgraph_edge *e, bool report)
565 bool want_inline = true;
566 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
568 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
570 else if (!DECL_DECLARED_INLINE_P (callee->decl)
571 && !flag_inline_small_functions)
573 e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
574 want_inline = false;
576 /* Do fast and conservative check if the function can be good
577 inline cnadidate. At themoment we allow inline hints to
578 promote non-inline function to inline and we increase
579 MAX_INLINE_INSNS_SINGLE 16fold for inline functions. */
580 else if ((!DECL_DECLARED_INLINE_P (callee->decl)
581 && (!e->count || !cgraph_maybe_hot_edge_p (e)))
582 && inline_summary (callee)->min_size - inline_edge_summary (e)->call_stmt_size
583 > MAX (MAX_INLINE_INSNS_SINGLE, MAX_INLINE_INSNS_AUTO))
585 e->inline_failed = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
586 want_inline = false;
588 else if ((DECL_DECLARED_INLINE_P (callee->decl) || e->count)
589 && inline_summary (callee)->min_size - inline_edge_summary (e)->call_stmt_size
590 > 16 * MAX_INLINE_INSNS_SINGLE)
592 e->inline_failed = (DECL_DECLARED_INLINE_P (callee->decl)
593 ? CIF_MAX_INLINE_INSNS_SINGLE_LIMIT
594 : CIF_MAX_INLINE_INSNS_AUTO_LIMIT);
595 want_inline = false;
597 else
599 int growth = estimate_edge_growth (e);
600 inline_hints hints = estimate_edge_hints (e);
601 bool big_speedup = big_speedup_p (e);
603 if (growth <= 0)
605 /* Apply MAX_INLINE_INSNS_SINGLE limit. Do not do so when
606 hints suggests that inlining given function is very profitable. */
607 else if (DECL_DECLARED_INLINE_P (callee->decl)
608 && growth >= MAX_INLINE_INSNS_SINGLE
609 && ((!big_speedup
610 && !(hints & (INLINE_HINT_indirect_call
611 | INLINE_HINT_known_hot
612 | INLINE_HINT_loop_iterations
613 | INLINE_HINT_array_index
614 | INLINE_HINT_loop_stride)))
615 || growth >= MAX_INLINE_INSNS_SINGLE * 16))
617 e->inline_failed = CIF_MAX_INLINE_INSNS_SINGLE_LIMIT;
618 want_inline = false;
620 else if (!DECL_DECLARED_INLINE_P (callee->decl)
621 && !flag_inline_functions)
623 /* growth_likely_positive is expensive, always test it last. */
624 if (growth >= MAX_INLINE_INSNS_SINGLE
625 || growth_likely_positive (callee, growth))
627 e->inline_failed = CIF_NOT_DECLARED_INLINED;
628 want_inline = false;
631 /* Apply MAX_INLINE_INSNS_AUTO limit for functions not declared inline
632 Upgrade it to MAX_INLINE_INSNS_SINGLE when hints suggests that
633 inlining given function is very profitable. */
634 else if (!DECL_DECLARED_INLINE_P (callee->decl)
635 && !big_speedup
636 && !(hints & INLINE_HINT_known_hot)
637 && growth >= ((hints & (INLINE_HINT_indirect_call
638 | INLINE_HINT_loop_iterations
639 | INLINE_HINT_array_index
640 | INLINE_HINT_loop_stride))
641 ? MAX (MAX_INLINE_INSNS_AUTO,
642 MAX_INLINE_INSNS_SINGLE)
643 : MAX_INLINE_INSNS_AUTO))
645 /* growth_likely_positive is expensive, always test it last. */
646 if (growth >= MAX_INLINE_INSNS_SINGLE
647 || growth_likely_positive (callee, growth))
649 e->inline_failed = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
650 want_inline = false;
653 /* If call is cold, do not inline when function body would grow. */
654 else if (!cgraph_maybe_hot_edge_p (e)
655 && (growth >= MAX_INLINE_INSNS_SINGLE
656 || growth_likely_positive (callee, growth)))
658 e->inline_failed = CIF_UNLIKELY_CALL;
659 want_inline = false;
662 if (!want_inline && report)
663 report_inline_failed_reason (e);
664 return want_inline;
667 /* EDGE is self recursive edge.
668 We hand two cases - when function A is inlining into itself
669 or when function A is being inlined into another inliner copy of function
670 A within function B.
672 In first case OUTER_NODE points to the toplevel copy of A, while
673 in the second case OUTER_NODE points to the outermost copy of A in B.
675 In both cases we want to be extra selective since
676 inlining the call will just introduce new recursive calls to appear. */
678 static bool
679 want_inline_self_recursive_call_p (struct cgraph_edge *edge,
680 struct cgraph_node *outer_node,
681 bool peeling,
682 int depth)
684 char const *reason = NULL;
685 bool want_inline = true;
686 int caller_freq = CGRAPH_FREQ_BASE;
687 int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO);
689 if (DECL_DECLARED_INLINE_P (edge->caller->decl))
690 max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH);
692 if (!cgraph_maybe_hot_edge_p (edge))
694 reason = "recursive call is cold";
695 want_inline = false;
697 else if (max_count && !outer_node->count)
699 reason = "not executed in profile";
700 want_inline = false;
702 else if (depth > max_depth)
704 reason = "--param max-inline-recursive-depth exceeded.";
705 want_inline = false;
708 if (outer_node->global.inlined_to)
709 caller_freq = outer_node->callers->frequency;
711 if (!caller_freq)
713 reason = "function is inlined and unlikely";
714 want_inline = false;
717 if (!want_inline)
719 /* Inlining of self recursive function into copy of itself within other function
720 is transformation similar to loop peeling.
722 Peeling is profitable if we can inline enough copies to make probability
723 of actual call to the self recursive function very small. Be sure that
724 the probability of recursion is small.
726 We ensure that the frequency of recursing is at most 1 - (1/max_depth).
727 This way the expected number of recision is at most max_depth. */
728 else if (peeling)
730 int max_prob = CGRAPH_FREQ_BASE - ((CGRAPH_FREQ_BASE + max_depth - 1)
731 / max_depth);
732 int i;
733 for (i = 1; i < depth; i++)
734 max_prob = max_prob * max_prob / CGRAPH_FREQ_BASE;
735 if (max_count
736 && (edge->count * CGRAPH_FREQ_BASE / outer_node->count
737 >= max_prob))
739 reason = "profile of recursive call is too large";
740 want_inline = false;
742 if (!max_count
743 && (edge->frequency * CGRAPH_FREQ_BASE / caller_freq
744 >= max_prob))
746 reason = "frequency of recursive call is too large";
747 want_inline = false;
750 /* Recursive inlining, i.e. equivalent of unrolling, is profitable if recursion
751 depth is large. We reduce function call overhead and increase chances that
752 things fit in hardware return predictor.
754 Recursive inlining might however increase cost of stack frame setup
755 actually slowing down functions whose recursion tree is wide rather than
756 deep.
758 Deciding reliably on when to do recursive inlining without profile feedback
759 is tricky. For now we disable recursive inlining when probability of self
760 recursion is low.
762 Recursive inlining of self recursive call within loop also results in large loop
763 depths that generally optimize badly. We may want to throttle down inlining
764 in those cases. In particular this seems to happen in one of libstdc++ rb tree
765 methods. */
766 else
768 if (max_count
769 && (edge->count * 100 / outer_node->count
770 <= PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY)))
772 reason = "profile of recursive call is too small";
773 want_inline = false;
775 else if (!max_count
776 && (edge->frequency * 100 / caller_freq
777 <= PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY)))
779 reason = "frequency of recursive call is too small";
780 want_inline = false;
783 if (!want_inline && dump_file)
784 fprintf (dump_file, " not inlining recursively: %s\n", reason);
785 return want_inline;
788 /* Return true when NODE has uninlinable caller;
789 set HAS_HOT_CALL if it has hot call.
790 Worker for cgraph_for_node_and_aliases. */
792 static bool
793 check_callers (struct cgraph_node *node, void *has_hot_call)
795 struct cgraph_edge *e;
796 for (e = node->callers; e; e = e->next_caller)
798 if (!can_inline_edge_p (e, true))
799 return true;
800 if (!(*(bool *)has_hot_call) && cgraph_maybe_hot_edge_p (e))
801 *(bool *)has_hot_call = true;
803 return false;
806 /* If NODE has a caller, return true. */
808 static bool
809 has_caller_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
811 if (node->callers)
812 return true;
813 return false;
816 /* Decide if inlining NODE would reduce unit size by eliminating
817 the offline copy of function.
818 When COLD is true the cold calls are considered, too. */
820 static bool
821 want_inline_function_to_all_callers_p (struct cgraph_node *node, bool cold)
823 struct cgraph_node *function = node->ultimate_alias_target ();
824 bool has_hot_call = false;
826 /* Does it have callers? */
827 if (!node->call_for_symbol_thunks_and_aliases (has_caller_p, NULL, true))
828 return false;
829 /* Already inlined? */
830 if (function->global.inlined_to)
831 return false;
832 if (node->ultimate_alias_target () != node)
833 return false;
834 /* Inlining into all callers would increase size? */
835 if (estimate_growth (node) > 0)
836 return false;
837 /* All inlines must be possible. */
838 if (node->call_for_symbol_thunks_and_aliases
839 (check_callers, &has_hot_call, true))
840 return false;
841 if (!cold && !has_hot_call)
842 return false;
843 return true;
846 #define RELATIVE_TIME_BENEFIT_RANGE (INT_MAX / 64)
848 /* Return relative time improvement for inlining EDGE in range
849 1...RELATIVE_TIME_BENEFIT_RANGE */
851 static inline int
852 relative_time_benefit (struct inline_summary *callee_info,
853 struct cgraph_edge *edge,
854 int edge_time)
856 gcov_type relbenefit;
857 gcov_type uninlined_call_time = compute_uninlined_call_time (callee_info, edge);
858 gcov_type inlined_call_time = compute_inlined_call_time (edge, edge_time);
860 /* Inlining into extern inline function is not a win. */
861 if (DECL_EXTERNAL (edge->caller->global.inlined_to
862 ? edge->caller->global.inlined_to->decl
863 : edge->caller->decl))
864 return 1;
866 /* Watch overflows. */
867 gcc_checking_assert (uninlined_call_time >= 0);
868 gcc_checking_assert (inlined_call_time >= 0);
869 gcc_checking_assert (uninlined_call_time >= inlined_call_time);
871 /* Compute relative time benefit, i.e. how much the call becomes faster.
872 ??? perhaps computing how much the caller+calle together become faster
873 would lead to more realistic results. */
874 if (!uninlined_call_time)
875 uninlined_call_time = 1;
876 relbenefit =
877 RDIV (((gcov_type)uninlined_call_time - inlined_call_time) * RELATIVE_TIME_BENEFIT_RANGE,
878 uninlined_call_time);
879 relbenefit = MIN (relbenefit, RELATIVE_TIME_BENEFIT_RANGE);
880 gcc_checking_assert (relbenefit >= 0);
881 relbenefit = MAX (relbenefit, 1);
882 return relbenefit;
886 /* A cost model driving the inlining heuristics in a way so the edges with
887 smallest badness are inlined first. After each inlining is performed
888 the costs of all caller edges of nodes affected are recomputed so the
889 metrics may accurately depend on values such as number of inlinable callers
890 of the function or function body size. */
892 static int
893 edge_badness (struct cgraph_edge *edge, bool dump)
895 gcov_type badness;
896 int growth, edge_time;
897 struct cgraph_node *callee = edge->callee->ultimate_alias_target ();
898 struct inline_summary *callee_info = inline_summary (callee);
899 inline_hints hints;
901 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
902 return INT_MIN;
904 growth = estimate_edge_growth (edge);
905 edge_time = estimate_edge_time (edge);
906 hints = estimate_edge_hints (edge);
907 gcc_checking_assert (edge_time >= 0);
908 gcc_checking_assert (edge_time <= callee_info->time);
909 gcc_checking_assert (growth <= callee_info->size);
911 if (dump)
913 fprintf (dump_file, " Badness calculation for %s/%i -> %s/%i\n",
914 xstrdup (edge->caller->name ()),
915 edge->caller->order,
916 xstrdup (callee->name ()),
917 edge->callee->order);
918 fprintf (dump_file, " size growth %i, time %i ",
919 growth,
920 edge_time);
921 dump_inline_hints (dump_file, hints);
922 if (big_speedup_p (edge))
923 fprintf (dump_file, " big_speedup");
924 fprintf (dump_file, "\n");
927 /* Always prefer inlining saving code size. */
928 if (growth <= 0)
930 badness = INT_MIN / 2 + growth;
931 if (dump)
932 fprintf (dump_file, " %i: Growth %i <= 0\n", (int) badness,
933 growth);
936 /* When profiling is available, compute badness as:
938 relative_edge_count * relative_time_benefit
939 goodness = -------------------------------------------
940 growth_f_caller
941 badness = -goodness
943 The fraction is upside down, because on edge counts and time beneits
944 the bounds are known. Edge growth is essentially unlimited. */
946 else if (max_count)
948 sreal tmp, relbenefit_real, growth_real;
949 int relbenefit = relative_time_benefit (callee_info, edge, edge_time);
950 /* Capping edge->count to max_count. edge->count can be larger than
951 max_count if an inline adds new edges which increase max_count
952 after max_count is computed. */
953 gcov_type edge_count = edge->count > max_count ? max_count : edge->count;
955 sreal_init (&relbenefit_real, relbenefit, 0);
956 sreal_init (&growth_real, growth, 0);
958 /* relative_edge_count. */
959 sreal_init (&tmp, edge_count, 0);
960 sreal_div (&tmp, &tmp, &max_count_real);
962 /* relative_time_benefit. */
963 sreal_mul (&tmp, &tmp, &relbenefit_real);
964 sreal_div (&tmp, &tmp, &max_relbenefit_real);
966 /* growth_f_caller. */
967 sreal_mul (&tmp, &tmp, &half_int_min_real);
968 sreal_div (&tmp, &tmp, &growth_real);
970 badness = -1 * sreal_to_int (&tmp);
972 if (dump)
974 fprintf (dump_file,
975 " %i (relative %f): profile info. Relative count %f%s"
976 " * Relative benefit %f\n",
977 (int) badness, (double) badness / INT_MIN,
978 (double) edge_count / max_count,
979 edge->count > max_count ? " (capped to max_count)" : "",
980 relbenefit * 100.0 / RELATIVE_TIME_BENEFIT_RANGE);
984 /* When function local profile is available. Compute badness as:
986 relative_time_benefit
987 goodness = ---------------------------------
988 growth_of_caller * overall_growth
990 badness = - goodness
992 compensated by the inline hints.
994 else if (flag_guess_branch_prob)
996 badness = (relative_time_benefit (callee_info, edge, edge_time)
997 * (INT_MIN / 16 / RELATIVE_TIME_BENEFIT_RANGE));
998 badness /= (MIN (65536/2, growth) * MIN (65536/2, MAX (1, callee_info->growth)));
999 gcc_checking_assert (badness <=0 && badness >= INT_MIN / 16);
1000 if ((hints & (INLINE_HINT_indirect_call
1001 | INLINE_HINT_loop_iterations
1002 | INLINE_HINT_array_index
1003 | INLINE_HINT_loop_stride))
1004 || callee_info->growth <= 0)
1005 badness *= 8;
1006 if (hints & (INLINE_HINT_same_scc))
1007 badness /= 16;
1008 else if (hints & (INLINE_HINT_in_scc))
1009 badness /= 8;
1010 else if (hints & (INLINE_HINT_cross_module))
1011 badness /= 2;
1012 gcc_checking_assert (badness <= 0 && badness >= INT_MIN / 2);
1013 if ((hints & INLINE_HINT_declared_inline) && badness >= INT_MIN / 32)
1014 badness *= 16;
1015 if (dump)
1017 fprintf (dump_file,
1018 " %i: guessed profile. frequency %f,"
1019 " benefit %f%%, time w/o inlining %i, time w inlining %i"
1020 " overall growth %i (current) %i (original)\n",
1021 (int) badness, (double)edge->frequency / CGRAPH_FREQ_BASE,
1022 relative_time_benefit (callee_info, edge, edge_time) * 100.0
1023 / RELATIVE_TIME_BENEFIT_RANGE,
1024 (int)compute_uninlined_call_time (callee_info, edge),
1025 (int)compute_inlined_call_time (edge, edge_time),
1026 estimate_growth (callee),
1027 callee_info->growth);
1030 /* When function local profile is not available or it does not give
1031 useful information (ie frequency is zero), base the cost on
1032 loop nest and overall size growth, so we optimize for overall number
1033 of functions fully inlined in program. */
1034 else
1036 int nest = MIN (inline_edge_summary (edge)->loop_depth, 8);
1037 badness = growth * 256;
1039 /* Decrease badness if call is nested. */
1040 if (badness > 0)
1041 badness >>= nest;
1042 else
1044 badness <<= nest;
1046 if (dump)
1047 fprintf (dump_file, " %i: no profile. nest %i\n", (int) badness,
1048 nest);
1051 /* Ensure that we did not overflow in all the fixed point math above. */
1052 gcc_assert (badness >= INT_MIN);
1053 gcc_assert (badness <= INT_MAX - 1);
1054 /* Make recursive inlining happen always after other inlining is done. */
1055 if (cgraph_edge_recursive_p (edge))
1056 return badness + 1;
1057 else
1058 return badness;
1061 /* Recompute badness of EDGE and update its key in HEAP if needed. */
1062 static inline void
1063 update_edge_key (fibheap_t heap, struct cgraph_edge *edge)
1065 int badness = edge_badness (edge, false);
1066 if (edge->aux)
1068 fibnode_t n = (fibnode_t) edge->aux;
1069 gcc_checking_assert (n->data == edge);
1071 /* fibheap_replace_key only decrease the keys.
1072 When we increase the key we do not update heap
1073 and instead re-insert the element once it becomes
1074 a minimum of heap. */
1075 if (badness < n->key)
1077 if (dump_file && (dump_flags & TDF_DETAILS))
1079 fprintf (dump_file,
1080 " decreasing badness %s/%i -> %s/%i, %i to %i\n",
1081 xstrdup (edge->caller->name ()),
1082 edge->caller->order,
1083 xstrdup (edge->callee->name ()),
1084 edge->callee->order,
1085 (int)n->key,
1086 badness);
1088 fibheap_replace_key (heap, n, badness);
1089 gcc_checking_assert (n->key == badness);
1092 else
1094 if (dump_file && (dump_flags & TDF_DETAILS))
1096 fprintf (dump_file,
1097 " enqueuing call %s/%i -> %s/%i, badness %i\n",
1098 xstrdup (edge->caller->name ()),
1099 edge->caller->order,
1100 xstrdup (edge->callee->name ()),
1101 edge->callee->order,
1102 badness);
1104 edge->aux = fibheap_insert (heap, badness, edge);
1109 /* NODE was inlined.
1110 All caller edges needs to be resetted because
1111 size estimates change. Similarly callees needs reset
1112 because better context may be known. */
1114 static void
1115 reset_edge_caches (struct cgraph_node *node)
1117 struct cgraph_edge *edge;
1118 struct cgraph_edge *e = node->callees;
1119 struct cgraph_node *where = node;
1120 struct ipa_ref *ref;
1122 if (where->global.inlined_to)
1123 where = where->global.inlined_to;
1125 /* WHERE body size has changed, the cached growth is invalid. */
1126 reset_node_growth_cache (where);
1128 for (edge = where->callers; edge; edge = edge->next_caller)
1129 if (edge->inline_failed)
1130 reset_edge_growth_cache (edge);
1132 FOR_EACH_ALIAS (where, ref)
1133 reset_edge_caches (dyn_cast <cgraph_node *> (ref->referring));
1135 if (!e)
1136 return;
1138 while (true)
1139 if (!e->inline_failed && e->callee->callees)
1140 e = e->callee->callees;
1141 else
1143 if (e->inline_failed)
1144 reset_edge_growth_cache (e);
1145 if (e->next_callee)
1146 e = e->next_callee;
1147 else
1151 if (e->caller == node)
1152 return;
1153 e = e->caller->callers;
1155 while (!e->next_callee);
1156 e = e->next_callee;
1161 /* Recompute HEAP nodes for each of caller of NODE.
1162 UPDATED_NODES track nodes we already visited, to avoid redundant work.
1163 When CHECK_INLINABLITY_FOR is set, re-check for specified edge that
1164 it is inlinable. Otherwise check all edges. */
1166 static void
1167 update_caller_keys (fibheap_t heap, struct cgraph_node *node,
1168 bitmap updated_nodes,
1169 struct cgraph_edge *check_inlinablity_for)
1171 struct cgraph_edge *edge;
1172 struct ipa_ref *ref;
1174 if ((!node->alias && !inline_summary (node)->inlinable)
1175 || node->global.inlined_to)
1176 return;
1177 if (!bitmap_set_bit (updated_nodes, node->uid))
1178 return;
1180 FOR_EACH_ALIAS (node, ref)
1182 struct cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1183 update_caller_keys (heap, alias, updated_nodes, check_inlinablity_for);
1186 for (edge = node->callers; edge; edge = edge->next_caller)
1187 if (edge->inline_failed)
1189 if (!check_inlinablity_for
1190 || check_inlinablity_for == edge)
1192 if (can_inline_edge_p (edge, false)
1193 && want_inline_small_function_p (edge, false))
1194 update_edge_key (heap, edge);
1195 else if (edge->aux)
1197 report_inline_failed_reason (edge);
1198 fibheap_delete_node (heap, (fibnode_t) edge->aux);
1199 edge->aux = NULL;
1202 else if (edge->aux)
1203 update_edge_key (heap, edge);
1207 /* Recompute HEAP nodes for each uninlined call in NODE.
1208 This is used when we know that edge badnesses are going only to increase
1209 (we introduced new call site) and thus all we need is to insert newly
1210 created edges into heap. */
1212 static void
1213 update_callee_keys (fibheap_t heap, struct cgraph_node *node,
1214 bitmap updated_nodes)
1216 struct cgraph_edge *e = node->callees;
1218 if (!e)
1219 return;
1220 while (true)
1221 if (!e->inline_failed && e->callee->callees)
1222 e = e->callee->callees;
1223 else
1225 enum availability avail;
1226 struct cgraph_node *callee;
1227 /* We do not reset callee growth cache here. Since we added a new call,
1228 growth chould have just increased and consequentely badness metric
1229 don't need updating. */
1230 if (e->inline_failed
1231 && (callee = e->callee->ultimate_alias_target (&avail))
1232 && inline_summary (callee)->inlinable
1233 && avail >= AVAIL_AVAILABLE
1234 && !bitmap_bit_p (updated_nodes, callee->uid))
1236 if (can_inline_edge_p (e, false)
1237 && want_inline_small_function_p (e, false))
1238 update_edge_key (heap, e);
1239 else if (e->aux)
1241 report_inline_failed_reason (e);
1242 fibheap_delete_node (heap, (fibnode_t) e->aux);
1243 e->aux = NULL;
1246 if (e->next_callee)
1247 e = e->next_callee;
1248 else
1252 if (e->caller == node)
1253 return;
1254 e = e->caller->callers;
1256 while (!e->next_callee);
1257 e = e->next_callee;
1262 /* Enqueue all recursive calls from NODE into priority queue depending on
1263 how likely we want to recursively inline the call. */
1265 static void
1266 lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
1267 fibheap_t heap)
1269 struct cgraph_edge *e;
1270 enum availability avail;
1272 for (e = where->callees; e; e = e->next_callee)
1273 if (e->callee == node
1274 || (e->callee->ultimate_alias_target (&avail) == node
1275 && avail > AVAIL_INTERPOSABLE))
1277 /* When profile feedback is available, prioritize by expected number
1278 of calls. */
1279 fibheap_insert (heap,
1280 !max_count ? -e->frequency
1281 : -(e->count / ((max_count + (1<<24) - 1) / (1<<24))),
1284 for (e = where->callees; e; e = e->next_callee)
1285 if (!e->inline_failed)
1286 lookup_recursive_calls (node, e->callee, heap);
1289 /* Decide on recursive inlining: in the case function has recursive calls,
1290 inline until body size reaches given argument. If any new indirect edges
1291 are discovered in the process, add them to *NEW_EDGES, unless NEW_EDGES
1292 is NULL. */
1294 static bool
1295 recursive_inlining (struct cgraph_edge *edge,
1296 vec<cgraph_edge *> *new_edges)
1298 int limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO);
1299 fibheap_t heap;
1300 struct cgraph_node *node;
1301 struct cgraph_edge *e;
1302 struct cgraph_node *master_clone = NULL, *next;
1303 int depth = 0;
1304 int n = 0;
1306 node = edge->caller;
1307 if (node->global.inlined_to)
1308 node = node->global.inlined_to;
1310 if (DECL_DECLARED_INLINE_P (node->decl))
1311 limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE);
1313 /* Make sure that function is small enough to be considered for inlining. */
1314 if (estimate_size_after_inlining (node, edge) >= limit)
1315 return false;
1316 heap = fibheap_new ();
1317 lookup_recursive_calls (node, node, heap);
1318 if (fibheap_empty (heap))
1320 fibheap_delete (heap);
1321 return false;
1324 if (dump_file)
1325 fprintf (dump_file,
1326 " Performing recursive inlining on %s\n",
1327 node->name ());
1329 /* Do the inlining and update list of recursive call during process. */
1330 while (!fibheap_empty (heap))
1332 struct cgraph_edge *curr
1333 = (struct cgraph_edge *) fibheap_extract_min (heap);
1334 struct cgraph_node *cnode, *dest = curr->callee;
1336 if (!can_inline_edge_p (curr, true))
1337 continue;
1339 /* MASTER_CLONE is produced in the case we already started modified
1340 the function. Be sure to redirect edge to the original body before
1341 estimating growths otherwise we will be seeing growths after inlining
1342 the already modified body. */
1343 if (master_clone)
1345 cgraph_redirect_edge_callee (curr, master_clone);
1346 reset_edge_growth_cache (curr);
1349 if (estimate_size_after_inlining (node, curr) > limit)
1351 cgraph_redirect_edge_callee (curr, dest);
1352 reset_edge_growth_cache (curr);
1353 break;
1356 depth = 1;
1357 for (cnode = curr->caller;
1358 cnode->global.inlined_to; cnode = cnode->callers->caller)
1359 if (node->decl
1360 == curr->callee->ultimate_alias_target ()->decl)
1361 depth++;
1363 if (!want_inline_self_recursive_call_p (curr, node, false, depth))
1365 cgraph_redirect_edge_callee (curr, dest);
1366 reset_edge_growth_cache (curr);
1367 continue;
1370 if (dump_file)
1372 fprintf (dump_file,
1373 " Inlining call of depth %i", depth);
1374 if (node->count)
1376 fprintf (dump_file, " called approx. %.2f times per call",
1377 (double)curr->count / node->count);
1379 fprintf (dump_file, "\n");
1381 if (!master_clone)
1383 /* We need original clone to copy around. */
1384 master_clone = node->create_clone (node->decl, node->count,
1385 CGRAPH_FREQ_BASE, false, vNULL,
1386 true, NULL, NULL);
1387 for (e = master_clone->callees; e; e = e->next_callee)
1388 if (!e->inline_failed)
1389 clone_inlined_nodes (e, true, false, NULL, CGRAPH_FREQ_BASE);
1390 cgraph_redirect_edge_callee (curr, master_clone);
1391 reset_edge_growth_cache (curr);
1394 inline_call (curr, false, new_edges, &overall_size, true);
1395 lookup_recursive_calls (node, curr->callee, heap);
1396 n++;
1399 if (!fibheap_empty (heap) && dump_file)
1400 fprintf (dump_file, " Recursive inlining growth limit met.\n");
1401 fibheap_delete (heap);
1403 if (!master_clone)
1404 return false;
1406 if (dump_file)
1407 fprintf (dump_file,
1408 "\n Inlined %i times, "
1409 "body grown from size %i to %i, time %i to %i\n", n,
1410 inline_summary (master_clone)->size, inline_summary (node)->size,
1411 inline_summary (master_clone)->time, inline_summary (node)->time);
1413 /* Remove master clone we used for inlining. We rely that clones inlined
1414 into master clone gets queued just before master clone so we don't
1415 need recursion. */
1416 for (node = cgraph_first_function (); node != master_clone;
1417 node = next)
1419 next = cgraph_next_function (node);
1420 if (node->global.inlined_to == master_clone)
1421 node->remove ();
1423 master_clone->remove ();
1424 return true;
1428 /* Given whole compilation unit estimate of INSNS, compute how large we can
1429 allow the unit to grow. */
1431 static int
1432 compute_max_insns (int insns)
1434 int max_insns = insns;
1435 if (max_insns < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
1436 max_insns = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
1438 return ((int64_t) max_insns
1439 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
1443 /* Compute badness of all edges in NEW_EDGES and add them to the HEAP. */
1445 static void
1446 add_new_edges_to_heap (fibheap_t heap, vec<cgraph_edge *> new_edges)
1448 while (new_edges.length () > 0)
1450 struct cgraph_edge *edge = new_edges.pop ();
1452 gcc_assert (!edge->aux);
1453 if (edge->inline_failed
1454 && can_inline_edge_p (edge, true)
1455 && want_inline_small_function_p (edge, true))
1456 edge->aux = fibheap_insert (heap, edge_badness (edge, false), edge);
1460 /* Remove EDGE from the fibheap. */
1462 static void
1463 heap_edge_removal_hook (struct cgraph_edge *e, void *data)
1465 if (e->callee)
1466 reset_node_growth_cache (e->callee);
1467 if (e->aux)
1469 fibheap_delete_node ((fibheap_t)data, (fibnode_t)e->aux);
1470 e->aux = NULL;
1474 /* Return true if speculation of edge E seems useful.
1475 If ANTICIPATE_INLINING is true, be conservative and hope that E
1476 may get inlined. */
1478 bool
1479 speculation_useful_p (struct cgraph_edge *e, bool anticipate_inlining)
1481 enum availability avail;
1482 struct cgraph_node *target = e->callee->ultimate_alias_target (&avail);
1483 struct cgraph_edge *direct, *indirect;
1484 struct ipa_ref *ref;
1486 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1488 if (!cgraph_maybe_hot_edge_p (e))
1489 return false;
1491 /* See if IP optimizations found something potentially useful about the
1492 function. For now we look only for CONST/PURE flags. Almost everything
1493 else we propagate is useless. */
1494 if (avail >= AVAIL_AVAILABLE)
1496 int ecf_flags = flags_from_decl_or_type (target->decl);
1497 if (ecf_flags & ECF_CONST)
1499 cgraph_speculative_call_info (e, direct, indirect, ref);
1500 if (!(indirect->indirect_info->ecf_flags & ECF_CONST))
1501 return true;
1503 else if (ecf_flags & ECF_PURE)
1505 cgraph_speculative_call_info (e, direct, indirect, ref);
1506 if (!(indirect->indirect_info->ecf_flags & ECF_PURE))
1507 return true;
1510 /* If we did not managed to inline the function nor redirect
1511 to an ipa-cp clone (that are seen by having local flag set),
1512 it is probably pointless to inline it unless hardware is missing
1513 indirect call predictor. */
1514 if (!anticipate_inlining && e->inline_failed && !target->local.local)
1515 return false;
1516 /* For overwritable targets there is not much to do. */
1517 if (e->inline_failed && !can_inline_edge_p (e, false, true))
1518 return false;
1519 /* OK, speculation seems interesting. */
1520 return true;
1523 /* We know that EDGE is not going to be inlined.
1524 See if we can remove speculation. */
1526 static void
1527 resolve_noninline_speculation (fibheap_t edge_heap, struct cgraph_edge *edge)
1529 if (edge->speculative && !speculation_useful_p (edge, false))
1531 struct cgraph_node *node = edge->caller;
1532 struct cgraph_node *where = node->global.inlined_to
1533 ? node->global.inlined_to : node;
1534 bitmap updated_nodes = BITMAP_ALLOC (NULL);
1536 spec_rem += edge->count;
1537 cgraph_resolve_speculation (edge, NULL);
1538 reset_edge_caches (where);
1539 inline_update_overall_summary (where);
1540 update_caller_keys (edge_heap, where,
1541 updated_nodes, NULL);
1542 update_callee_keys (edge_heap, where,
1543 updated_nodes);
1544 BITMAP_FREE (updated_nodes);
1548 /* We use greedy algorithm for inlining of small functions:
1549 All inline candidates are put into prioritized heap ordered in
1550 increasing badness.
1552 The inlining of small functions is bounded by unit growth parameters. */
1554 static void
1555 inline_small_functions (void)
1557 struct cgraph_node *node;
1558 struct cgraph_edge *edge;
1559 fibheap_t edge_heap = fibheap_new ();
1560 bitmap updated_nodes = BITMAP_ALLOC (NULL);
1561 int min_size, max_size;
1562 auto_vec<cgraph_edge *> new_indirect_edges;
1563 int initial_size = 0;
1564 struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1565 struct cgraph_edge_hook_list *edge_removal_hook_holder;
1567 if (flag_indirect_inlining)
1568 new_indirect_edges.create (8);
1570 edge_removal_hook_holder
1571 = cgraph_add_edge_removal_hook (&heap_edge_removal_hook, edge_heap);
1573 /* Compute overall unit size and other global parameters used by badness
1574 metrics. */
1576 max_count = 0;
1577 ipa_reduced_postorder (order, true, true, NULL);
1578 free (order);
1580 FOR_EACH_DEFINED_FUNCTION (node)
1581 if (!node->global.inlined_to)
1583 if (node->has_gimple_body_p ()
1584 || node->thunk.thunk_p)
1586 struct inline_summary *info = inline_summary (node);
1587 struct ipa_dfs_info *dfs = (struct ipa_dfs_info *) node->aux;
1589 /* Do not account external functions, they will be optimized out
1590 if not inlined. Also only count the non-cold portion of program. */
1591 if (!DECL_EXTERNAL (node->decl)
1592 && node->frequency != NODE_FREQUENCY_UNLIKELY_EXECUTED)
1593 initial_size += info->size;
1594 info->growth = estimate_growth (node);
1595 if (dfs && dfs->next_cycle)
1597 struct cgraph_node *n2;
1598 int id = dfs->scc_no + 1;
1599 for (n2 = node; n2;
1600 n2 = ((struct ipa_dfs_info *) node->aux)->next_cycle)
1602 struct inline_summary *info2 = inline_summary (n2);
1603 if (info2->scc_no)
1604 break;
1605 info2->scc_no = id;
1610 for (edge = node->callers; edge; edge = edge->next_caller)
1611 if (max_count < edge->count)
1612 max_count = edge->count;
1614 sreal_init (&max_count_real, max_count, 0);
1615 sreal_init (&max_relbenefit_real, RELATIVE_TIME_BENEFIT_RANGE, 0);
1616 sreal_init (&half_int_min_real, INT_MAX / 2, 0);
1617 ipa_free_postorder_info ();
1618 initialize_growth_caches ();
1620 if (dump_file)
1621 fprintf (dump_file,
1622 "\nDeciding on inlining of small functions. Starting with size %i.\n",
1623 initial_size);
1625 overall_size = initial_size;
1626 max_size = compute_max_insns (overall_size);
1627 min_size = overall_size;
1629 /* Populate the heap with all edges we might inline. */
1631 FOR_EACH_DEFINED_FUNCTION (node)
1633 bool update = false;
1634 struct cgraph_edge *next;
1636 if (dump_file)
1637 fprintf (dump_file, "Enqueueing calls in %s/%i.\n",
1638 node->name (), node->order);
1640 for (edge = node->callees; edge; edge = next)
1642 next = edge->next_callee;
1643 if (edge->inline_failed
1644 && !edge->aux
1645 && can_inline_edge_p (edge, true)
1646 && want_inline_small_function_p (edge, true)
1647 && edge->inline_failed)
1649 gcc_assert (!edge->aux);
1650 update_edge_key (edge_heap, edge);
1652 if (edge->speculative && !speculation_useful_p (edge, edge->aux != NULL))
1654 cgraph_resolve_speculation (edge, NULL);
1655 update = true;
1658 if (update)
1660 struct cgraph_node *where = node->global.inlined_to
1661 ? node->global.inlined_to : node;
1662 inline_update_overall_summary (where);
1663 reset_node_growth_cache (where);
1664 reset_edge_caches (where);
1665 update_caller_keys (edge_heap, where,
1666 updated_nodes, NULL);
1667 bitmap_clear (updated_nodes);
1671 gcc_assert (in_lto_p
1672 || !max_count
1673 || (profile_info && flag_branch_probabilities));
1675 while (!fibheap_empty (edge_heap))
1677 int old_size = overall_size;
1678 struct cgraph_node *where, *callee;
1679 int badness = fibheap_min_key (edge_heap);
1680 int current_badness;
1681 int cached_badness;
1682 int growth;
1684 edge = (struct cgraph_edge *) fibheap_extract_min (edge_heap);
1685 gcc_assert (edge->aux);
1686 edge->aux = NULL;
1687 if (!edge->inline_failed || !edge->callee->analyzed)
1688 continue;
1690 /* Be sure that caches are maintained consistent.
1691 We can not make this ENABLE_CHECKING only because it cause different
1692 updates of the fibheap queue. */
1693 cached_badness = edge_badness (edge, false);
1694 reset_edge_growth_cache (edge);
1695 reset_node_growth_cache (edge->callee);
1697 /* When updating the edge costs, we only decrease badness in the keys.
1698 Increases of badness are handled lazilly; when we see key with out
1699 of date value on it, we re-insert it now. */
1700 current_badness = edge_badness (edge, false);
1701 gcc_assert (cached_badness == current_badness);
1702 gcc_assert (current_badness >= badness);
1703 if (current_badness != badness)
1705 edge->aux = fibheap_insert (edge_heap, current_badness, edge);
1706 continue;
1709 if (!can_inline_edge_p (edge, true))
1711 resolve_noninline_speculation (edge_heap, edge);
1712 continue;
1715 callee = edge->callee->ultimate_alias_target ();
1716 growth = estimate_edge_growth (edge);
1717 if (dump_file)
1719 fprintf (dump_file,
1720 "\nConsidering %s/%i with %i size\n",
1721 callee->name (), callee->order,
1722 inline_summary (callee)->size);
1723 fprintf (dump_file,
1724 " to be inlined into %s/%i in %s:%i\n"
1725 " Estimated badness is %i, frequency %.2f.\n",
1726 edge->caller->name (), edge->caller->order,
1727 flag_wpa ? "unknown"
1728 : gimple_filename ((const_gimple) edge->call_stmt),
1729 flag_wpa ? -1
1730 : gimple_lineno ((const_gimple) edge->call_stmt),
1731 badness,
1732 edge->frequency / (double)CGRAPH_FREQ_BASE);
1733 if (edge->count)
1734 fprintf (dump_file," Called %"PRId64"x\n",
1735 edge->count);
1736 if (dump_flags & TDF_DETAILS)
1737 edge_badness (edge, true);
1740 if (overall_size + growth > max_size
1741 && !DECL_DISREGARD_INLINE_LIMITS (callee->decl))
1743 edge->inline_failed = CIF_INLINE_UNIT_GROWTH_LIMIT;
1744 report_inline_failed_reason (edge);
1745 resolve_noninline_speculation (edge_heap, edge);
1746 continue;
1749 if (!want_inline_small_function_p (edge, true))
1751 resolve_noninline_speculation (edge_heap, edge);
1752 continue;
1755 /* Heuristics for inlining small functions work poorly for
1756 recursive calls where we do effects similar to loop unrolling.
1757 When inlining such edge seems profitable, leave decision on
1758 specific inliner. */
1759 if (cgraph_edge_recursive_p (edge))
1761 where = edge->caller;
1762 if (where->global.inlined_to)
1763 where = where->global.inlined_to;
1764 if (!recursive_inlining (edge,
1765 flag_indirect_inlining
1766 ? &new_indirect_edges : NULL))
1768 edge->inline_failed = CIF_RECURSIVE_INLINING;
1769 resolve_noninline_speculation (edge_heap, edge);
1770 continue;
1772 reset_edge_caches (where);
1773 /* Recursive inliner inlines all recursive calls of the function
1774 at once. Consequently we need to update all callee keys. */
1775 if (flag_indirect_inlining)
1776 add_new_edges_to_heap (edge_heap, new_indirect_edges);
1777 update_callee_keys (edge_heap, where, updated_nodes);
1778 bitmap_clear (updated_nodes);
1780 else
1782 struct cgraph_node *outer_node = NULL;
1783 int depth = 0;
1785 /* Consider the case where self recursive function A is inlined
1786 into B. This is desired optimization in some cases, since it
1787 leads to effect similar of loop peeling and we might completely
1788 optimize out the recursive call. However we must be extra
1789 selective. */
1791 where = edge->caller;
1792 while (where->global.inlined_to)
1794 if (where->decl == callee->decl)
1795 outer_node = where, depth++;
1796 where = where->callers->caller;
1798 if (outer_node
1799 && !want_inline_self_recursive_call_p (edge, outer_node,
1800 true, depth))
1802 edge->inline_failed
1803 = (DECL_DISREGARD_INLINE_LIMITS (edge->callee->decl)
1804 ? CIF_RECURSIVE_INLINING : CIF_UNSPECIFIED);
1805 resolve_noninline_speculation (edge_heap, edge);
1806 continue;
1808 else if (depth && dump_file)
1809 fprintf (dump_file, " Peeling recursion with depth %i\n", depth);
1811 gcc_checking_assert (!callee->global.inlined_to);
1812 inline_call (edge, true, &new_indirect_edges, &overall_size, true);
1813 if (flag_indirect_inlining)
1814 add_new_edges_to_heap (edge_heap, new_indirect_edges);
1816 reset_edge_caches (edge->callee);
1817 reset_node_growth_cache (callee);
1819 update_callee_keys (edge_heap, where, updated_nodes);
1821 where = edge->caller;
1822 if (where->global.inlined_to)
1823 where = where->global.inlined_to;
1825 /* Our profitability metric can depend on local properties
1826 such as number of inlinable calls and size of the function body.
1827 After inlining these properties might change for the function we
1828 inlined into (since it's body size changed) and for the functions
1829 called by function we inlined (since number of it inlinable callers
1830 might change). */
1831 update_caller_keys (edge_heap, where, updated_nodes, NULL);
1832 bitmap_clear (updated_nodes);
1834 if (dump_file)
1836 fprintf (dump_file,
1837 " Inlined into %s which now has time %i and size %i,"
1838 "net change of %+i.\n",
1839 edge->caller->name (),
1840 inline_summary (edge->caller)->time,
1841 inline_summary (edge->caller)->size,
1842 overall_size - old_size);
1844 if (min_size > overall_size)
1846 min_size = overall_size;
1847 max_size = compute_max_insns (min_size);
1849 if (dump_file)
1850 fprintf (dump_file, "New minimal size reached: %i\n", min_size);
1854 free_growth_caches ();
1855 fibheap_delete (edge_heap);
1856 if (dump_file)
1857 fprintf (dump_file,
1858 "Unit growth for small function inlining: %i->%i (%i%%)\n",
1859 initial_size, overall_size,
1860 initial_size ? overall_size * 100 / (initial_size) - 100: 0);
1861 BITMAP_FREE (updated_nodes);
1862 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
1865 /* Flatten NODE. Performed both during early inlining and
1866 at IPA inlining time. */
1868 static void
1869 flatten_function (struct cgraph_node *node, bool early)
1871 struct cgraph_edge *e;
1873 /* We shouldn't be called recursively when we are being processed. */
1874 gcc_assert (node->aux == NULL);
1876 node->aux = (void *) node;
1878 for (e = node->callees; e; e = e->next_callee)
1880 struct cgraph_node *orig_callee;
1881 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
1883 /* We've hit cycle? It is time to give up. */
1884 if (callee->aux)
1886 if (dump_file)
1887 fprintf (dump_file,
1888 "Not inlining %s into %s to avoid cycle.\n",
1889 xstrdup (callee->name ()),
1890 xstrdup (e->caller->name ()));
1891 e->inline_failed = CIF_RECURSIVE_INLINING;
1892 continue;
1895 /* When the edge is already inlined, we just need to recurse into
1896 it in order to fully flatten the leaves. */
1897 if (!e->inline_failed)
1899 flatten_function (callee, early);
1900 continue;
1903 /* Flatten attribute needs to be processed during late inlining. For
1904 extra code quality we however do flattening during early optimization,
1905 too. */
1906 if (!early
1907 ? !can_inline_edge_p (e, true)
1908 : !can_early_inline_edge_p (e))
1909 continue;
1911 if (cgraph_edge_recursive_p (e))
1913 if (dump_file)
1914 fprintf (dump_file, "Not inlining: recursive call.\n");
1915 continue;
1918 if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
1919 != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
1921 if (dump_file)
1922 fprintf (dump_file, "Not inlining: SSA form does not match.\n");
1923 continue;
1926 /* Inline the edge and flatten the inline clone. Avoid
1927 recursing through the original node if the node was cloned. */
1928 if (dump_file)
1929 fprintf (dump_file, " Inlining %s into %s.\n",
1930 xstrdup (callee->name ()),
1931 xstrdup (e->caller->name ()));
1932 orig_callee = callee;
1933 inline_call (e, true, NULL, NULL, false);
1934 if (e->callee != orig_callee)
1935 orig_callee->aux = (void *) node;
1936 flatten_function (e->callee, early);
1937 if (e->callee != orig_callee)
1938 orig_callee->aux = NULL;
1941 node->aux = NULL;
1942 if (!node->global.inlined_to)
1943 inline_update_overall_summary (node);
1946 /* Count number of callers of NODE and store it into DATA (that
1947 points to int. Worker for cgraph_for_node_and_aliases. */
1949 static bool
1950 sum_callers (struct cgraph_node *node, void *data)
1952 struct cgraph_edge *e;
1953 int *num_calls = (int *)data;
1955 for (e = node->callers; e; e = e->next_caller)
1956 (*num_calls)++;
1957 return false;
1960 /* Inline NODE to all callers. Worker for cgraph_for_node_and_aliases.
1961 DATA points to number of calls originally found so we avoid infinite
1962 recursion. */
1964 static bool
1965 inline_to_all_callers (struct cgraph_node *node, void *data)
1967 int *num_calls = (int *)data;
1968 bool callee_removed = false;
1970 while (node->callers && !node->global.inlined_to)
1972 struct cgraph_node *caller = node->callers->caller;
1974 if (dump_file)
1976 fprintf (dump_file,
1977 "\nInlining %s size %i.\n",
1978 node->name (),
1979 inline_summary (node)->size);
1980 fprintf (dump_file,
1981 " Called once from %s %i insns.\n",
1982 node->callers->caller->name (),
1983 inline_summary (node->callers->caller)->size);
1986 inline_call (node->callers, true, NULL, NULL, true, &callee_removed);
1987 if (dump_file)
1988 fprintf (dump_file,
1989 " Inlined into %s which now has %i size\n",
1990 caller->name (),
1991 inline_summary (caller)->size);
1992 if (!(*num_calls)--)
1994 if (dump_file)
1995 fprintf (dump_file, "New calls found; giving up.\n");
1996 return callee_removed;
1998 if (callee_removed)
1999 return true;
2001 return false;
2004 /* Output overall time estimate. */
2005 static void
2006 dump_overall_stats (void)
2008 int64_t sum_weighted = 0, sum = 0;
2009 struct cgraph_node *node;
2011 FOR_EACH_DEFINED_FUNCTION (node)
2012 if (!node->global.inlined_to
2013 && !node->alias)
2015 int time = inline_summary (node)->time;
2016 sum += time;
2017 sum_weighted += time * node->count;
2019 fprintf (dump_file, "Overall time estimate: "
2020 "%"PRId64" weighted by profile: "
2021 "%"PRId64"\n", sum, sum_weighted);
2024 /* Output some useful stats about inlining. */
2026 static void
2027 dump_inline_stats (void)
2029 int64_t inlined_cnt = 0, inlined_indir_cnt = 0;
2030 int64_t inlined_virt_cnt = 0, inlined_virt_indir_cnt = 0;
2031 int64_t noninlined_cnt = 0, noninlined_indir_cnt = 0;
2032 int64_t noninlined_virt_cnt = 0, noninlined_virt_indir_cnt = 0;
2033 int64_t inlined_speculative = 0, inlined_speculative_ply = 0;
2034 int64_t indirect_poly_cnt = 0, indirect_cnt = 0;
2035 int64_t reason[CIF_N_REASONS][3];
2036 int i;
2037 struct cgraph_node *node;
2039 memset (reason, 0, sizeof (reason));
2040 FOR_EACH_DEFINED_FUNCTION (node)
2042 struct cgraph_edge *e;
2043 for (e = node->callees; e; e = e->next_callee)
2045 if (e->inline_failed)
2047 reason[(int) e->inline_failed][0] += e->count;
2048 reason[(int) e->inline_failed][1] += e->frequency;
2049 reason[(int) e->inline_failed][2] ++;
2050 if (DECL_VIRTUAL_P (e->callee->decl))
2052 if (e->indirect_inlining_edge)
2053 noninlined_virt_indir_cnt += e->count;
2054 else
2055 noninlined_virt_cnt += e->count;
2057 else
2059 if (e->indirect_inlining_edge)
2060 noninlined_indir_cnt += e->count;
2061 else
2062 noninlined_cnt += e->count;
2065 else
2067 if (e->speculative)
2069 if (DECL_VIRTUAL_P (e->callee->decl))
2070 inlined_speculative_ply += e->count;
2071 else
2072 inlined_speculative += e->count;
2074 else if (DECL_VIRTUAL_P (e->callee->decl))
2076 if (e->indirect_inlining_edge)
2077 inlined_virt_indir_cnt += e->count;
2078 else
2079 inlined_virt_cnt += e->count;
2081 else
2083 if (e->indirect_inlining_edge)
2084 inlined_indir_cnt += e->count;
2085 else
2086 inlined_cnt += e->count;
2090 for (e = node->indirect_calls; e; e = e->next_callee)
2091 if (e->indirect_info->polymorphic)
2092 indirect_poly_cnt += e->count;
2093 else
2094 indirect_cnt += e->count;
2096 if (max_count)
2098 fprintf (dump_file,
2099 "Inlined %"PRId64 " + speculative "
2100 "%"PRId64 " + speculative polymorphic "
2101 "%"PRId64 " + previously indirect "
2102 "%"PRId64 " + virtual "
2103 "%"PRId64 " + virtual and previously indirect "
2104 "%"PRId64 "\n" "Not inlined "
2105 "%"PRId64 " + previously indirect "
2106 "%"PRId64 " + virtual "
2107 "%"PRId64 " + virtual and previously indirect "
2108 "%"PRId64 " + stil indirect "
2109 "%"PRId64 " + still indirect polymorphic "
2110 "%"PRId64 "\n", inlined_cnt,
2111 inlined_speculative, inlined_speculative_ply,
2112 inlined_indir_cnt, inlined_virt_cnt, inlined_virt_indir_cnt,
2113 noninlined_cnt, noninlined_indir_cnt, noninlined_virt_cnt,
2114 noninlined_virt_indir_cnt, indirect_cnt, indirect_poly_cnt);
2115 fprintf (dump_file,
2116 "Removed speculations %"PRId64 "\n",
2117 spec_rem);
2119 dump_overall_stats ();
2120 fprintf (dump_file, "\nWhy inlining failed?\n");
2121 for (i = 0; i < CIF_N_REASONS; i++)
2122 if (reason[i][2])
2123 fprintf (dump_file, "%-50s: %8i calls, %8i freq, %"PRId64" count\n",
2124 cgraph_inline_failed_string ((cgraph_inline_failed_t) i),
2125 (int) reason[i][2], (int) reason[i][1], reason[i][0]);
2128 /* Decide on the inlining. We do so in the topological order to avoid
2129 expenses on updating data structures. */
2131 static unsigned int
2132 ipa_inline (void)
2134 struct cgraph_node *node;
2135 int nnodes;
2136 struct cgraph_node **order;
2137 int i;
2138 int cold;
2139 bool remove_functions = false;
2141 if (!optimize)
2142 return 0;
2144 order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
2146 if (in_lto_p && optimize)
2147 ipa_update_after_lto_read ();
2149 if (dump_file)
2150 dump_inline_summaries (dump_file);
2152 nnodes = ipa_reverse_postorder (order);
2154 FOR_EACH_FUNCTION (node)
2155 node->aux = 0;
2157 if (dump_file)
2158 fprintf (dump_file, "\nFlattening functions:\n");
2160 /* In the first pass handle functions to be flattened. Do this with
2161 a priority so none of our later choices will make this impossible. */
2162 for (i = nnodes - 1; i >= 0; i--)
2164 node = order[i];
2166 /* Handle nodes to be flattened.
2167 Ideally when processing callees we stop inlining at the
2168 entry of cycles, possibly cloning that entry point and
2169 try to flatten itself turning it into a self-recursive
2170 function. */
2171 if (lookup_attribute ("flatten",
2172 DECL_ATTRIBUTES (node->decl)) != NULL)
2174 if (dump_file)
2175 fprintf (dump_file,
2176 "Flattening %s\n", node->name ());
2177 flatten_function (node, false);
2180 if (dump_file)
2181 dump_overall_stats ();
2183 inline_small_functions ();
2185 /* Do first after-inlining removal. We want to remove all "stale" extern inline
2186 functions and virtual functions so we really know what is called once. */
2187 symtab_remove_unreachable_nodes (false, dump_file);
2188 free (order);
2190 /* Inline functions with a property that after inlining into all callers the
2191 code size will shrink because the out-of-line copy is eliminated.
2192 We do this regardless on the callee size as long as function growth limits
2193 are met. */
2194 if (dump_file)
2195 fprintf (dump_file,
2196 "\nDeciding on functions to be inlined into all callers and removing useless speculations:\n");
2198 /* Inlining one function called once has good chance of preventing
2199 inlining other function into the same callee. Ideally we should
2200 work in priority order, but probably inlining hot functions first
2201 is good cut without the extra pain of maintaining the queue.
2203 ??? this is not really fitting the bill perfectly: inlining function
2204 into callee often leads to better optimization of callee due to
2205 increased context for optimization.
2206 For example if main() function calls a function that outputs help
2207 and then function that does the main optmization, we should inline
2208 the second with priority even if both calls are cold by themselves.
2210 We probably want to implement new predicate replacing our use of
2211 maybe_hot_edge interpreted as maybe_hot_edge || callee is known
2212 to be hot. */
2213 for (cold = 0; cold <= 1; cold ++)
2215 FOR_EACH_DEFINED_FUNCTION (node)
2217 struct cgraph_edge *edge, *next;
2218 bool update=false;
2220 for (edge = node->callees; edge; edge = next)
2222 next = edge->next_callee;
2223 if (edge->speculative && !speculation_useful_p (edge, false))
2225 cgraph_resolve_speculation (edge, NULL);
2226 spec_rem += edge->count;
2227 update = true;
2228 remove_functions = true;
2231 if (update)
2233 struct cgraph_node *where = node->global.inlined_to
2234 ? node->global.inlined_to : node;
2235 reset_node_growth_cache (where);
2236 reset_edge_caches (where);
2237 inline_update_overall_summary (where);
2239 if (flag_inline_functions_called_once
2240 && want_inline_function_to_all_callers_p (node, cold))
2242 int num_calls = 0;
2243 node->call_for_symbol_thunks_and_aliases (sum_callers, &num_calls,
2244 true);
2245 while (node->call_for_symbol_thunks_and_aliases (inline_to_all_callers,
2246 &num_calls, true))
2248 remove_functions = true;
2253 /* Free ipa-prop structures if they are no longer needed. */
2254 if (optimize)
2255 ipa_free_all_structures_after_iinln ();
2257 if (dump_file)
2259 fprintf (dump_file,
2260 "\nInlined %i calls, eliminated %i functions\n\n",
2261 ncalls_inlined, nfunctions_inlined);
2262 dump_inline_stats ();
2265 if (dump_file)
2266 dump_inline_summaries (dump_file);
2267 /* In WPA we use inline summaries for partitioning process. */
2268 if (!flag_wpa)
2269 inline_free_summary ();
2270 return remove_functions ? TODO_remove_functions : 0;
2273 /* Inline always-inline function calls in NODE. */
2275 static bool
2276 inline_always_inline_functions (struct cgraph_node *node)
2278 struct cgraph_edge *e;
2279 bool inlined = false;
2281 for (e = node->callees; e; e = e->next_callee)
2283 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
2284 if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl))
2285 continue;
2287 if (cgraph_edge_recursive_p (e))
2289 if (dump_file)
2290 fprintf (dump_file, " Not inlining recursive call to %s.\n",
2291 e->callee->name ());
2292 e->inline_failed = CIF_RECURSIVE_INLINING;
2293 continue;
2296 if (!can_early_inline_edge_p (e))
2298 /* Set inlined to true if the callee is marked "always_inline" but
2299 is not inlinable. This will allow flagging an error later in
2300 expand_call_inline in tree-inline.c. */
2301 if (lookup_attribute ("always_inline",
2302 DECL_ATTRIBUTES (callee->decl)) != NULL)
2303 inlined = true;
2304 continue;
2307 if (dump_file)
2308 fprintf (dump_file, " Inlining %s into %s (always_inline).\n",
2309 xstrdup (e->callee->name ()),
2310 xstrdup (e->caller->name ()));
2311 inline_call (e, true, NULL, NULL, false);
2312 inlined = true;
2314 if (inlined)
2315 inline_update_overall_summary (node);
2317 return inlined;
2320 /* Decide on the inlining. We do so in the topological order to avoid
2321 expenses on updating data structures. */
2323 static bool
2324 early_inline_small_functions (struct cgraph_node *node)
2326 struct cgraph_edge *e;
2327 bool inlined = false;
2329 for (e = node->callees; e; e = e->next_callee)
2331 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
2332 if (!inline_summary (callee)->inlinable
2333 || !e->inline_failed)
2334 continue;
2336 /* Do not consider functions not declared inline. */
2337 if (!DECL_DECLARED_INLINE_P (callee->decl)
2338 && !flag_inline_small_functions
2339 && !flag_inline_functions)
2340 continue;
2342 if (dump_file)
2343 fprintf (dump_file, "Considering inline candidate %s.\n",
2344 callee->name ());
2346 if (!can_early_inline_edge_p (e))
2347 continue;
2349 if (cgraph_edge_recursive_p (e))
2351 if (dump_file)
2352 fprintf (dump_file, " Not inlining: recursive call.\n");
2353 continue;
2356 if (!want_early_inline_function_p (e))
2357 continue;
2359 if (dump_file)
2360 fprintf (dump_file, " Inlining %s into %s.\n",
2361 xstrdup (callee->name ()),
2362 xstrdup (e->caller->name ()));
2363 inline_call (e, true, NULL, NULL, true);
2364 inlined = true;
2367 return inlined;
2370 /* Do inlining of small functions. Doing so early helps profiling and other
2371 passes to be somewhat more effective and avoids some code duplication in
2372 later real inlining pass for testcases with very many function calls. */
2374 namespace {
2376 const pass_data pass_data_early_inline =
2378 GIMPLE_PASS, /* type */
2379 "einline", /* name */
2380 OPTGROUP_INLINE, /* optinfo_flags */
2381 TV_EARLY_INLINING, /* tv_id */
2382 PROP_ssa, /* properties_required */
2383 0, /* properties_provided */
2384 0, /* properties_destroyed */
2385 0, /* todo_flags_start */
2386 0, /* todo_flags_finish */
2389 class pass_early_inline : public gimple_opt_pass
2391 public:
2392 pass_early_inline (gcc::context *ctxt)
2393 : gimple_opt_pass (pass_data_early_inline, ctxt)
2396 /* opt_pass methods: */
2397 virtual unsigned int execute (function *);
2399 }; // class pass_early_inline
2401 unsigned int
2402 pass_early_inline::execute (function *fun)
2404 struct cgraph_node *node = cgraph_node::get (current_function_decl);
2405 struct cgraph_edge *edge;
2406 unsigned int todo = 0;
2407 int iterations = 0;
2408 bool inlined = false;
2410 if (seen_error ())
2411 return 0;
2413 /* Do nothing if datastructures for ipa-inliner are already computed. This
2414 happens when some pass decides to construct new function and
2415 cgraph_add_new_function calls lowering passes and early optimization on
2416 it. This may confuse ourself when early inliner decide to inline call to
2417 function clone, because function clones don't have parameter list in
2418 ipa-prop matching their signature. */
2419 if (ipa_node_params_vector.exists ())
2420 return 0;
2422 #ifdef ENABLE_CHECKING
2423 node->verify ();
2424 #endif
2425 node->remove_all_references ();
2427 /* Even when not optimizing or not inlining inline always-inline
2428 functions. */
2429 inlined = inline_always_inline_functions (node);
2431 if (!optimize
2432 || flag_no_inline
2433 || !flag_early_inlining
2434 /* Never inline regular functions into always-inline functions
2435 during incremental inlining. This sucks as functions calling
2436 always inline functions will get less optimized, but at the
2437 same time inlining of functions calling always inline
2438 function into an always inline function might introduce
2439 cycles of edges to be always inlined in the callgraph.
2441 We might want to be smarter and just avoid this type of inlining. */
2442 || DECL_DISREGARD_INLINE_LIMITS (node->decl))
2444 else if (lookup_attribute ("flatten",
2445 DECL_ATTRIBUTES (node->decl)) != NULL)
2447 /* When the function is marked to be flattened, recursively inline
2448 all calls in it. */
2449 if (dump_file)
2450 fprintf (dump_file,
2451 "Flattening %s\n", node->name ());
2452 flatten_function (node, true);
2453 inlined = true;
2455 else
2457 /* We iterate incremental inlining to get trivial cases of indirect
2458 inlining. */
2459 while (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS)
2460 && early_inline_small_functions (node))
2462 timevar_push (TV_INTEGRATION);
2463 todo |= optimize_inline_calls (current_function_decl);
2465 /* Technically we ought to recompute inline parameters so the new
2466 iteration of early inliner works as expected. We however have
2467 values approximately right and thus we only need to update edge
2468 info that might be cleared out for newly discovered edges. */
2469 for (edge = node->callees; edge; edge = edge->next_callee)
2471 struct inline_edge_summary *es = inline_edge_summary (edge);
2472 es->call_stmt_size
2473 = estimate_num_insns (edge->call_stmt, &eni_size_weights);
2474 es->call_stmt_time
2475 = estimate_num_insns (edge->call_stmt, &eni_time_weights);
2476 if (edge->callee->decl
2477 && !gimple_check_call_matching_types (
2478 edge->call_stmt, edge->callee->decl, false))
2479 edge->call_stmt_cannot_inline_p = true;
2481 if (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS) - 1)
2482 inline_update_overall_summary (node);
2483 timevar_pop (TV_INTEGRATION);
2484 iterations++;
2485 inlined = false;
2487 if (dump_file)
2488 fprintf (dump_file, "Iterations: %i\n", iterations);
2491 if (inlined)
2493 timevar_push (TV_INTEGRATION);
2494 todo |= optimize_inline_calls (current_function_decl);
2495 timevar_pop (TV_INTEGRATION);
2498 fun->always_inline_functions_inlined = true;
2500 return todo;
2503 } // anon namespace
2505 gimple_opt_pass *
2506 make_pass_early_inline (gcc::context *ctxt)
2508 return new pass_early_inline (ctxt);
2511 namespace {
2513 const pass_data pass_data_ipa_inline =
2515 IPA_PASS, /* type */
2516 "inline", /* name */
2517 OPTGROUP_INLINE, /* optinfo_flags */
2518 TV_IPA_INLINING, /* tv_id */
2519 0, /* properties_required */
2520 0, /* properties_provided */
2521 0, /* properties_destroyed */
2522 TODO_remove_functions, /* todo_flags_start */
2523 ( TODO_dump_symtab ), /* todo_flags_finish */
2526 class pass_ipa_inline : public ipa_opt_pass_d
2528 public:
2529 pass_ipa_inline (gcc::context *ctxt)
2530 : ipa_opt_pass_d (pass_data_ipa_inline, ctxt,
2531 inline_generate_summary, /* generate_summary */
2532 inline_write_summary, /* write_summary */
2533 inline_read_summary, /* read_summary */
2534 NULL, /* write_optimization_summary */
2535 NULL, /* read_optimization_summary */
2536 NULL, /* stmt_fixup */
2537 0, /* function_transform_todo_flags_start */
2538 inline_transform, /* function_transform */
2539 NULL) /* variable_transform */
2542 /* opt_pass methods: */
2543 virtual unsigned int execute (function *) { return ipa_inline (); }
2545 }; // class pass_ipa_inline
2547 } // anon namespace
2549 ipa_opt_pass_d *
2550 make_pass_ipa_inline (gcc::context *ctxt)
2552 return new pass_ipa_inline (ctxt);