vsx.md (vsx_xxmrghw_<mode>): Adjust for little-endian.
[gcc.git] / gcc / ipa-inline.c
1 /* Inlining decision heuristics.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
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
9 Software Foundation; either version 3, or (at your option) any later
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
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 /* Inlining decision heuristics
22
23 The implementation of inliner is organized as follows:
24
25 inlining heuristics limits
26
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.
35
36 inlining heuristics
37
38 The inliner itself is split into two passes:
39
40 pass_early_inlining
41
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.
45
46 The strength of the pass is that it is run in topological order
47 (reverse postorder) on the callgraph. Functions are converted into SSA
48 form just before this pass and optimized subsequently. As a result, the
49 callees of the function seen by the early inliner was already optimized
50 and results of early inlining adds a lot of optimization opportunities
51 for the local optimization.
52
53 The pass handle the obvious inlining decisions within the compilation
54 unit - inlining auto inline functions, inlining for size and
55 flattening.
56
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.
61
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
65 EARLY_INLINING_INSNS when callee is leaf function. In this case the
66 optimizations performed later are very likely to eliminate the cost.
67
68 pass_ipa_inline
69
70 This is the real inliner able to handle inlining with whole program
71 knowledge. It performs following steps:
72
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.
76
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.
81
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. */
91
92 #include "config.h"
93 #include "system.h"
94 #include "coretypes.h"
95 #include "tm.h"
96 #include "tree.h"
97 #include "trans-mem.h"
98 #include "calls.h"
99 #include "tree-inline.h"
100 #include "langhooks.h"
101 #include "flags.h"
102 #include "diagnostic.h"
103 #include "gimple-pretty-print.h"
104 #include "params.h"
105 #include "fibheap.h"
106 #include "intl.h"
107 #include "tree-pass.h"
108 #include "coverage.h"
109 #include "rtl.h"
110 #include "bitmap.h"
111 #include "basic-block.h"
112 #include "tree-ssa-alias.h"
113 #include "internal-fn.h"
114 #include "gimple-expr.h"
115 #include "is-a.h"
116 #include "gimple.h"
117 #include "gimple-ssa.h"
118 #include "ipa-prop.h"
119 #include "except.h"
120 #include "target.h"
121 #include "ipa-inline.h"
122 #include "ipa-utils.h"
123 #include "sreal.h"
124 #include "cilk.h"
125
126 /* Statistics we collect about inlining algorithm. */
127 static int overall_size;
128 static gcov_type max_count;
129 static sreal max_count_real, max_relbenefit_real, half_int_min_real;
130
131 /* Return false when inlining edge E would lead to violating
132 limits on function unit growth or stack usage growth.
133
134 The relative function body growth limit is present generally
135 to avoid problems with non-linear behavior of the compiler.
136 To allow inlining huge functions into tiny wrapper, the limit
137 is always based on the bigger of the two functions considered.
138
139 For stack growth limits we always base the growth in stack usage
140 of the callers. We want to prevent applications from segfaulting
141 on stack overflow when functions with huge stack frames gets
142 inlined. */
143
144 static bool
145 caller_growth_limits (struct cgraph_edge *e)
146 {
147 struct cgraph_node *to = e->caller;
148 struct cgraph_node *what = cgraph_function_or_thunk_node (e->callee, NULL);
149 int newsize;
150 int limit = 0;
151 HOST_WIDE_INT stack_size_limit = 0, inlined_stack;
152 struct inline_summary *info, *what_info, *outer_info = inline_summary (to);
153
154 /* Look for function e->caller is inlined to. While doing
155 so work out the largest function body on the way. As
156 described above, we want to base our function growth
157 limits based on that. Not on the self size of the
158 outer function, not on the self size of inline code
159 we immediately inline to. This is the most relaxed
160 interpretation of the rule "do not grow large functions
161 too much in order to prevent compiler from exploding". */
162 while (true)
163 {
164 info = inline_summary (to);
165 if (limit < info->self_size)
166 limit = info->self_size;
167 if (stack_size_limit < info->estimated_self_stack_size)
168 stack_size_limit = info->estimated_self_stack_size;
169 if (to->global.inlined_to)
170 to = to->callers->caller;
171 else
172 break;
173 }
174
175 what_info = inline_summary (what);
176
177 if (limit < what_info->self_size)
178 limit = what_info->self_size;
179
180 limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
181
182 /* Check the size after inlining against the function limits. But allow
183 the function to shrink if it went over the limits by forced inlining. */
184 newsize = estimate_size_after_inlining (to, e);
185 if (newsize >= info->size
186 && newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
187 && newsize > limit)
188 {
189 e->inline_failed = CIF_LARGE_FUNCTION_GROWTH_LIMIT;
190 return false;
191 }
192
193 if (!what_info->estimated_stack_size)
194 return true;
195
196 /* FIXME: Stack size limit often prevents inlining in Fortran programs
197 due to large i/o datastructures used by the Fortran front-end.
198 We ought to ignore this limit when we know that the edge is executed
199 on every invocation of the caller (i.e. its call statement dominates
200 exit block). We do not track this information, yet. */
201 stack_size_limit += ((gcov_type)stack_size_limit
202 * PARAM_VALUE (PARAM_STACK_FRAME_GROWTH) / 100);
203
204 inlined_stack = (outer_info->stack_frame_offset
205 + outer_info->estimated_self_stack_size
206 + what_info->estimated_stack_size);
207 /* Check new stack consumption with stack consumption at the place
208 stack is used. */
209 if (inlined_stack > stack_size_limit
210 /* If function already has large stack usage from sibling
211 inline call, we can inline, too.
212 This bit overoptimistically assume that we are good at stack
213 packing. */
214 && inlined_stack > info->estimated_stack_size
215 && inlined_stack > PARAM_VALUE (PARAM_LARGE_STACK_FRAME))
216 {
217 e->inline_failed = CIF_LARGE_STACK_FRAME_GROWTH_LIMIT;
218 return false;
219 }
220 return true;
221 }
222
223 /* Dump info about why inlining has failed. */
224
225 static void
226 report_inline_failed_reason (struct cgraph_edge *e)
227 {
228 if (dump_file)
229 {
230 fprintf (dump_file, " not inlinable: %s/%i -> %s/%i, %s\n",
231 xstrdup (e->caller->name ()), e->caller->order,
232 xstrdup (e->callee->name ()), e->callee->order,
233 cgraph_inline_failed_string (e->inline_failed));
234 }
235 }
236
237 /* Decide whether sanitizer-related attributes allow inlining. */
238
239 static bool
240 sanitize_attrs_match_for_inline_p (const_tree caller, const_tree callee)
241 {
242 /* Don't care if sanitizer is disabled */
243 if (!(flag_sanitize & SANITIZE_ADDRESS))
244 return true;
245
246 if (!caller || !callee)
247 return true;
248
249 return !!lookup_attribute ("no_sanitize_address",
250 DECL_ATTRIBUTES (caller)) ==
251 !!lookup_attribute ("no_sanitize_address",
252 DECL_ATTRIBUTES (callee));
253 }
254
255 /* Decide if we can inline the edge and possibly update
256 inline_failed reason.
257 We check whether inlining is possible at all and whether
258 caller growth limits allow doing so.
259
260 if REPORT is true, output reason to the dump file.
261
262 if DISREGARD_LIMITS is true, ignore size limits.*/
263
264 static bool
265 can_inline_edge_p (struct cgraph_edge *e, bool report,
266 bool disregard_limits = false)
267 {
268 bool inlinable = true;
269 enum availability avail;
270 struct cgraph_node *callee
271 = cgraph_function_or_thunk_node (e->callee, &avail);
272 tree caller_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (e->caller->decl);
273 tree callee_tree
274 = callee ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (callee->decl) : NULL;
275 struct function *caller_cfun = DECL_STRUCT_FUNCTION (e->caller->decl);
276 struct function *callee_cfun
277 = callee ? DECL_STRUCT_FUNCTION (callee->decl) : NULL;
278
279 if (!caller_cfun && e->caller->clone_of)
280 caller_cfun = DECL_STRUCT_FUNCTION (e->caller->clone_of->decl);
281
282 if (!callee_cfun && callee && callee->clone_of)
283 callee_cfun = DECL_STRUCT_FUNCTION (callee->clone_of->decl);
284
285 gcc_assert (e->inline_failed);
286
287 if (!callee || !callee->definition)
288 {
289 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
290 inlinable = false;
291 }
292 else if (callee->calls_comdat_local)
293 {
294 e->inline_failed = CIF_USES_COMDAT_LOCAL;
295 inlinable = false;
296 }
297 else if (!inline_summary (callee)->inlinable
298 || (caller_cfun && fn_contains_cilk_spawn_p (caller_cfun)))
299 {
300 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
301 inlinable = false;
302 }
303 else if (avail <= AVAIL_OVERWRITABLE)
304 {
305 e->inline_failed = CIF_OVERWRITABLE;
306 inlinable = false;
307 }
308 else if (e->call_stmt_cannot_inline_p)
309 {
310 if (e->inline_failed != CIF_FUNCTION_NOT_OPTIMIZED)
311 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
312 inlinable = false;
313 }
314 /* Don't inline if the functions have different EH personalities. */
315 else if (DECL_FUNCTION_PERSONALITY (e->caller->decl)
316 && DECL_FUNCTION_PERSONALITY (callee->decl)
317 && (DECL_FUNCTION_PERSONALITY (e->caller->decl)
318 != DECL_FUNCTION_PERSONALITY (callee->decl)))
319 {
320 e->inline_failed = CIF_EH_PERSONALITY;
321 inlinable = false;
322 }
323 /* TM pure functions should not be inlined into non-TM_pure
324 functions. */
325 else if (is_tm_pure (callee->decl)
326 && !is_tm_pure (e->caller->decl))
327 {
328 e->inline_failed = CIF_UNSPECIFIED;
329 inlinable = false;
330 }
331 /* Don't inline if the callee can throw non-call exceptions but the
332 caller cannot.
333 FIXME: this is obviously wrong for LTO where STRUCT_FUNCTION is missing.
334 Move the flag into cgraph node or mirror it in the inline summary. */
335 else if (callee_cfun && callee_cfun->can_throw_non_call_exceptions
336 && !(caller_cfun && caller_cfun->can_throw_non_call_exceptions))
337 {
338 e->inline_failed = CIF_NON_CALL_EXCEPTIONS;
339 inlinable = false;
340 }
341 /* Check compatibility of target optimization options. */
342 else if (!targetm.target_option.can_inline_p (e->caller->decl,
343 callee->decl))
344 {
345 e->inline_failed = CIF_TARGET_OPTION_MISMATCH;
346 inlinable = false;
347 }
348 /* Don't inline a function with mismatched sanitization attributes. */
349 else if (!sanitize_attrs_match_for_inline_p (e->caller->decl, callee->decl))
350 {
351 e->inline_failed = CIF_ATTRIBUTE_MISMATCH;
352 inlinable = false;
353 }
354 /* Check if caller growth allows the inlining. */
355 else if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl)
356 && !disregard_limits
357 && !lookup_attribute ("flatten",
358 DECL_ATTRIBUTES
359 (e->caller->global.inlined_to
360 ? e->caller->global.inlined_to->decl
361 : e->caller->decl))
362 && !caller_growth_limits (e))
363 inlinable = false;
364 /* Don't inline a function with a higher optimization level than the
365 caller. FIXME: this is really just tip of iceberg of handling
366 optimization attribute. */
367 else if (caller_tree != callee_tree)
368 {
369 struct cl_optimization *caller_opt
370 = TREE_OPTIMIZATION ((caller_tree)
371 ? caller_tree
372 : optimization_default_node);
373
374 struct cl_optimization *callee_opt
375 = TREE_OPTIMIZATION ((callee_tree)
376 ? callee_tree
377 : optimization_default_node);
378
379 if (((caller_opt->x_optimize > callee_opt->x_optimize)
380 || (caller_opt->x_optimize_size != callee_opt->x_optimize_size))
381 /* gcc.dg/pr43564.c. Look at forced inline even in -O0. */
382 && !DECL_DISREGARD_INLINE_LIMITS (e->callee->decl))
383 {
384 e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
385 inlinable = false;
386 }
387 }
388
389 if (!inlinable && report)
390 report_inline_failed_reason (e);
391 return inlinable;
392 }
393
394
395 /* Return true if the edge E is inlinable during early inlining. */
396
397 static bool
398 can_early_inline_edge_p (struct cgraph_edge *e)
399 {
400 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee,
401 NULL);
402 /* Early inliner might get called at WPA stage when IPA pass adds new
403 function. In this case we can not really do any of early inlining
404 because function bodies are missing. */
405 if (!gimple_has_body_p (callee->decl))
406 {
407 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
408 return false;
409 }
410 /* In early inliner some of callees may not be in SSA form yet
411 (i.e. the callgraph is cyclic and we did not process
412 the callee by early inliner, yet). We don't have CIF code for this
413 case; later we will re-do the decision in the real inliner. */
414 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->caller->decl))
415 || !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
416 {
417 if (dump_file)
418 fprintf (dump_file, " edge not inlinable: not in SSA form\n");
419 return false;
420 }
421 if (!can_inline_edge_p (e, true))
422 return false;
423 return true;
424 }
425
426
427 /* Return number of calls in N. Ignore cheap builtins. */
428
429 static int
430 num_calls (struct cgraph_node *n)
431 {
432 struct cgraph_edge *e;
433 int num = 0;
434
435 for (e = n->callees; e; e = e->next_callee)
436 if (!is_inexpensive_builtin (e->callee->decl))
437 num++;
438 return num;
439 }
440
441
442 /* Return true if we are interested in inlining small function. */
443
444 static bool
445 want_early_inline_function_p (struct cgraph_edge *e)
446 {
447 bool want_inline = true;
448 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
449
450 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
451 ;
452 else if (!DECL_DECLARED_INLINE_P (callee->decl)
453 && !flag_inline_small_functions)
454 {
455 e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
456 report_inline_failed_reason (e);
457 want_inline = false;
458 }
459 else
460 {
461 int growth = estimate_edge_growth (e);
462 int n;
463
464 if (growth <= 0)
465 ;
466 else if (!cgraph_maybe_hot_edge_p (e)
467 && growth > 0)
468 {
469 if (dump_file)
470 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
471 "call is cold and code would grow by %i\n",
472 xstrdup (e->caller->name ()),
473 e->caller->order,
474 xstrdup (callee->name ()), callee->order,
475 growth);
476 want_inline = false;
477 }
478 else if (growth > PARAM_VALUE (PARAM_EARLY_INLINING_INSNS))
479 {
480 if (dump_file)
481 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
482 "growth %i exceeds --param early-inlining-insns\n",
483 xstrdup (e->caller->name ()),
484 e->caller->order,
485 xstrdup (callee->name ()), callee->order,
486 growth);
487 want_inline = false;
488 }
489 else if ((n = num_calls (callee)) != 0
490 && growth * (n + 1) > PARAM_VALUE (PARAM_EARLY_INLINING_INSNS))
491 {
492 if (dump_file)
493 fprintf (dump_file, " will not early inline: %s/%i->%s/%i, "
494 "growth %i exceeds --param early-inlining-insns "
495 "divided by number of calls\n",
496 xstrdup (e->caller->name ()),
497 e->caller->order,
498 xstrdup (callee->name ()), callee->order,
499 growth);
500 want_inline = false;
501 }
502 }
503 return want_inline;
504 }
505
506 /* Compute time of the edge->caller + edge->callee execution when inlining
507 does not happen. */
508
509 inline gcov_type
510 compute_uninlined_call_time (struct inline_summary *callee_info,
511 struct cgraph_edge *edge)
512 {
513 gcov_type uninlined_call_time =
514 RDIV ((gcov_type)callee_info->time * MAX (edge->frequency, 1),
515 CGRAPH_FREQ_BASE);
516 gcov_type caller_time = inline_summary (edge->caller->global.inlined_to
517 ? edge->caller->global.inlined_to
518 : edge->caller)->time;
519 return uninlined_call_time + caller_time;
520 }
521
522 /* Same as compute_uinlined_call_time but compute time when inlining
523 does happen. */
524
525 inline gcov_type
526 compute_inlined_call_time (struct cgraph_edge *edge,
527 int edge_time)
528 {
529 gcov_type caller_time = inline_summary (edge->caller->global.inlined_to
530 ? edge->caller->global.inlined_to
531 : edge->caller)->time;
532 gcov_type time = (caller_time
533 + RDIV (((gcov_type) edge_time
534 - inline_edge_summary (edge)->call_stmt_time)
535 * MAX (edge->frequency, 1), CGRAPH_FREQ_BASE));
536 /* Possible one roundoff error, but watch for overflows. */
537 gcc_checking_assert (time >= INT_MIN / 2);
538 if (time < 0)
539 time = 0;
540 return time;
541 }
542
543 /* Return true if the speedup for inlining E is bigger than
544 PARAM_MAX_INLINE_MIN_SPEEDUP. */
545
546 static bool
547 big_speedup_p (struct cgraph_edge *e)
548 {
549 gcov_type time = compute_uninlined_call_time (inline_summary (e->callee),
550 e);
551 gcov_type inlined_time = compute_inlined_call_time (e,
552 estimate_edge_time (e));
553 if (time - inlined_time
554 > RDIV (time * PARAM_VALUE (PARAM_INLINE_MIN_SPEEDUP), 100))
555 return true;
556 return false;
557 }
558
559 /* Return true if we are interested in inlining small function.
560 When REPORT is true, report reason to dump file. */
561
562 static bool
563 want_inline_small_function_p (struct cgraph_edge *e, bool report)
564 {
565 bool want_inline = true;
566 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
567
568 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
569 ;
570 else if (!DECL_DECLARED_INLINE_P (callee->decl)
571 && !flag_inline_small_functions)
572 {
573 e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
574 want_inline = false;
575 }
576 /* Do fast and conservative check if the function can be good
577 inline cnadidate. At themoment we allow inline hints to
578 promote non-inline function to inline and we increase
579 MAX_INLINE_INSNS_SINGLE 16fold for inline functions. */
580 else if (!DECL_DECLARED_INLINE_P (callee->decl)
581 && inline_summary (callee)->min_size - inline_edge_summary (e)->call_stmt_size
582 > MAX (MAX_INLINE_INSNS_SINGLE, MAX_INLINE_INSNS_AUTO))
583 {
584 e->inline_failed = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
585 want_inline = false;
586 }
587 else if (DECL_DECLARED_INLINE_P (callee->decl)
588 && inline_summary (callee)->min_size - inline_edge_summary (e)->call_stmt_size
589 > 16 * MAX_INLINE_INSNS_SINGLE)
590 {
591 e->inline_failed = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
592 want_inline = false;
593 }
594 else
595 {
596 int growth = estimate_edge_growth (e);
597 inline_hints hints = estimate_edge_hints (e);
598 bool big_speedup = big_speedup_p (e);
599
600 if (growth <= 0)
601 ;
602 /* Apply MAX_INLINE_INSNS_SINGLE limit. Do not do so when
603 hints suggests that inlining given function is very profitable. */
604 else if (DECL_DECLARED_INLINE_P (callee->decl)
605 && growth >= MAX_INLINE_INSNS_SINGLE
606 && ((!big_speedup
607 && !(hints & (INLINE_HINT_indirect_call
608 | INLINE_HINT_loop_iterations
609 | INLINE_HINT_array_index
610 | INLINE_HINT_loop_stride)))
611 || growth >= MAX_INLINE_INSNS_SINGLE * 16))
612 {
613 e->inline_failed = CIF_MAX_INLINE_INSNS_SINGLE_LIMIT;
614 want_inline = false;
615 }
616 else if (!DECL_DECLARED_INLINE_P (callee->decl)
617 && !flag_inline_functions)
618 {
619 /* growth_likely_positive is expensive, always test it last. */
620 if (growth >= MAX_INLINE_INSNS_SINGLE
621 || growth_likely_positive (callee, growth))
622 {
623 e->inline_failed = CIF_NOT_DECLARED_INLINED;
624 want_inline = false;
625 }
626 }
627 /* Apply MAX_INLINE_INSNS_AUTO limit for functions not declared inline
628 Upgrade it to MAX_INLINE_INSNS_SINGLE when hints suggests that
629 inlining given function is very profitable. */
630 else if (!DECL_DECLARED_INLINE_P (callee->decl)
631 && !big_speedup
632 && growth >= ((hints & (INLINE_HINT_indirect_call
633 | INLINE_HINT_loop_iterations
634 | INLINE_HINT_array_index
635 | INLINE_HINT_loop_stride))
636 ? MAX (MAX_INLINE_INSNS_AUTO,
637 MAX_INLINE_INSNS_SINGLE)
638 : MAX_INLINE_INSNS_AUTO))
639 {
640 /* growth_likely_positive is expensive, always test it last. */
641 if (growth >= MAX_INLINE_INSNS_SINGLE
642 || growth_likely_positive (callee, growth))
643 {
644 e->inline_failed = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
645 want_inline = false;
646 }
647 }
648 /* If call is cold, do not inline when function body would grow. */
649 else if (!cgraph_maybe_hot_edge_p (e)
650 && (growth >= MAX_INLINE_INSNS_SINGLE
651 || growth_likely_positive (callee, growth)))
652 {
653 e->inline_failed = CIF_UNLIKELY_CALL;
654 want_inline = false;
655 }
656 }
657 if (!want_inline && report)
658 report_inline_failed_reason (e);
659 return want_inline;
660 }
661
662 /* EDGE is self recursive edge.
663 We hand two cases - when function A is inlining into itself
664 or when function A is being inlined into another inliner copy of function
665 A within function B.
666
667 In first case OUTER_NODE points to the toplevel copy of A, while
668 in the second case OUTER_NODE points to the outermost copy of A in B.
669
670 In both cases we want to be extra selective since
671 inlining the call will just introduce new recursive calls to appear. */
672
673 static bool
674 want_inline_self_recursive_call_p (struct cgraph_edge *edge,
675 struct cgraph_node *outer_node,
676 bool peeling,
677 int depth)
678 {
679 char const *reason = NULL;
680 bool want_inline = true;
681 int caller_freq = CGRAPH_FREQ_BASE;
682 int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO);
683
684 if (DECL_DECLARED_INLINE_P (edge->caller->decl))
685 max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH);
686
687 if (!cgraph_maybe_hot_edge_p (edge))
688 {
689 reason = "recursive call is cold";
690 want_inline = false;
691 }
692 else if (max_count && !outer_node->count)
693 {
694 reason = "not executed in profile";
695 want_inline = false;
696 }
697 else if (depth > max_depth)
698 {
699 reason = "--param max-inline-recursive-depth exceeded.";
700 want_inline = false;
701 }
702
703 if (outer_node->global.inlined_to)
704 caller_freq = outer_node->callers->frequency;
705
706 if (!caller_freq)
707 {
708 reason = "function is inlined and unlikely";
709 want_inline = false;
710 }
711
712 if (!want_inline)
713 ;
714 /* Inlining of self recursive function into copy of itself within other function
715 is transformation similar to loop peeling.
716
717 Peeling is profitable if we can inline enough copies to make probability
718 of actual call to the self recursive function very small. Be sure that
719 the probability of recursion is small.
720
721 We ensure that the frequency of recursing is at most 1 - (1/max_depth).
722 This way the expected number of recision is at most max_depth. */
723 else if (peeling)
724 {
725 int max_prob = CGRAPH_FREQ_BASE - ((CGRAPH_FREQ_BASE + max_depth - 1)
726 / max_depth);
727 int i;
728 for (i = 1; i < depth; i++)
729 max_prob = max_prob * max_prob / CGRAPH_FREQ_BASE;
730 if (max_count
731 && (edge->count * CGRAPH_FREQ_BASE / outer_node->count
732 >= max_prob))
733 {
734 reason = "profile of recursive call is too large";
735 want_inline = false;
736 }
737 if (!max_count
738 && (edge->frequency * CGRAPH_FREQ_BASE / caller_freq
739 >= max_prob))
740 {
741 reason = "frequency of recursive call is too large";
742 want_inline = false;
743 }
744 }
745 /* Recursive inlining, i.e. equivalent of unrolling, is profitable if recursion
746 depth is large. We reduce function call overhead and increase chances that
747 things fit in hardware return predictor.
748
749 Recursive inlining might however increase cost of stack frame setup
750 actually slowing down functions whose recursion tree is wide rather than
751 deep.
752
753 Deciding reliably on when to do recursive inlining without profile feedback
754 is tricky. For now we disable recursive inlining when probability of self
755 recursion is low.
756
757 Recursive inlining of self recursive call within loop also results in large loop
758 depths that generally optimize badly. We may want to throttle down inlining
759 in those cases. In particular this seems to happen in one of libstdc++ rb tree
760 methods. */
761 else
762 {
763 if (max_count
764 && (edge->count * 100 / outer_node->count
765 <= PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY)))
766 {
767 reason = "profile of recursive call is too small";
768 want_inline = false;
769 }
770 else if (!max_count
771 && (edge->frequency * 100 / caller_freq
772 <= PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY)))
773 {
774 reason = "frequency of recursive call is too small";
775 want_inline = false;
776 }
777 }
778 if (!want_inline && dump_file)
779 fprintf (dump_file, " not inlining recursively: %s\n", reason);
780 return want_inline;
781 }
782
783 /* Return true when NODE has uninlinable caller;
784 set HAS_HOT_CALL if it has hot call.
785 Worker for cgraph_for_node_and_aliases. */
786
787 static bool
788 check_callers (struct cgraph_node *node, void *has_hot_call)
789 {
790 struct cgraph_edge *e;
791 for (e = node->callers; e; e = e->next_caller)
792 {
793 if (!can_inline_edge_p (e, true))
794 return true;
795 if (!(*(bool *)has_hot_call) && cgraph_maybe_hot_edge_p (e))
796 *(bool *)has_hot_call = true;
797 }
798 return false;
799 }
800
801 /* If NODE has a caller, return true. */
802
803 static bool
804 has_caller_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
805 {
806 if (node->callers)
807 return true;
808 return false;
809 }
810
811 /* Decide if inlining NODE would reduce unit size by eliminating
812 the offline copy of function.
813 When COLD is true the cold calls are considered, too. */
814
815 static bool
816 want_inline_function_to_all_callers_p (struct cgraph_node *node, bool cold)
817 {
818 struct cgraph_node *function = cgraph_function_or_thunk_node (node, NULL);
819 bool has_hot_call = false;
820
821 /* Does it have callers? */
822 if (!cgraph_for_node_and_aliases (node, has_caller_p, NULL, true))
823 return false;
824 /* Already inlined? */
825 if (function->global.inlined_to)
826 return false;
827 if (cgraph_function_or_thunk_node (node, NULL) != node)
828 return false;
829 /* Inlining into all callers would increase size? */
830 if (estimate_growth (node) > 0)
831 return false;
832 /* All inlines must be possible. */
833 if (cgraph_for_node_and_aliases (node, check_callers, &has_hot_call, true))
834 return false;
835 if (!cold && !has_hot_call)
836 return false;
837 return true;
838 }
839
840 #define RELATIVE_TIME_BENEFIT_RANGE (INT_MAX / 64)
841
842 /* Return relative time improvement for inlining EDGE in range
843 1...RELATIVE_TIME_BENEFIT_RANGE */
844
845 static inline int
846 relative_time_benefit (struct inline_summary *callee_info,
847 struct cgraph_edge *edge,
848 int edge_time)
849 {
850 gcov_type relbenefit;
851 gcov_type uninlined_call_time = compute_uninlined_call_time (callee_info, edge);
852 gcov_type inlined_call_time = compute_inlined_call_time (edge, edge_time);
853
854 /* Inlining into extern inline function is not a win. */
855 if (DECL_EXTERNAL (edge->caller->global.inlined_to
856 ? edge->caller->global.inlined_to->decl
857 : edge->caller->decl))
858 return 1;
859
860 /* Watch overflows. */
861 gcc_checking_assert (uninlined_call_time >= 0);
862 gcc_checking_assert (inlined_call_time >= 0);
863 gcc_checking_assert (uninlined_call_time >= inlined_call_time);
864
865 /* Compute relative time benefit, i.e. how much the call becomes faster.
866 ??? perhaps computing how much the caller+calle together become faster
867 would lead to more realistic results. */
868 if (!uninlined_call_time)
869 uninlined_call_time = 1;
870 relbenefit =
871 RDIV (((gcov_type)uninlined_call_time - inlined_call_time) * RELATIVE_TIME_BENEFIT_RANGE,
872 uninlined_call_time);
873 relbenefit = MIN (relbenefit, RELATIVE_TIME_BENEFIT_RANGE);
874 gcc_checking_assert (relbenefit >= 0);
875 relbenefit = MAX (relbenefit, 1);
876 return relbenefit;
877 }
878
879
880 /* A cost model driving the inlining heuristics in a way so the edges with
881 smallest badness are inlined first. After each inlining is performed
882 the costs of all caller edges of nodes affected are recomputed so the
883 metrics may accurately depend on values such as number of inlinable callers
884 of the function or function body size. */
885
886 static int
887 edge_badness (struct cgraph_edge *edge, bool dump)
888 {
889 gcov_type badness;
890 int growth, edge_time;
891 struct cgraph_node *callee = cgraph_function_or_thunk_node (edge->callee,
892 NULL);
893 struct inline_summary *callee_info = inline_summary (callee);
894 inline_hints hints;
895
896 if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
897 return INT_MIN;
898
899 growth = estimate_edge_growth (edge);
900 edge_time = estimate_edge_time (edge);
901 hints = estimate_edge_hints (edge);
902 gcc_checking_assert (edge_time >= 0);
903 gcc_checking_assert (edge_time <= callee_info->time);
904 gcc_checking_assert (growth <= callee_info->size);
905
906 if (dump)
907 {
908 fprintf (dump_file, " Badness calculation for %s/%i -> %s/%i\n",
909 xstrdup (edge->caller->name ()),
910 edge->caller->order,
911 xstrdup (callee->name ()),
912 edge->callee->order);
913 fprintf (dump_file, " size growth %i, time %i ",
914 growth,
915 edge_time);
916 dump_inline_hints (dump_file, hints);
917 if (big_speedup_p (edge))
918 fprintf (dump_file, " big_speedup");
919 fprintf (dump_file, "\n");
920 }
921
922 /* Always prefer inlining saving code size. */
923 if (growth <= 0)
924 {
925 badness = INT_MIN / 2 + growth;
926 if (dump)
927 fprintf (dump_file, " %i: Growth %i <= 0\n", (int) badness,
928 growth);
929 }
930
931 /* When profiling is available, compute badness as:
932
933 relative_edge_count * relative_time_benefit
934 goodness = -------------------------------------------
935 growth_f_caller
936 badness = -goodness
937
938 The fraction is upside down, because on edge counts and time beneits
939 the bounds are known. Edge growth is essentially unlimited. */
940
941 else if (max_count)
942 {
943 sreal tmp, relbenefit_real, growth_real;
944 int relbenefit = relative_time_benefit (callee_info, edge, edge_time);
945 /* Capping edge->count to max_count. edge->count can be larger than
946 max_count if an inline adds new edges which increase max_count
947 after max_count is computed. */
948 gcov_type edge_count = edge->count > max_count ? max_count : edge->count;
949
950 sreal_init (&relbenefit_real, relbenefit, 0);
951 sreal_init (&growth_real, growth, 0);
952
953 /* relative_edge_count. */
954 sreal_init (&tmp, edge_count, 0);
955 sreal_div (&tmp, &tmp, &max_count_real);
956
957 /* relative_time_benefit. */
958 sreal_mul (&tmp, &tmp, &relbenefit_real);
959 sreal_div (&tmp, &tmp, &max_relbenefit_real);
960
961 /* growth_f_caller. */
962 sreal_mul (&tmp, &tmp, &half_int_min_real);
963 sreal_div (&tmp, &tmp, &growth_real);
964
965 badness = -1 * sreal_to_int (&tmp);
966
967 if (dump)
968 {
969 fprintf (dump_file,
970 " %i (relative %f): profile info. Relative count %f%s"
971 " * Relative benefit %f\n",
972 (int) badness, (double) badness / INT_MIN,
973 (double) edge_count / max_count,
974 edge->count > max_count ? " (capped to max_count)" : "",
975 relbenefit * 100.0 / RELATIVE_TIME_BENEFIT_RANGE);
976 }
977 }
978
979 /* When function local profile is available. Compute badness as:
980
981 relative_time_benefit
982 goodness = ---------------------------------
983 growth_of_caller * overall_growth
984
985 badness = - goodness
986
987 compensated by the inline hints.
988 */
989 else if (flag_guess_branch_prob)
990 {
991 badness = (relative_time_benefit (callee_info, edge, edge_time)
992 * (INT_MIN / 16 / RELATIVE_TIME_BENEFIT_RANGE));
993 badness /= (MIN (65536/2, growth) * MIN (65536/2, MAX (1, callee_info->growth)));
994 gcc_checking_assert (badness <=0 && badness >= INT_MIN / 16);
995 if ((hints & (INLINE_HINT_indirect_call
996 | INLINE_HINT_loop_iterations
997 | INLINE_HINT_array_index
998 | INLINE_HINT_loop_stride))
999 || callee_info->growth <= 0)
1000 badness *= 8;
1001 if (hints & (INLINE_HINT_same_scc))
1002 badness /= 16;
1003 else if (hints & (INLINE_HINT_in_scc))
1004 badness /= 8;
1005 else if (hints & (INLINE_HINT_cross_module))
1006 badness /= 2;
1007 gcc_checking_assert (badness <= 0 && badness >= INT_MIN / 2);
1008 if ((hints & INLINE_HINT_declared_inline) && badness >= INT_MIN / 32)
1009 badness *= 16;
1010 if (dump)
1011 {
1012 fprintf (dump_file,
1013 " %i: guessed profile. frequency %f,"
1014 " benefit %f%%, time w/o inlining %i, time w inlining %i"
1015 " overall growth %i (current) %i (original)\n",
1016 (int) badness, (double)edge->frequency / CGRAPH_FREQ_BASE,
1017 relative_time_benefit (callee_info, edge, edge_time) * 100.0
1018 / RELATIVE_TIME_BENEFIT_RANGE,
1019 (int)compute_uninlined_call_time (callee_info, edge),
1020 (int)compute_inlined_call_time (edge, edge_time),
1021 estimate_growth (callee),
1022 callee_info->growth);
1023 }
1024 }
1025 /* When function local profile is not available or it does not give
1026 useful information (ie frequency is zero), base the cost on
1027 loop nest and overall size growth, so we optimize for overall number
1028 of functions fully inlined in program. */
1029 else
1030 {
1031 int nest = MIN (inline_edge_summary (edge)->loop_depth, 8);
1032 badness = growth * 256;
1033
1034 /* Decrease badness if call is nested. */
1035 if (badness > 0)
1036 badness >>= nest;
1037 else
1038 {
1039 badness <<= nest;
1040 }
1041 if (dump)
1042 fprintf (dump_file, " %i: no profile. nest %i\n", (int) badness,
1043 nest);
1044 }
1045
1046 /* Ensure that we did not overflow in all the fixed point math above. */
1047 gcc_assert (badness >= INT_MIN);
1048 gcc_assert (badness <= INT_MAX - 1);
1049 /* Make recursive inlining happen always after other inlining is done. */
1050 if (cgraph_edge_recursive_p (edge))
1051 return badness + 1;
1052 else
1053 return badness;
1054 }
1055
1056 /* Recompute badness of EDGE and update its key in HEAP if needed. */
1057 static inline void
1058 update_edge_key (fibheap_t heap, struct cgraph_edge *edge)
1059 {
1060 int badness = edge_badness (edge, false);
1061 if (edge->aux)
1062 {
1063 fibnode_t n = (fibnode_t) edge->aux;
1064 gcc_checking_assert (n->data == edge);
1065
1066 /* fibheap_replace_key only decrease the keys.
1067 When we increase the key we do not update heap
1068 and instead re-insert the element once it becomes
1069 a minimum of heap. */
1070 if (badness < n->key)
1071 {
1072 if (dump_file && (dump_flags & TDF_DETAILS))
1073 {
1074 fprintf (dump_file,
1075 " decreasing badness %s/%i -> %s/%i, %i to %i\n",
1076 xstrdup (edge->caller->name ()),
1077 edge->caller->order,
1078 xstrdup (edge->callee->name ()),
1079 edge->callee->order,
1080 (int)n->key,
1081 badness);
1082 }
1083 fibheap_replace_key (heap, n, badness);
1084 gcc_checking_assert (n->key == badness);
1085 }
1086 }
1087 else
1088 {
1089 if (dump_file && (dump_flags & TDF_DETAILS))
1090 {
1091 fprintf (dump_file,
1092 " enqueuing call %s/%i -> %s/%i, badness %i\n",
1093 xstrdup (edge->caller->name ()),
1094 edge->caller->order,
1095 xstrdup (edge->callee->name ()),
1096 edge->callee->order,
1097 badness);
1098 }
1099 edge->aux = fibheap_insert (heap, badness, edge);
1100 }
1101 }
1102
1103
1104 /* NODE was inlined.
1105 All caller edges needs to be resetted because
1106 size estimates change. Similarly callees needs reset
1107 because better context may be known. */
1108
1109 static void
1110 reset_edge_caches (struct cgraph_node *node)
1111 {
1112 struct cgraph_edge *edge;
1113 struct cgraph_edge *e = node->callees;
1114 struct cgraph_node *where = node;
1115 int i;
1116 struct ipa_ref *ref;
1117
1118 if (where->global.inlined_to)
1119 where = where->global.inlined_to;
1120
1121 /* WHERE body size has changed, the cached growth is invalid. */
1122 reset_node_growth_cache (where);
1123
1124 for (edge = where->callers; edge; edge = edge->next_caller)
1125 if (edge->inline_failed)
1126 reset_edge_growth_cache (edge);
1127 for (i = 0; ipa_ref_list_referring_iterate (&where->ref_list,
1128 i, ref); i++)
1129 if (ref->use == IPA_REF_ALIAS)
1130 reset_edge_caches (ipa_ref_referring_node (ref));
1131
1132 if (!e)
1133 return;
1134
1135 while (true)
1136 if (!e->inline_failed && e->callee->callees)
1137 e = e->callee->callees;
1138 else
1139 {
1140 if (e->inline_failed)
1141 reset_edge_growth_cache (e);
1142 if (e->next_callee)
1143 e = e->next_callee;
1144 else
1145 {
1146 do
1147 {
1148 if (e->caller == node)
1149 return;
1150 e = e->caller->callers;
1151 }
1152 while (!e->next_callee);
1153 e = e->next_callee;
1154 }
1155 }
1156 }
1157
1158 /* Recompute HEAP nodes for each of caller of NODE.
1159 UPDATED_NODES track nodes we already visited, to avoid redundant work.
1160 When CHECK_INLINABLITY_FOR is set, re-check for specified edge that
1161 it is inlinable. Otherwise check all edges. */
1162
1163 static void
1164 update_caller_keys (fibheap_t heap, struct cgraph_node *node,
1165 bitmap updated_nodes,
1166 struct cgraph_edge *check_inlinablity_for)
1167 {
1168 struct cgraph_edge *edge;
1169 int i;
1170 struct ipa_ref *ref;
1171
1172 if ((!node->alias && !inline_summary (node)->inlinable)
1173 || node->global.inlined_to)
1174 return;
1175 if (!bitmap_set_bit (updated_nodes, node->uid))
1176 return;
1177
1178 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list,
1179 i, ref); i++)
1180 if (ref->use == IPA_REF_ALIAS)
1181 {
1182 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1183 update_caller_keys (heap, alias, updated_nodes, check_inlinablity_for);
1184 }
1185
1186 for (edge = node->callers; edge; edge = edge->next_caller)
1187 if (edge->inline_failed)
1188 {
1189 if (!check_inlinablity_for
1190 || check_inlinablity_for == edge)
1191 {
1192 if (can_inline_edge_p (edge, false)
1193 && want_inline_small_function_p (edge, false))
1194 update_edge_key (heap, edge);
1195 else if (edge->aux)
1196 {
1197 report_inline_failed_reason (edge);
1198 fibheap_delete_node (heap, (fibnode_t) edge->aux);
1199 edge->aux = NULL;
1200 }
1201 }
1202 else if (edge->aux)
1203 update_edge_key (heap, edge);
1204 }
1205 }
1206
1207 /* Recompute HEAP nodes for each uninlined call in NODE.
1208 This is used when we know that edge badnesses are going only to increase
1209 (we introduced new call site) and thus all we need is to insert newly
1210 created edges into heap. */
1211
1212 static void
1213 update_callee_keys (fibheap_t heap, struct cgraph_node *node,
1214 bitmap updated_nodes)
1215 {
1216 struct cgraph_edge *e = node->callees;
1217
1218 if (!e)
1219 return;
1220 while (true)
1221 if (!e->inline_failed && e->callee->callees)
1222 e = e->callee->callees;
1223 else
1224 {
1225 enum availability avail;
1226 struct cgraph_node *callee;
1227 /* We do not reset callee growth cache here. Since we added a new call,
1228 growth chould have just increased and consequentely badness metric
1229 don't need updating. */
1230 if (e->inline_failed
1231 && (callee = cgraph_function_or_thunk_node (e->callee, &avail))
1232 && inline_summary (callee)->inlinable
1233 && avail >= AVAIL_AVAILABLE
1234 && !bitmap_bit_p (updated_nodes, callee->uid))
1235 {
1236 if (can_inline_edge_p (e, false)
1237 && want_inline_small_function_p (e, false))
1238 update_edge_key (heap, e);
1239 else if (e->aux)
1240 {
1241 report_inline_failed_reason (e);
1242 fibheap_delete_node (heap, (fibnode_t) e->aux);
1243 e->aux = NULL;
1244 }
1245 }
1246 if (e->next_callee)
1247 e = e->next_callee;
1248 else
1249 {
1250 do
1251 {
1252 if (e->caller == node)
1253 return;
1254 e = e->caller->callers;
1255 }
1256 while (!e->next_callee);
1257 e = e->next_callee;
1258 }
1259 }
1260 }
1261
1262 /* Enqueue all recursive calls from NODE into priority queue depending on
1263 how likely we want to recursively inline the call. */
1264
1265 static void
1266 lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
1267 fibheap_t heap)
1268 {
1269 struct cgraph_edge *e;
1270 enum availability avail;
1271
1272 for (e = where->callees; e; e = e->next_callee)
1273 if (e->callee == node
1274 || (cgraph_function_or_thunk_node (e->callee, &avail) == node
1275 && avail > AVAIL_OVERWRITABLE))
1276 {
1277 /* When profile feedback is available, prioritize by expected number
1278 of calls. */
1279 fibheap_insert (heap,
1280 !max_count ? -e->frequency
1281 : -(e->count / ((max_count + (1<<24) - 1) / (1<<24))),
1282 e);
1283 }
1284 for (e = where->callees; e; e = e->next_callee)
1285 if (!e->inline_failed)
1286 lookup_recursive_calls (node, e->callee, heap);
1287 }
1288
1289 /* Decide on recursive inlining: in the case function has recursive calls,
1290 inline until body size reaches given argument. If any new indirect edges
1291 are discovered in the process, add them to *NEW_EDGES, unless NEW_EDGES
1292 is NULL. */
1293
1294 static bool
1295 recursive_inlining (struct cgraph_edge *edge,
1296 vec<cgraph_edge_p> *new_edges)
1297 {
1298 int limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO);
1299 fibheap_t heap;
1300 struct cgraph_node *node;
1301 struct cgraph_edge *e;
1302 struct cgraph_node *master_clone = NULL, *next;
1303 int depth = 0;
1304 int n = 0;
1305
1306 node = edge->caller;
1307 if (node->global.inlined_to)
1308 node = node->global.inlined_to;
1309
1310 if (DECL_DECLARED_INLINE_P (node->decl))
1311 limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE);
1312
1313 /* Make sure that function is small enough to be considered for inlining. */
1314 if (estimate_size_after_inlining (node, edge) >= limit)
1315 return false;
1316 heap = fibheap_new ();
1317 lookup_recursive_calls (node, node, heap);
1318 if (fibheap_empty (heap))
1319 {
1320 fibheap_delete (heap);
1321 return false;
1322 }
1323
1324 if (dump_file)
1325 fprintf (dump_file,
1326 " Performing recursive inlining on %s\n",
1327 node->name ());
1328
1329 /* Do the inlining and update list of recursive call during process. */
1330 while (!fibheap_empty (heap))
1331 {
1332 struct cgraph_edge *curr
1333 = (struct cgraph_edge *) fibheap_extract_min (heap);
1334 struct cgraph_node *cnode, *dest = curr->callee;
1335
1336 if (!can_inline_edge_p (curr, true))
1337 continue;
1338
1339 /* MASTER_CLONE is produced in the case we already started modified
1340 the function. Be sure to redirect edge to the original body before
1341 estimating growths otherwise we will be seeing growths after inlining
1342 the already modified body. */
1343 if (master_clone)
1344 {
1345 cgraph_redirect_edge_callee (curr, master_clone);
1346 reset_edge_growth_cache (curr);
1347 }
1348
1349 if (estimate_size_after_inlining (node, curr) > limit)
1350 {
1351 cgraph_redirect_edge_callee (curr, dest);
1352 reset_edge_growth_cache (curr);
1353 break;
1354 }
1355
1356 depth = 1;
1357 for (cnode = curr->caller;
1358 cnode->global.inlined_to; cnode = cnode->callers->caller)
1359 if (node->decl
1360 == cgraph_function_or_thunk_node (curr->callee, NULL)->decl)
1361 depth++;
1362
1363 if (!want_inline_self_recursive_call_p (curr, node, false, depth))
1364 {
1365 cgraph_redirect_edge_callee (curr, dest);
1366 reset_edge_growth_cache (curr);
1367 continue;
1368 }
1369
1370 if (dump_file)
1371 {
1372 fprintf (dump_file,
1373 " Inlining call of depth %i", depth);
1374 if (node->count)
1375 {
1376 fprintf (dump_file, " called approx. %.2f times per call",
1377 (double)curr->count / node->count);
1378 }
1379 fprintf (dump_file, "\n");
1380 }
1381 if (!master_clone)
1382 {
1383 /* We need original clone to copy around. */
1384 master_clone = cgraph_clone_node (node, node->decl,
1385 node->count, CGRAPH_FREQ_BASE,
1386 false, vNULL, true, NULL, NULL);
1387 for (e = master_clone->callees; e; e = e->next_callee)
1388 if (!e->inline_failed)
1389 clone_inlined_nodes (e, true, false, NULL, CGRAPH_FREQ_BASE);
1390 cgraph_redirect_edge_callee (curr, master_clone);
1391 reset_edge_growth_cache (curr);
1392 }
1393
1394 inline_call (curr, false, new_edges, &overall_size, true);
1395 lookup_recursive_calls (node, curr->callee, heap);
1396 n++;
1397 }
1398
1399 if (!fibheap_empty (heap) && dump_file)
1400 fprintf (dump_file, " Recursive inlining growth limit met.\n");
1401 fibheap_delete (heap);
1402
1403 if (!master_clone)
1404 return false;
1405
1406 if (dump_file)
1407 fprintf (dump_file,
1408 "\n Inlined %i times, "
1409 "body grown from size %i to %i, time %i to %i\n", n,
1410 inline_summary (master_clone)->size, inline_summary (node)->size,
1411 inline_summary (master_clone)->time, inline_summary (node)->time);
1412
1413 /* Remove master clone we used for inlining. We rely that clones inlined
1414 into master clone gets queued just before master clone so we don't
1415 need recursion. */
1416 for (node = cgraph_first_function (); node != master_clone;
1417 node = next)
1418 {
1419 next = cgraph_next_function (node);
1420 if (node->global.inlined_to == master_clone)
1421 cgraph_remove_node (node);
1422 }
1423 cgraph_remove_node (master_clone);
1424 return true;
1425 }
1426
1427
1428 /* Given whole compilation unit estimate of INSNS, compute how large we can
1429 allow the unit to grow. */
1430
1431 static int
1432 compute_max_insns (int insns)
1433 {
1434 int max_insns = insns;
1435 if (max_insns < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
1436 max_insns = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
1437
1438 return ((HOST_WIDEST_INT) max_insns
1439 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
1440 }
1441
1442
1443 /* Compute badness of all edges in NEW_EDGES and add them to the HEAP. */
1444
1445 static void
1446 add_new_edges_to_heap (fibheap_t heap, vec<cgraph_edge_p> new_edges)
1447 {
1448 while (new_edges.length () > 0)
1449 {
1450 struct cgraph_edge *edge = new_edges.pop ();
1451
1452 gcc_assert (!edge->aux);
1453 if (edge->inline_failed
1454 && can_inline_edge_p (edge, true)
1455 && want_inline_small_function_p (edge, true))
1456 edge->aux = fibheap_insert (heap, edge_badness (edge, false), edge);
1457 }
1458 }
1459
1460 /* Remove EDGE from the fibheap. */
1461
1462 static void
1463 heap_edge_removal_hook (struct cgraph_edge *e, void *data)
1464 {
1465 if (e->callee)
1466 reset_node_growth_cache (e->callee);
1467 if (e->aux)
1468 {
1469 fibheap_delete_node ((fibheap_t)data, (fibnode_t)e->aux);
1470 e->aux = NULL;
1471 }
1472 }
1473
1474 /* Return true if speculation of edge E seems useful.
1475 If ANTICIPATE_INLINING is true, be conservative and hope that E
1476 may get inlined. */
1477
1478 bool
1479 speculation_useful_p (struct cgraph_edge *e, bool anticipate_inlining)
1480 {
1481 enum availability avail;
1482 struct cgraph_node *target = cgraph_function_or_thunk_node (e->callee, &avail);
1483 struct cgraph_edge *direct, *indirect;
1484 struct ipa_ref *ref;
1485
1486 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1487
1488 if (!cgraph_maybe_hot_edge_p (e))
1489 return false;
1490
1491 /* See if IP optimizations found something potentially useful about the
1492 function. For now we look only for CONST/PURE flags. Almost everything
1493 else we propagate is useless. */
1494 if (avail >= AVAIL_AVAILABLE)
1495 {
1496 int ecf_flags = flags_from_decl_or_type (target->decl);
1497 if (ecf_flags & ECF_CONST)
1498 {
1499 cgraph_speculative_call_info (e, direct, indirect, ref);
1500 if (!(indirect->indirect_info->ecf_flags & ECF_CONST))
1501 return true;
1502 }
1503 else if (ecf_flags & ECF_PURE)
1504 {
1505 cgraph_speculative_call_info (e, direct, indirect, ref);
1506 if (!(indirect->indirect_info->ecf_flags & ECF_PURE))
1507 return true;
1508 }
1509 }
1510 /* If we did not managed to inline the function nor redirect
1511 to an ipa-cp clone (that are seen by having local flag set),
1512 it is probably pointless to inline it unless hardware is missing
1513 indirect call predictor. */
1514 if (!anticipate_inlining && e->inline_failed && !target->local.local)
1515 return false;
1516 /* For overwritable targets there is not much to do. */
1517 if (e->inline_failed && !can_inline_edge_p (e, false, true))
1518 return false;
1519 /* OK, speculation seems interesting. */
1520 return true;
1521 }
1522
1523 /* We know that EDGE is not going to be inlined.
1524 See if we can remove speculation. */
1525
1526 static void
1527 resolve_noninline_speculation (fibheap_t edge_heap, struct cgraph_edge *edge)
1528 {
1529 if (edge->speculative && !speculation_useful_p (edge, false))
1530 {
1531 struct cgraph_node *node = edge->caller;
1532 struct cgraph_node *where = node->global.inlined_to
1533 ? node->global.inlined_to : node;
1534 bitmap updated_nodes = BITMAP_ALLOC (NULL);
1535
1536 cgraph_resolve_speculation (edge, NULL);
1537 reset_edge_caches (where);
1538 inline_update_overall_summary (where);
1539 update_caller_keys (edge_heap, where,
1540 updated_nodes, NULL);
1541 update_callee_keys (edge_heap, where,
1542 updated_nodes);
1543 BITMAP_FREE (updated_nodes);
1544 }
1545 }
1546
1547 /* We use greedy algorithm for inlining of small functions:
1548 All inline candidates are put into prioritized heap ordered in
1549 increasing badness.
1550
1551 The inlining of small functions is bounded by unit growth parameters. */
1552
1553 static void
1554 inline_small_functions (void)
1555 {
1556 struct cgraph_node *node;
1557 struct cgraph_edge *edge;
1558 fibheap_t edge_heap = fibheap_new ();
1559 bitmap updated_nodes = BITMAP_ALLOC (NULL);
1560 int min_size, max_size;
1561 auto_vec<cgraph_edge_p> new_indirect_edges;
1562 int initial_size = 0;
1563 struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1564 struct cgraph_edge_hook_list *edge_removal_hook_holder;
1565
1566 if (flag_indirect_inlining)
1567 new_indirect_edges.create (8);
1568
1569 edge_removal_hook_holder
1570 = cgraph_add_edge_removal_hook (&heap_edge_removal_hook, edge_heap);
1571
1572 /* Compute overall unit size and other global parameters used by badness
1573 metrics. */
1574
1575 max_count = 0;
1576 ipa_reduced_postorder (order, true, true, NULL);
1577 free (order);
1578
1579 FOR_EACH_DEFINED_FUNCTION (node)
1580 if (!node->global.inlined_to)
1581 {
1582 if (cgraph_function_with_gimple_body_p (node)
1583 || node->thunk.thunk_p)
1584 {
1585 struct inline_summary *info = inline_summary (node);
1586 struct ipa_dfs_info *dfs = (struct ipa_dfs_info *) node->aux;
1587
1588 /* Do not account external functions, they will be optimized out
1589 if not inlined. Also only count the non-cold portion of program. */
1590 if (!DECL_EXTERNAL (node->decl)
1591 && node->frequency != NODE_FREQUENCY_UNLIKELY_EXECUTED)
1592 initial_size += info->size;
1593 info->growth = estimate_growth (node);
1594 if (dfs && dfs->next_cycle)
1595 {
1596 struct cgraph_node *n2;
1597 int id = dfs->scc_no + 1;
1598 for (n2 = node; n2;
1599 n2 = ((struct ipa_dfs_info *) node->aux)->next_cycle)
1600 {
1601 struct inline_summary *info2 = inline_summary (n2);
1602 if (info2->scc_no)
1603 break;
1604 info2->scc_no = id;
1605 }
1606 }
1607 }
1608
1609 for (edge = node->callers; edge; edge = edge->next_caller)
1610 if (max_count < edge->count)
1611 max_count = edge->count;
1612 }
1613 sreal_init (&max_count_real, max_count, 0);
1614 sreal_init (&max_relbenefit_real, RELATIVE_TIME_BENEFIT_RANGE, 0);
1615 sreal_init (&half_int_min_real, INT_MAX / 2, 0);
1616 ipa_free_postorder_info ();
1617 initialize_growth_caches ();
1618
1619 if (dump_file)
1620 fprintf (dump_file,
1621 "\nDeciding on inlining of small functions. Starting with size %i.\n",
1622 initial_size);
1623
1624 overall_size = initial_size;
1625 max_size = compute_max_insns (overall_size);
1626 min_size = overall_size;
1627
1628 /* Populate the heap with all edges we might inline. */
1629
1630 FOR_EACH_DEFINED_FUNCTION (node)
1631 {
1632 bool update = false;
1633 struct cgraph_edge *next;
1634
1635 if (dump_file)
1636 fprintf (dump_file, "Enqueueing calls in %s/%i.\n",
1637 node->name (), node->order);
1638
1639 for (edge = node->callees; edge; edge = next)
1640 {
1641 next = edge->next_callee;
1642 if (edge->inline_failed
1643 && !edge->aux
1644 && can_inline_edge_p (edge, true)
1645 && want_inline_small_function_p (edge, true)
1646 && edge->inline_failed)
1647 {
1648 gcc_assert (!edge->aux);
1649 update_edge_key (edge_heap, edge);
1650 }
1651 if (edge->speculative && !speculation_useful_p (edge, edge->aux != NULL))
1652 {
1653 cgraph_resolve_speculation (edge, NULL);
1654 update = true;
1655 }
1656 }
1657 if (update)
1658 {
1659 struct cgraph_node *where = node->global.inlined_to
1660 ? node->global.inlined_to : node;
1661 inline_update_overall_summary (where);
1662 reset_node_growth_cache (where);
1663 reset_edge_caches (where);
1664 update_caller_keys (edge_heap, where,
1665 updated_nodes, NULL);
1666 bitmap_clear (updated_nodes);
1667 }
1668 }
1669
1670 gcc_assert (in_lto_p
1671 || !max_count
1672 || (profile_info && flag_branch_probabilities));
1673
1674 while (!fibheap_empty (edge_heap))
1675 {
1676 int old_size = overall_size;
1677 struct cgraph_node *where, *callee;
1678 int badness = fibheap_min_key (edge_heap);
1679 int current_badness;
1680 int cached_badness;
1681 int growth;
1682
1683 edge = (struct cgraph_edge *) fibheap_extract_min (edge_heap);
1684 gcc_assert (edge->aux);
1685 edge->aux = NULL;
1686 if (!edge->inline_failed || !edge->callee->analyzed)
1687 continue;
1688
1689 /* Be sure that caches are maintained consistent.
1690 We can not make this ENABLE_CHECKING only because it cause different
1691 updates of the fibheap queue. */
1692 cached_badness = edge_badness (edge, false);
1693 reset_edge_growth_cache (edge);
1694 reset_node_growth_cache (edge->callee);
1695
1696 /* When updating the edge costs, we only decrease badness in the keys.
1697 Increases of badness are handled lazilly; when we see key with out
1698 of date value on it, we re-insert it now. */
1699 current_badness = edge_badness (edge, false);
1700 gcc_assert (cached_badness == current_badness);
1701 gcc_assert (current_badness >= badness);
1702 if (current_badness != badness)
1703 {
1704 edge->aux = fibheap_insert (edge_heap, current_badness, edge);
1705 continue;
1706 }
1707
1708 if (!can_inline_edge_p (edge, true))
1709 {
1710 resolve_noninline_speculation (edge_heap, edge);
1711 continue;
1712 }
1713
1714 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
1715 growth = estimate_edge_growth (edge);
1716 if (dump_file)
1717 {
1718 fprintf (dump_file,
1719 "\nConsidering %s/%i with %i size\n",
1720 callee->name (), callee->order,
1721 inline_summary (callee)->size);
1722 fprintf (dump_file,
1723 " to be inlined into %s/%i in %s:%i\n"
1724 " Estimated badness is %i, frequency %.2f.\n",
1725 edge->caller->name (), edge->caller->order,
1726 flag_wpa ? "unknown"
1727 : gimple_filename ((const_gimple) edge->call_stmt),
1728 flag_wpa ? -1
1729 : gimple_lineno ((const_gimple) edge->call_stmt),
1730 badness,
1731 edge->frequency / (double)CGRAPH_FREQ_BASE);
1732 if (edge->count)
1733 fprintf (dump_file," Called "HOST_WIDEST_INT_PRINT_DEC"x\n",
1734 edge->count);
1735 if (dump_flags & TDF_DETAILS)
1736 edge_badness (edge, true);
1737 }
1738
1739 if (overall_size + growth > max_size
1740 && !DECL_DISREGARD_INLINE_LIMITS (callee->decl))
1741 {
1742 edge->inline_failed = CIF_INLINE_UNIT_GROWTH_LIMIT;
1743 report_inline_failed_reason (edge);
1744 resolve_noninline_speculation (edge_heap, edge);
1745 continue;
1746 }
1747
1748 if (!want_inline_small_function_p (edge, true))
1749 {
1750 resolve_noninline_speculation (edge_heap, edge);
1751 continue;
1752 }
1753
1754 /* Heuristics for inlining small functions work poorly for
1755 recursive calls where we do effects similar to loop unrolling.
1756 When inlining such edge seems profitable, leave decision on
1757 specific inliner. */
1758 if (cgraph_edge_recursive_p (edge))
1759 {
1760 where = edge->caller;
1761 if (where->global.inlined_to)
1762 where = where->global.inlined_to;
1763 if (!recursive_inlining (edge,
1764 flag_indirect_inlining
1765 ? &new_indirect_edges : NULL))
1766 {
1767 edge->inline_failed = CIF_RECURSIVE_INLINING;
1768 resolve_noninline_speculation (edge_heap, edge);
1769 continue;
1770 }
1771 reset_edge_caches (where);
1772 /* Recursive inliner inlines all recursive calls of the function
1773 at once. Consequently we need to update all callee keys. */
1774 if (flag_indirect_inlining)
1775 add_new_edges_to_heap (edge_heap, new_indirect_edges);
1776 update_callee_keys (edge_heap, where, updated_nodes);
1777 bitmap_clear (updated_nodes);
1778 }
1779 else
1780 {
1781 struct cgraph_node *outer_node = NULL;
1782 int depth = 0;
1783
1784 /* Consider the case where self recursive function A is inlined
1785 into B. This is desired optimization in some cases, since it
1786 leads to effect similar of loop peeling and we might completely
1787 optimize out the recursive call. However we must be extra
1788 selective. */
1789
1790 where = edge->caller;
1791 while (where->global.inlined_to)
1792 {
1793 if (where->decl == callee->decl)
1794 outer_node = where, depth++;
1795 where = where->callers->caller;
1796 }
1797 if (outer_node
1798 && !want_inline_self_recursive_call_p (edge, outer_node,
1799 true, depth))
1800 {
1801 edge->inline_failed
1802 = (DECL_DISREGARD_INLINE_LIMITS (edge->callee->decl)
1803 ? CIF_RECURSIVE_INLINING : CIF_UNSPECIFIED);
1804 resolve_noninline_speculation (edge_heap, edge);
1805 continue;
1806 }
1807 else if (depth && dump_file)
1808 fprintf (dump_file, " Peeling recursion with depth %i\n", depth);
1809
1810 gcc_checking_assert (!callee->global.inlined_to);
1811 inline_call (edge, true, &new_indirect_edges, &overall_size, true);
1812 if (flag_indirect_inlining)
1813 add_new_edges_to_heap (edge_heap, new_indirect_edges);
1814
1815 reset_edge_caches (edge->callee);
1816 reset_node_growth_cache (callee);
1817
1818 update_callee_keys (edge_heap, where, updated_nodes);
1819 }
1820 where = edge->caller;
1821 if (where->global.inlined_to)
1822 where = where->global.inlined_to;
1823
1824 /* Our profitability metric can depend on local properties
1825 such as number of inlinable calls and size of the function body.
1826 After inlining these properties might change for the function we
1827 inlined into (since it's body size changed) and for the functions
1828 called by function we inlined (since number of it inlinable callers
1829 might change). */
1830 update_caller_keys (edge_heap, where, updated_nodes, NULL);
1831 bitmap_clear (updated_nodes);
1832
1833 if (dump_file)
1834 {
1835 fprintf (dump_file,
1836 " Inlined into %s which now has time %i and size %i,"
1837 "net change of %+i.\n",
1838 edge->caller->name (),
1839 inline_summary (edge->caller)->time,
1840 inline_summary (edge->caller)->size,
1841 overall_size - old_size);
1842 }
1843 if (min_size > overall_size)
1844 {
1845 min_size = overall_size;
1846 max_size = compute_max_insns (min_size);
1847
1848 if (dump_file)
1849 fprintf (dump_file, "New minimal size reached: %i\n", min_size);
1850 }
1851 }
1852
1853 free_growth_caches ();
1854 fibheap_delete (edge_heap);
1855 if (dump_file)
1856 fprintf (dump_file,
1857 "Unit growth for small function inlining: %i->%i (%i%%)\n",
1858 initial_size, overall_size,
1859 initial_size ? overall_size * 100 / (initial_size) - 100: 0);
1860 BITMAP_FREE (updated_nodes);
1861 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
1862 }
1863
1864 /* Flatten NODE. Performed both during early inlining and
1865 at IPA inlining time. */
1866
1867 static void
1868 flatten_function (struct cgraph_node *node, bool early)
1869 {
1870 struct cgraph_edge *e;
1871
1872 /* We shouldn't be called recursively when we are being processed. */
1873 gcc_assert (node->aux == NULL);
1874
1875 node->aux = (void *) node;
1876
1877 for (e = node->callees; e; e = e->next_callee)
1878 {
1879 struct cgraph_node *orig_callee;
1880 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
1881
1882 /* We've hit cycle? It is time to give up. */
1883 if (callee->aux)
1884 {
1885 if (dump_file)
1886 fprintf (dump_file,
1887 "Not inlining %s into %s to avoid cycle.\n",
1888 xstrdup (callee->name ()),
1889 xstrdup (e->caller->name ()));
1890 e->inline_failed = CIF_RECURSIVE_INLINING;
1891 continue;
1892 }
1893
1894 /* When the edge is already inlined, we just need to recurse into
1895 it in order to fully flatten the leaves. */
1896 if (!e->inline_failed)
1897 {
1898 flatten_function (callee, early);
1899 continue;
1900 }
1901
1902 /* Flatten attribute needs to be processed during late inlining. For
1903 extra code quality we however do flattening during early optimization,
1904 too. */
1905 if (!early
1906 ? !can_inline_edge_p (e, true)
1907 : !can_early_inline_edge_p (e))
1908 continue;
1909
1910 if (cgraph_edge_recursive_p (e))
1911 {
1912 if (dump_file)
1913 fprintf (dump_file, "Not inlining: recursive call.\n");
1914 continue;
1915 }
1916
1917 if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
1918 != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
1919 {
1920 if (dump_file)
1921 fprintf (dump_file, "Not inlining: SSA form does not match.\n");
1922 continue;
1923 }
1924
1925 /* Inline the edge and flatten the inline clone. Avoid
1926 recursing through the original node if the node was cloned. */
1927 if (dump_file)
1928 fprintf (dump_file, " Inlining %s into %s.\n",
1929 xstrdup (callee->name ()),
1930 xstrdup (e->caller->name ()));
1931 orig_callee = callee;
1932 inline_call (e, true, NULL, NULL, false);
1933 if (e->callee != orig_callee)
1934 orig_callee->aux = (void *) node;
1935 flatten_function (e->callee, early);
1936 if (e->callee != orig_callee)
1937 orig_callee->aux = NULL;
1938 }
1939
1940 node->aux = NULL;
1941 if (!node->global.inlined_to)
1942 inline_update_overall_summary (node);
1943 }
1944
1945 /* Count number of callers of NODE and store it into DATA (that
1946 points to int. Worker for cgraph_for_node_and_aliases. */
1947
1948 static bool
1949 sum_callers (struct cgraph_node *node, void *data)
1950 {
1951 struct cgraph_edge *e;
1952 int *num_calls = (int *)data;
1953
1954 for (e = node->callers; e; e = e->next_caller)
1955 (*num_calls)++;
1956 return false;
1957 }
1958
1959 /* Inline NODE to all callers. Worker for cgraph_for_node_and_aliases.
1960 DATA points to number of calls originally found so we avoid infinite
1961 recursion. */
1962
1963 static bool
1964 inline_to_all_callers (struct cgraph_node *node, void *data)
1965 {
1966 int *num_calls = (int *)data;
1967 while (node->callers && !node->global.inlined_to)
1968 {
1969 struct cgraph_node *caller = node->callers->caller;
1970
1971 if (dump_file)
1972 {
1973 fprintf (dump_file,
1974 "\nInlining %s size %i.\n",
1975 node->name (),
1976 inline_summary (node)->size);
1977 fprintf (dump_file,
1978 " Called once from %s %i insns.\n",
1979 node->callers->caller->name (),
1980 inline_summary (node->callers->caller)->size);
1981 }
1982
1983 inline_call (node->callers, true, NULL, NULL, true);
1984 if (dump_file)
1985 fprintf (dump_file,
1986 " Inlined into %s which now has %i size\n",
1987 caller->name (),
1988 inline_summary (caller)->size);
1989 if (!(*num_calls)--)
1990 {
1991 if (dump_file)
1992 fprintf (dump_file, "New calls found; giving up.\n");
1993 return true;
1994 }
1995 }
1996 return false;
1997 }
1998
1999 /* Decide on the inlining. We do so in the topological order to avoid
2000 expenses on updating data structures. */
2001
2002 static unsigned int
2003 ipa_inline (void)
2004 {
2005 struct cgraph_node *node;
2006 int nnodes;
2007 struct cgraph_node **order;
2008 int i;
2009 int cold;
2010 bool remove_functions = false;
2011
2012 if (!optimize)
2013 return 0;
2014
2015 order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
2016
2017 if (in_lto_p && optimize)
2018 ipa_update_after_lto_read ();
2019
2020 if (dump_file)
2021 dump_inline_summaries (dump_file);
2022
2023 nnodes = ipa_reverse_postorder (order);
2024
2025 FOR_EACH_FUNCTION (node)
2026 node->aux = 0;
2027
2028 if (dump_file)
2029 fprintf (dump_file, "\nFlattening functions:\n");
2030
2031 /* In the first pass handle functions to be flattened. Do this with
2032 a priority so none of our later choices will make this impossible. */
2033 for (i = nnodes - 1; i >= 0; i--)
2034 {
2035 node = order[i];
2036
2037 /* Handle nodes to be flattened.
2038 Ideally when processing callees we stop inlining at the
2039 entry of cycles, possibly cloning that entry point and
2040 try to flatten itself turning it into a self-recursive
2041 function. */
2042 if (lookup_attribute ("flatten",
2043 DECL_ATTRIBUTES (node->decl)) != NULL)
2044 {
2045 if (dump_file)
2046 fprintf (dump_file,
2047 "Flattening %s\n", node->name ());
2048 flatten_function (node, false);
2049 }
2050 }
2051
2052 inline_small_functions ();
2053
2054 /* Do first after-inlining removal. We want to remove all "stale" extern inline
2055 functions and virtual functions so we really know what is called once. */
2056 symtab_remove_unreachable_nodes (false, dump_file);
2057 free (order);
2058
2059 /* Inline functions with a property that after inlining into all callers the
2060 code size will shrink because the out-of-line copy is eliminated.
2061 We do this regardless on the callee size as long as function growth limits
2062 are met. */
2063 if (dump_file)
2064 fprintf (dump_file,
2065 "\nDeciding on functions to be inlined into all callers and removing useless speculations:\n");
2066
2067 /* Inlining one function called once has good chance of preventing
2068 inlining other function into the same callee. Ideally we should
2069 work in priority order, but probably inlining hot functions first
2070 is good cut without the extra pain of maintaining the queue.
2071
2072 ??? this is not really fitting the bill perfectly: inlining function
2073 into callee often leads to better optimization of callee due to
2074 increased context for optimization.
2075 For example if main() function calls a function that outputs help
2076 and then function that does the main optmization, we should inline
2077 the second with priority even if both calls are cold by themselves.
2078
2079 We probably want to implement new predicate replacing our use of
2080 maybe_hot_edge interpreted as maybe_hot_edge || callee is known
2081 to be hot. */
2082 for (cold = 0; cold <= 1; cold ++)
2083 {
2084 FOR_EACH_DEFINED_FUNCTION (node)
2085 {
2086 struct cgraph_edge *edge, *next;
2087 bool update=false;
2088
2089 for (edge = node->callees; edge; edge = next)
2090 {
2091 next = edge->next_callee;
2092 if (edge->speculative && !speculation_useful_p (edge, false))
2093 {
2094 cgraph_resolve_speculation (edge, NULL);
2095 update = true;
2096 remove_functions = true;
2097 }
2098 }
2099 if (update)
2100 {
2101 struct cgraph_node *where = node->global.inlined_to
2102 ? node->global.inlined_to : node;
2103 reset_node_growth_cache (where);
2104 reset_edge_caches (where);
2105 inline_update_overall_summary (where);
2106 }
2107 if (flag_inline_functions_called_once
2108 && want_inline_function_to_all_callers_p (node, cold))
2109 {
2110 int num_calls = 0;
2111 cgraph_for_node_and_aliases (node, sum_callers,
2112 &num_calls, true);
2113 cgraph_for_node_and_aliases (node, inline_to_all_callers,
2114 &num_calls, true);
2115 remove_functions = true;
2116 }
2117 }
2118 }
2119
2120 /* Free ipa-prop structures if they are no longer needed. */
2121 if (optimize)
2122 ipa_free_all_structures_after_iinln ();
2123
2124 if (dump_file)
2125 fprintf (dump_file,
2126 "\nInlined %i calls, eliminated %i functions\n\n",
2127 ncalls_inlined, nfunctions_inlined);
2128
2129 if (dump_file)
2130 dump_inline_summaries (dump_file);
2131 /* In WPA we use inline summaries for partitioning process. */
2132 if (!flag_wpa)
2133 inline_free_summary ();
2134 return remove_functions ? TODO_remove_functions : 0;
2135 }
2136
2137 /* Inline always-inline function calls in NODE. */
2138
2139 static bool
2140 inline_always_inline_functions (struct cgraph_node *node)
2141 {
2142 struct cgraph_edge *e;
2143 bool inlined = false;
2144
2145 for (e = node->callees; e; e = e->next_callee)
2146 {
2147 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
2148 if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl))
2149 continue;
2150
2151 if (cgraph_edge_recursive_p (e))
2152 {
2153 if (dump_file)
2154 fprintf (dump_file, " Not inlining recursive call to %s.\n",
2155 e->callee->name ());
2156 e->inline_failed = CIF_RECURSIVE_INLINING;
2157 continue;
2158 }
2159
2160 if (!can_early_inline_edge_p (e))
2161 {
2162 /* Set inlined to true if the callee is marked "always_inline" but
2163 is not inlinable. This will allow flagging an error later in
2164 expand_call_inline in tree-inline.c. */
2165 if (lookup_attribute ("always_inline",
2166 DECL_ATTRIBUTES (callee->decl)) != NULL)
2167 inlined = true;
2168 continue;
2169 }
2170
2171 if (dump_file)
2172 fprintf (dump_file, " Inlining %s into %s (always_inline).\n",
2173 xstrdup (e->callee->name ()),
2174 xstrdup (e->caller->name ()));
2175 inline_call (e, true, NULL, NULL, false);
2176 inlined = true;
2177 }
2178 if (inlined)
2179 inline_update_overall_summary (node);
2180
2181 return inlined;
2182 }
2183
2184 /* Decide on the inlining. We do so in the topological order to avoid
2185 expenses on updating data structures. */
2186
2187 static bool
2188 early_inline_small_functions (struct cgraph_node *node)
2189 {
2190 struct cgraph_edge *e;
2191 bool inlined = false;
2192
2193 for (e = node->callees; e; e = e->next_callee)
2194 {
2195 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
2196 if (!inline_summary (callee)->inlinable
2197 || !e->inline_failed)
2198 continue;
2199
2200 /* Do not consider functions not declared inline. */
2201 if (!DECL_DECLARED_INLINE_P (callee->decl)
2202 && !flag_inline_small_functions
2203 && !flag_inline_functions)
2204 continue;
2205
2206 if (dump_file)
2207 fprintf (dump_file, "Considering inline candidate %s.\n",
2208 callee->name ());
2209
2210 if (!can_early_inline_edge_p (e))
2211 continue;
2212
2213 if (cgraph_edge_recursive_p (e))
2214 {
2215 if (dump_file)
2216 fprintf (dump_file, " Not inlining: recursive call.\n");
2217 continue;
2218 }
2219
2220 if (!want_early_inline_function_p (e))
2221 continue;
2222
2223 if (dump_file)
2224 fprintf (dump_file, " Inlining %s into %s.\n",
2225 xstrdup (callee->name ()),
2226 xstrdup (e->caller->name ()));
2227 inline_call (e, true, NULL, NULL, true);
2228 inlined = true;
2229 }
2230
2231 return inlined;
2232 }
2233
2234 /* Do inlining of small functions. Doing so early helps profiling and other
2235 passes to be somewhat more effective and avoids some code duplication in
2236 later real inlining pass for testcases with very many function calls. */
2237
2238 namespace {
2239
2240 const pass_data pass_data_early_inline =
2241 {
2242 GIMPLE_PASS, /* type */
2243 "einline", /* name */
2244 OPTGROUP_INLINE, /* optinfo_flags */
2245 true, /* has_execute */
2246 TV_EARLY_INLINING, /* tv_id */
2247 PROP_ssa, /* properties_required */
2248 0, /* properties_provided */
2249 0, /* properties_destroyed */
2250 0, /* todo_flags_start */
2251 0, /* todo_flags_finish */
2252 };
2253
2254 class pass_early_inline : public gimple_opt_pass
2255 {
2256 public:
2257 pass_early_inline (gcc::context *ctxt)
2258 : gimple_opt_pass (pass_data_early_inline, ctxt)
2259 {}
2260
2261 /* opt_pass methods: */
2262 virtual unsigned int execute (function *);
2263
2264 }; // class pass_early_inline
2265
2266 unsigned int
2267 pass_early_inline::execute (function *fun)
2268 {
2269 struct cgraph_node *node = cgraph_get_node (current_function_decl);
2270 struct cgraph_edge *edge;
2271 unsigned int todo = 0;
2272 int iterations = 0;
2273 bool inlined = false;
2274
2275 if (seen_error ())
2276 return 0;
2277
2278 /* Do nothing if datastructures for ipa-inliner are already computed. This
2279 happens when some pass decides to construct new function and
2280 cgraph_add_new_function calls lowering passes and early optimization on
2281 it. This may confuse ourself when early inliner decide to inline call to
2282 function clone, because function clones don't have parameter list in
2283 ipa-prop matching their signature. */
2284 if (ipa_node_params_vector.exists ())
2285 return 0;
2286
2287 #ifdef ENABLE_CHECKING
2288 verify_cgraph_node (node);
2289 #endif
2290 ipa_remove_all_references (&node->ref_list);
2291
2292 /* Even when not optimizing or not inlining inline always-inline
2293 functions. */
2294 inlined = inline_always_inline_functions (node);
2295
2296 if (!optimize
2297 || flag_no_inline
2298 || !flag_early_inlining
2299 /* Never inline regular functions into always-inline functions
2300 during incremental inlining. This sucks as functions calling
2301 always inline functions will get less optimized, but at the
2302 same time inlining of functions calling always inline
2303 function into an always inline function might introduce
2304 cycles of edges to be always inlined in the callgraph.
2305
2306 We might want to be smarter and just avoid this type of inlining. */
2307 || DECL_DISREGARD_INLINE_LIMITS (node->decl))
2308 ;
2309 else if (lookup_attribute ("flatten",
2310 DECL_ATTRIBUTES (node->decl)) != NULL)
2311 {
2312 /* When the function is marked to be flattened, recursively inline
2313 all calls in it. */
2314 if (dump_file)
2315 fprintf (dump_file,
2316 "Flattening %s\n", node->name ());
2317 flatten_function (node, true);
2318 inlined = true;
2319 }
2320 else
2321 {
2322 /* We iterate incremental inlining to get trivial cases of indirect
2323 inlining. */
2324 while (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS)
2325 && early_inline_small_functions (node))
2326 {
2327 timevar_push (TV_INTEGRATION);
2328 todo |= optimize_inline_calls (current_function_decl);
2329
2330 /* Technically we ought to recompute inline parameters so the new
2331 iteration of early inliner works as expected. We however have
2332 values approximately right and thus we only need to update edge
2333 info that might be cleared out for newly discovered edges. */
2334 for (edge = node->callees; edge; edge = edge->next_callee)
2335 {
2336 struct inline_edge_summary *es = inline_edge_summary (edge);
2337 es->call_stmt_size
2338 = estimate_num_insns (edge->call_stmt, &eni_size_weights);
2339 es->call_stmt_time
2340 = estimate_num_insns (edge->call_stmt, &eni_time_weights);
2341 if (edge->callee->decl
2342 && !gimple_check_call_matching_types (
2343 edge->call_stmt, edge->callee->decl, false))
2344 edge->call_stmt_cannot_inline_p = true;
2345 }
2346 if (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS) - 1)
2347 inline_update_overall_summary (node);
2348 timevar_pop (TV_INTEGRATION);
2349 iterations++;
2350 inlined = false;
2351 }
2352 if (dump_file)
2353 fprintf (dump_file, "Iterations: %i\n", iterations);
2354 }
2355
2356 if (inlined)
2357 {
2358 timevar_push (TV_INTEGRATION);
2359 todo |= optimize_inline_calls (current_function_decl);
2360 timevar_pop (TV_INTEGRATION);
2361 }
2362
2363 fun->always_inline_functions_inlined = true;
2364
2365 return todo;
2366 }
2367
2368 } // anon namespace
2369
2370 gimple_opt_pass *
2371 make_pass_early_inline (gcc::context *ctxt)
2372 {
2373 return new pass_early_inline (ctxt);
2374 }
2375
2376 namespace {
2377
2378 const pass_data pass_data_ipa_inline =
2379 {
2380 IPA_PASS, /* type */
2381 "inline", /* name */
2382 OPTGROUP_INLINE, /* optinfo_flags */
2383 true, /* has_execute */
2384 TV_IPA_INLINING, /* tv_id */
2385 0, /* properties_required */
2386 0, /* properties_provided */
2387 0, /* properties_destroyed */
2388 TODO_remove_functions, /* todo_flags_start */
2389 ( TODO_dump_symtab ), /* todo_flags_finish */
2390 };
2391
2392 class pass_ipa_inline : public ipa_opt_pass_d
2393 {
2394 public:
2395 pass_ipa_inline (gcc::context *ctxt)
2396 : ipa_opt_pass_d (pass_data_ipa_inline, ctxt,
2397 inline_generate_summary, /* generate_summary */
2398 inline_write_summary, /* write_summary */
2399 inline_read_summary, /* read_summary */
2400 NULL, /* write_optimization_summary */
2401 NULL, /* read_optimization_summary */
2402 NULL, /* stmt_fixup */
2403 0, /* function_transform_todo_flags_start */
2404 inline_transform, /* function_transform */
2405 NULL) /* variable_transform */
2406 {}
2407
2408 /* opt_pass methods: */
2409 virtual unsigned int execute (function *) { return ipa_inline (); }
2410
2411 }; // class pass_ipa_inline
2412
2413 } // anon namespace
2414
2415 ipa_opt_pass_d *
2416 make_pass_ipa_inline (gcc::context *ctxt)
2417 {
2418 return new pass_ipa_inline (ctxt);
2419 }