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