]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ipa-inline.c
switch from gimple to gimple*
[thirdparty/gcc.git] / gcc / ipa-inline.c
CommitLineData
ca31b95f 1/* Inlining decision heuristics.
5624e564 2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
ca31b95f
JH
3 Contributed by Jan Hubicka
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9dcd6f09 9Software Foundation; either version 3, or (at your option) any later
ca31b95f
JH
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
ca31b95f
JH
20
21/* Inlining decision heuristics
22
4c0f7679 23 The implementation of inliner is organized as follows:
ca31b95f 24
ca31b95f
JH
25 inlining heuristics limits
26
4c0f7679
JH
27 can_inline_edge_p allow to check that particular inlining is allowed
28 by the limits specified by user (allowed function growth, growth and so
29 on).
30
31 Functions are inlined when it is obvious the result is profitable (such
32 as functions called once or when inlining reduce code size).
33 In addition to that we perform inlining of small functions and recursive
34 inlining.
ca31b95f
JH
35
36 inlining heuristics
37
4c0f7679
JH
38 The inliner itself is split into two passes:
39
40 pass_early_inlining
ca31b95f 41
4c0f7679
JH
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.
ca31b95f 45
4c0f7679
JH
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
09a2806f 50 and results of early inlining adds a lot of optimization opportunities
4c0f7679 51 for the local optimization.
ca31b95f 52
09a2806f 53 The pass handle the obvious inlining decisions within the compilation
4c0f7679
JH
54 unit - inlining auto inline functions, inlining for size and
55 flattening.
ca31b95f 56
4c0f7679
JH
57 main strength of the pass is the ability to eliminate abstraction
58 penalty in C++ code (via combination of inlining and early
59 optimization) and thus improve quality of analysis done by real IPA
60 optimizers.
873aa8f5 61
4c0f7679
JH
62 Because of lack of whole unit knowledge, the pass can not really make
63 good code size/performance tradeoffs. It however does very simple
64 speculative inlining allowing code size to grow by
09a2806f
JH
65 EARLY_INLINING_INSNS when callee is leaf function. In this case the
66 optimizations performed later are very likely to eliminate the cost.
873aa8f5 67
4c0f7679 68 pass_ipa_inline
873aa8f5 69
4c0f7679
JH
70 This is the real inliner able to handle inlining with whole program
71 knowledge. It performs following steps:
873aa8f5 72
4c0f7679
JH
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.
873aa8f5 76
4c0f7679
JH
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.
873aa8f5 81
4c0f7679
JH
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.
87
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. */
ca31b95f
JH
91
92#include "config.h"
93#include "system.h"
94#include "coretypes.h"
c7131fb2 95#include "backend.h"
ca31b95f 96#include "tree.h"
c7131fb2
AM
97#include "gimple.h"
98#include "rtl.h"
99#include "alias.h"
40e23961 100#include "fold-const.h"
d8a2d370
DN
101#include "trans-mem.h"
102#include "calls.h"
ca31b95f
JH
103#include "tree-inline.h"
104#include "langhooks.h"
105#include "flags.h"
ca31b95f 106#include "diagnostic.h"
cf835838 107#include "gimple-pretty-print.h"
ca31b95f 108#include "params.h"
ca31b95f
JH
109#include "intl.h"
110#include "tree-pass.h"
670cd5c5 111#include "coverage.h"
59f2e9d8 112#include "profile.h"
2fb9a547 113#include "internal-fn.h"
442b4905 114#include "gimple-ssa.h"
c582198b
AM
115#include "cgraph.h"
116#include "alloc-pool.h"
dd912cb8 117#include "symbol-summary.h"
3e293154 118#include "ipa-prop.h"
85057983 119#include "except.h"
4c0f7679 120#include "target.h"
03dfc36d 121#include "ipa-inline.h"
af8bca3c 122#include "ipa-utils.h"
1b08b734 123#include "sreal.h"
be3c16c4 124#include "auto-profile.h"
9b2b7279 125#include "builtins.h"
4a910049 126#include "fibonacci_heap.h"
bb1e543c 127#include "lto-streamer.h"
4a910049 128
f0e1509b
ML
129typedef fibonacci_heap <sreal, cgraph_edge> edge_heap_t;
130typedef fibonacci_node <sreal, cgraph_edge> edge_heap_node_t;
85057983 131
ca31b95f 132/* Statistics we collect about inlining algorithm. */
85057983 133static int overall_size;
632b4f8e 134static gcov_type max_count;
e86a910f 135static gcov_type spec_rem;
ca31b95f 136
6d4ab5f8
JH
137/* Pre-computed constants 1/CGRAPH_FREQ_BASE and 1/100. */
138static sreal cgraph_freq_base_rec, percent_rec;
139
4c0f7679
JH
140/* Return false when inlining edge E would lead to violating
141 limits on function unit growth or stack usage growth.
142
143 The relative function body growth limit is present generally
09a2806f 144 to avoid problems with non-linear behavior of the compiler.
4c0f7679
JH
145 To allow inlining huge functions into tiny wrapper, the limit
146 is always based on the bigger of the two functions considered.
147
148 For stack growth limits we always base the growth in stack usage
149 of the callers. We want to prevent applications from segfaulting
150 on stack overflow when functions with huge stack frames gets
151 inlined. */
ca31b95f
JH
152
153static bool
4c0f7679 154caller_growth_limits (struct cgraph_edge *e)
ca31b95f 155{
d7d1d041 156 struct cgraph_node *to = e->caller;
d52f5295 157 struct cgraph_node *what = e->callee->ultimate_alias_target ();
ca31b95f 158 int newsize;
4c0f7679
JH
159 int limit = 0;
160 HOST_WIDE_INT stack_size_limit = 0, inlined_stack;
9a1e784a 161 inline_summary *info, *what_info, *outer_info = inline_summaries->get (to);
4c0f7679
JH
162
163 /* Look for function e->caller is inlined to. While doing
164 so work out the largest function body on the way. As
165 described above, we want to base our function growth
166 limits based on that. Not on the self size of the
167 outer function, not on the self size of inline code
168 we immediately inline to. This is the most relaxed
169 interpretation of the rule "do not grow large functions
170 too much in order to prevent compiler from exploding". */
09dfe187 171 while (true)
4c0f7679 172 {
9a1e784a 173 info = inline_summaries->get (to);
4c0f7679
JH
174 if (limit < info->self_size)
175 limit = info->self_size;
176 if (stack_size_limit < info->estimated_self_stack_size)
177 stack_size_limit = info->estimated_self_stack_size;
178 if (to->global.inlined_to)
179 to = to->callers->caller;
09dfe187
JH
180 else
181 break;
4c0f7679 182 }
6971d714 183
9a1e784a 184 what_info = inline_summaries->get (what);
e7f23018 185
4c0f7679 186 if (limit < what_info->self_size)
e7f23018 187 limit = what_info->self_size;
ca31b95f
JH
188
189 limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
190
6971d714
RG
191 /* Check the size after inlining against the function limits. But allow
192 the function to shrink if it went over the limits by forced inlining. */
03dfc36d 193 newsize = estimate_size_after_inlining (to, e);
e7f23018 194 if (newsize >= info->size
6971d714 195 && newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
ca31b95f
JH
196 && newsize > limit)
197 {
4c0f7679 198 e->inline_failed = CIF_LARGE_FUNCTION_GROWTH_LIMIT;
ca31b95f
JH
199 return false;
200 }
ff28a94d 201
09dfe187
JH
202 if (!what_info->estimated_stack_size)
203 return true;
204
09a2806f
JH
205 /* FIXME: Stack size limit often prevents inlining in Fortran programs
206 due to large i/o datastructures used by the Fortran front-end.
4c0f7679
JH
207 We ought to ignore this limit when we know that the edge is executed
208 on every invocation of the caller (i.e. its call statement dominates
209 exit block). We do not track this information, yet. */
09dfe187 210 stack_size_limit += ((gcov_type)stack_size_limit
4c0f7679 211 * PARAM_VALUE (PARAM_STACK_FRAME_GROWTH) / 100);
ff28a94d 212
4c0f7679
JH
213 inlined_stack = (outer_info->stack_frame_offset
214 + outer_info->estimated_self_stack_size
e7f23018 215 + what_info->estimated_stack_size);
4c0f7679
JH
216 /* Check new stack consumption with stack consumption at the place
217 stack is used. */
218 if (inlined_stack > stack_size_limit
09a2806f 219 /* If function already has large stack usage from sibling
4c0f7679
JH
220 inline call, we can inline, too.
221 This bit overoptimistically assume that we are good at stack
222 packing. */
223 && inlined_stack > info->estimated_stack_size
ff28a94d
JH
224 && inlined_stack > PARAM_VALUE (PARAM_LARGE_STACK_FRAME))
225 {
4c0f7679 226 e->inline_failed = CIF_LARGE_STACK_FRAME_GROWTH_LIMIT;
ff28a94d
JH
227 return false;
228 }
ca31b95f
JH
229 return true;
230}
231
4c0f7679
JH
232/* Dump info about why inlining has failed. */
233
234static void
235report_inline_failed_reason (struct cgraph_edge *e)
236{
237 if (dump_file)
238 {
239 fprintf (dump_file, " not inlinable: %s/%i -> %s/%i, %s\n",
2a72a953
DM
240 xstrdup_for_dump (e->caller->name ()), e->caller->order,
241 xstrdup_for_dump (e->callee->name ()), e->callee->order,
4c0f7679 242 cgraph_inline_failed_string (e->inline_failed));
bb1e543c
JH
243 if ((e->inline_failed == CIF_TARGET_OPTION_MISMATCH
244 || e->inline_failed == CIF_OPTIMIZATION_MISMATCH)
245 && e->caller->lto_file_data
246 && e->callee->function_symbol ()->lto_file_data)
247 {
248 fprintf (dump_file, " LTO objects: %s, %s\n",
249 e->caller->lto_file_data->file_name,
250 e->callee->function_symbol ()->lto_file_data->file_name);
251 }
252 if (e->inline_failed == CIF_TARGET_OPTION_MISMATCH)
253 cl_target_option_print_diff
254 (dump_file, 2, target_opts_for_fn (e->caller->decl),
255 target_opts_for_fn (e->callee->ultimate_alias_target ()->decl));
256 if (e->inline_failed == CIF_OPTIMIZATION_MISMATCH)
257 cl_optimization_print_diff
258 (dump_file, 2, opts_for_fn (e->caller->decl),
259 opts_for_fn (e->callee->ultimate_alias_target ()->decl));
4c0f7679
JH
260 }
261}
262
25a07c7e
YG
263 /* Decide whether sanitizer-related attributes allow inlining. */
264
265static bool
266sanitize_attrs_match_for_inline_p (const_tree caller, const_tree callee)
267{
268 /* Don't care if sanitizer is disabled */
269 if (!(flag_sanitize & SANITIZE_ADDRESS))
270 return true;
271
272 if (!caller || !callee)
273 return true;
274
275 return !!lookup_attribute ("no_sanitize_address",
276 DECL_ATTRIBUTES (caller)) ==
277 !!lookup_attribute ("no_sanitize_address",
278 DECL_ATTRIBUTES (callee));
279}
280
8e926cb1
JH
281/* Used for flags where it is safe to inline when caller's value is
282 grater than callee's. */
283#define check_maybe_up(flag) \
284 (opts_for_fn (caller->decl)->x_##flag \
285 != opts_for_fn (callee->decl)->x_##flag \
286 && (!always_inline \
287 || opts_for_fn (caller->decl)->x_##flag \
288 < opts_for_fn (callee->decl)->x_##flag))
289/* Used for flags where it is safe to inline when caller's value is
290 smaller than callee's. */
291#define check_maybe_down(flag) \
292 (opts_for_fn (caller->decl)->x_##flag \
293 != opts_for_fn (callee->decl)->x_##flag \
294 && (!always_inline \
295 || opts_for_fn (caller->decl)->x_##flag \
296 > opts_for_fn (callee->decl)->x_##flag))
297/* Used for flags where exact match is needed for correctness. */
298#define check_match(flag) \
299 (opts_for_fn (caller->decl)->x_##flag \
300 != opts_for_fn (callee->decl)->x_##flag)
301
25a07c7e 302 /* Decide if we can inline the edge and possibly update
4c0f7679
JH
303 inline_failed reason.
304 We check whether inlining is possible at all and whether
305 caller growth limits allow doing so.
306
09ce3660
JH
307 if REPORT is true, output reason to the dump file.
308
1f26ac87 309 if DISREGARD_LIMITS is true, ignore size limits.*/
ca31b95f 310
61a05df1 311static bool
09ce3660 312can_inline_edge_p (struct cgraph_edge *e, bool report,
eb140ba0 313 bool disregard_limits = false, bool early = false)
ca31b95f 314{
7ce7e4d4
JH
315 gcc_checking_assert (e->inline_failed);
316
317 if (cgraph_inline_failed_type (e->inline_failed) == CIF_FINAL_ERROR)
318 {
319 if (report)
320 report_inline_failed_reason (e);
321 return false;
322 }
323
4c0f7679 324 bool inlinable = true;
a5b1779f 325 enum availability avail;
d52f5295 326 cgraph_node *callee = e->callee->ultimate_alias_target (&avail);
bb1e543c
JH
327 cgraph_node *caller = e->caller->global.inlined_to
328 ? e->caller->global.inlined_to : e->caller;
329 tree caller_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (caller->decl);
264b47b0 330 tree callee_tree
67348ccc 331 = callee ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (callee->decl) : NULL;
ea99e0be 332
7ce7e4d4 333 if (!callee->definition)
4c0f7679
JH
334 {
335 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
336 inlinable = false;
337 }
1f26ac87
JM
338 else if (callee->calls_comdat_local)
339 {
340 e->inline_failed = CIF_USES_COMDAT_LOCAL;
341 inlinable = false;
342 }
d52f5295 343 else if (avail <= AVAIL_INTERPOSABLE)
9de21a23 344 {
4c0f7679 345 e->inline_failed = CIF_OVERWRITABLE;
6957a6f6 346 inlinable = false;
9de21a23 347 }
89faf322 348 else if (e->call_stmt_cannot_inline_p)
4c0f7679 349 {
b631d45a
JH
350 if (e->inline_failed != CIF_FUNCTION_NOT_OPTIMIZED)
351 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
4c0f7679
JH
352 inlinable = false;
353 }
354 /* Don't inline if the functions have different EH personalities. */
bb1e543c 355 else if (DECL_FUNCTION_PERSONALITY (caller->decl)
67348ccc 356 && DECL_FUNCTION_PERSONALITY (callee->decl)
bb1e543c 357 && (DECL_FUNCTION_PERSONALITY (caller->decl)
67348ccc 358 != DECL_FUNCTION_PERSONALITY (callee->decl)))
4c0f7679
JH
359 {
360 e->inline_failed = CIF_EH_PERSONALITY;
361 inlinable = false;
362 }
a7ff6e27
AH
363 /* TM pure functions should not be inlined into non-TM_pure
364 functions. */
7ce7e4d4 365 else if (is_tm_pure (callee->decl) && !is_tm_pure (caller->decl))
0a35513e
AH
366 {
367 e->inline_failed = CIF_UNSPECIFIED;
368 inlinable = false;
369 }
09a2806f 370 /* Check compatibility of target optimization options. */
bb1e543c 371 else if (!targetm.target_option.can_inline_p (caller->decl,
67348ccc 372 callee->decl))
4c0f7679
JH
373 {
374 e->inline_failed = CIF_TARGET_OPTION_MISMATCH;
375 inlinable = false;
376 }
5058c037
JH
377 else if (!inline_summaries->get (callee)->inlinable)
378 {
379 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
380 inlinable = false;
381 }
382 else if (inline_summaries->get (caller)->contains_cilk_spawn)
383 {
384 e->inline_failed = CIF_CILK_SPAWN;
385 inlinable = false;
386 }
25a07c7e 387 /* Don't inline a function with mismatched sanitization attributes. */
bb1e543c 388 else if (!sanitize_attrs_match_for_inline_p (caller->decl, callee->decl))
25a07c7e
YG
389 {
390 e->inline_failed = CIF_ATTRIBUTE_MISMATCH;
391 inlinable = false;
392 }
4c0f7679 393 /* Check if caller growth allows the inlining. */
67348ccc 394 else if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl)
09ce3660 395 && !disregard_limits
9a4ac625 396 && !lookup_attribute ("flatten",
bb1e543c 397 DECL_ATTRIBUTES (caller->decl))
4c0f7679
JH
398 && !caller_growth_limits (e))
399 inlinable = false;
400 /* Don't inline a function with a higher optimization level than the
401 caller. FIXME: this is really just tip of iceberg of handling
402 optimization attribute. */
403 else if (caller_tree != callee_tree)
9de21a23 404 {
8e926cb1
JH
405 bool always_inline =
406 (DECL_DISREGARD_INLINE_LIMITS (callee->decl)
407 && lookup_attribute ("always_inline",
408 DECL_ATTRIBUTES (callee->decl)));
409
ddb3773a
JH
410 /* Until GCC 4.9 we did not check the semantics alterning flags
411 bellow and inline across optimization boundry.
412 Enabling checks bellow breaks several packages by refusing
413 to inline library always_inline functions. See PR65873.
414 Disable the check for early inlining for now until better solution
415 is found. */
416 if (always_inline && early)
417 ;
0c3068e0
RB
418 /* There are some options that change IL semantics which means
419 we cannot inline in these cases for correctness reason.
420 Not even for always_inline declared functions. */
421 /* Strictly speaking only when the callee contains signed integer
422 math where overflow is undefined. */
ddb3773a
JH
423 else if ((check_maybe_up (flag_strict_overflow)
424 /* this flag is set by optimize. Allow inlining across
425 optimize boundary. */
426 && (!opt_for_fn (caller->decl, optimize)
427 == !opt_for_fn (callee->decl, optimize) || !always_inline))
428 || check_match (flag_wrapv)
429 || check_match (flag_trapv)
430 /* Strictly speaking only when the callee uses FP math. */
431 || check_maybe_up (flag_rounding_math)
432 || check_maybe_up (flag_trapping_math)
433 || check_maybe_down (flag_unsafe_math_optimizations)
434 || check_maybe_down (flag_finite_math_only)
435 || check_maybe_up (flag_signaling_nans)
436 || check_maybe_down (flag_cx_limited_range)
437 || check_maybe_up (flag_signed_zeros)
438 || check_maybe_down (flag_associative_math)
439 || check_maybe_down (flag_reciprocal_math)
440 /* We do not want to make code compiled with exceptions to be
441 brought into a non-EH function unless we know that the callee
442 does not throw.
443 This is tracked by DECL_FUNCTION_PERSONALITY. */
444 || (check_match (flag_non_call_exceptions)
445 /* TODO: We also may allow bringing !flag_non_call_exceptions
446 to flag_non_call_exceptions function, but that may need
447 extra work in tree-inline to add the extra EH edges. */
448 && (!opt_for_fn (callee->decl, flag_non_call_exceptions)
449 || DECL_FUNCTION_PERSONALITY (callee->decl)))
450 || (check_maybe_up (flag_exceptions)
451 && DECL_FUNCTION_PERSONALITY (callee->decl))
452 /* Strictly speaking only when the callee contains function
453 calls that may end up setting errno. */
454 || check_maybe_up (flag_errno_math)
455 /* When devirtualization is diabled for callee, it is not safe
456 to inline it as we possibly mangled the type info.
457 Allow early inlining of always inlines. */
458 || (!early && check_maybe_down (flag_devirtualize)))
0c3068e0
RB
459 {
460 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
461 inlinable = false;
462 }
463 /* gcc.dg/pr43564.c. Apply user-forced inline even at -O0. */
8e926cb1 464 else if (always_inline)
bb1e543c 465 ;
0c3068e0
RB
466 /* When user added an attribute to the callee honor it. */
467 else if (lookup_attribute ("optimize", DECL_ATTRIBUTES (callee->decl))
468 && opts_for_fn (caller->decl) != opts_for_fn (callee->decl))
4c0f7679 469 {
fd811f03 470 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
4c0f7679
JH
471 inlinable = false;
472 }
ddb3773a
JH
473 /* If explicit optimize attribute are not used, the mismatch is caused
474 by different command line options used to build different units.
475 Do not care about COMDAT functions - those are intended to be
476 optimized with the optimization flags of module they are used in.
477 Also do not care about mixing up size/speed optimization when
478 DECL_DISREGARD_INLINE_LIMITS is set. */
479 else if ((callee->merged
480 && !lookup_attribute ("optimize",
481 DECL_ATTRIBUTES (caller->decl)))
482 || DECL_DISREGARD_INLINE_LIMITS (callee->decl))
483 ;
bb1e543c
JH
484 /* If mismatch is caused by merging two LTO units with different
485 optimizationflags we want to be bit nicer. However never inline
486 if one of functions is not optimized at all. */
487 else if (!opt_for_fn (callee->decl, optimize)
488 || !opt_for_fn (caller->decl, optimize))
489 {
490 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
491 inlinable = false;
492 }
493 /* If callee is optimized for size and caller is not, allow inlining if
494 code shrinks or we are in MAX_INLINE_INSNS_SINGLE limit and callee
495 is inline (and thus likely an unified comdat). This will allow caller
496 to run faster. */
497 else if (opt_for_fn (callee->decl, optimize_size)
498 > opt_for_fn (caller->decl, optimize_size))
499 {
500 int growth = estimate_edge_growth (e);
501 if (growth > 0
502 && (!DECL_DECLARED_INLINE_P (callee->decl)
503 && growth >= MAX (MAX_INLINE_INSNS_SINGLE,
504 MAX_INLINE_INSNS_AUTO)))
505 {
506 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
507 inlinable = false;
508 }
509 }
510 /* If callee is more aggressively optimized for performance than caller,
511 we generally want to inline only cheap (runtime wise) functions. */
512 else if (opt_for_fn (callee->decl, optimize_size)
513 < opt_for_fn (caller->decl, optimize_size)
514 || (opt_for_fn (callee->decl, optimize)
86f46e39 515 > opt_for_fn (caller->decl, optimize)))
bb1e543c
JH
516 {
517 if (estimate_edge_time (e)
518 >= 20 + inline_edge_summary (e)->call_stmt_time)
519 {
520 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
521 inlinable = false;
522 }
523 }
524
4c0f7679
JH
525 }
526
4c0f7679
JH
527 if (!inlinable && report)
528 report_inline_failed_reason (e);
529 return inlinable;
530}
531
532
533/* Return true if the edge E is inlinable during early inlining. */
534
535static bool
536can_early_inline_edge_p (struct cgraph_edge *e)
537{
d52f5295 538 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
4c0f7679
JH
539 /* Early inliner might get called at WPA stage when IPA pass adds new
540 function. In this case we can not really do any of early inlining
541 because function bodies are missing. */
67348ccc 542 if (!gimple_has_body_p (callee->decl))
4c0f7679
JH
543 {
544 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
9de21a23
JC
545 return false;
546 }
4c0f7679
JH
547 /* In early inliner some of callees may not be in SSA form yet
548 (i.e. the callgraph is cyclic and we did not process
549 the callee by early inliner, yet). We don't have CIF code for this
550 case; later we will re-do the decision in the real inliner. */
67348ccc
DM
551 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->caller->decl))
552 || !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
f27e50db 553 {
4c0f7679
JH
554 if (dump_file)
555 fprintf (dump_file, " edge not inlinable: not in SSA form\n");
f27e50db
JH
556 return false;
557 }
eb140ba0 558 if (!can_inline_edge_p (e, true, false, true))
4c0f7679
JH
559 return false;
560 return true;
561}
562
563
ae6e6a08 564/* Return number of calls in N. Ignore cheap builtins. */
4c0f7679 565
ae6e6a08
JH
566static int
567num_calls (struct cgraph_node *n)
4c0f7679
JH
568{
569 struct cgraph_edge *e;
ae6e6a08
JH
570 int num = 0;
571
4c0f7679 572 for (e = n->callees; e; e = e->next_callee)
67348ccc 573 if (!is_inexpensive_builtin (e->callee->decl))
ae6e6a08
JH
574 num++;
575 return num;
4c0f7679
JH
576}
577
f27e50db 578
4c0f7679 579/* Return true if we are interested in inlining small function. */
9de21a23 580
4c0f7679
JH
581static bool
582want_early_inline_function_p (struct cgraph_edge *e)
583{
584 bool want_inline = true;
d52f5295 585 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
4c0f7679 586
67348ccc 587 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
4c0f7679 588 ;
9a1e784a 589 /* For AutoFDO, we need to make sure that before profile summary, all
be3c16c4
DC
590 hot paths' IR look exactly the same as profiled binary. As a result,
591 in einliner, we will disregard size limit and inline those callsites
592 that are:
593 * inlined in the profiled binary, and
594 * the cloned callee has enough samples to be considered "hot". */
595 else if (flag_auto_profile && afdo_callsite_hot_enough_for_early_inline (e))
596 ;
67348ccc 597 else if (!DECL_DECLARED_INLINE_P (callee->decl)
2bf86c84 598 && !opt_for_fn (e->caller->decl, flag_inline_small_functions))
4c0f7679
JH
599 {
600 e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
601 report_inline_failed_reason (e);
602 want_inline = false;
603 }
604 else
9de21a23 605 {
4c0f7679 606 int growth = estimate_edge_growth (e);
ae6e6a08
JH
607 int n;
608
4c0f7679
JH
609 if (growth <= 0)
610 ;
3dafb85c 611 else if (!e->maybe_hot_p ()
4c0f7679
JH
612 && growth > 0)
613 {
614 if (dump_file)
615 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
616 "call is cold and code would grow by %i\n",
2a72a953 617 xstrdup_for_dump (e->caller->name ()),
67348ccc 618 e->caller->order,
2a72a953 619 xstrdup_for_dump (callee->name ()), callee->order,
4c0f7679
JH
620 growth);
621 want_inline = false;
622 }
ae6e6a08 623 else if (growth > PARAM_VALUE (PARAM_EARLY_INLINING_INSNS))
9de21a23 624 {
4c0f7679
JH
625 if (dump_file)
626 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
ae6e6a08 627 "growth %i exceeds --param early-inlining-insns\n",
2a72a953 628 xstrdup_for_dump (e->caller->name ()),
67348ccc 629 e->caller->order,
2a72a953 630 xstrdup_for_dump (callee->name ()), callee->order,
4c0f7679
JH
631 growth);
632 want_inline = false;
9de21a23 633 }
ae6e6a08
JH
634 else if ((n = num_calls (callee)) != 0
635 && growth * (n + 1) > PARAM_VALUE (PARAM_EARLY_INLINING_INSNS))
4c0f7679
JH
636 {
637 if (dump_file)
638 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
ae6e6a08
JH
639 "growth %i exceeds --param early-inlining-insns "
640 "divided by number of calls\n",
2a72a953 641 xstrdup_for_dump (e->caller->name ()),
67348ccc 642 e->caller->order,
2a72a953 643 xstrdup_for_dump (callee->name ()), callee->order,
4c0f7679
JH
644 growth);
645 want_inline = false;
646 }
647 }
648 return want_inline;
649}
650
d59171da
JH
651/* Compute time of the edge->caller + edge->callee execution when inlining
652 does not happen. */
653
6d4ab5f8 654inline sreal
d59171da
JH
655compute_uninlined_call_time (struct inline_summary *callee_info,
656 struct cgraph_edge *edge)
657{
208e5afa
JH
658 sreal uninlined_call_time = (sreal)callee_info->time;
659 cgraph_node *caller = (edge->caller->global.inlined_to
660 ? edge->caller->global.inlined_to
661 : edge->caller);
662
663 if (edge->count && caller->count)
664 uninlined_call_time *= (sreal)edge->count / caller->count;
665 if (edge->frequency)
666 uninlined_call_time *= cgraph_freq_base_rec * edge->frequency;
667 else
668 uninlined_call_time = uninlined_call_time >> 11;
669
670 int caller_time = inline_summaries->get (caller)->time;
d59171da
JH
671 return uninlined_call_time + caller_time;
672}
673
674/* Same as compute_uinlined_call_time but compute time when inlining
675 does happen. */
676
6d4ab5f8 677inline sreal
d59171da
JH
678compute_inlined_call_time (struct cgraph_edge *edge,
679 int edge_time)
680{
208e5afa
JH
681 cgraph_node *caller = (edge->caller->global.inlined_to
682 ? edge->caller->global.inlined_to
683 : edge->caller);
684 int caller_time = inline_summaries->get (caller)->time;
685 sreal time = edge_time;
686
687 if (edge->count && caller->count)
688 time *= (sreal)edge->count / caller->count;
689 if (edge->frequency)
690 time *= cgraph_freq_base_rec * edge->frequency;
691 else
692 time = time >> 11;
693
694 /* This calculation should match one in ipa-inline-analysis.
695 FIXME: Once ipa-inline-analysis is converted to sreal this can be
696 simplified. */
697 time -= (sreal) ((gcov_type) edge->frequency
698 * inline_edge_summary (edge)->call_stmt_time
699 * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE)) / INLINE_TIME_SCALE;
700 time += caller_time;
701 if (time <= 0)
702 time = ((sreal) 1) >> 8;
6d4ab5f8 703 gcc_checking_assert (time >= 0);
d59171da
JH
704 return time;
705}
706
42f7b0fa
JH
707/* Return true if the speedup for inlining E is bigger than
708 PARAM_MAX_INLINE_MIN_SPEEDUP. */
709
710static bool
711big_speedup_p (struct cgraph_edge *e)
712{
208e5afa
JH
713 sreal time = compute_uninlined_call_time (inline_summaries->get (e->callee),
714 e);
6d4ab5f8 715 sreal inlined_time = compute_inlined_call_time (e, estimate_edge_time (e));
208e5afa 716
42f7b0fa 717 if (time - inlined_time
6d4ab5f8
JH
718 > (sreal) time * PARAM_VALUE (PARAM_INLINE_MIN_SPEEDUP)
719 * percent_rec)
42f7b0fa
JH
720 return true;
721 return false;
722}
723
4c0f7679
JH
724/* Return true if we are interested in inlining small function.
725 When REPORT is true, report reason to dump file. */
726
727static bool
728want_inline_small_function_p (struct cgraph_edge *e, bool report)
729{
730 bool want_inline = true;
d52f5295 731 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
4c0f7679 732
67348ccc 733 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
4c0f7679 734 ;
67348ccc 735 else if (!DECL_DECLARED_INLINE_P (callee->decl)
2bf86c84 736 && !opt_for_fn (e->caller->decl, flag_inline_small_functions))
4c0f7679
JH
737 {
738 e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
739 want_inline = false;
9de21a23 740 }
4cd8957f 741 /* Do fast and conservative check if the function can be good
5970b079
EB
742 inline candidate. At the moment we allow inline hints to
743 promote non-inline functions to inline and we increase
744 MAX_INLINE_INSNS_SINGLE 16-fold for inline functions. */
b6d627e4 745 else if ((!DECL_DECLARED_INLINE_P (callee->decl)
3dafb85c 746 && (!e->count || !e->maybe_hot_p ()))
9a1e784a 747 && inline_summaries->get (callee)->min_size
5970b079 748 - inline_edge_summary (e)->call_stmt_size
4cd8957f
JH
749 > MAX (MAX_INLINE_INSNS_SINGLE, MAX_INLINE_INSNS_AUTO))
750 {
751 e->inline_failed = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
752 want_inline = false;
753 }
b6d627e4 754 else if ((DECL_DECLARED_INLINE_P (callee->decl) || e->count)
9a1e784a 755 && inline_summaries->get (callee)->min_size
5970b079 756 - inline_edge_summary (e)->call_stmt_size
4cd8957f
JH
757 > 16 * MAX_INLINE_INSNS_SINGLE)
758 {
b6d627e4
JH
759 e->inline_failed = (DECL_DECLARED_INLINE_P (callee->decl)
760 ? CIF_MAX_INLINE_INSNS_SINGLE_LIMIT
761 : CIF_MAX_INLINE_INSNS_AUTO_LIMIT);
4cd8957f
JH
762 want_inline = false;
763 }
ca31b95f 764 else
9de21a23 765 {
4c0f7679 766 int growth = estimate_edge_growth (e);
37678631 767 inline_hints hints = estimate_edge_hints (e);
42f7b0fa 768 bool big_speedup = big_speedup_p (e);
4c0f7679
JH
769
770 if (growth <= 0)
771 ;
37678631
JH
772 /* Apply MAX_INLINE_INSNS_SINGLE limit. Do not do so when
773 hints suggests that inlining given function is very profitable. */
67348ccc 774 else if (DECL_DECLARED_INLINE_P (callee->decl)
37678631 775 && growth >= MAX_INLINE_INSNS_SINGLE
4cd8957f
JH
776 && ((!big_speedup
777 && !(hints & (INLINE_HINT_indirect_call
b6d627e4 778 | INLINE_HINT_known_hot
4cd8957f
JH
779 | INLINE_HINT_loop_iterations
780 | INLINE_HINT_array_index
781 | INLINE_HINT_loop_stride)))
782 || growth >= MAX_INLINE_INSNS_SINGLE * 16))
4c0f7679
JH
783 {
784 e->inline_failed = CIF_MAX_INLINE_INSNS_SINGLE_LIMIT;
785 want_inline = false;
786 }
67348ccc 787 else if (!DECL_DECLARED_INLINE_P (callee->decl)
2bf86c84 788 && !opt_for_fn (e->caller->decl, flag_inline_functions))
4c0f7679 789 {
4cd8957f
JH
790 /* growth_likely_positive is expensive, always test it last. */
791 if (growth >= MAX_INLINE_INSNS_SINGLE
792 || growth_likely_positive (callee, growth))
793 {
794 e->inline_failed = CIF_NOT_DECLARED_INLINED;
795 want_inline = false;
796 }
4c0f7679 797 }
37678631
JH
798 /* Apply MAX_INLINE_INSNS_AUTO limit for functions not declared inline
799 Upgrade it to MAX_INLINE_INSNS_SINGLE when hints suggests that
800 inlining given function is very profitable. */
67348ccc 801 else if (!DECL_DECLARED_INLINE_P (callee->decl)
42f7b0fa 802 && !big_speedup
b6d627e4 803 && !(hints & INLINE_HINT_known_hot)
7c99ab65 804 && growth >= ((hints & (INLINE_HINT_indirect_call
128e0d89 805 | INLINE_HINT_loop_iterations
52843a47 806 | INLINE_HINT_array_index
128e0d89 807 | INLINE_HINT_loop_stride))
37678631
JH
808 ? MAX (MAX_INLINE_INSNS_AUTO,
809 MAX_INLINE_INSNS_SINGLE)
810 : MAX_INLINE_INSNS_AUTO))
4c0f7679 811 {
4cd8957f
JH
812 /* growth_likely_positive is expensive, always test it last. */
813 if (growth >= MAX_INLINE_INSNS_SINGLE
814 || growth_likely_positive (callee, growth))
815 {
816 e->inline_failed = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
817 want_inline = false;
818 }
4c0f7679 819 }
db22a743 820 /* If call is cold, do not inline when function body would grow. */
3dafb85c 821 else if (!e->maybe_hot_p ()
4cd8957f
JH
822 && (growth >= MAX_INLINE_INSNS_SINGLE
823 || growth_likely_positive (callee, growth)))
9de21a23 824 {
4c0f7679
JH
825 e->inline_failed = CIF_UNLIKELY_CALL;
826 want_inline = false;
9de21a23
JC
827 }
828 }
4c0f7679
JH
829 if (!want_inline && report)
830 report_inline_failed_reason (e);
831 return want_inline;
832}
9de21a23 833
4c0f7679
JH
834/* EDGE is self recursive edge.
835 We hand two cases - when function A is inlining into itself
836 or when function A is being inlined into another inliner copy of function
837 A within function B.
838
839 In first case OUTER_NODE points to the toplevel copy of A, while
840 in the second case OUTER_NODE points to the outermost copy of A in B.
841
842 In both cases we want to be extra selective since
843 inlining the call will just introduce new recursive calls to appear. */
09a2806f 844
4c0f7679
JH
845static bool
846want_inline_self_recursive_call_p (struct cgraph_edge *edge,
847 struct cgraph_node *outer_node,
848 bool peeling,
849 int depth)
850{
851 char const *reason = NULL;
852 bool want_inline = true;
853 int caller_freq = CGRAPH_FREQ_BASE;
854 int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO);
855
67348ccc 856 if (DECL_DECLARED_INLINE_P (edge->caller->decl))
4c0f7679
JH
857 max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH);
858
3dafb85c 859 if (!edge->maybe_hot_p ())
4c0f7679
JH
860 {
861 reason = "recursive call is cold";
862 want_inline = false;
863 }
864 else if (max_count && !outer_node->count)
865 {
866 reason = "not executed in profile";
867 want_inline = false;
868 }
869 else if (depth > max_depth)
870 {
871 reason = "--param max-inline-recursive-depth exceeded.";
872 want_inline = false;
873 }
874
875 if (outer_node->global.inlined_to)
876 caller_freq = outer_node->callers->frequency;
877
bd936951
JH
878 if (!caller_freq)
879 {
880 reason = "function is inlined and unlikely";
881 want_inline = false;
882 }
883
4c0f7679
JH
884 if (!want_inline)
885 ;
886 /* Inlining of self recursive function into copy of itself within other function
887 is transformation similar to loop peeling.
888
09a2806f 889 Peeling is profitable if we can inline enough copies to make probability
4c0f7679
JH
890 of actual call to the self recursive function very small. Be sure that
891 the probability of recursion is small.
892
09a2806f
JH
893 We ensure that the frequency of recursing is at most 1 - (1/max_depth).
894 This way the expected number of recision is at most max_depth. */
4c0f7679
JH
895 else if (peeling)
896 {
897 int max_prob = CGRAPH_FREQ_BASE - ((CGRAPH_FREQ_BASE + max_depth - 1)
898 / max_depth);
899 int i;
900 for (i = 1; i < depth; i++)
901 max_prob = max_prob * max_prob / CGRAPH_FREQ_BASE;
902 if (max_count
903 && (edge->count * CGRAPH_FREQ_BASE / outer_node->count
904 >= max_prob))
905 {
906 reason = "profile of recursive call is too large";
907 want_inline = false;
908 }
909 if (!max_count
910 && (edge->frequency * CGRAPH_FREQ_BASE / caller_freq
911 >= max_prob))
912 {
913 reason = "frequency of recursive call is too large";
914 want_inline = false;
915 }
916 }
09a2806f 917 /* Recursive inlining, i.e. equivalent of unrolling, is profitable if recursion
4c0f7679
JH
918 depth is large. We reduce function call overhead and increase chances that
919 things fit in hardware return predictor.
920
921 Recursive inlining might however increase cost of stack frame setup
922 actually slowing down functions whose recursion tree is wide rather than
923 deep.
924
09a2806f 925 Deciding reliably on when to do recursive inlining without profile feedback
4c0f7679
JH
926 is tricky. For now we disable recursive inlining when probability of self
927 recursion is low.
928
929 Recursive inlining of self recursive call within loop also results in large loop
930 depths that generally optimize badly. We may want to throttle down inlining
931 in those cases. In particular this seems to happen in one of libstdc++ rb tree
932 methods. */
933 else
934 {
935 if (max_count
936 && (edge->count * 100 / outer_node->count
937 <= PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY)))
938 {
939 reason = "profile of recursive call is too small";
940 want_inline = false;
941 }
942 else if (!max_count
943 && (edge->frequency * 100 / caller_freq
944 <= PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY)))
945 {
946 reason = "frequency of recursive call is too small";
947 want_inline = false;
948 }
949 }
950 if (!want_inline && dump_file)
951 fprintf (dump_file, " not inlining recursively: %s\n", reason);
952 return want_inline;
ca31b95f
JH
953}
954
19ba6aab
JH
955/* Return true when NODE has uninlinable caller;
956 set HAS_HOT_CALL if it has hot call.
9aa3f5c5
JH
957 Worker for cgraph_for_node_and_aliases. */
958
959static bool
19ba6aab 960check_callers (struct cgraph_node *node, void *has_hot_call)
9aa3f5c5 961{
19ba6aab
JH
962 struct cgraph_edge *e;
963 for (e = node->callers; e; e = e->next_caller)
964 {
2bf86c84
JH
965 if (!opt_for_fn (e->caller->decl, flag_inline_functions_called_once))
966 return true;
19ba6aab
JH
967 if (!can_inline_edge_p (e, true))
968 return true;
1af8bfe5
JH
969 if (e->recursive_p ())
970 return true;
3dafb85c 971 if (!(*(bool *)has_hot_call) && e->maybe_hot_p ())
19ba6aab
JH
972 *(bool *)has_hot_call = true;
973 }
974 return false;
9aa3f5c5
JH
975}
976
a81b0a3d
JH
977/* If NODE has a caller, return true. */
978
979static bool
980has_caller_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
981{
982 if (node->callers)
983 return true;
984 return false;
985}
09a2806f 986
100411f8
JH
987/* Decide if inlining NODE would reduce unit size by eliminating
988 the offline copy of function.
989 When COLD is true the cold calls are considered, too. */
09a2806f
JH
990
991static bool
100411f8 992want_inline_function_to_all_callers_p (struct cgraph_node *node, bool cold)
09a2806f 993{
5970b079
EB
994 bool has_hot_call = false;
995
9789b553
JH
996 /* Aliases gets inlined along with the function they alias. */
997 if (node->alias)
5970b079
EB
998 return false;
999 /* Already inlined? */
1000 if (node->global.inlined_to)
1001 return false;
1002 /* Does it have callers? */
1ede94c5 1003 if (!node->call_for_symbol_and_aliases (has_caller_p, NULL, true))
5970b079
EB
1004 return false;
1005 /* Inlining into all callers would increase size? */
1006 if (estimate_growth (node) > 0)
1007 return false;
1008 /* All inlines must be possible. */
1ede94c5
JH
1009 if (node->call_for_symbol_and_aliases (check_callers, &has_hot_call,
1010 true))
5970b079
EB
1011 return false;
1012 if (!cold && !has_hot_call)
1013 return false;
1014 return true;
09a2806f
JH
1015}
1016
670cd5c5
JH
1017/* A cost model driving the inlining heuristics in a way so the edges with
1018 smallest badness are inlined first. After each inlining is performed
0fa2e4df 1019 the costs of all caller edges of nodes affected are recomputed so the
670cd5c5 1020 metrics may accurately depend on values such as number of inlinable callers
45a80bb9 1021 of the function or function body size. */
670cd5c5 1022
f0e1509b 1023static sreal
4c0f7679 1024edge_badness (struct cgraph_edge *edge, bool dump)
670cd5c5 1025{
f0e1509b 1026 sreal badness;
d59171da 1027 int growth, edge_time;
d52f5295 1028 struct cgraph_node *callee = edge->callee->ultimate_alias_target ();
9a1e784a 1029 struct inline_summary *callee_info = inline_summaries->get (callee);
37678631 1030 inline_hints hints;
208e5afa
JH
1031 cgraph_node *caller = (edge->caller->global.inlined_to
1032 ? edge->caller->global.inlined_to
1033 : edge->caller);
1aa14195 1034
03dfc36d 1035 growth = estimate_edge_growth (edge);
d59171da 1036 edge_time = estimate_edge_time (edge);
37678631 1037 hints = estimate_edge_hints (edge);
d59171da
JH
1038 gcc_checking_assert (edge_time >= 0);
1039 gcc_checking_assert (edge_time <= callee_info->time);
1040 gcc_checking_assert (growth <= callee_info->size);
e89964e3 1041
1ce18dc8
JH
1042 if (dump)
1043 {
7153ca97 1044 fprintf (dump_file, " Badness calculation for %s/%i -> %s/%i\n",
2a72a953 1045 xstrdup_for_dump (edge->caller->name ()),
67348ccc 1046 edge->caller->order,
2a72a953 1047 xstrdup_for_dump (callee->name ()),
67348ccc 1048 edge->callee->order);
d59171da 1049 fprintf (dump_file, " size growth %i, time %i ",
1ce18dc8 1050 growth,
d59171da 1051 edge_time);
37678631 1052 dump_inline_hints (dump_file, hints);
42f7b0fa
JH
1053 if (big_speedup_p (edge))
1054 fprintf (dump_file, " big_speedup");
37678631 1055 fprintf (dump_file, "\n");
1ce18dc8 1056 }
45a80bb9
JH
1057
1058 /* Always prefer inlining saving code size. */
1059 if (growth <= 0)
1ce18dc8 1060 {
6d4ab5f8 1061 badness = (sreal) (-SREAL_MIN_SIG + growth) << (SREAL_MAX_EXP / 256);
1ce18dc8 1062 if (dump)
6d4ab5f8 1063 fprintf (dump_file, " %f: Growth %d <= 0\n", badness.to_double (),
1ce18dc8
JH
1064 growth);
1065 }
208e5afa
JH
1066 /* Inlining into EXTERNAL functions is not going to change anything unless
1067 they are themselves inlined. */
1068 else if (DECL_EXTERNAL (caller->decl))
1ce18dc8 1069 {
1ce18dc8 1070 if (dump)
208e5afa
JH
1071 fprintf (dump_file, " max: function is external\n");
1072 return sreal::max ();
1ce18dc8 1073 }
208e5afa 1074 /* When profile is available. Compute badness as:
b4c0a884 1075
208e5afa 1076 time_saved * caller_count
133a84ab
JH
1077 goodness = -------------------------------------------------
1078 growth_of_caller * overall_growth * combined_size
d59171da
JH
1079
1080 badness = - goodness
b4c0a884 1081
208e5afa
JH
1082 Again use negative value to make calls with profile appear hotter
1083 then calls without.
b4c0a884 1084 */
208e5afa 1085 else if (opt_for_fn (caller->decl, flag_guess_branch_prob) || caller->count)
670cd5c5 1086 {
6d4ab5f8 1087 sreal numerator, denominator;
41f669d8 1088 int overall_growth;
208e5afa
JH
1089
1090 numerator = (compute_uninlined_call_time (callee_info, edge)
1091 - compute_inlined_call_time (edge, edge_time));
1092 if (numerator == 0)
1093 numerator = ((sreal) 1 >> 8);
1094 if (caller->count)
1095 numerator *= caller->count;
1096 else if (opt_for_fn (caller->decl, flag_branch_probabilities))
1097 numerator = numerator >> 11;
1098 denominator = growth;
41f669d8
JH
1099
1100 overall_growth = callee_info->growth;
1101
1102 /* Look for inliner wrappers of the form:
1103
1104 inline_caller ()
1105 {
1106 do_fast_job...
1107 if (need_more_work)
1108 noninline_callee ();
1109 }
1110 Withhout panilizing this case, we usually inline noninline_callee
1111 into the inline_caller because overall_growth is small preventing
1112 further inlining of inline_caller.
1113
1114 Penalize only callgraph edges to functions with small overall
1115 growth ...
1116 */
1117 if (growth > overall_growth
1118 /* ... and having only one caller which is not inlined ... */
1119 && callee_info->single_caller
1120 && !edge->caller->global.inlined_to
1121 /* ... and edges executed only conditionally ... */
1122 && edge->frequency < CGRAPH_FREQ_BASE
1123 /* ... consider case where callee is not inline but caller is ... */
1124 && ((!DECL_DECLARED_INLINE_P (edge->callee->decl)
1125 && DECL_DECLARED_INLINE_P (caller->decl))
1126 /* ... or when early optimizers decided to split and edge
1127 frequency still indicates splitting is a win ... */
1128 || (callee->split_part && !caller->split_part
1129 && edge->frequency
1130 < CGRAPH_FREQ_BASE
1131 * PARAM_VALUE
1132 (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100
1133 /* ... and do not overwrite user specified hints. */
1134 && (!DECL_DECLARED_INLINE_P (edge->callee->decl)
1135 || DECL_DECLARED_INLINE_P (caller->decl)))))
1136 {
1137 struct inline_summary *caller_info = inline_summaries->get (caller);
1138 int caller_growth = caller_info->growth;
1139
1140 /* Only apply the penalty when caller looks like inline candidate,
1141 and it is not called once and. */
1142 if (!caller_info->single_caller && overall_growth < caller_growth
1143 && caller_info->inlinable
1144 && caller_info->size
1145 < (DECL_DECLARED_INLINE_P (caller->decl)
1146 ? MAX_INLINE_INSNS_SINGLE : MAX_INLINE_INSNS_AUTO))
1147 {
1148 if (dump)
1149 fprintf (dump_file,
1150 " Wrapper penalty. Increasing growth %i to %i\n",
1151 overall_growth, caller_growth);
1152 overall_growth = caller_growth;
1153 }
1154 }
1155 if (overall_growth > 0)
1156 {
1157 /* Strongly preffer functions with few callers that can be inlined
1158 fully. The square root here leads to smaller binaries at average.
1159 Watch however for extreme cases and return to linear function
1160 when growth is large. */
1161 if (overall_growth < 256)
1162 overall_growth *= overall_growth;
1163 else
1164 overall_growth += 256 * 256 - 256;
1165 denominator *= overall_growth;
1166 }
133a84ab 1167 denominator *= inline_summaries->get (caller)->self_size + growth;
6d4ab5f8
JH
1168
1169 badness = - numerator / denominator;
1170
1ce18dc8
JH
1171 if (dump)
1172 {
1173 fprintf (dump_file,
16998094
JM
1174 " %f: guessed profile. frequency %f, count %" PRId64
1175 " caller count %" PRId64
208e5afa 1176 " time w/o inlining %f, time w inlining %f"
41f669d8
JH
1177 " overall growth %i (current) %i (original)"
1178 " %i (compensated)\n",
1179 badness.to_double (),
1180 (double)edge->frequency / CGRAPH_FREQ_BASE,
208e5afa 1181 edge->count, caller->count,
6d4ab5f8
JH
1182 compute_uninlined_call_time (callee_info, edge).to_double (),
1183 compute_inlined_call_time (edge, edge_time).to_double (),
d59171da 1184 estimate_growth (callee),
41f669d8 1185 callee_info->growth, overall_growth);
1ce18dc8 1186 }
45a80bb9
JH
1187 }
1188 /* When function local profile is not available or it does not give
1189 useful information (ie frequency is zero), base the cost on
1190 loop nest and overall size growth, so we optimize for overall number
1191 of functions fully inlined in program. */
1192 else
1193 {
898b8927 1194 int nest = MIN (inline_edge_summary (edge)->loop_depth, 8);
6d4ab5f8 1195 badness = growth;
670cd5c5 1196
45a80bb9 1197 /* Decrease badness if call is nested. */
b8698a0f 1198 if (badness > 0)
f0e1509b 1199 badness = badness >> nest;
45a80bb9 1200 else
6d4ab5f8 1201 badness = badness << nest;
1ce18dc8 1202 if (dump)
41f669d8
JH
1203 fprintf (dump_file, " %f: no profile. nest %i\n",
1204 badness.to_double (), nest);
670cd5c5 1205 }
6d4ab5f8 1206 gcc_checking_assert (badness != 0);
1ce18dc8 1207
3dafb85c 1208 if (edge->recursive_p ())
6d4ab5f8
JH
1209 badness = badness.shift (badness > 0 ? 4 : -4);
1210 if ((hints & (INLINE_HINT_indirect_call
1211 | INLINE_HINT_loop_iterations
1212 | INLINE_HINT_array_index
1213 | INLINE_HINT_loop_stride))
1214 || callee_info->growth <= 0)
1215 badness = badness.shift (badness > 0 ? -2 : 2);
1216 if (hints & (INLINE_HINT_same_scc))
1217 badness = badness.shift (badness > 0 ? 3 : -3);
1218 else if (hints & (INLINE_HINT_in_scc))
1219 badness = badness.shift (badness > 0 ? 2 : -2);
1220 else if (hints & (INLINE_HINT_cross_module))
1221 badness = badness.shift (badness > 0 ? 1 : -1);
208e5afa
JH
1222 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
1223 badness = badness.shift (badness > 0 ? -4 : 4);
1224 else if ((hints & INLINE_HINT_declared_inline))
6d4ab5f8
JH
1225 badness = badness.shift (badness > 0 ? -3 : 3);
1226 if (dump)
1227 fprintf (dump_file, " Adjusted by hints %f\n", badness.to_double ());
1228 return badness;
670cd5c5
JH
1229}
1230
9b8051b4 1231/* Recompute badness of EDGE and update its key in HEAP if needed. */
4c0f7679 1232static inline void
4a910049 1233update_edge_key (edge_heap_t *heap, struct cgraph_edge *edge)
9b8051b4 1234{
f0e1509b 1235 sreal badness = edge_badness (edge, false);
9b8051b4
JH
1236 if (edge->aux)
1237 {
4a910049
ML
1238 edge_heap_node_t *n = (edge_heap_node_t *) edge->aux;
1239 gcc_checking_assert (n->get_data () == edge);
9b8051b4 1240
6d4ab5f8
JH
1241 /* fibonacci_heap::replace_key does busy updating of the
1242 heap that is unnecesarily expensive.
1243 We do lazy increases: after extracting minimum if the key
1244 turns out to be out of date, it is re-inserted into heap
1245 with correct value. */
4a910049 1246 if (badness < n->get_key ())
9b8051b4 1247 {
4c0f7679
JH
1248 if (dump_file && (dump_flags & TDF_DETAILS))
1249 {
1250 fprintf (dump_file,
6d4ab5f8
JH
1251 " decreasing badness %s/%i -> %s/%i, %f"
1252 " to %f\n",
2a72a953 1253 xstrdup_for_dump (edge->caller->name ()),
67348ccc 1254 edge->caller->order,
2a72a953 1255 xstrdup_for_dump (edge->callee->name ()),
67348ccc 1256 edge->callee->order,
6d4ab5f8
JH
1257 n->get_key ().to_double (),
1258 badness.to_double ());
4c0f7679 1259 }
4a910049 1260 heap->decrease_key (n, badness);
9b8051b4
JH
1261 }
1262 }
1263 else
4c0f7679
JH
1264 {
1265 if (dump_file && (dump_flags & TDF_DETAILS))
1266 {
1267 fprintf (dump_file,
6d4ab5f8 1268 " enqueuing call %s/%i -> %s/%i, badness %f\n",
2a72a953 1269 xstrdup_for_dump (edge->caller->name ()),
67348ccc 1270 edge->caller->order,
2a72a953 1271 xstrdup_for_dump (edge->callee->name ()),
67348ccc 1272 edge->callee->order,
6d4ab5f8 1273 badness.to_double ());
4c0f7679 1274 }
4a910049 1275 edge->aux = heap->insert (badness, edge);
4c0f7679 1276 }
9b8051b4
JH
1277}
1278
40fda55b
JH
1279
1280/* NODE was inlined.
1281 All caller edges needs to be resetted because
1282 size estimates change. Similarly callees needs reset
1283 because better context may be known. */
1284
1285static void
1286reset_edge_caches (struct cgraph_node *node)
1287{
1288 struct cgraph_edge *edge;
1289 struct cgraph_edge *e = node->callees;
1290 struct cgraph_node *where = node;
e55637b7 1291 struct ipa_ref *ref;
40fda55b
JH
1292
1293 if (where->global.inlined_to)
1294 where = where->global.inlined_to;
1295
40fda55b
JH
1296 for (edge = where->callers; edge; edge = edge->next_caller)
1297 if (edge->inline_failed)
1298 reset_edge_growth_cache (edge);
e55637b7
ML
1299
1300 FOR_EACH_ALIAS (where, ref)
1301 reset_edge_caches (dyn_cast <cgraph_node *> (ref->referring));
40fda55b
JH
1302
1303 if (!e)
1304 return;
1305
1306 while (true)
1307 if (!e->inline_failed && e->callee->callees)
1308 e = e->callee->callees;
1309 else
1310 {
1311 if (e->inline_failed)
1312 reset_edge_growth_cache (e);
1313 if (e->next_callee)
1314 e = e->next_callee;
1315 else
1316 {
1317 do
1318 {
1319 if (e->caller == node)
1320 return;
1321 e = e->caller->callers;
1322 }
1323 while (!e->next_callee);
1324 e = e->next_callee;
1325 }
1326 }
1327}
1328
1329/* Recompute HEAP nodes for each of caller of NODE.
1330 UPDATED_NODES track nodes we already visited, to avoid redundant work.
1331 When CHECK_INLINABLITY_FOR is set, re-check for specified edge that
1332 it is inlinable. Otherwise check all edges. */
670cd5c5
JH
1333
1334static void
4a910049 1335update_caller_keys (edge_heap_t *heap, struct cgraph_node *node,
40fda55b
JH
1336 bitmap updated_nodes,
1337 struct cgraph_edge *check_inlinablity_for)
670cd5c5
JH
1338{
1339 struct cgraph_edge *edge;
e55637b7 1340 struct ipa_ref *ref;
670cd5c5 1341
9a1e784a 1342 if ((!node->alias && !inline_summaries->get (node)->inlinable)
670cd5c5
JH
1343 || node->global.inlined_to)
1344 return;
fcaa4ca4 1345 if (!bitmap_set_bit (updated_nodes, node->uid))
670cd5c5 1346 return;
670cd5c5 1347
e55637b7
ML
1348 FOR_EACH_ALIAS (node, ref)
1349 {
1350 struct cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1351 update_caller_keys (heap, alias, updated_nodes, check_inlinablity_for);
1352 }
39e2db00 1353
cdc029b9 1354 for (edge = node->callers; edge; edge = edge->next_caller)
4c0f7679
JH
1355 if (edge->inline_failed)
1356 {
40fda55b
JH
1357 if (!check_inlinablity_for
1358 || check_inlinablity_for == edge)
f10d1a74 1359 {
40fda55b
JH
1360 if (can_inline_edge_p (edge, false)
1361 && want_inline_small_function_p (edge, false))
1362 update_edge_key (heap, edge);
1363 else if (edge->aux)
1364 {
1365 report_inline_failed_reason (edge);
4a910049 1366 heap->delete_node ((edge_heap_node_t *) edge->aux);
40fda55b
JH
1367 edge->aux = NULL;
1368 }
f10d1a74 1369 }
40fda55b
JH
1370 else if (edge->aux)
1371 update_edge_key (heap, edge);
4c0f7679 1372 }
9b8051b4
JH
1373}
1374
40fda55b 1375/* Recompute HEAP nodes for each uninlined call in NODE.
9b8051b4
JH
1376 This is used when we know that edge badnesses are going only to increase
1377 (we introduced new call site) and thus all we need is to insert newly
1378 created edges into heap. */
1379
1380static void
4a910049 1381update_callee_keys (edge_heap_t *heap, struct cgraph_node *node,
9b8051b4
JH
1382 bitmap updated_nodes)
1383{
1384 struct cgraph_edge *e = node->callees;
09a2806f 1385
9b8051b4
JH
1386 if (!e)
1387 return;
1388 while (true)
1389 if (!e->inline_failed && e->callee->callees)
1390 e = e->callee->callees;
1391 else
670cd5c5 1392 {
a5b1779f
JH
1393 enum availability avail;
1394 struct cgraph_node *callee;
58696ce5
JH
1395 /* We do not reset callee growth cache here. Since we added a new call,
1396 growth chould have just increased and consequentely badness metric
1397 don't need updating. */
9b8051b4 1398 if (e->inline_failed
d52f5295 1399 && (callee = e->callee->ultimate_alias_target (&avail))
9a1e784a 1400 && inline_summaries->get (callee)->inlinable
aaae719d 1401 && avail >= AVAIL_AVAILABLE
a5b1779f 1402 && !bitmap_bit_p (updated_nodes, callee->uid))
670cd5c5 1403 {
40fda55b
JH
1404 if (can_inline_edge_p (e, false)
1405 && want_inline_small_function_p (e, false))
1406 update_edge_key (heap, e);
1407 else if (e->aux)
1408 {
1409 report_inline_failed_reason (e);
4a910049 1410 heap->delete_node ((edge_heap_node_t *) e->aux);
40fda55b
JH
1411 e->aux = NULL;
1412 }
9b8051b4
JH
1413 }
1414 if (e->next_callee)
1415 e = e->next_callee;
1416 else
1417 {
1418 do
1ce18dc8 1419 {
9b8051b4
JH
1420 if (e->caller == node)
1421 return;
1422 e = e->caller->callers;
1ce18dc8 1423 }
9b8051b4
JH
1424 while (!e->next_callee);
1425 e = e->next_callee;
670cd5c5 1426 }
670cd5c5
JH
1427 }
1428}
1429
670cd5c5 1430/* Enqueue all recursive calls from NODE into priority queue depending on
0fa2e4df 1431 how likely we want to recursively inline the call. */
670cd5c5 1432
ca31b95f
JH
1433static void
1434lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
4a910049 1435 edge_heap_t *heap)
ca31b95f
JH
1436{
1437 struct cgraph_edge *e;
a5b1779f
JH
1438 enum availability avail;
1439
ca31b95f 1440 for (e = where->callees; e; e = e->next_callee)
a5b1779f 1441 if (e->callee == node
d52f5295
ML
1442 || (e->callee->ultimate_alias_target (&avail) == node
1443 && avail > AVAIL_INTERPOSABLE))
ca31b95f 1444 {
c5a4444c 1445 /* When profile feedback is available, prioritize by expected number
09a2806f 1446 of calls. */
4a910049
ML
1447 heap->insert (!max_count ? -e->frequency
1448 : -(e->count / ((max_count + (1<<24) - 1) / (1<<24))),
1449 e);
ca31b95f
JH
1450 }
1451 for (e = where->callees; e; e = e->next_callee)
1452 if (!e->inline_failed)
670cd5c5 1453 lookup_recursive_calls (node, e->callee, heap);
ca31b95f
JH
1454}
1455
1456/* Decide on recursive inlining: in the case function has recursive calls,
3e293154 1457 inline until body size reaches given argument. If any new indirect edges
e56f5f3e
JJ
1458 are discovered in the process, add them to *NEW_EDGES, unless NEW_EDGES
1459 is NULL. */
670cd5c5
JH
1460
1461static bool
4c0f7679 1462recursive_inlining (struct cgraph_edge *edge,
d52f5295 1463 vec<cgraph_edge *> *new_edges)
ca31b95f
JH
1464{
1465 int limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO);
d1704358 1466 edge_heap_t heap (sreal::min ());
d7d1d041 1467 struct cgraph_node *node;
ca31b95f 1468 struct cgraph_edge *e;
4c0f7679 1469 struct cgraph_node *master_clone = NULL, *next;
ca31b95f
JH
1470 int depth = 0;
1471 int n = 0;
1472
d7d1d041
RG
1473 node = edge->caller;
1474 if (node->global.inlined_to)
1475 node = node->global.inlined_to;
1476
67348ccc 1477 if (DECL_DECLARED_INLINE_P (node->decl))
4c0f7679 1478 limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE);
ca31b95f
JH
1479
1480 /* Make sure that function is small enough to be considered for inlining. */
4c0f7679 1481 if (estimate_size_after_inlining (node, edge) >= limit)
670cd5c5 1482 return false;
4a910049
ML
1483 lookup_recursive_calls (node, node, &heap);
1484 if (heap.empty ())
1485 return false;
ca31b95f
JH
1486
1487 if (dump_file)
b8698a0f 1488 fprintf (dump_file,
670cd5c5 1489 " Performing recursive inlining on %s\n",
fec39fa6 1490 node->name ());
ca31b95f 1491
ca31b95f 1492 /* Do the inlining and update list of recursive call during process. */
4a910049 1493 while (!heap.empty ())
ca31b95f 1494 {
4a910049 1495 struct cgraph_edge *curr = heap.extract_min ();
6ced940d 1496 struct cgraph_node *cnode, *dest = curr->callee;
d7d1d041 1497
4c0f7679
JH
1498 if (!can_inline_edge_p (curr, true))
1499 continue;
1500
6ced940d
JH
1501 /* MASTER_CLONE is produced in the case we already started modified
1502 the function. Be sure to redirect edge to the original body before
1503 estimating growths otherwise we will be seeing growths after inlining
1504 the already modified body. */
1505 if (master_clone)
1506 {
3dafb85c
ML
1507 curr->redirect_callee (master_clone);
1508 reset_edge_growth_cache (curr);
6ced940d
JH
1509 }
1510
1511 if (estimate_size_after_inlining (node, curr) > limit)
1512 {
3dafb85c 1513 curr->redirect_callee (dest);
6ced940d
JH
1514 reset_edge_growth_cache (curr);
1515 break;
1516 }
1517
c5a4444c
JH
1518 depth = 1;
1519 for (cnode = curr->caller;
1520 cnode->global.inlined_to; cnode = cnode->callers->caller)
67348ccc 1521 if (node->decl
d52f5295 1522 == curr->callee->ultimate_alias_target ()->decl)
f791d333 1523 depth++;
c5a4444c 1524
4c0f7679 1525 if (!want_inline_self_recursive_call_p (curr, node, false, depth))
6ced940d 1526 {
3dafb85c 1527 curr->redirect_callee (dest);
6ced940d
JH
1528 reset_edge_growth_cache (curr);
1529 continue;
1530 }
ca31b95f 1531
670cd5c5 1532 if (dump_file)
c5a4444c 1533 {
b8698a0f 1534 fprintf (dump_file,
c5a4444c
JH
1535 " Inlining call of depth %i", depth);
1536 if (node->count)
1537 {
1538 fprintf (dump_file, " called approx. %.2f times per call",
1539 (double)curr->count / node->count);
1540 }
1541 fprintf (dump_file, "\n");
1542 }
4c0f7679
JH
1543 if (!master_clone)
1544 {
1545 /* We need original clone to copy around. */
d52f5295
ML
1546 master_clone = node->create_clone (node->decl, node->count,
1547 CGRAPH_FREQ_BASE, false, vNULL,
1548 true, NULL, NULL);
4c0f7679
JH
1549 for (e = master_clone->callees; e; e = e->next_callee)
1550 if (!e->inline_failed)
bd936951 1551 clone_inlined_nodes (e, true, false, NULL, CGRAPH_FREQ_BASE);
3dafb85c 1552 curr->redirect_callee (master_clone);
6ced940d 1553 reset_edge_growth_cache (curr);
4c0f7679
JH
1554 }
1555
c170d40f 1556 inline_call (curr, false, new_edges, &overall_size, true);
4a910049 1557 lookup_recursive_calls (node, curr->callee, &heap);
ca31b95f
JH
1558 n++;
1559 }
4c0f7679 1560
4a910049 1561 if (!heap.empty () && dump_file)
c5a4444c 1562 fprintf (dump_file, " Recursive inlining growth limit met.\n");
4c0f7679
JH
1563
1564 if (!master_clone)
1565 return false;
1566
ca31b95f 1567 if (dump_file)
b8698a0f 1568 fprintf (dump_file,
4c0f7679
JH
1569 "\n Inlined %i times, "
1570 "body grown from size %i to %i, time %i to %i\n", n,
9a1e784a
ML
1571 inline_summaries->get (master_clone)->size, inline_summaries->get (node)->size,
1572 inline_summaries->get (master_clone)->time, inline_summaries->get (node)->time);
ca31b95f
JH
1573
1574 /* Remove master clone we used for inlining. We rely that clones inlined
1575 into master clone gets queued just before master clone so we don't
1576 need recursion. */
3dafb85c 1577 for (node = symtab->first_function (); node != master_clone;
96fc428c
JH
1578 node = next)
1579 {
3dafb85c 1580 next = symtab->next_function (node);
96fc428c 1581 if (node->global.inlined_to == master_clone)
d52f5295 1582 node->remove ();
96fc428c 1583 }
d52f5295 1584 master_clone->remove ();
4c0f7679 1585 return true;
ca31b95f
JH
1586}
1587
09a2806f 1588
88512ba0 1589/* Given whole compilation unit estimate of INSNS, compute how large we can
b7c27d51 1590 allow the unit to grow. */
09a2806f 1591
b7c27d51
JH
1592static int
1593compute_max_insns (int insns)
1594{
1595 int max_insns = insns;
1596 if (max_insns < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
1597 max_insns = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
1598
a9243bfc 1599 return ((int64_t) max_insns
3e2a6e7b 1600 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
b7c27d51
JH
1601}
1602
09a2806f 1603
3e293154 1604/* Compute badness of all edges in NEW_EDGES and add them to the HEAP. */
09a2806f 1605
3e293154 1606static void
4a910049 1607add_new_edges_to_heap (edge_heap_t *heap, vec<cgraph_edge *> new_edges)
3e293154 1608{
9771b263 1609 while (new_edges.length () > 0)
3e293154 1610 {
9771b263 1611 struct cgraph_edge *edge = new_edges.pop ();
3e293154
MJ
1612
1613 gcc_assert (!edge->aux);
a5b1779f 1614 if (edge->inline_failed
4c0f7679
JH
1615 && can_inline_edge_p (edge, true)
1616 && want_inline_small_function_p (edge, true))
4a910049 1617 edge->aux = heap->insert (edge_badness (edge, false), edge);
3e293154
MJ
1618 }
1619}
1620
042ae7d2
JH
1621/* Remove EDGE from the fibheap. */
1622
1623static void
1624heap_edge_removal_hook (struct cgraph_edge *e, void *data)
1625{
1626 if (e->aux)
1627 {
4a910049 1628 ((edge_heap_t *)data)->delete_node ((edge_heap_node_t *)e->aux);
042ae7d2
JH
1629 e->aux = NULL;
1630 }
1631}
3e293154 1632
09ce3660
JH
1633/* Return true if speculation of edge E seems useful.
1634 If ANTICIPATE_INLINING is true, be conservative and hope that E
1635 may get inlined. */
1636
1637bool
1638speculation_useful_p (struct cgraph_edge *e, bool anticipate_inlining)
1639{
1640 enum availability avail;
d52f5295 1641 struct cgraph_node *target = e->callee->ultimate_alias_target (&avail);
09ce3660
JH
1642 struct cgraph_edge *direct, *indirect;
1643 struct ipa_ref *ref;
1644
1645 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1646
3dafb85c 1647 if (!e->maybe_hot_p ())
09ce3660
JH
1648 return false;
1649
1650 /* See if IP optimizations found something potentially useful about the
1651 function. For now we look only for CONST/PURE flags. Almost everything
1652 else we propagate is useless. */
1653 if (avail >= AVAIL_AVAILABLE)
1654 {
67348ccc 1655 int ecf_flags = flags_from_decl_or_type (target->decl);
09ce3660
JH
1656 if (ecf_flags & ECF_CONST)
1657 {
3dafb85c 1658 e->speculative_call_info (direct, indirect, ref);
09ce3660
JH
1659 if (!(indirect->indirect_info->ecf_flags & ECF_CONST))
1660 return true;
1661 }
1662 else if (ecf_flags & ECF_PURE)
1663 {
3dafb85c 1664 e->speculative_call_info (direct, indirect, ref);
09ce3660
JH
1665 if (!(indirect->indirect_info->ecf_flags & ECF_PURE))
1666 return true;
1667 }
1668 }
1669 /* If we did not managed to inline the function nor redirect
1670 to an ipa-cp clone (that are seen by having local flag set),
1671 it is probably pointless to inline it unless hardware is missing
1672 indirect call predictor. */
1673 if (!anticipate_inlining && e->inline_failed && !target->local.local)
1674 return false;
1675 /* For overwritable targets there is not much to do. */
1676 if (e->inline_failed && !can_inline_edge_p (e, false, true))
1677 return false;
1678 /* OK, speculation seems interesting. */
1679 return true;
1680}
1681
1682/* We know that EDGE is not going to be inlined.
1683 See if we can remove speculation. */
1684
1685static void
4a910049 1686resolve_noninline_speculation (edge_heap_t *edge_heap, struct cgraph_edge *edge)
09ce3660
JH
1687{
1688 if (edge->speculative && !speculation_useful_p (edge, false))
1689 {
1690 struct cgraph_node *node = edge->caller;
1691 struct cgraph_node *where = node->global.inlined_to
1692 ? node->global.inlined_to : node;
1693 bitmap updated_nodes = BITMAP_ALLOC (NULL);
1694
e86a910f 1695 spec_rem += edge->count;
3dafb85c 1696 edge->resolve_speculation ();
09ce3660
JH
1697 reset_edge_caches (where);
1698 inline_update_overall_summary (where);
1699 update_caller_keys (edge_heap, where,
1700 updated_nodes, NULL);
d0b66480
JH
1701 update_callee_keys (edge_heap, where,
1702 updated_nodes);
09ce3660
JH
1703 BITMAP_FREE (updated_nodes);
1704 }
1705}
1706
bb1e543c
JH
1707/* Return true if NODE should be accounted for overall size estimate.
1708 Skip all nodes optimized for size so we can measure the growth of hot
1709 part of program no matter of the padding. */
1710
1711bool
1712inline_account_function_p (struct cgraph_node *node)
1713{
1714 return (!DECL_EXTERNAL (node->decl)
1715 && !opt_for_fn (node->decl, optimize_size)
1716 && node->frequency != NODE_FREQUENCY_UNLIKELY_EXECUTED);
1717}
1718
41f669d8
JH
1719/* Count number of callers of NODE and store it into DATA (that
1720 points to int. Worker for cgraph_for_node_and_aliases. */
1721
1722static bool
1723sum_callers (struct cgraph_node *node, void *data)
1724{
1725 struct cgraph_edge *e;
1726 int *num_calls = (int *)data;
1727
1728 for (e = node->callers; e; e = e->next_caller)
1729 (*num_calls)++;
1730 return false;
1731}
1732
ca31b95f 1733/* We use greedy algorithm for inlining of small functions:
09a2806f
JH
1734 All inline candidates are put into prioritized heap ordered in
1735 increasing badness.
ca31b95f 1736
09a2806f 1737 The inlining of small functions is bounded by unit growth parameters. */
ca31b95f
JH
1738
1739static void
4c0f7679 1740inline_small_functions (void)
ca31b95f
JH
1741{
1742 struct cgraph_node *node;
670cd5c5 1743 struct cgraph_edge *edge;
f0e1509b 1744 edge_heap_t edge_heap (sreal::min ());
670cd5c5 1745 bitmap updated_nodes = BITMAP_ALLOC (NULL);
85057983 1746 int min_size, max_size;
d52f5295 1747 auto_vec<cgraph_edge *> new_indirect_edges;
09a2806f 1748 int initial_size = 0;
3dafb85c 1749 struct cgraph_node **order = XCNEWVEC (cgraph_node *, symtab->cgraph_count);
042ae7d2 1750 struct cgraph_edge_hook_list *edge_removal_hook_holder;
2bf86c84 1751 new_indirect_edges.create (8);
670cd5c5 1752
042ae7d2 1753 edge_removal_hook_holder
4a910049 1754 = symtab->add_edge_removal_hook (&heap_edge_removal_hook, &edge_heap);
042ae7d2 1755
1a3118e9
JH
1756 /* Compute overall unit size and other global parameters used by badness
1757 metrics. */
ca31b95f 1758
09a2806f 1759 max_count = 0;
68cc8feb
JH
1760 ipa_reduced_postorder (order, true, true, NULL);
1761 free (order);
1a3118e9 1762
c47d0034
JH
1763 FOR_EACH_DEFINED_FUNCTION (node)
1764 if (!node->global.inlined_to)
e7f23018 1765 {
bb1e543c
JH
1766 if (!node->alias && node->analyzed
1767 && (node->has_gimple_body_p () || node->thunk.thunk_p))
a5b1779f 1768 {
9a1e784a 1769 struct inline_summary *info = inline_summaries->get (node);
67348ccc 1770 struct ipa_dfs_info *dfs = (struct ipa_dfs_info *) node->aux;
ca31b95f 1771
5e750dc6
JH
1772 /* Do not account external functions, they will be optimized out
1773 if not inlined. Also only count the non-cold portion of program. */
bb1e543c 1774 if (inline_account_function_p (node))
a5b1779f 1775 initial_size += info->size;
d59171da 1776 info->growth = estimate_growth (node);
41f669d8
JH
1777
1778 int num_calls = 0;
1779 node->call_for_symbol_and_aliases (sum_callers, &num_calls,
1780 true);
1781 if (num_calls == 1)
1782 info->single_caller = true;
bf3f6510
JH
1783 if (dfs && dfs->next_cycle)
1784 {
1785 struct cgraph_node *n2;
1786 int id = dfs->scc_no + 1;
1787 for (n2 = node; n2;
67348ccc 1788 n2 = ((struct ipa_dfs_info *) node->aux)->next_cycle)
bf3f6510 1789 {
9a1e784a 1790 struct inline_summary *info2 = inline_summaries->get (n2);
bf3f6510
JH
1791 if (info2->scc_no)
1792 break;
1793 info2->scc_no = id;
1794 }
1795 }
a5b1779f 1796 }
09a2806f 1797
e7f23018 1798 for (edge = node->callers; edge; edge = edge->next_caller)
632b4f8e
JH
1799 if (max_count < edge->count)
1800 max_count = edge->count;
e7f23018 1801 }
b48ccf0d
JH
1802 ipa_free_postorder_info ();
1803 initialize_growth_caches ();
1804
1805 if (dump_file)
1806 fprintf (dump_file,
1807 "\nDeciding on inlining of small functions. Starting with size %i.\n",
1808 initial_size);
b7c27d51 1809
8a8dccb2 1810 overall_size = initial_size;
85057983
JH
1811 max_size = compute_max_insns (overall_size);
1812 min_size = overall_size;
1a3118e9 1813
a99be3c9 1814 /* Populate the heap with all edges we might inline. */
1a3118e9 1815
c47d0034 1816 FOR_EACH_DEFINED_FUNCTION (node)
09ce3660
JH
1817 {
1818 bool update = false;
bcda57c1 1819 struct cgraph_edge *next = NULL;
c1eed5a1 1820 bool has_speculative = false;
1a3118e9 1821
09ce3660
JH
1822 if (dump_file)
1823 fprintf (dump_file, "Enqueueing calls in %s/%i.\n",
fec39fa6 1824 node->name (), node->order);
09ce3660
JH
1825
1826 for (edge = node->callees; edge; edge = next)
1827 {
1828 next = edge->next_callee;
1a3118e9 1829 if (edge->inline_failed
09ce3660 1830 && !edge->aux
1a3118e9
JH
1831 && can_inline_edge_p (edge, true)
1832 && want_inline_small_function_p (edge, true)
1833 && edge->inline_failed)
1834 {
1835 gcc_assert (!edge->aux);
4a910049 1836 update_edge_key (&edge_heap, edge);
1a3118e9 1837 }
c1eed5a1
JH
1838 if (edge->speculative)
1839 has_speculative = true;
1840 }
1841 if (has_speculative)
1842 for (edge = node->callees; edge; edge = next)
1843 if (edge->speculative && !speculation_useful_p (edge,
1844 edge->aux != NULL))
09ce3660 1845 {
3dafb85c 1846 edge->resolve_speculation ();
09ce3660
JH
1847 update = true;
1848 }
09ce3660
JH
1849 if (update)
1850 {
1851 struct cgraph_node *where = node->global.inlined_to
1852 ? node->global.inlined_to : node;
1853 inline_update_overall_summary (where);
09ce3660 1854 reset_edge_caches (where);
4a910049 1855 update_caller_keys (&edge_heap, where,
09ce3660 1856 updated_nodes, NULL);
2001028a
JH
1857 update_callee_keys (&edge_heap, where,
1858 updated_nodes);
09ce3660
JH
1859 bitmap_clear (updated_nodes);
1860 }
1861 }
1a3118e9 1862
09a2806f
JH
1863 gcc_assert (in_lto_p
1864 || !max_count
1865 || (profile_info && flag_branch_probabilities));
b7c27d51 1866
4a910049 1867 while (!edge_heap.empty ())
ca31b95f 1868 {
85057983 1869 int old_size = overall_size;
1ce18dc8 1870 struct cgraph_node *where, *callee;
f0e1509b
ML
1871 sreal badness = edge_heap.min_key ();
1872 sreal current_badness;
1ce18dc8 1873 int growth;
670cd5c5 1874
4a910049 1875 edge = edge_heap.extract_min ();
1ce18dc8
JH
1876 gcc_assert (edge->aux);
1877 edge->aux = NULL;
9de6f6c3 1878 if (!edge->inline_failed || !edge->callee->analyzed)
1ce18dc8 1879 continue;
cdc029b9 1880
6d4ab5f8
JH
1881#ifdef ENABLE_CHECKING
1882 /* Be sure that caches are maintained consistent. */
1883 sreal cached_badness = edge_badness (edge, false);
0d92b555
JH
1884
1885 int old_size_est = estimate_edge_size (edge);
1886 int old_time_est = estimate_edge_time (edge);
1887 int old_hints_est = estimate_edge_hints (edge);
1888
40fda55b 1889 reset_edge_growth_cache (edge);
0d92b555
JH
1890 gcc_assert (old_size_est == estimate_edge_size (edge));
1891 gcc_assert (old_time_est == estimate_edge_time (edge));
52d36202
JH
1892 /* FIXME:
1893
1894 gcc_assert (old_hints_est == estimate_edge_hints (edge));
1895
1896 fails with profile feedback because some hints depends on
1897 maybe_hot_edge_p predicate and because callee gets inlined to other
1898 calls, the edge may become cold.
1899 This ought to be fixed by computing relative probabilities
1900 for given invocation but that will be better done once whole
1901 code is converted to sreals. Disable for now and revert to "wrong"
1902 value so enable/disable checking paths agree. */
1903 edge_growth_cache[edge->uid].hints = old_hints_est + 1;
40fda55b 1904
cdc029b9 1905 /* When updating the edge costs, we only decrease badness in the keys.
09a2806f
JH
1906 Increases of badness are handled lazilly; when we see key with out
1907 of date value on it, we re-insert it now. */
4c0f7679 1908 current_badness = edge_badness (edge, false);
208e5afa
JH
1909 /* Disable checking for profile because roundoff errors may cause slight
1910 deviations in the order. */
1911 gcc_assert (max_count || cached_badness == current_badness);
2001028a 1912 gcc_assert (current_badness >= badness);
6d4ab5f8
JH
1913#else
1914 current_badness = edge_badness (edge, false);
1915#endif
cdc029b9
JH
1916 if (current_badness != badness)
1917 {
d75de25b 1918 if (edge_heap.min () && current_badness > edge_heap.min_key ())
6d4ab5f8
JH
1919 {
1920 edge->aux = edge_heap.insert (current_badness, edge);
1921 continue;
1922 }
1923 else
1924 badness = current_badness;
cdc029b9 1925 }
4c0f7679
JH
1926
1927 if (!can_inline_edge_p (edge, true))
09ce3660 1928 {
4a910049 1929 resolve_noninline_speculation (&edge_heap, edge);
09ce3660
JH
1930 continue;
1931 }
cdc029b9 1932
d52f5295 1933 callee = edge->callee->ultimate_alias_target ();
03dfc36d 1934 growth = estimate_edge_growth (edge);
ca31b95f 1935 if (dump_file)
ca31b95f 1936 {
b8698a0f 1937 fprintf (dump_file,
9de04252 1938 "\nConsidering %s/%i with %i size\n",
fec39fa6 1939 callee->name (), callee->order,
9a1e784a 1940 inline_summaries->get (callee)->size);
b8698a0f 1941 fprintf (dump_file,
9de04252 1942 " to be inlined into %s/%i in %s:%i\n"
6d4ab5f8 1943 " Estimated badness is %f, frequency %.2f.\n",
fec39fa6 1944 edge->caller->name (), edge->caller->order,
9e145afd 1945 edge->call_stmt
355fe088 1946 && (LOCATION_LOCUS (gimple_location ((const gimple *)
1b7706c8
JJ
1947 edge->call_stmt))
1948 > BUILTINS_LOCATION)
355fe088 1949 ? gimple_filename ((const gimple *) edge->call_stmt)
9e145afd
N
1950 : "unknown",
1951 edge->call_stmt
355fe088 1952 ? gimple_lineno ((const gimple *) edge->call_stmt)
9e145afd 1953 : -1,
6d4ab5f8 1954 badness.to_double (),
45a80bb9 1955 edge->frequency / (double)CGRAPH_FREQ_BASE);
670cd5c5 1956 if (edge->count)
16998094 1957 fprintf (dump_file," Called %" PRId64"x\n",
4c0f7679 1958 edge->count);
1ce18dc8 1959 if (dump_flags & TDF_DETAILS)
4c0f7679 1960 edge_badness (edge, true);
ca31b95f
JH
1961 }
1962
4c0f7679 1963 if (overall_size + growth > max_size
67348ccc 1964 && !DECL_DISREGARD_INLINE_LIMITS (callee->decl))
670cd5c5 1965 {
4c0f7679
JH
1966 edge->inline_failed = CIF_INLINE_UNIT_GROWTH_LIMIT;
1967 report_inline_failed_reason (edge);
4a910049 1968 resolve_noninline_speculation (&edge_heap, edge);
670cd5c5
JH
1969 continue;
1970 }
4c0f7679
JH
1971
1972 if (!want_inline_small_function_p (edge, true))
09ce3660 1973 {
4a910049 1974 resolve_noninline_speculation (&edge_heap, edge);
09ce3660
JH
1975 continue;
1976 }
09a2806f 1977
7ba03e5e
JL
1978 /* Heuristics for inlining small functions work poorly for
1979 recursive calls where we do effects similar to loop unrolling.
1980 When inlining such edge seems profitable, leave decision on
09a2806f 1981 specific inliner. */
3dafb85c 1982 if (edge->recursive_p ())
670cd5c5
JH
1983 {
1984 where = edge->caller;
1985 if (where->global.inlined_to)
1986 where = where->global.inlined_to;
4c0f7679 1987 if (!recursive_inlining (edge,
2bf86c84
JH
1988 opt_for_fn (edge->caller->decl,
1989 flag_indirect_inlining)
4c0f7679 1990 ? &new_indirect_edges : NULL))
d7d1d041
RG
1991 {
1992 edge->inline_failed = CIF_RECURSIVE_INLINING;
4a910049 1993 resolve_noninline_speculation (&edge_heap, edge);
d7d1d041
RG
1994 continue;
1995 }
40fda55b 1996 reset_edge_caches (where);
09a2806f
JH
1997 /* Recursive inliner inlines all recursive calls of the function
1998 at once. Consequently we need to update all callee keys. */
2bf86c84 1999 if (opt_for_fn (edge->caller->decl, flag_indirect_inlining))
4a910049
ML
2000 add_new_edges_to_heap (&edge_heap, new_indirect_edges);
2001 update_callee_keys (&edge_heap, where, updated_nodes);
09ce3660 2002 bitmap_clear (updated_nodes);
670cd5c5
JH
2003 }
2004 else
2005 {
4c0f7679
JH
2006 struct cgraph_node *outer_node = NULL;
2007 int depth = 0;
2008
7ba03e5e
JL
2009 /* Consider the case where self recursive function A is inlined
2010 into B. This is desired optimization in some cases, since it
2011 leads to effect similar of loop peeling and we might completely
2012 optimize out the recursive call. However we must be extra
2013 selective. */
4c0f7679
JH
2014
2015 where = edge->caller;
2016 while (where->global.inlined_to)
670cd5c5 2017 {
67348ccc 2018 if (where->decl == callee->decl)
4c0f7679
JH
2019 outer_node = where, depth++;
2020 where = where->callers->caller;
2021 }
2022 if (outer_node
2023 && !want_inline_self_recursive_call_p (edge, outer_node,
2024 true, depth))
2025 {
2026 edge->inline_failed
67348ccc 2027 = (DECL_DISREGARD_INLINE_LIMITS (edge->callee->decl)
4c0f7679 2028 ? CIF_RECURSIVE_INLINING : CIF_UNSPECIFIED);
4a910049 2029 resolve_noninline_speculation (&edge_heap, edge);
670cd5c5
JH
2030 continue;
2031 }
4c0f7679
JH
2032 else if (depth && dump_file)
2033 fprintf (dump_file, " Peeling recursion with depth %i\n", depth);
2034
9b8051b4 2035 gcc_checking_assert (!callee->global.inlined_to);
c170d40f 2036 inline_call (edge, true, &new_indirect_edges, &overall_size, true);
2bf86c84 2037 add_new_edges_to_heap (&edge_heap, new_indirect_edges);
f8e2a1ed 2038
0d92b555 2039 reset_edge_caches (edge->callee->function_symbol ());
40fda55b 2040
4a910049 2041 update_callee_keys (&edge_heap, where, updated_nodes);
670cd5c5
JH
2042 }
2043 where = edge->caller;
2044 if (where->global.inlined_to)
2045 where = where->global.inlined_to;
2046
2047 /* Our profitability metric can depend on local properties
2048 such as number of inlinable calls and size of the function body.
2049 After inlining these properties might change for the function we
2050 inlined into (since it's body size changed) and for the functions
2051 called by function we inlined (since number of it inlinable callers
2052 might change). */
4a910049 2053 update_caller_keys (&edge_heap, where, updated_nodes, NULL);
208e5afa
JH
2054 /* Offline copy count has possibly changed, recompute if profile is
2055 available. */
2056 if (max_count)
2057 {
2058 struct cgraph_node *n = cgraph_node::get (edge->callee->decl);
2059 if (n != edge->callee && n->analyzed)
2060 update_callee_keys (&edge_heap, n, updated_nodes);
2061 }
670cd5c5 2062 bitmap_clear (updated_nodes);
ca31b95f 2063
670cd5c5 2064 if (dump_file)
50fe876d 2065 {
b8698a0f 2066 fprintf (dump_file,
2f2935b6 2067 " Inlined into %s which now has time %i and size %i,"
85057983 2068 "net change of %+i.\n",
fec39fa6 2069 edge->caller->name (),
9a1e784a
ML
2070 inline_summaries->get (edge->caller)->time,
2071 inline_summaries->get (edge->caller)->size,
85057983 2072 overall_size - old_size);
50fe876d 2073 }
85057983 2074 if (min_size > overall_size)
b7c27d51 2075 {
85057983
JH
2076 min_size = overall_size;
2077 max_size = compute_max_insns (min_size);
b7c27d51
JH
2078
2079 if (dump_file)
85057983 2080 fprintf (dump_file, "New minimal size reached: %i\n", min_size);
b7c27d51 2081 }
ca31b95f 2082 }
3e293154 2083
632b4f8e 2084 free_growth_caches ();
09a2806f
JH
2085 if (dump_file)
2086 fprintf (dump_file,
2087 "Unit growth for small function inlining: %i->%i (%i%%)\n",
632b4f8e
JH
2088 initial_size, overall_size,
2089 initial_size ? overall_size * 100 / (initial_size) - 100: 0);
670cd5c5 2090 BITMAP_FREE (updated_nodes);
3dafb85c 2091 symtab->remove_edge_removal_hook (edge_removal_hook_holder);
ca31b95f
JH
2092}
2093
09a2806f
JH
2094/* Flatten NODE. Performed both during early inlining and
2095 at IPA inlining time. */
af961c7f
RG
2096
2097static void
632b4f8e 2098flatten_function (struct cgraph_node *node, bool early)
af961c7f
RG
2099{
2100 struct cgraph_edge *e;
2101
2102 /* We shouldn't be called recursively when we are being processed. */
67348ccc 2103 gcc_assert (node->aux == NULL);
af961c7f 2104
67348ccc 2105 node->aux = (void *) node;
af961c7f
RG
2106
2107 for (e = node->callees; e; e = e->next_callee)
2108 {
2109 struct cgraph_node *orig_callee;
d52f5295 2110 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
af961c7f 2111
af961c7f 2112 /* We've hit cycle? It is time to give up. */
67348ccc 2113 if (callee->aux)
af961c7f
RG
2114 {
2115 if (dump_file)
2116 fprintf (dump_file,
2117 "Not inlining %s into %s to avoid cycle.\n",
2a72a953
DM
2118 xstrdup_for_dump (callee->name ()),
2119 xstrdup_for_dump (e->caller->name ()));
af961c7f
RG
2120 e->inline_failed = CIF_RECURSIVE_INLINING;
2121 continue;
2122 }
2123
2124 /* When the edge is already inlined, we just need to recurse into
2125 it in order to fully flatten the leaves. */
2126 if (!e->inline_failed)
2127 {
a5b1779f 2128 flatten_function (callee, early);
af961c7f
RG
2129 continue;
2130 }
2131
4c0f7679
JH
2132 /* Flatten attribute needs to be processed during late inlining. For
2133 extra code quality we however do flattening during early optimization,
2134 too. */
632b4f8e 2135 if (!early
4c0f7679
JH
2136 ? !can_inline_edge_p (e, true)
2137 : !can_early_inline_edge_p (e))
2138 continue;
2139
3dafb85c 2140 if (e->recursive_p ())
af961c7f
RG
2141 {
2142 if (dump_file)
2143 fprintf (dump_file, "Not inlining: recursive call.\n");
2144 continue;
2145 }
2146
67348ccc
DM
2147 if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
2148 != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
59e0c6b7
RG
2149 {
2150 if (dump_file)
2151 fprintf (dump_file, "Not inlining: SSA form does not match.\n");
2152 continue;
2153 }
2154
af961c7f
RG
2155 /* Inline the edge and flatten the inline clone. Avoid
2156 recursing through the original node if the node was cloned. */
2157 if (dump_file)
2158 fprintf (dump_file, " Inlining %s into %s.\n",
2a72a953
DM
2159 xstrdup_for_dump (callee->name ()),
2160 xstrdup_for_dump (e->caller->name ()));
a5b1779f 2161 orig_callee = callee;
c170d40f 2162 inline_call (e, true, NULL, NULL, false);
af961c7f 2163 if (e->callee != orig_callee)
67348ccc 2164 orig_callee->aux = (void *) node;
632b4f8e 2165 flatten_function (e->callee, early);
af961c7f 2166 if (e->callee != orig_callee)
67348ccc 2167 orig_callee->aux = NULL;
af961c7f
RG
2168 }
2169
67348ccc 2170 node->aux = NULL;
c170d40f
JH
2171 if (!node->global.inlined_to)
2172 inline_update_overall_summary (node);
af961c7f
RG
2173}
2174
a81b0a3d
JH
2175/* Inline NODE to all callers. Worker for cgraph_for_node_and_aliases.
2176 DATA points to number of calls originally found so we avoid infinite
2177 recursion. */
2178
2179static bool
2180inline_to_all_callers (struct cgraph_node *node, void *data)
2181{
2182 int *num_calls = (int *)data;
1bbb87c4
JH
2183 bool callee_removed = false;
2184
a81b0a3d
JH
2185 while (node->callers && !node->global.inlined_to)
2186 {
2187 struct cgraph_node *caller = node->callers->caller;
2188
1af8bfe5
JH
2189 if (!can_inline_edge_p (node->callers, true)
2190 || node->callers->recursive_p ())
2191 {
2192 if (dump_file)
2193 fprintf (dump_file, "Uninlinable call found; giving up.\n");
2194 *num_calls = 0;
2195 return false;
2196 }
2197
a81b0a3d
JH
2198 if (dump_file)
2199 {
2200 fprintf (dump_file,
2201 "\nInlining %s size %i.\n",
fec39fa6 2202 node->name (),
9a1e784a 2203 inline_summaries->get (node)->size);
a81b0a3d
JH
2204 fprintf (dump_file,
2205 " Called once from %s %i insns.\n",
fec39fa6 2206 node->callers->caller->name (),
9a1e784a 2207 inline_summaries->get (node->callers->caller)->size);
a81b0a3d
JH
2208 }
2209
1bbb87c4 2210 inline_call (node->callers, true, NULL, NULL, true, &callee_removed);
a81b0a3d
JH
2211 if (dump_file)
2212 fprintf (dump_file,
2213 " Inlined into %s which now has %i size\n",
fec39fa6 2214 caller->name (),
9a1e784a 2215 inline_summaries->get (caller)->size);
a81b0a3d
JH
2216 if (!(*num_calls)--)
2217 {
2218 if (dump_file)
2219 fprintf (dump_file, "New calls found; giving up.\n");
1bbb87c4 2220 return callee_removed;
a81b0a3d 2221 }
1bbb87c4
JH
2222 if (callee_removed)
2223 return true;
a81b0a3d
JH
2224 }
2225 return false;
2226}
2227
e86a910f
JH
2228/* Output overall time estimate. */
2229static void
2230dump_overall_stats (void)
2231{
a9243bfc 2232 int64_t sum_weighted = 0, sum = 0;
e86a910f
JH
2233 struct cgraph_node *node;
2234
2235 FOR_EACH_DEFINED_FUNCTION (node)
2236 if (!node->global.inlined_to
2237 && !node->alias)
2238 {
9a1e784a 2239 int time = inline_summaries->get (node)->time;
e86a910f
JH
2240 sum += time;
2241 sum_weighted += time * node->count;
2242 }
2243 fprintf (dump_file, "Overall time estimate: "
16998094
JM
2244 "%" PRId64" weighted by profile: "
2245 "%" PRId64"\n", sum, sum_weighted);
e86a910f
JH
2246}
2247
2248/* Output some useful stats about inlining. */
2249
2250static void
2251dump_inline_stats (void)
2252{
a9243bfc
RB
2253 int64_t inlined_cnt = 0, inlined_indir_cnt = 0;
2254 int64_t inlined_virt_cnt = 0, inlined_virt_indir_cnt = 0;
2255 int64_t noninlined_cnt = 0, noninlined_indir_cnt = 0;
2256 int64_t noninlined_virt_cnt = 0, noninlined_virt_indir_cnt = 0;
2257 int64_t inlined_speculative = 0, inlined_speculative_ply = 0;
2258 int64_t indirect_poly_cnt = 0, indirect_cnt = 0;
2259 int64_t reason[CIF_N_REASONS][3];
e86a910f
JH
2260 int i;
2261 struct cgraph_node *node;
2262
2263 memset (reason, 0, sizeof (reason));
2264 FOR_EACH_DEFINED_FUNCTION (node)
2265 {
2266 struct cgraph_edge *e;
2267 for (e = node->callees; e; e = e->next_callee)
2268 {
2269 if (e->inline_failed)
2270 {
2271 reason[(int) e->inline_failed][0] += e->count;
2272 reason[(int) e->inline_failed][1] += e->frequency;
2273 reason[(int) e->inline_failed][2] ++;
2274 if (DECL_VIRTUAL_P (e->callee->decl))
2275 {
2276 if (e->indirect_inlining_edge)
2277 noninlined_virt_indir_cnt += e->count;
2278 else
2279 noninlined_virt_cnt += e->count;
2280 }
2281 else
2282 {
2283 if (e->indirect_inlining_edge)
2284 noninlined_indir_cnt += e->count;
2285 else
2286 noninlined_cnt += e->count;
2287 }
2288 }
2289 else
2290 {
2291 if (e->speculative)
2292 {
2293 if (DECL_VIRTUAL_P (e->callee->decl))
2294 inlined_speculative_ply += e->count;
2295 else
2296 inlined_speculative += e->count;
2297 }
2298 else if (DECL_VIRTUAL_P (e->callee->decl))
2299 {
2300 if (e->indirect_inlining_edge)
2301 inlined_virt_indir_cnt += e->count;
2302 else
2303 inlined_virt_cnt += e->count;
2304 }
2305 else
2306 {
2307 if (e->indirect_inlining_edge)
2308 inlined_indir_cnt += e->count;
2309 else
2310 inlined_cnt += e->count;
2311 }
2312 }
2313 }
2314 for (e = node->indirect_calls; e; e = e->next_callee)
2315 if (e->indirect_info->polymorphic)
2316 indirect_poly_cnt += e->count;
2317 else
2318 indirect_cnt += e->count;
2319 }
2320 if (max_count)
2321 {
2322 fprintf (dump_file,
16998094
JM
2323 "Inlined %" PRId64 " + speculative "
2324 "%" PRId64 " + speculative polymorphic "
2325 "%" PRId64 " + previously indirect "
2326 "%" PRId64 " + virtual "
2327 "%" PRId64 " + virtual and previously indirect "
2328 "%" PRId64 "\n" "Not inlined "
2329 "%" PRId64 " + previously indirect "
2330 "%" PRId64 " + virtual "
2331 "%" PRId64 " + virtual and previously indirect "
2332 "%" PRId64 " + stil indirect "
2333 "%" PRId64 " + still indirect polymorphic "
2334 "%" PRId64 "\n", inlined_cnt,
e86a910f
JH
2335 inlined_speculative, inlined_speculative_ply,
2336 inlined_indir_cnt, inlined_virt_cnt, inlined_virt_indir_cnt,
2337 noninlined_cnt, noninlined_indir_cnt, noninlined_virt_cnt,
2338 noninlined_virt_indir_cnt, indirect_cnt, indirect_poly_cnt);
2339 fprintf (dump_file,
16998094 2340 "Removed speculations %" PRId64 "\n",
e86a910f
JH
2341 spec_rem);
2342 }
2343 dump_overall_stats ();
2344 fprintf (dump_file, "\nWhy inlining failed?\n");
2345 for (i = 0; i < CIF_N_REASONS; i++)
2346 if (reason[i][2])
16998094 2347 fprintf (dump_file, "%-50s: %8i calls, %8i freq, %" PRId64" count\n",
e86a910f
JH
2348 cgraph_inline_failed_string ((cgraph_inline_failed_t) i),
2349 (int) reason[i][2], (int) reason[i][1], reason[i][0]);
2350}
2351
ca31b95f
JH
2352/* Decide on the inlining. We do so in the topological order to avoid
2353 expenses on updating data structures. */
2354
c2924966 2355static unsigned int
4c0f7679 2356ipa_inline (void)
ca31b95f
JH
2357{
2358 struct cgraph_node *node;
2359 int nnodes;
b591a8b7 2360 struct cgraph_node **order;
ca31b95f 2361 int i;
09ce3660 2362 int cold;
8a41354f
JH
2363 bool remove_functions = false;
2364
2365 if (!optimize)
2366 return 0;
ca31b95f 2367
6d4ab5f8
JH
2368 cgraph_freq_base_rec = (sreal) 1 / (sreal) CGRAPH_FREQ_BASE;
2369 percent_rec = (sreal) 1 / (sreal) 100;
2370
3dafb85c 2371 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
b591a8b7 2372
5ee53a06 2373 if (in_lto_p && optimize)
fb3f88cc 2374 ipa_update_after_lto_read ();
0dbca537 2375
10a5dd5d
JH
2376 if (dump_file)
2377 dump_inline_summaries (dump_file);
670cd5c5 2378
af8bca3c 2379 nnodes = ipa_reverse_postorder (order);
ca31b95f 2380
65c70e6b 2381 FOR_EACH_FUNCTION (node)
7ce7e4d4
JH
2382 {
2383 node->aux = 0;
2384
2385 /* Recompute the default reasons for inlining because they may have
2386 changed during merging. */
2387 if (in_lto_p)
2388 {
2389 for (cgraph_edge *e = node->callees; e; e = e->next_callee)
2390 {
2391 gcc_assert (e->inline_failed);
2392 initialize_inline_failed (e);
2393 }
2394 for (cgraph_edge *e = node->indirect_calls; e; e = e->next_callee)
2395 initialize_inline_failed (e);
2396 }
2397 }
ca31b95f
JH
2398
2399 if (dump_file)
af961c7f 2400 fprintf (dump_file, "\nFlattening functions:\n");
ca31b95f 2401
af961c7f
RG
2402 /* In the first pass handle functions to be flattened. Do this with
2403 a priority so none of our later choices will make this impossible. */
2404 for (i = nnodes - 1; i >= 0; i--)
ca31b95f 2405 {
af961c7f
RG
2406 node = order[i];
2407
09a2806f 2408 /* Handle nodes to be flattened.
af961c7f
RG
2409 Ideally when processing callees we stop inlining at the
2410 entry of cycles, possibly cloning that entry point and
2411 try to flatten itself turning it into a self-recursive
2412 function. */
2413 if (lookup_attribute ("flatten",
67348ccc 2414 DECL_ATTRIBUTES (node->decl)) != NULL)
f8e2a1ed 2415 {
ca31b95f 2416 if (dump_file)
b8698a0f 2417 fprintf (dump_file,
fec39fa6 2418 "Flattening %s\n", node->name ());
632b4f8e 2419 flatten_function (node, false);
ca31b95f 2420 }
ca31b95f 2421 }
e86a910f
JH
2422 if (dump_file)
2423 dump_overall_stats ();
ca31b95f 2424
4c0f7679 2425 inline_small_functions ();
e70670cf 2426
17e0fc92
JH
2427 gcc_assert (symtab->state == IPA_SSA);
2428 symtab->state = IPA_SSA_AFTER_INLINING;
2429 /* Do first after-inlining removal. We want to remove all "stale" extern
2430 inline functions and virtual functions so we really know what is called
2431 once. */
2432 symtab->remove_unreachable_nodes (dump_file);
4c0f7679 2433 free (order);
ca31b95f 2434
100411f8
JH
2435 /* Inline functions with a property that after inlining into all callers the
2436 code size will shrink because the out-of-line copy is eliminated.
2437 We do this regardless on the callee size as long as function growth limits
2438 are met. */
09ce3660
JH
2439 if (dump_file)
2440 fprintf (dump_file,
17e0fc92
JH
2441 "\nDeciding on functions to be inlined into all callers and "
2442 "removing useless speculations:\n");
09ce3660
JH
2443
2444 /* Inlining one function called once has good chance of preventing
2445 inlining other function into the same callee. Ideally we should
2446 work in priority order, but probably inlining hot functions first
2447 is good cut without the extra pain of maintaining the queue.
2448
2449 ??? this is not really fitting the bill perfectly: inlining function
2450 into callee often leads to better optimization of callee due to
2451 increased context for optimization.
2452 For example if main() function calls a function that outputs help
2453 and then function that does the main optmization, we should inline
2454 the second with priority even if both calls are cold by themselves.
2455
2456 We probably want to implement new predicate replacing our use of
2457 maybe_hot_edge interpreted as maybe_hot_edge || callee is known
2458 to be hot. */
2459 for (cold = 0; cold <= 1; cold ++)
355866de 2460 {
09ce3660 2461 FOR_EACH_DEFINED_FUNCTION (node)
ca31b95f 2462 {
09ce3660
JH
2463 struct cgraph_edge *edge, *next;
2464 bool update=false;
2465
2466 for (edge = node->callees; edge; edge = next)
ca31b95f 2467 {
09ce3660
JH
2468 next = edge->next_callee;
2469 if (edge->speculative && !speculation_useful_p (edge, false))
e3c7b49c 2470 {
3dafb85c 2471 edge->resolve_speculation ();
e86a910f 2472 spec_rem += edge->count;
09ce3660 2473 update = true;
8a41354f 2474 remove_functions = true;
09ce3660
JH
2475 }
2476 }
2477 if (update)
2478 {
2479 struct cgraph_node *where = node->global.inlined_to
2480 ? node->global.inlined_to : node;
09ce3660
JH
2481 reset_edge_caches (where);
2482 inline_update_overall_summary (where);
2483 }
2bf86c84 2484 if (want_inline_function_to_all_callers_p (node, cold))
09ce3660
JH
2485 {
2486 int num_calls = 0;
1ede94c5
JH
2487 node->call_for_symbol_and_aliases (sum_callers, &num_calls,
2488 true);
2489 while (node->call_for_symbol_and_aliases
17e0fc92 2490 (inline_to_all_callers, &num_calls, true))
1bbb87c4 2491 ;
f019b607 2492 remove_functions = true;
ca31b95f
JH
2493 }
2494 }
2495 }
2496
f8e2a1ed 2497 /* Free ipa-prop structures if they are no longer needed. */
5ee53a06 2498 if (optimize)
e33c6cd6 2499 ipa_free_all_structures_after_iinln ();
f8e2a1ed 2500
ca31b95f 2501 if (dump_file)
e86a910f
JH
2502 {
2503 fprintf (dump_file,
2504 "\nInlined %i calls, eliminated %i functions\n\n",
2505 ncalls_inlined, nfunctions_inlined);
2506 dump_inline_stats ();
2507 }
09a2806f 2508
898b8927
JH
2509 if (dump_file)
2510 dump_inline_summaries (dump_file);
10a5dd5d
JH
2511 /* In WPA we use inline summaries for partitioning process. */
2512 if (!flag_wpa)
2513 inline_free_summary ();
8a41354f 2514 return remove_functions ? TODO_remove_functions : 0;
ca31b95f
JH
2515}
2516
275b4baa
RG
2517/* Inline always-inline function calls in NODE. */
2518
2519static bool
4c0f7679 2520inline_always_inline_functions (struct cgraph_node *node)
275b4baa
RG
2521{
2522 struct cgraph_edge *e;
2523 bool inlined = false;
2524
2525 for (e = node->callees; e; e = e->next_callee)
2526 {
d52f5295 2527 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
67348ccc 2528 if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl))
275b4baa
RG
2529 continue;
2530
3dafb85c 2531 if (e->recursive_p ())
275b4baa
RG
2532 {
2533 if (dump_file)
4c0f7679 2534 fprintf (dump_file, " Not inlining recursive call to %s.\n",
fec39fa6 2535 e->callee->name ());
275b4baa
RG
2536 e->inline_failed = CIF_RECURSIVE_INLINING;
2537 continue;
2538 }
2539
4c0f7679 2540 if (!can_early_inline_edge_p (e))
bef8491a
ST
2541 {
2542 /* Set inlined to true if the callee is marked "always_inline" but
2543 is not inlinable. This will allow flagging an error later in
2544 expand_call_inline in tree-inline.c. */
2545 if (lookup_attribute ("always_inline",
67348ccc 2546 DECL_ATTRIBUTES (callee->decl)) != NULL)
bef8491a
ST
2547 inlined = true;
2548 continue;
2549 }
275b4baa
RG
2550
2551 if (dump_file)
4c0f7679 2552 fprintf (dump_file, " Inlining %s into %s (always_inline).\n",
2a72a953
DM
2553 xstrdup_for_dump (e->callee->name ()),
2554 xstrdup_for_dump (e->caller->name ()));
c170d40f 2555 inline_call (e, true, NULL, NULL, false);
275b4baa
RG
2556 inlined = true;
2557 }
c170d40f
JH
2558 if (inlined)
2559 inline_update_overall_summary (node);
275b4baa
RG
2560
2561 return inlined;
2562}
2563
ca31b95f 2564/* Decide on the inlining. We do so in the topological order to avoid
af961c7f 2565 expenses on updating data structures. */
ca31b95f 2566
7fa49e7b 2567static bool
4c0f7679 2568early_inline_small_functions (struct cgraph_node *node)
ca31b95f
JH
2569{
2570 struct cgraph_edge *e;
d63db217 2571 bool inlined = false;
7fa49e7b 2572
275b4baa 2573 for (e = node->callees; e; e = e->next_callee)
c3056c2d 2574 {
d52f5295 2575 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
9a1e784a 2576 if (!inline_summaries->get (callee)->inlinable
4c0f7679 2577 || !e->inline_failed)
275b4baa
RG
2578 continue;
2579
2580 /* Do not consider functions not declared inline. */
67348ccc 2581 if (!DECL_DECLARED_INLINE_P (callee->decl)
2bf86c84
JH
2582 && !opt_for_fn (node->decl, flag_inline_small_functions)
2583 && !opt_for_fn (node->decl, flag_inline_functions))
275b4baa
RG
2584 continue;
2585
c3056c2d 2586 if (dump_file)
275b4baa 2587 fprintf (dump_file, "Considering inline candidate %s.\n",
fec39fa6 2588 callee->name ());
ca31b95f 2589
4c0f7679
JH
2590 if (!can_early_inline_edge_p (e))
2591 continue;
2592
3dafb85c 2593 if (e->recursive_p ())
275b4baa
RG
2594 {
2595 if (dump_file)
4c0f7679 2596 fprintf (dump_file, " Not inlining: recursive call.\n");
22ad64b6 2597 continue;
275b4baa 2598 }
af961c7f 2599
4c0f7679 2600 if (!want_early_inline_function_p (e))
275b4baa 2601 continue;
ca31b95f 2602
4c0f7679
JH
2603 if (dump_file)
2604 fprintf (dump_file, " Inlining %s into %s.\n",
2a72a953
DM
2605 xstrdup_for_dump (callee->name ()),
2606 xstrdup_for_dump (e->caller->name ()));
c170d40f 2607 inline_call (e, true, NULL, NULL, true);
4c0f7679 2608 inlined = true;
38bc76da 2609 }
275b4baa 2610
7fa49e7b 2611 return inlined;
ca31b95f
JH
2612}
2613
be55bfe6 2614unsigned int
be3c16c4 2615early_inliner (function *fun)
d63db217 2616{
d52f5295 2617 struct cgraph_node *node = cgraph_node::get (current_function_decl);
10a5dd5d 2618 struct cgraph_edge *edge;
7fa49e7b 2619 unsigned int todo = 0;
796bda22 2620 int iterations = 0;
275b4baa 2621 bool inlined = false;
d63db217 2622
1da2ed5f 2623 if (seen_error ())
c2924966 2624 return 0;
af961c7f 2625
ecb62563
JH
2626 /* Do nothing if datastructures for ipa-inliner are already computed. This
2627 happens when some pass decides to construct new function and
2628 cgraph_add_new_function calls lowering passes and early optimization on
2629 it. This may confuse ourself when early inliner decide to inline call to
2630 function clone, because function clones don't have parameter list in
2631 ipa-prop matching their signature. */
dd912cb8 2632 if (ipa_node_params_sum)
ecb62563
JH
2633 return 0;
2634
275b4baa 2635#ifdef ENABLE_CHECKING
d52f5295 2636 node->verify ();
275b4baa 2637#endif
d122681a 2638 node->remove_all_references ();
275b4baa 2639
c291690e
IE
2640 /* Rebuild this reference because it dosn't depend on
2641 function's body and it's required to pass cgraph_node
2642 verification. */
2643 if (node->instrumented_version
2644 && !node->instrumentation_clone)
2645 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
2646
275b4baa
RG
2647 /* Even when not optimizing or not inlining inline always-inline
2648 functions. */
4c0f7679 2649 inlined = inline_always_inline_functions (node);
275b4baa 2650
af961c7f
RG
2651 if (!optimize
2652 || flag_no_inline
4c0f7679
JH
2653 || !flag_early_inlining
2654 /* Never inline regular functions into always-inline functions
2655 during incremental inlining. This sucks as functions calling
2656 always inline functions will get less optimized, but at the
2657 same time inlining of functions calling always inline
09a2806f 2658 function into an always inline function might introduce
4c0f7679
JH
2659 cycles of edges to be always inlined in the callgraph.
2660
2661 We might want to be smarter and just avoid this type of inlining. */
d67bce7c
JH
2662 || (DECL_DISREGARD_INLINE_LIMITS (node->decl)
2663 && lookup_attribute ("always_inline",
2664 DECL_ATTRIBUTES (node->decl))))
275b4baa
RG
2665 ;
2666 else if (lookup_attribute ("flatten",
67348ccc 2667 DECL_ATTRIBUTES (node->decl)) != NULL)
7fa49e7b 2668 {
275b4baa
RG
2669 /* When the function is marked to be flattened, recursively inline
2670 all calls in it. */
2671 if (dump_file)
2672 fprintf (dump_file,
fec39fa6 2673 "Flattening %s\n", node->name ());
632b4f8e 2674 flatten_function (node, true);
275b4baa 2675 inlined = true;
7fa49e7b 2676 }
af961c7f
RG
2677 else
2678 {
d67bce7c
JH
2679 /* If some always_inline functions was inlined, apply the changes.
2680 This way we will not account always inline into growth limits and
2681 moreover we will inline calls from always inlines that we skipped
2682 previously becuase of conditional above. */
2683 if (inlined)
2684 {
2685 timevar_push (TV_INTEGRATION);
2686 todo |= optimize_inline_calls (current_function_decl);
1cf06f1e
MP
2687 /* optimize_inline_calls call above might have introduced new
2688 statements that don't have inline parameters computed. */
2689 for (edge = node->callees; edge; edge = edge->next_callee)
2690 {
2691 if (inline_edge_summary_vec.length () > (unsigned) edge->uid)
2692 {
2693 struct inline_edge_summary *es = inline_edge_summary (edge);
2694 es->call_stmt_size
2695 = estimate_num_insns (edge->call_stmt, &eni_size_weights);
2696 es->call_stmt_time
2697 = estimate_num_insns (edge->call_stmt, &eni_time_weights);
2698 }
2699 }
d67bce7c
JH
2700 inline_update_overall_summary (node);
2701 inlined = false;
2702 timevar_pop (TV_INTEGRATION);
2703 }
af961c7f
RG
2704 /* We iterate incremental inlining to get trivial cases of indirect
2705 inlining. */
2706 while (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS)
4c0f7679 2707 && early_inline_small_functions (node))
af961c7f
RG
2708 {
2709 timevar_push (TV_INTEGRATION);
2710 todo |= optimize_inline_calls (current_function_decl);
4c0f7679
JH
2711
2712 /* Technically we ought to recompute inline parameters so the new
2713 iteration of early inliner works as expected. We however have
2714 values approximately right and thus we only need to update edge
2715 info that might be cleared out for newly discovered edges. */
2716 for (edge = node->callees; edge; edge = edge->next_callee)
2717 {
d5e254e1
IE
2718 /* We have no summary for new bound store calls yet. */
2719 if (inline_edge_summary_vec.length () > (unsigned)edge->uid)
2720 {
2721 struct inline_edge_summary *es = inline_edge_summary (edge);
2722 es->call_stmt_size
2723 = estimate_num_insns (edge->call_stmt, &eni_size_weights);
2724 es->call_stmt_time
2725 = estimate_num_insns (edge->call_stmt, &eni_time_weights);
2726 }
67348ccc 2727 if (edge->callee->decl
4de09b85 2728 && !gimple_check_call_matching_types (
67348ccc 2729 edge->call_stmt, edge->callee->decl, false))
89faf322 2730 edge->call_stmt_cannot_inline_p = true;
4c0f7679 2731 }
2f2a7d15
DC
2732 if (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS) - 1)
2733 inline_update_overall_summary (node);
af961c7f 2734 timevar_pop (TV_INTEGRATION);
275b4baa
RG
2735 iterations++;
2736 inlined = false;
af961c7f
RG
2737 }
2738 if (dump_file)
2739 fprintf (dump_file, "Iterations: %i\n", iterations);
2740 }
2741
275b4baa
RG
2742 if (inlined)
2743 {
2744 timevar_push (TV_INTEGRATION);
2745 todo |= optimize_inline_calls (current_function_decl);
2746 timevar_pop (TV_INTEGRATION);
2747 }
2748
be55bfe6 2749 fun->always_inline_functions_inlined = true;
d63db217 2750
af961c7f 2751 return todo;
d63db217
JH
2752}
2753
be3c16c4
DC
2754/* Do inlining of small functions. Doing so early helps profiling and other
2755 passes to be somewhat more effective and avoids some code duplication in
2756 later real inlining pass for testcases with very many function calls. */
2757
2758namespace {
2759
2760const pass_data pass_data_early_inline =
2761{
2762 GIMPLE_PASS, /* type */
2763 "einline", /* name */
2764 OPTGROUP_INLINE, /* optinfo_flags */
2765 TV_EARLY_INLINING, /* tv_id */
2766 PROP_ssa, /* properties_required */
2767 0, /* properties_provided */
2768 0, /* properties_destroyed */
2769 0, /* todo_flags_start */
2770 0, /* todo_flags_finish */
2771};
2772
2773class pass_early_inline : public gimple_opt_pass
2774{
2775public:
2776 pass_early_inline (gcc::context *ctxt)
2777 : gimple_opt_pass (pass_data_early_inline, ctxt)
2778 {}
2779
2780 /* opt_pass methods: */
2781 virtual unsigned int execute (function *);
2782
2783}; // class pass_early_inline
2784
2785unsigned int
2786pass_early_inline::execute (function *fun)
2787{
2788 return early_inliner (fun);
2789}
2790
27a4cd48
DM
2791} // anon namespace
2792
2793gimple_opt_pass *
2794make_pass_early_inline (gcc::context *ctxt)
2795{
2796 return new pass_early_inline (ctxt);
2797}
2798
27a4cd48
DM
2799namespace {
2800
2801const pass_data pass_data_ipa_inline =
873aa8f5 2802{
27a4cd48
DM
2803 IPA_PASS, /* type */
2804 "inline", /* name */
2805 OPTGROUP_INLINE, /* optinfo_flags */
27a4cd48
DM
2806 TV_IPA_INLINING, /* tv_id */
2807 0, /* properties_required */
2808 0, /* properties_provided */
2809 0, /* properties_destroyed */
8605403e 2810 0, /* todo_flags_start */
8a41354f 2811 ( TODO_dump_symtab ), /* todo_flags_finish */
ca31b95f 2812};
27a4cd48
DM
2813
2814class pass_ipa_inline : public ipa_opt_pass_d
2815{
2816public:
c3284718
RS
2817 pass_ipa_inline (gcc::context *ctxt)
2818 : ipa_opt_pass_d (pass_data_ipa_inline, ctxt,
2819 inline_generate_summary, /* generate_summary */
2820 inline_write_summary, /* write_summary */
2821 inline_read_summary, /* read_summary */
2822 NULL, /* write_optimization_summary */
2823 NULL, /* read_optimization_summary */
2824 NULL, /* stmt_fixup */
2825 0, /* function_transform_todo_flags_start */
2826 inline_transform, /* function_transform */
2827 NULL) /* variable_transform */
27a4cd48
DM
2828 {}
2829
2830 /* opt_pass methods: */
be55bfe6 2831 virtual unsigned int execute (function *) { return ipa_inline (); }
27a4cd48
DM
2832
2833}; // class pass_ipa_inline
2834
2835} // anon namespace
2836
2837ipa_opt_pass_d *
2838make_pass_ipa_inline (gcc::context *ctxt)
2839{
2840 return new pass_ipa_inline (ctxt);
2841}