/cp
[official-gcc.git] / gcc / ipa-inline.c
blob5f47e0b4e1ac84f13d49bc18b947e665f9abe90f
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"
126 /* Statistics we collect about inlining algorithm. */
127 static int overall_size;
128 static gcov_type max_count;
129 static sreal max_count_real, max_relbenefit_real, half_int_min_real;
131 /* Return false when inlining edge E would lead to violating
132 limits on function unit growth or stack usage growth.
134 The relative function body growth limit is present generally
135 to avoid problems with non-linear behavior of the compiler.
136 To allow inlining huge functions into tiny wrapper, the limit
137 is always based on the bigger of the two functions considered.
139 For stack growth limits we always base the growth in stack usage
140 of the callers. We want to prevent applications from segfaulting
141 on stack overflow when functions with huge stack frames gets
142 inlined. */
144 static bool
145 caller_growth_limits (struct cgraph_edge *e)
147 struct cgraph_node *to = e->caller;
148 struct cgraph_node *what = cgraph_function_or_thunk_node (e->callee, NULL);
149 int newsize;
150 int limit = 0;
151 HOST_WIDE_INT stack_size_limit = 0, inlined_stack;
152 struct inline_summary *info, *what_info, *outer_info = inline_summary (to);
154 /* Look for function e->caller is inlined to. While doing
155 so work out the largest function body on the way. As
156 described above, we want to base our function growth
157 limits based on that. Not on the self size of the
158 outer function, not on the self size of inline code
159 we immediately inline to. This is the most relaxed
160 interpretation of the rule "do not grow large functions
161 too much in order to prevent compiler from exploding". */
162 while (true)
164 info = inline_summary (to);
165 if (limit < info->self_size)
166 limit = info->self_size;
167 if (stack_size_limit < info->estimated_self_stack_size)
168 stack_size_limit = info->estimated_self_stack_size;
169 if (to->global.inlined_to)
170 to = to->callers->caller;
171 else
172 break;
175 what_info = inline_summary (what);
177 if (limit < what_info->self_size)
178 limit = what_info->self_size;
180 limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
182 /* Check the size after inlining against the function limits. But allow
183 the function to shrink if it went over the limits by forced inlining. */
184 newsize = estimate_size_after_inlining (to, e);
185 if (newsize >= info->size
186 && newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
187 && newsize > limit)
189 e->inline_failed = CIF_LARGE_FUNCTION_GROWTH_LIMIT;
190 return false;
193 if (!what_info->estimated_stack_size)
194 return true;
196 /* FIXME: Stack size limit often prevents inlining in Fortran programs
197 due to large i/o datastructures used by the Fortran front-end.
198 We ought to ignore this limit when we know that the edge is executed
199 on every invocation of the caller (i.e. its call statement dominates
200 exit block). We do not track this information, yet. */
201 stack_size_limit += ((gcov_type)stack_size_limit
202 * PARAM_VALUE (PARAM_STACK_FRAME_GROWTH) / 100);
204 inlined_stack = (outer_info->stack_frame_offset
205 + outer_info->estimated_self_stack_size
206 + what_info->estimated_stack_size);
207 /* Check new stack consumption with stack consumption at the place
208 stack is used. */
209 if (inlined_stack > stack_size_limit
210 /* If function already has large stack usage from sibling
211 inline call, we can inline, too.
212 This bit overoptimistically assume that we are good at stack
213 packing. */
214 && inlined_stack > info->estimated_stack_size
215 && inlined_stack > PARAM_VALUE (PARAM_LARGE_STACK_FRAME))
217 e->inline_failed = CIF_LARGE_STACK_FRAME_GROWTH_LIMIT;
218 return false;
220 return true;
223 /* Dump info about why inlining has failed. */
225 static void
226 report_inline_failed_reason (struct cgraph_edge *e)
228 if (dump_file)
230 fprintf (dump_file, " not inlinable: %s/%i -> %s/%i, %s\n",
231 xstrdup (e->caller->name ()), e->caller->order,
232 xstrdup (e->callee->name ()), e->callee->order,
233 cgraph_inline_failed_string (e->inline_failed));
237 /* Decide if we can inline the edge and possibly update
238 inline_failed reason.
239 We check whether inlining is possible at all and whether
240 caller growth limits allow doing so.
242 if REPORT is true, output reason to the dump file.
244 if DISREGARD_LIMITS is true, ignore size limits.*/
246 static bool
247 can_inline_edge_p (struct cgraph_edge *e, bool report,
248 bool disregard_limits = false)
250 bool inlinable = true;
251 enum availability avail;
252 struct cgraph_node *callee
253 = cgraph_function_or_thunk_node (e->callee, &avail);
254 tree caller_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (e->caller->decl);
255 tree callee_tree
256 = callee ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (callee->decl) : NULL;
257 struct function *caller_cfun = DECL_STRUCT_FUNCTION (e->caller->decl);
258 struct function *callee_cfun
259 = callee ? DECL_STRUCT_FUNCTION (callee->decl) : NULL;
261 if (!caller_cfun && e->caller->clone_of)
262 caller_cfun = DECL_STRUCT_FUNCTION (e->caller->clone_of->decl);
264 if (!callee_cfun && callee && callee->clone_of)
265 callee_cfun = DECL_STRUCT_FUNCTION (callee->clone_of->decl);
267 gcc_assert (e->inline_failed);
269 if (!callee || !callee->definition)
271 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
272 inlinable = false;
274 else if (callee->calls_comdat_local)
276 e->inline_failed = CIF_USES_COMDAT_LOCAL;
277 inlinable = false;
279 else if (!inline_summary (callee)->inlinable
280 || (caller_cfun && fn_contains_cilk_spawn_p (caller_cfun)))
282 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
283 inlinable = false;
285 else if (avail <= AVAIL_OVERWRITABLE)
287 e->inline_failed = CIF_OVERWRITABLE;
288 inlinable = false;
290 else if (e->call_stmt_cannot_inline_p)
292 if (e->inline_failed != CIF_FUNCTION_NOT_OPTIMIZED)
293 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
294 inlinable = false;
296 /* Don't inline if the functions have different EH personalities. */
297 else if (DECL_FUNCTION_PERSONALITY (e->caller->decl)
298 && DECL_FUNCTION_PERSONALITY (callee->decl)
299 && (DECL_FUNCTION_PERSONALITY (e->caller->decl)
300 != DECL_FUNCTION_PERSONALITY (callee->decl)))
302 e->inline_failed = CIF_EH_PERSONALITY;
303 inlinable = false;
305 /* TM pure functions should not be inlined into non-TM_pure
306 functions. */
307 else if (is_tm_pure (callee->decl)
308 && !is_tm_pure (e->caller->decl))
310 e->inline_failed = CIF_UNSPECIFIED;
311 inlinable = false;
313 /* Don't inline if the callee can throw non-call exceptions but the
314 caller cannot.
315 FIXME: this is obviously wrong for LTO where STRUCT_FUNCTION is missing.
316 Move the flag into cgraph node or mirror it in the inline summary. */
317 else if (callee_cfun && callee_cfun->can_throw_non_call_exceptions
318 && !(caller_cfun && caller_cfun->can_throw_non_call_exceptions))
320 e->inline_failed = CIF_NON_CALL_EXCEPTIONS;
321 inlinable = false;
323 /* Check compatibility of target optimization options. */
324 else if (!targetm.target_option.can_inline_p (e->caller->decl,
325 callee->decl))
327 e->inline_failed = CIF_TARGET_OPTION_MISMATCH;
328 inlinable = false;
330 /* Check if caller growth allows the inlining. */
331 else if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl)
332 && !disregard_limits
333 && !lookup_attribute ("flatten",
334 DECL_ATTRIBUTES
335 (e->caller->global.inlined_to
336 ? e->caller->global.inlined_to->decl
337 : e->caller->decl))
338 && !caller_growth_limits (e))
339 inlinable = false;
340 /* Don't inline a function with a higher optimization level than the
341 caller. FIXME: this is really just tip of iceberg of handling
342 optimization attribute. */
343 else if (caller_tree != callee_tree)
345 struct cl_optimization *caller_opt
346 = TREE_OPTIMIZATION ((caller_tree)
347 ? caller_tree
348 : optimization_default_node);
350 struct cl_optimization *callee_opt
351 = TREE_OPTIMIZATION ((callee_tree)
352 ? callee_tree
353 : optimization_default_node);
355 if (((caller_opt->x_optimize > callee_opt->x_optimize)
356 || (caller_opt->x_optimize_size != callee_opt->x_optimize_size))
357 /* gcc.dg/pr43564.c. Look at forced inline even in -O0. */
358 && !DECL_DISREGARD_INLINE_LIMITS (e->callee->decl))
360 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
361 inlinable = false;
365 if (!inlinable && report)
366 report_inline_failed_reason (e);
367 return inlinable;
371 /* Return true if the edge E is inlinable during early inlining. */
373 static bool
374 can_early_inline_edge_p (struct cgraph_edge *e)
376 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee,
377 NULL);
378 /* Early inliner might get called at WPA stage when IPA pass adds new
379 function. In this case we can not really do any of early inlining
380 because function bodies are missing. */
381 if (!gimple_has_body_p (callee->decl))
383 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
384 return false;
386 /* In early inliner some of callees may not be in SSA form yet
387 (i.e. the callgraph is cyclic and we did not process
388 the callee by early inliner, yet). We don't have CIF code for this
389 case; later we will re-do the decision in the real inliner. */
390 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->caller->decl))
391 || !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
393 if (dump_file)
394 fprintf (dump_file, " edge not inlinable: not in SSA form\n");
395 return false;
397 if (!can_inline_edge_p (e, true))
398 return false;
399 return true;
403 /* Return number of calls in N. Ignore cheap builtins. */
405 static int
406 num_calls (struct cgraph_node *n)
408 struct cgraph_edge *e;
409 int num = 0;
411 for (e = n->callees; e; e = e->next_callee)
412 if (!is_inexpensive_builtin (e->callee->decl))
413 num++;
414 return num;
418 /* Return true if we are interested in inlining small function. */
420 static bool
421 want_early_inline_function_p (struct cgraph_edge *e)
423 bool want_inline = true;
424 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
426 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
428 else if (!DECL_DECLARED_INLINE_P (callee->decl)
429 && !flag_inline_small_functions)
431 e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
432 report_inline_failed_reason (e);
433 want_inline = false;
435 else
437 int growth = estimate_edge_growth (e);
438 int n;
440 if (growth <= 0)
442 else if (!cgraph_maybe_hot_edge_p (e)
443 && growth > 0)
445 if (dump_file)
446 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
447 "call is cold and code would grow by %i\n",
448 xstrdup (e->caller->name ()),
449 e->caller->order,
450 xstrdup (callee->name ()), callee->order,
451 growth);
452 want_inline = false;
454 else if (growth > PARAM_VALUE (PARAM_EARLY_INLINING_INSNS))
456 if (dump_file)
457 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
458 "growth %i exceeds --param early-inlining-insns\n",
459 xstrdup (e->caller->name ()),
460 e->caller->order,
461 xstrdup (callee->name ()), callee->order,
462 growth);
463 want_inline = false;
465 else if ((n = num_calls (callee)) != 0
466 && growth * (n + 1) > PARAM_VALUE (PARAM_EARLY_INLINING_INSNS))
468 if (dump_file)
469 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
470 "growth %i exceeds --param early-inlining-insns "
471 "divided by number of calls\n",
472 xstrdup (e->caller->name ()),
473 e->caller->order,
474 xstrdup (callee->name ()), callee->order,
475 growth);
476 want_inline = false;
479 return want_inline;
482 /* Compute time of the edge->caller + edge->callee execution when inlining
483 does not happen. */
485 inline gcov_type
486 compute_uninlined_call_time (struct inline_summary *callee_info,
487 struct cgraph_edge *edge)
489 gcov_type uninlined_call_time =
490 RDIV ((gcov_type)callee_info->time * MAX (edge->frequency, 1),
491 CGRAPH_FREQ_BASE);
492 gcov_type caller_time = inline_summary (edge->caller->global.inlined_to
493 ? edge->caller->global.inlined_to
494 : edge->caller)->time;
495 return uninlined_call_time + caller_time;
498 /* Same as compute_uinlined_call_time but compute time when inlining
499 does happen. */
501 inline gcov_type
502 compute_inlined_call_time (struct cgraph_edge *edge,
503 int edge_time)
505 gcov_type caller_time = inline_summary (edge->caller->global.inlined_to
506 ? edge->caller->global.inlined_to
507 : edge->caller)->time;
508 gcov_type time = (caller_time
509 + RDIV (((gcov_type) edge_time
510 - inline_edge_summary (edge)->call_stmt_time)
511 * MAX (edge->frequency, 1), CGRAPH_FREQ_BASE));
512 /* Possible one roundoff error, but watch for overflows. */
513 gcc_checking_assert (time >= INT_MIN / 2);
514 if (time < 0)
515 time = 0;
516 return time;
519 /* Return true if the speedup for inlining E is bigger than
520 PARAM_MAX_INLINE_MIN_SPEEDUP. */
522 static bool
523 big_speedup_p (struct cgraph_edge *e)
525 gcov_type time = compute_uninlined_call_time (inline_summary (e->callee),
527 gcov_type inlined_time = compute_inlined_call_time (e,
528 estimate_edge_time (e));
529 if (time - inlined_time
530 > RDIV (time * PARAM_VALUE (PARAM_INLINE_MIN_SPEEDUP), 100))
531 return true;
532 return false;
535 /* Return true if we are interested in inlining small function.
536 When REPORT is true, report reason to dump file. */
538 static bool
539 want_inline_small_function_p (struct cgraph_edge *e, bool report)
541 bool want_inline = true;
542 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
544 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
546 else if (!DECL_DECLARED_INLINE_P (callee->decl)
547 && !flag_inline_small_functions)
549 e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
550 want_inline = false;
552 else
554 int growth = estimate_edge_growth (e);
555 inline_hints hints = estimate_edge_hints (e);
556 bool big_speedup = big_speedup_p (e);
558 if (growth <= 0)
560 /* Apply MAX_INLINE_INSNS_SINGLE limit. Do not do so when
561 hints suggests that inlining given function is very profitable. */
562 else if (DECL_DECLARED_INLINE_P (callee->decl)
563 && growth >= MAX_INLINE_INSNS_SINGLE
564 && !big_speedup
565 && !(hints & (INLINE_HINT_indirect_call
566 | INLINE_HINT_loop_iterations
567 | INLINE_HINT_array_index
568 | INLINE_HINT_loop_stride)))
570 e->inline_failed = CIF_MAX_INLINE_INSNS_SINGLE_LIMIT;
571 want_inline = false;
573 /* Before giving up based on fact that caller size will grow, allow
574 functions that are called few times and eliminating the offline
575 copy will lead to overall code size reduction.
576 Not all of these will be handled by subsequent inlining of functions
577 called once: in particular weak functions are not handled or funcitons
578 that inline to multiple calls but a lot of bodies is optimized out.
579 Finally we want to inline earlier to allow inlining of callbacks.
581 This is slightly wrong on aggressive side: it is entirely possible
582 that function is called many times with a context where inlining
583 reduces code size and few times with a context where inlining increase
584 code size. Resoluting growth estimate will be negative even if it
585 would make more sense to keep offline copy and do not inline into the
586 call sites that makes the code size grow.
588 When badness orders the calls in a way that code reducing calls come
589 first, this situation is not a problem at all: after inlining all
590 "good" calls, we will realize that keeping the function around is
591 better. */
592 else if (growth <= MAX_INLINE_INSNS_SINGLE
593 /* Unlike for functions called once, we play unsafe with
594 COMDATs. We can allow that since we know functions
595 in consideration are small (and thus risk is small) and
596 moreover grow estimates already accounts that COMDAT
597 functions may or may not disappear when eliminated from
598 current unit. With good probability making aggressive
599 choice in all units is going to make overall program
600 smaller.
602 Consequently we ask cgraph_can_remove_if_no_direct_calls_p
603 instead of
604 cgraph_will_be_removed_from_program_if_no_direct_calls */
605 && !DECL_EXTERNAL (callee->decl)
606 && cgraph_can_remove_if_no_direct_calls_p (callee)
607 && estimate_growth (callee) <= 0)
609 else if (!DECL_DECLARED_INLINE_P (callee->decl)
610 && !flag_inline_functions)
612 e->inline_failed = CIF_NOT_DECLARED_INLINED;
613 want_inline = false;
615 /* Apply MAX_INLINE_INSNS_AUTO limit for functions not declared inline
616 Upgrade it to MAX_INLINE_INSNS_SINGLE when hints suggests that
617 inlining given function is very profitable. */
618 else if (!DECL_DECLARED_INLINE_P (callee->decl)
619 && !big_speedup
620 && growth >= ((hints & (INLINE_HINT_indirect_call
621 | INLINE_HINT_loop_iterations
622 | INLINE_HINT_array_index
623 | INLINE_HINT_loop_stride))
624 ? MAX (MAX_INLINE_INSNS_AUTO,
625 MAX_INLINE_INSNS_SINGLE)
626 : MAX_INLINE_INSNS_AUTO))
628 e->inline_failed = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
629 want_inline = false;
631 /* If call is cold, do not inline when function body would grow. */
632 else if (!cgraph_maybe_hot_edge_p (e))
634 e->inline_failed = CIF_UNLIKELY_CALL;
635 want_inline = false;
638 if (!want_inline && report)
639 report_inline_failed_reason (e);
640 return want_inline;
643 /* EDGE is self recursive edge.
644 We hand two cases - when function A is inlining into itself
645 or when function A is being inlined into another inliner copy of function
646 A within function B.
648 In first case OUTER_NODE points to the toplevel copy of A, while
649 in the second case OUTER_NODE points to the outermost copy of A in B.
651 In both cases we want to be extra selective since
652 inlining the call will just introduce new recursive calls to appear. */
654 static bool
655 want_inline_self_recursive_call_p (struct cgraph_edge *edge,
656 struct cgraph_node *outer_node,
657 bool peeling,
658 int depth)
660 char const *reason = NULL;
661 bool want_inline = true;
662 int caller_freq = CGRAPH_FREQ_BASE;
663 int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO);
665 if (DECL_DECLARED_INLINE_P (edge->caller->decl))
666 max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH);
668 if (!cgraph_maybe_hot_edge_p (edge))
670 reason = "recursive call is cold";
671 want_inline = false;
673 else if (max_count && !outer_node->count)
675 reason = "not executed in profile";
676 want_inline = false;
678 else if (depth > max_depth)
680 reason = "--param max-inline-recursive-depth exceeded.";
681 want_inline = false;
684 if (outer_node->global.inlined_to)
685 caller_freq = outer_node->callers->frequency;
687 if (!want_inline)
689 /* Inlining of self recursive function into copy of itself within other function
690 is transformation similar to loop peeling.
692 Peeling is profitable if we can inline enough copies to make probability
693 of actual call to the self recursive function very small. Be sure that
694 the probability of recursion is small.
696 We ensure that the frequency of recursing is at most 1 - (1/max_depth).
697 This way the expected number of recision is at most max_depth. */
698 else if (peeling)
700 int max_prob = CGRAPH_FREQ_BASE - ((CGRAPH_FREQ_BASE + max_depth - 1)
701 / max_depth);
702 int i;
703 for (i = 1; i < depth; i++)
704 max_prob = max_prob * max_prob / CGRAPH_FREQ_BASE;
705 if (max_count
706 && (edge->count * CGRAPH_FREQ_BASE / outer_node->count
707 >= max_prob))
709 reason = "profile of recursive call is too large";
710 want_inline = false;
712 if (!max_count
713 && (edge->frequency * CGRAPH_FREQ_BASE / caller_freq
714 >= max_prob))
716 reason = "frequency of recursive call is too large";
717 want_inline = false;
720 /* Recursive inlining, i.e. equivalent of unrolling, is profitable if recursion
721 depth is large. We reduce function call overhead and increase chances that
722 things fit in hardware return predictor.
724 Recursive inlining might however increase cost of stack frame setup
725 actually slowing down functions whose recursion tree is wide rather than
726 deep.
728 Deciding reliably on when to do recursive inlining without profile feedback
729 is tricky. For now we disable recursive inlining when probability of self
730 recursion is low.
732 Recursive inlining of self recursive call within loop also results in large loop
733 depths that generally optimize badly. We may want to throttle down inlining
734 in those cases. In particular this seems to happen in one of libstdc++ rb tree
735 methods. */
736 else
738 if (max_count
739 && (edge->count * 100 / outer_node->count
740 <= PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY)))
742 reason = "profile of recursive call is too small";
743 want_inline = false;
745 else if (!max_count
746 && (edge->frequency * 100 / caller_freq
747 <= PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY)))
749 reason = "frequency of recursive call is too small";
750 want_inline = false;
753 if (!want_inline && dump_file)
754 fprintf (dump_file, " not inlining recursively: %s\n", reason);
755 return want_inline;
758 /* Return true when NODE has uninlinable caller;
759 set HAS_HOT_CALL if it has hot call.
760 Worker for cgraph_for_node_and_aliases. */
762 static bool
763 check_callers (struct cgraph_node *node, void *has_hot_call)
765 struct cgraph_edge *e;
766 for (e = node->callers; e; e = e->next_caller)
768 if (!can_inline_edge_p (e, true))
769 return true;
770 if (!(*(bool *)has_hot_call) && cgraph_maybe_hot_edge_p (e))
771 *(bool *)has_hot_call = true;
773 return false;
776 /* If NODE has a caller, return true. */
778 static bool
779 has_caller_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
781 if (node->callers)
782 return true;
783 return false;
786 /* Decide if inlining NODE would reduce unit size by eliminating
787 the offline copy of function.
788 When COLD is true the cold calls are considered, too. */
790 static bool
791 want_inline_function_to_all_callers_p (struct cgraph_node *node, bool cold)
793 struct cgraph_node *function = cgraph_function_or_thunk_node (node, NULL);
794 bool has_hot_call = false;
796 /* Does it have callers? */
797 if (!cgraph_for_node_and_aliases (node, has_caller_p, NULL, true))
798 return false;
799 /* Already inlined? */
800 if (function->global.inlined_to)
801 return false;
802 if (cgraph_function_or_thunk_node (node, NULL) != node)
803 return false;
804 /* Inlining into all callers would increase size? */
805 if (estimate_growth (node) > 0)
806 return false;
807 /* All inlines must be possible. */
808 if (cgraph_for_node_and_aliases (node, check_callers, &has_hot_call, true))
809 return false;
810 if (!cold && !has_hot_call)
811 return false;
812 return true;
815 #define RELATIVE_TIME_BENEFIT_RANGE (INT_MAX / 64)
817 /* Return relative time improvement for inlining EDGE in range
818 1...RELATIVE_TIME_BENEFIT_RANGE */
820 static inline int
821 relative_time_benefit (struct inline_summary *callee_info,
822 struct cgraph_edge *edge,
823 int edge_time)
825 gcov_type relbenefit;
826 gcov_type uninlined_call_time = compute_uninlined_call_time (callee_info, edge);
827 gcov_type inlined_call_time = compute_inlined_call_time (edge, edge_time);
829 /* Inlining into extern inline function is not a win. */
830 if (DECL_EXTERNAL (edge->caller->global.inlined_to
831 ? edge->caller->global.inlined_to->decl
832 : edge->caller->decl))
833 return 1;
835 /* Watch overflows. */
836 gcc_checking_assert (uninlined_call_time >= 0);
837 gcc_checking_assert (inlined_call_time >= 0);
838 gcc_checking_assert (uninlined_call_time >= inlined_call_time);
840 /* Compute relative time benefit, i.e. how much the call becomes faster.
841 ??? perhaps computing how much the caller+calle together become faster
842 would lead to more realistic results. */
843 if (!uninlined_call_time)
844 uninlined_call_time = 1;
845 relbenefit =
846 RDIV (((gcov_type)uninlined_call_time - inlined_call_time) * RELATIVE_TIME_BENEFIT_RANGE,
847 uninlined_call_time);
848 relbenefit = MIN (relbenefit, RELATIVE_TIME_BENEFIT_RANGE);
849 gcc_checking_assert (relbenefit >= 0);
850 relbenefit = MAX (relbenefit, 1);
851 return relbenefit;
855 /* A cost model driving the inlining heuristics in a way so the edges with
856 smallest badness are inlined first. After each inlining is performed
857 the costs of all caller edges of nodes affected are recomputed so the
858 metrics may accurately depend on values such as number of inlinable callers
859 of the function or function body size. */
861 static int
862 edge_badness (struct cgraph_edge *edge, bool dump)
864 gcov_type badness;
865 int growth, edge_time;
866 struct cgraph_node *callee = cgraph_function_or_thunk_node (edge->callee,
867 NULL);
868 struct inline_summary *callee_info = inline_summary (callee);
869 inline_hints hints;
871 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
872 return INT_MIN;
874 growth = estimate_edge_growth (edge);
875 edge_time = estimate_edge_time (edge);
876 hints = estimate_edge_hints (edge);
877 gcc_checking_assert (edge_time >= 0);
878 gcc_checking_assert (edge_time <= callee_info->time);
879 gcc_checking_assert (growth <= callee_info->size);
881 if (dump)
883 fprintf (dump_file, " Badness calculation for %s/%i -> %s/%i\n",
884 xstrdup (edge->caller->name ()),
885 edge->caller->order,
886 xstrdup (callee->name ()),
887 edge->callee->order);
888 fprintf (dump_file, " size growth %i, time %i ",
889 growth,
890 edge_time);
891 dump_inline_hints (dump_file, hints);
892 if (big_speedup_p (edge))
893 fprintf (dump_file, " big_speedup");
894 fprintf (dump_file, "\n");
897 /* Always prefer inlining saving code size. */
898 if (growth <= 0)
900 badness = INT_MIN / 2 + growth;
901 if (dump)
902 fprintf (dump_file, " %i: Growth %i <= 0\n", (int) badness,
903 growth);
906 /* When profiling is available, compute badness as:
908 relative_edge_count * relative_time_benefit
909 goodness = -------------------------------------------
910 growth_f_caller
911 badness = -goodness
913 The fraction is upside down, because on edge counts and time beneits
914 the bounds are known. Edge growth is essentially unlimited. */
916 else if (max_count)
918 sreal tmp, relbenefit_real, growth_real;
919 int relbenefit = relative_time_benefit (callee_info, edge, edge_time);
920 /* Capping edge->count to max_count. edge->count can be larger than
921 max_count if an inline adds new edges which increase max_count
922 after max_count is computed. */
923 gcov_type edge_count = edge->count > max_count ? max_count : edge->count;
925 sreal_init (&relbenefit_real, relbenefit, 0);
926 sreal_init (&growth_real, growth, 0);
928 /* relative_edge_count. */
929 sreal_init (&tmp, edge_count, 0);
930 sreal_div (&tmp, &tmp, &max_count_real);
932 /* relative_time_benefit. */
933 sreal_mul (&tmp, &tmp, &relbenefit_real);
934 sreal_div (&tmp, &tmp, &max_relbenefit_real);
936 /* growth_f_caller. */
937 sreal_mul (&tmp, &tmp, &half_int_min_real);
938 sreal_div (&tmp, &tmp, &growth_real);
940 badness = -1 * sreal_to_int (&tmp);
942 if (dump)
944 fprintf (dump_file,
945 " %i (relative %f): profile info. Relative count %f%s"
946 " * Relative benefit %f\n",
947 (int) badness, (double) badness / INT_MIN,
948 (double) edge_count / max_count,
949 edge->count > max_count ? " (capped to max_count)" : "",
950 relbenefit * 100.0 / RELATIVE_TIME_BENEFIT_RANGE);
954 /* When function local profile is available. Compute badness as:
956 relative_time_benefit
957 goodness = ---------------------------------
958 growth_of_caller * overall_growth
960 badness = - goodness
962 compensated by the inline hints.
964 else if (flag_guess_branch_prob)
966 badness = (relative_time_benefit (callee_info, edge, edge_time)
967 * (INT_MIN / 16 / RELATIVE_TIME_BENEFIT_RANGE));
968 badness /= (MIN (65536/2, growth) * MIN (65536/2, MAX (1, callee_info->growth)));
969 gcc_checking_assert (badness <=0 && badness >= INT_MIN / 16);
970 if ((hints & (INLINE_HINT_indirect_call
971 | INLINE_HINT_loop_iterations
972 | INLINE_HINT_array_index
973 | INLINE_HINT_loop_stride))
974 || callee_info->growth <= 0)
975 badness *= 8;
976 if (hints & (INLINE_HINT_same_scc))
977 badness /= 16;
978 else if (hints & (INLINE_HINT_in_scc))
979 badness /= 8;
980 else if (hints & (INLINE_HINT_cross_module))
981 badness /= 2;
982 gcc_checking_assert (badness <= 0 && badness >= INT_MIN / 2);
983 if ((hints & INLINE_HINT_declared_inline) && badness >= INT_MIN / 32)
984 badness *= 16;
985 if (dump)
987 fprintf (dump_file,
988 " %i: guessed profile. frequency %f,"
989 " benefit %f%%, time w/o inlining %i, time w inlining %i"
990 " overall growth %i (current) %i (original)\n",
991 (int) badness, (double)edge->frequency / CGRAPH_FREQ_BASE,
992 relative_time_benefit (callee_info, edge, edge_time) * 100.0
993 / RELATIVE_TIME_BENEFIT_RANGE,
994 (int)compute_uninlined_call_time (callee_info, edge),
995 (int)compute_inlined_call_time (edge, edge_time),
996 estimate_growth (callee),
997 callee_info->growth);
1000 /* When function local profile is not available or it does not give
1001 useful information (ie frequency is zero), base the cost on
1002 loop nest and overall size growth, so we optimize for overall number
1003 of functions fully inlined in program. */
1004 else
1006 int nest = MIN (inline_edge_summary (edge)->loop_depth, 8);
1007 badness = growth * 256;
1009 /* Decrease badness if call is nested. */
1010 if (badness > 0)
1011 badness >>= nest;
1012 else
1014 badness <<= nest;
1016 if (dump)
1017 fprintf (dump_file, " %i: no profile. nest %i\n", (int) badness,
1018 nest);
1021 /* Ensure that we did not overflow in all the fixed point math above. */
1022 gcc_assert (badness >= INT_MIN);
1023 gcc_assert (badness <= INT_MAX - 1);
1024 /* Make recursive inlining happen always after other inlining is done. */
1025 if (cgraph_edge_recursive_p (edge))
1026 return badness + 1;
1027 else
1028 return badness;
1031 /* Recompute badness of EDGE and update its key in HEAP if needed. */
1032 static inline void
1033 update_edge_key (fibheap_t heap, struct cgraph_edge *edge)
1035 int badness = edge_badness (edge, false);
1036 if (edge->aux)
1038 fibnode_t n = (fibnode_t) edge->aux;
1039 gcc_checking_assert (n->data == edge);
1041 /* fibheap_replace_key only decrease the keys.
1042 When we increase the key we do not update heap
1043 and instead re-insert the element once it becomes
1044 a minimum of heap. */
1045 if (badness < n->key)
1047 if (dump_file && (dump_flags & TDF_DETAILS))
1049 fprintf (dump_file,
1050 " decreasing badness %s/%i -> %s/%i, %i to %i\n",
1051 xstrdup (edge->caller->name ()),
1052 edge->caller->order,
1053 xstrdup (edge->callee->name ()),
1054 edge->callee->order,
1055 (int)n->key,
1056 badness);
1058 fibheap_replace_key (heap, n, badness);
1059 gcc_checking_assert (n->key == badness);
1062 else
1064 if (dump_file && (dump_flags & TDF_DETAILS))
1066 fprintf (dump_file,
1067 " enqueuing call %s/%i -> %s/%i, badness %i\n",
1068 xstrdup (edge->caller->name ()),
1069 edge->caller->order,
1070 xstrdup (edge->callee->name ()),
1071 edge->callee->order,
1072 badness);
1074 edge->aux = fibheap_insert (heap, badness, edge);
1079 /* NODE was inlined.
1080 All caller edges needs to be resetted because
1081 size estimates change. Similarly callees needs reset
1082 because better context may be known. */
1084 static void
1085 reset_edge_caches (struct cgraph_node *node)
1087 struct cgraph_edge *edge;
1088 struct cgraph_edge *e = node->callees;
1089 struct cgraph_node *where = node;
1090 int i;
1091 struct ipa_ref *ref;
1093 if (where->global.inlined_to)
1094 where = where->global.inlined_to;
1096 /* WHERE body size has changed, the cached growth is invalid. */
1097 reset_node_growth_cache (where);
1099 for (edge = where->callers; edge; edge = edge->next_caller)
1100 if (edge->inline_failed)
1101 reset_edge_growth_cache (edge);
1102 for (i = 0; ipa_ref_list_referring_iterate (&where->ref_list,
1103 i, ref); i++)
1104 if (ref->use == IPA_REF_ALIAS)
1105 reset_edge_caches (ipa_ref_referring_node (ref));
1107 if (!e)
1108 return;
1110 while (true)
1111 if (!e->inline_failed && e->callee->callees)
1112 e = e->callee->callees;
1113 else
1115 if (e->inline_failed)
1116 reset_edge_growth_cache (e);
1117 if (e->next_callee)
1118 e = e->next_callee;
1119 else
1123 if (e->caller == node)
1124 return;
1125 e = e->caller->callers;
1127 while (!e->next_callee);
1128 e = e->next_callee;
1133 /* Recompute HEAP nodes for each of caller of NODE.
1134 UPDATED_NODES track nodes we already visited, to avoid redundant work.
1135 When CHECK_INLINABLITY_FOR is set, re-check for specified edge that
1136 it is inlinable. Otherwise check all edges. */
1138 static void
1139 update_caller_keys (fibheap_t heap, struct cgraph_node *node,
1140 bitmap updated_nodes,
1141 struct cgraph_edge *check_inlinablity_for)
1143 struct cgraph_edge *edge;
1144 int i;
1145 struct ipa_ref *ref;
1147 if ((!node->alias && !inline_summary (node)->inlinable)
1148 || node->global.inlined_to)
1149 return;
1150 if (!bitmap_set_bit (updated_nodes, node->uid))
1151 return;
1153 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list,
1154 i, ref); i++)
1155 if (ref->use == IPA_REF_ALIAS)
1157 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1158 update_caller_keys (heap, alias, updated_nodes, check_inlinablity_for);
1161 for (edge = node->callers; edge; edge = edge->next_caller)
1162 if (edge->inline_failed)
1164 if (!check_inlinablity_for
1165 || check_inlinablity_for == edge)
1167 if (can_inline_edge_p (edge, false)
1168 && want_inline_small_function_p (edge, false))
1169 update_edge_key (heap, edge);
1170 else if (edge->aux)
1172 report_inline_failed_reason (edge);
1173 fibheap_delete_node (heap, (fibnode_t) edge->aux);
1174 edge->aux = NULL;
1177 else if (edge->aux)
1178 update_edge_key (heap, edge);
1182 /* Recompute HEAP nodes for each uninlined call in NODE.
1183 This is used when we know that edge badnesses are going only to increase
1184 (we introduced new call site) and thus all we need is to insert newly
1185 created edges into heap. */
1187 static void
1188 update_callee_keys (fibheap_t heap, struct cgraph_node *node,
1189 bitmap updated_nodes)
1191 struct cgraph_edge *e = node->callees;
1193 if (!e)
1194 return;
1195 while (true)
1196 if (!e->inline_failed && e->callee->callees)
1197 e = e->callee->callees;
1198 else
1200 enum availability avail;
1201 struct cgraph_node *callee;
1202 /* We do not reset callee growth cache here. Since we added a new call,
1203 growth chould have just increased and consequentely badness metric
1204 don't need updating. */
1205 if (e->inline_failed
1206 && (callee = cgraph_function_or_thunk_node (e->callee, &avail))
1207 && inline_summary (callee)->inlinable
1208 && avail >= AVAIL_AVAILABLE
1209 && !bitmap_bit_p (updated_nodes, callee->uid))
1211 if (can_inline_edge_p (e, false)
1212 && want_inline_small_function_p (e, false))
1213 update_edge_key (heap, e);
1214 else if (e->aux)
1216 report_inline_failed_reason (e);
1217 fibheap_delete_node (heap, (fibnode_t) e->aux);
1218 e->aux = NULL;
1221 if (e->next_callee)
1222 e = e->next_callee;
1223 else
1227 if (e->caller == node)
1228 return;
1229 e = e->caller->callers;
1231 while (!e->next_callee);
1232 e = e->next_callee;
1237 /* Enqueue all recursive calls from NODE into priority queue depending on
1238 how likely we want to recursively inline the call. */
1240 static void
1241 lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
1242 fibheap_t heap)
1244 struct cgraph_edge *e;
1245 enum availability avail;
1247 for (e = where->callees; e; e = e->next_callee)
1248 if (e->callee == node
1249 || (cgraph_function_or_thunk_node (e->callee, &avail) == node
1250 && avail > AVAIL_OVERWRITABLE))
1252 /* When profile feedback is available, prioritize by expected number
1253 of calls. */
1254 fibheap_insert (heap,
1255 !max_count ? -e->frequency
1256 : -(e->count / ((max_count + (1<<24) - 1) / (1<<24))),
1259 for (e = where->callees; e; e = e->next_callee)
1260 if (!e->inline_failed)
1261 lookup_recursive_calls (node, e->callee, heap);
1264 /* Decide on recursive inlining: in the case function has recursive calls,
1265 inline until body size reaches given argument. If any new indirect edges
1266 are discovered in the process, add them to *NEW_EDGES, unless NEW_EDGES
1267 is NULL. */
1269 static bool
1270 recursive_inlining (struct cgraph_edge *edge,
1271 vec<cgraph_edge_p> *new_edges)
1273 int limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO);
1274 fibheap_t heap;
1275 struct cgraph_node *node;
1276 struct cgraph_edge *e;
1277 struct cgraph_node *master_clone = NULL, *next;
1278 int depth = 0;
1279 int n = 0;
1281 node = edge->caller;
1282 if (node->global.inlined_to)
1283 node = node->global.inlined_to;
1285 if (DECL_DECLARED_INLINE_P (node->decl))
1286 limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE);
1288 /* Make sure that function is small enough to be considered for inlining. */
1289 if (estimate_size_after_inlining (node, edge) >= limit)
1290 return false;
1291 heap = fibheap_new ();
1292 lookup_recursive_calls (node, node, heap);
1293 if (fibheap_empty (heap))
1295 fibheap_delete (heap);
1296 return false;
1299 if (dump_file)
1300 fprintf (dump_file,
1301 " Performing recursive inlining on %s\n",
1302 node->name ());
1304 /* Do the inlining and update list of recursive call during process. */
1305 while (!fibheap_empty (heap))
1307 struct cgraph_edge *curr
1308 = (struct cgraph_edge *) fibheap_extract_min (heap);
1309 struct cgraph_node *cnode, *dest = curr->callee;
1311 if (!can_inline_edge_p (curr, true))
1312 continue;
1314 /* MASTER_CLONE is produced in the case we already started modified
1315 the function. Be sure to redirect edge to the original body before
1316 estimating growths otherwise we will be seeing growths after inlining
1317 the already modified body. */
1318 if (master_clone)
1320 cgraph_redirect_edge_callee (curr, master_clone);
1321 reset_edge_growth_cache (curr);
1324 if (estimate_size_after_inlining (node, curr) > limit)
1326 cgraph_redirect_edge_callee (curr, dest);
1327 reset_edge_growth_cache (curr);
1328 break;
1331 depth = 1;
1332 for (cnode = curr->caller;
1333 cnode->global.inlined_to; cnode = cnode->callers->caller)
1334 if (node->decl
1335 == cgraph_function_or_thunk_node (curr->callee, NULL)->decl)
1336 depth++;
1338 if (!want_inline_self_recursive_call_p (curr, node, false, depth))
1340 cgraph_redirect_edge_callee (curr, dest);
1341 reset_edge_growth_cache (curr);
1342 continue;
1345 if (dump_file)
1347 fprintf (dump_file,
1348 " Inlining call of depth %i", depth);
1349 if (node->count)
1351 fprintf (dump_file, " called approx. %.2f times per call",
1352 (double)curr->count / node->count);
1354 fprintf (dump_file, "\n");
1356 if (!master_clone)
1358 /* We need original clone to copy around. */
1359 master_clone = cgraph_clone_node (node, node->decl,
1360 node->count, CGRAPH_FREQ_BASE,
1361 false, vNULL, true, NULL);
1362 for (e = master_clone->callees; e; e = e->next_callee)
1363 if (!e->inline_failed)
1364 clone_inlined_nodes (e, true, false, NULL);
1365 cgraph_redirect_edge_callee (curr, master_clone);
1366 reset_edge_growth_cache (curr);
1369 inline_call (curr, false, new_edges, &overall_size, true);
1370 lookup_recursive_calls (node, curr->callee, heap);
1371 n++;
1374 if (!fibheap_empty (heap) && dump_file)
1375 fprintf (dump_file, " Recursive inlining growth limit met.\n");
1376 fibheap_delete (heap);
1378 if (!master_clone)
1379 return false;
1381 if (dump_file)
1382 fprintf (dump_file,
1383 "\n Inlined %i times, "
1384 "body grown from size %i to %i, time %i to %i\n", n,
1385 inline_summary (master_clone)->size, inline_summary (node)->size,
1386 inline_summary (master_clone)->time, inline_summary (node)->time);
1388 /* Remove master clone we used for inlining. We rely that clones inlined
1389 into master clone gets queued just before master clone so we don't
1390 need recursion. */
1391 for (node = cgraph_first_function (); node != master_clone;
1392 node = next)
1394 next = cgraph_next_function (node);
1395 if (node->global.inlined_to == master_clone)
1396 cgraph_remove_node (node);
1398 cgraph_remove_node (master_clone);
1399 return true;
1403 /* Given whole compilation unit estimate of INSNS, compute how large we can
1404 allow the unit to grow. */
1406 static int
1407 compute_max_insns (int insns)
1409 int max_insns = insns;
1410 if (max_insns < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
1411 max_insns = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
1413 return ((HOST_WIDEST_INT) max_insns
1414 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
1418 /* Compute badness of all edges in NEW_EDGES and add them to the HEAP. */
1420 static void
1421 add_new_edges_to_heap (fibheap_t heap, vec<cgraph_edge_p> new_edges)
1423 while (new_edges.length () > 0)
1425 struct cgraph_edge *edge = new_edges.pop ();
1427 gcc_assert (!edge->aux);
1428 if (edge->inline_failed
1429 && can_inline_edge_p (edge, true)
1430 && want_inline_small_function_p (edge, true))
1431 edge->aux = fibheap_insert (heap, edge_badness (edge, false), edge);
1435 /* Remove EDGE from the fibheap. */
1437 static void
1438 heap_edge_removal_hook (struct cgraph_edge *e, void *data)
1440 if (e->callee)
1441 reset_node_growth_cache (e->callee);
1442 if (e->aux)
1444 fibheap_delete_node ((fibheap_t)data, (fibnode_t)e->aux);
1445 e->aux = NULL;
1449 /* Return true if speculation of edge E seems useful.
1450 If ANTICIPATE_INLINING is true, be conservative and hope that E
1451 may get inlined. */
1453 bool
1454 speculation_useful_p (struct cgraph_edge *e, bool anticipate_inlining)
1456 enum availability avail;
1457 struct cgraph_node *target = cgraph_function_or_thunk_node (e->callee, &avail);
1458 struct cgraph_edge *direct, *indirect;
1459 struct ipa_ref *ref;
1461 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1463 if (!cgraph_maybe_hot_edge_p (e))
1464 return false;
1466 /* See if IP optimizations found something potentially useful about the
1467 function. For now we look only for CONST/PURE flags. Almost everything
1468 else we propagate is useless. */
1469 if (avail >= AVAIL_AVAILABLE)
1471 int ecf_flags = flags_from_decl_or_type (target->decl);
1472 if (ecf_flags & ECF_CONST)
1474 cgraph_speculative_call_info (e, direct, indirect, ref);
1475 if (!(indirect->indirect_info->ecf_flags & ECF_CONST))
1476 return true;
1478 else if (ecf_flags & ECF_PURE)
1480 cgraph_speculative_call_info (e, direct, indirect, ref);
1481 if (!(indirect->indirect_info->ecf_flags & ECF_PURE))
1482 return true;
1485 /* If we did not managed to inline the function nor redirect
1486 to an ipa-cp clone (that are seen by having local flag set),
1487 it is probably pointless to inline it unless hardware is missing
1488 indirect call predictor. */
1489 if (!anticipate_inlining && e->inline_failed && !target->local.local)
1490 return false;
1491 /* For overwritable targets there is not much to do. */
1492 if (e->inline_failed && !can_inline_edge_p (e, false, true))
1493 return false;
1494 /* OK, speculation seems interesting. */
1495 return true;
1498 /* We know that EDGE is not going to be inlined.
1499 See if we can remove speculation. */
1501 static void
1502 resolve_noninline_speculation (fibheap_t edge_heap, struct cgraph_edge *edge)
1504 if (edge->speculative && !speculation_useful_p (edge, false))
1506 struct cgraph_node *node = edge->caller;
1507 struct cgraph_node *where = node->global.inlined_to
1508 ? node->global.inlined_to : node;
1509 bitmap updated_nodes = BITMAP_ALLOC (NULL);
1511 cgraph_resolve_speculation (edge, NULL);
1512 reset_edge_caches (where);
1513 inline_update_overall_summary (where);
1514 update_caller_keys (edge_heap, where,
1515 updated_nodes, NULL);
1516 update_callee_keys (edge_heap, where,
1517 updated_nodes);
1518 BITMAP_FREE (updated_nodes);
1522 /* We use greedy algorithm for inlining of small functions:
1523 All inline candidates are put into prioritized heap ordered in
1524 increasing badness.
1526 The inlining of small functions is bounded by unit growth parameters. */
1528 static void
1529 inline_small_functions (void)
1531 struct cgraph_node *node;
1532 struct cgraph_edge *edge;
1533 fibheap_t edge_heap = fibheap_new ();
1534 bitmap updated_nodes = BITMAP_ALLOC (NULL);
1535 int min_size, max_size;
1536 auto_vec<cgraph_edge_p> new_indirect_edges;
1537 int initial_size = 0;
1538 struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1539 struct cgraph_edge_hook_list *edge_removal_hook_holder;
1541 if (flag_indirect_inlining)
1542 new_indirect_edges.create (8);
1544 edge_removal_hook_holder
1545 = cgraph_add_edge_removal_hook (&heap_edge_removal_hook, edge_heap);
1547 /* Compute overall unit size and other global parameters used by badness
1548 metrics. */
1550 max_count = 0;
1551 ipa_reduced_postorder (order, true, true, NULL);
1552 free (order);
1554 FOR_EACH_DEFINED_FUNCTION (node)
1555 if (!node->global.inlined_to)
1557 if (cgraph_function_with_gimple_body_p (node)
1558 || node->thunk.thunk_p)
1560 struct inline_summary *info = inline_summary (node);
1561 struct ipa_dfs_info *dfs = (struct ipa_dfs_info *) node->aux;
1563 if (!DECL_EXTERNAL (node->decl))
1564 initial_size += info->size;
1565 info->growth = estimate_growth (node);
1566 if (dfs && dfs->next_cycle)
1568 struct cgraph_node *n2;
1569 int id = dfs->scc_no + 1;
1570 for (n2 = node; n2;
1571 n2 = ((struct ipa_dfs_info *) node->aux)->next_cycle)
1573 struct inline_summary *info2 = inline_summary (n2);
1574 if (info2->scc_no)
1575 break;
1576 info2->scc_no = id;
1581 for (edge = node->callers; edge; edge = edge->next_caller)
1582 if (max_count < edge->count)
1583 max_count = edge->count;
1585 sreal_init (&max_count_real, max_count, 0);
1586 sreal_init (&max_relbenefit_real, RELATIVE_TIME_BENEFIT_RANGE, 0);
1587 sreal_init (&half_int_min_real, INT_MAX / 2, 0);
1588 ipa_free_postorder_info ();
1589 initialize_growth_caches ();
1591 if (dump_file)
1592 fprintf (dump_file,
1593 "\nDeciding on inlining of small functions. Starting with size %i.\n",
1594 initial_size);
1596 overall_size = initial_size;
1597 max_size = compute_max_insns (overall_size);
1598 min_size = overall_size;
1600 /* Populate the heap with all edges we might inline. */
1602 FOR_EACH_DEFINED_FUNCTION (node)
1604 bool update = false;
1605 struct cgraph_edge *next;
1607 if (dump_file)
1608 fprintf (dump_file, "Enqueueing calls in %s/%i.\n",
1609 node->name (), node->order);
1611 for (edge = node->callees; edge; edge = next)
1613 next = edge->next_callee;
1614 if (edge->inline_failed
1615 && !edge->aux
1616 && can_inline_edge_p (edge, true)
1617 && want_inline_small_function_p (edge, true)
1618 && edge->inline_failed)
1620 gcc_assert (!edge->aux);
1621 update_edge_key (edge_heap, edge);
1623 if (edge->speculative && !speculation_useful_p (edge, edge->aux != NULL))
1625 cgraph_resolve_speculation (edge, NULL);
1626 update = true;
1629 if (update)
1631 struct cgraph_node *where = node->global.inlined_to
1632 ? node->global.inlined_to : node;
1633 inline_update_overall_summary (where);
1634 reset_node_growth_cache (where);
1635 reset_edge_caches (where);
1636 update_caller_keys (edge_heap, where,
1637 updated_nodes, NULL);
1638 bitmap_clear (updated_nodes);
1642 gcc_assert (in_lto_p
1643 || !max_count
1644 || (profile_info && flag_branch_probabilities));
1646 while (!fibheap_empty (edge_heap))
1648 int old_size = overall_size;
1649 struct cgraph_node *where, *callee;
1650 int badness = fibheap_min_key (edge_heap);
1651 int current_badness;
1652 int cached_badness;
1653 int growth;
1655 edge = (struct cgraph_edge *) fibheap_extract_min (edge_heap);
1656 gcc_assert (edge->aux);
1657 edge->aux = NULL;
1658 if (!edge->inline_failed)
1659 continue;
1661 /* Be sure that caches are maintained consistent.
1662 We can not make this ENABLE_CHECKING only because it cause different
1663 updates of the fibheap queue. */
1664 cached_badness = edge_badness (edge, false);
1665 reset_edge_growth_cache (edge);
1666 reset_node_growth_cache (edge->callee);
1668 /* When updating the edge costs, we only decrease badness in the keys.
1669 Increases of badness are handled lazilly; when we see key with out
1670 of date value on it, we re-insert it now. */
1671 current_badness = edge_badness (edge, false);
1672 gcc_assert (cached_badness == current_badness);
1673 gcc_assert (current_badness >= badness);
1674 if (current_badness != badness)
1676 edge->aux = fibheap_insert (edge_heap, current_badness, edge);
1677 continue;
1680 if (!can_inline_edge_p (edge, true))
1682 resolve_noninline_speculation (edge_heap, edge);
1683 continue;
1686 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
1687 growth = estimate_edge_growth (edge);
1688 if (dump_file)
1690 fprintf (dump_file,
1691 "\nConsidering %s/%i with %i size\n",
1692 callee->name (), callee->order,
1693 inline_summary (callee)->size);
1694 fprintf (dump_file,
1695 " to be inlined into %s/%i in %s:%i\n"
1696 " Estimated growth after inlined into all is %+i insns.\n"
1697 " Estimated badness is %i, frequency %.2f.\n",
1698 edge->caller->name (), edge->caller->order,
1699 flag_wpa ? "unknown"
1700 : gimple_filename ((const_gimple) edge->call_stmt),
1701 flag_wpa ? -1
1702 : gimple_lineno ((const_gimple) edge->call_stmt),
1703 estimate_growth (callee),
1704 badness,
1705 edge->frequency / (double)CGRAPH_FREQ_BASE);
1706 if (edge->count)
1707 fprintf (dump_file," Called "HOST_WIDEST_INT_PRINT_DEC"x\n",
1708 edge->count);
1709 if (dump_flags & TDF_DETAILS)
1710 edge_badness (edge, true);
1713 if (overall_size + growth > max_size
1714 && !DECL_DISREGARD_INLINE_LIMITS (callee->decl))
1716 edge->inline_failed = CIF_INLINE_UNIT_GROWTH_LIMIT;
1717 report_inline_failed_reason (edge);
1718 resolve_noninline_speculation (edge_heap, edge);
1719 continue;
1722 if (!want_inline_small_function_p (edge, true))
1724 resolve_noninline_speculation (edge_heap, edge);
1725 continue;
1728 /* Heuristics for inlining small functions works poorly for
1729 recursive calls where we do efect similar to loop unrolling.
1730 When inliing such edge seems profitable, leave decision on
1731 specific inliner. */
1732 if (cgraph_edge_recursive_p (edge))
1734 where = edge->caller;
1735 if (where->global.inlined_to)
1736 where = where->global.inlined_to;
1737 if (!recursive_inlining (edge,
1738 flag_indirect_inlining
1739 ? &new_indirect_edges : NULL))
1741 edge->inline_failed = CIF_RECURSIVE_INLINING;
1742 resolve_noninline_speculation (edge_heap, edge);
1743 continue;
1745 reset_edge_caches (where);
1746 /* Recursive inliner inlines all recursive calls of the function
1747 at once. Consequently we need to update all callee keys. */
1748 if (flag_indirect_inlining)
1749 add_new_edges_to_heap (edge_heap, new_indirect_edges);
1750 update_callee_keys (edge_heap, where, updated_nodes);
1751 bitmap_clear (updated_nodes);
1753 else
1755 struct cgraph_node *outer_node = NULL;
1756 int depth = 0;
1758 /* Consider the case where self recursive function A is inlined into B.
1759 This is desired optimization in some cases, since it leads to effect
1760 similar of loop peeling and we might completely optimize out the
1761 recursive call. However we must be extra selective. */
1763 where = edge->caller;
1764 while (where->global.inlined_to)
1766 if (where->decl == callee->decl)
1767 outer_node = where, depth++;
1768 where = where->callers->caller;
1770 if (outer_node
1771 && !want_inline_self_recursive_call_p (edge, outer_node,
1772 true, depth))
1774 edge->inline_failed
1775 = (DECL_DISREGARD_INLINE_LIMITS (edge->callee->decl)
1776 ? CIF_RECURSIVE_INLINING : CIF_UNSPECIFIED);
1777 resolve_noninline_speculation (edge_heap, edge);
1778 continue;
1780 else if (depth && dump_file)
1781 fprintf (dump_file, " Peeling recursion with depth %i\n", depth);
1783 gcc_checking_assert (!callee->global.inlined_to);
1784 inline_call (edge, true, &new_indirect_edges, &overall_size, true);
1785 if (flag_indirect_inlining)
1786 add_new_edges_to_heap (edge_heap, new_indirect_edges);
1788 reset_edge_caches (edge->callee);
1789 reset_node_growth_cache (callee);
1791 update_callee_keys (edge_heap, where, updated_nodes);
1793 where = edge->caller;
1794 if (where->global.inlined_to)
1795 where = where->global.inlined_to;
1797 /* Our profitability metric can depend on local properties
1798 such as number of inlinable calls and size of the function body.
1799 After inlining these properties might change for the function we
1800 inlined into (since it's body size changed) and for the functions
1801 called by function we inlined (since number of it inlinable callers
1802 might change). */
1803 update_caller_keys (edge_heap, where, updated_nodes, NULL);
1804 bitmap_clear (updated_nodes);
1806 if (dump_file)
1808 fprintf (dump_file,
1809 " Inlined into %s which now has time %i and size %i,"
1810 "net change of %+i.\n",
1811 edge->caller->name (),
1812 inline_summary (edge->caller)->time,
1813 inline_summary (edge->caller)->size,
1814 overall_size - old_size);
1816 if (min_size > overall_size)
1818 min_size = overall_size;
1819 max_size = compute_max_insns (min_size);
1821 if (dump_file)
1822 fprintf (dump_file, "New minimal size reached: %i\n", min_size);
1826 free_growth_caches ();
1827 fibheap_delete (edge_heap);
1828 if (dump_file)
1829 fprintf (dump_file,
1830 "Unit growth for small function inlining: %i->%i (%i%%)\n",
1831 initial_size, overall_size,
1832 initial_size ? overall_size * 100 / (initial_size) - 100: 0);
1833 BITMAP_FREE (updated_nodes);
1834 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
1837 /* Flatten NODE. Performed both during early inlining and
1838 at IPA inlining time. */
1840 static void
1841 flatten_function (struct cgraph_node *node, bool early)
1843 struct cgraph_edge *e;
1845 /* We shouldn't be called recursively when we are being processed. */
1846 gcc_assert (node->aux == NULL);
1848 node->aux = (void *) node;
1850 for (e = node->callees; e; e = e->next_callee)
1852 struct cgraph_node *orig_callee;
1853 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
1855 /* We've hit cycle? It is time to give up. */
1856 if (callee->aux)
1858 if (dump_file)
1859 fprintf (dump_file,
1860 "Not inlining %s into %s to avoid cycle.\n",
1861 xstrdup (callee->name ()),
1862 xstrdup (e->caller->name ()));
1863 e->inline_failed = CIF_RECURSIVE_INLINING;
1864 continue;
1867 /* When the edge is already inlined, we just need to recurse into
1868 it in order to fully flatten the leaves. */
1869 if (!e->inline_failed)
1871 flatten_function (callee, early);
1872 continue;
1875 /* Flatten attribute needs to be processed during late inlining. For
1876 extra code quality we however do flattening during early optimization,
1877 too. */
1878 if (!early
1879 ? !can_inline_edge_p (e, true)
1880 : !can_early_inline_edge_p (e))
1881 continue;
1883 if (cgraph_edge_recursive_p (e))
1885 if (dump_file)
1886 fprintf (dump_file, "Not inlining: recursive call.\n");
1887 continue;
1890 if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
1891 != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
1893 if (dump_file)
1894 fprintf (dump_file, "Not inlining: SSA form does not match.\n");
1895 continue;
1898 /* Inline the edge and flatten the inline clone. Avoid
1899 recursing through the original node if the node was cloned. */
1900 if (dump_file)
1901 fprintf (dump_file, " Inlining %s into %s.\n",
1902 xstrdup (callee->name ()),
1903 xstrdup (e->caller->name ()));
1904 orig_callee = callee;
1905 inline_call (e, true, NULL, NULL, false);
1906 if (e->callee != orig_callee)
1907 orig_callee->aux = (void *) node;
1908 flatten_function (e->callee, early);
1909 if (e->callee != orig_callee)
1910 orig_callee->aux = NULL;
1913 node->aux = NULL;
1914 if (!node->global.inlined_to)
1915 inline_update_overall_summary (node);
1918 /* Count number of callers of NODE and store it into DATA (that
1919 points to int. Worker for cgraph_for_node_and_aliases. */
1921 static bool
1922 sum_callers (struct cgraph_node *node, void *data)
1924 struct cgraph_edge *e;
1925 int *num_calls = (int *)data;
1927 for (e = node->callers; e; e = e->next_caller)
1928 (*num_calls)++;
1929 return false;
1932 /* Inline NODE to all callers. Worker for cgraph_for_node_and_aliases.
1933 DATA points to number of calls originally found so we avoid infinite
1934 recursion. */
1936 static bool
1937 inline_to_all_callers (struct cgraph_node *node, void *data)
1939 int *num_calls = (int *)data;
1940 while (node->callers && !node->global.inlined_to)
1942 struct cgraph_node *caller = node->callers->caller;
1944 if (dump_file)
1946 fprintf (dump_file,
1947 "\nInlining %s size %i.\n",
1948 node->name (),
1949 inline_summary (node)->size);
1950 fprintf (dump_file,
1951 " Called once from %s %i insns.\n",
1952 node->callers->caller->name (),
1953 inline_summary (node->callers->caller)->size);
1956 inline_call (node->callers, true, NULL, NULL, true);
1957 if (dump_file)
1958 fprintf (dump_file,
1959 " Inlined into %s which now has %i size\n",
1960 caller->name (),
1961 inline_summary (caller)->size);
1962 if (!(*num_calls)--)
1964 if (dump_file)
1965 fprintf (dump_file, "New calls found; giving up.\n");
1966 return true;
1969 return false;
1972 /* Decide on the inlining. We do so in the topological order to avoid
1973 expenses on updating data structures. */
1975 static unsigned int
1976 ipa_inline (void)
1978 struct cgraph_node *node;
1979 int nnodes;
1980 struct cgraph_node **order;
1981 int i;
1982 int cold;
1983 bool remove_functions = false;
1985 if (!optimize)
1986 return 0;
1988 order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1990 if (in_lto_p && optimize)
1991 ipa_update_after_lto_read ();
1993 if (dump_file)
1994 dump_inline_summaries (dump_file);
1996 nnodes = ipa_reverse_postorder (order);
1998 FOR_EACH_FUNCTION (node)
1999 node->aux = 0;
2001 if (dump_file)
2002 fprintf (dump_file, "\nFlattening functions:\n");
2004 /* In the first pass handle functions to be flattened. Do this with
2005 a priority so none of our later choices will make this impossible. */
2006 for (i = nnodes - 1; i >= 0; i--)
2008 node = order[i];
2010 /* Handle nodes to be flattened.
2011 Ideally when processing callees we stop inlining at the
2012 entry of cycles, possibly cloning that entry point and
2013 try to flatten itself turning it into a self-recursive
2014 function. */
2015 if (lookup_attribute ("flatten",
2016 DECL_ATTRIBUTES (node->decl)) != NULL)
2018 if (dump_file)
2019 fprintf (dump_file,
2020 "Flattening %s\n", node->name ());
2021 flatten_function (node, false);
2025 inline_small_functions ();
2027 /* Do first after-inlining removal. We want to remove all "stale" extern inline
2028 functions and virtual functions so we really know what is called once. */
2029 symtab_remove_unreachable_nodes (false, dump_file);
2030 free (order);
2032 /* Inline functions with a property that after inlining into all callers the
2033 code size will shrink because the out-of-line copy is eliminated.
2034 We do this regardless on the callee size as long as function growth limits
2035 are met. */
2036 if (dump_file)
2037 fprintf (dump_file,
2038 "\nDeciding on functions to be inlined into all callers and removing useless speculations:\n");
2040 /* Inlining one function called once has good chance of preventing
2041 inlining other function into the same callee. Ideally we should
2042 work in priority order, but probably inlining hot functions first
2043 is good cut without the extra pain of maintaining the queue.
2045 ??? this is not really fitting the bill perfectly: inlining function
2046 into callee often leads to better optimization of callee due to
2047 increased context for optimization.
2048 For example if main() function calls a function that outputs help
2049 and then function that does the main optmization, we should inline
2050 the second with priority even if both calls are cold by themselves.
2052 We probably want to implement new predicate replacing our use of
2053 maybe_hot_edge interpreted as maybe_hot_edge || callee is known
2054 to be hot. */
2055 for (cold = 0; cold <= 1; cold ++)
2057 FOR_EACH_DEFINED_FUNCTION (node)
2059 struct cgraph_edge *edge, *next;
2060 bool update=false;
2062 for (edge = node->callees; edge; edge = next)
2064 next = edge->next_callee;
2065 if (edge->speculative && !speculation_useful_p (edge, false))
2067 cgraph_resolve_speculation (edge, NULL);
2068 update = true;
2069 remove_functions = true;
2072 if (update)
2074 struct cgraph_node *where = node->global.inlined_to
2075 ? node->global.inlined_to : node;
2076 reset_node_growth_cache (where);
2077 reset_edge_caches (where);
2078 inline_update_overall_summary (where);
2080 if (flag_inline_functions_called_once
2081 && want_inline_function_to_all_callers_p (node, cold))
2083 int num_calls = 0;
2084 cgraph_for_node_and_aliases (node, sum_callers,
2085 &num_calls, true);
2086 cgraph_for_node_and_aliases (node, inline_to_all_callers,
2087 &num_calls, true);
2088 remove_functions = true;
2093 /* Free ipa-prop structures if they are no longer needed. */
2094 if (optimize)
2095 ipa_free_all_structures_after_iinln ();
2097 if (dump_file)
2098 fprintf (dump_file,
2099 "\nInlined %i calls, eliminated %i functions\n\n",
2100 ncalls_inlined, nfunctions_inlined);
2102 if (dump_file)
2103 dump_inline_summaries (dump_file);
2104 /* In WPA we use inline summaries for partitioning process. */
2105 if (!flag_wpa)
2106 inline_free_summary ();
2107 return remove_functions ? TODO_remove_functions : 0;
2110 /* Inline always-inline function calls in NODE. */
2112 static bool
2113 inline_always_inline_functions (struct cgraph_node *node)
2115 struct cgraph_edge *e;
2116 bool inlined = false;
2118 for (e = node->callees; e; e = e->next_callee)
2120 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
2121 if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl))
2122 continue;
2124 if (cgraph_edge_recursive_p (e))
2126 if (dump_file)
2127 fprintf (dump_file, " Not inlining recursive call to %s.\n",
2128 e->callee->name ());
2129 e->inline_failed = CIF_RECURSIVE_INLINING;
2130 continue;
2133 if (!can_early_inline_edge_p (e))
2135 /* Set inlined to true if the callee is marked "always_inline" but
2136 is not inlinable. This will allow flagging an error later in
2137 expand_call_inline in tree-inline.c. */
2138 if (lookup_attribute ("always_inline",
2139 DECL_ATTRIBUTES (callee->decl)) != NULL)
2140 inlined = true;
2141 continue;
2144 if (dump_file)
2145 fprintf (dump_file, " Inlining %s into %s (always_inline).\n",
2146 xstrdup (e->callee->name ()),
2147 xstrdup (e->caller->name ()));
2148 inline_call (e, true, NULL, NULL, false);
2149 inlined = true;
2151 if (inlined)
2152 inline_update_overall_summary (node);
2154 return inlined;
2157 /* Decide on the inlining. We do so in the topological order to avoid
2158 expenses on updating data structures. */
2160 static bool
2161 early_inline_small_functions (struct cgraph_node *node)
2163 struct cgraph_edge *e;
2164 bool inlined = false;
2166 for (e = node->callees; e; e = e->next_callee)
2168 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
2169 if (!inline_summary (callee)->inlinable
2170 || !e->inline_failed)
2171 continue;
2173 /* Do not consider functions not declared inline. */
2174 if (!DECL_DECLARED_INLINE_P (callee->decl)
2175 && !flag_inline_small_functions
2176 && !flag_inline_functions)
2177 continue;
2179 if (dump_file)
2180 fprintf (dump_file, "Considering inline candidate %s.\n",
2181 callee->name ());
2183 if (!can_early_inline_edge_p (e))
2184 continue;
2186 if (cgraph_edge_recursive_p (e))
2188 if (dump_file)
2189 fprintf (dump_file, " Not inlining: recursive call.\n");
2190 continue;
2193 if (!want_early_inline_function_p (e))
2194 continue;
2196 if (dump_file)
2197 fprintf (dump_file, " Inlining %s into %s.\n",
2198 xstrdup (callee->name ()),
2199 xstrdup (e->caller->name ()));
2200 inline_call (e, true, NULL, NULL, true);
2201 inlined = true;
2204 return inlined;
2207 /* Do inlining of small functions. Doing so early helps profiling and other
2208 passes to be somewhat more effective and avoids some code duplication in
2209 later real inlining pass for testcases with very many function calls. */
2210 static unsigned int
2211 early_inliner (void)
2213 struct cgraph_node *node = cgraph_get_node (current_function_decl);
2214 struct cgraph_edge *edge;
2215 unsigned int todo = 0;
2216 int iterations = 0;
2217 bool inlined = false;
2219 if (seen_error ())
2220 return 0;
2222 /* Do nothing if datastructures for ipa-inliner are already computed. This
2223 happens when some pass decides to construct new function and
2224 cgraph_add_new_function calls lowering passes and early optimization on
2225 it. This may confuse ourself when early inliner decide to inline call to
2226 function clone, because function clones don't have parameter list in
2227 ipa-prop matching their signature. */
2228 if (ipa_node_params_vector.exists ())
2229 return 0;
2231 #ifdef ENABLE_CHECKING
2232 verify_cgraph_node (node);
2233 #endif
2234 ipa_remove_all_references (&node->ref_list);
2236 /* Even when not optimizing or not inlining inline always-inline
2237 functions. */
2238 inlined = inline_always_inline_functions (node);
2240 if (!optimize
2241 || flag_no_inline
2242 || !flag_early_inlining
2243 /* Never inline regular functions into always-inline functions
2244 during incremental inlining. This sucks as functions calling
2245 always inline functions will get less optimized, but at the
2246 same time inlining of functions calling always inline
2247 function into an always inline function might introduce
2248 cycles of edges to be always inlined in the callgraph.
2250 We might want to be smarter and just avoid this type of inlining. */
2251 || DECL_DISREGARD_INLINE_LIMITS (node->decl))
2253 else if (lookup_attribute ("flatten",
2254 DECL_ATTRIBUTES (node->decl)) != NULL)
2256 /* When the function is marked to be flattened, recursively inline
2257 all calls in it. */
2258 if (dump_file)
2259 fprintf (dump_file,
2260 "Flattening %s\n", node->name ());
2261 flatten_function (node, true);
2262 inlined = true;
2264 else
2266 /* We iterate incremental inlining to get trivial cases of indirect
2267 inlining. */
2268 while (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS)
2269 && early_inline_small_functions (node))
2271 timevar_push (TV_INTEGRATION);
2272 todo |= optimize_inline_calls (current_function_decl);
2274 /* Technically we ought to recompute inline parameters so the new
2275 iteration of early inliner works as expected. We however have
2276 values approximately right and thus we only need to update edge
2277 info that might be cleared out for newly discovered edges. */
2278 for (edge = node->callees; edge; edge = edge->next_callee)
2280 struct inline_edge_summary *es = inline_edge_summary (edge);
2281 es->call_stmt_size
2282 = estimate_num_insns (edge->call_stmt, &eni_size_weights);
2283 es->call_stmt_time
2284 = estimate_num_insns (edge->call_stmt, &eni_time_weights);
2285 if (edge->callee->decl
2286 && !gimple_check_call_matching_types (
2287 edge->call_stmt, edge->callee->decl, false))
2288 edge->call_stmt_cannot_inline_p = true;
2290 timevar_pop (TV_INTEGRATION);
2291 iterations++;
2292 inlined = false;
2294 if (dump_file)
2295 fprintf (dump_file, "Iterations: %i\n", iterations);
2298 if (inlined)
2300 timevar_push (TV_INTEGRATION);
2301 todo |= optimize_inline_calls (current_function_decl);
2302 timevar_pop (TV_INTEGRATION);
2305 cfun->always_inline_functions_inlined = true;
2307 return todo;
2310 namespace {
2312 const pass_data pass_data_early_inline =
2314 GIMPLE_PASS, /* type */
2315 "einline", /* name */
2316 OPTGROUP_INLINE, /* optinfo_flags */
2317 false, /* has_gate */
2318 true, /* has_execute */
2319 TV_EARLY_INLINING, /* tv_id */
2320 PROP_ssa, /* properties_required */
2321 0, /* properties_provided */
2322 0, /* properties_destroyed */
2323 0, /* todo_flags_start */
2324 0, /* todo_flags_finish */
2327 class pass_early_inline : public gimple_opt_pass
2329 public:
2330 pass_early_inline (gcc::context *ctxt)
2331 : gimple_opt_pass (pass_data_early_inline, ctxt)
2334 /* opt_pass methods: */
2335 unsigned int execute () { return early_inliner (); }
2337 }; // class pass_early_inline
2339 } // anon namespace
2341 gimple_opt_pass *
2342 make_pass_early_inline (gcc::context *ctxt)
2344 return new pass_early_inline (ctxt);
2347 namespace {
2349 const pass_data pass_data_ipa_inline =
2351 IPA_PASS, /* type */
2352 "inline", /* name */
2353 OPTGROUP_INLINE, /* optinfo_flags */
2354 false, /* has_gate */
2355 true, /* has_execute */
2356 TV_IPA_INLINING, /* tv_id */
2357 0, /* properties_required */
2358 0, /* properties_provided */
2359 0, /* properties_destroyed */
2360 TODO_remove_functions, /* todo_flags_start */
2361 ( TODO_dump_symtab ), /* todo_flags_finish */
2364 class pass_ipa_inline : public ipa_opt_pass_d
2366 public:
2367 pass_ipa_inline (gcc::context *ctxt)
2368 : ipa_opt_pass_d (pass_data_ipa_inline, ctxt,
2369 inline_generate_summary, /* generate_summary */
2370 inline_write_summary, /* write_summary */
2371 inline_read_summary, /* read_summary */
2372 NULL, /* write_optimization_summary */
2373 NULL, /* read_optimization_summary */
2374 NULL, /* stmt_fixup */
2375 0, /* function_transform_todo_flags_start */
2376 inline_transform, /* function_transform */
2377 NULL) /* variable_transform */
2380 /* opt_pass methods: */
2381 unsigned int execute () { return ipa_inline (); }
2383 }; // class pass_ipa_inline
2385 } // anon namespace
2387 ipa_opt_pass_d *
2388 make_pass_ipa_inline (gcc::context *ctxt)
2390 return new pass_ipa_inline (ctxt);