1 /* Inlining decision heuristics.
2 Copyright (C) 2003-2024 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
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
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
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
38 The inliner itself is split into two passes:
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
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
62 Because of lack of whole unit knowledge, the pass cannot 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.
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. */
94 #include "coretypes.h"
100 #include "alloc-pool.h"
101 #include "tree-pass.h"
102 #include "gimple-ssa.h"
104 #include "lto-streamer.h"
105 #include "trans-mem.h"
107 #include "tree-inline.h"
109 #include "symbol-summary.h"
110 #include "tree-vrp.h"
113 #include "ipa-prop.h"
114 #include "ipa-fnsummary.h"
115 #include "ipa-inline.h"
116 #include "ipa-utils.h"
117 #include "auto-profile.h"
118 #include "builtins.h"
119 #include "fibonacci_heap.h"
120 #include "stringpool.h"
123 #include "ipa-strub.h"
125 /* Inliner uses greedy algorithm to inline calls in a priority order.
126 Badness is used as the key in a Fibonacci heap which roughly corresponds
127 to negation of benefit to cost ratios.
128 In case multiple calls has same priority we want to stabilize the outcomes
129 for which we use ids. */
136 : badness (sreal::min ()), uid (0)
139 inline_badness (cgraph_edge
*e
, sreal b
)
140 : badness (b
), uid (e
->get_uid ())
143 bool operator<= (const inline_badness
&other
)
145 if (badness
!= other
.badness
)
146 return badness
<= other
.badness
;
147 return uid
<= other
.uid
;
149 bool operator== (const inline_badness
&other
)
151 return badness
== other
.badness
&& uid
== other
.uid
;
153 bool operator!= (const inline_badness
&other
)
155 return badness
!= other
.badness
|| uid
!= other
.uid
;
157 bool operator< (const inline_badness
&other
)
159 if (badness
!= other
.badness
)
160 return badness
< other
.badness
;
161 return uid
< other
.uid
;
163 bool operator> (const inline_badness
&other
)
165 if (badness
!= other
.badness
)
166 return badness
> other
.badness
;
167 return uid
> other
.uid
;
171 typedef fibonacci_heap
<inline_badness
, cgraph_edge
> edge_heap_t
;
172 typedef fibonacci_node
<inline_badness
, cgraph_edge
> edge_heap_node_t
;
174 /* Statistics we collect about inlining algorithm. */
175 static int overall_size
;
176 static profile_count max_count
;
177 static profile_count spec_rem
;
179 /* Return false when inlining edge E would lead to violating
180 limits on function unit growth or stack usage growth.
182 The relative function body growth limit is present generally
183 to avoid problems with non-linear behavior of the compiler.
184 To allow inlining huge functions into tiny wrapper, the limit
185 is always based on the bigger of the two functions considered.
187 For stack growth limits we always base the growth in stack usage
188 of the callers. We want to prevent applications from segfaulting
189 on stack overflow when functions with huge stack frames gets
193 caller_growth_limits (struct cgraph_edge
*e
)
195 struct cgraph_node
*to
= e
->caller
;
196 struct cgraph_node
*what
= e
->callee
->ultimate_alias_target ();
199 HOST_WIDE_INT stack_size_limit
= 0, inlined_stack
;
200 ipa_size_summary
*outer_info
= ipa_size_summaries
->get (to
);
202 /* Look for function e->caller is inlined to. While doing
203 so work out the largest function body on the way. As
204 described above, we want to base our function growth
205 limits based on that. Not on the self size of the
206 outer function, not on the self size of inline code
207 we immediately inline to. This is the most relaxed
208 interpretation of the rule "do not grow large functions
209 too much in order to prevent compiler from exploding". */
212 ipa_size_summary
*size_info
= ipa_size_summaries
->get (to
);
213 if (limit
< size_info
->self_size
)
214 limit
= size_info
->self_size
;
215 if (stack_size_limit
< size_info
->estimated_self_stack_size
)
216 stack_size_limit
= size_info
->estimated_self_stack_size
;
218 to
= to
->callers
->caller
;
223 ipa_fn_summary
*what_info
= ipa_fn_summaries
->get (what
);
224 ipa_size_summary
*what_size_info
= ipa_size_summaries
->get (what
);
226 if (limit
< what_size_info
->self_size
)
227 limit
= what_size_info
->self_size
;
229 limit
+= limit
* opt_for_fn (to
->decl
, param_large_function_growth
) / 100;
231 /* Check the size after inlining against the function limits. But allow
232 the function to shrink if it went over the limits by forced inlining. */
233 newsize
= estimate_size_after_inlining (to
, e
);
234 if (newsize
>= ipa_size_summaries
->get (what
)->size
235 && newsize
> opt_for_fn (to
->decl
, param_large_function_insns
)
238 e
->inline_failed
= CIF_LARGE_FUNCTION_GROWTH_LIMIT
;
242 if (!what_info
->estimated_stack_size
)
245 /* FIXME: Stack size limit often prevents inlining in Fortran programs
246 due to large i/o datastructures used by the Fortran front-end.
247 We ought to ignore this limit when we know that the edge is executed
248 on every invocation of the caller (i.e. its call statement dominates
249 exit block). We do not track this information, yet. */
250 stack_size_limit
+= ((gcov_type
)stack_size_limit
251 * opt_for_fn (to
->decl
, param_stack_frame_growth
)
254 inlined_stack
= (ipa_get_stack_frame_offset (to
)
255 + outer_info
->estimated_self_stack_size
256 + what_info
->estimated_stack_size
);
257 /* Check new stack consumption with stack consumption at the place
259 if (inlined_stack
> stack_size_limit
260 /* If function already has large stack usage from sibling
261 inline call, we can inline, too.
262 This bit overoptimistically assume that we are good at stack
264 && inlined_stack
> ipa_fn_summaries
->get (to
)->estimated_stack_size
265 && inlined_stack
> opt_for_fn (to
->decl
, param_large_stack_frame
))
267 e
->inline_failed
= CIF_LARGE_STACK_FRAME_GROWTH_LIMIT
;
273 /* Dump info about why inlining has failed. */
276 report_inline_failed_reason (struct cgraph_edge
*e
)
278 if (dump_enabled_p ())
280 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, e
->call_stmt
,
281 " not inlinable: %C -> %C, %s\n",
282 e
->caller
, e
->callee
,
283 cgraph_inline_failed_string (e
->inline_failed
));
284 if ((e
->inline_failed
== CIF_TARGET_OPTION_MISMATCH
285 || e
->inline_failed
== CIF_OPTIMIZATION_MISMATCH
)
286 && e
->caller
->lto_file_data
287 && e
->callee
->ultimate_alias_target ()->lto_file_data
)
289 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, e
->call_stmt
,
290 " LTO objects: %s, %s\n",
291 e
->caller
->lto_file_data
->file_name
,
292 e
->callee
->ultimate_alias_target ()->lto_file_data
->file_name
);
294 if (e
->inline_failed
== CIF_TARGET_OPTION_MISMATCH
)
296 cl_target_option_print_diff
297 (dump_file
, 2, target_opts_for_fn (e
->caller
->decl
),
298 target_opts_for_fn (e
->callee
->ultimate_alias_target ()->decl
));
299 if (e
->inline_failed
== CIF_OPTIMIZATION_MISMATCH
)
301 cl_optimization_print_diff
302 (dump_file
, 2, opts_for_fn (e
->caller
->decl
),
303 opts_for_fn (e
->callee
->ultimate_alias_target ()->decl
));
307 /* Decide whether sanitizer-related attributes allow inlining. */
310 sanitize_attrs_match_for_inline_p (const_tree caller
, const_tree callee
)
312 if (!caller
|| !callee
)
315 /* Follow clang and allow inlining for always_inline functions. */
316 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (callee
)))
319 const sanitize_code codes
[] =
324 SANITIZE_UNDEFINED_NONDEFAULT
,
325 SANITIZE_POINTER_COMPARE
,
326 SANITIZE_POINTER_SUBTRACT
329 for (unsigned i
= 0; i
< ARRAY_SIZE (codes
); i
++)
330 if (sanitize_flags_p (codes
[i
], caller
)
331 != sanitize_flags_p (codes
[i
], callee
))
334 if (sanitize_coverage_p (caller
) != sanitize_coverage_p (callee
))
340 /* Used for flags where it is safe to inline when caller's value is
341 grater than callee's. */
342 #define check_maybe_up(flag) \
343 (opts_for_fn (caller->decl)->x_##flag \
344 != opts_for_fn (callee->decl)->x_##flag \
346 || opts_for_fn (caller->decl)->x_##flag \
347 < opts_for_fn (callee->decl)->x_##flag))
348 /* Used for flags where it is safe to inline when caller's value is
349 smaller than callee's. */
350 #define check_maybe_down(flag) \
351 (opts_for_fn (caller->decl)->x_##flag \
352 != opts_for_fn (callee->decl)->x_##flag \
354 || opts_for_fn (caller->decl)->x_##flag \
355 > opts_for_fn (callee->decl)->x_##flag))
356 /* Used for flags where exact match is needed for correctness. */
357 #define check_match(flag) \
358 (opts_for_fn (caller->decl)->x_##flag \
359 != opts_for_fn (callee->decl)->x_##flag)
361 /* Decide if we can inline the edge and possibly update
362 inline_failed reason.
363 We check whether inlining is possible at all and whether
364 caller growth limits allow doing so.
366 if REPORT is true, output reason to the dump file. */
369 can_inline_edge_p (struct cgraph_edge
*e
, bool report
,
372 gcc_checking_assert (e
->inline_failed
);
374 if (cgraph_inline_failed_type (e
->inline_failed
) == CIF_FINAL_ERROR
)
377 report_inline_failed_reason (e
);
381 bool inlinable
= true;
382 enum availability avail
;
383 cgraph_node
*caller
= (e
->caller
->inlined_to
384 ? e
->caller
->inlined_to
: e
->caller
);
385 cgraph_node
*callee
= e
->callee
->ultimate_alias_target (&avail
, caller
);
387 if (!callee
->definition
)
389 e
->inline_failed
= CIF_BODY_NOT_AVAILABLE
;
392 if (!early
&& (!opt_for_fn (callee
->decl
, optimize
)
393 || !opt_for_fn (caller
->decl
, optimize
)))
395 e
->inline_failed
= CIF_FUNCTION_NOT_OPTIMIZED
;
398 else if (callee
->calls_comdat_local
)
400 e
->inline_failed
= CIF_USES_COMDAT_LOCAL
;
403 else if (avail
<= AVAIL_INTERPOSABLE
)
405 e
->inline_failed
= CIF_OVERWRITABLE
;
408 /* All edges with call_stmt_cannot_inline_p should have inline_failed
409 initialized to one of FINAL_ERROR reasons. */
410 else if (e
->call_stmt_cannot_inline_p
)
412 /* Don't inline if the functions have different EH personalities. */
413 else if (DECL_FUNCTION_PERSONALITY (caller
->decl
)
414 && DECL_FUNCTION_PERSONALITY (callee
->decl
)
415 && (DECL_FUNCTION_PERSONALITY (caller
->decl
)
416 != DECL_FUNCTION_PERSONALITY (callee
->decl
)))
418 e
->inline_failed
= CIF_EH_PERSONALITY
;
421 /* TM pure functions should not be inlined into non-TM_pure
423 else if (is_tm_pure (callee
->decl
) && !is_tm_pure (caller
->decl
))
425 e
->inline_failed
= CIF_UNSPECIFIED
;
428 /* Check compatibility of target optimization options. */
429 else if (!targetm
.target_option
.can_inline_p (caller
->decl
,
432 e
->inline_failed
= CIF_TARGET_OPTION_MISMATCH
;
435 else if (ipa_fn_summaries
->get (callee
) == NULL
436 || !ipa_fn_summaries
->get (callee
)->inlinable
)
438 e
->inline_failed
= CIF_FUNCTION_NOT_INLINABLE
;
441 /* Don't inline a function with mismatched sanitization attributes. */
442 else if (!sanitize_attrs_match_for_inline_p (caller
->decl
, callee
->decl
))
444 e
->inline_failed
= CIF_SANITIZE_ATTRIBUTE_MISMATCH
;
448 if (inlinable
&& !strub_inlinable_to_p (callee
, caller
))
450 e
->inline_failed
= CIF_UNSPECIFIED
;
453 if (!inlinable
&& report
)
454 report_inline_failed_reason (e
);
458 /* Return inlining_insns_single limit for function N. If HINT or HINT2 is true
459 scale up the bound. */
462 inline_insns_single (cgraph_node
*n
, bool hint
, bool hint2
)
466 int64_t spd
= opt_for_fn (n
->decl
, param_inline_heuristics_hint_percent
);
470 return opt_for_fn (n
->decl
, param_max_inline_insns_single
) * spd
/ 100;
473 return opt_for_fn (n
->decl
, param_max_inline_insns_single
)
474 * opt_for_fn (n
->decl
, param_inline_heuristics_hint_percent
) / 100;
475 return opt_for_fn (n
->decl
, param_max_inline_insns_single
);
478 /* Return inlining_insns_auto limit for function N. If HINT or HINT2 is true
479 scale up the bound. */
482 inline_insns_auto (cgraph_node
*n
, bool hint
, bool hint2
)
484 int max_inline_insns_auto
= opt_for_fn (n
->decl
, param_max_inline_insns_auto
);
487 int64_t spd
= opt_for_fn (n
->decl
, param_inline_heuristics_hint_percent
);
491 return max_inline_insns_auto
* spd
/ 100;
494 return max_inline_insns_auto
495 * opt_for_fn (n
->decl
, param_inline_heuristics_hint_percent
) / 100;
496 return max_inline_insns_auto
;
499 enum can_inline_edge_by_limits_flags
501 /* True if we are early inlining. */
502 CAN_INLINE_EARLY
= 1,
503 /* Ignore size limits. */
504 CAN_INLINE_DISREGARD_LIMITS
= 2,
505 /* Force size limits (ignore always_inline). This is used for
506 recrusive inlining where always_inline may lead to inline bombs
507 and technically it is non-sential anyway. */
508 CAN_INLINE_FORCE_LIMITS
= 4,
509 /* Report decision to dump file. */
510 CAN_INLINE_REPORT
= 8,
513 /* Decide if we can inline the edge and possibly update
514 inline_failed reason.
515 We check whether inlining is possible at all and whether
516 caller growth limits allow doing so. */
519 can_inline_edge_by_limits_p (struct cgraph_edge
*e
, int flags
)
521 gcc_checking_assert (e
->inline_failed
);
523 if (cgraph_inline_failed_type (e
->inline_failed
) == CIF_FINAL_ERROR
)
525 if (flags
& CAN_INLINE_REPORT
)
526 report_inline_failed_reason (e
);
530 bool inlinable
= true;
531 enum availability avail
;
532 cgraph_node
*caller
= (e
->caller
->inlined_to
533 ? e
->caller
->inlined_to
: e
->caller
);
534 cgraph_node
*callee
= e
->callee
->ultimate_alias_target (&avail
, caller
);
535 tree caller_tree
= DECL_FUNCTION_SPECIFIC_OPTIMIZATION (caller
->decl
);
537 = callee
? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (callee
->decl
) : NULL
;
538 /* Check if caller growth allows the inlining. */
539 if (!(flags
& CAN_INLINE_DISREGARD_LIMITS
)
540 && ((flags
& CAN_INLINE_FORCE_LIMITS
)
541 || (!DECL_DISREGARD_INLINE_LIMITS (callee
->decl
)
542 && !lookup_attribute ("flatten",
543 DECL_ATTRIBUTES (caller
->decl
))))
544 && !caller_growth_limits (e
))
546 else if (callee
->externally_visible
547 && !DECL_DISREGARD_INLINE_LIMITS (callee
->decl
)
548 && flag_live_patching
== LIVE_PATCHING_INLINE_ONLY_STATIC
)
550 e
->inline_failed
= CIF_EXTERN_LIVE_ONLY_STATIC
;
553 /* Don't inline a function with a higher optimization level than the
554 caller. FIXME: this is really just tip of iceberg of handling
555 optimization attribute. */
556 else if (caller_tree
!= callee_tree
)
559 (DECL_DISREGARD_INLINE_LIMITS (callee
->decl
)
560 && lookup_attribute ("always_inline",
561 DECL_ATTRIBUTES (callee
->decl
)));
562 ipa_fn_summary
*caller_info
= ipa_fn_summaries
->get (caller
);
563 ipa_fn_summary
*callee_info
= ipa_fn_summaries
->get (callee
);
565 /* Until GCC 4.9 we did not check the semantics-altering flags
566 below and inlined across optimization boundaries.
567 Enabling checks below breaks several packages by refusing
568 to inline library always_inline functions. See PR65873.
569 Disable the check for early inlining for now until better solution
571 if (always_inline
&& (flags
& CAN_INLINE_EARLY
))
573 /* There are some options that change IL semantics which means
574 we cannot inline in these cases for correctness reason.
575 Not even for always_inline declared functions. */
576 else if (check_match (flag_wrapv
)
577 || check_match (flag_trapv
)
578 || check_match (flag_pcc_struct_return
)
579 || check_maybe_down (optimize_debug
)
580 /* When caller or callee does FP math, be sure FP codegen flags
582 || ((caller_info
->fp_expressions
&& callee_info
->fp_expressions
)
583 && (check_maybe_up (flag_rounding_math
)
584 || check_maybe_up (flag_trapping_math
)
585 || check_maybe_down (flag_unsafe_math_optimizations
)
586 || check_maybe_down (flag_finite_math_only
)
587 || check_maybe_up (flag_signaling_nans
)
588 || check_maybe_down (flag_cx_limited_range
)
589 || check_maybe_up (flag_signed_zeros
)
590 || check_maybe_down (flag_associative_math
)
591 || check_maybe_down (flag_reciprocal_math
)
592 || check_maybe_down (flag_fp_int_builtin_inexact
)
593 /* Strictly speaking only when the callee contains function
594 calls that may end up setting errno. */
595 || check_maybe_up (flag_errno_math
)))
596 /* We do not want to make code compiled with exceptions to be
597 brought into a non-EH function unless we know that the callee
599 This is tracked by DECL_FUNCTION_PERSONALITY. */
600 || (check_maybe_up (flag_non_call_exceptions
)
601 && DECL_FUNCTION_PERSONALITY (callee
->decl
))
602 || (check_maybe_up (flag_exceptions
)
603 && DECL_FUNCTION_PERSONALITY (callee
->decl
))
604 /* When devirtualization is disabled for callee, it is not safe
605 to inline it as we possibly mangled the type info.
606 Allow early inlining of always inlines. */
607 || (!(flags
& CAN_INLINE_EARLY
) && check_maybe_down (flag_devirtualize
)))
609 e
->inline_failed
= CIF_OPTIMIZATION_MISMATCH
;
612 /* gcc.dg/pr43564.c. Apply user-forced inline even at -O0. */
613 else if (always_inline
)
615 /* When user added an attribute to the callee honor it. */
616 else if (lookup_attribute ("optimize", DECL_ATTRIBUTES (callee
->decl
))
617 && opts_for_fn (caller
->decl
) != opts_for_fn (callee
->decl
))
619 e
->inline_failed
= CIF_OPTIMIZATION_MISMATCH
;
622 /* If explicit optimize attribute are not used, the mismatch is caused
623 by different command line options used to build different units.
624 Do not care about COMDAT functions - those are intended to be
625 optimized with the optimization flags of module they are used in.
626 Also do not care about mixing up size/speed optimization when
627 DECL_DISREGARD_INLINE_LIMITS is set. */
628 else if ((callee
->merged_comdat
629 && !lookup_attribute ("optimize",
630 DECL_ATTRIBUTES (caller
->decl
)))
631 || DECL_DISREGARD_INLINE_LIMITS (callee
->decl
))
633 /* If mismatch is caused by merging two LTO units with different
634 optimization flags we want to be bit nicer. However never inline
635 if one of functions is not optimized at all. */
636 else if (!opt_for_fn (callee
->decl
, optimize
)
637 || !opt_for_fn (caller
->decl
, optimize
))
639 e
->inline_failed
= CIF_OPTIMIZATION_MISMATCH
;
642 /* If callee is optimized for size and caller is not, allow inlining if
643 code shrinks or we are in param_max_inline_insns_single limit and
644 callee is inline (and thus likely an unified comdat).
645 This will allow caller to run faster. */
646 else if (opt_for_fn (callee
->decl
, optimize_size
)
647 > opt_for_fn (caller
->decl
, optimize_size
))
649 int growth
= estimate_edge_growth (e
);
650 if (growth
> opt_for_fn (caller
->decl
, param_max_inline_insns_size
)
651 && (!DECL_DECLARED_INLINE_P (callee
->decl
)
652 && growth
>= MAX (inline_insns_single (caller
, false, false),
653 inline_insns_auto (caller
, false, false))))
655 e
->inline_failed
= CIF_OPTIMIZATION_MISMATCH
;
659 /* If callee is more aggressively optimized for performance than caller,
660 we generally want to inline only cheap (runtime wise) functions. */
661 else if (opt_for_fn (callee
->decl
, optimize_size
)
662 < opt_for_fn (caller
->decl
, optimize_size
)
663 || (opt_for_fn (callee
->decl
, optimize
)
664 > opt_for_fn (caller
->decl
, optimize
)))
666 if (estimate_edge_time (e
)
667 >= 20 + ipa_call_summaries
->get (e
)->call_stmt_time
)
669 e
->inline_failed
= CIF_OPTIMIZATION_MISMATCH
;
676 if (!inlinable
&& (flags
& CAN_INLINE_REPORT
))
677 report_inline_failed_reason (e
);
682 /* Return true if the edge E is inlinable during early inlining. */
685 can_early_inline_edge_p (struct cgraph_edge
*e
)
687 cgraph_node
*caller
= (e
->caller
->inlined_to
688 ? e
->caller
->inlined_to
: e
->caller
);
689 struct cgraph_node
*callee
= e
->callee
->ultimate_alias_target ();
690 /* Early inliner might get called at WPA stage when IPA pass adds new
691 function. In this case we cannot really do any of early inlining
692 because function bodies are missing. */
693 if (cgraph_inline_failed_type (e
->inline_failed
) == CIF_FINAL_ERROR
)
695 if (!gimple_has_body_p (callee
->decl
))
697 e
->inline_failed
= CIF_BODY_NOT_AVAILABLE
;
700 gcc_assert (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e
->caller
->decl
))
701 && gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee
->decl
)));
702 if ((profile_arc_flag
|| condition_coverage_flag
)
703 && ((lookup_attribute ("no_profile_instrument_function",
704 DECL_ATTRIBUTES (caller
->decl
)) == NULL_TREE
)
705 != (lookup_attribute ("no_profile_instrument_function",
706 DECL_ATTRIBUTES (callee
->decl
)) == NULL_TREE
)))
709 if (!can_inline_edge_p (e
, true, true)
710 || !can_inline_edge_by_limits_p (e
, CAN_INLINE_EARLY
| CAN_INLINE_REPORT
))
712 /* When inlining regular functions into always-inline functions
713 during early inlining watch for possible inline cycles. */
714 if (DECL_DISREGARD_INLINE_LIMITS (caller
->decl
)
715 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (caller
->decl
))
716 && (!DECL_DISREGARD_INLINE_LIMITS (callee
->decl
)
717 || !lookup_attribute ("always_inline", DECL_ATTRIBUTES (callee
->decl
))))
719 /* If there are indirect calls, inlining may produce direct call.
720 TODO: We may lift this restriction if we avoid errors on formely
721 indirect calls to always_inline functions. Taking address
722 of always_inline function is generally bad idea and should
723 have been declared as undefined, but sadly we allow this. */
724 if (caller
->indirect_calls
|| e
->callee
->indirect_calls
)
726 ipa_fn_summary
*callee_info
= ipa_fn_summaries
->get (callee
);
727 if (callee_info
->safe_to_inline_to_always_inline
)
728 return callee_info
->safe_to_inline_to_always_inline
- 1;
729 for (cgraph_edge
*e2
= callee
->callees
; e2
; e2
= e2
->next_callee
)
731 struct cgraph_node
*callee2
= e2
->callee
->ultimate_alias_target ();
732 /* As early inliner runs in RPO order, we will see uninlined
733 always_inline calls only in the case of cyclic graphs. */
734 if (DECL_DISREGARD_INLINE_LIMITS (callee2
->decl
)
735 || lookup_attribute ("always_inline", DECL_ATTRIBUTES (callee2
->decl
)))
737 callee_info
->safe_to_inline_to_always_inline
= 1;
740 /* With LTO watch for case where function is later replaced
741 by always_inline definition.
742 TODO: We may either stop treating noninlined cross-module always
743 inlines as errors, or we can extend decl merging to produce
744 syntacic alias and honor always inline only in units it has
745 been declared as such. */
746 if (flag_lto
&& callee2
->externally_visible
)
748 callee_info
->safe_to_inline_to_always_inline
= 1;
752 callee_info
->safe_to_inline_to_always_inline
= 2;
758 /* Return number of calls in N. Ignore cheap builtins. */
761 num_calls (struct cgraph_node
*n
)
763 struct cgraph_edge
*e
;
766 for (e
= n
->callees
; e
; e
= e
->next_callee
)
767 if (!is_inexpensive_builtin (e
->callee
->decl
))
773 /* Return true if we are interested in inlining small function. */
776 want_early_inline_function_p (struct cgraph_edge
*e
)
778 bool want_inline
= true;
779 struct cgraph_node
*callee
= e
->callee
->ultimate_alias_target ();
781 if (DECL_DISREGARD_INLINE_LIMITS (callee
->decl
))
783 /* For AutoFDO, we need to make sure that before profile summary, all
784 hot paths' IR look exactly the same as profiled binary. As a result,
785 in einliner, we will disregard size limit and inline those callsites
787 * inlined in the profiled binary, and
788 * the cloned callee has enough samples to be considered "hot". */
789 else if (flag_auto_profile
&& afdo_callsite_hot_enough_for_early_inline (e
))
791 else if (!DECL_DECLARED_INLINE_P (callee
->decl
)
792 && !opt_for_fn (e
->caller
->decl
, flag_inline_small_functions
))
794 e
->inline_failed
= CIF_FUNCTION_NOT_INLINE_CANDIDATE
;
795 report_inline_failed_reason (e
);
800 /* First take care of very large functions. */
801 int min_growth
= estimate_min_edge_growth (e
), growth
= 0;
803 int early_inlining_insns
= param_early_inlining_insns
;
805 if (min_growth
> early_inlining_insns
)
807 if (dump_enabled_p ())
808 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, e
->call_stmt
,
809 " will not early inline: %C->%C, "
810 "call is cold and code would grow "
817 growth
= estimate_edge_growth (e
);
820 if (!want_inline
|| growth
<= param_max_inline_insns_size
)
822 else if (!e
->maybe_hot_p ())
824 if (dump_enabled_p ())
825 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, e
->call_stmt
,
826 " will not early inline: %C->%C, "
827 "call is cold and code would grow by %i\n",
832 else if (growth
> early_inlining_insns
)
834 if (dump_enabled_p ())
835 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, e
->call_stmt
,
836 " will not early inline: %C->%C, "
837 "growth %i exceeds --param early-inlining-insns\n",
838 e
->caller
, callee
, growth
);
841 else if ((n
= num_calls (callee
)) != 0
842 && growth
* (n
+ 1) > early_inlining_insns
)
844 if (dump_enabled_p ())
845 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, e
->call_stmt
,
846 " will not early inline: %C->%C, "
847 "growth %i exceeds --param early-inlining-insns "
848 "divided by number of calls\n",
849 e
->caller
, callee
, growth
);
856 /* Compute time of the edge->caller + edge->callee execution when inlining
860 compute_uninlined_call_time (struct cgraph_edge
*edge
,
861 sreal uninlined_call_time
,
864 cgraph_node
*caller
= (edge
->caller
->inlined_to
865 ? edge
->caller
->inlined_to
869 uninlined_call_time
*= freq
;
871 uninlined_call_time
= uninlined_call_time
>> 11;
873 sreal caller_time
= ipa_fn_summaries
->get (caller
)->time
;
874 return uninlined_call_time
+ caller_time
;
877 /* Same as compute_uinlined_call_time but compute time when inlining
881 compute_inlined_call_time (struct cgraph_edge
*edge
,
885 cgraph_node
*caller
= (edge
->caller
->inlined_to
886 ? edge
->caller
->inlined_to
888 sreal caller_time
= ipa_fn_summaries
->get (caller
)->time
;
895 /* This calculation should match one in ipa-inline-analysis.cc
896 (estimate_edge_size_and_time). */
897 time
-= (sreal
)ipa_call_summaries
->get (edge
)->call_stmt_time
* freq
;
900 time
= ((sreal
) 1) >> 8;
901 gcc_checking_assert (time
>= 0);
905 /* Determine time saved by inlining EDGE of frequency FREQ
906 where callee's runtime w/o inlining is UNINLINED_TYPE
907 and with inlined is INLINED_TYPE. */
910 inlining_speedup (struct cgraph_edge
*edge
,
912 sreal uninlined_time
,
915 sreal speedup
= uninlined_time
- inlined_time
;
916 /* Handling of call_time should match one in ipa-inline-fnsummary.c
917 (estimate_edge_size_and_time). */
918 sreal call_time
= ipa_call_summaries
->get (edge
)->call_stmt_time
;
922 speedup
= (speedup
+ call_time
);
924 speedup
= speedup
* freq
;
927 speedup
= speedup
>> 11;
928 gcc_checking_assert (speedup
>= 0);
932 /* Return true if the speedup for inlining E is bigger than
933 param_inline_min_speedup. */
936 big_speedup_p (struct cgraph_edge
*e
)
939 sreal spec_time
= estimate_edge_time (e
, &unspec_time
);
940 sreal freq
= e
->sreal_frequency ();
941 sreal time
= compute_uninlined_call_time (e
, unspec_time
, freq
);
942 sreal inlined_time
= compute_inlined_call_time (e
, spec_time
, freq
);
943 cgraph_node
*caller
= (e
->caller
->inlined_to
944 ? e
->caller
->inlined_to
946 int limit
= opt_for_fn (caller
->decl
, param_inline_min_speedup
);
948 if ((time
- inlined_time
) * 100 > time
* limit
)
953 /* Return true if we are interested in inlining small function.
954 When REPORT is true, report reason to dump file. */
957 want_inline_small_function_p (struct cgraph_edge
*e
, bool report
)
959 bool want_inline
= true;
960 struct cgraph_node
*callee
= e
->callee
->ultimate_alias_target ();
961 cgraph_node
*to
= (e
->caller
->inlined_to
962 ? e
->caller
->inlined_to
: e
->caller
);
964 /* Allow this function to be called before can_inline_edge_p,
965 since it's usually cheaper. */
966 if (cgraph_inline_failed_type (e
->inline_failed
) == CIF_FINAL_ERROR
)
968 else if (DECL_DISREGARD_INLINE_LIMITS (callee
->decl
))
970 else if (!DECL_DECLARED_INLINE_P (callee
->decl
)
971 && !opt_for_fn (e
->caller
->decl
, flag_inline_small_functions
))
973 e
->inline_failed
= CIF_FUNCTION_NOT_INLINE_CANDIDATE
;
976 /* Do fast and conservative check if the function can be good
978 else if ((!DECL_DECLARED_INLINE_P (callee
->decl
)
979 && (!e
->count
.ipa ().initialized_p () || !e
->maybe_hot_p ()))
980 && ipa_fn_summaries
->get (callee
)->min_size
981 - ipa_call_summaries
->get (e
)->call_stmt_size
982 > inline_insns_auto (e
->caller
, true, true))
984 e
->inline_failed
= CIF_MAX_INLINE_INSNS_AUTO_LIMIT
;
987 else if ((DECL_DECLARED_INLINE_P (callee
->decl
)
988 || e
->count
.ipa ().nonzero_p ())
989 && ipa_fn_summaries
->get (callee
)->min_size
990 - ipa_call_summaries
->get (e
)->call_stmt_size
991 > inline_insns_single (e
->caller
, true, true))
993 e
->inline_failed
= (DECL_DECLARED_INLINE_P (callee
->decl
)
994 ? CIF_MAX_INLINE_INSNS_SINGLE_LIMIT
995 : CIF_MAX_INLINE_INSNS_AUTO_LIMIT
);
1000 int growth
= estimate_edge_growth (e
);
1001 ipa_hints hints
= estimate_edge_hints (e
);
1002 /* We have two independent groups of hints. If one matches in each
1003 of groups the limits are inreased. If both groups matches, limit
1004 is increased even more. */
1005 bool apply_hints
= (hints
& (INLINE_HINT_indirect_call
1006 | INLINE_HINT_known_hot
1007 | INLINE_HINT_loop_iterations
1008 | INLINE_HINT_loop_stride
));
1009 bool apply_hints2
= (hints
& INLINE_HINT_builtin_constant_p
);
1011 if (growth
<= opt_for_fn (to
->decl
,
1012 param_max_inline_insns_size
))
1014 /* Apply param_max_inline_insns_single limit. Do not do so when
1015 hints suggests that inlining given function is very profitable.
1016 Avoid computation of big_speedup_p when not necessary to change
1017 outcome of decision. */
1018 else if (DECL_DECLARED_INLINE_P (callee
->decl
)
1019 && growth
>= inline_insns_single (e
->caller
, apply_hints
,
1021 && (apply_hints
|| apply_hints2
1022 || growth
>= inline_insns_single (e
->caller
, true,
1024 || !big_speedup_p (e
)))
1026 e
->inline_failed
= CIF_MAX_INLINE_INSNS_SINGLE_LIMIT
;
1027 want_inline
= false;
1029 else if (!DECL_DECLARED_INLINE_P (callee
->decl
)
1030 && !opt_for_fn (e
->caller
->decl
, flag_inline_functions
)
1031 && growth
>= opt_for_fn (to
->decl
,
1032 param_max_inline_insns_small
))
1034 /* growth_positive_p is expensive, always test it last. */
1035 if (growth
>= inline_insns_single (e
->caller
, false, false)
1036 || growth_positive_p (callee
, e
, growth
))
1038 e
->inline_failed
= CIF_NOT_DECLARED_INLINED
;
1039 want_inline
= false;
1042 /* Apply param_max_inline_insns_auto limit for functions not declared
1043 inline. Bypass the limit when speedup seems big. */
1044 else if (!DECL_DECLARED_INLINE_P (callee
->decl
)
1045 && growth
>= inline_insns_auto (e
->caller
, apply_hints
,
1047 && (apply_hints
|| apply_hints2
1048 || growth
>= inline_insns_auto (e
->caller
, true,
1050 || !big_speedup_p (e
)))
1052 /* growth_positive_p is expensive, always test it last. */
1053 if (growth
>= inline_insns_single (e
->caller
, false, false)
1054 || growth_positive_p (callee
, e
, growth
))
1056 e
->inline_failed
= CIF_MAX_INLINE_INSNS_AUTO_LIMIT
;
1057 want_inline
= false;
1060 /* If call is cold, do not inline when function body would grow. */
1061 else if (!e
->maybe_hot_p ()
1062 && (growth
>= inline_insns_single (e
->caller
, false, false)
1063 || growth_positive_p (callee
, e
, growth
)))
1065 e
->inline_failed
= CIF_UNLIKELY_CALL
;
1066 want_inline
= false;
1069 if (!want_inline
&& report
)
1070 report_inline_failed_reason (e
);
1074 /* EDGE is self recursive edge.
1075 We handle two cases - when function A is inlining into itself
1076 or when function A is being inlined into another inliner copy of function
1077 A within function B.
1079 In first case OUTER_NODE points to the toplevel copy of A, while
1080 in the second case OUTER_NODE points to the outermost copy of A in B.
1082 In both cases we want to be extra selective since
1083 inlining the call will just introduce new recursive calls to appear. */
1086 want_inline_self_recursive_call_p (struct cgraph_edge
*edge
,
1087 struct cgraph_node
*outer_node
,
1091 char const *reason
= NULL
;
1092 bool want_inline
= true;
1093 sreal caller_freq
= 1;
1094 int max_depth
= opt_for_fn (outer_node
->decl
,
1095 param_max_inline_recursive_depth_auto
);
1097 if (DECL_DECLARED_INLINE_P (edge
->caller
->decl
))
1098 max_depth
= opt_for_fn (outer_node
->decl
,
1099 param_max_inline_recursive_depth
);
1101 if (!edge
->maybe_hot_p ())
1103 reason
= "recursive call is cold";
1104 want_inline
= false;
1106 else if (depth
> max_depth
)
1108 reason
= "--param max-inline-recursive-depth exceeded.";
1109 want_inline
= false;
1111 else if (outer_node
->inlined_to
1112 && (caller_freq
= outer_node
->callers
->sreal_frequency ()) == 0)
1114 reason
= "caller frequency is 0";
1115 want_inline
= false;
1120 /* Inlining of self recursive function into copy of itself within other
1121 function is transformation similar to loop peeling.
1123 Peeling is profitable if we can inline enough copies to make probability
1124 of actual call to the self recursive function very small. Be sure that
1125 the probability of recursion is small.
1127 We ensure that the frequency of recursing is at most 1 - (1/max_depth).
1128 This way the expected number of recursion is at most max_depth. */
1131 sreal max_prob
= (sreal
)1 - ((sreal
)1 / (sreal
)max_depth
);
1133 for (i
= 1; i
< depth
; i
++)
1134 max_prob
= max_prob
* max_prob
;
1135 if (edge
->sreal_frequency () >= max_prob
* caller_freq
)
1137 reason
= "frequency of recursive call is too large";
1138 want_inline
= false;
1141 /* Recursive inlining, i.e. equivalent of unrolling, is profitable if
1142 recursion depth is large. We reduce function call overhead and increase
1143 chances that things fit in hardware return predictor.
1145 Recursive inlining might however increase cost of stack frame setup
1146 actually slowing down functions whose recursion tree is wide rather than
1149 Deciding reliably on when to do recursive inlining without profile feedback
1150 is tricky. For now we disable recursive inlining when probability of self
1153 Recursive inlining of self recursive call within loop also results in
1154 large loop depths that generally optimize badly. We may want to throttle
1155 down inlining in those cases. In particular this seems to happen in one
1156 of libstdc++ rb tree methods. */
1159 if (edge
->sreal_frequency () * 100
1161 * opt_for_fn (outer_node
->decl
,
1162 param_min_inline_recursive_probability
))
1164 reason
= "frequency of recursive call is too small";
1165 want_inline
= false;
1168 if (!can_inline_edge_by_limits_p (edge
, CAN_INLINE_FORCE_LIMITS
| CAN_INLINE_REPORT
))
1170 reason
= "inline limits exceeded for always_inline function";
1171 want_inline
= false;
1173 if (!want_inline
&& dump_enabled_p ())
1174 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, edge
->call_stmt
,
1175 " not inlining recursively: %s\n", reason
);
1179 /* Return true when NODE has uninlinable caller;
1180 set HAS_HOT_CALL if it has hot call.
1181 Worker for cgraph_for_node_and_aliases. */
1184 check_callers (struct cgraph_node
*node
, void *has_hot_call
)
1186 struct cgraph_edge
*e
;
1187 for (e
= node
->callers
; e
; e
= e
->next_caller
)
1189 if (!opt_for_fn (e
->caller
->decl
, flag_inline_functions_called_once
)
1190 || !opt_for_fn (e
->caller
->decl
, optimize
))
1192 if (!can_inline_edge_p (e
, true))
1194 if (e
->recursive_p ())
1196 if (!can_inline_edge_by_limits_p (e
, CAN_INLINE_REPORT
))
1198 /* Inlining large functions to large loop depth is often harmful because
1199 of register pressure it implies. */
1200 if ((int)ipa_call_summaries
->get (e
)->loop_depth
1201 > param_inline_functions_called_once_loop_depth
)
1203 /* Do not produce gigantic functions. */
1204 if (estimate_size_after_inlining (e
->caller
->inlined_to
?
1205 e
->caller
->inlined_to
: e
->caller
, e
)
1206 > param_inline_functions_called_once_insns
)
1208 if (!(*(bool *)has_hot_call
) && e
->maybe_hot_p ())
1209 *(bool *)has_hot_call
= true;
1214 /* If NODE has a caller, return true. */
1217 has_caller_p (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
1224 /* Decide if inlining NODE would reduce unit size by eliminating
1225 the offline copy of function.
1226 When COLD is true the cold calls are considered, too. */
1229 want_inline_function_to_all_callers_p (struct cgraph_node
*node
, bool cold
)
1231 bool has_hot_call
= false;
1233 /* Aliases gets inlined along with the function they alias. */
1236 /* Already inlined? */
1237 if (node
->inlined_to
)
1239 /* Does it have callers? */
1240 if (!node
->call_for_symbol_and_aliases (has_caller_p
, NULL
, true))
1242 /* Inlining into all callers would increase size? */
1243 if (growth_positive_p (node
, NULL
, INT_MIN
) > 0)
1245 /* All inlines must be possible. */
1246 if (node
->call_for_symbol_and_aliases (check_callers
, &has_hot_call
,
1249 if (!cold
&& !has_hot_call
)
1254 /* Return true if WHERE of SIZE is a possible candidate for wrapper heuristics
1255 in estimate_edge_badness. */
1258 wrapper_heuristics_may_apply (struct cgraph_node
*where
, int size
)
1260 return size
< (DECL_DECLARED_INLINE_P (where
->decl
)
1261 ? inline_insns_single (where
, false, false)
1262 : inline_insns_auto (where
, false, false));
1265 /* A cost model driving the inlining heuristics in a way so the edges with
1266 smallest badness are inlined first. After each inlining is performed
1267 the costs of all caller edges of nodes affected are recomputed so the
1268 metrics may accurately depend on values such as number of inlinable callers
1269 of the function or function body size. */
1272 edge_badness (struct cgraph_edge
*edge
, bool dump
)
1276 sreal edge_time
, unspec_edge_time
;
1277 struct cgraph_node
*callee
= edge
->callee
->ultimate_alias_target ();
1278 class ipa_fn_summary
*callee_info
= ipa_fn_summaries
->get (callee
);
1280 cgraph_node
*caller
= (edge
->caller
->inlined_to
1281 ? edge
->caller
->inlined_to
1284 growth
= estimate_edge_growth (edge
);
1285 edge_time
= estimate_edge_time (edge
, &unspec_edge_time
);
1286 hints
= estimate_edge_hints (edge
);
1287 gcc_checking_assert (edge_time
>= 0);
1288 /* Check that inlined time is better, but tolerate some roundoff issues.
1289 FIXME: When callee profile drops to 0 we account calls more. This
1290 should be fixed by never doing that. */
1291 gcc_checking_assert ((edge_time
* 100
1292 - callee_info
->time
* 101).to_int () <= 0
1293 || callee
->count
.ipa ().initialized_p ());
1294 gcc_checking_assert (growth
<= ipa_size_summaries
->get (callee
)->size
);
1298 fprintf (dump_file
, " Badness calculation for %s -> %s\n",
1299 edge
->caller
->dump_name (),
1300 edge
->callee
->dump_name ());
1301 fprintf (dump_file
, " size growth %i, time %f unspec %f ",
1303 edge_time
.to_double (),
1304 unspec_edge_time
.to_double ());
1305 ipa_dump_hints (dump_file
, hints
);
1306 if (big_speedup_p (edge
))
1307 fprintf (dump_file
, " big_speedup");
1308 fprintf (dump_file
, "\n");
1311 /* Always prefer inlining saving code size. */
1314 badness
= (sreal
) (-SREAL_MIN_SIG
+ growth
) << (SREAL_MAX_EXP
/ 256);
1316 fprintf (dump_file
, " %f: Growth %d <= 0\n", badness
.to_double (),
1319 /* Inlining into EXTERNAL functions is not going to change anything unless
1320 they are themselves inlined. */
1321 else if (DECL_EXTERNAL (caller
->decl
))
1324 fprintf (dump_file
, " max: function is external\n");
1325 return sreal::max ();
1327 /* When profile is available. Compute badness as:
1329 time_saved * caller_count
1330 goodness = -------------------------------------------------
1331 growth_of_caller * overall_growth * combined_size
1333 badness = - goodness
1335 Again use negative value to make calls with profile appear hotter
1338 else if (opt_for_fn (caller
->decl
, flag_guess_branch_prob
)
1339 || caller
->count
.ipa ().nonzero_p ())
1341 sreal numerator
, denominator
;
1343 sreal freq
= edge
->sreal_frequency ();
1345 numerator
= inlining_speedup (edge
, freq
, unspec_edge_time
, edge_time
);
1347 numerator
= ((sreal
) 1 >> 8);
1348 if (caller
->count
.ipa ().nonzero_p ())
1349 numerator
*= caller
->count
.ipa ().to_gcov_type ();
1350 else if (caller
->count
.ipa ().initialized_p ())
1351 numerator
= numerator
>> 11;
1352 denominator
= growth
;
1354 overall_growth
= callee_info
->growth
;
1356 /* Look for inliner wrappers of the form:
1362 noninline_callee ();
1364 Without penalizing this case, we usually inline noninline_callee
1365 into the inline_caller because overall_growth is small preventing
1366 further inlining of inline_caller.
1368 Penalize only callgraph edges to functions with small overall
1371 if (growth
> overall_growth
1372 /* ... and having only one caller which is not inlined ... */
1373 && callee_info
->single_caller
1374 && !edge
->caller
->inlined_to
1375 /* ... and edges executed only conditionally ... */
1377 /* ... consider case where callee is not inline but caller is ... */
1378 && ((!DECL_DECLARED_INLINE_P (edge
->callee
->decl
)
1379 && DECL_DECLARED_INLINE_P (caller
->decl
))
1380 /* ... or when early optimizers decided to split and edge
1381 frequency still indicates splitting is a win ... */
1382 || (callee
->split_part
&& !caller
->split_part
1384 < opt_for_fn (caller
->decl
,
1385 param_partial_inlining_entry_probability
)
1386 /* ... and do not overwrite user specified hints. */
1387 && (!DECL_DECLARED_INLINE_P (edge
->callee
->decl
)
1388 || DECL_DECLARED_INLINE_P (caller
->decl
)))))
1390 ipa_fn_summary
*caller_info
= ipa_fn_summaries
->get (caller
);
1391 int caller_growth
= caller_info
->growth
;
1393 /* Only apply the penalty when caller looks like inline candidate,
1394 and it is not called once. */
1395 if (!caller_info
->single_caller
&& overall_growth
< caller_growth
1396 && caller_info
->inlinable
1397 && wrapper_heuristics_may_apply
1398 (caller
, ipa_size_summaries
->get (caller
)->size
))
1402 " Wrapper penalty. Increasing growth %i to %i\n",
1403 overall_growth
, caller_growth
);
1404 overall_growth
= caller_growth
;
1407 if (overall_growth
> 0)
1409 /* Strongly prefer functions with few callers that can be inlined
1410 fully. The square root here leads to smaller binaries at average.
1411 Watch however for extreme cases and return to linear function
1412 when growth is large. */
1413 if (overall_growth
< 256)
1414 overall_growth
*= overall_growth
;
1416 overall_growth
+= 256 * 256 - 256;
1417 denominator
*= overall_growth
;
1419 denominator
*= ipa_size_summaries
->get (caller
)->size
+ growth
;
1421 badness
= - numerator
/ denominator
;
1426 " %f: guessed profile. frequency %f, count %" PRId64
1427 " caller count %" PRId64
1429 " overall growth %i (current) %i (original)"
1430 " %i (compensated)\n",
1431 badness
.to_double (),
1433 edge
->count
.ipa ().initialized_p ()
1434 ? edge
->count
.ipa ().to_gcov_type () : -1,
1435 caller
->count
.ipa ().initialized_p ()
1436 ? caller
->count
.ipa ().to_gcov_type () : -1,
1437 inlining_speedup (edge
, freq
, unspec_edge_time
,
1438 edge_time
).to_double (),
1439 estimate_growth (callee
),
1440 callee_info
->growth
, overall_growth
);
1443 /* When function local profile is not available or it does not give
1444 useful information (i.e. frequency is zero), base the cost on
1445 loop nest and overall size growth, so we optimize for overall number
1446 of functions fully inlined in program. */
1449 int nest
= MIN (ipa_call_summaries
->get (edge
)->loop_depth
, 8);
1452 /* Decrease badness if call is nested. */
1454 badness
= badness
>> nest
;
1456 badness
= badness
<< nest
;
1458 fprintf (dump_file
, " %f: no profile. nest %i\n",
1459 badness
.to_double (), nest
);
1461 gcc_checking_assert (badness
!= 0);
1463 if (edge
->recursive_p ())
1464 badness
= badness
.shift (badness
> 0 ? 4 : -4);
1465 if ((hints
& (INLINE_HINT_indirect_call
1466 | INLINE_HINT_loop_iterations
1467 | INLINE_HINT_loop_stride
))
1468 || callee_info
->growth
<= 0)
1469 badness
= badness
.shift (badness
> 0 ? -2 : 2);
1470 if (hints
& INLINE_HINT_builtin_constant_p
)
1471 badness
= badness
.shift (badness
> 0 ? -4 : 4);
1472 if (hints
& (INLINE_HINT_same_scc
))
1473 badness
= badness
.shift (badness
> 0 ? 3 : -3);
1474 else if (hints
& (INLINE_HINT_in_scc
))
1475 badness
= badness
.shift (badness
> 0 ? 2 : -2);
1476 else if (hints
& (INLINE_HINT_cross_module
))
1477 badness
= badness
.shift (badness
> 0 ? 1 : -1);
1478 if (DECL_DISREGARD_INLINE_LIMITS (callee
->decl
))
1479 badness
= badness
.shift (badness
> 0 ? -4 : 4);
1480 else if ((hints
& INLINE_HINT_declared_inline
))
1481 badness
= badness
.shift (badness
> 0 ? -3 : 3);
1483 fprintf (dump_file
, " Adjusted by hints %f\n", badness
.to_double ());
1487 /* Recompute badness of EDGE and update its key in HEAP if needed. */
1489 update_edge_key (edge_heap_t
*heap
, struct cgraph_edge
*edge
)
1491 sreal badness
= edge_badness (edge
, false);
1494 edge_heap_node_t
*n
= (edge_heap_node_t
*) edge
->aux
;
1495 gcc_checking_assert (n
->get_data () == edge
);
1497 /* fibonacci_heap::replace_key does busy updating of the
1498 heap that is unnecessarily expensive.
1499 We do lazy increases: after extracting minimum if the key
1500 turns out to be out of date, it is re-inserted into heap
1501 with correct value. */
1502 if (badness
< n
->get_key ().badness
)
1504 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1507 " decreasing badness %s -> %s, %f to %f\n",
1508 edge
->caller
->dump_name (),
1509 edge
->callee
->dump_name (),
1510 n
->get_key ().badness
.to_double (),
1511 badness
.to_double ());
1513 inline_badness
b (edge
, badness
);
1514 heap
->decrease_key (n
, b
);
1519 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1522 " enqueuing call %s -> %s, badness %f\n",
1523 edge
->caller
->dump_name (),
1524 edge
->callee
->dump_name (),
1525 badness
.to_double ());
1527 inline_badness
b (edge
, badness
);
1528 edge
->aux
= heap
->insert (b
, edge
);
1533 /* NODE was inlined.
1534 All caller edges needs to be reset because
1535 size estimates change. Similarly callees needs reset
1536 because better context may be known. */
1539 reset_edge_caches (struct cgraph_node
*node
)
1541 struct cgraph_edge
*edge
;
1542 struct cgraph_edge
*e
= node
->callees
;
1543 struct cgraph_node
*where
= node
;
1544 struct ipa_ref
*ref
;
1546 if (where
->inlined_to
)
1547 where
= where
->inlined_to
;
1549 reset_node_cache (where
);
1551 if (edge_growth_cache
!= NULL
)
1552 for (edge
= where
->callers
; edge
; edge
= edge
->next_caller
)
1553 if (edge
->inline_failed
)
1554 edge_growth_cache
->remove (edge
);
1556 FOR_EACH_ALIAS (where
, ref
)
1557 reset_edge_caches (dyn_cast
<cgraph_node
*> (ref
->referring
));
1563 if (!e
->inline_failed
&& e
->callee
->callees
)
1564 e
= e
->callee
->callees
;
1567 if (edge_growth_cache
!= NULL
&& e
->inline_failed
)
1568 edge_growth_cache
->remove (e
);
1575 if (e
->caller
== node
)
1577 e
= e
->caller
->callers
;
1579 while (!e
->next_callee
);
1585 /* Recompute HEAP nodes for each of caller of NODE.
1586 UPDATED_NODES track nodes we already visited, to avoid redundant work.
1587 When CHECK_INLINABLITY_FOR is set, re-check for specified edge that
1588 it is inlinable. Otherwise check all edges. */
1591 update_caller_keys (edge_heap_t
*heap
, struct cgraph_node
*node
,
1592 bitmap updated_nodes
,
1593 struct cgraph_edge
*check_inlinablity_for
)
1595 struct cgraph_edge
*edge
;
1596 struct ipa_ref
*ref
;
1598 if ((!node
->alias
&& !ipa_fn_summaries
->get (node
)->inlinable
)
1599 || node
->inlined_to
)
1601 if (!bitmap_set_bit (updated_nodes
, node
->get_uid ()))
1604 FOR_EACH_ALIAS (node
, ref
)
1606 struct cgraph_node
*alias
= dyn_cast
<cgraph_node
*> (ref
->referring
);
1607 update_caller_keys (heap
, alias
, updated_nodes
, check_inlinablity_for
);
1610 for (edge
= node
->callers
; edge
; edge
= edge
->next_caller
)
1611 if (edge
->inline_failed
)
1613 if (!check_inlinablity_for
1614 || check_inlinablity_for
== edge
)
1616 if (can_inline_edge_p (edge
, false)
1617 && want_inline_small_function_p (edge
, false)
1618 && can_inline_edge_by_limits_p (edge
, 0))
1619 update_edge_key (heap
, edge
);
1622 report_inline_failed_reason (edge
);
1623 heap
->delete_node ((edge_heap_node_t
*) edge
->aux
);
1628 update_edge_key (heap
, edge
);
1632 /* Recompute HEAP nodes for each uninlined call in NODE
1633 If UPDATE_SINCE is non-NULL check if edges called within that function
1634 are inlinable (typically UPDATE_SINCE is the inline clone we introduced
1635 where all edges have new context).
1637 This is used when we know that edge badnesses are going only to increase
1638 (we introduced new call site) and thus all we need is to insert newly
1639 created edges into heap. */
1642 update_callee_keys (edge_heap_t
*heap
, struct cgraph_node
*node
,
1643 struct cgraph_node
*update_since
,
1644 bitmap updated_nodes
)
1646 struct cgraph_edge
*e
= node
->callees
;
1647 bool check_inlinability
= update_since
== node
;
1652 if (!e
->inline_failed
&& e
->callee
->callees
)
1654 if (e
->callee
== update_since
)
1655 check_inlinability
= true;
1656 e
= e
->callee
->callees
;
1660 enum availability avail
;
1661 struct cgraph_node
*callee
;
1662 if (!check_inlinability
)
1665 && !bitmap_bit_p (updated_nodes
,
1666 e
->callee
->ultimate_alias_target
1667 (&avail
, e
->caller
)->get_uid ()))
1668 update_edge_key (heap
, e
);
1670 /* We do not reset callee growth cache here. Since we added a new call,
1671 growth should have just increased and consequently badness metric
1672 don't need updating. */
1673 else if (e
->inline_failed
1674 && (callee
= e
->callee
->ultimate_alias_target (&avail
,
1676 && avail
>= AVAIL_AVAILABLE
1677 && ipa_fn_summaries
->get (callee
) != NULL
1678 && ipa_fn_summaries
->get (callee
)->inlinable
1679 && !bitmap_bit_p (updated_nodes
, callee
->get_uid ()))
1681 if (can_inline_edge_p (e
, false)
1682 && want_inline_small_function_p (e
, false)
1683 && can_inline_edge_by_limits_p (e
, 0))
1685 gcc_checking_assert (check_inlinability
|| can_inline_edge_p (e
, false));
1686 gcc_checking_assert (check_inlinability
|| e
->aux
);
1687 update_edge_key (heap
, e
);
1691 report_inline_failed_reason (e
);
1692 heap
->delete_node ((edge_heap_node_t
*) e
->aux
);
1696 /* In case we redirected to unreachable node we only need to remove the
1700 heap
->delete_node ((edge_heap_node_t
*) e
->aux
);
1709 if (e
->caller
== node
)
1711 if (e
->caller
== update_since
)
1712 check_inlinability
= false;
1713 e
= e
->caller
->callers
;
1715 while (!e
->next_callee
);
1721 /* Enqueue all recursive calls from NODE into priority queue depending on
1722 how likely we want to recursively inline the call. */
1725 lookup_recursive_calls (struct cgraph_node
*node
, struct cgraph_node
*where
,
1728 struct cgraph_edge
*e
;
1729 enum availability avail
;
1731 for (e
= where
->callees
; e
; e
= e
->next_callee
)
1732 if (e
->callee
== node
1733 || (e
->callee
->ultimate_alias_target (&avail
, e
->caller
) == node
1734 && avail
> AVAIL_INTERPOSABLE
))
1736 inline_badness
b (e
, -e
->sreal_frequency ());
1737 heap
->insert (b
, e
);
1739 for (e
= where
->callees
; e
; e
= e
->next_callee
)
1740 if (!e
->inline_failed
)
1741 lookup_recursive_calls (node
, e
->callee
, heap
);
1744 /* Decide on recursive inlining: in the case function has recursive calls,
1745 inline until body size reaches given argument. If any new indirect edges
1746 are discovered in the process, add them to *NEW_EDGES, unless NEW_EDGES
1750 recursive_inlining (struct cgraph_edge
*edge
,
1751 vec
<cgraph_edge
*> *new_edges
)
1753 cgraph_node
*to
= (edge
->caller
->inlined_to
1754 ? edge
->caller
->inlined_to
: edge
->caller
);
1755 int limit
= opt_for_fn (to
->decl
,
1756 param_max_inline_insns_recursive_auto
);
1757 inline_badness
b (edge
, sreal::min ());
1758 edge_heap_t
heap (b
);
1759 struct cgraph_node
*node
;
1760 struct cgraph_edge
*e
;
1761 struct cgraph_node
*master_clone
= NULL
, *next
;
1765 node
= edge
->caller
;
1766 if (node
->inlined_to
)
1767 node
= node
->inlined_to
;
1769 if (DECL_DECLARED_INLINE_P (node
->decl
))
1770 limit
= opt_for_fn (to
->decl
, param_max_inline_insns_recursive
);
1772 /* Make sure that function is small enough to be considered for inlining. */
1773 if (estimate_size_after_inlining (node
, edge
) >= limit
)
1775 lookup_recursive_calls (node
, node
, &heap
);
1781 " Performing recursive inlining on %s\n", node
->dump_name ());
1783 /* Do the inlining and update list of recursive call during process. */
1784 while (!heap
.empty ())
1786 struct cgraph_edge
*curr
= heap
.extract_min ();
1787 struct cgraph_node
*cnode
, *dest
= curr
->callee
;
1789 if (!can_inline_edge_p (curr
, true)
1790 || !can_inline_edge_by_limits_p (curr
, CAN_INLINE_REPORT
| CAN_INLINE_FORCE_LIMITS
))
1793 /* MASTER_CLONE is produced in the case we already started modified
1794 the function. Be sure to redirect edge to the original body before
1795 estimating growths otherwise we will be seeing growths after inlining
1796 the already modified body. */
1799 curr
->redirect_callee (master_clone
);
1800 if (edge_growth_cache
!= NULL
)
1801 edge_growth_cache
->remove (curr
);
1804 if (estimate_size_after_inlining (node
, curr
) > limit
)
1806 curr
->redirect_callee (dest
);
1807 if (edge_growth_cache
!= NULL
)
1808 edge_growth_cache
->remove (curr
);
1813 for (cnode
= curr
->caller
;
1814 cnode
->inlined_to
; cnode
= cnode
->callers
->caller
)
1816 == curr
->callee
->ultimate_alias_target ()->decl
)
1819 if (!want_inline_self_recursive_call_p (curr
, node
, false, depth
))
1821 curr
->redirect_callee (dest
);
1822 if (edge_growth_cache
!= NULL
)
1823 edge_growth_cache
->remove (curr
);
1830 " Inlining call of depth %i", depth
);
1831 if (node
->count
.nonzero_p () && curr
->count
.initialized_p ())
1833 fprintf (dump_file
, " called approx. %.2f times per call",
1834 (double)curr
->count
.to_gcov_type ()
1835 / node
->count
.to_gcov_type ());
1837 fprintf (dump_file
, "\n");
1841 /* We need original clone to copy around. */
1842 master_clone
= node
->create_clone (node
->decl
, node
->count
,
1843 false, vNULL
, true, NULL
, NULL
);
1844 for (e
= master_clone
->callees
; e
; e
= e
->next_callee
)
1845 if (!e
->inline_failed
)
1846 clone_inlined_nodes (e
, true, false, NULL
);
1847 curr
->redirect_callee (master_clone
);
1848 if (edge_growth_cache
!= NULL
)
1849 edge_growth_cache
->remove (curr
);
1852 inline_call (curr
, false, new_edges
, &overall_size
, true);
1853 reset_node_cache (node
);
1854 lookup_recursive_calls (node
, curr
->callee
, &heap
);
1858 if (!heap
.empty () && dump_file
)
1859 fprintf (dump_file
, " Recursive inlining growth limit met.\n");
1864 if (dump_enabled_p ())
1865 dump_printf_loc (MSG_NOTE
, edge
->call_stmt
,
1866 "\n Inlined %i times, "
1867 "body grown from size %i to %i, time %f to %f\n", n
,
1868 ipa_size_summaries
->get (master_clone
)->size
,
1869 ipa_size_summaries
->get (node
)->size
,
1870 ipa_fn_summaries
->get (master_clone
)->time
.to_double (),
1871 ipa_fn_summaries
->get (node
)->time
.to_double ());
1873 /* Remove master clone we used for inlining. We rely that clones inlined
1874 into master clone gets queued just before master clone so we don't
1876 for (node
= symtab
->first_function (); node
!= master_clone
;
1879 next
= symtab
->next_function (node
);
1880 if (node
->inlined_to
== master_clone
)
1883 master_clone
->remove ();
1888 /* Given whole compilation unit estimate of INSNS, compute how large we can
1889 allow the unit to grow. */
1892 compute_max_insns (cgraph_node
*node
, int insns
)
1894 int max_insns
= insns
;
1895 if (max_insns
< opt_for_fn (node
->decl
, param_large_unit_insns
))
1896 max_insns
= opt_for_fn (node
->decl
, param_large_unit_insns
);
1898 return ((int64_t) max_insns
1899 * (100 + opt_for_fn (node
->decl
, param_inline_unit_growth
)) / 100);
1903 /* Compute badness of all edges in NEW_EDGES and add them to the HEAP. */
1906 add_new_edges_to_heap (edge_heap_t
*heap
, vec
<cgraph_edge
*> &new_edges
)
1908 while (new_edges
.length () > 0)
1910 struct cgraph_edge
*edge
= new_edges
.pop ();
1912 gcc_assert (!edge
->aux
);
1913 gcc_assert (edge
->callee
);
1914 if (edge
->inline_failed
1915 && can_inline_edge_p (edge
, true)
1916 && want_inline_small_function_p (edge
, true)
1917 && can_inline_edge_by_limits_p (edge
, CAN_INLINE_REPORT
))
1919 inline_badness
b (edge
, edge_badness (edge
, false));
1920 edge
->aux
= heap
->insert (b
, edge
);
1925 /* Remove EDGE from the fibheap. */
1928 heap_edge_removal_hook (struct cgraph_edge
*e
, void *data
)
1932 ((edge_heap_t
*)data
)->delete_node ((edge_heap_node_t
*)e
->aux
);
1937 /* Return true if speculation of edge E seems useful.
1938 If ANTICIPATE_INLINING is true, be conservative and hope that E
1942 speculation_useful_p (struct cgraph_edge
*e
, bool anticipate_inlining
)
1944 /* If we have already decided to inline the edge, it seems useful. */
1945 if (!e
->inline_failed
)
1948 enum availability avail
;
1949 struct cgraph_node
*target
= e
->callee
->ultimate_alias_target (&avail
,
1952 gcc_assert (e
->speculative
&& !e
->indirect_unknown_callee
);
1954 if (!e
->maybe_hot_p ())
1957 /* See if IP optimizations found something potentially useful about the
1958 function. For now we look only for CONST/PURE flags. Almost everything
1959 else we propagate is useless. */
1960 if (avail
>= AVAIL_AVAILABLE
)
1962 int ecf_flags
= flags_from_decl_or_type (target
->decl
);
1963 if (ecf_flags
& ECF_CONST
)
1965 if (!(e
->speculative_call_indirect_edge ()->indirect_info
1966 ->ecf_flags
& ECF_CONST
))
1969 else if (ecf_flags
& ECF_PURE
)
1971 if (!(e
->speculative_call_indirect_edge ()->indirect_info
1972 ->ecf_flags
& ECF_PURE
))
1976 /* If we did not managed to inline the function nor redirect
1977 to an ipa-cp clone (that are seen by having local flag set),
1978 it is probably pointless to inline it unless hardware is missing
1979 indirect call predictor. */
1980 if (!anticipate_inlining
&& !target
->local
)
1982 /* For overwritable targets there is not much to do. */
1983 if (!can_inline_edge_p (e
, false)
1984 || !can_inline_edge_by_limits_p (e
, CAN_INLINE_DISREGARD_LIMITS
))
1986 /* OK, speculation seems interesting. */
1990 /* We know that EDGE is not going to be inlined.
1991 See if we can remove speculation. */
1994 resolve_noninline_speculation (edge_heap_t
*edge_heap
, struct cgraph_edge
*edge
)
1996 if (edge
->speculative
&& !speculation_useful_p (edge
, false))
1998 struct cgraph_node
*node
= edge
->caller
;
1999 struct cgraph_node
*where
= node
->inlined_to
2000 ? node
->inlined_to
: node
;
2001 auto_bitmap updated_nodes
;
2003 if (edge
->count
.ipa ().initialized_p ())
2004 spec_rem
+= edge
->count
.ipa ();
2005 cgraph_edge::resolve_speculation (edge
);
2006 reset_edge_caches (where
);
2007 ipa_update_overall_fn_summary (where
);
2008 update_caller_keys (edge_heap
, where
,
2009 updated_nodes
, NULL
);
2010 update_callee_keys (edge_heap
, where
, NULL
,
2015 /* Return true if NODE should be accounted for overall size estimate.
2016 Skip all nodes optimized for size so we can measure the growth of hot
2017 part of program no matter of the padding. */
2020 inline_account_function_p (struct cgraph_node
*node
)
2022 return (!DECL_EXTERNAL (node
->decl
)
2023 && !opt_for_fn (node
->decl
, optimize_size
)
2024 && node
->frequency
!= NODE_FREQUENCY_UNLIKELY_EXECUTED
);
2027 /* Count number of callers of NODE and store it into DATA (that
2028 points to int. Worker for cgraph_for_node_and_aliases. */
2031 sum_callers (struct cgraph_node
*node
, void *data
)
2033 struct cgraph_edge
*e
;
2034 int *num_calls
= (int *)data
;
2036 for (e
= node
->callers
; e
; e
= e
->next_caller
)
2041 /* We only propagate across edges with non-interposable callee. */
2044 ignore_edge_p (struct cgraph_edge
*e
)
2046 enum availability avail
;
2047 e
->callee
->function_or_virtual_thunk_symbol (&avail
, e
->caller
);
2048 return (avail
<= AVAIL_INTERPOSABLE
);
2051 /* We use greedy algorithm for inlining of small functions:
2052 All inline candidates are put into prioritized heap ordered in
2055 The inlining of small functions is bounded by unit growth parameters. */
2058 inline_small_functions (void)
2060 struct cgraph_node
*node
;
2061 struct cgraph_edge
*edge
;
2063 edge_heap_t
edge_heap (b
);
2064 auto_bitmap updated_nodes
;
2066 auto_vec
<cgraph_edge
*> new_indirect_edges
;
2067 int initial_size
= 0;
2068 struct cgraph_node
**order
= XCNEWVEC (cgraph_node
*, symtab
->cgraph_count
);
2069 struct cgraph_edge_hook_list
*edge_removal_hook_holder
;
2070 new_indirect_edges
.create (8);
2072 edge_removal_hook_holder
2073 = symtab
->add_edge_removal_hook (&heap_edge_removal_hook
, &edge_heap
);
2075 /* Compute overall unit size and other global parameters used by badness
2078 max_count
= profile_count::uninitialized ();
2079 ipa_reduced_postorder (order
, true, ignore_edge_p
);
2082 FOR_EACH_DEFINED_FUNCTION (node
)
2083 if (!node
->inlined_to
)
2085 if (!node
->alias
&& node
->analyzed
2086 && (node
->has_gimple_body_p () || node
->thunk
)
2087 && opt_for_fn (node
->decl
, optimize
))
2089 class ipa_fn_summary
*info
= ipa_fn_summaries
->get (node
);
2090 struct ipa_dfs_info
*dfs
= (struct ipa_dfs_info
*) node
->aux
;
2092 /* Do not account external functions, they will be optimized out
2093 if not inlined. Also only count the non-cold portion of program. */
2094 if (inline_account_function_p (node
))
2095 initial_size
+= ipa_size_summaries
->get (node
)->size
;
2096 info
->growth
= estimate_growth (node
);
2099 node
->call_for_symbol_and_aliases (sum_callers
, &num_calls
,
2102 info
->single_caller
= true;
2103 if (dfs
&& dfs
->next_cycle
)
2105 struct cgraph_node
*n2
;
2106 int id
= dfs
->scc_no
+ 1;
2108 n2
= ((struct ipa_dfs_info
*) n2
->aux
)->next_cycle
)
2109 if (opt_for_fn (n2
->decl
, optimize
))
2111 ipa_fn_summary
*info2
= ipa_fn_summaries
->get
2112 (n2
->inlined_to
? n2
->inlined_to
: n2
);
2120 for (edge
= node
->callers
; edge
; edge
= edge
->next_caller
)
2121 max_count
= max_count
.max (edge
->count
.ipa ());
2123 ipa_free_postorder_info ();
2124 initialize_growth_caches ();
2128 "\nDeciding on inlining of small functions. Starting with size %i.\n",
2131 overall_size
= initial_size
;
2132 min_size
= overall_size
;
2134 /* Populate the heap with all edges we might inline. */
2136 FOR_EACH_DEFINED_FUNCTION (node
)
2138 bool update
= false;
2139 struct cgraph_edge
*next
= NULL
;
2140 bool has_speculative
= false;
2142 if (!opt_for_fn (node
->decl
, optimize
)
2143 /* With -Og we do not want to perform IPA inlining of small
2144 functions since there are no scalar cleanups after it
2145 that would realize the anticipated win. All abstraction
2146 is removed during early inlining. */
2147 || opt_for_fn (node
->decl
, optimize_debug
))
2151 fprintf (dump_file
, "Enqueueing calls in %s.\n", node
->dump_name ());
2153 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
2155 if (edge
->inline_failed
2157 && can_inline_edge_p (edge
, true)
2158 && want_inline_small_function_p (edge
, true)
2159 && can_inline_edge_by_limits_p (edge
, CAN_INLINE_REPORT
)
2160 && edge
->inline_failed
)
2162 gcc_assert (!edge
->aux
);
2163 update_edge_key (&edge_heap
, edge
);
2165 if (edge
->speculative
)
2166 has_speculative
= true;
2168 if (has_speculative
)
2169 for (edge
= node
->callees
; edge
; edge
= next
)
2171 next
= edge
->next_callee
;
2172 if (edge
->speculative
2173 && !speculation_useful_p (edge
, edge
->aux
!= NULL
))
2175 cgraph_edge::resolve_speculation (edge
);
2181 struct cgraph_node
*where
= node
->inlined_to
2182 ? node
->inlined_to
: node
;
2183 ipa_update_overall_fn_summary (where
);
2184 reset_edge_caches (where
);
2185 update_caller_keys (&edge_heap
, where
,
2186 updated_nodes
, NULL
);
2187 update_callee_keys (&edge_heap
, where
, NULL
,
2189 bitmap_clear (updated_nodes
);
2193 gcc_assert (in_lto_p
2195 || (profile_info
&& flag_branch_probabilities
));
2197 while (!edge_heap
.empty ())
2199 int old_size
= overall_size
;
2200 struct cgraph_node
*where
, *callee
;
2201 sreal badness
= edge_heap
.min_key ().badness
;
2202 sreal current_badness
;
2205 edge
= edge_heap
.extract_min ();
2206 gcc_assert (edge
->aux
);
2208 if (!edge
->inline_failed
|| !edge
->callee
->analyzed
)
2211 /* Be sure that caches are maintained consistent.
2212 This check is affected by scaling roundoff errors when compiling for
2213 IPA this we skip it in that case. */
2214 if (flag_checking
&& !edge
->callee
->count
.ipa_p ()
2215 && (!max_count
.initialized_p () || !max_count
.nonzero_p ()))
2217 sreal cached_badness
= edge_badness (edge
, false);
2219 int old_size_est
= estimate_edge_size (edge
);
2220 sreal old_time_est
= estimate_edge_time (edge
);
2221 int old_hints_est
= estimate_edge_hints (edge
);
2223 if (edge_growth_cache
!= NULL
)
2224 edge_growth_cache
->remove (edge
);
2225 reset_node_cache (edge
->caller
->inlined_to
2226 ? edge
->caller
->inlined_to
2228 gcc_assert (old_size_est
== estimate_edge_size (edge
));
2229 gcc_assert (old_time_est
== estimate_edge_time (edge
));
2232 gcc_assert (old_hints_est == estimate_edge_hints (edge));
2234 fails with profile feedback because some hints depends on
2235 maybe_hot_edge_p predicate and because callee gets inlined to other
2236 calls, the edge may become cold.
2237 This ought to be fixed by computing relative probabilities
2238 for given invocation but that will be better done once whole
2239 code is converted to sreals. Disable for now and revert to "wrong"
2240 value so enable/disable checking paths agree. */
2241 edge_growth_cache
->get (edge
)->hints
= old_hints_est
+ 1;
2243 /* When updating the edge costs, we only decrease badness in the keys.
2244 Increases of badness are handled lazily; when we see key with out
2245 of date value on it, we re-insert it now. */
2246 current_badness
= edge_badness (edge
, false);
2247 gcc_assert (cached_badness
== current_badness
);
2248 gcc_assert (current_badness
>= badness
);
2251 current_badness
= edge_badness (edge
, false);
2252 if (current_badness
!= badness
)
2254 if (edge_heap
.min () && current_badness
> edge_heap
.min_key ().badness
)
2256 inline_badness
b (edge
, current_badness
);
2257 edge
->aux
= edge_heap
.insert (b
, edge
);
2261 badness
= current_badness
;
2264 if (!can_inline_edge_p (edge
, true)
2265 || !can_inline_edge_by_limits_p (edge
, CAN_INLINE_REPORT
))
2267 resolve_noninline_speculation (&edge_heap
, edge
);
2271 callee
= edge
->callee
->ultimate_alias_target ();
2272 growth
= estimate_edge_growth (edge
);
2276 "\nConsidering %s with %i size\n",
2277 callee
->dump_name (),
2278 ipa_size_summaries
->get (callee
)->size
);
2280 " to be inlined into %s in %s:%i\n"
2281 " Estimated badness is %f, frequency %.2f.\n",
2282 edge
->caller
->dump_name (),
2284 && (LOCATION_LOCUS (gimple_location ((const gimple
*)
2286 > BUILTINS_LOCATION
)
2287 ? gimple_filename ((const gimple
*) edge
->call_stmt
)
2290 ? gimple_lineno ((const gimple
*) edge
->call_stmt
)
2292 badness
.to_double (),
2293 edge
->sreal_frequency ().to_double ());
2294 if (edge
->count
.ipa ().initialized_p ())
2296 fprintf (dump_file
, " Called ");
2297 edge
->count
.ipa ().dump (dump_file
);
2298 fprintf (dump_file
, " times\n");
2300 if (dump_flags
& TDF_DETAILS
)
2301 edge_badness (edge
, true);
2304 where
= edge
->caller
;
2306 if (overall_size
+ growth
> compute_max_insns (where
, min_size
)
2307 && !DECL_DISREGARD_INLINE_LIMITS (callee
->decl
))
2309 edge
->inline_failed
= CIF_INLINE_UNIT_GROWTH_LIMIT
;
2310 report_inline_failed_reason (edge
);
2311 resolve_noninline_speculation (&edge_heap
, edge
);
2315 if (!want_inline_small_function_p (edge
, true))
2317 resolve_noninline_speculation (&edge_heap
, edge
);
2321 profile_count old_count
= callee
->count
;
2323 /* Heuristics for inlining small functions work poorly for
2324 recursive calls where we do effects similar to loop unrolling.
2325 When inlining such edge seems profitable, leave decision on
2326 specific inliner. */
2327 if (edge
->recursive_p ())
2329 if (where
->inlined_to
)
2330 where
= where
->inlined_to
;
2332 /* Disable always_inline on self recursive functions.
2333 This prevents some inlining bombs such as one in PR113291
2335 It is not enough to stop inlining in self recursive always_inlines
2336 since they may grow large enough so always inlining them even
2337 with recursin depth 0 is too much.
2339 All sane uses of always_inline should be handled during
2340 early optimizations. */
2341 DECL_DISREGARD_INLINE_LIMITS (where
->decl
) = false;
2343 if (!recursive_inlining (edge
,
2344 opt_for_fn (edge
->caller
->decl
,
2345 flag_indirect_inlining
)
2346 ? &new_indirect_edges
: NULL
))
2348 edge
->inline_failed
= CIF_RECURSIVE_INLINING
;
2349 resolve_noninline_speculation (&edge_heap
, edge
);
2352 reset_edge_caches (where
);
2353 /* Recursive inliner inlines all recursive calls of the function
2354 at once. Consequently we need to update all callee keys. */
2355 if (opt_for_fn (edge
->caller
->decl
, flag_indirect_inlining
))
2356 add_new_edges_to_heap (&edge_heap
, new_indirect_edges
);
2357 update_callee_keys (&edge_heap
, where
, where
, updated_nodes
);
2358 bitmap_clear (updated_nodes
);
2362 struct cgraph_node
*outer_node
= NULL
;
2365 /* Consider the case where self recursive function A is inlined
2366 into B. This is desired optimization in some cases, since it
2367 leads to effect similar of loop peeling and we might completely
2368 optimize out the recursive call. However we must be extra
2371 where
= edge
->caller
;
2372 while (where
->inlined_to
)
2374 if (where
->decl
== callee
->decl
)
2375 outer_node
= where
, depth
++;
2376 where
= where
->callers
->caller
;
2379 && !want_inline_self_recursive_call_p (edge
, outer_node
,
2383 = (DECL_DISREGARD_INLINE_LIMITS (edge
->callee
->decl
)
2384 ? CIF_RECURSIVE_INLINING
: CIF_UNSPECIFIED
);
2385 resolve_noninline_speculation (&edge_heap
, edge
);
2388 else if (depth
&& dump_file
)
2389 fprintf (dump_file
, " Peeling recursion with depth %i\n", depth
);
2391 gcc_checking_assert (!callee
->inlined_to
);
2393 int old_size
= ipa_size_summaries
->get (where
)->size
;
2394 sreal old_time
= ipa_fn_summaries
->get (where
)->time
;
2396 inline_call (edge
, true, &new_indirect_edges
, &overall_size
, true);
2397 reset_edge_caches (edge
->callee
);
2398 add_new_edges_to_heap (&edge_heap
, new_indirect_edges
);
2400 /* If caller's size and time increased we do not need to update
2401 all edges because badness is not going to decrease. */
2402 if (old_size
<= ipa_size_summaries
->get (where
)->size
2403 && old_time
<= ipa_fn_summaries
->get (where
)->time
2404 /* Wrapper penalty may be non-monotonous in this respect.
2405 Fortunately it only affects small functions. */
2406 && !wrapper_heuristics_may_apply (where
, old_size
))
2407 update_callee_keys (&edge_heap
, edge
->callee
, edge
->callee
,
2410 update_callee_keys (&edge_heap
, where
,
2414 where
= edge
->caller
;
2415 if (where
->inlined_to
)
2416 where
= where
->inlined_to
;
2418 /* Our profitability metric can depend on local properties
2419 such as number of inlinable calls and size of the function body.
2420 After inlining these properties might change for the function we
2421 inlined into (since it's body size changed) and for the functions
2422 called by function we inlined (since number of it inlinable callers
2424 update_caller_keys (&edge_heap
, where
, updated_nodes
, NULL
);
2425 /* Offline copy count has possibly changed, recompute if profile is
2427 struct cgraph_node
*n
2428 = cgraph_node::get (edge
->callee
->decl
)->ultimate_alias_target ();
2429 if (n
!= edge
->callee
&& n
->analyzed
&& !(n
->count
== old_count
)
2430 && n
->count
.ipa_p ())
2431 update_callee_keys (&edge_heap
, n
, NULL
, updated_nodes
);
2432 bitmap_clear (updated_nodes
);
2434 if (dump_enabled_p ())
2436 ipa_fn_summary
*s
= ipa_fn_summaries
->get (where
);
2438 /* dump_printf can't handle %+i. */
2439 char buf_net_change
[100];
2440 snprintf (buf_net_change
, sizeof buf_net_change
, "%+i",
2441 overall_size
- old_size
);
2443 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, edge
->call_stmt
,
2444 " Inlined %C into %C which now has time %f and "
2445 "size %i, net change of %s%s.\n",
2446 edge
->callee
, edge
->caller
,
2447 s
->time
.to_double (),
2448 ipa_size_summaries
->get (edge
->caller
)->size
,
2450 cross_module_call_p (edge
) ? " (cross module)":"");
2452 if (min_size
> overall_size
)
2454 min_size
= overall_size
;
2457 fprintf (dump_file
, "New minimal size reached: %i\n", min_size
);
2461 free_growth_caches ();
2462 if (dump_enabled_p ())
2463 dump_printf (MSG_NOTE
,
2464 "Unit growth for small function inlining: %i->%i (%i%%)\n",
2465 initial_size
, overall_size
,
2466 initial_size
? overall_size
* 100 / (initial_size
) - 100: 0);
2467 symtab
->remove_edge_removal_hook (edge_removal_hook_holder
);
2470 /* Flatten NODE. Performed both during early inlining and
2471 at IPA inlining time. */
2474 flatten_function (struct cgraph_node
*node
, bool early
, bool update
)
2476 struct cgraph_edge
*e
;
2478 /* We shouldn't be called recursively when we are being processed. */
2479 gcc_assert (node
->aux
== NULL
);
2481 node
->aux
= (void *) node
;
2483 for (e
= node
->callees
; e
; e
= e
->next_callee
)
2485 struct cgraph_node
*orig_callee
;
2486 struct cgraph_node
*callee
= e
->callee
->ultimate_alias_target ();
2488 /* We've hit cycle? It is time to give up. */
2491 if (dump_enabled_p ())
2492 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, e
->call_stmt
,
2493 "Not inlining %C into %C to avoid cycle.\n",
2495 if (cgraph_inline_failed_type (e
->inline_failed
) != CIF_FINAL_ERROR
)
2496 e
->inline_failed
= CIF_RECURSIVE_INLINING
;
2500 /* When the edge is already inlined, we just need to recurse into
2501 it in order to fully flatten the leaves. */
2502 if (!e
->inline_failed
)
2504 flatten_function (callee
, early
, false);
2508 /* Flatten attribute needs to be processed during late inlining. For
2509 extra code quality we however do flattening during early optimization,
2512 ? !can_inline_edge_p (e
, true)
2513 && !can_inline_edge_by_limits_p (e
, CAN_INLINE_REPORT
)
2514 : !can_early_inline_edge_p (e
))
2517 if (e
->recursive_p ())
2519 if (dump_enabled_p ())
2520 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, e
->call_stmt
,
2521 "Not inlining: recursive call.\n");
2525 if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node
->decl
))
2526 != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee
->decl
)))
2528 if (dump_enabled_p ())
2529 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, e
->call_stmt
,
2530 "Not inlining: SSA form does not match.\n");
2534 /* Inline the edge and flatten the inline clone. Avoid
2535 recursing through the original node if the node was cloned. */
2536 if (dump_enabled_p ())
2537 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, e
->call_stmt
,
2538 " Inlining %C into %C.\n",
2540 orig_callee
= callee
;
2541 inline_call (e
, true, NULL
, NULL
, false);
2542 if (e
->callee
!= orig_callee
)
2543 orig_callee
->aux
= (void *) node
;
2544 flatten_function (e
->callee
, early
, false);
2545 if (e
->callee
!= orig_callee
)
2546 orig_callee
->aux
= NULL
;
2550 cgraph_node
*where
= node
->inlined_to
? node
->inlined_to
: node
;
2551 if (update
&& opt_for_fn (where
->decl
, optimize
))
2552 ipa_update_overall_fn_summary (where
);
2555 /* Inline NODE to all callers. Worker for cgraph_for_node_and_aliases.
2556 DATA points to number of calls originally found so we avoid infinite
2560 inline_to_all_callers_1 (struct cgraph_node
*node
, void *data
,
2561 hash_set
<cgraph_node
*> *callers
)
2563 int *num_calls
= (int *)data
;
2564 bool callee_removed
= false;
2566 while (node
->callers
&& !node
->inlined_to
)
2568 struct cgraph_node
*caller
= node
->callers
->caller
;
2570 if (!can_inline_edge_p (node
->callers
, true)
2571 || !can_inline_edge_by_limits_p (node
->callers
, CAN_INLINE_REPORT
)
2572 || node
->callers
->recursive_p ())
2575 fprintf (dump_file
, "Uninlinable call found; giving up.\n");
2582 cgraph_node
*ultimate
= node
->ultimate_alias_target ();
2584 "\nInlining %s size %i.\n",
2585 ultimate
->dump_name (),
2586 ipa_size_summaries
->get (ultimate
)->size
);
2588 " Called once from %s %i insns.\n",
2589 node
->callers
->caller
->dump_name (),
2590 ipa_size_summaries
->get (node
->callers
->caller
)->size
);
2593 /* Remember which callers we inlined to, delaying updating the
2595 callers
->add (node
->callers
->caller
);
2596 inline_call (node
->callers
, true, NULL
, NULL
, false, &callee_removed
);
2599 " Inlined into %s which now has %i size\n",
2600 caller
->dump_name (),
2601 ipa_size_summaries
->get (caller
)->size
);
2602 if (!(*num_calls
)--)
2605 fprintf (dump_file
, "New calls found; giving up.\n");
2606 return callee_removed
;
2614 /* Wrapper around inline_to_all_callers_1 doing delayed overall summary
2618 inline_to_all_callers (struct cgraph_node
*node
, void *data
)
2620 hash_set
<cgraph_node
*> callers
;
2621 bool res
= inline_to_all_callers_1 (node
, data
, &callers
);
2622 /* Perform the delayed update of the overall summary of all callers
2623 processed. This avoids quadratic behavior in the cases where
2624 we have a lot of calls to the same function. */
2625 for (hash_set
<cgraph_node
*>::iterator i
= callers
.begin ();
2626 i
!= callers
.end (); ++i
)
2627 ipa_update_overall_fn_summary ((*i
)->inlined_to
? (*i
)->inlined_to
: *i
);
2631 /* Output overall time estimate. */
2633 dump_overall_stats (void)
2635 sreal sum_weighted
= 0, sum
= 0;
2636 struct cgraph_node
*node
;
2638 FOR_EACH_DEFINED_FUNCTION (node
)
2639 if (!node
->inlined_to
2642 ipa_fn_summary
*s
= ipa_fn_summaries
->get (node
);
2646 if (node
->count
.ipa ().initialized_p ())
2647 sum_weighted
+= s
->time
* node
->count
.ipa ().to_gcov_type ();
2650 fprintf (dump_file
, "Overall time estimate: "
2651 "%f weighted by profile: "
2652 "%f\n", sum
.to_double (), sum_weighted
.to_double ());
2655 /* Output some useful stats about inlining. */
2658 dump_inline_stats (void)
2660 int64_t inlined_cnt
= 0, inlined_indir_cnt
= 0;
2661 int64_t inlined_virt_cnt
= 0, inlined_virt_indir_cnt
= 0;
2662 int64_t noninlined_cnt
= 0, noninlined_indir_cnt
= 0;
2663 int64_t noninlined_virt_cnt
= 0, noninlined_virt_indir_cnt
= 0;
2664 int64_t inlined_speculative
= 0, inlined_speculative_ply
= 0;
2665 int64_t indirect_poly_cnt
= 0, indirect_cnt
= 0;
2666 int64_t reason
[CIF_N_REASONS
][2];
2667 sreal reason_freq
[CIF_N_REASONS
];
2669 struct cgraph_node
*node
;
2671 memset (reason
, 0, sizeof (reason
));
2672 for (i
=0; i
< CIF_N_REASONS
; i
++)
2674 FOR_EACH_DEFINED_FUNCTION (node
)
2676 struct cgraph_edge
*e
;
2677 for (e
= node
->callees
; e
; e
= e
->next_callee
)
2679 if (e
->inline_failed
)
2681 if (e
->count
.ipa ().initialized_p ())
2682 reason
[(int) e
->inline_failed
][0] += e
->count
.ipa ().to_gcov_type ();
2683 reason_freq
[(int) e
->inline_failed
] += e
->sreal_frequency ();
2684 reason
[(int) e
->inline_failed
][1] ++;
2685 if (DECL_VIRTUAL_P (e
->callee
->decl
)
2686 && e
->count
.ipa ().initialized_p ())
2688 if (e
->indirect_inlining_edge
)
2689 noninlined_virt_indir_cnt
+= e
->count
.ipa ().to_gcov_type ();
2691 noninlined_virt_cnt
+= e
->count
.ipa ().to_gcov_type ();
2693 else if (e
->count
.ipa ().initialized_p ())
2695 if (e
->indirect_inlining_edge
)
2696 noninlined_indir_cnt
+= e
->count
.ipa ().to_gcov_type ();
2698 noninlined_cnt
+= e
->count
.ipa ().to_gcov_type ();
2701 else if (e
->count
.ipa ().initialized_p ())
2705 if (DECL_VIRTUAL_P (e
->callee
->decl
))
2706 inlined_speculative_ply
+= e
->count
.ipa ().to_gcov_type ();
2708 inlined_speculative
+= e
->count
.ipa ().to_gcov_type ();
2710 else if (DECL_VIRTUAL_P (e
->callee
->decl
))
2712 if (e
->indirect_inlining_edge
)
2713 inlined_virt_indir_cnt
+= e
->count
.ipa ().to_gcov_type ();
2715 inlined_virt_cnt
+= e
->count
.ipa ().to_gcov_type ();
2719 if (e
->indirect_inlining_edge
)
2720 inlined_indir_cnt
+= e
->count
.ipa ().to_gcov_type ();
2722 inlined_cnt
+= e
->count
.ipa ().to_gcov_type ();
2726 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
2727 if (e
->indirect_info
->polymorphic
2728 & e
->count
.ipa ().initialized_p ())
2729 indirect_poly_cnt
+= e
->count
.ipa ().to_gcov_type ();
2730 else if (e
->count
.ipa ().initialized_p ())
2731 indirect_cnt
+= e
->count
.ipa ().to_gcov_type ();
2733 if (max_count
.initialized_p ())
2736 "Inlined %" PRId64
" + speculative "
2737 "%" PRId64
" + speculative polymorphic "
2738 "%" PRId64
" + previously indirect "
2739 "%" PRId64
" + virtual "
2740 "%" PRId64
" + virtual and previously indirect "
2741 "%" PRId64
"\n" "Not inlined "
2742 "%" PRId64
" + previously indirect "
2743 "%" PRId64
" + virtual "
2744 "%" PRId64
" + virtual and previously indirect "
2745 "%" PRId64
" + still indirect "
2746 "%" PRId64
" + still indirect polymorphic "
2747 "%" PRId64
"\n", inlined_cnt
,
2748 inlined_speculative
, inlined_speculative_ply
,
2749 inlined_indir_cnt
, inlined_virt_cnt
, inlined_virt_indir_cnt
,
2750 noninlined_cnt
, noninlined_indir_cnt
, noninlined_virt_cnt
,
2751 noninlined_virt_indir_cnt
, indirect_cnt
, indirect_poly_cnt
);
2752 fprintf (dump_file
, "Removed speculations ");
2753 spec_rem
.dump (dump_file
);
2754 fprintf (dump_file
, "\n");
2756 dump_overall_stats ();
2757 fprintf (dump_file
, "\nWhy inlining failed?\n");
2758 for (i
= 0; i
< CIF_N_REASONS
; i
++)
2760 fprintf (dump_file
, "%-50s: %8i calls, %8f freq, %" PRId64
" count\n",
2761 cgraph_inline_failed_string ((cgraph_inline_failed_t
) i
),
2762 (int) reason
[i
][1], reason_freq
[i
].to_double (), reason
[i
][0]);
2765 /* Called when node is removed. */
2768 flatten_remove_node_hook (struct cgraph_node
*node
, void *data
)
2770 if (lookup_attribute ("flatten", DECL_ATTRIBUTES (node
->decl
)) == NULL
)
2773 hash_set
<struct cgraph_node
*> *removed
2774 = (hash_set
<struct cgraph_node
*> *) data
;
2775 removed
->add (node
);
2778 /* Decide on the inlining. We do so in the topological order to avoid
2779 expenses on updating data structures. */
2784 struct cgraph_node
*node
;
2786 struct cgraph_node
**order
;
2789 bool remove_functions
= false;
2791 order
= XCNEWVEC (struct cgraph_node
*, symtab
->cgraph_count
);
2794 ipa_dump_fn_summaries (dump_file
);
2796 nnodes
= ipa_reverse_postorder (order
);
2797 spec_rem
= profile_count::zero ();
2799 FOR_EACH_FUNCTION (node
)
2803 /* Recompute the default reasons for inlining because they may have
2804 changed during merging. */
2807 for (cgraph_edge
*e
= node
->callees
; e
; e
= e
->next_callee
)
2809 gcc_assert (e
->inline_failed
);
2810 initialize_inline_failed (e
);
2812 for (cgraph_edge
*e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
2813 initialize_inline_failed (e
);
2818 fprintf (dump_file
, "\nFlattening functions:\n");
2820 /* First shrink order array, so that it only contains nodes with
2821 flatten attribute. */
2822 for (i
= nnodes
- 1, j
= i
; i
>= 0; i
--)
2825 if (node
->definition
2826 /* Do not try to flatten aliases. These may happen for example when
2827 creating local aliases. */
2829 && lookup_attribute ("flatten",
2830 DECL_ATTRIBUTES (node
->decl
)) != NULL
)
2831 order
[j
--] = order
[i
];
2834 /* After the above loop, order[j + 1] ... order[nnodes - 1] contain
2835 nodes with flatten attribute. If there is more than one such
2836 node, we need to register a node removal hook, as flatten_function
2837 could remove other nodes with flatten attribute. See PR82801. */
2838 struct cgraph_node_hook_list
*node_removal_hook_holder
= NULL
;
2839 hash_set
<struct cgraph_node
*> *flatten_removed_nodes
= NULL
;
2842 flatten_removed_nodes
= new hash_set
<struct cgraph_node
*>;
2843 node_removal_hook_holder
2844 = symtab
->add_cgraph_removal_hook (&flatten_remove_node_hook
,
2845 flatten_removed_nodes
);
2848 /* In the first pass handle functions to be flattened. Do this with
2849 a priority so none of our later choices will make this impossible. */
2850 for (i
= nnodes
- 1; i
> j
; i
--)
2853 if (flatten_removed_nodes
2854 && flatten_removed_nodes
->contains (node
))
2857 /* Handle nodes to be flattened.
2858 Ideally when processing callees we stop inlining at the
2859 entry of cycles, possibly cloning that entry point and
2860 try to flatten itself turning it into a self-recursive
2863 fprintf (dump_file
, "Flattening %s\n", node
->dump_name ());
2864 flatten_function (node
, false, true);
2869 symtab
->remove_cgraph_removal_hook (node_removal_hook_holder
);
2870 delete flatten_removed_nodes
;
2875 dump_overall_stats ();
2877 inline_small_functions ();
2879 gcc_assert (symtab
->state
== IPA_SSA
);
2880 symtab
->state
= IPA_SSA_AFTER_INLINING
;
2881 /* Do first after-inlining removal. We want to remove all "stale" extern
2882 inline functions and virtual functions so we really know what is called
2884 symtab
->remove_unreachable_nodes (dump_file
);
2886 /* Inline functions with a property that after inlining into all callers the
2887 code size will shrink because the out-of-line copy is eliminated.
2888 We do this regardless on the callee size as long as function growth limits
2892 "\nDeciding on functions to be inlined into all callers and "
2893 "removing useless speculations:\n");
2895 /* Inlining one function called once has good chance of preventing
2896 inlining other function into the same callee. Ideally we should
2897 work in priority order, but probably inlining hot functions first
2898 is good cut without the extra pain of maintaining the queue.
2900 ??? this is not really fitting the bill perfectly: inlining function
2901 into callee often leads to better optimization of callee due to
2902 increased context for optimization.
2903 For example if main() function calls a function that outputs help
2904 and then function that does the main optimization, we should inline
2905 the second with priority even if both calls are cold by themselves.
2907 We probably want to implement new predicate replacing our use of
2908 maybe_hot_edge interpreted as maybe_hot_edge || callee is known
2910 for (cold
= 0; cold
<= 1; cold
++)
2912 FOR_EACH_DEFINED_FUNCTION (node
)
2914 struct cgraph_edge
*edge
, *next
;
2917 if (!opt_for_fn (node
->decl
, optimize
)
2918 || !opt_for_fn (node
->decl
, flag_inline_functions_called_once
))
2921 for (edge
= node
->callees
; edge
; edge
= next
)
2923 next
= edge
->next_callee
;
2924 if (edge
->speculative
&& !speculation_useful_p (edge
, false))
2926 if (edge
->count
.ipa ().initialized_p ())
2927 spec_rem
+= edge
->count
.ipa ();
2928 cgraph_edge::resolve_speculation (edge
);
2930 remove_functions
= true;
2935 struct cgraph_node
*where
= node
->inlined_to
2936 ? node
->inlined_to
: node
;
2937 reset_edge_caches (where
);
2938 ipa_update_overall_fn_summary (where
);
2940 if (want_inline_function_to_all_callers_p (node
, cold
))
2943 node
->call_for_symbol_and_aliases (sum_callers
, &num_calls
,
2945 while (node
->call_for_symbol_and_aliases
2946 (inline_to_all_callers
, &num_calls
, true))
2948 remove_functions
= true;
2953 if (dump_enabled_p ())
2954 dump_printf (MSG_NOTE
,
2955 "\nInlined %i calls, eliminated %i functions\n\n",
2956 ncalls_inlined
, nfunctions_inlined
);
2958 dump_inline_stats ();
2961 ipa_dump_fn_summaries (dump_file
);
2962 return remove_functions
? TODO_remove_functions
: 0;
2965 /* Inline always-inline function calls in NODE
2966 (which itself is possibly inline). */
2969 inline_always_inline_functions (struct cgraph_node
*node
)
2971 struct cgraph_edge
*e
;
2972 bool inlined
= false;
2974 for (e
= node
->callees
; e
; e
= e
->next_callee
)
2976 struct cgraph_node
*callee
= e
->callee
->ultimate_alias_target ();
2977 gcc_checking_assert (!callee
->aux
|| callee
->aux
== (void *)(size_t)1);
2978 if (!DECL_DISREGARD_INLINE_LIMITS (callee
->decl
)
2979 /* Watch for self-recursive cycles. */
2983 if (e
->recursive_p ())
2985 if (dump_enabled_p ())
2986 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, e
->call_stmt
,
2987 " Not inlining recursive call to %C.\n",
2989 e
->inline_failed
= CIF_RECURSIVE_INLINING
;
2992 if (callee
->definition
2993 && !ipa_fn_summaries
->get (callee
))
2994 compute_fn_summary (callee
, true);
2996 if (!can_early_inline_edge_p (e
))
2998 /* Set inlined to true if the callee is marked "always_inline" but
2999 is not inlinable. This will allow flagging an error later in
3000 expand_call_inline in tree-inline.cc. */
3001 if (lookup_attribute ("always_inline",
3002 DECL_ATTRIBUTES (callee
->decl
)) != NULL
)
3007 if (dump_enabled_p ())
3008 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, e
->call_stmt
,
3009 " Inlining %C into %C (always_inline).\n",
3010 e
->callee
, e
->caller
);
3011 inline_call (e
, true, NULL
, NULL
, false);
3012 callee
->aux
= (void *)(size_t)1;
3013 /* Inline recursively to handle the case where always_inline function was
3014 not optimized yet since it is a part of a cycle in callgraph. */
3015 inline_always_inline_functions (e
->callee
);
3022 /* Decide on the inlining. We do so in the topological order to avoid
3023 expenses on updating data structures. */
3026 early_inline_small_functions (struct cgraph_node
*node
)
3028 struct cgraph_edge
*e
;
3029 bool inlined
= false;
3031 for (e
= node
->callees
; e
; e
= e
->next_callee
)
3033 struct cgraph_node
*callee
= e
->callee
->ultimate_alias_target ();
3035 /* We can encounter not-yet-analyzed function during
3036 early inlining on callgraphs with strongly
3037 connected components. */
3038 ipa_fn_summary
*s
= ipa_fn_summaries
->get (callee
);
3039 if (s
== NULL
|| !s
->inlinable
|| !e
->inline_failed
)
3042 /* Do not consider functions not declared inline. */
3043 if (!DECL_DECLARED_INLINE_P (callee
->decl
)
3044 && !opt_for_fn (node
->decl
, flag_inline_small_functions
)
3045 && !opt_for_fn (node
->decl
, flag_inline_functions
))
3048 if (dump_enabled_p ())
3049 dump_printf_loc (MSG_NOTE
, e
->call_stmt
,
3050 "Considering inline candidate %C.\n",
3053 if (!can_early_inline_edge_p (e
))
3056 if (e
->recursive_p ())
3058 if (dump_enabled_p ())
3059 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, e
->call_stmt
,
3060 " Not inlining: recursive call.\n");
3064 if (!want_early_inline_function_p (e
))
3067 if (dump_enabled_p ())
3068 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, e
->call_stmt
,
3069 " Inlining %C into %C.\n",
3071 inline_call (e
, true, NULL
, NULL
, false);
3076 ipa_update_overall_fn_summary (node
);
3082 early_inliner (function
*fun
)
3084 struct cgraph_node
*node
= cgraph_node::get (current_function_decl
);
3085 struct cgraph_edge
*edge
;
3086 unsigned int todo
= 0;
3088 bool inlined
= false;
3093 /* Do nothing if datastructures for ipa-inliner are already computed. This
3094 happens when some pass decides to construct new function and
3095 cgraph_add_new_function calls lowering passes and early optimization on
3096 it. This may confuse ourself when early inliner decide to inline call to
3097 function clone, because function clones don't have parameter list in
3098 ipa-prop matching their signature. */
3099 if (ipa_node_params_sum
)
3104 node
->remove_all_references ();
3106 /* Even when not optimizing or not inlining inline always-inline
3108 inlined
= inline_always_inline_functions (node
);
3112 || !flag_early_inlining
)
3114 else if (lookup_attribute ("flatten",
3115 DECL_ATTRIBUTES (node
->decl
)) != NULL
)
3117 /* When the function is marked to be flattened, recursively inline
3119 if (dump_enabled_p ())
3120 dump_printf (MSG_OPTIMIZED_LOCATIONS
,
3121 "Flattening %C\n", node
);
3122 flatten_function (node
, true, true);
3127 /* If some always_inline functions was inlined, apply the changes.
3128 This way we will not account always inline into growth limits and
3129 moreover we will inline calls from always inlines that we skipped
3130 previously because of conditional in can_early_inline_edge_p
3131 which prevents some inlining to always_inline. */
3134 timevar_push (TV_INTEGRATION
);
3135 todo
|= optimize_inline_calls (current_function_decl
);
3136 /* optimize_inline_calls call above might have introduced new
3137 statements that don't have inline parameters computed. */
3138 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
3140 /* We can enounter not-yet-analyzed function during
3141 early inlining on callgraphs with strongly
3142 connected components. */
3143 ipa_call_summary
*es
= ipa_call_summaries
->get_create (edge
);
3145 = estimate_num_insns (edge
->call_stmt
, &eni_size_weights
);
3147 = estimate_num_insns (edge
->call_stmt
, &eni_time_weights
);
3149 ipa_update_overall_fn_summary (node
);
3151 timevar_pop (TV_INTEGRATION
);
3153 /* We iterate incremental inlining to get trivial cases of indirect
3155 while (iterations
< opt_for_fn (node
->decl
,
3156 param_early_inliner_max_iterations
)
3157 && early_inline_small_functions (node
))
3159 timevar_push (TV_INTEGRATION
);
3160 todo
|= optimize_inline_calls (current_function_decl
);
3162 /* Technically we ought to recompute inline parameters so the new
3163 iteration of early inliner works as expected. We however have
3164 values approximately right and thus we only need to update edge
3165 info that might be cleared out for newly discovered edges. */
3166 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
3168 /* We have no summary for new bound store calls yet. */
3169 ipa_call_summary
*es
= ipa_call_summaries
->get_create (edge
);
3171 = estimate_num_insns (edge
->call_stmt
, &eni_size_weights
);
3173 = estimate_num_insns (edge
->call_stmt
, &eni_time_weights
);
3175 if (iterations
< opt_for_fn (node
->decl
,
3176 param_early_inliner_max_iterations
) - 1)
3177 ipa_update_overall_fn_summary (node
);
3178 timevar_pop (TV_INTEGRATION
);
3183 fprintf (dump_file
, "Iterations: %i\n", iterations
);
3188 timevar_push (TV_INTEGRATION
);
3189 todo
|= optimize_inline_calls (current_function_decl
);
3190 timevar_pop (TV_INTEGRATION
);
3193 fun
->always_inline_functions_inlined
= true;
3198 /* Do inlining of small functions. Doing so early helps profiling and other
3199 passes to be somewhat more effective and avoids some code duplication in
3200 later real inlining pass for testcases with very many function calls. */
3204 const pass_data pass_data_early_inline
=
3206 GIMPLE_PASS
, /* type */
3207 "einline", /* name */
3208 OPTGROUP_INLINE
, /* optinfo_flags */
3209 TV_EARLY_INLINING
, /* tv_id */
3210 PROP_ssa
, /* properties_required */
3211 0, /* properties_provided */
3212 0, /* properties_destroyed */
3213 0, /* todo_flags_start */
3214 0, /* todo_flags_finish */
3217 class pass_early_inline
: public gimple_opt_pass
3220 pass_early_inline (gcc::context
*ctxt
)
3221 : gimple_opt_pass (pass_data_early_inline
, ctxt
)
3224 /* opt_pass methods: */
3225 unsigned int execute (function
*) final override
;
3227 }; // class pass_early_inline
3230 pass_early_inline::execute (function
*fun
)
3232 return early_inliner (fun
);
3238 make_pass_early_inline (gcc::context
*ctxt
)
3240 return new pass_early_inline (ctxt
);
3245 const pass_data pass_data_ipa_inline
=
3247 IPA_PASS
, /* type */
3248 "inline", /* name */
3249 OPTGROUP_INLINE
, /* optinfo_flags */
3250 TV_IPA_INLINING
, /* tv_id */
3251 0, /* properties_required */
3252 0, /* properties_provided */
3253 0, /* properties_destroyed */
3254 0, /* todo_flags_start */
3255 ( TODO_dump_symtab
), /* todo_flags_finish */
3258 class pass_ipa_inline
: public ipa_opt_pass_d
3261 pass_ipa_inline (gcc::context
*ctxt
)
3262 : ipa_opt_pass_d (pass_data_ipa_inline
, ctxt
,
3263 NULL
, /* generate_summary */
3264 NULL
, /* write_summary */
3265 NULL
, /* read_summary */
3266 NULL
, /* write_optimization_summary */
3267 NULL
, /* read_optimization_summary */
3268 NULL
, /* stmt_fixup */
3269 0, /* function_transform_todo_flags_start */
3270 inline_transform
, /* function_transform */
3271 NULL
) /* variable_transform */
3274 /* opt_pass methods: */
3275 unsigned int execute (function
*) final override
{ return ipa_inline (); }
3277 }; // class pass_ipa_inline
3282 make_pass_ipa_inline (gcc::context
*ctxt
)
3284 return new pass_ipa_inline (ctxt
);