Make all gimple_omp_for_ accessors typesafe
[official-gcc.git] / gcc / ipa-inline.c
blobbb8c99d228718132f8c12ba4c0ef4ebab27eea88
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 "profile.h"
112 #include "predict.h"
113 #include "vec.h"
114 #include "hashtab.h"
115 #include "hash-set.h"
116 #include "machmode.h"
117 #include "hard-reg-set.h"
118 #include "input.h"
119 #include "function.h"
120 #include "basic-block.h"
121 #include "tree-ssa-alias.h"
122 #include "internal-fn.h"
123 #include "gimple-expr.h"
124 #include "is-a.h"
125 #include "gimple.h"
126 #include "gimple-ssa.h"
127 #include "ipa-prop.h"
128 #include "except.h"
129 #include "target.h"
130 #include "ipa-inline.h"
131 #include "ipa-utils.h"
132 #include "sreal.h"
133 #include "auto-profile.h"
134 #include "cilk.h"
135 #include "builtins.h"
137 /* Statistics we collect about inlining algorithm. */
138 static int overall_size;
139 static gcov_type max_count;
140 static sreal max_count_real, max_relbenefit_real, half_int_min_real;
141 static gcov_type spec_rem;
143 /* Return false when inlining edge E would lead to violating
144 limits on function unit growth or stack usage growth.
146 The relative function body growth limit is present generally
147 to avoid problems with non-linear behavior of the compiler.
148 To allow inlining huge functions into tiny wrapper, the limit
149 is always based on the bigger of the two functions considered.
151 For stack growth limits we always base the growth in stack usage
152 of the callers. We want to prevent applications from segfaulting
153 on stack overflow when functions with huge stack frames gets
154 inlined. */
156 static bool
157 caller_growth_limits (struct cgraph_edge *e)
159 struct cgraph_node *to = e->caller;
160 struct cgraph_node *what = e->callee->ultimate_alias_target ();
161 int newsize;
162 int limit = 0;
163 HOST_WIDE_INT stack_size_limit = 0, inlined_stack;
164 struct inline_summary *info, *what_info, *outer_info = inline_summary (to);
166 /* Look for function e->caller is inlined to. While doing
167 so work out the largest function body on the way. As
168 described above, we want to base our function growth
169 limits based on that. Not on the self size of the
170 outer function, not on the self size of inline code
171 we immediately inline to. This is the most relaxed
172 interpretation of the rule "do not grow large functions
173 too much in order to prevent compiler from exploding". */
174 while (true)
176 info = inline_summary (to);
177 if (limit < info->self_size)
178 limit = info->self_size;
179 if (stack_size_limit < info->estimated_self_stack_size)
180 stack_size_limit = info->estimated_self_stack_size;
181 if (to->global.inlined_to)
182 to = to->callers->caller;
183 else
184 break;
187 what_info = inline_summary (what);
189 if (limit < what_info->self_size)
190 limit = what_info->self_size;
192 limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
194 /* Check the size after inlining against the function limits. But allow
195 the function to shrink if it went over the limits by forced inlining. */
196 newsize = estimate_size_after_inlining (to, e);
197 if (newsize >= info->size
198 && newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
199 && newsize > limit)
201 e->inline_failed = CIF_LARGE_FUNCTION_GROWTH_LIMIT;
202 return false;
205 if (!what_info->estimated_stack_size)
206 return true;
208 /* FIXME: Stack size limit often prevents inlining in Fortran programs
209 due to large i/o datastructures used by the Fortran front-end.
210 We ought to ignore this limit when we know that the edge is executed
211 on every invocation of the caller (i.e. its call statement dominates
212 exit block). We do not track this information, yet. */
213 stack_size_limit += ((gcov_type)stack_size_limit
214 * PARAM_VALUE (PARAM_STACK_FRAME_GROWTH) / 100);
216 inlined_stack = (outer_info->stack_frame_offset
217 + outer_info->estimated_self_stack_size
218 + what_info->estimated_stack_size);
219 /* Check new stack consumption with stack consumption at the place
220 stack is used. */
221 if (inlined_stack > stack_size_limit
222 /* If function already has large stack usage from sibling
223 inline call, we can inline, too.
224 This bit overoptimistically assume that we are good at stack
225 packing. */
226 && inlined_stack > info->estimated_stack_size
227 && inlined_stack > PARAM_VALUE (PARAM_LARGE_STACK_FRAME))
229 e->inline_failed = CIF_LARGE_STACK_FRAME_GROWTH_LIMIT;
230 return false;
232 return true;
235 /* Dump info about why inlining has failed. */
237 static void
238 report_inline_failed_reason (struct cgraph_edge *e)
240 if (dump_file)
242 fprintf (dump_file, " not inlinable: %s/%i -> %s/%i, %s\n",
243 xstrdup (e->caller->name ()), e->caller->order,
244 xstrdup (e->callee->name ()), e->callee->order,
245 cgraph_inline_failed_string (e->inline_failed));
249 /* Decide whether sanitizer-related attributes allow inlining. */
251 static bool
252 sanitize_attrs_match_for_inline_p (const_tree caller, const_tree callee)
254 /* Don't care if sanitizer is disabled */
255 if (!(flag_sanitize & SANITIZE_ADDRESS))
256 return true;
258 if (!caller || !callee)
259 return true;
261 return !!lookup_attribute ("no_sanitize_address",
262 DECL_ATTRIBUTES (caller)) ==
263 !!lookup_attribute ("no_sanitize_address",
264 DECL_ATTRIBUTES (callee));
267 /* Decide if we can inline the edge and possibly update
268 inline_failed reason.
269 We check whether inlining is possible at all and whether
270 caller growth limits allow doing so.
272 if REPORT is true, output reason to the dump file.
274 if DISREGARD_LIMITS is true, ignore size limits.*/
276 static bool
277 can_inline_edge_p (struct cgraph_edge *e, bool report,
278 bool disregard_limits = false)
280 bool inlinable = true;
281 enum availability avail;
282 cgraph_node *callee = e->callee->ultimate_alias_target (&avail);
283 tree caller_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (e->caller->decl);
284 tree callee_tree
285 = callee ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (callee->decl) : NULL;
286 struct function *caller_fun = e->caller->get_fun ();
287 struct function *callee_fun = callee ? callee->get_fun () : NULL;
289 gcc_assert (e->inline_failed);
291 if (!callee || !callee->definition)
293 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
294 inlinable = false;
296 else if (callee->calls_comdat_local)
298 e->inline_failed = CIF_USES_COMDAT_LOCAL;
299 inlinable = false;
301 else if (!inline_summary (callee)->inlinable
302 || (caller_fun && fn_contains_cilk_spawn_p (caller_fun)))
304 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
305 inlinable = false;
307 else if (avail <= AVAIL_INTERPOSABLE)
309 e->inline_failed = CIF_OVERWRITABLE;
310 inlinable = false;
312 else if (e->call_stmt_cannot_inline_p)
314 if (e->inline_failed != CIF_FUNCTION_NOT_OPTIMIZED)
315 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
316 inlinable = false;
318 /* Don't inline if the functions have different EH personalities. */
319 else if (DECL_FUNCTION_PERSONALITY (e->caller->decl)
320 && DECL_FUNCTION_PERSONALITY (callee->decl)
321 && (DECL_FUNCTION_PERSONALITY (e->caller->decl)
322 != DECL_FUNCTION_PERSONALITY (callee->decl)))
324 e->inline_failed = CIF_EH_PERSONALITY;
325 inlinable = false;
327 /* TM pure functions should not be inlined into non-TM_pure
328 functions. */
329 else if (is_tm_pure (callee->decl)
330 && !is_tm_pure (e->caller->decl))
332 e->inline_failed = CIF_UNSPECIFIED;
333 inlinable = false;
335 /* Don't inline if the callee can throw non-call exceptions but the
336 caller cannot.
337 FIXME: this is obviously wrong for LTO where STRUCT_FUNCTION is missing.
338 Move the flag into cgraph node or mirror it in the inline summary. */
339 else if (callee_fun && callee_fun->can_throw_non_call_exceptions
340 && !(caller_fun && caller_fun->can_throw_non_call_exceptions))
342 e->inline_failed = CIF_NON_CALL_EXCEPTIONS;
343 inlinable = false;
345 /* Check compatibility of target optimization options. */
346 else if (!targetm.target_option.can_inline_p (e->caller->decl,
347 callee->decl))
349 e->inline_failed = CIF_TARGET_OPTION_MISMATCH;
350 inlinable = false;
352 /* Don't inline a function with mismatched sanitization attributes. */
353 else if (!sanitize_attrs_match_for_inline_p (e->caller->decl, callee->decl))
355 e->inline_failed = CIF_ATTRIBUTE_MISMATCH;
356 inlinable = false;
358 /* Check if caller growth allows the inlining. */
359 else if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl)
360 && !disregard_limits
361 && !lookup_attribute ("flatten",
362 DECL_ATTRIBUTES
363 (e->caller->global.inlined_to
364 ? e->caller->global.inlined_to->decl
365 : e->caller->decl))
366 && !caller_growth_limits (e))
367 inlinable = false;
368 /* Don't inline a function with a higher optimization level than the
369 caller. FIXME: this is really just tip of iceberg of handling
370 optimization attribute. */
371 else if (caller_tree != callee_tree)
373 struct cl_optimization *caller_opt
374 = TREE_OPTIMIZATION ((caller_tree)
375 ? caller_tree
376 : optimization_default_node);
378 struct cl_optimization *callee_opt
379 = TREE_OPTIMIZATION ((callee_tree)
380 ? callee_tree
381 : optimization_default_node);
383 if (((caller_opt->x_optimize > callee_opt->x_optimize)
384 || (caller_opt->x_optimize_size != callee_opt->x_optimize_size))
385 /* gcc.dg/pr43564.c. Look at forced inline even in -O0. */
386 && !DECL_DISREGARD_INLINE_LIMITS (e->callee->decl))
388 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
389 inlinable = false;
393 if (!inlinable && report)
394 report_inline_failed_reason (e);
395 return inlinable;
399 /* Return true if the edge E is inlinable during early inlining. */
401 static bool
402 can_early_inline_edge_p (struct cgraph_edge *e)
404 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
405 /* Early inliner might get called at WPA stage when IPA pass adds new
406 function. In this case we can not really do any of early inlining
407 because function bodies are missing. */
408 if (!gimple_has_body_p (callee->decl))
410 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
411 return false;
413 /* In early inliner some of callees may not be in SSA form yet
414 (i.e. the callgraph is cyclic and we did not process
415 the callee by early inliner, yet). We don't have CIF code for this
416 case; later we will re-do the decision in the real inliner. */
417 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->caller->decl))
418 || !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
420 if (dump_file)
421 fprintf (dump_file, " edge not inlinable: not in SSA form\n");
422 return false;
424 if (!can_inline_edge_p (e, true))
425 return false;
426 return true;
430 /* Return number of calls in N. Ignore cheap builtins. */
432 static int
433 num_calls (struct cgraph_node *n)
435 struct cgraph_edge *e;
436 int num = 0;
438 for (e = n->callees; e; e = e->next_callee)
439 if (!is_inexpensive_builtin (e->callee->decl))
440 num++;
441 return num;
445 /* Return true if we are interested in inlining small function. */
447 static bool
448 want_early_inline_function_p (struct cgraph_edge *e)
450 bool want_inline = true;
451 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
453 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
455 /* For AutoFDO, we need to make sure that before profile annotation, all
456 hot paths' IR look exactly the same as profiled binary. As a result,
457 in einliner, we will disregard size limit and inline those callsites
458 that are:
459 * inlined in the profiled binary, and
460 * the cloned callee has enough samples to be considered "hot". */
461 else if (flag_auto_profile && afdo_callsite_hot_enough_for_early_inline (e))
463 else if (!DECL_DECLARED_INLINE_P (callee->decl)
464 && !flag_inline_small_functions)
466 e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
467 report_inline_failed_reason (e);
468 want_inline = false;
470 else
472 int growth = estimate_edge_growth (e);
473 int n;
475 if (growth <= 0)
477 else if (!e->maybe_hot_p ()
478 && growth > 0)
480 if (dump_file)
481 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
482 "call is cold and code would grow by %i\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 (growth > PARAM_VALUE (PARAM_EARLY_INLINING_INSNS))
491 if (dump_file)
492 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
493 "growth %i exceeds --param early-inlining-insns\n",
494 xstrdup (e->caller->name ()),
495 e->caller->order,
496 xstrdup (callee->name ()), callee->order,
497 growth);
498 want_inline = false;
500 else if ((n = num_calls (callee)) != 0
501 && growth * (n + 1) > PARAM_VALUE (PARAM_EARLY_INLINING_INSNS))
503 if (dump_file)
504 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
505 "growth %i exceeds --param early-inlining-insns "
506 "divided by number of calls\n",
507 xstrdup (e->caller->name ()),
508 e->caller->order,
509 xstrdup (callee->name ()), callee->order,
510 growth);
511 want_inline = false;
514 return want_inline;
517 /* Compute time of the edge->caller + edge->callee execution when inlining
518 does not happen. */
520 inline gcov_type
521 compute_uninlined_call_time (struct inline_summary *callee_info,
522 struct cgraph_edge *edge)
524 gcov_type uninlined_call_time =
525 RDIV ((gcov_type)callee_info->time * MAX (edge->frequency, 1),
526 CGRAPH_FREQ_BASE);
527 gcov_type caller_time = inline_summary (edge->caller->global.inlined_to
528 ? edge->caller->global.inlined_to
529 : edge->caller)->time;
530 return uninlined_call_time + caller_time;
533 /* Same as compute_uinlined_call_time but compute time when inlining
534 does happen. */
536 inline gcov_type
537 compute_inlined_call_time (struct cgraph_edge *edge,
538 int edge_time)
540 gcov_type caller_time = inline_summary (edge->caller->global.inlined_to
541 ? edge->caller->global.inlined_to
542 : edge->caller)->time;
543 gcov_type time = (caller_time
544 + RDIV (((gcov_type) edge_time
545 - inline_edge_summary (edge)->call_stmt_time)
546 * MAX (edge->frequency, 1), CGRAPH_FREQ_BASE));
547 /* Possible one roundoff error, but watch for overflows. */
548 gcc_checking_assert (time >= INT_MIN / 2);
549 if (time < 0)
550 time = 0;
551 return time;
554 /* Return true if the speedup for inlining E is bigger than
555 PARAM_MAX_INLINE_MIN_SPEEDUP. */
557 static bool
558 big_speedup_p (struct cgraph_edge *e)
560 gcov_type time = compute_uninlined_call_time (inline_summary (e->callee),
562 gcov_type inlined_time = compute_inlined_call_time (e,
563 estimate_edge_time (e));
564 if (time - inlined_time
565 > RDIV (time * PARAM_VALUE (PARAM_INLINE_MIN_SPEEDUP), 100))
566 return true;
567 return false;
570 /* Return true if we are interested in inlining small function.
571 When REPORT is true, report reason to dump file. */
573 static bool
574 want_inline_small_function_p (struct cgraph_edge *e, bool report)
576 bool want_inline = true;
577 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
579 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
581 else if (!DECL_DECLARED_INLINE_P (callee->decl)
582 && !flag_inline_small_functions)
584 e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
585 want_inline = false;
587 /* Do fast and conservative check if the function can be good
588 inline cnadidate. At themoment we allow inline hints to
589 promote non-inline function to inline and we increase
590 MAX_INLINE_INSNS_SINGLE 16fold for inline functions. */
591 else if ((!DECL_DECLARED_INLINE_P (callee->decl)
592 && (!e->count || !e->maybe_hot_p ()))
593 && inline_summary (callee)->min_size - inline_edge_summary (e)->call_stmt_size
594 > MAX (MAX_INLINE_INSNS_SINGLE, MAX_INLINE_INSNS_AUTO))
596 e->inline_failed = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
597 want_inline = false;
599 else if ((DECL_DECLARED_INLINE_P (callee->decl) || e->count)
600 && inline_summary (callee)->min_size - inline_edge_summary (e)->call_stmt_size
601 > 16 * MAX_INLINE_INSNS_SINGLE)
603 e->inline_failed = (DECL_DECLARED_INLINE_P (callee->decl)
604 ? CIF_MAX_INLINE_INSNS_SINGLE_LIMIT
605 : CIF_MAX_INLINE_INSNS_AUTO_LIMIT);
606 want_inline = false;
608 else
610 int growth = estimate_edge_growth (e);
611 inline_hints hints = estimate_edge_hints (e);
612 bool big_speedup = big_speedup_p (e);
614 if (growth <= 0)
616 /* Apply MAX_INLINE_INSNS_SINGLE limit. Do not do so when
617 hints suggests that inlining given function is very profitable. */
618 else if (DECL_DECLARED_INLINE_P (callee->decl)
619 && growth >= MAX_INLINE_INSNS_SINGLE
620 && ((!big_speedup
621 && !(hints & (INLINE_HINT_indirect_call
622 | INLINE_HINT_known_hot
623 | INLINE_HINT_loop_iterations
624 | INLINE_HINT_array_index
625 | INLINE_HINT_loop_stride)))
626 || growth >= MAX_INLINE_INSNS_SINGLE * 16))
628 e->inline_failed = CIF_MAX_INLINE_INSNS_SINGLE_LIMIT;
629 want_inline = false;
631 else if (!DECL_DECLARED_INLINE_P (callee->decl)
632 && !flag_inline_functions)
634 /* growth_likely_positive is expensive, always test it last. */
635 if (growth >= MAX_INLINE_INSNS_SINGLE
636 || growth_likely_positive (callee, growth))
638 e->inline_failed = CIF_NOT_DECLARED_INLINED;
639 want_inline = false;
642 /* Apply MAX_INLINE_INSNS_AUTO limit for functions not declared inline
643 Upgrade it to MAX_INLINE_INSNS_SINGLE when hints suggests that
644 inlining given function is very profitable. */
645 else if (!DECL_DECLARED_INLINE_P (callee->decl)
646 && !big_speedup
647 && !(hints & INLINE_HINT_known_hot)
648 && growth >= ((hints & (INLINE_HINT_indirect_call
649 | INLINE_HINT_loop_iterations
650 | INLINE_HINT_array_index
651 | INLINE_HINT_loop_stride))
652 ? MAX (MAX_INLINE_INSNS_AUTO,
653 MAX_INLINE_INSNS_SINGLE)
654 : MAX_INLINE_INSNS_AUTO))
656 /* growth_likely_positive is expensive, always test it last. */
657 if (growth >= MAX_INLINE_INSNS_SINGLE
658 || growth_likely_positive (callee, growth))
660 e->inline_failed = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
661 want_inline = false;
664 /* If call is cold, do not inline when function body would grow. */
665 else if (!e->maybe_hot_p ()
666 && (growth >= MAX_INLINE_INSNS_SINGLE
667 || growth_likely_positive (callee, growth)))
669 e->inline_failed = CIF_UNLIKELY_CALL;
670 want_inline = false;
673 if (!want_inline && report)
674 report_inline_failed_reason (e);
675 return want_inline;
678 /* EDGE is self recursive edge.
679 We hand two cases - when function A is inlining into itself
680 or when function A is being inlined into another inliner copy of function
681 A within function B.
683 In first case OUTER_NODE points to the toplevel copy of A, while
684 in the second case OUTER_NODE points to the outermost copy of A in B.
686 In both cases we want to be extra selective since
687 inlining the call will just introduce new recursive calls to appear. */
689 static bool
690 want_inline_self_recursive_call_p (struct cgraph_edge *edge,
691 struct cgraph_node *outer_node,
692 bool peeling,
693 int depth)
695 char const *reason = NULL;
696 bool want_inline = true;
697 int caller_freq = CGRAPH_FREQ_BASE;
698 int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO);
700 if (DECL_DECLARED_INLINE_P (edge->caller->decl))
701 max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH);
703 if (!edge->maybe_hot_p ())
705 reason = "recursive call is cold";
706 want_inline = false;
708 else if (max_count && !outer_node->count)
710 reason = "not executed in profile";
711 want_inline = false;
713 else if (depth > max_depth)
715 reason = "--param max-inline-recursive-depth exceeded.";
716 want_inline = false;
719 if (outer_node->global.inlined_to)
720 caller_freq = outer_node->callers->frequency;
722 if (!caller_freq)
724 reason = "function is inlined and unlikely";
725 want_inline = false;
728 if (!want_inline)
730 /* Inlining of self recursive function into copy of itself within other function
731 is transformation similar to loop peeling.
733 Peeling is profitable if we can inline enough copies to make probability
734 of actual call to the self recursive function very small. Be sure that
735 the probability of recursion is small.
737 We ensure that the frequency of recursing is at most 1 - (1/max_depth).
738 This way the expected number of recision is at most max_depth. */
739 else if (peeling)
741 int max_prob = CGRAPH_FREQ_BASE - ((CGRAPH_FREQ_BASE + max_depth - 1)
742 / max_depth);
743 int i;
744 for (i = 1; i < depth; i++)
745 max_prob = max_prob * max_prob / CGRAPH_FREQ_BASE;
746 if (max_count
747 && (edge->count * CGRAPH_FREQ_BASE / outer_node->count
748 >= max_prob))
750 reason = "profile of recursive call is too large";
751 want_inline = false;
753 if (!max_count
754 && (edge->frequency * CGRAPH_FREQ_BASE / caller_freq
755 >= max_prob))
757 reason = "frequency of recursive call is too large";
758 want_inline = false;
761 /* Recursive inlining, i.e. equivalent of unrolling, is profitable if recursion
762 depth is large. We reduce function call overhead and increase chances that
763 things fit in hardware return predictor.
765 Recursive inlining might however increase cost of stack frame setup
766 actually slowing down functions whose recursion tree is wide rather than
767 deep.
769 Deciding reliably on when to do recursive inlining without profile feedback
770 is tricky. For now we disable recursive inlining when probability of self
771 recursion is low.
773 Recursive inlining of self recursive call within loop also results in large loop
774 depths that generally optimize badly. We may want to throttle down inlining
775 in those cases. In particular this seems to happen in one of libstdc++ rb tree
776 methods. */
777 else
779 if (max_count
780 && (edge->count * 100 / outer_node->count
781 <= PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY)))
783 reason = "profile of recursive call is too small";
784 want_inline = false;
786 else if (!max_count
787 && (edge->frequency * 100 / caller_freq
788 <= PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY)))
790 reason = "frequency of recursive call is too small";
791 want_inline = false;
794 if (!want_inline && dump_file)
795 fprintf (dump_file, " not inlining recursively: %s\n", reason);
796 return want_inline;
799 /* Return true when NODE has uninlinable caller;
800 set HAS_HOT_CALL if it has hot call.
801 Worker for cgraph_for_node_and_aliases. */
803 static bool
804 check_callers (struct cgraph_node *node, void *has_hot_call)
806 struct cgraph_edge *e;
807 for (e = node->callers; e; e = e->next_caller)
809 if (!can_inline_edge_p (e, true))
810 return true;
811 if (!(*(bool *)has_hot_call) && e->maybe_hot_p ())
812 *(bool *)has_hot_call = true;
814 return false;
817 /* If NODE has a caller, return true. */
819 static bool
820 has_caller_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
822 if (node->callers)
823 return true;
824 return false;
827 /* Decide if inlining NODE would reduce unit size by eliminating
828 the offline copy of function.
829 When COLD is true the cold calls are considered, too. */
831 static bool
832 want_inline_function_to_all_callers_p (struct cgraph_node *node, bool cold)
834 struct cgraph_node *function = node->ultimate_alias_target ();
835 bool has_hot_call = false;
837 /* Does it have callers? */
838 if (!node->call_for_symbol_thunks_and_aliases (has_caller_p, NULL, true))
839 return false;
840 /* Already inlined? */
841 if (function->global.inlined_to)
842 return false;
843 if (node->ultimate_alias_target () != node)
844 return false;
845 /* Inlining into all callers would increase size? */
846 if (estimate_growth (node) > 0)
847 return false;
848 /* All inlines must be possible. */
849 if (node->call_for_symbol_thunks_and_aliases
850 (check_callers, &has_hot_call, true))
851 return false;
852 if (!cold && !has_hot_call)
853 return false;
854 return true;
857 #define RELATIVE_TIME_BENEFIT_RANGE (INT_MAX / 64)
859 /* Return relative time improvement for inlining EDGE in range
860 1...RELATIVE_TIME_BENEFIT_RANGE */
862 static inline int
863 relative_time_benefit (struct inline_summary *callee_info,
864 struct cgraph_edge *edge,
865 int edge_time)
867 gcov_type relbenefit;
868 gcov_type uninlined_call_time = compute_uninlined_call_time (callee_info, edge);
869 gcov_type inlined_call_time = compute_inlined_call_time (edge, edge_time);
871 /* Inlining into extern inline function is not a win. */
872 if (DECL_EXTERNAL (edge->caller->global.inlined_to
873 ? edge->caller->global.inlined_to->decl
874 : edge->caller->decl))
875 return 1;
877 /* Watch overflows. */
878 gcc_checking_assert (uninlined_call_time >= 0);
879 gcc_checking_assert (inlined_call_time >= 0);
880 gcc_checking_assert (uninlined_call_time >= inlined_call_time);
882 /* Compute relative time benefit, i.e. how much the call becomes faster.
883 ??? perhaps computing how much the caller+calle together become faster
884 would lead to more realistic results. */
885 if (!uninlined_call_time)
886 uninlined_call_time = 1;
887 relbenefit =
888 RDIV (((gcov_type)uninlined_call_time - inlined_call_time) * RELATIVE_TIME_BENEFIT_RANGE,
889 uninlined_call_time);
890 relbenefit = MIN (relbenefit, RELATIVE_TIME_BENEFIT_RANGE);
891 gcc_checking_assert (relbenefit >= 0);
892 relbenefit = MAX (relbenefit, 1);
893 return relbenefit;
897 /* A cost model driving the inlining heuristics in a way so the edges with
898 smallest badness are inlined first. After each inlining is performed
899 the costs of all caller edges of nodes affected are recomputed so the
900 metrics may accurately depend on values such as number of inlinable callers
901 of the function or function body size. */
903 static int
904 edge_badness (struct cgraph_edge *edge, bool dump)
906 gcov_type badness;
907 int growth, edge_time;
908 struct cgraph_node *callee = edge->callee->ultimate_alias_target ();
909 struct inline_summary *callee_info = inline_summary (callee);
910 inline_hints hints;
912 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
913 return INT_MIN;
915 growth = estimate_edge_growth (edge);
916 edge_time = estimate_edge_time (edge);
917 hints = estimate_edge_hints (edge);
918 gcc_checking_assert (edge_time >= 0);
919 gcc_checking_assert (edge_time <= callee_info->time);
920 gcc_checking_assert (growth <= callee_info->size);
922 if (dump)
924 fprintf (dump_file, " Badness calculation for %s/%i -> %s/%i\n",
925 xstrdup (edge->caller->name ()),
926 edge->caller->order,
927 xstrdup (callee->name ()),
928 edge->callee->order);
929 fprintf (dump_file, " size growth %i, time %i ",
930 growth,
931 edge_time);
932 dump_inline_hints (dump_file, hints);
933 if (big_speedup_p (edge))
934 fprintf (dump_file, " big_speedup");
935 fprintf (dump_file, "\n");
938 /* Always prefer inlining saving code size. */
939 if (growth <= 0)
941 badness = INT_MIN / 2 + growth;
942 if (dump)
943 fprintf (dump_file, " %i: Growth %i <= 0\n", (int) badness,
944 growth);
947 /* When profiling is available, compute badness as:
949 relative_edge_count * relative_time_benefit
950 goodness = -------------------------------------------
951 growth_f_caller
952 badness = -goodness
954 The fraction is upside down, because on edge counts and time beneits
955 the bounds are known. Edge growth is essentially unlimited. */
957 else if (max_count)
959 sreal tmp, relbenefit_real, growth_real;
960 int relbenefit = relative_time_benefit (callee_info, edge, edge_time);
961 /* Capping edge->count to max_count. edge->count can be larger than
962 max_count if an inline adds new edges which increase max_count
963 after max_count is computed. */
964 gcov_type edge_count = edge->count > max_count ? max_count : edge->count;
966 sreal_init (&relbenefit_real, relbenefit, 0);
967 sreal_init (&growth_real, growth, 0);
969 /* relative_edge_count. */
970 sreal_init (&tmp, edge_count, 0);
971 sreal_div (&tmp, &tmp, &max_count_real);
973 /* relative_time_benefit. */
974 sreal_mul (&tmp, &tmp, &relbenefit_real);
975 sreal_div (&tmp, &tmp, &max_relbenefit_real);
977 /* growth_f_caller. */
978 sreal_mul (&tmp, &tmp, &half_int_min_real);
979 sreal_div (&tmp, &tmp, &growth_real);
981 badness = -1 * sreal_to_int (&tmp);
983 if (dump)
985 fprintf (dump_file,
986 " %i (relative %f): profile info. Relative count %f%s"
987 " * Relative benefit %f\n",
988 (int) badness, (double) badness / INT_MIN,
989 (double) edge_count / max_count,
990 edge->count > max_count ? " (capped to max_count)" : "",
991 relbenefit * 100.0 / RELATIVE_TIME_BENEFIT_RANGE);
995 /* When function local profile is available. Compute badness as:
997 relative_time_benefit
998 goodness = ---------------------------------
999 growth_of_caller * overall_growth
1001 badness = - goodness
1003 compensated by the inline hints.
1005 else if (flag_guess_branch_prob)
1007 badness = (relative_time_benefit (callee_info, edge, edge_time)
1008 * (INT_MIN / 16 / RELATIVE_TIME_BENEFIT_RANGE));
1009 badness /= (MIN (65536/2, growth) * MIN (65536/2, MAX (1, callee_info->growth)));
1010 gcc_checking_assert (badness <=0 && badness >= INT_MIN / 16);
1011 if ((hints & (INLINE_HINT_indirect_call
1012 | INLINE_HINT_loop_iterations
1013 | INLINE_HINT_array_index
1014 | INLINE_HINT_loop_stride))
1015 || callee_info->growth <= 0)
1016 badness *= 8;
1017 if (hints & (INLINE_HINT_same_scc))
1018 badness /= 16;
1019 else if (hints & (INLINE_HINT_in_scc))
1020 badness /= 8;
1021 else if (hints & (INLINE_HINT_cross_module))
1022 badness /= 2;
1023 gcc_checking_assert (badness <= 0 && badness >= INT_MIN / 2);
1024 if ((hints & INLINE_HINT_declared_inline) && badness >= INT_MIN / 32)
1025 badness *= 16;
1026 if (dump)
1028 fprintf (dump_file,
1029 " %i: guessed profile. frequency %f,"
1030 " benefit %f%%, time w/o inlining %i, time w inlining %i"
1031 " overall growth %i (current) %i (original)\n",
1032 (int) badness, (double)edge->frequency / CGRAPH_FREQ_BASE,
1033 relative_time_benefit (callee_info, edge, edge_time) * 100.0
1034 / RELATIVE_TIME_BENEFIT_RANGE,
1035 (int)compute_uninlined_call_time (callee_info, edge),
1036 (int)compute_inlined_call_time (edge, edge_time),
1037 estimate_growth (callee),
1038 callee_info->growth);
1041 /* When function local profile is not available or it does not give
1042 useful information (ie frequency is zero), base the cost on
1043 loop nest and overall size growth, so we optimize for overall number
1044 of functions fully inlined in program. */
1045 else
1047 int nest = MIN (inline_edge_summary (edge)->loop_depth, 8);
1048 badness = growth * 256;
1050 /* Decrease badness if call is nested. */
1051 if (badness > 0)
1052 badness >>= nest;
1053 else
1055 badness <<= nest;
1057 if (dump)
1058 fprintf (dump_file, " %i: no profile. nest %i\n", (int) badness,
1059 nest);
1062 /* Ensure that we did not overflow in all the fixed point math above. */
1063 gcc_assert (badness >= INT_MIN);
1064 gcc_assert (badness <= INT_MAX - 1);
1065 /* Make recursive inlining happen always after other inlining is done. */
1066 if (edge->recursive_p ())
1067 return badness + 1;
1068 else
1069 return badness;
1072 /* Recompute badness of EDGE and update its key in HEAP if needed. */
1073 static inline void
1074 update_edge_key (fibheap_t heap, struct cgraph_edge *edge)
1076 int badness = edge_badness (edge, false);
1077 if (edge->aux)
1079 fibnode_t n = (fibnode_t) edge->aux;
1080 gcc_checking_assert (n->data == edge);
1082 /* fibheap_replace_key only decrease the keys.
1083 When we increase the key we do not update heap
1084 and instead re-insert the element once it becomes
1085 a minimum of heap. */
1086 if (badness < n->key)
1088 if (dump_file && (dump_flags & TDF_DETAILS))
1090 fprintf (dump_file,
1091 " decreasing badness %s/%i -> %s/%i, %i to %i\n",
1092 xstrdup (edge->caller->name ()),
1093 edge->caller->order,
1094 xstrdup (edge->callee->name ()),
1095 edge->callee->order,
1096 (int)n->key,
1097 badness);
1099 fibheap_replace_key (heap, n, badness);
1100 gcc_checking_assert (n->key == badness);
1103 else
1105 if (dump_file && (dump_flags & TDF_DETAILS))
1107 fprintf (dump_file,
1108 " enqueuing call %s/%i -> %s/%i, badness %i\n",
1109 xstrdup (edge->caller->name ()),
1110 edge->caller->order,
1111 xstrdup (edge->callee->name ()),
1112 edge->callee->order,
1113 badness);
1115 edge->aux = fibheap_insert (heap, badness, edge);
1120 /* NODE was inlined.
1121 All caller edges needs to be resetted because
1122 size estimates change. Similarly callees needs reset
1123 because better context may be known. */
1125 static void
1126 reset_edge_caches (struct cgraph_node *node)
1128 struct cgraph_edge *edge;
1129 struct cgraph_edge *e = node->callees;
1130 struct cgraph_node *where = node;
1131 struct ipa_ref *ref;
1133 if (where->global.inlined_to)
1134 where = where->global.inlined_to;
1136 /* WHERE body size has changed, the cached growth is invalid. */
1137 reset_node_growth_cache (where);
1139 for (edge = where->callers; edge; edge = edge->next_caller)
1140 if (edge->inline_failed)
1141 reset_edge_growth_cache (edge);
1143 FOR_EACH_ALIAS (where, ref)
1144 reset_edge_caches (dyn_cast <cgraph_node *> (ref->referring));
1146 if (!e)
1147 return;
1149 while (true)
1150 if (!e->inline_failed && e->callee->callees)
1151 e = e->callee->callees;
1152 else
1154 if (e->inline_failed)
1155 reset_edge_growth_cache (e);
1156 if (e->next_callee)
1157 e = e->next_callee;
1158 else
1162 if (e->caller == node)
1163 return;
1164 e = e->caller->callers;
1166 while (!e->next_callee);
1167 e = e->next_callee;
1172 /* Recompute HEAP nodes for each of caller of NODE.
1173 UPDATED_NODES track nodes we already visited, to avoid redundant work.
1174 When CHECK_INLINABLITY_FOR is set, re-check for specified edge that
1175 it is inlinable. Otherwise check all edges. */
1177 static void
1178 update_caller_keys (fibheap_t heap, struct cgraph_node *node,
1179 bitmap updated_nodes,
1180 struct cgraph_edge *check_inlinablity_for)
1182 struct cgraph_edge *edge;
1183 struct ipa_ref *ref;
1185 if ((!node->alias && !inline_summary (node)->inlinable)
1186 || node->global.inlined_to)
1187 return;
1188 if (!bitmap_set_bit (updated_nodes, node->uid))
1189 return;
1191 FOR_EACH_ALIAS (node, ref)
1193 struct cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1194 update_caller_keys (heap, alias, updated_nodes, check_inlinablity_for);
1197 for (edge = node->callers; edge; edge = edge->next_caller)
1198 if (edge->inline_failed)
1200 if (!check_inlinablity_for
1201 || check_inlinablity_for == edge)
1203 if (can_inline_edge_p (edge, false)
1204 && want_inline_small_function_p (edge, false))
1205 update_edge_key (heap, edge);
1206 else if (edge->aux)
1208 report_inline_failed_reason (edge);
1209 fibheap_delete_node (heap, (fibnode_t) edge->aux);
1210 edge->aux = NULL;
1213 else if (edge->aux)
1214 update_edge_key (heap, edge);
1218 /* Recompute HEAP nodes for each uninlined call in NODE.
1219 This is used when we know that edge badnesses are going only to increase
1220 (we introduced new call site) and thus all we need is to insert newly
1221 created edges into heap. */
1223 static void
1224 update_callee_keys (fibheap_t heap, struct cgraph_node *node,
1225 bitmap updated_nodes)
1227 struct cgraph_edge *e = node->callees;
1229 if (!e)
1230 return;
1231 while (true)
1232 if (!e->inline_failed && e->callee->callees)
1233 e = e->callee->callees;
1234 else
1236 enum availability avail;
1237 struct cgraph_node *callee;
1238 /* We do not reset callee growth cache here. Since we added a new call,
1239 growth chould have just increased and consequentely badness metric
1240 don't need updating. */
1241 if (e->inline_failed
1242 && (callee = e->callee->ultimate_alias_target (&avail))
1243 && inline_summary (callee)->inlinable
1244 && avail >= AVAIL_AVAILABLE
1245 && !bitmap_bit_p (updated_nodes, callee->uid))
1247 if (can_inline_edge_p (e, false)
1248 && want_inline_small_function_p (e, false))
1249 update_edge_key (heap, e);
1250 else if (e->aux)
1252 report_inline_failed_reason (e);
1253 fibheap_delete_node (heap, (fibnode_t) e->aux);
1254 e->aux = NULL;
1257 if (e->next_callee)
1258 e = e->next_callee;
1259 else
1263 if (e->caller == node)
1264 return;
1265 e = e->caller->callers;
1267 while (!e->next_callee);
1268 e = e->next_callee;
1273 /* Enqueue all recursive calls from NODE into priority queue depending on
1274 how likely we want to recursively inline the call. */
1276 static void
1277 lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
1278 fibheap_t heap)
1280 struct cgraph_edge *e;
1281 enum availability avail;
1283 for (e = where->callees; e; e = e->next_callee)
1284 if (e->callee == node
1285 || (e->callee->ultimate_alias_target (&avail) == node
1286 && avail > AVAIL_INTERPOSABLE))
1288 /* When profile feedback is available, prioritize by expected number
1289 of calls. */
1290 fibheap_insert (heap,
1291 !max_count ? -e->frequency
1292 : -(e->count / ((max_count + (1<<24) - 1) / (1<<24))),
1295 for (e = where->callees; e; e = e->next_callee)
1296 if (!e->inline_failed)
1297 lookup_recursive_calls (node, e->callee, heap);
1300 /* Decide on recursive inlining: in the case function has recursive calls,
1301 inline until body size reaches given argument. If any new indirect edges
1302 are discovered in the process, add them to *NEW_EDGES, unless NEW_EDGES
1303 is NULL. */
1305 static bool
1306 recursive_inlining (struct cgraph_edge *edge,
1307 vec<cgraph_edge *> *new_edges)
1309 int limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO);
1310 fibheap_t heap;
1311 struct cgraph_node *node;
1312 struct cgraph_edge *e;
1313 struct cgraph_node *master_clone = NULL, *next;
1314 int depth = 0;
1315 int n = 0;
1317 node = edge->caller;
1318 if (node->global.inlined_to)
1319 node = node->global.inlined_to;
1321 if (DECL_DECLARED_INLINE_P (node->decl))
1322 limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE);
1324 /* Make sure that function is small enough to be considered for inlining. */
1325 if (estimate_size_after_inlining (node, edge) >= limit)
1326 return false;
1327 heap = fibheap_new ();
1328 lookup_recursive_calls (node, node, heap);
1329 if (fibheap_empty (heap))
1331 fibheap_delete (heap);
1332 return false;
1335 if (dump_file)
1336 fprintf (dump_file,
1337 " Performing recursive inlining on %s\n",
1338 node->name ());
1340 /* Do the inlining and update list of recursive call during process. */
1341 while (!fibheap_empty (heap))
1343 struct cgraph_edge *curr
1344 = (struct cgraph_edge *) fibheap_extract_min (heap);
1345 struct cgraph_node *cnode, *dest = curr->callee;
1347 if (!can_inline_edge_p (curr, true))
1348 continue;
1350 /* MASTER_CLONE is produced in the case we already started modified
1351 the function. Be sure to redirect edge to the original body before
1352 estimating growths otherwise we will be seeing growths after inlining
1353 the already modified body. */
1354 if (master_clone)
1356 curr->redirect_callee (master_clone);
1357 reset_edge_growth_cache (curr);
1360 if (estimate_size_after_inlining (node, curr) > limit)
1362 curr->redirect_callee (dest);
1363 reset_edge_growth_cache (curr);
1364 break;
1367 depth = 1;
1368 for (cnode = curr->caller;
1369 cnode->global.inlined_to; cnode = cnode->callers->caller)
1370 if (node->decl
1371 == curr->callee->ultimate_alias_target ()->decl)
1372 depth++;
1374 if (!want_inline_self_recursive_call_p (curr, node, false, depth))
1376 curr->redirect_callee (dest);
1377 reset_edge_growth_cache (curr);
1378 continue;
1381 if (dump_file)
1383 fprintf (dump_file,
1384 " Inlining call of depth %i", depth);
1385 if (node->count)
1387 fprintf (dump_file, " called approx. %.2f times per call",
1388 (double)curr->count / node->count);
1390 fprintf (dump_file, "\n");
1392 if (!master_clone)
1394 /* We need original clone to copy around. */
1395 master_clone = node->create_clone (node->decl, node->count,
1396 CGRAPH_FREQ_BASE, false, vNULL,
1397 true, NULL, NULL);
1398 for (e = master_clone->callees; e; e = e->next_callee)
1399 if (!e->inline_failed)
1400 clone_inlined_nodes (e, true, false, NULL, CGRAPH_FREQ_BASE);
1401 curr->redirect_callee (master_clone);
1402 reset_edge_growth_cache (curr);
1405 inline_call (curr, false, new_edges, &overall_size, true);
1406 lookup_recursive_calls (node, curr->callee, heap);
1407 n++;
1410 if (!fibheap_empty (heap) && dump_file)
1411 fprintf (dump_file, " Recursive inlining growth limit met.\n");
1412 fibheap_delete (heap);
1414 if (!master_clone)
1415 return false;
1417 if (dump_file)
1418 fprintf (dump_file,
1419 "\n Inlined %i times, "
1420 "body grown from size %i to %i, time %i to %i\n", n,
1421 inline_summary (master_clone)->size, inline_summary (node)->size,
1422 inline_summary (master_clone)->time, inline_summary (node)->time);
1424 /* Remove master clone we used for inlining. We rely that clones inlined
1425 into master clone gets queued just before master clone so we don't
1426 need recursion. */
1427 for (node = symtab->first_function (); node != master_clone;
1428 node = next)
1430 next = symtab->next_function (node);
1431 if (node->global.inlined_to == master_clone)
1432 node->remove ();
1434 master_clone->remove ();
1435 return true;
1439 /* Given whole compilation unit estimate of INSNS, compute how large we can
1440 allow the unit to grow. */
1442 static int
1443 compute_max_insns (int insns)
1445 int max_insns = insns;
1446 if (max_insns < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
1447 max_insns = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
1449 return ((int64_t) max_insns
1450 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
1454 /* Compute badness of all edges in NEW_EDGES and add them to the HEAP. */
1456 static void
1457 add_new_edges_to_heap (fibheap_t heap, vec<cgraph_edge *> new_edges)
1459 while (new_edges.length () > 0)
1461 struct cgraph_edge *edge = new_edges.pop ();
1463 gcc_assert (!edge->aux);
1464 if (edge->inline_failed
1465 && can_inline_edge_p (edge, true)
1466 && want_inline_small_function_p (edge, true))
1467 edge->aux = fibheap_insert (heap, edge_badness (edge, false), edge);
1471 /* Remove EDGE from the fibheap. */
1473 static void
1474 heap_edge_removal_hook (struct cgraph_edge *e, void *data)
1476 if (e->callee)
1477 reset_node_growth_cache (e->callee);
1478 if (e->aux)
1480 fibheap_delete_node ((fibheap_t)data, (fibnode_t)e->aux);
1481 e->aux = NULL;
1485 /* Return true if speculation of edge E seems useful.
1486 If ANTICIPATE_INLINING is true, be conservative and hope that E
1487 may get inlined. */
1489 bool
1490 speculation_useful_p (struct cgraph_edge *e, bool anticipate_inlining)
1492 enum availability avail;
1493 struct cgraph_node *target = e->callee->ultimate_alias_target (&avail);
1494 struct cgraph_edge *direct, *indirect;
1495 struct ipa_ref *ref;
1497 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1499 if (!e->maybe_hot_p ())
1500 return false;
1502 /* See if IP optimizations found something potentially useful about the
1503 function. For now we look only for CONST/PURE flags. Almost everything
1504 else we propagate is useless. */
1505 if (avail >= AVAIL_AVAILABLE)
1507 int ecf_flags = flags_from_decl_or_type (target->decl);
1508 if (ecf_flags & ECF_CONST)
1510 e->speculative_call_info (direct, indirect, ref);
1511 if (!(indirect->indirect_info->ecf_flags & ECF_CONST))
1512 return true;
1514 else if (ecf_flags & ECF_PURE)
1516 e->speculative_call_info (direct, indirect, ref);
1517 if (!(indirect->indirect_info->ecf_flags & ECF_PURE))
1518 return true;
1521 /* If we did not managed to inline the function nor redirect
1522 to an ipa-cp clone (that are seen by having local flag set),
1523 it is probably pointless to inline it unless hardware is missing
1524 indirect call predictor. */
1525 if (!anticipate_inlining && e->inline_failed && !target->local.local)
1526 return false;
1527 /* For overwritable targets there is not much to do. */
1528 if (e->inline_failed && !can_inline_edge_p (e, false, true))
1529 return false;
1530 /* OK, speculation seems interesting. */
1531 return true;
1534 /* We know that EDGE is not going to be inlined.
1535 See if we can remove speculation. */
1537 static void
1538 resolve_noninline_speculation (fibheap_t edge_heap, struct cgraph_edge *edge)
1540 if (edge->speculative && !speculation_useful_p (edge, false))
1542 struct cgraph_node *node = edge->caller;
1543 struct cgraph_node *where = node->global.inlined_to
1544 ? node->global.inlined_to : node;
1545 bitmap updated_nodes = BITMAP_ALLOC (NULL);
1547 spec_rem += edge->count;
1548 edge->resolve_speculation ();
1549 reset_edge_caches (where);
1550 inline_update_overall_summary (where);
1551 update_caller_keys (edge_heap, where,
1552 updated_nodes, NULL);
1553 update_callee_keys (edge_heap, where,
1554 updated_nodes);
1555 BITMAP_FREE (updated_nodes);
1559 /* We use greedy algorithm for inlining of small functions:
1560 All inline candidates are put into prioritized heap ordered in
1561 increasing badness.
1563 The inlining of small functions is bounded by unit growth parameters. */
1565 static void
1566 inline_small_functions (void)
1568 struct cgraph_node *node;
1569 struct cgraph_edge *edge;
1570 fibheap_t edge_heap = fibheap_new ();
1571 bitmap updated_nodes = BITMAP_ALLOC (NULL);
1572 int min_size, max_size;
1573 auto_vec<cgraph_edge *> new_indirect_edges;
1574 int initial_size = 0;
1575 struct cgraph_node **order = XCNEWVEC (cgraph_node *, symtab->cgraph_count);
1576 struct cgraph_edge_hook_list *edge_removal_hook_holder;
1577 if (flag_indirect_inlining)
1578 new_indirect_edges.create (8);
1580 edge_removal_hook_holder
1581 = symtab->add_edge_removal_hook (&heap_edge_removal_hook, edge_heap);
1583 /* Compute overall unit size and other global parameters used by badness
1584 metrics. */
1586 max_count = 0;
1587 ipa_reduced_postorder (order, true, true, NULL);
1588 free (order);
1590 FOR_EACH_DEFINED_FUNCTION (node)
1591 if (!node->global.inlined_to)
1593 if (node->has_gimple_body_p ()
1594 || node->thunk.thunk_p)
1596 struct inline_summary *info = inline_summary (node);
1597 struct ipa_dfs_info *dfs = (struct ipa_dfs_info *) node->aux;
1599 /* Do not account external functions, they will be optimized out
1600 if not inlined. Also only count the non-cold portion of program. */
1601 if (!DECL_EXTERNAL (node->decl)
1602 && node->frequency != NODE_FREQUENCY_UNLIKELY_EXECUTED)
1603 initial_size += info->size;
1604 info->growth = estimate_growth (node);
1605 if (dfs && dfs->next_cycle)
1607 struct cgraph_node *n2;
1608 int id = dfs->scc_no + 1;
1609 for (n2 = node; n2;
1610 n2 = ((struct ipa_dfs_info *) node->aux)->next_cycle)
1612 struct inline_summary *info2 = inline_summary (n2);
1613 if (info2->scc_no)
1614 break;
1615 info2->scc_no = id;
1620 for (edge = node->callers; edge; edge = edge->next_caller)
1621 if (max_count < edge->count)
1622 max_count = edge->count;
1624 sreal_init (&max_count_real, max_count, 0);
1625 sreal_init (&max_relbenefit_real, RELATIVE_TIME_BENEFIT_RANGE, 0);
1626 sreal_init (&half_int_min_real, INT_MAX / 2, 0);
1627 ipa_free_postorder_info ();
1628 initialize_growth_caches ();
1630 if (dump_file)
1631 fprintf (dump_file,
1632 "\nDeciding on inlining of small functions. Starting with size %i.\n",
1633 initial_size);
1635 overall_size = initial_size;
1636 max_size = compute_max_insns (overall_size);
1637 min_size = overall_size;
1639 /* Populate the heap with all edges we might inline. */
1641 FOR_EACH_DEFINED_FUNCTION (node)
1643 bool update = false;
1644 struct cgraph_edge *next;
1646 if (dump_file)
1647 fprintf (dump_file, "Enqueueing calls in %s/%i.\n",
1648 node->name (), node->order);
1650 for (edge = node->callees; edge; edge = next)
1652 next = edge->next_callee;
1653 if (edge->inline_failed
1654 && !edge->aux
1655 && can_inline_edge_p (edge, true)
1656 && want_inline_small_function_p (edge, true)
1657 && edge->inline_failed)
1659 gcc_assert (!edge->aux);
1660 update_edge_key (edge_heap, edge);
1662 if (edge->speculative && !speculation_useful_p (edge, edge->aux != NULL))
1664 edge->resolve_speculation ();
1665 update = true;
1668 if (update)
1670 struct cgraph_node *where = node->global.inlined_to
1671 ? node->global.inlined_to : node;
1672 inline_update_overall_summary (where);
1673 reset_node_growth_cache (where);
1674 reset_edge_caches (where);
1675 update_caller_keys (edge_heap, where,
1676 updated_nodes, NULL);
1677 bitmap_clear (updated_nodes);
1681 gcc_assert (in_lto_p
1682 || !max_count
1683 || (profile_info && flag_branch_probabilities));
1685 while (!fibheap_empty (edge_heap))
1687 int old_size = overall_size;
1688 struct cgraph_node *where, *callee;
1689 int badness = fibheap_min_key (edge_heap);
1690 int current_badness;
1691 int cached_badness;
1692 int growth;
1694 edge = (struct cgraph_edge *) fibheap_extract_min (edge_heap);
1695 gcc_assert (edge->aux);
1696 edge->aux = NULL;
1697 if (!edge->inline_failed || !edge->callee->analyzed)
1698 continue;
1700 /* Be sure that caches are maintained consistent.
1701 We can not make this ENABLE_CHECKING only because it cause different
1702 updates of the fibheap queue. */
1703 cached_badness = edge_badness (edge, false);
1704 reset_edge_growth_cache (edge);
1705 reset_node_growth_cache (edge->callee);
1707 /* When updating the edge costs, we only decrease badness in the keys.
1708 Increases of badness are handled lazilly; when we see key with out
1709 of date value on it, we re-insert it now. */
1710 current_badness = edge_badness (edge, false);
1711 gcc_assert (cached_badness == current_badness);
1712 gcc_assert (current_badness >= badness);
1713 if (current_badness != badness)
1715 edge->aux = fibheap_insert (edge_heap, current_badness, edge);
1716 continue;
1719 if (!can_inline_edge_p (edge, true))
1721 resolve_noninline_speculation (edge_heap, edge);
1722 continue;
1725 callee = edge->callee->ultimate_alias_target ();
1726 growth = estimate_edge_growth (edge);
1727 if (dump_file)
1729 fprintf (dump_file,
1730 "\nConsidering %s/%i with %i size\n",
1731 callee->name (), callee->order,
1732 inline_summary (callee)->size);
1733 fprintf (dump_file,
1734 " to be inlined into %s/%i in %s:%i\n"
1735 " Estimated badness is %i, frequency %.2f.\n",
1736 edge->caller->name (), edge->caller->order,
1737 flag_wpa ? "unknown"
1738 : gimple_filename ((const_gimple) edge->call_stmt),
1739 flag_wpa ? -1
1740 : gimple_lineno ((const_gimple) edge->call_stmt),
1741 badness,
1742 edge->frequency / (double)CGRAPH_FREQ_BASE);
1743 if (edge->count)
1744 fprintf (dump_file," Called %"PRId64"x\n",
1745 edge->count);
1746 if (dump_flags & TDF_DETAILS)
1747 edge_badness (edge, true);
1750 if (overall_size + growth > max_size
1751 && !DECL_DISREGARD_INLINE_LIMITS (callee->decl))
1753 edge->inline_failed = CIF_INLINE_UNIT_GROWTH_LIMIT;
1754 report_inline_failed_reason (edge);
1755 resolve_noninline_speculation (edge_heap, edge);
1756 continue;
1759 if (!want_inline_small_function_p (edge, true))
1761 resolve_noninline_speculation (edge_heap, edge);
1762 continue;
1765 /* Heuristics for inlining small functions work poorly for
1766 recursive calls where we do effects similar to loop unrolling.
1767 When inlining such edge seems profitable, leave decision on
1768 specific inliner. */
1769 if (edge->recursive_p ())
1771 where = edge->caller;
1772 if (where->global.inlined_to)
1773 where = where->global.inlined_to;
1774 if (!recursive_inlining (edge,
1775 flag_indirect_inlining
1776 ? &new_indirect_edges : NULL))
1778 edge->inline_failed = CIF_RECURSIVE_INLINING;
1779 resolve_noninline_speculation (edge_heap, edge);
1780 continue;
1782 reset_edge_caches (where);
1783 /* Recursive inliner inlines all recursive calls of the function
1784 at once. Consequently we need to update all callee keys. */
1785 if (flag_indirect_inlining)
1786 add_new_edges_to_heap (edge_heap, new_indirect_edges);
1787 update_callee_keys (edge_heap, where, updated_nodes);
1788 bitmap_clear (updated_nodes);
1790 else
1792 struct cgraph_node *outer_node = NULL;
1793 int depth = 0;
1795 /* Consider the case where self recursive function A is inlined
1796 into B. This is desired optimization in some cases, since it
1797 leads to effect similar of loop peeling and we might completely
1798 optimize out the recursive call. However we must be extra
1799 selective. */
1801 where = edge->caller;
1802 while (where->global.inlined_to)
1804 if (where->decl == callee->decl)
1805 outer_node = where, depth++;
1806 where = where->callers->caller;
1808 if (outer_node
1809 && !want_inline_self_recursive_call_p (edge, outer_node,
1810 true, depth))
1812 edge->inline_failed
1813 = (DECL_DISREGARD_INLINE_LIMITS (edge->callee->decl)
1814 ? CIF_RECURSIVE_INLINING : CIF_UNSPECIFIED);
1815 resolve_noninline_speculation (edge_heap, edge);
1816 continue;
1818 else if (depth && dump_file)
1819 fprintf (dump_file, " Peeling recursion with depth %i\n", depth);
1821 gcc_checking_assert (!callee->global.inlined_to);
1822 inline_call (edge, true, &new_indirect_edges, &overall_size, true);
1823 if (flag_indirect_inlining)
1824 add_new_edges_to_heap (edge_heap, new_indirect_edges);
1826 reset_edge_caches (edge->callee);
1827 reset_node_growth_cache (callee);
1829 update_callee_keys (edge_heap, where, updated_nodes);
1831 where = edge->caller;
1832 if (where->global.inlined_to)
1833 where = where->global.inlined_to;
1835 /* Our profitability metric can depend on local properties
1836 such as number of inlinable calls and size of the function body.
1837 After inlining these properties might change for the function we
1838 inlined into (since it's body size changed) and for the functions
1839 called by function we inlined (since number of it inlinable callers
1840 might change). */
1841 update_caller_keys (edge_heap, where, updated_nodes, NULL);
1842 bitmap_clear (updated_nodes);
1844 if (dump_file)
1846 fprintf (dump_file,
1847 " Inlined into %s which now has time %i and size %i,"
1848 "net change of %+i.\n",
1849 edge->caller->name (),
1850 inline_summary (edge->caller)->time,
1851 inline_summary (edge->caller)->size,
1852 overall_size - old_size);
1854 if (min_size > overall_size)
1856 min_size = overall_size;
1857 max_size = compute_max_insns (min_size);
1859 if (dump_file)
1860 fprintf (dump_file, "New minimal size reached: %i\n", min_size);
1864 free_growth_caches ();
1865 fibheap_delete (edge_heap);
1866 if (dump_file)
1867 fprintf (dump_file,
1868 "Unit growth for small function inlining: %i->%i (%i%%)\n",
1869 initial_size, overall_size,
1870 initial_size ? overall_size * 100 / (initial_size) - 100: 0);
1871 BITMAP_FREE (updated_nodes);
1872 symtab->remove_edge_removal_hook (edge_removal_hook_holder);
1875 /* Flatten NODE. Performed both during early inlining and
1876 at IPA inlining time. */
1878 static void
1879 flatten_function (struct cgraph_node *node, bool early)
1881 struct cgraph_edge *e;
1883 /* We shouldn't be called recursively when we are being processed. */
1884 gcc_assert (node->aux == NULL);
1886 node->aux = (void *) node;
1888 for (e = node->callees; e; e = e->next_callee)
1890 struct cgraph_node *orig_callee;
1891 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
1893 /* We've hit cycle? It is time to give up. */
1894 if (callee->aux)
1896 if (dump_file)
1897 fprintf (dump_file,
1898 "Not inlining %s into %s to avoid cycle.\n",
1899 xstrdup (callee->name ()),
1900 xstrdup (e->caller->name ()));
1901 e->inline_failed = CIF_RECURSIVE_INLINING;
1902 continue;
1905 /* When the edge is already inlined, we just need to recurse into
1906 it in order to fully flatten the leaves. */
1907 if (!e->inline_failed)
1909 flatten_function (callee, early);
1910 continue;
1913 /* Flatten attribute needs to be processed during late inlining. For
1914 extra code quality we however do flattening during early optimization,
1915 too. */
1916 if (!early
1917 ? !can_inline_edge_p (e, true)
1918 : !can_early_inline_edge_p (e))
1919 continue;
1921 if (e->recursive_p ())
1923 if (dump_file)
1924 fprintf (dump_file, "Not inlining: recursive call.\n");
1925 continue;
1928 if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
1929 != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
1931 if (dump_file)
1932 fprintf (dump_file, "Not inlining: SSA form does not match.\n");
1933 continue;
1936 /* Inline the edge and flatten the inline clone. Avoid
1937 recursing through the original node if the node was cloned. */
1938 if (dump_file)
1939 fprintf (dump_file, " Inlining %s into %s.\n",
1940 xstrdup (callee->name ()),
1941 xstrdup (e->caller->name ()));
1942 orig_callee = callee;
1943 inline_call (e, true, NULL, NULL, false);
1944 if (e->callee != orig_callee)
1945 orig_callee->aux = (void *) node;
1946 flatten_function (e->callee, early);
1947 if (e->callee != orig_callee)
1948 orig_callee->aux = NULL;
1951 node->aux = NULL;
1952 if (!node->global.inlined_to)
1953 inline_update_overall_summary (node);
1956 /* Count number of callers of NODE and store it into DATA (that
1957 points to int. Worker for cgraph_for_node_and_aliases. */
1959 static bool
1960 sum_callers (struct cgraph_node *node, void *data)
1962 struct cgraph_edge *e;
1963 int *num_calls = (int *)data;
1965 for (e = node->callers; e; e = e->next_caller)
1966 (*num_calls)++;
1967 return false;
1970 /* Inline NODE to all callers. Worker for cgraph_for_node_and_aliases.
1971 DATA points to number of calls originally found so we avoid infinite
1972 recursion. */
1974 static bool
1975 inline_to_all_callers (struct cgraph_node *node, void *data)
1977 int *num_calls = (int *)data;
1978 bool callee_removed = false;
1980 while (node->callers && !node->global.inlined_to)
1982 struct cgraph_node *caller = node->callers->caller;
1984 if (dump_file)
1986 fprintf (dump_file,
1987 "\nInlining %s size %i.\n",
1988 node->name (),
1989 inline_summary (node)->size);
1990 fprintf (dump_file,
1991 " Called once from %s %i insns.\n",
1992 node->callers->caller->name (),
1993 inline_summary (node->callers->caller)->size);
1996 inline_call (node->callers, true, NULL, NULL, true, &callee_removed);
1997 if (dump_file)
1998 fprintf (dump_file,
1999 " Inlined into %s which now has %i size\n",
2000 caller->name (),
2001 inline_summary (caller)->size);
2002 if (!(*num_calls)--)
2004 if (dump_file)
2005 fprintf (dump_file, "New calls found; giving up.\n");
2006 return callee_removed;
2008 if (callee_removed)
2009 return true;
2011 return false;
2014 /* Output overall time estimate. */
2015 static void
2016 dump_overall_stats (void)
2018 int64_t sum_weighted = 0, sum = 0;
2019 struct cgraph_node *node;
2021 FOR_EACH_DEFINED_FUNCTION (node)
2022 if (!node->global.inlined_to
2023 && !node->alias)
2025 int time = inline_summary (node)->time;
2026 sum += time;
2027 sum_weighted += time * node->count;
2029 fprintf (dump_file, "Overall time estimate: "
2030 "%"PRId64" weighted by profile: "
2031 "%"PRId64"\n", sum, sum_weighted);
2034 /* Output some useful stats about inlining. */
2036 static void
2037 dump_inline_stats (void)
2039 int64_t inlined_cnt = 0, inlined_indir_cnt = 0;
2040 int64_t inlined_virt_cnt = 0, inlined_virt_indir_cnt = 0;
2041 int64_t noninlined_cnt = 0, noninlined_indir_cnt = 0;
2042 int64_t noninlined_virt_cnt = 0, noninlined_virt_indir_cnt = 0;
2043 int64_t inlined_speculative = 0, inlined_speculative_ply = 0;
2044 int64_t indirect_poly_cnt = 0, indirect_cnt = 0;
2045 int64_t reason[CIF_N_REASONS][3];
2046 int i;
2047 struct cgraph_node *node;
2049 memset (reason, 0, sizeof (reason));
2050 FOR_EACH_DEFINED_FUNCTION (node)
2052 struct cgraph_edge *e;
2053 for (e = node->callees; e; e = e->next_callee)
2055 if (e->inline_failed)
2057 reason[(int) e->inline_failed][0] += e->count;
2058 reason[(int) e->inline_failed][1] += e->frequency;
2059 reason[(int) e->inline_failed][2] ++;
2060 if (DECL_VIRTUAL_P (e->callee->decl))
2062 if (e->indirect_inlining_edge)
2063 noninlined_virt_indir_cnt += e->count;
2064 else
2065 noninlined_virt_cnt += e->count;
2067 else
2069 if (e->indirect_inlining_edge)
2070 noninlined_indir_cnt += e->count;
2071 else
2072 noninlined_cnt += e->count;
2075 else
2077 if (e->speculative)
2079 if (DECL_VIRTUAL_P (e->callee->decl))
2080 inlined_speculative_ply += e->count;
2081 else
2082 inlined_speculative += e->count;
2084 else if (DECL_VIRTUAL_P (e->callee->decl))
2086 if (e->indirect_inlining_edge)
2087 inlined_virt_indir_cnt += e->count;
2088 else
2089 inlined_virt_cnt += e->count;
2091 else
2093 if (e->indirect_inlining_edge)
2094 inlined_indir_cnt += e->count;
2095 else
2096 inlined_cnt += e->count;
2100 for (e = node->indirect_calls; e; e = e->next_callee)
2101 if (e->indirect_info->polymorphic)
2102 indirect_poly_cnt += e->count;
2103 else
2104 indirect_cnt += e->count;
2106 if (max_count)
2108 fprintf (dump_file,
2109 "Inlined %"PRId64 " + speculative "
2110 "%"PRId64 " + speculative polymorphic "
2111 "%"PRId64 " + previously indirect "
2112 "%"PRId64 " + virtual "
2113 "%"PRId64 " + virtual and previously indirect "
2114 "%"PRId64 "\n" "Not inlined "
2115 "%"PRId64 " + previously indirect "
2116 "%"PRId64 " + virtual "
2117 "%"PRId64 " + virtual and previously indirect "
2118 "%"PRId64 " + stil indirect "
2119 "%"PRId64 " + still indirect polymorphic "
2120 "%"PRId64 "\n", inlined_cnt,
2121 inlined_speculative, inlined_speculative_ply,
2122 inlined_indir_cnt, inlined_virt_cnt, inlined_virt_indir_cnt,
2123 noninlined_cnt, noninlined_indir_cnt, noninlined_virt_cnt,
2124 noninlined_virt_indir_cnt, indirect_cnt, indirect_poly_cnt);
2125 fprintf (dump_file,
2126 "Removed speculations %"PRId64 "\n",
2127 spec_rem);
2129 dump_overall_stats ();
2130 fprintf (dump_file, "\nWhy inlining failed?\n");
2131 for (i = 0; i < CIF_N_REASONS; i++)
2132 if (reason[i][2])
2133 fprintf (dump_file, "%-50s: %8i calls, %8i freq, %"PRId64" count\n",
2134 cgraph_inline_failed_string ((cgraph_inline_failed_t) i),
2135 (int) reason[i][2], (int) reason[i][1], reason[i][0]);
2138 /* Decide on the inlining. We do so in the topological order to avoid
2139 expenses on updating data structures. */
2141 static unsigned int
2142 ipa_inline (void)
2144 struct cgraph_node *node;
2145 int nnodes;
2146 struct cgraph_node **order;
2147 int i;
2148 int cold;
2149 bool remove_functions = false;
2151 if (!optimize)
2152 return 0;
2154 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2156 if (in_lto_p && optimize)
2157 ipa_update_after_lto_read ();
2159 if (dump_file)
2160 dump_inline_summaries (dump_file);
2162 nnodes = ipa_reverse_postorder (order);
2164 FOR_EACH_FUNCTION (node)
2165 node->aux = 0;
2167 if (dump_file)
2168 fprintf (dump_file, "\nFlattening functions:\n");
2170 /* In the first pass handle functions to be flattened. Do this with
2171 a priority so none of our later choices will make this impossible. */
2172 for (i = nnodes - 1; i >= 0; i--)
2174 node = order[i];
2176 /* Handle nodes to be flattened.
2177 Ideally when processing callees we stop inlining at the
2178 entry of cycles, possibly cloning that entry point and
2179 try to flatten itself turning it into a self-recursive
2180 function. */
2181 if (lookup_attribute ("flatten",
2182 DECL_ATTRIBUTES (node->decl)) != NULL)
2184 if (dump_file)
2185 fprintf (dump_file,
2186 "Flattening %s\n", node->name ());
2187 flatten_function (node, false);
2190 if (dump_file)
2191 dump_overall_stats ();
2193 inline_small_functions ();
2195 /* Do first after-inlining removal. We want to remove all "stale" extern inline
2196 functions and virtual functions so we really know what is called once. */
2197 symtab->remove_unreachable_nodes (false, dump_file);
2198 free (order);
2200 /* Inline functions with a property that after inlining into all callers the
2201 code size will shrink because the out-of-line copy is eliminated.
2202 We do this regardless on the callee size as long as function growth limits
2203 are met. */
2204 if (dump_file)
2205 fprintf (dump_file,
2206 "\nDeciding on functions to be inlined into all callers and removing useless speculations:\n");
2208 /* Inlining one function called once has good chance of preventing
2209 inlining other function into the same callee. Ideally we should
2210 work in priority order, but probably inlining hot functions first
2211 is good cut without the extra pain of maintaining the queue.
2213 ??? this is not really fitting the bill perfectly: inlining function
2214 into callee often leads to better optimization of callee due to
2215 increased context for optimization.
2216 For example if main() function calls a function that outputs help
2217 and then function that does the main optmization, we should inline
2218 the second with priority even if both calls are cold by themselves.
2220 We probably want to implement new predicate replacing our use of
2221 maybe_hot_edge interpreted as maybe_hot_edge || callee is known
2222 to be hot. */
2223 for (cold = 0; cold <= 1; cold ++)
2225 FOR_EACH_DEFINED_FUNCTION (node)
2227 struct cgraph_edge *edge, *next;
2228 bool update=false;
2230 for (edge = node->callees; edge; edge = next)
2232 next = edge->next_callee;
2233 if (edge->speculative && !speculation_useful_p (edge, false))
2235 edge->resolve_speculation ();
2236 spec_rem += edge->count;
2237 update = true;
2238 remove_functions = true;
2241 if (update)
2243 struct cgraph_node *where = node->global.inlined_to
2244 ? node->global.inlined_to : node;
2245 reset_node_growth_cache (where);
2246 reset_edge_caches (where);
2247 inline_update_overall_summary (where);
2249 if (flag_inline_functions_called_once
2250 && want_inline_function_to_all_callers_p (node, cold))
2252 int num_calls = 0;
2253 node->call_for_symbol_thunks_and_aliases (sum_callers, &num_calls,
2254 true);
2255 while (node->call_for_symbol_thunks_and_aliases (inline_to_all_callers,
2256 &num_calls, true))
2258 remove_functions = true;
2263 /* Free ipa-prop structures if they are no longer needed. */
2264 if (optimize)
2265 ipa_free_all_structures_after_iinln ();
2267 if (dump_file)
2269 fprintf (dump_file,
2270 "\nInlined %i calls, eliminated %i functions\n\n",
2271 ncalls_inlined, nfunctions_inlined);
2272 dump_inline_stats ();
2275 if (dump_file)
2276 dump_inline_summaries (dump_file);
2277 /* In WPA we use inline summaries for partitioning process. */
2278 if (!flag_wpa)
2279 inline_free_summary ();
2280 return remove_functions ? TODO_remove_functions : 0;
2283 /* Inline always-inline function calls in NODE. */
2285 static bool
2286 inline_always_inline_functions (struct cgraph_node *node)
2288 struct cgraph_edge *e;
2289 bool inlined = false;
2291 for (e = node->callees; e; e = e->next_callee)
2293 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
2294 if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl))
2295 continue;
2297 if (e->recursive_p ())
2299 if (dump_file)
2300 fprintf (dump_file, " Not inlining recursive call to %s.\n",
2301 e->callee->name ());
2302 e->inline_failed = CIF_RECURSIVE_INLINING;
2303 continue;
2306 if (!can_early_inline_edge_p (e))
2308 /* Set inlined to true if the callee is marked "always_inline" but
2309 is not inlinable. This will allow flagging an error later in
2310 expand_call_inline in tree-inline.c. */
2311 if (lookup_attribute ("always_inline",
2312 DECL_ATTRIBUTES (callee->decl)) != NULL)
2313 inlined = true;
2314 continue;
2317 if (dump_file)
2318 fprintf (dump_file, " Inlining %s into %s (always_inline).\n",
2319 xstrdup (e->callee->name ()),
2320 xstrdup (e->caller->name ()));
2321 inline_call (e, true, NULL, NULL, false);
2322 inlined = true;
2324 if (inlined)
2325 inline_update_overall_summary (node);
2327 return inlined;
2330 /* Decide on the inlining. We do so in the topological order to avoid
2331 expenses on updating data structures. */
2333 static bool
2334 early_inline_small_functions (struct cgraph_node *node)
2336 struct cgraph_edge *e;
2337 bool inlined = false;
2339 for (e = node->callees; e; e = e->next_callee)
2341 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
2342 if (!inline_summary (callee)->inlinable
2343 || !e->inline_failed)
2344 continue;
2346 /* Do not consider functions not declared inline. */
2347 if (!DECL_DECLARED_INLINE_P (callee->decl)
2348 && !flag_inline_small_functions
2349 && !flag_inline_functions)
2350 continue;
2352 if (dump_file)
2353 fprintf (dump_file, "Considering inline candidate %s.\n",
2354 callee->name ());
2356 if (!can_early_inline_edge_p (e))
2357 continue;
2359 if (e->recursive_p ())
2361 if (dump_file)
2362 fprintf (dump_file, " Not inlining: recursive call.\n");
2363 continue;
2366 if (!want_early_inline_function_p (e))
2367 continue;
2369 if (dump_file)
2370 fprintf (dump_file, " Inlining %s into %s.\n",
2371 xstrdup (callee->name ()),
2372 xstrdup (e->caller->name ()));
2373 inline_call (e, true, NULL, NULL, true);
2374 inlined = true;
2377 return inlined;
2380 unsigned int
2381 early_inliner (function *fun)
2383 struct cgraph_node *node = cgraph_node::get (current_function_decl);
2384 struct cgraph_edge *edge;
2385 unsigned int todo = 0;
2386 int iterations = 0;
2387 bool inlined = false;
2389 if (seen_error ())
2390 return 0;
2392 /* Do nothing if datastructures for ipa-inliner are already computed. This
2393 happens when some pass decides to construct new function and
2394 cgraph_add_new_function calls lowering passes and early optimization on
2395 it. This may confuse ourself when early inliner decide to inline call to
2396 function clone, because function clones don't have parameter list in
2397 ipa-prop matching their signature. */
2398 if (ipa_node_params_vector.exists ())
2399 return 0;
2401 #ifdef ENABLE_CHECKING
2402 node->verify ();
2403 #endif
2404 node->remove_all_references ();
2406 /* Even when not optimizing or not inlining inline always-inline
2407 functions. */
2408 inlined = inline_always_inline_functions (node);
2410 if (!optimize
2411 || flag_no_inline
2412 || !flag_early_inlining
2413 /* Never inline regular functions into always-inline functions
2414 during incremental inlining. This sucks as functions calling
2415 always inline functions will get less optimized, but at the
2416 same time inlining of functions calling always inline
2417 function into an always inline function might introduce
2418 cycles of edges to be always inlined in the callgraph.
2420 We might want to be smarter and just avoid this type of inlining. */
2421 || DECL_DISREGARD_INLINE_LIMITS (node->decl))
2423 else if (lookup_attribute ("flatten",
2424 DECL_ATTRIBUTES (node->decl)) != NULL)
2426 /* When the function is marked to be flattened, recursively inline
2427 all calls in it. */
2428 if (dump_file)
2429 fprintf (dump_file,
2430 "Flattening %s\n", node->name ());
2431 flatten_function (node, true);
2432 inlined = true;
2434 else
2436 /* We iterate incremental inlining to get trivial cases of indirect
2437 inlining. */
2438 while (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS)
2439 && early_inline_small_functions (node))
2441 timevar_push (TV_INTEGRATION);
2442 todo |= optimize_inline_calls (current_function_decl);
2444 /* Technically we ought to recompute inline parameters so the new
2445 iteration of early inliner works as expected. We however have
2446 values approximately right and thus we only need to update edge
2447 info that might be cleared out for newly discovered edges. */
2448 for (edge = node->callees; edge; edge = edge->next_callee)
2450 struct inline_edge_summary *es = inline_edge_summary (edge);
2451 es->call_stmt_size
2452 = estimate_num_insns (edge->call_stmt, &eni_size_weights);
2453 es->call_stmt_time
2454 = estimate_num_insns (edge->call_stmt, &eni_time_weights);
2455 if (edge->callee->decl
2456 && !gimple_check_call_matching_types (
2457 edge->call_stmt, edge->callee->decl, false))
2458 edge->call_stmt_cannot_inline_p = true;
2460 if (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS) - 1)
2461 inline_update_overall_summary (node);
2462 timevar_pop (TV_INTEGRATION);
2463 iterations++;
2464 inlined = false;
2466 if (dump_file)
2467 fprintf (dump_file, "Iterations: %i\n", iterations);
2470 if (inlined)
2472 timevar_push (TV_INTEGRATION);
2473 todo |= optimize_inline_calls (current_function_decl);
2474 timevar_pop (TV_INTEGRATION);
2477 fun->always_inline_functions_inlined = true;
2479 return todo;
2482 /* Do inlining of small functions. Doing so early helps profiling and other
2483 passes to be somewhat more effective and avoids some code duplication in
2484 later real inlining pass for testcases with very many function calls. */
2486 namespace {
2488 const pass_data pass_data_early_inline =
2490 GIMPLE_PASS, /* type */
2491 "einline", /* name */
2492 OPTGROUP_INLINE, /* optinfo_flags */
2493 TV_EARLY_INLINING, /* tv_id */
2494 PROP_ssa, /* properties_required */
2495 0, /* properties_provided */
2496 0, /* properties_destroyed */
2497 0, /* todo_flags_start */
2498 0, /* todo_flags_finish */
2501 class pass_early_inline : public gimple_opt_pass
2503 public:
2504 pass_early_inline (gcc::context *ctxt)
2505 : gimple_opt_pass (pass_data_early_inline, ctxt)
2508 /* opt_pass methods: */
2509 virtual unsigned int execute (function *);
2511 }; // class pass_early_inline
2513 unsigned int
2514 pass_early_inline::execute (function *fun)
2516 return early_inliner (fun);
2519 } // anon namespace
2521 gimple_opt_pass *
2522 make_pass_early_inline (gcc::context *ctxt)
2524 return new pass_early_inline (ctxt);
2527 namespace {
2529 const pass_data pass_data_ipa_inline =
2531 IPA_PASS, /* type */
2532 "inline", /* name */
2533 OPTGROUP_INLINE, /* optinfo_flags */
2534 TV_IPA_INLINING, /* tv_id */
2535 0, /* properties_required */
2536 0, /* properties_provided */
2537 0, /* properties_destroyed */
2538 0, /* todo_flags_start */
2539 ( TODO_dump_symtab ), /* todo_flags_finish */
2542 class pass_ipa_inline : public ipa_opt_pass_d
2544 public:
2545 pass_ipa_inline (gcc::context *ctxt)
2546 : ipa_opt_pass_d (pass_data_ipa_inline, ctxt,
2547 inline_generate_summary, /* generate_summary */
2548 inline_write_summary, /* write_summary */
2549 inline_read_summary, /* read_summary */
2550 NULL, /* write_optimization_summary */
2551 NULL, /* read_optimization_summary */
2552 NULL, /* stmt_fixup */
2553 0, /* function_transform_todo_flags_start */
2554 inline_transform, /* function_transform */
2555 NULL) /* variable_transform */
2558 /* opt_pass methods: */
2559 virtual unsigned int execute (function *) { return ipa_inline (); }
2561 }; // class pass_ipa_inline
2563 } // anon namespace
2565 ipa_opt_pass_d *
2566 make_pass_ipa_inline (gcc::context *ctxt)
2568 return new pass_ipa_inline (ctxt);