]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ipa-inline.cc
c++, mingw: Fix up types of dtor hooks to __cxa_{,thread_}atexit/__cxa_throw on mingw...
[thirdparty/gcc.git] / gcc / ipa-inline.cc
CommitLineData
ca31b95f 1/* Inlining decision heuristics.
a945c346 2 Copyright (C) 2003-2024 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
67914693 62 Because of lack of whole unit knowledge, the pass cannot really make
4c0f7679
JH
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"
957060b5
AM
96#include "target.h"
97#include "rtl.h"
ca31b95f 98#include "tree.h"
c7131fb2 99#include "gimple.h"
957060b5
AM
100#include "alloc-pool.h"
101#include "tree-pass.h"
102#include "gimple-ssa.h"
103#include "cgraph.h"
957060b5 104#include "lto-streamer.h"
d8a2d370
DN
105#include "trans-mem.h"
106#include "calls.h"
ca31b95f 107#include "tree-inline.h"
59f2e9d8 108#include "profile.h"
dd912cb8 109#include "symbol-summary.h"
8bc5448f 110#include "tree-vrp.h"
c8742849
MJ
111#include "sreal.h"
112#include "ipa-cp.h"
3e293154 113#include "ipa-prop.h"
27d020cf 114#include "ipa-fnsummary.h"
03dfc36d 115#include "ipa-inline.h"
af8bca3c 116#include "ipa-utils.h"
be3c16c4 117#include "auto-profile.h"
9b2b7279 118#include "builtins.h"
4a910049 119#include "fibonacci_heap.h"
314e6352
ML
120#include "stringpool.h"
121#include "attribs.h"
45b2222a 122#include "asan.h"
f0a90c7d 123#include "ipa-strub.h"
4a910049 124
68c0df8d
JH
125/* Inliner uses greedy algorithm to inline calls in a priority order.
126 Badness is used as the key in a Fibonacci heap which roughly corresponds
127 to negation of benefit to cost ratios.
128 In case multiple calls has same priority we want to stabilize the outcomes
129 for which we use ids. */
130class inline_badness
131{
132public:
133 sreal badness;
134 int uid;
135 inline_badness ()
136 : badness (sreal::min ()), uid (0)
137 {
138 }
139 inline_badness (cgraph_edge *e, sreal b)
140 : badness (b), uid (e->get_uid ())
141 {
142 }
143 bool operator<= (const inline_badness &other)
144 {
145 if (badness != other.badness)
146 return badness <= other.badness;
147 return uid <= other.uid;
148 }
149 bool operator== (const inline_badness &other)
150 {
151 return badness == other.badness && uid == other.uid;
152 }
153 bool operator!= (const inline_badness &other)
154 {
155 return badness != other.badness || uid != other.uid;
156 }
157 bool operator< (const inline_badness &other)
158 {
159 if (badness != other.badness)
160 return badness < other.badness;
161 return uid < other.uid;
162 }
163 bool operator> (const inline_badness &other)
164 {
165 if (badness != other.badness)
166 return badness > other.badness;
167 return uid > other.uid;
168 }
169};
170
171typedef fibonacci_heap <inline_badness, cgraph_edge> edge_heap_t;
172typedef fibonacci_node <inline_badness, cgraph_edge> edge_heap_node_t;
85057983 173
ca31b95f 174/* Statistics we collect about inlining algorithm. */
85057983 175static int overall_size;
3995f3a2
JH
176static profile_count max_count;
177static profile_count spec_rem;
ca31b95f 178
4c0f7679
JH
179/* Return false when inlining edge E would lead to violating
180 limits on function unit growth or stack usage growth.
181
182 The relative function body growth limit is present generally
09a2806f 183 to avoid problems with non-linear behavior of the compiler.
4c0f7679
JH
184 To allow inlining huge functions into tiny wrapper, the limit
185 is always based on the bigger of the two functions considered.
186
187 For stack growth limits we always base the growth in stack usage
188 of the callers. We want to prevent applications from segfaulting
189 on stack overflow when functions with huge stack frames gets
190 inlined. */
ca31b95f
JH
191
192static bool
4c0f7679 193caller_growth_limits (struct cgraph_edge *e)
ca31b95f 194{
d7d1d041 195 struct cgraph_node *to = e->caller;
d52f5295 196 struct cgraph_node *what = e->callee->ultimate_alias_target ();
ca31b95f 197 int newsize;
4c0f7679
JH
198 int limit = 0;
199 HOST_WIDE_INT stack_size_limit = 0, inlined_stack;
f658ad30 200 ipa_size_summary *outer_info = ipa_size_summaries->get (to);
4c0f7679
JH
201
202 /* Look for function e->caller is inlined to. While doing
203 so work out the largest function body on the way. As
204 described above, we want to base our function growth
205 limits based on that. Not on the self size of the
206 outer function, not on the self size of inline code
207 we immediately inline to. This is the most relaxed
208 interpretation of the rule "do not grow large functions
209 too much in order to prevent compiler from exploding". */
09dfe187 210 while (true)
4c0f7679 211 {
f658ad30
JH
212 ipa_size_summary *size_info = ipa_size_summaries->get (to);
213 if (limit < size_info->self_size)
214 limit = size_info->self_size;
215 if (stack_size_limit < size_info->estimated_self_stack_size)
216 stack_size_limit = size_info->estimated_self_stack_size;
a62bfab5 217 if (to->inlined_to)
4c0f7679 218 to = to->callers->caller;
09dfe187
JH
219 else
220 break;
4c0f7679 221 }
6971d714 222
f658ad30
JH
223 ipa_fn_summary *what_info = ipa_fn_summaries->get (what);
224 ipa_size_summary *what_size_info = ipa_size_summaries->get (what);
e7f23018 225
f658ad30
JH
226 if (limit < what_size_info->self_size)
227 limit = what_size_info->self_size;
ca31b95f 228
1e83bd70 229 limit += limit * opt_for_fn (to->decl, param_large_function_growth) / 100;
ca31b95f 230
6971d714
RG
231 /* Check the size after inlining against the function limits. But allow
232 the function to shrink if it went over the limits by forced inlining. */
03dfc36d 233 newsize = estimate_size_after_inlining (to, e);
f658ad30 234 if (newsize >= ipa_size_summaries->get (what)->size
709d7838
LXH
235 && newsize > opt_for_fn (to->decl, param_large_function_insns)
236 && newsize > limit)
ca31b95f 237 {
4c0f7679 238 e->inline_failed = CIF_LARGE_FUNCTION_GROWTH_LIMIT;
ca31b95f
JH
239 return false;
240 }
ff28a94d 241
09dfe187
JH
242 if (!what_info->estimated_stack_size)
243 return true;
244
09a2806f
JH
245 /* FIXME: Stack size limit often prevents inlining in Fortran programs
246 due to large i/o datastructures used by the Fortran front-end.
4c0f7679
JH
247 We ought to ignore this limit when we know that the edge is executed
248 on every invocation of the caller (i.e. its call statement dominates
249 exit block). We do not track this information, yet. */
09dfe187 250 stack_size_limit += ((gcov_type)stack_size_limit
1e83bd70
JH
251 * opt_for_fn (to->decl, param_stack_frame_growth)
252 / 100);
ff28a94d 253
f658ad30 254 inlined_stack = (ipa_get_stack_frame_offset (to)
4c0f7679 255 + outer_info->estimated_self_stack_size
e7f23018 256 + what_info->estimated_stack_size);
4c0f7679
JH
257 /* Check new stack consumption with stack consumption at the place
258 stack is used. */
259 if (inlined_stack > stack_size_limit
09a2806f 260 /* If function already has large stack usage from sibling
4c0f7679
JH
261 inline call, we can inline, too.
262 This bit overoptimistically assume that we are good at stack
263 packing. */
f658ad30 264 && inlined_stack > ipa_fn_summaries->get (to)->estimated_stack_size
1e83bd70 265 && inlined_stack > opt_for_fn (to->decl, param_large_stack_frame))
ff28a94d 266 {
4c0f7679 267 e->inline_failed = CIF_LARGE_STACK_FRAME_GROWTH_LIMIT;
ff28a94d
JH
268 return false;
269 }
ca31b95f
JH
270 return true;
271}
272
4c0f7679
JH
273/* Dump info about why inlining has failed. */
274
275static void
276report_inline_failed_reason (struct cgraph_edge *e)
277{
4174a33a 278 if (dump_enabled_p ())
4c0f7679 279 {
4174a33a
DM
280 dump_printf_loc (MSG_MISSED_OPTIMIZATION, e->call_stmt,
281 " not inlinable: %C -> %C, %s\n",
282 e->caller, e->callee,
283 cgraph_inline_failed_string (e->inline_failed));
bb1e543c
JH
284 if ((e->inline_failed == CIF_TARGET_OPTION_MISMATCH
285 || e->inline_failed == CIF_OPTIMIZATION_MISMATCH)
286 && e->caller->lto_file_data
1f6f9079 287 && e->callee->ultimate_alias_target ()->lto_file_data)
bb1e543c 288 {
4174a33a
DM
289 dump_printf_loc (MSG_MISSED_OPTIMIZATION, e->call_stmt,
290 " LTO objects: %s, %s\n",
291 e->caller->lto_file_data->file_name,
292 e->callee->ultimate_alias_target ()->lto_file_data->file_name);
bb1e543c
JH
293 }
294 if (e->inline_failed == CIF_TARGET_OPTION_MISMATCH)
9228f64c
DM
295 if (dump_file)
296 cl_target_option_print_diff
297 (dump_file, 2, target_opts_for_fn (e->caller->decl),
298 target_opts_for_fn (e->callee->ultimate_alias_target ()->decl));
bb1e543c 299 if (e->inline_failed == CIF_OPTIMIZATION_MISMATCH)
9228f64c
DM
300 if (dump_file)
301 cl_optimization_print_diff
302 (dump_file, 2, opts_for_fn (e->caller->decl),
303 opts_for_fn (e->callee->ultimate_alias_target ()->decl));
4c0f7679
JH
304 }
305}
306
25a07c7e
YG
307 /* Decide whether sanitizer-related attributes allow inlining. */
308
309static bool
310sanitize_attrs_match_for_inline_p (const_tree caller, const_tree callee)
311{
25a07c7e
YG
312 if (!caller || !callee)
313 return true;
314
4089df8e
ML
315 /* Follow clang and allow inlining for always_inline functions. */
316 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (callee)))
6206a883
JJ
317 return true;
318
4089df8e
ML
319 const sanitize_code codes[] =
320 {
321 SANITIZE_ADDRESS,
322 SANITIZE_THREAD,
323 SANITIZE_UNDEFINED,
324 SANITIZE_UNDEFINED_NONDEFAULT,
325 SANITIZE_POINTER_COMPARE,
326 SANITIZE_POINTER_SUBTRACT
327 };
328
ca32b29e 329 for (unsigned i = 0; i < ARRAY_SIZE (codes); i++)
4089df8e
ML
330 if (sanitize_flags_p (codes[i], caller)
331 != sanitize_flags_p (codes[i], callee))
332 return false;
333
cec4d4a6
ML
334 if (sanitize_coverage_p (caller) != sanitize_coverage_p (callee))
335 return false;
336
4089df8e 337 return true;
25a07c7e
YG
338}
339
8e926cb1
JH
340/* Used for flags where it is safe to inline when caller's value is
341 grater than callee's. */
342#define check_maybe_up(flag) \
343 (opts_for_fn (caller->decl)->x_##flag \
344 != opts_for_fn (callee->decl)->x_##flag \
345 && (!always_inline \
346 || opts_for_fn (caller->decl)->x_##flag \
347 < opts_for_fn (callee->decl)->x_##flag))
348/* Used for flags where it is safe to inline when caller's value is
349 smaller than callee's. */
350#define check_maybe_down(flag) \
351 (opts_for_fn (caller->decl)->x_##flag \
352 != opts_for_fn (callee->decl)->x_##flag \
353 && (!always_inline \
354 || opts_for_fn (caller->decl)->x_##flag \
355 > opts_for_fn (callee->decl)->x_##flag))
356/* Used for flags where exact match is needed for correctness. */
357#define check_match(flag) \
358 (opts_for_fn (caller->decl)->x_##flag \
359 != opts_for_fn (callee->decl)->x_##flag)
360
9a4841a3 361/* Decide if we can inline the edge and possibly update
4c0f7679
JH
362 inline_failed reason.
363 We check whether inlining is possible at all and whether
364 caller growth limits allow doing so.
365
9a4841a3 366 if REPORT is true, output reason to the dump file. */
ca31b95f 367
61a05df1 368static bool
09ce3660 369can_inline_edge_p (struct cgraph_edge *e, bool report,
9a4841a3 370 bool early = false)
ca31b95f 371{
7ce7e4d4
JH
372 gcc_checking_assert (e->inline_failed);
373
374 if (cgraph_inline_failed_type (e->inline_failed) == CIF_FINAL_ERROR)
375 {
376 if (report)
377 report_inline_failed_reason (e);
378 return false;
379 }
380
4c0f7679 381 bool inlinable = true;
a5b1779f 382 enum availability avail;
a62bfab5
ML
383 cgraph_node *caller = (e->caller->inlined_to
384 ? e->caller->inlined_to : e->caller);
e6007a27 385 cgraph_node *callee = e->callee->ultimate_alias_target (&avail, caller);
ea99e0be 386
7ce7e4d4 387 if (!callee->definition)
4c0f7679
JH
388 {
389 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
390 inlinable = false;
391 }
8a4a6d2e 392 if (!early && (!opt_for_fn (callee->decl, optimize)
12b8cb2e 393 || !opt_for_fn (caller->decl, optimize)))
29f1e2b1
JH
394 {
395 e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
396 inlinable = false;
397 }
1f26ac87
JM
398 else if (callee->calls_comdat_local)
399 {
400 e->inline_failed = CIF_USES_COMDAT_LOCAL;
401 inlinable = false;
402 }
d52f5295 403 else if (avail <= AVAIL_INTERPOSABLE)
9de21a23 404 {
4c0f7679 405 e->inline_failed = CIF_OVERWRITABLE;
6957a6f6 406 inlinable = false;
9de21a23 407 }
1a0bf5e1
JH
408 /* All edges with call_stmt_cannot_inline_p should have inline_failed
409 initialized to one of FINAL_ERROR reasons. */
89faf322 410 else if (e->call_stmt_cannot_inline_p)
1a0bf5e1 411 gcc_unreachable ();
4c0f7679 412 /* Don't inline if the functions have different EH personalities. */
bb1e543c 413 else if (DECL_FUNCTION_PERSONALITY (caller->decl)
67348ccc 414 && DECL_FUNCTION_PERSONALITY (callee->decl)
bb1e543c 415 && (DECL_FUNCTION_PERSONALITY (caller->decl)
67348ccc 416 != DECL_FUNCTION_PERSONALITY (callee->decl)))
4c0f7679
JH
417 {
418 e->inline_failed = CIF_EH_PERSONALITY;
419 inlinable = false;
420 }
a7ff6e27
AH
421 /* TM pure functions should not be inlined into non-TM_pure
422 functions. */
7ce7e4d4 423 else if (is_tm_pure (callee->decl) && !is_tm_pure (caller->decl))
0a35513e
AH
424 {
425 e->inline_failed = CIF_UNSPECIFIED;
426 inlinable = false;
427 }
09a2806f 428 /* Check compatibility of target optimization options. */
bb1e543c 429 else if (!targetm.target_option.can_inline_p (caller->decl,
67348ccc 430 callee->decl))
4c0f7679
JH
431 {
432 e->inline_failed = CIF_TARGET_OPTION_MISMATCH;
433 inlinable = false;
434 }
56f62793
ML
435 else if (ipa_fn_summaries->get (callee) == NULL
436 || !ipa_fn_summaries->get (callee)->inlinable)
5058c037
JH
437 {
438 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
439 inlinable = false;
440 }
25a07c7e 441 /* Don't inline a function with mismatched sanitization attributes. */
bb1e543c 442 else if (!sanitize_attrs_match_for_inline_p (caller->decl, callee->decl))
25a07c7e 443 {
4089df8e 444 e->inline_failed = CIF_SANITIZE_ATTRIBUTE_MISMATCH;
25a07c7e
YG
445 inlinable = false;
446 }
aad72d2e 447
f0a90c7d
AO
448 if (inlinable && !strub_inlinable_to_p (callee, caller))
449 {
450 e->inline_failed = CIF_UNSPECIFIED;
451 inlinable = false;
452 }
9a4841a3
JH
453 if (!inlinable && report)
454 report_inline_failed_reason (e);
455 return inlinable;
456}
457
3fd58767 458/* Return inlining_insns_single limit for function N. If HINT or HINT2 is true
2925cad2 459 scale up the bound. */
562d1e95
JH
460
461static int
3fd58767 462inline_insns_single (cgraph_node *n, bool hint, bool hint2)
562d1e95 463{
3fd58767
JH
464 if (hint && hint2)
465 {
466 int64_t spd = opt_for_fn (n->decl, param_inline_heuristics_hint_percent);
467 spd = spd * spd;
468 if (spd > 1000000)
469 spd = 1000000;
470 return opt_for_fn (n->decl, param_max_inline_insns_single) * spd / 100;
471 }
472 if (hint || hint2)
1e83bd70
JH
473 return opt_for_fn (n->decl, param_max_inline_insns_single)
474 * opt_for_fn (n->decl, param_inline_heuristics_hint_percent) / 100;
475 return opt_for_fn (n->decl, param_max_inline_insns_single);
562d1e95
JH
476}
477
3fd58767 478/* Return inlining_insns_auto limit for function N. If HINT or HINT2 is true
2925cad2 479 scale up the bound. */
562d1e95
JH
480
481static int
3fd58767 482inline_insns_auto (cgraph_node *n, bool hint, bool hint2)
562d1e95 483{
78a502ca 484 int max_inline_insns_auto = opt_for_fn (n->decl, param_max_inline_insns_auto);
3fd58767
JH
485 if (hint && hint2)
486 {
487 int64_t spd = opt_for_fn (n->decl, param_inline_heuristics_hint_percent);
488 spd = spd * spd;
489 if (spd > 1000000)
490 spd = 1000000;
491 return max_inline_insns_auto * spd / 100;
492 }
493 if (hint || hint2)
1e83bd70
JH
494 return max_inline_insns_auto
495 * opt_for_fn (n->decl, param_inline_heuristics_hint_percent) / 100;
78a502ca 496 return max_inline_insns_auto;
562d1e95
JH
497}
498
9a4841a3
JH
499/* Decide if we can inline the edge and possibly update
500 inline_failed reason.
501 We check whether inlining is possible at all and whether
502 caller growth limits allow doing so.
503
504 if REPORT is true, output reason to the dump file.
505
506 if DISREGARD_LIMITS is true, ignore size limits. */
507
508static bool
509can_inline_edge_by_limits_p (struct cgraph_edge *e, bool report,
510 bool disregard_limits = false, bool early = false)
511{
512 gcc_checking_assert (e->inline_failed);
513
514 if (cgraph_inline_failed_type (e->inline_failed) == CIF_FINAL_ERROR)
515 {
516 if (report)
517 report_inline_failed_reason (e);
518 return false;
519 }
520
521 bool inlinable = true;
522 enum availability avail;
a62bfab5
ML
523 cgraph_node *caller = (e->caller->inlined_to
524 ? e->caller->inlined_to : e->caller);
9a4841a3
JH
525 cgraph_node *callee = e->callee->ultimate_alias_target (&avail, caller);
526 tree caller_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (caller->decl);
527 tree callee_tree
528 = callee ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (callee->decl) : NULL;
4c0f7679 529 /* Check if caller growth allows the inlining. */
9a4841a3
JH
530 if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl)
531 && !disregard_limits
532 && !lookup_attribute ("flatten",
533 DECL_ATTRIBUTES (caller->decl))
534 && !caller_growth_limits (e))
4c0f7679 535 inlinable = false;
bc53dee0
QZ
536 else if (callee->externally_visible
537 && !DECL_DISREGARD_INLINE_LIMITS (callee->decl)
538 && flag_live_patching == LIVE_PATCHING_INLINE_ONLY_STATIC)
539 {
540 e->inline_failed = CIF_EXTERN_LIVE_ONLY_STATIC;
541 inlinable = false;
542 }
4c0f7679
JH
543 /* Don't inline a function with a higher optimization level than the
544 caller. FIXME: this is really just tip of iceberg of handling
545 optimization attribute. */
546 else if (caller_tree != callee_tree)
9de21a23 547 {
8e926cb1
JH
548 bool always_inline =
549 (DECL_DISREGARD_INLINE_LIMITS (callee->decl)
550 && lookup_attribute ("always_inline",
551 DECL_ATTRIBUTES (callee->decl)));
56f62793
ML
552 ipa_fn_summary *caller_info = ipa_fn_summaries->get (caller);
553 ipa_fn_summary *callee_info = ipa_fn_summaries->get (callee);
8e926cb1 554
553bb257
KT
555 /* Until GCC 4.9 we did not check the semantics-altering flags
556 below and inlined across optimization boundaries.
557 Enabling checks below breaks several packages by refusing
ddb3773a
JH
558 to inline library always_inline functions. See PR65873.
559 Disable the check for early inlining for now until better solution
560 is found. */
561 if (always_inline && early)
562 ;
0c3068e0
RB
563 /* There are some options that change IL semantics which means
564 we cannot inline in these cases for correctness reason.
565 Not even for always_inline declared functions. */
2bf54d93 566 else if (check_match (flag_wrapv)
ddb3773a 567 || check_match (flag_trapv)
29f1e2b1 568 || check_match (flag_pcc_struct_return)
2523d721 569 || check_maybe_down (optimize_debug)
818b88a7
JH
570 /* When caller or callee does FP math, be sure FP codegen flags
571 compatible. */
572 || ((caller_info->fp_expressions && callee_info->fp_expressions)
573 && (check_maybe_up (flag_rounding_math)
574 || check_maybe_up (flag_trapping_math)
575 || check_maybe_down (flag_unsafe_math_optimizations)
576 || check_maybe_down (flag_finite_math_only)
577 || check_maybe_up (flag_signaling_nans)
578 || check_maybe_down (flag_cx_limited_range)
579 || check_maybe_up (flag_signed_zeros)
580 || check_maybe_down (flag_associative_math)
581 || check_maybe_down (flag_reciprocal_math)
0d2f700f 582 || check_maybe_down (flag_fp_int_builtin_inexact)
818b88a7
JH
583 /* Strictly speaking only when the callee contains function
584 calls that may end up setting errno. */
585 || check_maybe_up (flag_errno_math)))
ddb3773a
JH
586 /* We do not want to make code compiled with exceptions to be
587 brought into a non-EH function unless we know that the callee
588 does not throw.
589 This is tracked by DECL_FUNCTION_PERSONALITY. */
6a96e917
EB
590 || (check_maybe_up (flag_non_call_exceptions)
591 && DECL_FUNCTION_PERSONALITY (callee->decl))
ddb3773a
JH
592 || (check_maybe_up (flag_exceptions)
593 && DECL_FUNCTION_PERSONALITY (callee->decl))
956d615d 594 /* When devirtualization is disabled for callee, it is not safe
ddb3773a
JH
595 to inline it as we possibly mangled the type info.
596 Allow early inlining of always inlines. */
597 || (!early && check_maybe_down (flag_devirtualize)))
0c3068e0
RB
598 {
599 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
600 inlinable = false;
601 }
602 /* gcc.dg/pr43564.c. Apply user-forced inline even at -O0. */
8e926cb1 603 else if (always_inline)
bb1e543c 604 ;
0c3068e0
RB
605 /* When user added an attribute to the callee honor it. */
606 else if (lookup_attribute ("optimize", DECL_ATTRIBUTES (callee->decl))
607 && opts_for_fn (caller->decl) != opts_for_fn (callee->decl))
4c0f7679 608 {
fd811f03 609 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
4c0f7679
JH
610 inlinable = false;
611 }
ddb3773a
JH
612 /* If explicit optimize attribute are not used, the mismatch is caused
613 by different command line options used to build different units.
614 Do not care about COMDAT functions - those are intended to be
615 optimized with the optimization flags of module they are used in.
616 Also do not care about mixing up size/speed optimization when
617 DECL_DISREGARD_INLINE_LIMITS is set. */
88636b62 618 else if ((callee->merged_comdat
ddb3773a
JH
619 && !lookup_attribute ("optimize",
620 DECL_ATTRIBUTES (caller->decl)))
621 || DECL_DISREGARD_INLINE_LIMITS (callee->decl))
622 ;
bb1e543c 623 /* If mismatch is caused by merging two LTO units with different
956d615d 624 optimization flags we want to be bit nicer. However never inline
bb1e543c
JH
625 if one of functions is not optimized at all. */
626 else if (!opt_for_fn (callee->decl, optimize)
627 || !opt_for_fn (caller->decl, optimize))
628 {
629 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
630 inlinable = false;
631 }
632 /* If callee is optimized for size and caller is not, allow inlining if
028d4092
ML
633 code shrinks or we are in param_max_inline_insns_single limit and
634 callee is inline (and thus likely an unified comdat).
635 This will allow caller to run faster. */
bb1e543c
JH
636 else if (opt_for_fn (callee->decl, optimize_size)
637 > opt_for_fn (caller->decl, optimize_size))
638 {
639 int growth = estimate_edge_growth (e);
1e83bd70 640 if (growth > opt_for_fn (caller->decl, param_max_inline_insns_size)
bb1e543c 641 && (!DECL_DECLARED_INLINE_P (callee->decl)
3fd58767
JH
642 && growth >= MAX (inline_insns_single (caller, false, false),
643 inline_insns_auto (caller, false, false))))
bb1e543c
JH
644 {
645 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
646 inlinable = false;
647 }
648 }
649 /* If callee is more aggressively optimized for performance than caller,
650 we generally want to inline only cheap (runtime wise) functions. */
651 else if (opt_for_fn (callee->decl, optimize_size)
652 < opt_for_fn (caller->decl, optimize_size)
653 || (opt_for_fn (callee->decl, optimize)
86f46e39 654 > opt_for_fn (caller->decl, optimize)))
bb1e543c
JH
655 {
656 if (estimate_edge_time (e)
56f62793 657 >= 20 + ipa_call_summaries->get (e)->call_stmt_time)
bb1e543c
JH
658 {
659 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
660 inlinable = false;
661 }
662 }
663
4c0f7679
JH
664 }
665
4c0f7679
JH
666 if (!inlinable && report)
667 report_inline_failed_reason (e);
668 return inlinable;
669}
670
671
672/* Return true if the edge E is inlinable during early inlining. */
673
674static bool
675can_early_inline_edge_p (struct cgraph_edge *e)
676{
9eb8785b
ML
677 cgraph_node *caller = (e->caller->inlined_to
678 ? e->caller->inlined_to : e->caller);
d52f5295 679 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
4c0f7679 680 /* Early inliner might get called at WPA stage when IPA pass adds new
67914693 681 function. In this case we cannot really do any of early inlining
4c0f7679 682 because function bodies are missing. */
1a0bf5e1
JH
683 if (cgraph_inline_failed_type (e->inline_failed) == CIF_FINAL_ERROR)
684 return false;
67348ccc 685 if (!gimple_has_body_p (callee->decl))
4c0f7679
JH
686 {
687 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
9de21a23
JC
688 return false;
689 }
d88fd2e1
JH
690 gcc_assert (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->caller->decl))
691 && gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)));
08a52331 692 if ((profile_arc_flag || condition_coverage_flag)
d88fd2e1
JH
693 && ((lookup_attribute ("no_profile_instrument_function",
694 DECL_ATTRIBUTES (caller->decl)) == NULL_TREE)
695 != (lookup_attribute ("no_profile_instrument_function",
696 DECL_ATTRIBUTES (callee->decl)) == NULL_TREE)))
9eb8785b
ML
697 return false;
698
9a4841a3
JH
699 if (!can_inline_edge_p (e, true, true)
700 || !can_inline_edge_by_limits_p (e, true, false, true))
4c0f7679 701 return false;
d88fd2e1
JH
702 /* When inlining regular functions into always-inline functions
703 during early inlining watch for possible inline cycles. */
704 if (DECL_DISREGARD_INLINE_LIMITS (caller->decl)
705 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (caller->decl))
706 && (!DECL_DISREGARD_INLINE_LIMITS (callee->decl)
707 || !lookup_attribute ("always_inline", DECL_ATTRIBUTES (callee->decl))))
708 {
709 /* If there are indirect calls, inlining may produce direct call.
710 TODO: We may lift this restriction if we avoid errors on formely
711 indirect calls to always_inline functions. Taking address
712 of always_inline function is generally bad idea and should
713 have been declared as undefined, but sadly we allow this. */
714 if (caller->indirect_calls || e->callee->indirect_calls)
715 return false;
716 ipa_fn_summary *callee_info = ipa_fn_summaries->get (callee);
717 if (callee_info->safe_to_inline_to_always_inline)
718 return callee_info->safe_to_inline_to_always_inline - 1;
719 for (cgraph_edge *e2 = callee->callees; e2; e2 = e2->next_callee)
720 {
721 struct cgraph_node *callee2 = e2->callee->ultimate_alias_target ();
722 /* As early inliner runs in RPO order, we will see uninlined
723 always_inline calls only in the case of cyclic graphs. */
724 if (DECL_DISREGARD_INLINE_LIMITS (callee2->decl)
725 || lookup_attribute ("always_inline", DECL_ATTRIBUTES (callee2->decl)))
726 {
727 callee_info->safe_to_inline_to_always_inline = 1;
728 return false;
729 }
730 /* With LTO watch for case where function is later replaced
731 by always_inline definition.
732 TODO: We may either stop treating noninlined cross-module always
733 inlines as errors, or we can extend decl merging to produce
734 syntacic alias and honor always inline only in units it has
735 been declared as such. */
736 if (flag_lto && callee2->externally_visible)
737 {
738 callee_info->safe_to_inline_to_always_inline = 1;
739 return false;
740 }
741 }
742 callee_info->safe_to_inline_to_always_inline = 2;
743 }
4c0f7679
JH
744 return true;
745}
746
747
ae6e6a08 748/* Return number of calls in N. Ignore cheap builtins. */
4c0f7679 749
ae6e6a08
JH
750static int
751num_calls (struct cgraph_node *n)
4c0f7679
JH
752{
753 struct cgraph_edge *e;
ae6e6a08
JH
754 int num = 0;
755
4c0f7679 756 for (e = n->callees; e; e = e->next_callee)
67348ccc 757 if (!is_inexpensive_builtin (e->callee->decl))
ae6e6a08
JH
758 num++;
759 return num;
4c0f7679
JH
760}
761
f27e50db 762
4c0f7679 763/* Return true if we are interested in inlining small function. */
9de21a23 764
4c0f7679
JH
765static bool
766want_early_inline_function_p (struct cgraph_edge *e)
767{
768 bool want_inline = true;
d52f5295 769 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
4c0f7679 770
67348ccc 771 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
4c0f7679 772 ;
9a1e784a 773 /* For AutoFDO, we need to make sure that before profile summary, all
be3c16c4
DC
774 hot paths' IR look exactly the same as profiled binary. As a result,
775 in einliner, we will disregard size limit and inline those callsites
776 that are:
777 * inlined in the profiled binary, and
778 * the cloned callee has enough samples to be considered "hot". */
779 else if (flag_auto_profile && afdo_callsite_hot_enough_for_early_inline (e))
780 ;
67348ccc 781 else if (!DECL_DECLARED_INLINE_P (callee->decl)
2bf86c84 782 && !opt_for_fn (e->caller->decl, flag_inline_small_functions))
4c0f7679
JH
783 {
784 e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
785 report_inline_failed_reason (e);
786 want_inline = false;
787 }
788 else
9de21a23 789 {
49e26500
JH
790 /* First take care of very large functions. */
791 int min_growth = estimate_min_edge_growth (e), growth = 0;
ae6e6a08 792 int n;
1e83bd70 793 int early_inlining_insns = param_early_inlining_insns;
0b92cf30 794
49e26500
JH
795 if (min_growth > early_inlining_insns)
796 {
797 if (dump_enabled_p ())
798 dump_printf_loc (MSG_MISSED_OPTIMIZATION, e->call_stmt,
799 " will not early inline: %C->%C, "
800 "call is cold and code would grow "
801 "at least by %i\n",
802 e->caller, callee,
803 min_growth);
804 want_inline = false;
805 }
806 else
807 growth = estimate_edge_growth (e);
808
ae6e6a08 809
49e26500 810 if (!want_inline || growth <= param_max_inline_insns_size)
4c0f7679 811 ;
f256c274 812 else if (!e->maybe_hot_p ())
4c0f7679 813 {
4174a33a
DM
814 if (dump_enabled_p ())
815 dump_printf_loc (MSG_MISSED_OPTIMIZATION, e->call_stmt,
816 " will not early inline: %C->%C, "
817 "call is cold and code would grow by %i\n",
818 e->caller, callee,
819 growth);
4c0f7679
JH
820 want_inline = false;
821 }
0b92cf30 822 else if (growth > early_inlining_insns)
9de21a23 823 {
4174a33a
DM
824 if (dump_enabled_p ())
825 dump_printf_loc (MSG_MISSED_OPTIMIZATION, e->call_stmt,
826 " will not early inline: %C->%C, "
9c28689a
JH
827 "growth %i exceeds --param early-inlining-insns\n",
828 e->caller, callee, growth);
4c0f7679 829 want_inline = false;
9de21a23 830 }
ae6e6a08 831 else if ((n = num_calls (callee)) != 0
0b92cf30 832 && growth * (n + 1) > early_inlining_insns)
4c0f7679 833 {
4174a33a
DM
834 if (dump_enabled_p ())
835 dump_printf_loc (MSG_MISSED_OPTIMIZATION, e->call_stmt,
836 " will not early inline: %C->%C, "
9c28689a 837 "growth %i exceeds --param early-inlining-insns "
4174a33a 838 "divided by number of calls\n",
9c28689a 839 e->caller, callee, growth);
4c0f7679
JH
840 want_inline = false;
841 }
842 }
843 return want_inline;
844}
845
d59171da
JH
846/* Compute time of the edge->caller + edge->callee execution when inlining
847 does not happen. */
848
6d4ab5f8 849inline sreal
4adaad64 850compute_uninlined_call_time (struct cgraph_edge *edge,
b5351388
JH
851 sreal uninlined_call_time,
852 sreal freq)
d59171da 853{
a62bfab5
ML
854 cgraph_node *caller = (edge->caller->inlined_to
855 ? edge->caller->inlined_to
208e5afa
JH
856 : edge->caller);
857
0009a6c3 858 if (freq > 0)
41f0e819 859 uninlined_call_time *= freq;
208e5afa
JH
860 else
861 uninlined_call_time = uninlined_call_time >> 11;
862
56f62793 863 sreal caller_time = ipa_fn_summaries->get (caller)->time;
d59171da
JH
864 return uninlined_call_time + caller_time;
865}
866
867/* Same as compute_uinlined_call_time but compute time when inlining
868 does happen. */
869
6d4ab5f8 870inline sreal
d59171da 871compute_inlined_call_time (struct cgraph_edge *edge,
b5351388
JH
872 sreal time,
873 sreal freq)
d59171da 874{
a62bfab5
ML
875 cgraph_node *caller = (edge->caller->inlined_to
876 ? edge->caller->inlined_to
208e5afa 877 : edge->caller);
56f62793 878 sreal caller_time = ipa_fn_summaries->get (caller)->time;
208e5afa 879
0009a6c3 880 if (freq > 0)
41f0e819 881 time *= freq;
208e5afa
JH
882 else
883 time = time >> 11;
884
e53b6e56 885 /* This calculation should match one in ipa-inline-analysis.cc
4adaad64 886 (estimate_edge_size_and_time). */
56f62793 887 time -= (sreal)ipa_call_summaries->get (edge)->call_stmt_time * freq;
208e5afa
JH
888 time += caller_time;
889 if (time <= 0)
890 time = ((sreal) 1) >> 8;
6d4ab5f8 891 gcc_checking_assert (time >= 0);
d59171da
JH
892 return time;
893}
894
956d615d
JJ
895/* Determine time saved by inlining EDGE of frequency FREQ
896 where callee's runtime w/o inlining is UNINLINED_TYPE
ea8dd3b6
JH
897 and with inlined is INLINED_TYPE. */
898
899inline sreal
900inlining_speedup (struct cgraph_edge *edge,
901 sreal freq,
902 sreal uninlined_time,
903 sreal inlined_time)
904{
905 sreal speedup = uninlined_time - inlined_time;
906 /* Handling of call_time should match one in ipa-inline-fnsummary.c
907 (estimate_edge_size_and_time). */
908 sreal call_time = ipa_call_summaries->get (edge)->call_stmt_time;
909
910 if (freq > 0)
911 {
912 speedup = (speedup + call_time);
913 if (freq != 1)
914 speedup = speedup * freq;
915 }
916 else if (freq == 0)
917 speedup = speedup >> 11;
918 gcc_checking_assert (speedup >= 0);
919 return speedup;
920}
921
42f7b0fa 922/* Return true if the speedup for inlining E is bigger than
3fd58767 923 param_inline_min_speedup. */
42f7b0fa
JH
924
925static bool
926big_speedup_p (struct cgraph_edge *e)
927{
4adaad64
JH
928 sreal unspec_time;
929 sreal spec_time = estimate_edge_time (e, &unspec_time);
b5351388
JH
930 sreal freq = e->sreal_frequency ();
931 sreal time = compute_uninlined_call_time (e, unspec_time, freq);
932 sreal inlined_time = compute_inlined_call_time (e, spec_time, freq);
a62bfab5
ML
933 cgraph_node *caller = (e->caller->inlined_to
934 ? e->caller->inlined_to
935 : e->caller);
1e83bd70 936 int limit = opt_for_fn (caller->decl, param_inline_min_speedup);
208e5afa 937
562d1e95 938 if ((time - inlined_time) * 100 > time * limit)
42f7b0fa
JH
939 return true;
940 return false;
941}
942
4c0f7679
JH
943/* Return true if we are interested in inlining small function.
944 When REPORT is true, report reason to dump file. */
945
946static bool
947want_inline_small_function_p (struct cgraph_edge *e, bool report)
948{
949 bool want_inline = true;
d52f5295 950 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
1e83bd70
JH
951 cgraph_node *to = (e->caller->inlined_to
952 ? e->caller->inlined_to : e->caller);
4c0f7679 953
01b9bf06
RS
954 /* Allow this function to be called before can_inline_edge_p,
955 since it's usually cheaper. */
956 if (cgraph_inline_failed_type (e->inline_failed) == CIF_FINAL_ERROR)
957 want_inline = false;
958 else if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
4c0f7679 959 ;
67348ccc 960 else if (!DECL_DECLARED_INLINE_P (callee->decl)
2bf86c84 961 && !opt_for_fn (e->caller->decl, flag_inline_small_functions))
4c0f7679
JH
962 {
963 e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
964 want_inline = false;
9de21a23 965 }
4cd8957f 966 /* Do fast and conservative check if the function can be good
2925cad2 967 inline candidate. */
b6d627e4 968 else if ((!DECL_DECLARED_INLINE_P (callee->decl)
1bad9c18 969 && (!e->count.ipa ().initialized_p () || !e->maybe_hot_p ()))
56f62793
ML
970 && ipa_fn_summaries->get (callee)->min_size
971 - ipa_call_summaries->get (e)->call_stmt_size
3fd58767 972 > inline_insns_auto (e->caller, true, true))
4cd8957f 973 {
afeb8875 974 e->inline_failed = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
4cd8957f
JH
975 want_inline = false;
976 }
3995f3a2 977 else if ((DECL_DECLARED_INLINE_P (callee->decl)
1bad9c18 978 || e->count.ipa ().nonzero_p ())
56f62793
ML
979 && ipa_fn_summaries->get (callee)->min_size
980 - ipa_call_summaries->get (e)->call_stmt_size
3fd58767 981 > inline_insns_single (e->caller, true, true))
4cd8957f 982 {
9c28689a
JH
983 e->inline_failed = (DECL_DECLARED_INLINE_P (callee->decl)
984 ? CIF_MAX_INLINE_INSNS_SINGLE_LIMIT
985 : CIF_MAX_INLINE_INSNS_AUTO_LIMIT);
4cd8957f
JH
986 want_inline = false;
987 }
ca31b95f 988 else
9de21a23 989 {
4c0f7679 990 int growth = estimate_edge_growth (e);
0bceb671 991 ipa_hints hints = estimate_edge_hints (e);
3fd58767
JH
992 /* We have two independent groups of hints. If one matches in each
993 of groups the limits are inreased. If both groups matches, limit
994 is increased even more. */
2925cad2
JH
995 bool apply_hints = (hints & (INLINE_HINT_indirect_call
996 | INLINE_HINT_known_hot
997 | INLINE_HINT_loop_iterations
3fd58767
JH
998 | INLINE_HINT_loop_stride));
999 bool apply_hints2 = (hints & INLINE_HINT_builtin_constant_p);
4c0f7679 1000
1e83bd70
JH
1001 if (growth <= opt_for_fn (to->decl,
1002 param_max_inline_insns_size))
4c0f7679 1003 ;
028d4092 1004 /* Apply param_max_inline_insns_single limit. Do not do so when
2925cad2
JH
1005 hints suggests that inlining given function is very profitable.
1006 Avoid computation of big_speedup_p when not necessary to change
1007 outcome of decision. */
67348ccc 1008 else if (DECL_DECLARED_INLINE_P (callee->decl)
3fd58767
JH
1009 && growth >= inline_insns_single (e->caller, apply_hints,
1010 apply_hints2)
1011 && (apply_hints || apply_hints2
1012 || growth >= inline_insns_single (e->caller, true,
1013 apply_hints2)
2925cad2 1014 || !big_speedup_p (e)))
4c0f7679 1015 {
1e83bd70 1016 e->inline_failed = CIF_MAX_INLINE_INSNS_SINGLE_LIMIT;
4c0f7679
JH
1017 want_inline = false;
1018 }
67348ccc 1019 else if (!DECL_DECLARED_INLINE_P (callee->decl)
f256c274 1020 && !opt_for_fn (e->caller->decl, flag_inline_functions)
1e83bd70
JH
1021 && growth >= opt_for_fn (to->decl,
1022 param_max_inline_insns_small))
4c0f7679 1023 {
49d9c9d2 1024 /* growth_positive_p is expensive, always test it last. */
3fd58767 1025 if (growth >= inline_insns_single (e->caller, false, false)
49d9c9d2 1026 || growth_positive_p (callee, e, growth))
4cd8957f 1027 {
562d1e95 1028 e->inline_failed = CIF_NOT_DECLARED_INLINED;
4cd8957f 1029 want_inline = false;
562d1e95 1030 }
4c0f7679 1031 }
028d4092
ML
1032 /* Apply param_max_inline_insns_auto limit for functions not declared
1033 inline. Bypass the limit when speedup seems big. */
67348ccc 1034 else if (!DECL_DECLARED_INLINE_P (callee->decl)
3fd58767
JH
1035 && growth >= inline_insns_auto (e->caller, apply_hints,
1036 apply_hints2)
1037 && (apply_hints || apply_hints2
1038 || growth >= inline_insns_auto (e->caller, true,
1039 apply_hints2)
2925cad2 1040 || !big_speedup_p (e)))
4c0f7679 1041 {
49d9c9d2 1042 /* growth_positive_p is expensive, always test it last. */
3fd58767 1043 if (growth >= inline_insns_single (e->caller, false, false)
49d9c9d2 1044 || growth_positive_p (callee, e, growth))
4cd8957f 1045 {
afeb8875 1046 e->inline_failed = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
4cd8957f 1047 want_inline = false;
562d1e95 1048 }
4c0f7679 1049 }
db22a743 1050 /* If call is cold, do not inline when function body would grow. */
3dafb85c 1051 else if (!e->maybe_hot_p ()
3fd58767 1052 && (growth >= inline_insns_single (e->caller, false, false)
49d9c9d2 1053 || growth_positive_p (callee, e, growth)))
9de21a23 1054 {
562d1e95 1055 e->inline_failed = CIF_UNLIKELY_CALL;
4c0f7679 1056 want_inline = false;
9de21a23
JC
1057 }
1058 }
4c0f7679
JH
1059 if (!want_inline && report)
1060 report_inline_failed_reason (e);
1061 return want_inline;
1062}
9de21a23 1063
4c0f7679 1064/* EDGE is self recursive edge.
1e83bd70 1065 We handle two cases - when function A is inlining into itself
4c0f7679
JH
1066 or when function A is being inlined into another inliner copy of function
1067 A within function B.
1068
1069 In first case OUTER_NODE points to the toplevel copy of A, while
1070 in the second case OUTER_NODE points to the outermost copy of A in B.
1071
1072 In both cases we want to be extra selective since
1073 inlining the call will just introduce new recursive calls to appear. */
09a2806f 1074
4c0f7679
JH
1075static bool
1076want_inline_self_recursive_call_p (struct cgraph_edge *edge,
1077 struct cgraph_node *outer_node,
1078 bool peeling,
1079 int depth)
1080{
1081 char const *reason = NULL;
1082 bool want_inline = true;
0009a6c3 1083 sreal caller_freq = 1;
1e83bd70
JH
1084 int max_depth = opt_for_fn (outer_node->decl,
1085 param_max_inline_recursive_depth_auto);
4c0f7679 1086
67348ccc 1087 if (DECL_DECLARED_INLINE_P (edge->caller->decl))
1e83bd70
JH
1088 max_depth = opt_for_fn (outer_node->decl,
1089 param_max_inline_recursive_depth);
4c0f7679 1090
3dafb85c 1091 if (!edge->maybe_hot_p ())
4c0f7679
JH
1092 {
1093 reason = "recursive call is cold";
1094 want_inline = false;
1095 }
4c0f7679
JH
1096 else if (depth > max_depth)
1097 {
1098 reason = "--param max-inline-recursive-depth exceeded.";
1099 want_inline = false;
1100 }
a62bfab5 1101 else if (outer_node->inlined_to
0009a6c3 1102 && (caller_freq = outer_node->callers->sreal_frequency ()) == 0)
bd936951 1103 {
0009a6c3 1104 reason = "caller frequency is 0";
bd936951
JH
1105 want_inline = false;
1106 }
1107
4c0f7679
JH
1108 if (!want_inline)
1109 ;
0009a6c3
JH
1110 /* Inlining of self recursive function into copy of itself within other
1111 function is transformation similar to loop peeling.
4c0f7679 1112
09a2806f 1113 Peeling is profitable if we can inline enough copies to make probability
4c0f7679
JH
1114 of actual call to the self recursive function very small. Be sure that
1115 the probability of recursion is small.
1116
09a2806f 1117 We ensure that the frequency of recursing is at most 1 - (1/max_depth).
0009a6c3 1118 This way the expected number of recursion is at most max_depth. */
4c0f7679
JH
1119 else if (peeling)
1120 {
0009a6c3 1121 sreal max_prob = (sreal)1 - ((sreal)1 / (sreal)max_depth);
4c0f7679
JH
1122 int i;
1123 for (i = 1; i < depth; i++)
0009a6c3
JH
1124 max_prob = max_prob * max_prob;
1125 if (edge->sreal_frequency () >= max_prob * caller_freq)
4c0f7679
JH
1126 {
1127 reason = "frequency of recursive call is too large";
1128 want_inline = false;
1129 }
1130 }
0009a6c3
JH
1131 /* Recursive inlining, i.e. equivalent of unrolling, is profitable if
1132 recursion depth is large. We reduce function call overhead and increase
1133 chances that things fit in hardware return predictor.
4c0f7679
JH
1134
1135 Recursive inlining might however increase cost of stack frame setup
1136 actually slowing down functions whose recursion tree is wide rather than
1137 deep.
1138
09a2806f 1139 Deciding reliably on when to do recursive inlining without profile feedback
4c0f7679
JH
1140 is tricky. For now we disable recursive inlining when probability of self
1141 recursion is low.
1142
0009a6c3
JH
1143 Recursive inlining of self recursive call within loop also results in
1144 large loop depths that generally optimize badly. We may want to throttle
1145 down inlining in those cases. In particular this seems to happen in one
1146 of libstdc++ rb tree methods. */
4c0f7679
JH
1147 else
1148 {
0009a6c3
JH
1149 if (edge->sreal_frequency () * 100
1150 <= caller_freq
1e83bd70
JH
1151 * opt_for_fn (outer_node->decl,
1152 param_min_inline_recursive_probability))
4c0f7679
JH
1153 {
1154 reason = "frequency of recursive call is too small";
1155 want_inline = false;
1156 }
1157 }
4174a33a
DM
1158 if (!want_inline && dump_enabled_p ())
1159 dump_printf_loc (MSG_MISSED_OPTIMIZATION, edge->call_stmt,
1160 " not inlining recursively: %s\n", reason);
4c0f7679 1161 return want_inline;
ca31b95f
JH
1162}
1163
19ba6aab
JH
1164/* Return true when NODE has uninlinable caller;
1165 set HAS_HOT_CALL if it has hot call.
9aa3f5c5
JH
1166 Worker for cgraph_for_node_and_aliases. */
1167
1168static bool
19ba6aab 1169check_callers (struct cgraph_node *node, void *has_hot_call)
9aa3f5c5 1170{
19ba6aab 1171 struct cgraph_edge *e;
f157c536
JH
1172 for (e = node->callers; e; e = e->next_caller)
1173 {
1174 if (!opt_for_fn (e->caller->decl, flag_inline_functions_called_once)
1175 || !opt_for_fn (e->caller->decl, optimize))
1176 return true;
1177 if (!can_inline_edge_p (e, true))
1178 return true;
1179 if (e->recursive_p ())
1180 return true;
1181 if (!can_inline_edge_by_limits_p (e, true))
1182 return true;
1183 /* Inlining large functions to large loop depth is often harmful because
1184 of register pressure it implies. */
1185 if ((int)ipa_call_summaries->get (e)->loop_depth
1186 > param_inline_functions_called_once_loop_depth)
1187 return true;
1188 /* Do not produce gigantic functions. */
1189 if (estimate_size_after_inlining (e->caller->inlined_to ?
1190 e->caller->inlined_to : e->caller, e)
1191 > param_inline_functions_called_once_insns)
1192 return true;
1193 if (!(*(bool *)has_hot_call) && e->maybe_hot_p ())
1194 *(bool *)has_hot_call = true;
1195 }
19ba6aab 1196 return false;
9aa3f5c5
JH
1197}
1198
a81b0a3d
JH
1199/* If NODE has a caller, return true. */
1200
1201static bool
1202has_caller_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1203{
1204 if (node->callers)
1205 return true;
1206 return false;
1207}
09a2806f 1208
100411f8
JH
1209/* Decide if inlining NODE would reduce unit size by eliminating
1210 the offline copy of function.
1211 When COLD is true the cold calls are considered, too. */
09a2806f
JH
1212
1213static bool
100411f8 1214want_inline_function_to_all_callers_p (struct cgraph_node *node, bool cold)
09a2806f 1215{
5970b079
EB
1216 bool has_hot_call = false;
1217
9789b553
JH
1218 /* Aliases gets inlined along with the function they alias. */
1219 if (node->alias)
5970b079
EB
1220 return false;
1221 /* Already inlined? */
a62bfab5 1222 if (node->inlined_to)
5970b079
EB
1223 return false;
1224 /* Does it have callers? */
1ede94c5 1225 if (!node->call_for_symbol_and_aliases (has_caller_p, NULL, true))
5970b079
EB
1226 return false;
1227 /* Inlining into all callers would increase size? */
49d9c9d2 1228 if (growth_positive_p (node, NULL, INT_MIN) > 0)
5970b079
EB
1229 return false;
1230 /* All inlines must be possible. */
1ede94c5
JH
1231 if (node->call_for_symbol_and_aliases (check_callers, &has_hot_call,
1232 true))
5970b079
EB
1233 return false;
1234 if (!cold && !has_hot_call)
1235 return false;
1236 return true;
09a2806f
JH
1237}
1238
041cb615
JH
1239/* Return true if WHERE of SIZE is a possible candidate for wrapper heuristics
1240 in estimate_edge_badness. */
1241
1242static bool
1243wrapper_heuristics_may_apply (struct cgraph_node *where, int size)
1244{
1245 return size < (DECL_DECLARED_INLINE_P (where->decl)
3fd58767
JH
1246 ? inline_insns_single (where, false, false)
1247 : inline_insns_auto (where, false, false));
041cb615
JH
1248}
1249
670cd5c5
JH
1250/* A cost model driving the inlining heuristics in a way so the edges with
1251 smallest badness are inlined first. After each inlining is performed
0fa2e4df 1252 the costs of all caller edges of nodes affected are recomputed so the
670cd5c5 1253 metrics may accurately depend on values such as number of inlinable callers
45a80bb9 1254 of the function or function body size. */
670cd5c5 1255
f0e1509b 1256static sreal
4c0f7679 1257edge_badness (struct cgraph_edge *edge, bool dump)
670cd5c5 1258{
f0e1509b 1259 sreal badness;
ab38481c 1260 int growth;
4adaad64 1261 sreal edge_time, unspec_edge_time;
d52f5295 1262 struct cgraph_node *callee = edge->callee->ultimate_alias_target ();
99b1c316 1263 class ipa_fn_summary *callee_info = ipa_fn_summaries->get (callee);
0bceb671 1264 ipa_hints hints;
a62bfab5
ML
1265 cgraph_node *caller = (edge->caller->inlined_to
1266 ? edge->caller->inlined_to
208e5afa 1267 : edge->caller);
1aa14195 1268
03dfc36d 1269 growth = estimate_edge_growth (edge);
4adaad64 1270 edge_time = estimate_edge_time (edge, &unspec_edge_time);
37678631 1271 hints = estimate_edge_hints (edge);
d59171da 1272 gcc_checking_assert (edge_time >= 0);
1bad9c18
JH
1273 /* Check that inlined time is better, but tolerate some roundoff issues.
1274 FIXME: When callee profile drops to 0 we account calls more. This
1275 should be fixed by never doing that. */
3f05a4f0
JH
1276 gcc_checking_assert ((edge_time * 100
1277 - callee_info->time * 101).to_int () <= 0
1bad9c18 1278 || callee->count.ipa ().initialized_p ());
f658ad30 1279 gcc_checking_assert (growth <= ipa_size_summaries->get (callee)->size);
e89964e3 1280
1ce18dc8
JH
1281 if (dump)
1282 {
464d0118
ML
1283 fprintf (dump_file, " Badness calculation for %s -> %s\n",
1284 edge->caller->dump_name (),
1285 edge->callee->dump_name ());
4adaad64 1286 fprintf (dump_file, " size growth %i, time %f unspec %f ",
1ce18dc8 1287 growth,
4adaad64
JH
1288 edge_time.to_double (),
1289 unspec_edge_time.to_double ());
0bceb671 1290 ipa_dump_hints (dump_file, hints);
42f7b0fa
JH
1291 if (big_speedup_p (edge))
1292 fprintf (dump_file, " big_speedup");
37678631 1293 fprintf (dump_file, "\n");
1ce18dc8 1294 }
45a80bb9
JH
1295
1296 /* Always prefer inlining saving code size. */
1297 if (growth <= 0)
1ce18dc8 1298 {
6d4ab5f8 1299 badness = (sreal) (-SREAL_MIN_SIG + growth) << (SREAL_MAX_EXP / 256);
1ce18dc8 1300 if (dump)
6d4ab5f8 1301 fprintf (dump_file, " %f: Growth %d <= 0\n", badness.to_double (),
1ce18dc8
JH
1302 growth);
1303 }
208e5afa
JH
1304 /* Inlining into EXTERNAL functions is not going to change anything unless
1305 they are themselves inlined. */
1306 else if (DECL_EXTERNAL (caller->decl))
1ce18dc8 1307 {
1ce18dc8 1308 if (dump)
208e5afa
JH
1309 fprintf (dump_file, " max: function is external\n");
1310 return sreal::max ();
1ce18dc8 1311 }
208e5afa 1312 /* When profile is available. Compute badness as:
b4c0a884 1313
208e5afa 1314 time_saved * caller_count
133a84ab
JH
1315 goodness = -------------------------------------------------
1316 growth_of_caller * overall_growth * combined_size
d59171da
JH
1317
1318 badness = - goodness
b4c0a884 1319
208e5afa
JH
1320 Again use negative value to make calls with profile appear hotter
1321 then calls without.
b4c0a884 1322 */
3995f3a2 1323 else if (opt_for_fn (caller->decl, flag_guess_branch_prob)
1bad9c18 1324 || caller->count.ipa ().nonzero_p ())
670cd5c5 1325 {
6d4ab5f8 1326 sreal numerator, denominator;
41f669d8 1327 int overall_growth;
b5351388 1328 sreal freq = edge->sreal_frequency ();
208e5afa 1329
ea8dd3b6 1330 numerator = inlining_speedup (edge, freq, unspec_edge_time, edge_time);
0009a6c3 1331 if (numerator <= 0)
208e5afa 1332 numerator = ((sreal) 1 >> 8);
1bad9c18
JH
1333 if (caller->count.ipa ().nonzero_p ())
1334 numerator *= caller->count.ipa ().to_gcov_type ();
1335 else if (caller->count.ipa ().initialized_p ())
208e5afa
JH
1336 numerator = numerator >> 11;
1337 denominator = growth;
41f669d8
JH
1338
1339 overall_growth = callee_info->growth;
1340
1341 /* Look for inliner wrappers of the form:
1342
1343 inline_caller ()
1344 {
1345 do_fast_job...
1346 if (need_more_work)
1347 noninline_callee ();
1348 }
956d615d 1349 Without penalizing this case, we usually inline noninline_callee
41f669d8
JH
1350 into the inline_caller because overall_growth is small preventing
1351 further inlining of inline_caller.
1352
1353 Penalize only callgraph edges to functions with small overall
1354 growth ...
1355 */
1356 if (growth > overall_growth
1357 /* ... and having only one caller which is not inlined ... */
1358 && callee_info->single_caller
a62bfab5 1359 && !edge->caller->inlined_to
41f669d8 1360 /* ... and edges executed only conditionally ... */
b5351388 1361 && freq < 1
41f669d8
JH
1362 /* ... consider case where callee is not inline but caller is ... */
1363 && ((!DECL_DECLARED_INLINE_P (edge->callee->decl)
1364 && DECL_DECLARED_INLINE_P (caller->decl))
1365 /* ... or when early optimizers decided to split and edge
1366 frequency still indicates splitting is a win ... */
1367 || (callee->split_part && !caller->split_part
1e83bd70
JH
1368 && freq * 100
1369 < opt_for_fn (caller->decl,
1370 param_partial_inlining_entry_probability)
41f669d8
JH
1371 /* ... and do not overwrite user specified hints. */
1372 && (!DECL_DECLARED_INLINE_P (edge->callee->decl)
1373 || DECL_DECLARED_INLINE_P (caller->decl)))))
1374 {
56f62793 1375 ipa_fn_summary *caller_info = ipa_fn_summaries->get (caller);
41f669d8
JH
1376 int caller_growth = caller_info->growth;
1377
1378 /* Only apply the penalty when caller looks like inline candidate,
2925cad2 1379 and it is not called once. */
41f669d8
JH
1380 if (!caller_info->single_caller && overall_growth < caller_growth
1381 && caller_info->inlinable
041cb615
JH
1382 && wrapper_heuristics_may_apply
1383 (caller, ipa_size_summaries->get (caller)->size))
41f669d8
JH
1384 {
1385 if (dump)
1386 fprintf (dump_file,
1387 " Wrapper penalty. Increasing growth %i to %i\n",
1388 overall_growth, caller_growth);
1389 overall_growth = caller_growth;
1390 }
1391 }
1392 if (overall_growth > 0)
1393 {
e4112065 1394 /* Strongly prefer functions with few callers that can be inlined
41f669d8
JH
1395 fully. The square root here leads to smaller binaries at average.
1396 Watch however for extreme cases and return to linear function
1397 when growth is large. */
1398 if (overall_growth < 256)
1399 overall_growth *= overall_growth;
1400 else
1401 overall_growth += 256 * 256 - 256;
1402 denominator *= overall_growth;
1403 }
f658ad30 1404 denominator *= ipa_size_summaries->get (caller)->size + growth;
6d4ab5f8
JH
1405
1406 badness = - numerator / denominator;
1407
1ce18dc8
JH
1408 if (dump)
1409 {
1410 fprintf (dump_file,
16998094
JM
1411 " %f: guessed profile. frequency %f, count %" PRId64
1412 " caller count %" PRId64
ea8dd3b6 1413 " time saved %f"
41f669d8
JH
1414 " overall growth %i (current) %i (original)"
1415 " %i (compensated)\n",
1416 badness.to_double (),
b5351388 1417 freq.to_double (),
f157c536
JH
1418 edge->count.ipa ().initialized_p ()
1419 ? edge->count.ipa ().to_gcov_type () : -1,
1420 caller->count.ipa ().initialized_p ()
1421 ? caller->count.ipa ().to_gcov_type () : -1,
1422 inlining_speedup (edge, freq, unspec_edge_time,
1423 edge_time).to_double (),
d59171da 1424 estimate_growth (callee),
41f669d8 1425 callee_info->growth, overall_growth);
1ce18dc8 1426 }
45a80bb9
JH
1427 }
1428 /* When function local profile is not available or it does not give
956d615d 1429 useful information (i.e. frequency is zero), base the cost on
45a80bb9
JH
1430 loop nest and overall size growth, so we optimize for overall number
1431 of functions fully inlined in program. */
1432 else
1433 {
56f62793 1434 int nest = MIN (ipa_call_summaries->get (edge)->loop_depth, 8);
6d4ab5f8 1435 badness = growth;
670cd5c5 1436
45a80bb9 1437 /* Decrease badness if call is nested. */
b8698a0f 1438 if (badness > 0)
f0e1509b 1439 badness = badness >> nest;
45a80bb9 1440 else
6d4ab5f8 1441 badness = badness << nest;
1ce18dc8 1442 if (dump)
41f669d8
JH
1443 fprintf (dump_file, " %f: no profile. nest %i\n",
1444 badness.to_double (), nest);
670cd5c5 1445 }
6d4ab5f8 1446 gcc_checking_assert (badness != 0);
1ce18dc8 1447
3dafb85c 1448 if (edge->recursive_p ())
6d4ab5f8
JH
1449 badness = badness.shift (badness > 0 ? 4 : -4);
1450 if ((hints & (INLINE_HINT_indirect_call
1451 | INLINE_HINT_loop_iterations
6d4ab5f8
JH
1452 | INLINE_HINT_loop_stride))
1453 || callee_info->growth <= 0)
1454 badness = badness.shift (badness > 0 ? -2 : 2);
caaa218f
JH
1455 if (hints & INLINE_HINT_builtin_constant_p)
1456 badness = badness.shift (badness > 0 ? -4 : 4);
6d4ab5f8
JH
1457 if (hints & (INLINE_HINT_same_scc))
1458 badness = badness.shift (badness > 0 ? 3 : -3);
1459 else if (hints & (INLINE_HINT_in_scc))
1460 badness = badness.shift (badness > 0 ? 2 : -2);
1461 else if (hints & (INLINE_HINT_cross_module))
1462 badness = badness.shift (badness > 0 ? 1 : -1);
208e5afa
JH
1463 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
1464 badness = badness.shift (badness > 0 ? -4 : 4);
1465 else if ((hints & INLINE_HINT_declared_inline))
6d4ab5f8
JH
1466 badness = badness.shift (badness > 0 ? -3 : 3);
1467 if (dump)
1468 fprintf (dump_file, " Adjusted by hints %f\n", badness.to_double ());
1469 return badness;
670cd5c5
JH
1470}
1471
9b8051b4 1472/* Recompute badness of EDGE and update its key in HEAP if needed. */
4c0f7679 1473static inline void
4a910049 1474update_edge_key (edge_heap_t *heap, struct cgraph_edge *edge)
9b8051b4 1475{
f0e1509b 1476 sreal badness = edge_badness (edge, false);
9b8051b4
JH
1477 if (edge->aux)
1478 {
4a910049
ML
1479 edge_heap_node_t *n = (edge_heap_node_t *) edge->aux;
1480 gcc_checking_assert (n->get_data () == edge);
9b8051b4 1481
6d4ab5f8 1482 /* fibonacci_heap::replace_key does busy updating of the
956d615d 1483 heap that is unnecessarily expensive.
6d4ab5f8
JH
1484 We do lazy increases: after extracting minimum if the key
1485 turns out to be out of date, it is re-inserted into heap
1486 with correct value. */
68c0df8d 1487 if (badness < n->get_key ().badness)
9b8051b4 1488 {
4c0f7679
JH
1489 if (dump_file && (dump_flags & TDF_DETAILS))
1490 {
1491 fprintf (dump_file,
464d0118
ML
1492 " decreasing badness %s -> %s, %f to %f\n",
1493 edge->caller->dump_name (),
1494 edge->callee->dump_name (),
68c0df8d 1495 n->get_key ().badness.to_double (),
6d4ab5f8 1496 badness.to_double ());
4c0f7679 1497 }
68c0df8d
JH
1498 inline_badness b (edge, badness);
1499 heap->decrease_key (n, b);
9b8051b4
JH
1500 }
1501 }
1502 else
4c0f7679
JH
1503 {
1504 if (dump_file && (dump_flags & TDF_DETAILS))
1505 {
1506 fprintf (dump_file,
464d0118
ML
1507 " enqueuing call %s -> %s, badness %f\n",
1508 edge->caller->dump_name (),
1509 edge->callee->dump_name (),
6d4ab5f8 1510 badness.to_double ());
4c0f7679 1511 }
68c0df8d
JH
1512 inline_badness b (edge, badness);
1513 edge->aux = heap->insert (b, edge);
4c0f7679 1514 }
9b8051b4
JH
1515}
1516
40fda55b
JH
1517
1518/* NODE was inlined.
956d615d 1519 All caller edges needs to be reset because
40fda55b
JH
1520 size estimates change. Similarly callees needs reset
1521 because better context may be known. */
1522
1523static void
1524reset_edge_caches (struct cgraph_node *node)
1525{
1526 struct cgraph_edge *edge;
1527 struct cgraph_edge *e = node->callees;
1528 struct cgraph_node *where = node;
e55637b7 1529 struct ipa_ref *ref;
40fda55b 1530
a62bfab5
ML
1531 if (where->inlined_to)
1532 where = where->inlined_to;
40fda55b 1533
ac6f2e59
JH
1534 reset_node_cache (where);
1535
9fb50ad8
ML
1536 if (edge_growth_cache != NULL)
1537 for (edge = where->callers; edge; edge = edge->next_caller)
1538 if (edge->inline_failed)
1539 edge_growth_cache->remove (edge);
e55637b7
ML
1540
1541 FOR_EACH_ALIAS (where, ref)
1542 reset_edge_caches (dyn_cast <cgraph_node *> (ref->referring));
40fda55b
JH
1543
1544 if (!e)
1545 return;
1546
1547 while (true)
1548 if (!e->inline_failed && e->callee->callees)
1549 e = e->callee->callees;
1550 else
1551 {
9fb50ad8
ML
1552 if (edge_growth_cache != NULL && e->inline_failed)
1553 edge_growth_cache->remove (e);
40fda55b
JH
1554 if (e->next_callee)
1555 e = e->next_callee;
1556 else
1557 {
1558 do
1559 {
1560 if (e->caller == node)
1561 return;
1562 e = e->caller->callers;
1563 }
1564 while (!e->next_callee);
1565 e = e->next_callee;
1566 }
1567 }
1568}
1569
1570/* Recompute HEAP nodes for each of caller of NODE.
1571 UPDATED_NODES track nodes we already visited, to avoid redundant work.
1572 When CHECK_INLINABLITY_FOR is set, re-check for specified edge that
1573 it is inlinable. Otherwise check all edges. */
670cd5c5
JH
1574
1575static void
4a910049 1576update_caller_keys (edge_heap_t *heap, struct cgraph_node *node,
40fda55b
JH
1577 bitmap updated_nodes,
1578 struct cgraph_edge *check_inlinablity_for)
670cd5c5
JH
1579{
1580 struct cgraph_edge *edge;
e55637b7 1581 struct ipa_ref *ref;
670cd5c5 1582
56f62793 1583 if ((!node->alias && !ipa_fn_summaries->get (node)->inlinable)
a62bfab5 1584 || node->inlined_to)
670cd5c5 1585 return;
4325656f 1586 if (!bitmap_set_bit (updated_nodes, node->get_uid ()))
670cd5c5 1587 return;
670cd5c5 1588
e55637b7
ML
1589 FOR_EACH_ALIAS (node, ref)
1590 {
1591 struct cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1592 update_caller_keys (heap, alias, updated_nodes, check_inlinablity_for);
1593 }
39e2db00 1594
cdc029b9 1595 for (edge = node->callers; edge; edge = edge->next_caller)
4c0f7679
JH
1596 if (edge->inline_failed)
1597 {
40fda55b
JH
1598 if (!check_inlinablity_for
1599 || check_inlinablity_for == edge)
f10d1a74 1600 {
9a4841a3
JH
1601 if (can_inline_edge_p (edge, false)
1602 && want_inline_small_function_p (edge, false)
1603 && can_inline_edge_by_limits_p (edge, false))
40fda55b
JH
1604 update_edge_key (heap, edge);
1605 else if (edge->aux)
1606 {
1607 report_inline_failed_reason (edge);
4a910049 1608 heap->delete_node ((edge_heap_node_t *) edge->aux);
40fda55b
JH
1609 edge->aux = NULL;
1610 }
f10d1a74 1611 }
40fda55b
JH
1612 else if (edge->aux)
1613 update_edge_key (heap, edge);
4c0f7679 1614 }
9b8051b4
JH
1615}
1616
7c6f2fb9
JH
1617/* Recompute HEAP nodes for each uninlined call in NODE
1618 If UPDATE_SINCE is non-NULL check if edges called within that function
1619 are inlinable (typically UPDATE_SINCE is the inline clone we introduced
1620 where all edges have new context).
1621
9b8051b4
JH
1622 This is used when we know that edge badnesses are going only to increase
1623 (we introduced new call site) and thus all we need is to insert newly
1624 created edges into heap. */
1625
1626static void
4a910049 1627update_callee_keys (edge_heap_t *heap, struct cgraph_node *node,
7c6f2fb9 1628 struct cgraph_node *update_since,
9b8051b4
JH
1629 bitmap updated_nodes)
1630{
1631 struct cgraph_edge *e = node->callees;
7c6f2fb9 1632 bool check_inlinability = update_since == node;
09a2806f 1633
9b8051b4
JH
1634 if (!e)
1635 return;
1636 while (true)
1637 if (!e->inline_failed && e->callee->callees)
7c6f2fb9
JH
1638 {
1639 if (e->callee == update_since)
1640 check_inlinability = true;
1641 e = e->callee->callees;
1642 }
9b8051b4 1643 else
670cd5c5 1644 {
a5b1779f
JH
1645 enum availability avail;
1646 struct cgraph_node *callee;
7c6f2fb9
JH
1647 if (!check_inlinability)
1648 {
1649 if (e->aux
1650 && !bitmap_bit_p (updated_nodes,
1651 e->callee->ultimate_alias_target
1652 (&avail, e->caller)->get_uid ()))
1653 update_edge_key (heap, e);
1654 }
58696ce5 1655 /* We do not reset callee growth cache here. Since we added a new call,
956d615d 1656 growth should have just increased and consequently badness metric
58696ce5 1657 don't need updating. */
7c6f2fb9
JH
1658 else if (e->inline_failed
1659 && (callee = e->callee->ultimate_alias_target (&avail,
1660 e->caller))
1661 && avail >= AVAIL_AVAILABLE
1662 && ipa_fn_summaries->get (callee) != NULL
1663 && ipa_fn_summaries->get (callee)->inlinable
1664 && !bitmap_bit_p (updated_nodes, callee->get_uid ()))
670cd5c5 1665 {
9a4841a3
JH
1666 if (can_inline_edge_p (e, false)
1667 && want_inline_small_function_p (e, false)
1668 && can_inline_edge_by_limits_p (e, false))
7c6f2fb9
JH
1669 {
1670 gcc_checking_assert (check_inlinability || can_inline_edge_p (e, false));
1671 gcc_checking_assert (check_inlinability || e->aux);
1672 update_edge_key (heap, e);
1673 }
40fda55b
JH
1674 else if (e->aux)
1675 {
1676 report_inline_failed_reason (e);
4a910049 1677 heap->delete_node ((edge_heap_node_t *) e->aux);
40fda55b
JH
1678 e->aux = NULL;
1679 }
9b8051b4 1680 }
7c6f2fb9
JH
1681 /* In case we redirected to unreachable node we only need to remove the
1682 fibheap entry. */
1683 else if (e->aux)
1684 {
1685 heap->delete_node ((edge_heap_node_t *) e->aux);
1686 e->aux = NULL;
1687 }
9b8051b4
JH
1688 if (e->next_callee)
1689 e = e->next_callee;
1690 else
1691 {
1692 do
1ce18dc8 1693 {
9b8051b4
JH
1694 if (e->caller == node)
1695 return;
7c6f2fb9
JH
1696 if (e->caller == update_since)
1697 check_inlinability = false;
9b8051b4 1698 e = e->caller->callers;
1ce18dc8 1699 }
9b8051b4
JH
1700 while (!e->next_callee);
1701 e = e->next_callee;
670cd5c5 1702 }
670cd5c5
JH
1703 }
1704}
1705
670cd5c5 1706/* Enqueue all recursive calls from NODE into priority queue depending on
0fa2e4df 1707 how likely we want to recursively inline the call. */
670cd5c5 1708
ca31b95f
JH
1709static void
1710lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
4a910049 1711 edge_heap_t *heap)
ca31b95f
JH
1712{
1713 struct cgraph_edge *e;
a5b1779f
JH
1714 enum availability avail;
1715
ca31b95f 1716 for (e = where->callees; e; e = e->next_callee)
a5b1779f 1717 if (e->callee == node
e6007a27 1718 || (e->callee->ultimate_alias_target (&avail, e->caller) == node
d52f5295 1719 && avail > AVAIL_INTERPOSABLE))
68c0df8d
JH
1720 {
1721 inline_badness b (e, -e->sreal_frequency ());
1722 heap->insert (b, e);
1723 }
ca31b95f
JH
1724 for (e = where->callees; e; e = e->next_callee)
1725 if (!e->inline_failed)
670cd5c5 1726 lookup_recursive_calls (node, e->callee, heap);
ca31b95f
JH
1727}
1728
1729/* Decide on recursive inlining: in the case function has recursive calls,
3e293154 1730 inline until body size reaches given argument. If any new indirect edges
e56f5f3e
JJ
1731 are discovered in the process, add them to *NEW_EDGES, unless NEW_EDGES
1732 is NULL. */
670cd5c5
JH
1733
1734static bool
4c0f7679 1735recursive_inlining (struct cgraph_edge *edge,
d52f5295 1736 vec<cgraph_edge *> *new_edges)
ca31b95f 1737{
1e83bd70
JH
1738 cgraph_node *to = (edge->caller->inlined_to
1739 ? edge->caller->inlined_to : edge->caller);
1740 int limit = opt_for_fn (to->decl,
1741 param_max_inline_insns_recursive_auto);
68c0df8d
JH
1742 inline_badness b (edge, sreal::min ());
1743 edge_heap_t heap (b);
d7d1d041 1744 struct cgraph_node *node;
ca31b95f 1745 struct cgraph_edge *e;
4c0f7679 1746 struct cgraph_node *master_clone = NULL, *next;
ca31b95f
JH
1747 int depth = 0;
1748 int n = 0;
1749
d7d1d041 1750 node = edge->caller;
a62bfab5
ML
1751 if (node->inlined_to)
1752 node = node->inlined_to;
d7d1d041 1753
67348ccc 1754 if (DECL_DECLARED_INLINE_P (node->decl))
1e83bd70 1755 limit = opt_for_fn (to->decl, param_max_inline_insns_recursive);
ca31b95f
JH
1756
1757 /* Make sure that function is small enough to be considered for inlining. */
4c0f7679 1758 if (estimate_size_after_inlining (node, edge) >= limit)
670cd5c5 1759 return false;
4a910049
ML
1760 lookup_recursive_calls (node, node, &heap);
1761 if (heap.empty ())
1762 return false;
ca31b95f
JH
1763
1764 if (dump_file)
b8698a0f 1765 fprintf (dump_file,
3629ff8a 1766 " Performing recursive inlining on %s\n", node->dump_name ());
ca31b95f 1767
ca31b95f 1768 /* Do the inlining and update list of recursive call during process. */
4a910049 1769 while (!heap.empty ())
ca31b95f 1770 {
4a910049 1771 struct cgraph_edge *curr = heap.extract_min ();
6ced940d 1772 struct cgraph_node *cnode, *dest = curr->callee;
d7d1d041 1773
9a4841a3 1774 if (!can_inline_edge_p (curr, true)
4eb50396 1775 || !can_inline_edge_by_limits_p (curr, true))
4c0f7679
JH
1776 continue;
1777
6ced940d
JH
1778 /* MASTER_CLONE is produced in the case we already started modified
1779 the function. Be sure to redirect edge to the original body before
1780 estimating growths otherwise we will be seeing growths after inlining
1781 the already modified body. */
1782 if (master_clone)
1783 {
3dafb85c 1784 curr->redirect_callee (master_clone);
9fb50ad8
ML
1785 if (edge_growth_cache != NULL)
1786 edge_growth_cache->remove (curr);
6ced940d
JH
1787 }
1788
1789 if (estimate_size_after_inlining (node, curr) > limit)
1790 {
3dafb85c 1791 curr->redirect_callee (dest);
9fb50ad8
ML
1792 if (edge_growth_cache != NULL)
1793 edge_growth_cache->remove (curr);
6ced940d
JH
1794 break;
1795 }
1796
c5a4444c
JH
1797 depth = 1;
1798 for (cnode = curr->caller;
a62bfab5 1799 cnode->inlined_to; cnode = cnode->callers->caller)
67348ccc 1800 if (node->decl
d52f5295 1801 == curr->callee->ultimate_alias_target ()->decl)
f791d333 1802 depth++;
c5a4444c 1803
4c0f7679 1804 if (!want_inline_self_recursive_call_p (curr, node, false, depth))
6ced940d 1805 {
3dafb85c 1806 curr->redirect_callee (dest);
9fb50ad8
ML
1807 if (edge_growth_cache != NULL)
1808 edge_growth_cache->remove (curr);
6ced940d
JH
1809 continue;
1810 }
ca31b95f 1811
670cd5c5 1812 if (dump_file)
c5a4444c 1813 {
b8698a0f 1814 fprintf (dump_file,
c5a4444c 1815 " Inlining call of depth %i", depth);
ae94bb0e 1816 if (node->count.nonzero_p () && curr->count.initialized_p ())
c5a4444c
JH
1817 {
1818 fprintf (dump_file, " called approx. %.2f times per call",
3995f3a2
JH
1819 (double)curr->count.to_gcov_type ()
1820 / node->count.to_gcov_type ());
c5a4444c
JH
1821 }
1822 fprintf (dump_file, "\n");
1823 }
4c0f7679
JH
1824 if (!master_clone)
1825 {
1826 /* We need original clone to copy around. */
d52f5295 1827 master_clone = node->create_clone (node->decl, node->count,
1bad9c18 1828 false, vNULL, true, NULL, NULL);
4c0f7679
JH
1829 for (e = master_clone->callees; e; e = e->next_callee)
1830 if (!e->inline_failed)
1bad9c18 1831 clone_inlined_nodes (e, true, false, NULL);
3dafb85c 1832 curr->redirect_callee (master_clone);
9fb50ad8
ML
1833 if (edge_growth_cache != NULL)
1834 edge_growth_cache->remove (curr);
4c0f7679
JH
1835 }
1836
c170d40f 1837 inline_call (curr, false, new_edges, &overall_size, true);
b914768c 1838 reset_node_cache (node);
4a910049 1839 lookup_recursive_calls (node, curr->callee, &heap);
ca31b95f
JH
1840 n++;
1841 }
4c0f7679 1842
4a910049 1843 if (!heap.empty () && dump_file)
c5a4444c 1844 fprintf (dump_file, " Recursive inlining growth limit met.\n");
4c0f7679
JH
1845
1846 if (!master_clone)
1847 return false;
1848
4174a33a
DM
1849 if (dump_enabled_p ())
1850 dump_printf_loc (MSG_NOTE, edge->call_stmt,
1851 "\n Inlined %i times, "
1852 "body grown from size %i to %i, time %f to %f\n", n,
f658ad30
JH
1853 ipa_size_summaries->get (master_clone)->size,
1854 ipa_size_summaries->get (node)->size,
4174a33a
DM
1855 ipa_fn_summaries->get (master_clone)->time.to_double (),
1856 ipa_fn_summaries->get (node)->time.to_double ());
ca31b95f
JH
1857
1858 /* Remove master clone we used for inlining. We rely that clones inlined
1859 into master clone gets queued just before master clone so we don't
1860 need recursion. */
3dafb85c 1861 for (node = symtab->first_function (); node != master_clone;
96fc428c
JH
1862 node = next)
1863 {
3dafb85c 1864 next = symtab->next_function (node);
a62bfab5 1865 if (node->inlined_to == master_clone)
d52f5295 1866 node->remove ();
96fc428c 1867 }
d52f5295 1868 master_clone->remove ();
4c0f7679 1869 return true;
ca31b95f
JH
1870}
1871
09a2806f 1872
88512ba0 1873/* Given whole compilation unit estimate of INSNS, compute how large we can
b7c27d51 1874 allow the unit to grow. */
09a2806f 1875
8fcfc44f 1876static int64_t
1e83bd70 1877compute_max_insns (cgraph_node *node, int insns)
b7c27d51
JH
1878{
1879 int max_insns = insns;
1e83bd70
JH
1880 if (max_insns < opt_for_fn (node->decl, param_large_unit_insns))
1881 max_insns = opt_for_fn (node->decl, param_large_unit_insns);
b7c27d51 1882
a9243bfc 1883 return ((int64_t) max_insns
1e83bd70 1884 * (100 + opt_for_fn (node->decl, param_inline_unit_growth)) / 100);
b7c27d51
JH
1885}
1886
09a2806f 1887
3e293154 1888/* Compute badness of all edges in NEW_EDGES and add them to the HEAP. */
09a2806f 1889
3e293154 1890static void
00dcc88a 1891add_new_edges_to_heap (edge_heap_t *heap, vec<cgraph_edge *> &new_edges)
3e293154 1892{
9771b263 1893 while (new_edges.length () > 0)
3e293154 1894 {
9771b263 1895 struct cgraph_edge *edge = new_edges.pop ();
3e293154
MJ
1896
1897 gcc_assert (!edge->aux);
4517b378 1898 gcc_assert (edge->callee);
a5b1779f 1899 if (edge->inline_failed
4c0f7679 1900 && can_inline_edge_p (edge, true)
9a4841a3
JH
1901 && want_inline_small_function_p (edge, true)
1902 && can_inline_edge_by_limits_p (edge, true))
68c0df8d
JH
1903 {
1904 inline_badness b (edge, edge_badness (edge, false));
1905 edge->aux = heap->insert (b, edge);
1906 }
3e293154
MJ
1907 }
1908}
1909
042ae7d2
JH
1910/* Remove EDGE from the fibheap. */
1911
1912static void
1913heap_edge_removal_hook (struct cgraph_edge *e, void *data)
1914{
1915 if (e->aux)
1916 {
4a910049 1917 ((edge_heap_t *)data)->delete_node ((edge_heap_node_t *)e->aux);
042ae7d2
JH
1918 e->aux = NULL;
1919 }
1920}
3e293154 1921
09ce3660
JH
1922/* Return true if speculation of edge E seems useful.
1923 If ANTICIPATE_INLINING is true, be conservative and hope that E
1924 may get inlined. */
1925
1926bool
1927speculation_useful_p (struct cgraph_edge *e, bool anticipate_inlining)
1928{
4517b378
MJ
1929 /* If we have already decided to inline the edge, it seems useful. */
1930 if (!e->inline_failed)
1931 return true;
1932
09ce3660 1933 enum availability avail;
e6007a27
JH
1934 struct cgraph_node *target = e->callee->ultimate_alias_target (&avail,
1935 e->caller);
09ce3660
JH
1936
1937 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1938
3dafb85c 1939 if (!e->maybe_hot_p ())
09ce3660
JH
1940 return false;
1941
1942 /* See if IP optimizations found something potentially useful about the
1943 function. For now we look only for CONST/PURE flags. Almost everything
1944 else we propagate is useless. */
1945 if (avail >= AVAIL_AVAILABLE)
1946 {
67348ccc 1947 int ecf_flags = flags_from_decl_or_type (target->decl);
09ce3660
JH
1948 if (ecf_flags & ECF_CONST)
1949 {
845bb366
JH
1950 if (!(e->speculative_call_indirect_edge ()->indirect_info
1951 ->ecf_flags & ECF_CONST))
09ce3660
JH
1952 return true;
1953 }
1954 else if (ecf_flags & ECF_PURE)
1955 {
845bb366
JH
1956 if (!(e->speculative_call_indirect_edge ()->indirect_info
1957 ->ecf_flags & ECF_PURE))
09ce3660
JH
1958 return true;
1959 }
1960 }
1961 /* If we did not managed to inline the function nor redirect
1962 to an ipa-cp clone (that are seen by having local flag set),
1963 it is probably pointless to inline it unless hardware is missing
1964 indirect call predictor. */
87f94429 1965 if (!anticipate_inlining && !target->local)
09ce3660
JH
1966 return false;
1967 /* For overwritable targets there is not much to do. */
4517b378
MJ
1968 if (!can_inline_edge_p (e, false)
1969 || !can_inline_edge_by_limits_p (e, false, true))
09ce3660
JH
1970 return false;
1971 /* OK, speculation seems interesting. */
1972 return true;
1973}
1974
1975/* We know that EDGE is not going to be inlined.
1976 See if we can remove speculation. */
1977
1978static void
4a910049 1979resolve_noninline_speculation (edge_heap_t *edge_heap, struct cgraph_edge *edge)
09ce3660
JH
1980{
1981 if (edge->speculative && !speculation_useful_p (edge, false))
1982 {
1983 struct cgraph_node *node = edge->caller;
a62bfab5
ML
1984 struct cgraph_node *where = node->inlined_to
1985 ? node->inlined_to : node;
0e3de1d4 1986 auto_bitmap updated_nodes;
09ce3660 1987
1bad9c18
JH
1988 if (edge->count.ipa ().initialized_p ())
1989 spec_rem += edge->count.ipa ();
27c5a177 1990 cgraph_edge::resolve_speculation (edge);
09ce3660 1991 reset_edge_caches (where);
0bceb671 1992 ipa_update_overall_fn_summary (where);
09ce3660
JH
1993 update_caller_keys (edge_heap, where,
1994 updated_nodes, NULL);
7c6f2fb9 1995 update_callee_keys (edge_heap, where, NULL,
d0b66480 1996 updated_nodes);
09ce3660
JH
1997 }
1998}
1999
bb1e543c
JH
2000/* Return true if NODE should be accounted for overall size estimate.
2001 Skip all nodes optimized for size so we can measure the growth of hot
2002 part of program no matter of the padding. */
2003
2004bool
2005inline_account_function_p (struct cgraph_node *node)
2006{
2007 return (!DECL_EXTERNAL (node->decl)
2008 && !opt_for_fn (node->decl, optimize_size)
2009 && node->frequency != NODE_FREQUENCY_UNLIKELY_EXECUTED);
2010}
2011
41f669d8
JH
2012/* Count number of callers of NODE and store it into DATA (that
2013 points to int. Worker for cgraph_for_node_and_aliases. */
2014
2015static bool
2016sum_callers (struct cgraph_node *node, void *data)
2017{
2018 struct cgraph_edge *e;
2019 int *num_calls = (int *)data;
2020
2021 for (e = node->callers; e; e = e->next_caller)
2022 (*num_calls)++;
2023 return false;
2024}
2025
97e59627
ML
2026/* We only propagate across edges with non-interposable callee. */
2027
2028inline bool
2029ignore_edge_p (struct cgraph_edge *e)
2030{
2031 enum availability avail;
2032 e->callee->function_or_virtual_thunk_symbol (&avail, e->caller);
2033 return (avail <= AVAIL_INTERPOSABLE);
2034}
2035
ca31b95f 2036/* We use greedy algorithm for inlining of small functions:
09a2806f
JH
2037 All inline candidates are put into prioritized heap ordered in
2038 increasing badness.
ca31b95f 2039
09a2806f 2040 The inlining of small functions is bounded by unit growth parameters. */
ca31b95f
JH
2041
2042static void
4c0f7679 2043inline_small_functions (void)
ca31b95f
JH
2044{
2045 struct cgraph_node *node;
670cd5c5 2046 struct cgraph_edge *edge;
68c0df8d
JH
2047 inline_badness b;
2048 edge_heap_t edge_heap (b);
0e3de1d4 2049 auto_bitmap updated_nodes;
1e83bd70 2050 int min_size;
d52f5295 2051 auto_vec<cgraph_edge *> new_indirect_edges;
09a2806f 2052 int initial_size = 0;
3dafb85c 2053 struct cgraph_node **order = XCNEWVEC (cgraph_node *, symtab->cgraph_count);
042ae7d2 2054 struct cgraph_edge_hook_list *edge_removal_hook_holder;
2bf86c84 2055 new_indirect_edges.create (8);
670cd5c5 2056
042ae7d2 2057 edge_removal_hook_holder
4a910049 2058 = symtab->add_edge_removal_hook (&heap_edge_removal_hook, &edge_heap);
042ae7d2 2059
1a3118e9
JH
2060 /* Compute overall unit size and other global parameters used by badness
2061 metrics. */
ca31b95f 2062
3995f3a2 2063 max_count = profile_count::uninitialized ();
97e59627 2064 ipa_reduced_postorder (order, true, ignore_edge_p);
68cc8feb 2065 free (order);
1a3118e9 2066
c47d0034 2067 FOR_EACH_DEFINED_FUNCTION (node)
a62bfab5 2068 if (!node->inlined_to)
e7f23018 2069 {
bb1e543c 2070 if (!node->alias && node->analyzed
67f3791f 2071 && (node->has_gimple_body_p () || node->thunk)
29f1e2b1 2072 && opt_for_fn (node->decl, optimize))
a5b1779f 2073 {
99b1c316 2074 class ipa_fn_summary *info = ipa_fn_summaries->get (node);
67348ccc 2075 struct ipa_dfs_info *dfs = (struct ipa_dfs_info *) node->aux;
ca31b95f 2076
5e750dc6
JH
2077 /* Do not account external functions, they will be optimized out
2078 if not inlined. Also only count the non-cold portion of program. */
bb1e543c 2079 if (inline_account_function_p (node))
f658ad30 2080 initial_size += ipa_size_summaries->get (node)->size;
d59171da 2081 info->growth = estimate_growth (node);
41f669d8
JH
2082
2083 int num_calls = 0;
2084 node->call_for_symbol_and_aliases (sum_callers, &num_calls,
2085 true);
2086 if (num_calls == 1)
2087 info->single_caller = true;
bf3f6510
JH
2088 if (dfs && dfs->next_cycle)
2089 {
2090 struct cgraph_node *n2;
2091 int id = dfs->scc_no + 1;
2092 for (n2 = node; n2;
1a03b929 2093 n2 = ((struct ipa_dfs_info *) n2->aux)->next_cycle)
29f1e2b1
JH
2094 if (opt_for_fn (n2->decl, optimize))
2095 {
f658ad30 2096 ipa_fn_summary *info2 = ipa_fn_summaries->get
a62bfab5 2097 (n2->inlined_to ? n2->inlined_to : n2);
29f1e2b1
JH
2098 if (info2->scc_no)
2099 break;
2100 info2->scc_no = id;
2101 }
bf3f6510 2102 }
a5b1779f 2103 }
09a2806f 2104
e7f23018 2105 for (edge = node->callers; edge; edge = edge->next_caller)
1bad9c18 2106 max_count = max_count.max (edge->count.ipa ());
e7f23018 2107 }
b48ccf0d 2108 ipa_free_postorder_info ();
ac6f2e59 2109 initialize_growth_caches ();
b48ccf0d
JH
2110
2111 if (dump_file)
2112 fprintf (dump_file,
2113 "\nDeciding on inlining of small functions. Starting with size %i.\n",
2114 initial_size);
b7c27d51 2115
8a8dccb2 2116 overall_size = initial_size;
85057983 2117 min_size = overall_size;
1a3118e9 2118
a99be3c9 2119 /* Populate the heap with all edges we might inline. */
1a3118e9 2120
c47d0034 2121 FOR_EACH_DEFINED_FUNCTION (node)
09ce3660
JH
2122 {
2123 bool update = false;
bcda57c1 2124 struct cgraph_edge *next = NULL;
c1eed5a1 2125 bool has_speculative = false;
1a3118e9 2126
c9521268
RB
2127 if (!opt_for_fn (node->decl, optimize)
2128 /* With -Og we do not want to perform IPA inlining of small
2129 functions since there are no scalar cleanups after it
2130 that would realize the anticipated win. All abstraction
2131 is removed during early inlining. */
2132 || opt_for_fn (node->decl, optimize_debug))
29f1e2b1
JH
2133 continue;
2134
09ce3660 2135 if (dump_file)
464d0118 2136 fprintf (dump_file, "Enqueueing calls in %s.\n", node->dump_name ());
09ce3660 2137
8fcfc44f 2138 for (edge = node->callees; edge; edge = edge->next_callee)
09ce3660 2139 {
1a3118e9 2140 if (edge->inline_failed
09ce3660 2141 && !edge->aux
1a3118e9
JH
2142 && can_inline_edge_p (edge, true)
2143 && want_inline_small_function_p (edge, true)
9a4841a3 2144 && can_inline_edge_by_limits_p (edge, true)
1a3118e9
JH
2145 && edge->inline_failed)
2146 {
2147 gcc_assert (!edge->aux);
4a910049 2148 update_edge_key (&edge_heap, edge);
1a3118e9 2149 }
c1eed5a1
JH
2150 if (edge->speculative)
2151 has_speculative = true;
2152 }
2153 if (has_speculative)
2154 for (edge = node->callees; edge; edge = next)
2aae99f7 2155 {
2e98ac86 2156 next = edge->next_callee;
2aae99f7
XHL
2157 if (edge->speculative
2158 && !speculation_useful_p (edge, edge->aux != NULL))
2159 {
27c5a177 2160 cgraph_edge::resolve_speculation (edge);
2aae99f7
XHL
2161 update = true;
2162 }
2aae99f7 2163 }
09ce3660
JH
2164 if (update)
2165 {
a62bfab5
ML
2166 struct cgraph_node *where = node->inlined_to
2167 ? node->inlined_to : node;
0bceb671 2168 ipa_update_overall_fn_summary (where);
09ce3660 2169 reset_edge_caches (where);
4a910049 2170 update_caller_keys (&edge_heap, where,
09ce3660 2171 updated_nodes, NULL);
7c6f2fb9 2172 update_callee_keys (&edge_heap, where, NULL,
2001028a 2173 updated_nodes);
09ce3660
JH
2174 bitmap_clear (updated_nodes);
2175 }
2176 }
1a3118e9 2177
09a2806f 2178 gcc_assert (in_lto_p
3995f3a2 2179 || !(max_count > 0)
09a2806f 2180 || (profile_info && flag_branch_probabilities));
b7c27d51 2181
4a910049 2182 while (!edge_heap.empty ())
ca31b95f 2183 {
85057983 2184 int old_size = overall_size;
1ce18dc8 2185 struct cgraph_node *where, *callee;
68c0df8d 2186 sreal badness = edge_heap.min_key ().badness;
f0e1509b 2187 sreal current_badness;
1ce18dc8 2188 int growth;
670cd5c5 2189
4a910049 2190 edge = edge_heap.extract_min ();
1ce18dc8
JH
2191 gcc_assert (edge->aux);
2192 edge->aux = NULL;
9de6f6c3 2193 if (!edge->inline_failed || !edge->callee->analyzed)
1ce18dc8 2194 continue;
cdc029b9 2195
1bad9c18
JH
2196 /* Be sure that caches are maintained consistent.
2197 This check is affected by scaling roundoff errors when compiling for
2198 IPA this we skip it in that case. */
b914768c 2199 if (flag_checking && !edge->callee->count.ipa_p ()
517048ce 2200 && (!max_count.initialized_p () || !max_count.nonzero_p ()))
1bad9c18
JH
2201 {
2202 sreal cached_badness = edge_badness (edge, false);
2203
2204 int old_size_est = estimate_edge_size (edge);
2205 sreal old_time_est = estimate_edge_time (edge);
2206 int old_hints_est = estimate_edge_hints (edge);
2207
9fb50ad8
ML
2208 if (edge_growth_cache != NULL)
2209 edge_growth_cache->remove (edge);
b914768c
JH
2210 reset_node_cache (edge->caller->inlined_to
2211 ? edge->caller->inlined_to
2212 : edge->caller);
1bad9c18
JH
2213 gcc_assert (old_size_est == estimate_edge_size (edge));
2214 gcc_assert (old_time_est == estimate_edge_time (edge));
2215 /* FIXME:
2216
2217 gcc_assert (old_hints_est == estimate_edge_hints (edge));
2218
2219 fails with profile feedback because some hints depends on
2220 maybe_hot_edge_p predicate and because callee gets inlined to other
2221 calls, the edge may become cold.
2222 This ought to be fixed by computing relative probabilities
2223 for given invocation but that will be better done once whole
2224 code is converted to sreals. Disable for now and revert to "wrong"
2225 value so enable/disable checking paths agree. */
9fb50ad8 2226 edge_growth_cache->get (edge)->hints = old_hints_est + 1;
1bad9c18
JH
2227
2228 /* When updating the edge costs, we only decrease badness in the keys.
956d615d 2229 Increases of badness are handled lazily; when we see key with out
1bad9c18
JH
2230 of date value on it, we re-insert it now. */
2231 current_badness = edge_badness (edge, false);
2232 gcc_assert (cached_badness == current_badness);
2233 gcc_assert (current_badness >= badness);
2234 }
f4118c87
JH
2235 else
2236 current_badness = edge_badness (edge, false);
cdc029b9
JH
2237 if (current_badness != badness)
2238 {
68c0df8d 2239 if (edge_heap.min () && current_badness > edge_heap.min_key ().badness)
6d4ab5f8 2240 {
68c0df8d
JH
2241 inline_badness b (edge, current_badness);
2242 edge->aux = edge_heap.insert (b, edge);
6d4ab5f8
JH
2243 continue;
2244 }
2245 else
2246 badness = current_badness;
cdc029b9 2247 }
4c0f7679 2248
9a4841a3
JH
2249 if (!can_inline_edge_p (edge, true)
2250 || !can_inline_edge_by_limits_p (edge, true))
09ce3660 2251 {
4a910049 2252 resolve_noninline_speculation (&edge_heap, edge);
09ce3660
JH
2253 continue;
2254 }
cdc029b9 2255
d52f5295 2256 callee = edge->callee->ultimate_alias_target ();
03dfc36d 2257 growth = estimate_edge_growth (edge);
ca31b95f 2258 if (dump_file)
ca31b95f 2259 {
b8698a0f 2260 fprintf (dump_file,
464d0118
ML
2261 "\nConsidering %s with %i size\n",
2262 callee->dump_name (),
f658ad30 2263 ipa_size_summaries->get (callee)->size);
b8698a0f 2264 fprintf (dump_file,
464d0118 2265 " to be inlined into %s in %s:%i\n"
6d4ab5f8 2266 " Estimated badness is %f, frequency %.2f.\n",
464d0118 2267 edge->caller->dump_name (),
9e145afd 2268 edge->call_stmt
355fe088 2269 && (LOCATION_LOCUS (gimple_location ((const gimple *)
1b7706c8
JJ
2270 edge->call_stmt))
2271 > BUILTINS_LOCATION)
355fe088 2272 ? gimple_filename ((const gimple *) edge->call_stmt)
9e145afd
N
2273 : "unknown",
2274 edge->call_stmt
355fe088 2275 ? gimple_lineno ((const gimple *) edge->call_stmt)
9e145afd 2276 : -1,
6d4ab5f8 2277 badness.to_double (),
0cea1d34 2278 edge->sreal_frequency ().to_double ());
1bad9c18 2279 if (edge->count.ipa ().initialized_p ())
3995f3a2
JH
2280 {
2281 fprintf (dump_file, " Called ");
1bad9c18 2282 edge->count.ipa ().dump (dump_file);
517048ce 2283 fprintf (dump_file, " times\n");
3995f3a2 2284 }
1ce18dc8 2285 if (dump_flags & TDF_DETAILS)
4c0f7679 2286 edge_badness (edge, true);
ca31b95f
JH
2287 }
2288
1e83bd70
JH
2289 where = edge->caller;
2290
2291 if (overall_size + growth > compute_max_insns (where, min_size)
67348ccc 2292 && !DECL_DISREGARD_INLINE_LIMITS (callee->decl))
670cd5c5 2293 {
4c0f7679
JH
2294 edge->inline_failed = CIF_INLINE_UNIT_GROWTH_LIMIT;
2295 report_inline_failed_reason (edge);
4a910049 2296 resolve_noninline_speculation (&edge_heap, edge);
670cd5c5
JH
2297 continue;
2298 }
4c0f7679
JH
2299
2300 if (!want_inline_small_function_p (edge, true))
09ce3660 2301 {
4a910049 2302 resolve_noninline_speculation (&edge_heap, edge);
09ce3660
JH
2303 continue;
2304 }
09a2806f 2305
7c6f2fb9
JH
2306 profile_count old_count = callee->count;
2307
7ba03e5e
JL
2308 /* Heuristics for inlining small functions work poorly for
2309 recursive calls where we do effects similar to loop unrolling.
2310 When inlining such edge seems profitable, leave decision on
09a2806f 2311 specific inliner. */
3dafb85c 2312 if (edge->recursive_p ())
670cd5c5 2313 {
a62bfab5
ML
2314 if (where->inlined_to)
2315 where = where->inlined_to;
4c0f7679 2316 if (!recursive_inlining (edge,
2bf86c84
JH
2317 opt_for_fn (edge->caller->decl,
2318 flag_indirect_inlining)
4c0f7679 2319 ? &new_indirect_edges : NULL))
d7d1d041
RG
2320 {
2321 edge->inline_failed = CIF_RECURSIVE_INLINING;
4a910049 2322 resolve_noninline_speculation (&edge_heap, edge);
d7d1d041
RG
2323 continue;
2324 }
40fda55b 2325 reset_edge_caches (where);
09a2806f
JH
2326 /* Recursive inliner inlines all recursive calls of the function
2327 at once. Consequently we need to update all callee keys. */
2bf86c84 2328 if (opt_for_fn (edge->caller->decl, flag_indirect_inlining))
4a910049 2329 add_new_edges_to_heap (&edge_heap, new_indirect_edges);
7c6f2fb9 2330 update_callee_keys (&edge_heap, where, where, updated_nodes);
09ce3660 2331 bitmap_clear (updated_nodes);
670cd5c5
JH
2332 }
2333 else
2334 {
4c0f7679
JH
2335 struct cgraph_node *outer_node = NULL;
2336 int depth = 0;
2337
7ba03e5e
JL
2338 /* Consider the case where self recursive function A is inlined
2339 into B. This is desired optimization in some cases, since it
2340 leads to effect similar of loop peeling and we might completely
2341 optimize out the recursive call. However we must be extra
2342 selective. */
4c0f7679
JH
2343
2344 where = edge->caller;
a62bfab5 2345 while (where->inlined_to)
670cd5c5 2346 {
67348ccc 2347 if (where->decl == callee->decl)
4c0f7679
JH
2348 outer_node = where, depth++;
2349 where = where->callers->caller;
2350 }
2351 if (outer_node
2352 && !want_inline_self_recursive_call_p (edge, outer_node,
2353 true, depth))
2354 {
2355 edge->inline_failed
67348ccc 2356 = (DECL_DISREGARD_INLINE_LIMITS (edge->callee->decl)
4c0f7679 2357 ? CIF_RECURSIVE_INLINING : CIF_UNSPECIFIED);
4a910049 2358 resolve_noninline_speculation (&edge_heap, edge);
670cd5c5
JH
2359 continue;
2360 }
4c0f7679
JH
2361 else if (depth && dump_file)
2362 fprintf (dump_file, " Peeling recursion with depth %i\n", depth);
2363
a62bfab5 2364 gcc_checking_assert (!callee->inlined_to);
041cb615
JH
2365
2366 int old_size = ipa_size_summaries->get (where)->size;
2367 sreal old_time = ipa_fn_summaries->get (where)->time;
2368
c170d40f 2369 inline_call (edge, true, &new_indirect_edges, &overall_size, true);
1f6f9079 2370 reset_edge_caches (edge->callee);
8d890d37 2371 add_new_edges_to_heap (&edge_heap, new_indirect_edges);
40fda55b 2372
041cb615 2373 /* If caller's size and time increased we do not need to update
956d615d 2374 all edges because badness is not going to decrease. */
041cb615
JH
2375 if (old_size <= ipa_size_summaries->get (where)->size
2376 && old_time <= ipa_fn_summaries->get (where)->time
2377 /* Wrapper penalty may be non-monotonous in this respect.
2378 Fortunately it only affects small functions. */
2379 && !wrapper_heuristics_may_apply (where, old_size))
7c6f2fb9
JH
2380 update_callee_keys (&edge_heap, edge->callee, edge->callee,
2381 updated_nodes);
041cb615 2382 else
7c6f2fb9
JH
2383 update_callee_keys (&edge_heap, where,
2384 edge->callee,
2385 updated_nodes);
670cd5c5
JH
2386 }
2387 where = edge->caller;
a62bfab5
ML
2388 if (where->inlined_to)
2389 where = where->inlined_to;
670cd5c5
JH
2390
2391 /* Our profitability metric can depend on local properties
2392 such as number of inlinable calls and size of the function body.
2393 After inlining these properties might change for the function we
2394 inlined into (since it's body size changed) and for the functions
2395 called by function we inlined (since number of it inlinable callers
2396 might change). */
4a910049 2397 update_caller_keys (&edge_heap, where, updated_nodes, NULL);
208e5afa
JH
2398 /* Offline copy count has possibly changed, recompute if profile is
2399 available. */
7c6f2fb9
JH
2400 struct cgraph_node *n
2401 = cgraph_node::get (edge->callee->decl)->ultimate_alias_target ();
2402 if (n != edge->callee && n->analyzed && !(n->count == old_count)
2403 && n->count.ipa_p ())
2404 update_callee_keys (&edge_heap, n, NULL, updated_nodes);
670cd5c5 2405 bitmap_clear (updated_nodes);
ca31b95f 2406
4174a33a 2407 if (dump_enabled_p ())
50fe876d 2408 {
f658ad30 2409 ipa_fn_summary *s = ipa_fn_summaries->get (where);
4174a33a
DM
2410
2411 /* dump_printf can't handle %+i. */
2412 char buf_net_change[100];
2413 snprintf (buf_net_change, sizeof buf_net_change, "%+i",
2414 overall_size - old_size);
2415
2416 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, edge->call_stmt,
2417 " Inlined %C into %C which now has time %f and "
b74d8dc4 2418 "size %i, net change of %s%s.\n",
4174a33a 2419 edge->callee, edge->caller,
f658ad30
JH
2420 s->time.to_double (),
2421 ipa_size_summaries->get (edge->caller)->size,
b74d8dc4
JH
2422 buf_net_change,
2423 cross_module_call_p (edge) ? " (cross module)":"");
50fe876d 2424 }
85057983 2425 if (min_size > overall_size)
b7c27d51 2426 {
85057983 2427 min_size = overall_size;
b7c27d51
JH
2428
2429 if (dump_file)
85057983 2430 fprintf (dump_file, "New minimal size reached: %i\n", min_size);
b7c27d51 2431 }
ca31b95f 2432 }
3e293154 2433
632b4f8e 2434 free_growth_caches ();
4174a33a
DM
2435 if (dump_enabled_p ())
2436 dump_printf (MSG_NOTE,
2437 "Unit growth for small function inlining: %i->%i (%i%%)\n",
2438 initial_size, overall_size,
2439 initial_size ? overall_size * 100 / (initial_size) - 100: 0);
3dafb85c 2440 symtab->remove_edge_removal_hook (edge_removal_hook_holder);
ca31b95f
JH
2441}
2442
09a2806f
JH
2443/* Flatten NODE. Performed both during early inlining and
2444 at IPA inlining time. */
af961c7f
RG
2445
2446static void
f6e809c8 2447flatten_function (struct cgraph_node *node, bool early, bool update)
af961c7f
RG
2448{
2449 struct cgraph_edge *e;
2450
2451 /* We shouldn't be called recursively when we are being processed. */
67348ccc 2452 gcc_assert (node->aux == NULL);
af961c7f 2453
67348ccc 2454 node->aux = (void *) node;
af961c7f
RG
2455
2456 for (e = node->callees; e; e = e->next_callee)
2457 {
2458 struct cgraph_node *orig_callee;
d52f5295 2459 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
af961c7f 2460
af961c7f 2461 /* We've hit cycle? It is time to give up. */
67348ccc 2462 if (callee->aux)
af961c7f 2463 {
4174a33a
DM
2464 if (dump_enabled_p ())
2465 dump_printf_loc (MSG_MISSED_OPTIMIZATION, e->call_stmt,
2466 "Not inlining %C into %C to avoid cycle.\n",
2467 callee, e->caller);
a99670f9
JH
2468 if (cgraph_inline_failed_type (e->inline_failed) != CIF_FINAL_ERROR)
2469 e->inline_failed = CIF_RECURSIVE_INLINING;
af961c7f
RG
2470 continue;
2471 }
2472
2473 /* When the edge is already inlined, we just need to recurse into
2474 it in order to fully flatten the leaves. */
2475 if (!e->inline_failed)
2476 {
f6e809c8 2477 flatten_function (callee, early, false);
af961c7f
RG
2478 continue;
2479 }
2480
4c0f7679
JH
2481 /* Flatten attribute needs to be processed during late inlining. For
2482 extra code quality we however do flattening during early optimization,
2483 too. */
632b4f8e 2484 if (!early
4c0f7679 2485 ? !can_inline_edge_p (e, true)
9a4841a3 2486 && !can_inline_edge_by_limits_p (e, true)
4c0f7679
JH
2487 : !can_early_inline_edge_p (e))
2488 continue;
2489
3dafb85c 2490 if (e->recursive_p ())
af961c7f 2491 {
4174a33a
DM
2492 if (dump_enabled_p ())
2493 dump_printf_loc (MSG_MISSED_OPTIMIZATION, e->call_stmt,
2494 "Not inlining: recursive call.\n");
af961c7f
RG
2495 continue;
2496 }
2497
67348ccc
DM
2498 if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
2499 != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
59e0c6b7 2500 {
4174a33a
DM
2501 if (dump_enabled_p ())
2502 dump_printf_loc (MSG_MISSED_OPTIMIZATION, e->call_stmt,
2503 "Not inlining: SSA form does not match.\n");
59e0c6b7
RG
2504 continue;
2505 }
2506
af961c7f
RG
2507 /* Inline the edge and flatten the inline clone. Avoid
2508 recursing through the original node if the node was cloned. */
4174a33a
DM
2509 if (dump_enabled_p ())
2510 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, e->call_stmt,
2511 " Inlining %C into %C.\n",
2512 callee, e->caller);
a5b1779f 2513 orig_callee = callee;
c170d40f 2514 inline_call (e, true, NULL, NULL, false);
af961c7f 2515 if (e->callee != orig_callee)
67348ccc 2516 orig_callee->aux = (void *) node;
f6e809c8 2517 flatten_function (e->callee, early, false);
af961c7f 2518 if (e->callee != orig_callee)
67348ccc 2519 orig_callee->aux = NULL;
af961c7f
RG
2520 }
2521
67348ccc 2522 node->aux = NULL;
7237f93e
JH
2523 cgraph_node *where = node->inlined_to ? node->inlined_to : node;
2524 if (update && opt_for_fn (where->decl, optimize))
2525 ipa_update_overall_fn_summary (where);
af961c7f
RG
2526}
2527
a81b0a3d
JH
2528/* Inline NODE to all callers. Worker for cgraph_for_node_and_aliases.
2529 DATA points to number of calls originally found so we avoid infinite
2530 recursion. */
2531
2532static bool
bddead15
RB
2533inline_to_all_callers_1 (struct cgraph_node *node, void *data,
2534 hash_set<cgraph_node *> *callers)
a81b0a3d
JH
2535{
2536 int *num_calls = (int *)data;
1bbb87c4
JH
2537 bool callee_removed = false;
2538
a62bfab5 2539 while (node->callers && !node->inlined_to)
a81b0a3d
JH
2540 {
2541 struct cgraph_node *caller = node->callers->caller;
2542
1af8bfe5 2543 if (!can_inline_edge_p (node->callers, true)
9a4841a3 2544 || !can_inline_edge_by_limits_p (node->callers, true)
1af8bfe5
JH
2545 || node->callers->recursive_p ())
2546 {
2547 if (dump_file)
2548 fprintf (dump_file, "Uninlinable call found; giving up.\n");
2549 *num_calls = 0;
2550 return false;
2551 }
2552
a81b0a3d
JH
2553 if (dump_file)
2554 {
5e626cd9 2555 cgraph_node *ultimate = node->ultimate_alias_target ();
a81b0a3d
JH
2556 fprintf (dump_file,
2557 "\nInlining %s size %i.\n",
3629ff8a 2558 ultimate->dump_name (),
f658ad30 2559 ipa_size_summaries->get (ultimate)->size);
a81b0a3d
JH
2560 fprintf (dump_file,
2561 " Called once from %s %i insns.\n",
3629ff8a 2562 node->callers->caller->dump_name (),
f658ad30 2563 ipa_size_summaries->get (node->callers->caller)->size);
a81b0a3d
JH
2564 }
2565
bddead15
RB
2566 /* Remember which callers we inlined to, delaying updating the
2567 overall summary. */
2568 callers->add (node->callers->caller);
2569 inline_call (node->callers, true, NULL, NULL, false, &callee_removed);
a81b0a3d
JH
2570 if (dump_file)
2571 fprintf (dump_file,
2572 " Inlined into %s which now has %i size\n",
3629ff8a 2573 caller->dump_name (),
f658ad30 2574 ipa_size_summaries->get (caller)->size);
a81b0a3d
JH
2575 if (!(*num_calls)--)
2576 {
2577 if (dump_file)
2578 fprintf (dump_file, "New calls found; giving up.\n");
1bbb87c4 2579 return callee_removed;
a81b0a3d 2580 }
1bbb87c4
JH
2581 if (callee_removed)
2582 return true;
a81b0a3d
JH
2583 }
2584 return false;
2585}
2586
bddead15
RB
2587/* Wrapper around inline_to_all_callers_1 doing delayed overall summary
2588 update. */
2589
2590static bool
2591inline_to_all_callers (struct cgraph_node *node, void *data)
2592{
2593 hash_set<cgraph_node *> callers;
2594 bool res = inline_to_all_callers_1 (node, data, &callers);
2595 /* Perform the delayed update of the overall summary of all callers
2596 processed. This avoids quadratic behavior in the cases where
2597 we have a lot of calls to the same function. */
2598 for (hash_set<cgraph_node *>::iterator i = callers.begin ();
2599 i != callers.end (); ++i)
7237f93e 2600 ipa_update_overall_fn_summary ((*i)->inlined_to ? (*i)->inlined_to : *i);
bddead15
RB
2601 return res;
2602}
2603
e86a910f
JH
2604/* Output overall time estimate. */
2605static void
2606dump_overall_stats (void)
2607{
ab38481c 2608 sreal sum_weighted = 0, sum = 0;
e86a910f
JH
2609 struct cgraph_node *node;
2610
2611 FOR_EACH_DEFINED_FUNCTION (node)
a62bfab5 2612 if (!node->inlined_to
e86a910f
JH
2613 && !node->alias)
2614 {
56f62793
ML
2615 ipa_fn_summary *s = ipa_fn_summaries->get (node);
2616 if (s != NULL)
2617 {
2618 sum += s->time;
2619 if (node->count.ipa ().initialized_p ())
2620 sum_weighted += s->time * node->count.ipa ().to_gcov_type ();
2621 }
e86a910f
JH
2622 }
2623 fprintf (dump_file, "Overall time estimate: "
ab38481c
JH
2624 "%f weighted by profile: "
2625 "%f\n", sum.to_double (), sum_weighted.to_double ());
e86a910f
JH
2626}
2627
2628/* Output some useful stats about inlining. */
2629
2630static void
2631dump_inline_stats (void)
2632{
a9243bfc
RB
2633 int64_t inlined_cnt = 0, inlined_indir_cnt = 0;
2634 int64_t inlined_virt_cnt = 0, inlined_virt_indir_cnt = 0;
2635 int64_t noninlined_cnt = 0, noninlined_indir_cnt = 0;
2636 int64_t noninlined_virt_cnt = 0, noninlined_virt_indir_cnt = 0;
2637 int64_t inlined_speculative = 0, inlined_speculative_ply = 0;
2638 int64_t indirect_poly_cnt = 0, indirect_cnt = 0;
0009a6c3
JH
2639 int64_t reason[CIF_N_REASONS][2];
2640 sreal reason_freq[CIF_N_REASONS];
e86a910f
JH
2641 int i;
2642 struct cgraph_node *node;
2643
2644 memset (reason, 0, sizeof (reason));
0009a6c3
JH
2645 for (i=0; i < CIF_N_REASONS; i++)
2646 reason_freq[i] = 0;
e86a910f
JH
2647 FOR_EACH_DEFINED_FUNCTION (node)
2648 {
2649 struct cgraph_edge *e;
2650 for (e = node->callees; e; e = e->next_callee)
2651 {
2652 if (e->inline_failed)
2653 {
1bad9c18
JH
2654 if (e->count.ipa ().initialized_p ())
2655 reason[(int) e->inline_failed][0] += e->count.ipa ().to_gcov_type ();
0009a6c3
JH
2656 reason_freq[(int) e->inline_failed] += e->sreal_frequency ();
2657 reason[(int) e->inline_failed][1] ++;
3995f3a2 2658 if (DECL_VIRTUAL_P (e->callee->decl)
1bad9c18 2659 && e->count.ipa ().initialized_p ())
e86a910f
JH
2660 {
2661 if (e->indirect_inlining_edge)
1bad9c18 2662 noninlined_virt_indir_cnt += e->count.ipa ().to_gcov_type ();
e86a910f 2663 else
1bad9c18 2664 noninlined_virt_cnt += e->count.ipa ().to_gcov_type ();
e86a910f 2665 }
1bad9c18 2666 else if (e->count.ipa ().initialized_p ())
e86a910f
JH
2667 {
2668 if (e->indirect_inlining_edge)
1bad9c18 2669 noninlined_indir_cnt += e->count.ipa ().to_gcov_type ();
e86a910f 2670 else
1bad9c18 2671 noninlined_cnt += e->count.ipa ().to_gcov_type ();
e86a910f
JH
2672 }
2673 }
1bad9c18 2674 else if (e->count.ipa ().initialized_p ())
e86a910f
JH
2675 {
2676 if (e->speculative)
2677 {
2678 if (DECL_VIRTUAL_P (e->callee->decl))
1bad9c18 2679 inlined_speculative_ply += e->count.ipa ().to_gcov_type ();
e86a910f 2680 else
1bad9c18 2681 inlined_speculative += e->count.ipa ().to_gcov_type ();
e86a910f
JH
2682 }
2683 else if (DECL_VIRTUAL_P (e->callee->decl))
2684 {
2685 if (e->indirect_inlining_edge)
1bad9c18 2686 inlined_virt_indir_cnt += e->count.ipa ().to_gcov_type ();
e86a910f 2687 else
1bad9c18 2688 inlined_virt_cnt += e->count.ipa ().to_gcov_type ();
e86a910f
JH
2689 }
2690 else
2691 {
2692 if (e->indirect_inlining_edge)
1bad9c18 2693 inlined_indir_cnt += e->count.ipa ().to_gcov_type ();
e86a910f 2694 else
1bad9c18 2695 inlined_cnt += e->count.ipa ().to_gcov_type ();
e86a910f
JH
2696 }
2697 }
2698 }
2699 for (e = node->indirect_calls; e; e = e->next_callee)
3995f3a2 2700 if (e->indirect_info->polymorphic
1bad9c18
JH
2701 & e->count.ipa ().initialized_p ())
2702 indirect_poly_cnt += e->count.ipa ().to_gcov_type ();
2703 else if (e->count.ipa ().initialized_p ())
2704 indirect_cnt += e->count.ipa ().to_gcov_type ();
e86a910f 2705 }
3995f3a2 2706 if (max_count.initialized_p ())
e86a910f
JH
2707 {
2708 fprintf (dump_file,
16998094
JM
2709 "Inlined %" PRId64 " + speculative "
2710 "%" PRId64 " + speculative polymorphic "
2711 "%" PRId64 " + previously indirect "
2712 "%" PRId64 " + virtual "
2713 "%" PRId64 " + virtual and previously indirect "
2714 "%" PRId64 "\n" "Not inlined "
2715 "%" PRId64 " + previously indirect "
2716 "%" PRId64 " + virtual "
2717 "%" PRId64 " + virtual and previously indirect "
956d615d 2718 "%" PRId64 " + still indirect "
16998094
JM
2719 "%" PRId64 " + still indirect polymorphic "
2720 "%" PRId64 "\n", inlined_cnt,
e86a910f
JH
2721 inlined_speculative, inlined_speculative_ply,
2722 inlined_indir_cnt, inlined_virt_cnt, inlined_virt_indir_cnt,
2723 noninlined_cnt, noninlined_indir_cnt, noninlined_virt_cnt,
2724 noninlined_virt_indir_cnt, indirect_cnt, indirect_poly_cnt);
3995f3a2
JH
2725 fprintf (dump_file, "Removed speculations ");
2726 spec_rem.dump (dump_file);
2727 fprintf (dump_file, "\n");
e86a910f
JH
2728 }
2729 dump_overall_stats ();
2730 fprintf (dump_file, "\nWhy inlining failed?\n");
2731 for (i = 0; i < CIF_N_REASONS; i++)
0009a6c3
JH
2732 if (reason[i][1])
2733 fprintf (dump_file, "%-50s: %8i calls, %8f freq, %" PRId64" count\n",
e86a910f 2734 cgraph_inline_failed_string ((cgraph_inline_failed_t) i),
0009a6c3 2735 (int) reason[i][1], reason_freq[i].to_double (), reason[i][0]);
e86a910f
JH
2736}
2737
d6ea70a0
JJ
2738/* Called when node is removed. */
2739
2740static void
2741flatten_remove_node_hook (struct cgraph_node *node, void *data)
2742{
2743 if (lookup_attribute ("flatten", DECL_ATTRIBUTES (node->decl)) == NULL)
2744 return;
2745
2746 hash_set<struct cgraph_node *> *removed
2747 = (hash_set<struct cgraph_node *> *) data;
2748 removed->add (node);
2749}
2750
ca31b95f
JH
2751/* Decide on the inlining. We do so in the topological order to avoid
2752 expenses on updating data structures. */
2753
c2924966 2754static unsigned int
4c0f7679 2755ipa_inline (void)
ca31b95f
JH
2756{
2757 struct cgraph_node *node;
2758 int nnodes;
b591a8b7 2759 struct cgraph_node **order;
d6ea70a0 2760 int i, j;
09ce3660 2761 int cold;
8a41354f
JH
2762 bool remove_functions = false;
2763
3dafb85c 2764 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
b591a8b7 2765
10a5dd5d 2766 if (dump_file)
0bceb671 2767 ipa_dump_fn_summaries (dump_file);
670cd5c5 2768
af8bca3c 2769 nnodes = ipa_reverse_postorder (order);
e7a74006 2770 spec_rem = profile_count::zero ();
ca31b95f 2771
65c70e6b 2772 FOR_EACH_FUNCTION (node)
7ce7e4d4
JH
2773 {
2774 node->aux = 0;
2775
2776 /* Recompute the default reasons for inlining because they may have
2777 changed during merging. */
2778 if (in_lto_p)
2779 {
2780 for (cgraph_edge *e = node->callees; e; e = e->next_callee)
2781 {
2782 gcc_assert (e->inline_failed);
2783 initialize_inline_failed (e);
2784 }
2785 for (cgraph_edge *e = node->indirect_calls; e; e = e->next_callee)
2786 initialize_inline_failed (e);
2787 }
2788 }
ca31b95f
JH
2789
2790 if (dump_file)
af961c7f 2791 fprintf (dump_file, "\nFlattening functions:\n");
ca31b95f 2792
d6ea70a0
JJ
2793 /* First shrink order array, so that it only contains nodes with
2794 flatten attribute. */
2795 for (i = nnodes - 1, j = i; i >= 0; i--)
2796 {
2797 node = order[i];
2895b172 2798 if (node->definition
f22712bd
JH
2799 /* Do not try to flatten aliases. These may happen for example when
2800 creating local aliases. */
2801 && !node->alias
2895b172
JH
2802 && lookup_attribute ("flatten",
2803 DECL_ATTRIBUTES (node->decl)) != NULL)
d6ea70a0
JJ
2804 order[j--] = order[i];
2805 }
2806
2807 /* After the above loop, order[j + 1] ... order[nnodes - 1] contain
2808 nodes with flatten attribute. If there is more than one such
2809 node, we need to register a node removal hook, as flatten_function
2810 could remove other nodes with flatten attribute. See PR82801. */
2811 struct cgraph_node_hook_list *node_removal_hook_holder = NULL;
2812 hash_set<struct cgraph_node *> *flatten_removed_nodes = NULL;
2813 if (j < nnodes - 2)
2814 {
2815 flatten_removed_nodes = new hash_set<struct cgraph_node *>;
2816 node_removal_hook_holder
2817 = symtab->add_cgraph_removal_hook (&flatten_remove_node_hook,
2818 flatten_removed_nodes);
2819 }
2820
af961c7f
RG
2821 /* In the first pass handle functions to be flattened. Do this with
2822 a priority so none of our later choices will make this impossible. */
d6ea70a0 2823 for (i = nnodes - 1; i > j; i--)
ca31b95f 2824 {
af961c7f 2825 node = order[i];
d6ea70a0
JJ
2826 if (flatten_removed_nodes
2827 && flatten_removed_nodes->contains (node))
2828 continue;
af961c7f 2829
09a2806f 2830 /* Handle nodes to be flattened.
af961c7f
RG
2831 Ideally when processing callees we stop inlining at the
2832 entry of cycles, possibly cloning that entry point and
2833 try to flatten itself turning it into a self-recursive
2834 function. */
d6ea70a0 2835 if (dump_file)
3629ff8a 2836 fprintf (dump_file, "Flattening %s\n", node->dump_name ());
f6e809c8 2837 flatten_function (node, false, true);
d6ea70a0
JJ
2838 }
2839
2840 if (j < nnodes - 2)
2841 {
2842 symtab->remove_cgraph_removal_hook (node_removal_hook_holder);
2843 delete flatten_removed_nodes;
ca31b95f 2844 }
d6ea70a0
JJ
2845 free (order);
2846
e86a910f
JH
2847 if (dump_file)
2848 dump_overall_stats ();
ca31b95f 2849
4c0f7679 2850 inline_small_functions ();
e70670cf 2851
17e0fc92
JH
2852 gcc_assert (symtab->state == IPA_SSA);
2853 symtab->state = IPA_SSA_AFTER_INLINING;
2854 /* Do first after-inlining removal. We want to remove all "stale" extern
2855 inline functions and virtual functions so we really know what is called
2856 once. */
2857 symtab->remove_unreachable_nodes (dump_file);
ca31b95f 2858
100411f8
JH
2859 /* Inline functions with a property that after inlining into all callers the
2860 code size will shrink because the out-of-line copy is eliminated.
2861 We do this regardless on the callee size as long as function growth limits
2862 are met. */
09ce3660
JH
2863 if (dump_file)
2864 fprintf (dump_file,
17e0fc92
JH
2865 "\nDeciding on functions to be inlined into all callers and "
2866 "removing useless speculations:\n");
09ce3660
JH
2867
2868 /* Inlining one function called once has good chance of preventing
2869 inlining other function into the same callee. Ideally we should
2870 work in priority order, but probably inlining hot functions first
2871 is good cut without the extra pain of maintaining the queue.
2872
2873 ??? this is not really fitting the bill perfectly: inlining function
2874 into callee often leads to better optimization of callee due to
2875 increased context for optimization.
2876 For example if main() function calls a function that outputs help
956d615d 2877 and then function that does the main optimization, we should inline
09ce3660
JH
2878 the second with priority even if both calls are cold by themselves.
2879
2880 We probably want to implement new predicate replacing our use of
2881 maybe_hot_edge interpreted as maybe_hot_edge || callee is known
2882 to be hot. */
2883 for (cold = 0; cold <= 1; cold ++)
355866de 2884 {
09ce3660 2885 FOR_EACH_DEFINED_FUNCTION (node)
ca31b95f 2886 {
09ce3660
JH
2887 struct cgraph_edge *edge, *next;
2888 bool update=false;
2889
29f1e2b1
JH
2890 if (!opt_for_fn (node->decl, optimize)
2891 || !opt_for_fn (node->decl, flag_inline_functions_called_once))
2892 continue;
2893
09ce3660 2894 for (edge = node->callees; edge; edge = next)
ca31b95f 2895 {
09ce3660
JH
2896 next = edge->next_callee;
2897 if (edge->speculative && !speculation_useful_p (edge, false))
e3c7b49c 2898 {
1bad9c18
JH
2899 if (edge->count.ipa ().initialized_p ())
2900 spec_rem += edge->count.ipa ();
27c5a177 2901 cgraph_edge::resolve_speculation (edge);
09ce3660 2902 update = true;
8a41354f 2903 remove_functions = true;
09ce3660
JH
2904 }
2905 }
2906 if (update)
2907 {
a62bfab5
ML
2908 struct cgraph_node *where = node->inlined_to
2909 ? node->inlined_to : node;
09ce3660 2910 reset_edge_caches (where);
0bceb671 2911 ipa_update_overall_fn_summary (where);
09ce3660 2912 }
2bf86c84 2913 if (want_inline_function_to_all_callers_p (node, cold))
09ce3660
JH
2914 {
2915 int num_calls = 0;
1ede94c5
JH
2916 node->call_for_symbol_and_aliases (sum_callers, &num_calls,
2917 true);
2918 while (node->call_for_symbol_and_aliases
17e0fc92 2919 (inline_to_all_callers, &num_calls, true))
1bbb87c4 2920 ;
f019b607 2921 remove_functions = true;
ca31b95f
JH
2922 }
2923 }
2924 }
2925
4174a33a
DM
2926 if (dump_enabled_p ())
2927 dump_printf (MSG_NOTE,
2928 "\nInlined %i calls, eliminated %i functions\n\n",
2929 ncalls_inlined, nfunctions_inlined);
ca31b95f 2930 if (dump_file)
4174a33a 2931 dump_inline_stats ();
09a2806f 2932
898b8927 2933 if (dump_file)
0bceb671 2934 ipa_dump_fn_summaries (dump_file);
8a41354f 2935 return remove_functions ? TODO_remove_functions : 0;
ca31b95f
JH
2936}
2937
d88fd2e1
JH
2938/* Inline always-inline function calls in NODE
2939 (which itself is possibly inline). */
275b4baa
RG
2940
2941static bool
4c0f7679 2942inline_always_inline_functions (struct cgraph_node *node)
275b4baa
RG
2943{
2944 struct cgraph_edge *e;
2945 bool inlined = false;
2946
2947 for (e = node->callees; e; e = e->next_callee)
2948 {
d52f5295 2949 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
d88fd2e1
JH
2950 gcc_checking_assert (!callee->aux || callee->aux == (void *)(size_t)1);
2951 if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl)
2952 /* Watch for self-recursive cycles. */
2953 || callee->aux)
275b4baa
RG
2954 continue;
2955
3dafb85c 2956 if (e->recursive_p ())
275b4baa 2957 {
4174a33a
DM
2958 if (dump_enabled_p ())
2959 dump_printf_loc (MSG_MISSED_OPTIMIZATION, e->call_stmt,
2960 " Not inlining recursive call to %C.\n",
2961 e->callee);
275b4baa
RG
2962 e->inline_failed = CIF_RECURSIVE_INLINING;
2963 continue;
2964 }
d88fd2e1
JH
2965 if (callee->definition
2966 && !ipa_fn_summaries->get (callee))
2967 compute_fn_summary (callee, true);
275b4baa 2968
4c0f7679 2969 if (!can_early_inline_edge_p (e))
bef8491a
ST
2970 {
2971 /* Set inlined to true if the callee is marked "always_inline" but
2972 is not inlinable. This will allow flagging an error later in
e53b6e56 2973 expand_call_inline in tree-inline.cc. */
bef8491a 2974 if (lookup_attribute ("always_inline",
67348ccc 2975 DECL_ATTRIBUTES (callee->decl)) != NULL)
bef8491a
ST
2976 inlined = true;
2977 continue;
2978 }
275b4baa 2979
4174a33a
DM
2980 if (dump_enabled_p ())
2981 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, e->call_stmt,
2982 " Inlining %C into %C (always_inline).\n",
2983 e->callee, e->caller);
c170d40f 2984 inline_call (e, true, NULL, NULL, false);
d88fd2e1
JH
2985 callee->aux = (void *)(size_t)1;
2986 /* Inline recursively to handle the case where always_inline function was
2987 not optimized yet since it is a part of a cycle in callgraph. */
2988 inline_always_inline_functions (e->callee);
2989 callee->aux = NULL;
275b4baa
RG
2990 inlined = true;
2991 }
275b4baa
RG
2992 return inlined;
2993}
2994
ca31b95f 2995/* Decide on the inlining. We do so in the topological order to avoid
af961c7f 2996 expenses on updating data structures. */
ca31b95f 2997
7fa49e7b 2998static bool
4c0f7679 2999early_inline_small_functions (struct cgraph_node *node)
ca31b95f
JH
3000{
3001 struct cgraph_edge *e;
d63db217 3002 bool inlined = false;
7fa49e7b 3003
275b4baa 3004 for (e = node->callees; e; e = e->next_callee)
c3056c2d 3005 {
d52f5295 3006 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
56f62793 3007
956d615d 3008 /* We can encounter not-yet-analyzed function during
56f62793
ML
3009 early inlining on callgraphs with strongly
3010 connected components. */
3011 ipa_fn_summary *s = ipa_fn_summaries->get (callee);
3012 if (s == NULL || !s->inlinable || !e->inline_failed)
275b4baa
RG
3013 continue;
3014
3015 /* Do not consider functions not declared inline. */
67348ccc 3016 if (!DECL_DECLARED_INLINE_P (callee->decl)
2bf86c84
JH
3017 && !opt_for_fn (node->decl, flag_inline_small_functions)
3018 && !opt_for_fn (node->decl, flag_inline_functions))
275b4baa
RG
3019 continue;
3020
4174a33a
DM
3021 if (dump_enabled_p ())
3022 dump_printf_loc (MSG_NOTE, e->call_stmt,
3023 "Considering inline candidate %C.\n",
3024 callee);
ca31b95f 3025
4c0f7679
JH
3026 if (!can_early_inline_edge_p (e))
3027 continue;
3028
3dafb85c 3029 if (e->recursive_p ())
275b4baa 3030 {
4174a33a
DM
3031 if (dump_enabled_p ())
3032 dump_printf_loc (MSG_MISSED_OPTIMIZATION, e->call_stmt,
3033 " Not inlining: recursive call.\n");
22ad64b6 3034 continue;
275b4baa 3035 }
af961c7f 3036
4c0f7679 3037 if (!want_early_inline_function_p (e))
275b4baa 3038 continue;
ca31b95f 3039
4174a33a
DM
3040 if (dump_enabled_p ())
3041 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, e->call_stmt,
3042 " Inlining %C into %C.\n",
3043 callee, e->caller);
bddead15 3044 inline_call (e, true, NULL, NULL, false);
4c0f7679 3045 inlined = true;
38bc76da 3046 }
275b4baa 3047
bddead15 3048 if (inlined)
0bceb671 3049 ipa_update_overall_fn_summary (node);
bddead15 3050
7fa49e7b 3051 return inlined;
ca31b95f
JH
3052}
3053
be55bfe6 3054unsigned int
be3c16c4 3055early_inliner (function *fun)
d63db217 3056{
d52f5295 3057 struct cgraph_node *node = cgraph_node::get (current_function_decl);
10a5dd5d 3058 struct cgraph_edge *edge;
7fa49e7b 3059 unsigned int todo = 0;
796bda22 3060 int iterations = 0;
275b4baa 3061 bool inlined = false;
d63db217 3062
1da2ed5f 3063 if (seen_error ())
c2924966 3064 return 0;
af961c7f 3065
ecb62563
JH
3066 /* Do nothing if datastructures for ipa-inliner are already computed. This
3067 happens when some pass decides to construct new function and
3068 cgraph_add_new_function calls lowering passes and early optimization on
3069 it. This may confuse ourself when early inliner decide to inline call to
3070 function clone, because function clones don't have parameter list in
3071 ipa-prop matching their signature. */
dd912cb8 3072 if (ipa_node_params_sum)
ecb62563
JH
3073 return 0;
3074
b2b29377
MM
3075 if (flag_checking)
3076 node->verify ();
d122681a 3077 node->remove_all_references ();
275b4baa
RG
3078
3079 /* Even when not optimizing or not inlining inline always-inline
3080 functions. */
4c0f7679 3081 inlined = inline_always_inline_functions (node);
275b4baa 3082
af961c7f
RG
3083 if (!optimize
3084 || flag_no_inline
d88fd2e1 3085 || !flag_early_inlining)
275b4baa
RG
3086 ;
3087 else if (lookup_attribute ("flatten",
67348ccc 3088 DECL_ATTRIBUTES (node->decl)) != NULL)
7fa49e7b 3089 {
275b4baa
RG
3090 /* When the function is marked to be flattened, recursively inline
3091 all calls in it. */
4174a33a
DM
3092 if (dump_enabled_p ())
3093 dump_printf (MSG_OPTIMIZED_LOCATIONS,
3094 "Flattening %C\n", node);
f6e809c8 3095 flatten_function (node, true, true);
275b4baa 3096 inlined = true;
7fa49e7b 3097 }
af961c7f
RG
3098 else
3099 {
d67bce7c
JH
3100 /* If some always_inline functions was inlined, apply the changes.
3101 This way we will not account always inline into growth limits and
3102 moreover we will inline calls from always inlines that we skipped
d88fd2e1
JH
3103 previously because of conditional in can_early_inline_edge_p
3104 which prevents some inlining to always_inline. */
d67bce7c
JH
3105 if (inlined)
3106 {
3107 timevar_push (TV_INTEGRATION);
3108 todo |= optimize_inline_calls (current_function_decl);
1cf06f1e
MP
3109 /* optimize_inline_calls call above might have introduced new
3110 statements that don't have inline parameters computed. */
3111 for (edge = node->callees; edge; edge = edge->next_callee)
3112 {
56f62793
ML
3113 /* We can enounter not-yet-analyzed function during
3114 early inlining on callgraphs with strongly
3115 connected components. */
99353fcf 3116 ipa_call_summary *es = ipa_call_summaries->get_create (edge);
263e19c7
JH
3117 es->call_stmt_size
3118 = estimate_num_insns (edge->call_stmt, &eni_size_weights);
3119 es->call_stmt_time
3120 = estimate_num_insns (edge->call_stmt, &eni_time_weights);
1cf06f1e 3121 }
0bceb671 3122 ipa_update_overall_fn_summary (node);
d67bce7c
JH
3123 inlined = false;
3124 timevar_pop (TV_INTEGRATION);
3125 }
af961c7f
RG
3126 /* We iterate incremental inlining to get trivial cases of indirect
3127 inlining. */
fdfd7f53
ML
3128 while (iterations < opt_for_fn (node->decl,
3129 param_early_inliner_max_iterations)
4c0f7679 3130 && early_inline_small_functions (node))
af961c7f
RG
3131 {
3132 timevar_push (TV_INTEGRATION);
3133 todo |= optimize_inline_calls (current_function_decl);
4c0f7679
JH
3134
3135 /* Technically we ought to recompute inline parameters so the new
3136 iteration of early inliner works as expected. We however have
3137 values approximately right and thus we only need to update edge
3138 info that might be cleared out for newly discovered edges. */
3139 for (edge = node->callees; edge; edge = edge->next_callee)
3140 {
d5e254e1 3141 /* We have no summary for new bound store calls yet. */
b412559e
ML
3142 ipa_call_summary *es = ipa_call_summaries->get_create (edge);
3143 es->call_stmt_size
3144 = estimate_num_insns (edge->call_stmt, &eni_size_weights);
3145 es->call_stmt_time
3146 = estimate_num_insns (edge->call_stmt, &eni_time_weights);
4c0f7679 3147 }
fdfd7f53
ML
3148 if (iterations < opt_for_fn (node->decl,
3149 param_early_inliner_max_iterations) - 1)
0bceb671 3150 ipa_update_overall_fn_summary (node);
af961c7f 3151 timevar_pop (TV_INTEGRATION);
275b4baa
RG
3152 iterations++;
3153 inlined = false;
af961c7f
RG
3154 }
3155 if (dump_file)
3156 fprintf (dump_file, "Iterations: %i\n", iterations);
3157 }
3158
275b4baa
RG
3159 if (inlined)
3160 {
3161 timevar_push (TV_INTEGRATION);
3162 todo |= optimize_inline_calls (current_function_decl);
3163 timevar_pop (TV_INTEGRATION);
3164 }
3165
be55bfe6 3166 fun->always_inline_functions_inlined = true;
d63db217 3167
af961c7f 3168 return todo;
d63db217
JH
3169}
3170
be3c16c4
DC
3171/* Do inlining of small functions. Doing so early helps profiling and other
3172 passes to be somewhat more effective and avoids some code duplication in
3173 later real inlining pass for testcases with very many function calls. */
3174
3175namespace {
3176
3177const pass_data pass_data_early_inline =
3178{
3179 GIMPLE_PASS, /* type */
3180 "einline", /* name */
3181 OPTGROUP_INLINE, /* optinfo_flags */
3182 TV_EARLY_INLINING, /* tv_id */
3183 PROP_ssa, /* properties_required */
3184 0, /* properties_provided */
3185 0, /* properties_destroyed */
3186 0, /* todo_flags_start */
3187 0, /* todo_flags_finish */
3188};
3189
3190class pass_early_inline : public gimple_opt_pass
3191{
3192public:
3193 pass_early_inline (gcc::context *ctxt)
3194 : gimple_opt_pass (pass_data_early_inline, ctxt)
3195 {}
3196
3197 /* opt_pass methods: */
725793af 3198 unsigned int execute (function *) final override;
be3c16c4
DC
3199
3200}; // class pass_early_inline
3201
3202unsigned int
3203pass_early_inline::execute (function *fun)
3204{
3205 return early_inliner (fun);
3206}
3207
27a4cd48
DM
3208} // anon namespace
3209
3210gimple_opt_pass *
3211make_pass_early_inline (gcc::context *ctxt)
3212{
3213 return new pass_early_inline (ctxt);
3214}
3215
27a4cd48
DM
3216namespace {
3217
3218const pass_data pass_data_ipa_inline =
873aa8f5 3219{
27a4cd48
DM
3220 IPA_PASS, /* type */
3221 "inline", /* name */
3222 OPTGROUP_INLINE, /* optinfo_flags */
27a4cd48
DM
3223 TV_IPA_INLINING, /* tv_id */
3224 0, /* properties_required */
3225 0, /* properties_provided */
3226 0, /* properties_destroyed */
8605403e 3227 0, /* todo_flags_start */
8a41354f 3228 ( TODO_dump_symtab ), /* todo_flags_finish */
ca31b95f 3229};
27a4cd48
DM
3230
3231class pass_ipa_inline : public ipa_opt_pass_d
3232{
3233public:
c3284718
RS
3234 pass_ipa_inline (gcc::context *ctxt)
3235 : ipa_opt_pass_d (pass_data_ipa_inline, ctxt,
d2db2e6b
JH
3236 NULL, /* generate_summary */
3237 NULL, /* write_summary */
3238 NULL, /* read_summary */
c3284718
RS
3239 NULL, /* write_optimization_summary */
3240 NULL, /* read_optimization_summary */
3241 NULL, /* stmt_fixup */
3242 0, /* function_transform_todo_flags_start */
3243 inline_transform, /* function_transform */
3244 NULL) /* variable_transform */
27a4cd48
DM
3245 {}
3246
3247 /* opt_pass methods: */
725793af 3248 unsigned int execute (function *) final override { return ipa_inline (); }
27a4cd48
DM
3249
3250}; // class pass_ipa_inline
3251
3252} // anon namespace
3253
3254ipa_opt_pass_d *
3255make_pass_ipa_inline (gcc::context *ctxt)
3256{
3257 return new pass_ipa_inline (ctxt);
3258}