re PR bootstrap/51346 (LTO bootstrap failed with bootstrap-profiled)
[official-gcc.git] / gcc / ipa-inline.c
blobe3c6b3ce824fe43d08dc36725e9c2e3d9bccff93
1 /* Inlining decision heuristics.
2 Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Inlining decision heuristics
24 The implementation of inliner is organized as follows:
26 inlining heuristics limits
28 can_inline_edge_p allow to check that particular inlining is allowed
29 by the limits specified by user (allowed function growth, growth and so
30 on).
32 Functions are inlined when it is obvious the result is profitable (such
33 as functions called once or when inlining reduce code size).
34 In addition to that we perform inlining of small functions and recursive
35 inlining.
37 inlining heuristics
39 The inliner itself is split into two passes:
41 pass_early_inlining
43 Simple local inlining pass inlining callees into current function.
44 This pass makes no use of whole unit analysis and thus it can do only
45 very simple decisions based on local properties.
47 The strength of the pass is that it is run in topological order
48 (reverse postorder) on the callgraph. Functions are converted into SSA
49 form just before this pass and optimized subsequently. As a result, the
50 callees of the function seen by the early inliner was already optimized
51 and results of early inlining adds a lot of optimization opportunities
52 for the local optimization.
54 The pass handle the obvious inlining decisions within the compilation
55 unit - inlining auto inline functions, inlining for size and
56 flattening.
58 main strength of the pass is the ability to eliminate abstraction
59 penalty in C++ code (via combination of inlining and early
60 optimization) and thus improve quality of analysis done by real IPA
61 optimizers.
63 Because of lack of whole unit knowledge, the pass can not really make
64 good code size/performance tradeoffs. It however does very simple
65 speculative inlining allowing code size to grow by
66 EARLY_INLINING_INSNS when callee is leaf function. In this case the
67 optimizations performed later are very likely to eliminate the cost.
69 pass_ipa_inline
71 This is the real inliner able to handle inlining with whole program
72 knowledge. It performs following steps:
74 1) inlining of small functions. This is implemented by greedy
75 algorithm ordering all inlinable cgraph edges by their badness and
76 inlining them in this order as long as inline limits allows doing so.
78 This heuristics is not very good on inlining recursive calls. Recursive
79 calls can be inlined with results similar to loop unrolling. To do so,
80 special purpose recursive inliner is executed on function when
81 recursive edge is met as viable candidate.
83 2) Unreachable functions are removed from callgraph. Inlining leads
84 to devirtualization and other modification of callgraph so functions
85 may become unreachable during the process. Also functions declared as
86 extern inline or virtual functions are removed, since after inlining
87 we no longer need the offline bodies.
89 3) Functions called once and not exported from the unit are inlined.
90 This should almost always lead to reduction of code size by eliminating
91 the need for offline copy of the function. */
93 #include "config.h"
94 #include "system.h"
95 #include "coretypes.h"
96 #include "tm.h"
97 #include "tree.h"
98 #include "tree-inline.h"
99 #include "langhooks.h"
100 #include "flags.h"
101 #include "cgraph.h"
102 #include "diagnostic.h"
103 #include "gimple-pretty-print.h"
104 #include "timevar.h"
105 #include "params.h"
106 #include "fibheap.h"
107 #include "intl.h"
108 #include "tree-pass.h"
109 #include "coverage.h"
110 #include "ggc.h"
111 #include "rtl.h"
112 #include "tree-flow.h"
113 #include "ipa-prop.h"
114 #include "except.h"
115 #include "target.h"
116 #include "ipa-inline.h"
117 #include "ipa-utils.h"
119 /* Statistics we collect about inlining algorithm. */
120 static int overall_size;
121 static gcov_type max_count;
123 /* Return false when inlining edge E would lead to violating
124 limits on function unit growth or stack usage growth.
126 The relative function body growth limit is present generally
127 to avoid problems with non-linear behavior of the compiler.
128 To allow inlining huge functions into tiny wrapper, the limit
129 is always based on the bigger of the two functions considered.
131 For stack growth limits we always base the growth in stack usage
132 of the callers. We want to prevent applications from segfaulting
133 on stack overflow when functions with huge stack frames gets
134 inlined. */
136 static bool
137 caller_growth_limits (struct cgraph_edge *e)
139 struct cgraph_node *to = e->caller;
140 struct cgraph_node *what = cgraph_function_or_thunk_node (e->callee, NULL);
141 int newsize;
142 int limit = 0;
143 HOST_WIDE_INT stack_size_limit = 0, inlined_stack;
144 struct inline_summary *info, *what_info, *outer_info = inline_summary (to);
146 /* Look for function e->caller is inlined to. While doing
147 so work out the largest function body on the way. As
148 described above, we want to base our function growth
149 limits based on that. Not on the self size of the
150 outer function, not on the self size of inline code
151 we immediately inline to. This is the most relaxed
152 interpretation of the rule "do not grow large functions
153 too much in order to prevent compiler from exploding". */
154 while (true)
156 info = inline_summary (to);
157 if (limit < info->self_size)
158 limit = info->self_size;
159 if (stack_size_limit < info->estimated_self_stack_size)
160 stack_size_limit = info->estimated_self_stack_size;
161 if (to->global.inlined_to)
162 to = to->callers->caller;
163 else
164 break;
167 what_info = inline_summary (what);
169 if (limit < what_info->self_size)
170 limit = what_info->self_size;
172 limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
174 /* Check the size after inlining against the function limits. But allow
175 the function to shrink if it went over the limits by forced inlining. */
176 newsize = estimate_size_after_inlining (to, e);
177 if (newsize >= info->size
178 && newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
179 && newsize > limit)
181 e->inline_failed = CIF_LARGE_FUNCTION_GROWTH_LIMIT;
182 return false;
185 if (!what_info->estimated_stack_size)
186 return true;
188 /* FIXME: Stack size limit often prevents inlining in Fortran programs
189 due to large i/o datastructures used by the Fortran front-end.
190 We ought to ignore this limit when we know that the edge is executed
191 on every invocation of the caller (i.e. its call statement dominates
192 exit block). We do not track this information, yet. */
193 stack_size_limit += ((gcov_type)stack_size_limit
194 * PARAM_VALUE (PARAM_STACK_FRAME_GROWTH) / 100);
196 inlined_stack = (outer_info->stack_frame_offset
197 + outer_info->estimated_self_stack_size
198 + what_info->estimated_stack_size);
199 /* Check new stack consumption with stack consumption at the place
200 stack is used. */
201 if (inlined_stack > stack_size_limit
202 /* If function already has large stack usage from sibling
203 inline call, we can inline, too.
204 This bit overoptimistically assume that we are good at stack
205 packing. */
206 && inlined_stack > info->estimated_stack_size
207 && inlined_stack > PARAM_VALUE (PARAM_LARGE_STACK_FRAME))
209 e->inline_failed = CIF_LARGE_STACK_FRAME_GROWTH_LIMIT;
210 return false;
212 return true;
215 /* Dump info about why inlining has failed. */
217 static void
218 report_inline_failed_reason (struct cgraph_edge *e)
220 if (dump_file)
222 fprintf (dump_file, " not inlinable: %s/%i -> %s/%i, %s\n",
223 cgraph_node_name (e->caller), e->caller->uid,
224 cgraph_node_name (e->callee), e->callee->uid,
225 cgraph_inline_failed_string (e->inline_failed));
229 /* Decide if we can inline the edge and possibly update
230 inline_failed reason.
231 We check whether inlining is possible at all and whether
232 caller growth limits allow doing so.
234 if REPORT is true, output reason to the dump file. */
236 static bool
237 can_inline_edge_p (struct cgraph_edge *e, bool report)
239 bool inlinable = true;
240 enum availability avail;
241 struct cgraph_node *callee
242 = cgraph_function_or_thunk_node (e->callee, &avail);
243 tree caller_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (e->caller->decl);
244 tree callee_tree
245 = callee ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (callee->decl) : NULL;
246 struct function *caller_cfun = DECL_STRUCT_FUNCTION (e->caller->decl);
247 struct function *callee_cfun
248 = callee ? DECL_STRUCT_FUNCTION (callee->decl) : NULL;
249 bool call_stmt_cannot_inline_p;
251 /* If E has a call statement in it, use the inline attribute from
252 the statement, otherwise use the inline attribute in E. Edges
253 will not have statements when working in WPA mode. */
254 call_stmt_cannot_inline_p = (e->call_stmt)
255 ? gimple_call_cannot_inline_p (e->call_stmt)
256 : e->call_stmt_cannot_inline_p;
258 if (!caller_cfun && e->caller->clone_of)
259 caller_cfun = DECL_STRUCT_FUNCTION (e->caller->clone_of->decl);
261 if (!callee_cfun && callee && callee->clone_of)
262 callee_cfun = DECL_STRUCT_FUNCTION (callee->clone_of->decl);
264 gcc_assert (e->inline_failed);
266 if (!callee || !callee->analyzed)
268 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
269 inlinable = false;
271 else if (!inline_summary (callee)->inlinable)
273 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
274 inlinable = false;
276 else if (avail <= AVAIL_OVERWRITABLE)
278 e->inline_failed = CIF_OVERWRITABLE;
279 return false;
281 else if (call_stmt_cannot_inline_p)
283 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
284 inlinable = false;
286 /* Don't inline if the functions have different EH personalities. */
287 else if (DECL_FUNCTION_PERSONALITY (e->caller->decl)
288 && DECL_FUNCTION_PERSONALITY (callee->decl)
289 && (DECL_FUNCTION_PERSONALITY (e->caller->decl)
290 != DECL_FUNCTION_PERSONALITY (callee->decl)))
292 e->inline_failed = CIF_EH_PERSONALITY;
293 inlinable = false;
295 /* TM pure functions should not get inlined if the outer function is
296 a TM safe function. */
297 else if (is_tm_pure (callee->decl)
298 && is_tm_safe (e->caller->decl))
300 e->inline_failed = CIF_UNSPECIFIED;
301 inlinable = false;
303 /* Don't inline if the callee can throw non-call exceptions but the
304 caller cannot.
305 FIXME: this is obviously wrong for LTO where STRUCT_FUNCTION is missing.
306 Move the flag into cgraph node or mirror it in the inline summary. */
307 else if (callee_cfun && callee_cfun->can_throw_non_call_exceptions
308 && !(caller_cfun && caller_cfun->can_throw_non_call_exceptions))
310 e->inline_failed = CIF_NON_CALL_EXCEPTIONS;
311 inlinable = false;
313 /* Check compatibility of target optimization options. */
314 else if (!targetm.target_option.can_inline_p (e->caller->decl,
315 callee->decl))
317 e->inline_failed = CIF_TARGET_OPTION_MISMATCH;
318 inlinable = false;
320 /* Check if caller growth allows the inlining. */
321 else if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl)
322 && !lookup_attribute ("flatten",
323 DECL_ATTRIBUTES
324 (e->caller->global.inlined_to
325 ? e->caller->global.inlined_to->decl
326 : e->caller->decl))
327 && !caller_growth_limits (e))
328 inlinable = false;
329 /* Don't inline a function with a higher optimization level than the
330 caller. FIXME: this is really just tip of iceberg of handling
331 optimization attribute. */
332 else if (caller_tree != callee_tree)
334 struct cl_optimization *caller_opt
335 = TREE_OPTIMIZATION ((caller_tree)
336 ? caller_tree
337 : optimization_default_node);
339 struct cl_optimization *callee_opt
340 = TREE_OPTIMIZATION ((callee_tree)
341 ? callee_tree
342 : optimization_default_node);
344 if (((caller_opt->x_optimize > callee_opt->x_optimize)
345 || (caller_opt->x_optimize_size != callee_opt->x_optimize_size))
346 /* gcc.dg/pr43564.c. Look at forced inline even in -O0. */
347 && !DECL_DISREGARD_INLINE_LIMITS (e->callee->decl))
349 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
350 inlinable = false;
354 if (!inlinable && report)
355 report_inline_failed_reason (e);
356 return inlinable;
360 /* Return true if the edge E is inlinable during early inlining. */
362 static bool
363 can_early_inline_edge_p (struct cgraph_edge *e)
365 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee,
366 NULL);
367 /* Early inliner might get called at WPA stage when IPA pass adds new
368 function. In this case we can not really do any of early inlining
369 because function bodies are missing. */
370 if (!gimple_has_body_p (callee->decl))
372 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
373 return false;
375 /* In early inliner some of callees may not be in SSA form yet
376 (i.e. the callgraph is cyclic and we did not process
377 the callee by early inliner, yet). We don't have CIF code for this
378 case; later we will re-do the decision in the real inliner. */
379 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->caller->decl))
380 || !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
382 if (dump_file)
383 fprintf (dump_file, " edge not inlinable: not in SSA form\n");
384 return false;
386 if (!can_inline_edge_p (e, true))
387 return false;
388 return true;
392 /* Return true when N is leaf function. Accept cheap builtins
393 in leaf functions. */
395 static bool
396 leaf_node_p (struct cgraph_node *n)
398 struct cgraph_edge *e;
399 for (e = n->callees; e; e = e->next_callee)
400 if (!is_inexpensive_builtin (e->callee->decl))
401 return false;
402 return true;
406 /* Return true if we are interested in inlining small function. */
408 static bool
409 want_early_inline_function_p (struct cgraph_edge *e)
411 bool want_inline = true;
412 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
414 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
416 else if (!DECL_DECLARED_INLINE_P (callee->decl)
417 && !flag_inline_small_functions)
419 e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
420 report_inline_failed_reason (e);
421 want_inline = false;
423 else
425 int growth = estimate_edge_growth (e);
426 if (growth <= 0)
428 else if (!cgraph_maybe_hot_edge_p (e)
429 && growth > 0)
431 if (dump_file)
432 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
433 "call is cold and code would grow by %i\n",
434 cgraph_node_name (e->caller), e->caller->uid,
435 cgraph_node_name (callee), callee->uid,
436 growth);
437 want_inline = false;
439 else if (!leaf_node_p (callee)
440 && growth > 0)
442 if (dump_file)
443 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
444 "callee is not leaf and code would grow by %i\n",
445 cgraph_node_name (e->caller), e->caller->uid,
446 cgraph_node_name (callee), callee->uid,
447 growth);
448 want_inline = false;
450 else if (growth > PARAM_VALUE (PARAM_EARLY_INLINING_INSNS))
452 if (dump_file)
453 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
454 "growth %i exceeds --param early-inlining-insns\n",
455 cgraph_node_name (e->caller), e->caller->uid,
456 cgraph_node_name (callee), callee->uid,
457 growth);
458 want_inline = false;
461 return want_inline;
464 /* Return true if we are interested in inlining small function.
465 When REPORT is true, report reason to dump file. */
467 static bool
468 want_inline_small_function_p (struct cgraph_edge *e, bool report)
470 bool want_inline = true;
471 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
473 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
475 else if (!DECL_DECLARED_INLINE_P (callee->decl)
476 && !flag_inline_small_functions)
478 e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
479 want_inline = false;
481 else
483 int growth = estimate_edge_growth (e);
485 if (growth <= 0)
487 else if (DECL_DECLARED_INLINE_P (callee->decl)
488 && growth >= MAX_INLINE_INSNS_SINGLE)
490 e->inline_failed = CIF_MAX_INLINE_INSNS_SINGLE_LIMIT;
491 want_inline = false;
493 else if (!DECL_DECLARED_INLINE_P (callee->decl)
494 && !flag_inline_functions)
496 e->inline_failed = CIF_NOT_DECLARED_INLINED;
497 want_inline = false;
499 else if (!DECL_DECLARED_INLINE_P (callee->decl)
500 && growth >= MAX_INLINE_INSNS_AUTO)
502 e->inline_failed = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
503 want_inline = false;
505 /* If call is cold, do not inline when function body would grow.
506 Still inline when the overall unit size will shrink because the offline
507 copy of function being eliminated.
509 This is slightly wrong on aggressive side: it is entirely possible
510 that function is called many times with a context where inlining
511 reduces code size and few times with a context where inlining increase
512 code size. Resoluting growth estimate will be negative even if it
513 would make more sense to keep offline copy and do not inline into the
514 call sites that makes the code size grow.
516 When badness orders the calls in a way that code reducing calls come
517 first, this situation is not a problem at all: after inlining all
518 "good" calls, we will realize that keeping the function around is
519 better. */
520 else if (!cgraph_maybe_hot_edge_p (e)
521 && (DECL_EXTERNAL (callee->decl)
523 /* Unlike for functions called once, we play unsafe with
524 COMDATs. We can allow that since we know functions
525 in consideration are small (and thus risk is small) and
526 moreover grow estimates already accounts that COMDAT
527 functions may or may not disappear when eliminated from
528 current unit. With good probability making aggressive
529 choice in all units is going to make overall program
530 smaller.
532 Consequently we ask cgraph_can_remove_if_no_direct_calls_p
533 instead of
534 cgraph_will_be_removed_from_program_if_no_direct_calls */
536 || !cgraph_can_remove_if_no_direct_calls_p (callee)
537 || estimate_growth (callee) > 0))
539 e->inline_failed = CIF_UNLIKELY_CALL;
540 want_inline = false;
543 if (!want_inline && report)
544 report_inline_failed_reason (e);
545 return want_inline;
548 /* EDGE is self recursive edge.
549 We hand two cases - when function A is inlining into itself
550 or when function A is being inlined into another inliner copy of function
551 A within function B.
553 In first case OUTER_NODE points to the toplevel copy of A, while
554 in the second case OUTER_NODE points to the outermost copy of A in B.
556 In both cases we want to be extra selective since
557 inlining the call will just introduce new recursive calls to appear. */
559 static bool
560 want_inline_self_recursive_call_p (struct cgraph_edge *edge,
561 struct cgraph_node *outer_node,
562 bool peeling,
563 int depth)
565 char const *reason = NULL;
566 bool want_inline = true;
567 int caller_freq = CGRAPH_FREQ_BASE;
568 int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO);
570 if (DECL_DECLARED_INLINE_P (edge->caller->decl))
571 max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH);
573 if (!cgraph_maybe_hot_edge_p (edge))
575 reason = "recursive call is cold";
576 want_inline = false;
578 else if (max_count && !outer_node->count)
580 reason = "not executed in profile";
581 want_inline = false;
583 else if (depth > max_depth)
585 reason = "--param max-inline-recursive-depth exceeded.";
586 want_inline = false;
589 if (outer_node->global.inlined_to)
590 caller_freq = outer_node->callers->frequency;
592 if (!want_inline)
594 /* Inlining of self recursive function into copy of itself within other function
595 is transformation similar to loop peeling.
597 Peeling is profitable if we can inline enough copies to make probability
598 of actual call to the self recursive function very small. Be sure that
599 the probability of recursion is small.
601 We ensure that the frequency of recursing is at most 1 - (1/max_depth).
602 This way the expected number of recision is at most max_depth. */
603 else if (peeling)
605 int max_prob = CGRAPH_FREQ_BASE - ((CGRAPH_FREQ_BASE + max_depth - 1)
606 / max_depth);
607 int i;
608 for (i = 1; i < depth; i++)
609 max_prob = max_prob * max_prob / CGRAPH_FREQ_BASE;
610 if (max_count
611 && (edge->count * CGRAPH_FREQ_BASE / outer_node->count
612 >= max_prob))
614 reason = "profile of recursive call is too large";
615 want_inline = false;
617 if (!max_count
618 && (edge->frequency * CGRAPH_FREQ_BASE / caller_freq
619 >= max_prob))
621 reason = "frequency of recursive call is too large";
622 want_inline = false;
625 /* Recursive inlining, i.e. equivalent of unrolling, is profitable if recursion
626 depth is large. We reduce function call overhead and increase chances that
627 things fit in hardware return predictor.
629 Recursive inlining might however increase cost of stack frame setup
630 actually slowing down functions whose recursion tree is wide rather than
631 deep.
633 Deciding reliably on when to do recursive inlining without profile feedback
634 is tricky. For now we disable recursive inlining when probability of self
635 recursion is low.
637 Recursive inlining of self recursive call within loop also results in large loop
638 depths that generally optimize badly. We may want to throttle down inlining
639 in those cases. In particular this seems to happen in one of libstdc++ rb tree
640 methods. */
641 else
643 if (max_count
644 && (edge->count * 100 / outer_node->count
645 <= PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY)))
647 reason = "profile of recursive call is too small";
648 want_inline = false;
650 else if (!max_count
651 && (edge->frequency * 100 / caller_freq
652 <= PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY)))
654 reason = "frequency of recursive call is too small";
655 want_inline = false;
658 if (!want_inline && dump_file)
659 fprintf (dump_file, " not inlining recursively: %s\n", reason);
660 return want_inline;
663 /* Return true when NODE has caller other than EDGE.
664 Worker for cgraph_for_node_and_aliases. */
666 static bool
667 check_caller_edge (struct cgraph_node *node, void *edge)
669 return (node->callers
670 && node->callers != edge);
674 /* Decide if NODE is called once inlining it would eliminate need
675 for the offline copy of function. */
677 static bool
678 want_inline_function_called_once_p (struct cgraph_node *node)
680 struct cgraph_node *function = cgraph_function_or_thunk_node (node, NULL);
681 /* Already inlined? */
682 if (function->global.inlined_to)
683 return false;
684 /* Zero or more then one callers? */
685 if (!node->callers
686 || node->callers->next_caller)
687 return false;
688 /* Maybe other aliases has more direct calls. */
689 if (cgraph_for_node_and_aliases (node, check_caller_edge, node->callers, true))
690 return false;
691 /* Recursive call makes no sense to inline. */
692 if (cgraph_edge_recursive_p (node->callers))
693 return false;
694 /* External functions are not really in the unit, so inlining
695 them when called once would just increase the program size. */
696 if (DECL_EXTERNAL (function->decl))
697 return false;
698 /* Offline body must be optimized out. */
699 if (!cgraph_will_be_removed_from_program_if_no_direct_calls (function))
700 return false;
701 if (!can_inline_edge_p (node->callers, true))
702 return false;
703 return true;
707 /* Return relative time improvement for inlining EDGE in range
708 1...2^9. */
710 static inline int
711 relative_time_benefit (struct inline_summary *callee_info,
712 struct cgraph_edge *edge,
713 int time_growth)
715 int relbenefit;
716 gcov_type uninlined_call_time;
718 uninlined_call_time =
719 ((gcov_type)
720 (callee_info->time
721 + inline_edge_summary (edge)->call_stmt_time) * edge->frequency
722 + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
723 /* Compute relative time benefit, i.e. how much the call becomes faster.
724 ??? perhaps computing how much the caller+calle together become faster
725 would lead to more realistic results. */
726 if (!uninlined_call_time)
727 uninlined_call_time = 1;
728 relbenefit =
729 (uninlined_call_time - time_growth) * 256 / (uninlined_call_time);
730 relbenefit = MIN (relbenefit, 512);
731 relbenefit = MAX (relbenefit, 1);
732 return relbenefit;
736 /* A cost model driving the inlining heuristics in a way so the edges with
737 smallest badness are inlined first. After each inlining is performed
738 the costs of all caller edges of nodes affected are recomputed so the
739 metrics may accurately depend on values such as number of inlinable callers
740 of the function or function body size. */
742 static int
743 edge_badness (struct cgraph_edge *edge, bool dump)
745 gcov_type badness;
746 int growth, time_growth;
747 struct cgraph_node *callee = cgraph_function_or_thunk_node (edge->callee,
748 NULL);
749 struct inline_summary *callee_info = inline_summary (callee);
751 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
752 return INT_MIN;
754 growth = estimate_edge_growth (edge);
755 time_growth = estimate_edge_time (edge);
757 if (dump)
759 fprintf (dump_file, " Badness calculation for %s -> %s\n",
760 cgraph_node_name (edge->caller),
761 cgraph_node_name (callee));
762 fprintf (dump_file, " size growth %i, time growth %i\n",
763 growth,
764 time_growth);
767 /* Always prefer inlining saving code size. */
768 if (growth <= 0)
770 badness = INT_MIN / 2 + growth;
771 if (dump)
772 fprintf (dump_file, " %i: Growth %i <= 0\n", (int) badness,
773 growth);
776 /* When profiling is available, compute badness as:
778 relative_edge_count * relative_time_benefit
779 goodness = -------------------------------------------
780 edge_growth
781 badness = -goodness
783 The fraction is upside down, becuase on edge counts and time beneits
784 the bounds are known. Edge growth is essentially unlimited. */
786 else if (max_count)
788 int relbenefit = relative_time_benefit (callee_info, edge, time_growth);
789 badness =
790 ((int)
791 ((double) edge->count * INT_MIN / 2 / max_count / 512) *
792 relative_time_benefit (callee_info, edge, time_growth)) / growth;
794 /* Be sure that insanity of the profile won't lead to increasing counts
795 in the scalling and thus to overflow in the computation above. */
796 gcc_assert (max_count >= edge->count);
797 if (dump)
799 fprintf (dump_file,
800 " %i (relative %f): profile info. Relative count %f"
801 " * Relative benefit %f\n",
802 (int) badness, (double) badness / INT_MIN,
803 (double) edge->count / max_count,
804 relbenefit * 100 / 256.0);
808 /* When function local profile is available. Compute badness as:
811 growth_of_callee
812 badness = -------------------------------------- + growth_for-all
813 relative_time_benefit * edge_frequency
816 else if (flag_guess_branch_prob)
818 int div = edge->frequency * (1<<10) / CGRAPH_FREQ_MAX;
819 int growth_for_all;
821 div = MAX (div, 1);
822 gcc_checking_assert (edge->frequency <= CGRAPH_FREQ_MAX);
823 div *= relative_time_benefit (callee_info, edge, time_growth);
825 /* frequency is normalized in range 1...2^10.
826 relbenefit in range 1...2^9
827 DIV should be in range 1....2^19. */
828 gcc_checking_assert (div >= 1 && div <= (1<<19));
830 /* Result must be integer in range 0...INT_MAX.
831 Set the base of fixed point calculation so we don't lose much of
832 precision for small bandesses (those are interesting) yet we don't
833 overflow for growths that are still in interesting range.
835 Fixed point arithmetic with point at 8th bit. */
836 badness = ((gcov_type)growth) * (1<<(19+8));
837 badness = (badness + div / 2) / div;
839 /* Overall growth of inlining all calls of function matters: we want to
840 inline so offline copy of function is no longer needed.
842 Additionally functions that can be fully inlined without much of
843 effort are better inline candidates than functions that can be fully
844 inlined only after noticeable overall unit growths. The latter
845 are better in a sense compressing of code size by factoring out common
846 code into separate function shared by multiple code paths.
848 We might mix the valud into the fraction by taking into account
849 relative growth of the unit, but for now just add the number
850 into resulting fraction. */
851 if (badness > INT_MAX / 2)
853 badness = INT_MAX / 2;
854 if (dump)
855 fprintf (dump_file, "Badness overflow\n");
857 growth_for_all = estimate_growth (callee);
858 badness += growth_for_all;
859 if (dump)
861 fprintf (dump_file,
862 " %i: guessed profile. frequency %f, overall growth %i,"
863 " benefit %f%%, divisor %i\n",
864 (int) badness, (double)edge->frequency / CGRAPH_FREQ_BASE, growth_for_all,
865 relative_time_benefit (callee_info, edge, time_growth) * 100 / 256.0, div);
868 /* When function local profile is not available or it does not give
869 useful information (ie frequency is zero), base the cost on
870 loop nest and overall size growth, so we optimize for overall number
871 of functions fully inlined in program. */
872 else
874 int nest = MIN (inline_edge_summary (edge)->loop_depth, 8);
875 badness = estimate_growth (callee) * 256;
877 /* Decrease badness if call is nested. */
878 if (badness > 0)
879 badness >>= nest;
880 else
882 badness <<= nest;
884 if (dump)
885 fprintf (dump_file, " %i: no profile. nest %i\n", (int) badness,
886 nest);
889 /* Ensure that we did not overflow in all the fixed point math above. */
890 gcc_assert (badness >= INT_MIN);
891 gcc_assert (badness <= INT_MAX - 1);
892 /* Make recursive inlining happen always after other inlining is done. */
893 if (cgraph_edge_recursive_p (edge))
894 return badness + 1;
895 else
896 return badness;
899 /* Recompute badness of EDGE and update its key in HEAP if needed. */
900 static inline void
901 update_edge_key (fibheap_t heap, struct cgraph_edge *edge)
903 int badness = edge_badness (edge, false);
904 if (edge->aux)
906 fibnode_t n = (fibnode_t) edge->aux;
907 gcc_checking_assert (n->data == edge);
909 /* fibheap_replace_key only decrease the keys.
910 When we increase the key we do not update heap
911 and instead re-insert the element once it becomes
912 a minimum of heap. */
913 if (badness < n->key)
915 if (dump_file && (dump_flags & TDF_DETAILS))
917 fprintf (dump_file,
918 " decreasing badness %s/%i -> %s/%i, %i to %i\n",
919 cgraph_node_name (edge->caller), edge->caller->uid,
920 cgraph_node_name (edge->callee), edge->callee->uid,
921 (int)n->key,
922 badness);
924 fibheap_replace_key (heap, n, badness);
925 gcc_checking_assert (n->key == badness);
928 else
930 if (dump_file && (dump_flags & TDF_DETAILS))
932 fprintf (dump_file,
933 " enqueuing call %s/%i -> %s/%i, badness %i\n",
934 cgraph_node_name (edge->caller), edge->caller->uid,
935 cgraph_node_name (edge->callee), edge->callee->uid,
936 badness);
938 edge->aux = fibheap_insert (heap, badness, edge);
943 /* NODE was inlined.
944 All caller edges needs to be resetted because
945 size estimates change. Similarly callees needs reset
946 because better context may be known. */
948 static void
949 reset_edge_caches (struct cgraph_node *node)
951 struct cgraph_edge *edge;
952 struct cgraph_edge *e = node->callees;
953 struct cgraph_node *where = node;
954 int i;
955 struct ipa_ref *ref;
957 if (where->global.inlined_to)
958 where = where->global.inlined_to;
960 /* WHERE body size has changed, the cached growth is invalid. */
961 reset_node_growth_cache (where);
963 for (edge = where->callers; edge; edge = edge->next_caller)
964 if (edge->inline_failed)
965 reset_edge_growth_cache (edge);
966 for (i = 0; ipa_ref_list_refering_iterate (&where->ref_list, i, ref); i++)
967 if (ref->use == IPA_REF_ALIAS)
968 reset_edge_caches (ipa_ref_refering_node (ref));
970 if (!e)
971 return;
973 while (true)
974 if (!e->inline_failed && e->callee->callees)
975 e = e->callee->callees;
976 else
978 if (e->inline_failed)
979 reset_edge_growth_cache (e);
980 if (e->next_callee)
981 e = e->next_callee;
982 else
986 if (e->caller == node)
987 return;
988 e = e->caller->callers;
990 while (!e->next_callee);
991 e = e->next_callee;
996 /* Recompute HEAP nodes for each of caller of NODE.
997 UPDATED_NODES track nodes we already visited, to avoid redundant work.
998 When CHECK_INLINABLITY_FOR is set, re-check for specified edge that
999 it is inlinable. Otherwise check all edges. */
1001 static void
1002 update_caller_keys (fibheap_t heap, struct cgraph_node *node,
1003 bitmap updated_nodes,
1004 struct cgraph_edge *check_inlinablity_for)
1006 struct cgraph_edge *edge;
1007 int i;
1008 struct ipa_ref *ref;
1010 if ((!node->alias && !inline_summary (node)->inlinable)
1011 || cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE
1012 || node->global.inlined_to)
1013 return;
1014 if (!bitmap_set_bit (updated_nodes, node->uid))
1015 return;
1017 for (i = 0; ipa_ref_list_refering_iterate (&node->ref_list, i, ref); i++)
1018 if (ref->use == IPA_REF_ALIAS)
1020 struct cgraph_node *alias = ipa_ref_refering_node (ref);
1021 update_caller_keys (heap, alias, updated_nodes, check_inlinablity_for);
1024 for (edge = node->callers; edge; edge = edge->next_caller)
1025 if (edge->inline_failed)
1027 if (!check_inlinablity_for
1028 || check_inlinablity_for == edge)
1030 if (can_inline_edge_p (edge, false)
1031 && want_inline_small_function_p (edge, false))
1032 update_edge_key (heap, edge);
1033 else if (edge->aux)
1035 report_inline_failed_reason (edge);
1036 fibheap_delete_node (heap, (fibnode_t) edge->aux);
1037 edge->aux = NULL;
1040 else if (edge->aux)
1041 update_edge_key (heap, edge);
1045 /* Recompute HEAP nodes for each uninlined call in NODE.
1046 This is used when we know that edge badnesses are going only to increase
1047 (we introduced new call site) and thus all we need is to insert newly
1048 created edges into heap. */
1050 static void
1051 update_callee_keys (fibheap_t heap, struct cgraph_node *node,
1052 bitmap updated_nodes)
1054 struct cgraph_edge *e = node->callees;
1056 if (!e)
1057 return;
1058 while (true)
1059 if (!e->inline_failed && e->callee->callees)
1060 e = e->callee->callees;
1061 else
1063 enum availability avail;
1064 struct cgraph_node *callee;
1065 /* We do not reset callee growth cache here. Since we added a new call,
1066 growth chould have just increased and consequentely badness metric
1067 don't need updating. */
1068 if (e->inline_failed
1069 && (callee = cgraph_function_or_thunk_node (e->callee, &avail))
1070 && inline_summary (callee)->inlinable
1071 && cgraph_function_body_availability (callee) >= AVAIL_AVAILABLE
1072 && !bitmap_bit_p (updated_nodes, callee->uid))
1074 if (can_inline_edge_p (e, false)
1075 && want_inline_small_function_p (e, false))
1076 update_edge_key (heap, e);
1077 else if (e->aux)
1079 report_inline_failed_reason (e);
1080 fibheap_delete_node (heap, (fibnode_t) e->aux);
1081 e->aux = NULL;
1084 if (e->next_callee)
1085 e = e->next_callee;
1086 else
1090 if (e->caller == node)
1091 return;
1092 e = e->caller->callers;
1094 while (!e->next_callee);
1095 e = e->next_callee;
1100 /* Recompute heap nodes for each of caller edges of each of callees.
1101 Walk recursively into all inline clones. */
1103 static void
1104 update_all_callee_keys (fibheap_t heap, struct cgraph_node *node,
1105 bitmap updated_nodes)
1107 struct cgraph_edge *e = node->callees;
1108 if (!e)
1109 return;
1110 while (true)
1111 if (!e->inline_failed && e->callee->callees)
1112 e = e->callee->callees;
1113 else
1115 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee,
1116 NULL);
1118 /* We inlined and thus callees might have different number of calls.
1119 Reset their caches */
1120 reset_node_growth_cache (callee);
1121 if (e->inline_failed)
1122 update_caller_keys (heap, callee, updated_nodes, e);
1123 if (e->next_callee)
1124 e = e->next_callee;
1125 else
1129 if (e->caller == node)
1130 return;
1131 e = e->caller->callers;
1133 while (!e->next_callee);
1134 e = e->next_callee;
1139 /* Enqueue all recursive calls from NODE into priority queue depending on
1140 how likely we want to recursively inline the call. */
1142 static void
1143 lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
1144 fibheap_t heap)
1146 struct cgraph_edge *e;
1147 enum availability avail;
1149 for (e = where->callees; e; e = e->next_callee)
1150 if (e->callee == node
1151 || (cgraph_function_or_thunk_node (e->callee, &avail) == node
1152 && avail > AVAIL_OVERWRITABLE))
1154 /* When profile feedback is available, prioritize by expected number
1155 of calls. */
1156 fibheap_insert (heap,
1157 !max_count ? -e->frequency
1158 : -(e->count / ((max_count + (1<<24) - 1) / (1<<24))),
1161 for (e = where->callees; e; e = e->next_callee)
1162 if (!e->inline_failed)
1163 lookup_recursive_calls (node, e->callee, heap);
1166 /* Decide on recursive inlining: in the case function has recursive calls,
1167 inline until body size reaches given argument. If any new indirect edges
1168 are discovered in the process, add them to *NEW_EDGES, unless NEW_EDGES
1169 is NULL. */
1171 static bool
1172 recursive_inlining (struct cgraph_edge *edge,
1173 VEC (cgraph_edge_p, heap) **new_edges)
1175 int limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO);
1176 fibheap_t heap;
1177 struct cgraph_node *node;
1178 struct cgraph_edge *e;
1179 struct cgraph_node *master_clone = NULL, *next;
1180 int depth = 0;
1181 int n = 0;
1183 node = edge->caller;
1184 if (node->global.inlined_to)
1185 node = node->global.inlined_to;
1187 if (DECL_DECLARED_INLINE_P (node->decl))
1188 limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE);
1190 /* Make sure that function is small enough to be considered for inlining. */
1191 if (estimate_size_after_inlining (node, edge) >= limit)
1192 return false;
1193 heap = fibheap_new ();
1194 lookup_recursive_calls (node, node, heap);
1195 if (fibheap_empty (heap))
1197 fibheap_delete (heap);
1198 return false;
1201 if (dump_file)
1202 fprintf (dump_file,
1203 " Performing recursive inlining on %s\n",
1204 cgraph_node_name (node));
1206 /* Do the inlining and update list of recursive call during process. */
1207 while (!fibheap_empty (heap))
1209 struct cgraph_edge *curr
1210 = (struct cgraph_edge *) fibheap_extract_min (heap);
1211 struct cgraph_node *cnode;
1213 if (estimate_size_after_inlining (node, curr) > limit)
1214 break;
1216 if (!can_inline_edge_p (curr, true))
1217 continue;
1219 depth = 1;
1220 for (cnode = curr->caller;
1221 cnode->global.inlined_to; cnode = cnode->callers->caller)
1222 if (node->decl
1223 == cgraph_function_or_thunk_node (curr->callee, NULL)->decl)
1224 depth++;
1226 if (!want_inline_self_recursive_call_p (curr, node, false, depth))
1227 continue;
1229 if (dump_file)
1231 fprintf (dump_file,
1232 " Inlining call of depth %i", depth);
1233 if (node->count)
1235 fprintf (dump_file, " called approx. %.2f times per call",
1236 (double)curr->count / node->count);
1238 fprintf (dump_file, "\n");
1240 if (!master_clone)
1242 /* We need original clone to copy around. */
1243 master_clone = cgraph_clone_node (node, node->decl,
1244 node->count, CGRAPH_FREQ_BASE,
1245 false, NULL, true);
1246 for (e = master_clone->callees; e; e = e->next_callee)
1247 if (!e->inline_failed)
1248 clone_inlined_nodes (e, true, false, NULL);
1251 cgraph_redirect_edge_callee (curr, master_clone);
1252 inline_call (curr, false, new_edges, &overall_size);
1253 lookup_recursive_calls (node, curr->callee, heap);
1254 n++;
1257 if (!fibheap_empty (heap) && dump_file)
1258 fprintf (dump_file, " Recursive inlining growth limit met.\n");
1259 fibheap_delete (heap);
1261 if (!master_clone)
1262 return false;
1264 if (dump_file)
1265 fprintf (dump_file,
1266 "\n Inlined %i times, "
1267 "body grown from size %i to %i, time %i to %i\n", n,
1268 inline_summary (master_clone)->size, inline_summary (node)->size,
1269 inline_summary (master_clone)->time, inline_summary (node)->time);
1271 /* Remove master clone we used for inlining. We rely that clones inlined
1272 into master clone gets queued just before master clone so we don't
1273 need recursion. */
1274 for (node = cgraph_nodes; node != master_clone;
1275 node = next)
1277 next = node->next;
1278 if (node->global.inlined_to == master_clone)
1279 cgraph_remove_node (node);
1281 cgraph_remove_node (master_clone);
1282 return true;
1286 /* Given whole compilation unit estimate of INSNS, compute how large we can
1287 allow the unit to grow. */
1289 static int
1290 compute_max_insns (int insns)
1292 int max_insns = insns;
1293 if (max_insns < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
1294 max_insns = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
1296 return ((HOST_WIDEST_INT) max_insns
1297 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
1301 /* Compute badness of all edges in NEW_EDGES and add them to the HEAP. */
1303 static void
1304 add_new_edges_to_heap (fibheap_t heap, VEC (cgraph_edge_p, heap) *new_edges)
1306 while (VEC_length (cgraph_edge_p, new_edges) > 0)
1308 struct cgraph_edge *edge = VEC_pop (cgraph_edge_p, new_edges);
1310 gcc_assert (!edge->aux);
1311 if (edge->inline_failed
1312 && can_inline_edge_p (edge, true)
1313 && want_inline_small_function_p (edge, true))
1314 edge->aux = fibheap_insert (heap, edge_badness (edge, false), edge);
1319 /* We use greedy algorithm for inlining of small functions:
1320 All inline candidates are put into prioritized heap ordered in
1321 increasing badness.
1323 The inlining of small functions is bounded by unit growth parameters. */
1325 static void
1326 inline_small_functions (void)
1328 struct cgraph_node *node;
1329 struct cgraph_edge *edge;
1330 fibheap_t heap = fibheap_new ();
1331 bitmap updated_nodes = BITMAP_ALLOC (NULL);
1332 int min_size, max_size;
1333 VEC (cgraph_edge_p, heap) *new_indirect_edges = NULL;
1334 int initial_size = 0;
1336 if (flag_indirect_inlining)
1337 new_indirect_edges = VEC_alloc (cgraph_edge_p, heap, 8);
1339 if (dump_file)
1340 fprintf (dump_file,
1341 "\nDeciding on inlining of small functions. Starting with size %i.\n",
1342 initial_size);
1344 /* Compute overall unit size and other global parameters used by badness
1345 metrics. */
1347 max_count = 0;
1348 initialize_growth_caches ();
1350 FOR_EACH_DEFINED_FUNCTION (node)
1351 if (!node->global.inlined_to)
1353 if (cgraph_function_with_gimple_body_p (node)
1354 || node->thunk.thunk_p)
1356 struct inline_summary *info = inline_summary (node);
1358 if (!DECL_EXTERNAL (node->decl))
1359 initial_size += info->size;
1362 for (edge = node->callers; edge; edge = edge->next_caller)
1363 if (max_count < edge->count)
1364 max_count = edge->count;
1367 overall_size = initial_size;
1368 max_size = compute_max_insns (overall_size);
1369 min_size = overall_size;
1371 /* Populate the heeap with all edges we might inline. */
1373 FOR_EACH_DEFINED_FUNCTION (node)
1374 if (!node->global.inlined_to)
1376 if (dump_file)
1377 fprintf (dump_file, "Enqueueing calls of %s/%i.\n",
1378 cgraph_node_name (node), node->uid);
1380 for (edge = node->callers; edge; edge = edge->next_caller)
1381 if (edge->inline_failed
1382 && can_inline_edge_p (edge, true)
1383 && want_inline_small_function_p (edge, true)
1384 && edge->inline_failed)
1386 gcc_assert (!edge->aux);
1387 update_edge_key (heap, edge);
1391 gcc_assert (in_lto_p
1392 || !max_count
1393 || (profile_info && flag_branch_probabilities));
1395 while (!fibheap_empty (heap))
1397 int old_size = overall_size;
1398 struct cgraph_node *where, *callee;
1399 int badness = fibheap_min_key (heap);
1400 int current_badness;
1401 int cached_badness;
1402 int growth;
1404 edge = (struct cgraph_edge *) fibheap_extract_min (heap);
1405 gcc_assert (edge->aux);
1406 edge->aux = NULL;
1407 if (!edge->inline_failed)
1408 continue;
1410 /* Be sure that caches are maintained consistent.
1411 We can not make this ENABLE_CHECKING only because it cause differnt
1412 updates of the fibheap queue. */
1413 cached_badness = edge_badness (edge, false);
1414 reset_edge_growth_cache (edge);
1415 reset_node_growth_cache (edge->callee);
1417 /* When updating the edge costs, we only decrease badness in the keys.
1418 Increases of badness are handled lazilly; when we see key with out
1419 of date value on it, we re-insert it now. */
1420 current_badness = edge_badness (edge, false);
1421 gcc_assert (cached_badness == current_badness);
1422 gcc_assert (current_badness >= badness);
1423 if (current_badness != badness)
1425 edge->aux = fibheap_insert (heap, current_badness, edge);
1426 continue;
1429 if (!can_inline_edge_p (edge, true))
1430 continue;
1432 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
1433 growth = estimate_edge_growth (edge);
1434 if (dump_file)
1436 fprintf (dump_file,
1437 "\nConsidering %s with %i size\n",
1438 cgraph_node_name (callee),
1439 inline_summary (callee)->size);
1440 fprintf (dump_file,
1441 " to be inlined into %s in %s:%i\n"
1442 " Estimated growth after inlined into all is %+i insns.\n"
1443 " Estimated badness is %i, frequency %.2f.\n",
1444 cgraph_node_name (edge->caller),
1445 flag_wpa ? "unknown"
1446 : gimple_filename ((const_gimple) edge->call_stmt),
1447 flag_wpa ? -1
1448 : gimple_lineno ((const_gimple) edge->call_stmt),
1449 estimate_growth (callee),
1450 badness,
1451 edge->frequency / (double)CGRAPH_FREQ_BASE);
1452 if (edge->count)
1453 fprintf (dump_file," Called "HOST_WIDEST_INT_PRINT_DEC"x\n",
1454 edge->count);
1455 if (dump_flags & TDF_DETAILS)
1456 edge_badness (edge, true);
1459 if (overall_size + growth > max_size
1460 && !DECL_DISREGARD_INLINE_LIMITS (callee->decl))
1462 edge->inline_failed = CIF_INLINE_UNIT_GROWTH_LIMIT;
1463 report_inline_failed_reason (edge);
1464 continue;
1467 if (!want_inline_small_function_p (edge, true))
1468 continue;
1470 /* Heuristics for inlining small functions works poorly for
1471 recursive calls where we do efect similar to loop unrolling.
1472 When inliing such edge seems profitable, leave decision on
1473 specific inliner. */
1474 if (cgraph_edge_recursive_p (edge))
1476 where = edge->caller;
1477 if (where->global.inlined_to)
1478 where = where->global.inlined_to;
1479 if (!recursive_inlining (edge,
1480 flag_indirect_inlining
1481 ? &new_indirect_edges : NULL))
1483 edge->inline_failed = CIF_RECURSIVE_INLINING;
1484 continue;
1486 reset_edge_caches (where);
1487 /* Recursive inliner inlines all recursive calls of the function
1488 at once. Consequently we need to update all callee keys. */
1489 if (flag_indirect_inlining)
1490 add_new_edges_to_heap (heap, new_indirect_edges);
1491 update_all_callee_keys (heap, where, updated_nodes);
1493 else
1495 struct cgraph_node *outer_node = NULL;
1496 int depth = 0;
1498 /* Consider the case where self recursive function A is inlined into B.
1499 This is desired optimization in some cases, since it leads to effect
1500 similar of loop peeling and we might completely optimize out the
1501 recursive call. However we must be extra selective. */
1503 where = edge->caller;
1504 while (where->global.inlined_to)
1506 if (where->decl == callee->decl)
1507 outer_node = where, depth++;
1508 where = where->callers->caller;
1510 if (outer_node
1511 && !want_inline_self_recursive_call_p (edge, outer_node,
1512 true, depth))
1514 edge->inline_failed
1515 = (DECL_DISREGARD_INLINE_LIMITS (edge->callee->decl)
1516 ? CIF_RECURSIVE_INLINING : CIF_UNSPECIFIED);
1517 continue;
1519 else if (depth && dump_file)
1520 fprintf (dump_file, " Peeling recursion with depth %i\n", depth);
1522 gcc_checking_assert (!callee->global.inlined_to);
1523 inline_call (edge, true, &new_indirect_edges, &overall_size);
1524 if (flag_indirect_inlining)
1525 add_new_edges_to_heap (heap, new_indirect_edges);
1527 reset_edge_caches (edge->callee);
1528 reset_node_growth_cache (callee);
1530 /* We inlined last offline copy to the body. This might lead
1531 to callees of function having fewer call sites and thus they
1532 may need updating.
1534 FIXME: the callee size could also shrink because more information
1535 is propagated from caller. We don't track when this happen and
1536 thus we need to recompute everything all the time. Once this is
1537 solved, "|| 1" should go away. */
1538 if (callee->global.inlined_to || 1)
1539 update_all_callee_keys (heap, callee, updated_nodes);
1540 else
1541 update_callee_keys (heap, edge->callee, updated_nodes);
1543 where = edge->caller;
1544 if (where->global.inlined_to)
1545 where = where->global.inlined_to;
1547 /* Our profitability metric can depend on local properties
1548 such as number of inlinable calls and size of the function body.
1549 After inlining these properties might change for the function we
1550 inlined into (since it's body size changed) and for the functions
1551 called by function we inlined (since number of it inlinable callers
1552 might change). */
1553 update_caller_keys (heap, where, updated_nodes, NULL);
1555 /* We removed one call of the function we just inlined. If offline
1556 copy is still needed, be sure to update the keys. */
1557 if (callee != where && !callee->global.inlined_to)
1558 update_caller_keys (heap, callee, updated_nodes, NULL);
1559 bitmap_clear (updated_nodes);
1561 if (dump_file)
1563 fprintf (dump_file,
1564 " Inlined into %s which now has time %i and size %i,"
1565 "net change of %+i.\n",
1566 cgraph_node_name (edge->caller),
1567 inline_summary (edge->caller)->time,
1568 inline_summary (edge->caller)->size,
1569 overall_size - old_size);
1571 if (min_size > overall_size)
1573 min_size = overall_size;
1574 max_size = compute_max_insns (min_size);
1576 if (dump_file)
1577 fprintf (dump_file, "New minimal size reached: %i\n", min_size);
1581 free_growth_caches ();
1582 if (new_indirect_edges)
1583 VEC_free (cgraph_edge_p, heap, new_indirect_edges);
1584 fibheap_delete (heap);
1585 if (dump_file)
1586 fprintf (dump_file,
1587 "Unit growth for small function inlining: %i->%i (%i%%)\n",
1588 initial_size, overall_size,
1589 initial_size ? overall_size * 100 / (initial_size) - 100: 0);
1590 BITMAP_FREE (updated_nodes);
1593 /* Flatten NODE. Performed both during early inlining and
1594 at IPA inlining time. */
1596 static void
1597 flatten_function (struct cgraph_node *node, bool early)
1599 struct cgraph_edge *e;
1601 /* We shouldn't be called recursively when we are being processed. */
1602 gcc_assert (node->aux == NULL);
1604 node->aux = (void *) node;
1606 for (e = node->callees; e; e = e->next_callee)
1608 struct cgraph_node *orig_callee;
1609 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
1611 /* We've hit cycle? It is time to give up. */
1612 if (callee->aux)
1614 if (dump_file)
1615 fprintf (dump_file,
1616 "Not inlining %s into %s to avoid cycle.\n",
1617 cgraph_node_name (callee),
1618 cgraph_node_name (e->caller));
1619 e->inline_failed = CIF_RECURSIVE_INLINING;
1620 continue;
1623 /* When the edge is already inlined, we just need to recurse into
1624 it in order to fully flatten the leaves. */
1625 if (!e->inline_failed)
1627 flatten_function (callee, early);
1628 continue;
1631 /* Flatten attribute needs to be processed during late inlining. For
1632 extra code quality we however do flattening during early optimization,
1633 too. */
1634 if (!early
1635 ? !can_inline_edge_p (e, true)
1636 : !can_early_inline_edge_p (e))
1637 continue;
1639 if (cgraph_edge_recursive_p (e))
1641 if (dump_file)
1642 fprintf (dump_file, "Not inlining: recursive call.\n");
1643 continue;
1646 if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
1647 != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
1649 if (dump_file)
1650 fprintf (dump_file, "Not inlining: SSA form does not match.\n");
1651 continue;
1654 /* Inline the edge and flatten the inline clone. Avoid
1655 recursing through the original node if the node was cloned. */
1656 if (dump_file)
1657 fprintf (dump_file, " Inlining %s into %s.\n",
1658 cgraph_node_name (callee),
1659 cgraph_node_name (e->caller));
1660 orig_callee = callee;
1661 inline_call (e, true, NULL, NULL);
1662 if (e->callee != orig_callee)
1663 orig_callee->aux = (void *) node;
1664 flatten_function (e->callee, early);
1665 if (e->callee != orig_callee)
1666 orig_callee->aux = NULL;
1669 node->aux = NULL;
1672 /* Decide on the inlining. We do so in the topological order to avoid
1673 expenses on updating data structures. */
1675 static unsigned int
1676 ipa_inline (void)
1678 struct cgraph_node *node;
1679 int nnodes;
1680 struct cgraph_node **order =
1681 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1682 int i;
1684 if (in_lto_p && optimize)
1685 ipa_update_after_lto_read ();
1687 if (dump_file)
1688 dump_inline_summaries (dump_file);
1690 nnodes = ipa_reverse_postorder (order);
1692 for (node = cgraph_nodes; node; node = node->next)
1693 node->aux = 0;
1695 if (dump_file)
1696 fprintf (dump_file, "\nFlattening functions:\n");
1698 /* In the first pass handle functions to be flattened. Do this with
1699 a priority so none of our later choices will make this impossible. */
1700 for (i = nnodes - 1; i >= 0; i--)
1702 node = order[i];
1704 /* Handle nodes to be flattened.
1705 Ideally when processing callees we stop inlining at the
1706 entry of cycles, possibly cloning that entry point and
1707 try to flatten itself turning it into a self-recursive
1708 function. */
1709 if (lookup_attribute ("flatten",
1710 DECL_ATTRIBUTES (node->decl)) != NULL)
1712 if (dump_file)
1713 fprintf (dump_file,
1714 "Flattening %s\n", cgraph_node_name (node));
1715 flatten_function (node, false);
1719 inline_small_functions ();
1720 cgraph_remove_unreachable_nodes (true, dump_file);
1721 free (order);
1723 /* We already perform some inlining of functions called once during
1724 inlining small functions above. After unreachable nodes are removed,
1725 we still might do a quick check that nothing new is found. */
1726 if (flag_inline_functions_called_once)
1728 int cold;
1729 if (dump_file)
1730 fprintf (dump_file, "\nDeciding on functions called once:\n");
1732 /* Inlining one function called once has good chance of preventing
1733 inlining other function into the same callee. Ideally we should
1734 work in priority order, but probably inlining hot functions first
1735 is good cut without the extra pain of maintaining the queue.
1737 ??? this is not really fitting the bill perfectly: inlining function
1738 into callee often leads to better optimization of callee due to
1739 increased context for optimization.
1740 For example if main() function calls a function that outputs help
1741 and then function that does the main optmization, we should inline
1742 the second with priority even if both calls are cold by themselves.
1744 We probably want to implement new predicate replacing our use of
1745 maybe_hot_edge interpreted as maybe_hot_edge || callee is known
1746 to be hot. */
1747 for (cold = 0; cold <= 1; cold ++)
1749 for (node = cgraph_nodes; node; node = node->next)
1751 if (want_inline_function_called_once_p (node)
1752 && (cold
1753 || cgraph_maybe_hot_edge_p (node->callers)))
1755 struct cgraph_node *caller = node->callers->caller;
1757 if (dump_file)
1759 fprintf (dump_file,
1760 "\nInlining %s size %i.\n",
1761 cgraph_node_name (node), inline_summary (node)->size);
1762 fprintf (dump_file,
1763 " Called once from %s %i insns.\n",
1764 cgraph_node_name (node->callers->caller),
1765 inline_summary (node->callers->caller)->size);
1768 inline_call (node->callers, true, NULL, NULL);
1769 if (dump_file)
1770 fprintf (dump_file,
1771 " Inlined into %s which now has %i size\n",
1772 cgraph_node_name (caller),
1773 inline_summary (caller)->size);
1779 /* Free ipa-prop structures if they are no longer needed. */
1780 if (optimize)
1781 ipa_free_all_structures_after_iinln ();
1783 if (dump_file)
1784 fprintf (dump_file,
1785 "\nInlined %i calls, eliminated %i functions\n\n",
1786 ncalls_inlined, nfunctions_inlined);
1788 if (dump_file)
1789 dump_inline_summaries (dump_file);
1790 /* In WPA we use inline summaries for partitioning process. */
1791 if (!flag_wpa)
1792 inline_free_summary ();
1793 return 0;
1796 /* Inline always-inline function calls in NODE. */
1798 static bool
1799 inline_always_inline_functions (struct cgraph_node *node)
1801 struct cgraph_edge *e;
1802 bool inlined = false;
1804 for (e = node->callees; e; e = e->next_callee)
1806 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
1807 if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl))
1808 continue;
1810 if (cgraph_edge_recursive_p (e))
1812 if (dump_file)
1813 fprintf (dump_file, " Not inlining recursive call to %s.\n",
1814 cgraph_node_name (e->callee));
1815 e->inline_failed = CIF_RECURSIVE_INLINING;
1816 continue;
1819 if (!can_early_inline_edge_p (e))
1820 continue;
1822 if (dump_file)
1823 fprintf (dump_file, " Inlining %s into %s (always_inline).\n",
1824 cgraph_node_name (e->callee),
1825 cgraph_node_name (e->caller));
1826 inline_call (e, true, NULL, NULL);
1827 inlined = true;
1830 return inlined;
1833 /* Decide on the inlining. We do so in the topological order to avoid
1834 expenses on updating data structures. */
1836 static bool
1837 early_inline_small_functions (struct cgraph_node *node)
1839 struct cgraph_edge *e;
1840 bool inlined = false;
1842 for (e = node->callees; e; e = e->next_callee)
1844 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
1845 if (!inline_summary (callee)->inlinable
1846 || !e->inline_failed)
1847 continue;
1849 /* Do not consider functions not declared inline. */
1850 if (!DECL_DECLARED_INLINE_P (callee->decl)
1851 && !flag_inline_small_functions
1852 && !flag_inline_functions)
1853 continue;
1855 if (dump_file)
1856 fprintf (dump_file, "Considering inline candidate %s.\n",
1857 cgraph_node_name (callee));
1859 if (!can_early_inline_edge_p (e))
1860 continue;
1862 if (cgraph_edge_recursive_p (e))
1864 if (dump_file)
1865 fprintf (dump_file, " Not inlining: recursive call.\n");
1866 continue;
1869 if (!want_early_inline_function_p (e))
1870 continue;
1872 if (dump_file)
1873 fprintf (dump_file, " Inlining %s into %s.\n",
1874 cgraph_node_name (callee),
1875 cgraph_node_name (e->caller));
1876 inline_call (e, true, NULL, NULL);
1877 inlined = true;
1880 return inlined;
1883 /* Do inlining of small functions. Doing so early helps profiling and other
1884 passes to be somewhat more effective and avoids some code duplication in
1885 later real inlining pass for testcases with very many function calls. */
1886 static unsigned int
1887 early_inliner (void)
1889 struct cgraph_node *node = cgraph_get_node (current_function_decl);
1890 struct cgraph_edge *edge;
1891 unsigned int todo = 0;
1892 int iterations = 0;
1893 bool inlined = false;
1895 if (seen_error ())
1896 return 0;
1898 /* Do nothing if datastructures for ipa-inliner are already computed. This
1899 happens when some pass decides to construct new function and
1900 cgraph_add_new_function calls lowering passes and early optimization on
1901 it. This may confuse ourself when early inliner decide to inline call to
1902 function clone, because function clones don't have parameter list in
1903 ipa-prop matching their signature. */
1904 if (ipa_node_params_vector)
1905 return 0;
1907 #ifdef ENABLE_CHECKING
1908 verify_cgraph_node (node);
1909 #endif
1911 /* Even when not optimizing or not inlining inline always-inline
1912 functions. */
1913 inlined = inline_always_inline_functions (node);
1915 if (!optimize
1916 || flag_no_inline
1917 || !flag_early_inlining
1918 /* Never inline regular functions into always-inline functions
1919 during incremental inlining. This sucks as functions calling
1920 always inline functions will get less optimized, but at the
1921 same time inlining of functions calling always inline
1922 function into an always inline function might introduce
1923 cycles of edges to be always inlined in the callgraph.
1925 We might want to be smarter and just avoid this type of inlining. */
1926 || DECL_DISREGARD_INLINE_LIMITS (node->decl))
1928 else if (lookup_attribute ("flatten",
1929 DECL_ATTRIBUTES (node->decl)) != NULL)
1931 /* When the function is marked to be flattened, recursively inline
1932 all calls in it. */
1933 if (dump_file)
1934 fprintf (dump_file,
1935 "Flattening %s\n", cgraph_node_name (node));
1936 flatten_function (node, true);
1937 inlined = true;
1939 else
1941 /* We iterate incremental inlining to get trivial cases of indirect
1942 inlining. */
1943 while (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS)
1944 && early_inline_small_functions (node))
1946 timevar_push (TV_INTEGRATION);
1947 todo |= optimize_inline_calls (current_function_decl);
1949 /* Technically we ought to recompute inline parameters so the new
1950 iteration of early inliner works as expected. We however have
1951 values approximately right and thus we only need to update edge
1952 info that might be cleared out for newly discovered edges. */
1953 for (edge = node->callees; edge; edge = edge->next_callee)
1955 struct inline_edge_summary *es = inline_edge_summary (edge);
1956 es->call_stmt_size
1957 = estimate_num_insns (edge->call_stmt, &eni_size_weights);
1958 es->call_stmt_time
1959 = estimate_num_insns (edge->call_stmt, &eni_time_weights);
1960 edge->call_stmt_cannot_inline_p
1961 = gimple_call_cannot_inline_p (edge->call_stmt);
1963 timevar_pop (TV_INTEGRATION);
1964 iterations++;
1965 inlined = false;
1967 if (dump_file)
1968 fprintf (dump_file, "Iterations: %i\n", iterations);
1971 if (inlined)
1973 timevar_push (TV_INTEGRATION);
1974 todo |= optimize_inline_calls (current_function_decl);
1975 timevar_pop (TV_INTEGRATION);
1978 cfun->always_inline_functions_inlined = true;
1980 return todo;
1983 struct gimple_opt_pass pass_early_inline =
1986 GIMPLE_PASS,
1987 "einline", /* name */
1988 NULL, /* gate */
1989 early_inliner, /* execute */
1990 NULL, /* sub */
1991 NULL, /* next */
1992 0, /* static_pass_number */
1993 TV_INLINE_HEURISTICS, /* tv_id */
1994 PROP_ssa, /* properties_required */
1995 0, /* properties_provided */
1996 0, /* properties_destroyed */
1997 0, /* todo_flags_start */
1998 0 /* todo_flags_finish */
2003 /* When to run IPA inlining. Inlining of always-inline functions
2004 happens during early inlining.
2006 Enable inlining unconditoinally at -flto. We need size estimates to
2007 drive partitioning. */
2009 static bool
2010 gate_ipa_inline (void)
2012 return optimize || flag_lto || flag_wpa;
2015 struct ipa_opt_pass_d pass_ipa_inline =
2018 IPA_PASS,
2019 "inline", /* name */
2020 gate_ipa_inline, /* gate */
2021 ipa_inline, /* execute */
2022 NULL, /* sub */
2023 NULL, /* next */
2024 0, /* static_pass_number */
2025 TV_INLINE_HEURISTICS, /* tv_id */
2026 0, /* properties_required */
2027 0, /* properties_provided */
2028 0, /* properties_destroyed */
2029 TODO_remove_functions, /* todo_flags_finish */
2030 TODO_dump_cgraph
2031 | TODO_remove_functions | TODO_ggc_collect /* todo_flags_finish */
2033 inline_generate_summary, /* generate_summary */
2034 inline_write_summary, /* write_summary */
2035 inline_read_summary, /* read_summary */
2036 NULL, /* write_optimization_summary */
2037 NULL, /* read_optimization_summary */
2038 NULL, /* stmt_fixup */
2039 0, /* TODOs */
2040 inline_transform, /* function_transform */
2041 NULL, /* variable_transform */