Merged with trunk at revision 155767
[official-gcc.git] / gcc / ipa-inline.c
blobc6c84f99572fff17117d71c03093d32e165e39c0
1 /* Inlining decision heuristics.
2 Copyright (C) 2003, 2004, 2007, 2008, 2009 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* Inlining decision heuristics
23 We separate inlining decisions from the inliner itself and store it
24 inside callgraph as so called inline plan. Refer to cgraph.c
25 documentation about particular representation of inline plans in the
26 callgraph.
28 There are three major parts of this file:
30 cgraph_mark_inline implementation
32 This function allows to mark given call inline and performs necessary
33 modifications of cgraph (production of the clones and updating overall
34 statistics)
36 inlining heuristics limits
38 These functions allow to check that particular inlining is allowed
39 by the limits specified by user (allowed function growth, overall unit
40 growth and so on).
42 inlining heuristics
44 This is implementation of IPA pass aiming to get as much of benefit
45 from inlining obeying the limits checked above.
47 The implementation of particular heuristics is separated from
48 the rest of code to make it easier to replace it with more complicated
49 implementation in the future. The rest of inlining code acts as a
50 library aimed to modify the callgraph and verify that the parameters
51 on code size growth fits.
53 To mark given call inline, use cgraph_mark_inline function, the
54 verification is performed by cgraph_default_inline_p and
55 cgraph_check_inline_limits.
57 The heuristics implements simple knapsack style algorithm ordering
58 all functions by their "profitability" (estimated by code size growth)
59 and inlining them in priority order.
61 cgraph_decide_inlining implements heuristics taking whole callgraph
62 into account, while cgraph_decide_inlining_incrementally considers
63 only one function at a time and is used by early inliner.
65 The inliner itself is split into several passes:
67 pass_inline_parameters
69 This pass computes local properties of functions that are used by inliner:
70 estimated function body size, whether function is inlinable at all and
71 stack frame consumption.
73 Before executing any of inliner passes, this local pass has to be applied
74 to each function in the callgraph (ie run as subpass of some earlier
75 IPA pass). The results are made out of date by any optimization applied
76 on the function body.
78 pass_early_inlining
80 Simple local inlining pass inlining callees into current function. This
81 pass makes no global whole compilation unit analysis and this when allowed
82 to do inlining expanding code size it might result in unbounded growth of
83 whole unit.
85 The pass is run during conversion into SSA form. Only functions already
86 converted into SSA form are inlined, so the conversion must happen in
87 topological order on the callgraph (that is maintained by pass manager).
88 The functions after inlining are early optimized so the early inliner sees
89 unoptimized function itself, but all considered callees are already
90 optimized allowing it to unfold abstraction penalty on C++ effectively and
91 cheaply.
93 pass_ipa_early_inlining
95 With profiling, the early inlining is also necessary to reduce
96 instrumentation costs on program with high abstraction penalty (doing
97 many redundant calls). This can't happen in parallel with early
98 optimization and profile instrumentation, because we would end up
99 re-instrumenting already instrumented function bodies we brought in via
100 inlining.
102 To avoid this, this pass is executed as IPA pass before profiling. It is
103 simple wrapper to pass_early_inlining and ensures first inlining.
105 pass_ipa_inline
107 This is the main pass implementing simple greedy algorithm to do inlining
108 of small functions that results in overall growth of compilation unit and
109 inlining of functions called once. The pass compute just so called inline
110 plan (representation of inlining to be done in callgraph) and unlike early
111 inlining it is not performing the inlining itself.
113 pass_apply_inline
115 This pass performs actual inlining according to pass_ipa_inline on given
116 function. Possible the function body before inlining is saved when it is
117 needed for further inlining later.
120 #include "config.h"
121 #include "system.h"
122 #include "coretypes.h"
123 #include "tm.h"
124 #include "tree.h"
125 #include "tree-inline.h"
126 #include "langhooks.h"
127 #include "flags.h"
128 #include "cgraph.h"
129 #include "diagnostic.h"
130 #include "timevar.h"
131 #include "params.h"
132 #include "fibheap.h"
133 #include "intl.h"
134 #include "tree-pass.h"
135 #include "hashtab.h"
136 #include "coverage.h"
137 #include "ggc.h"
138 #include "tree-flow.h"
139 #include "rtl.h"
140 #include "ipa-prop.h"
141 #include "except.h"
143 #define MAX_TIME 1000000000
145 /* Mode incremental inliner operate on:
147 In ALWAYS_INLINE only functions marked
148 always_inline are inlined. This mode is used after detecting cycle during
149 flattening.
151 In SIZE mode, only functions that reduce function body size after inlining
152 are inlined, this is used during early inlining.
154 in ALL mode, everything is inlined. This is used during flattening. */
155 enum inlining_mode {
156 INLINE_NONE = 0,
157 INLINE_ALWAYS_INLINE,
158 INLINE_SIZE_NORECURSIVE,
159 INLINE_SIZE,
160 INLINE_ALL
162 static bool
163 cgraph_decide_inlining_incrementally (struct cgraph_node *, enum inlining_mode,
164 int);
167 /* Statistics we collect about inlining algorithm. */
168 static int ncalls_inlined;
169 static int nfunctions_inlined;
170 static int overall_size;
171 static gcov_type max_count, max_benefit;
173 /* Holders of ipa cgraph hooks: */
174 static struct cgraph_node_hook_list *function_insertion_hook_holder;
176 static inline struct inline_summary *
177 inline_summary (struct cgraph_node *node)
179 return &node->local.inline_summary;
182 /* Estimate self time of the function after inlining WHAT into TO. */
184 static int
185 cgraph_estimate_time_after_inlining (int frequency, struct cgraph_node *to,
186 struct cgraph_node *what)
188 gcov_type time = (((gcov_type)what->global.time
189 - inline_summary (what)->time_inlining_benefit)
190 * frequency + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE
191 + to->global.time;
192 if (time < 0)
193 time = 0;
194 if (time > MAX_TIME)
195 time = MAX_TIME;
196 return time;
199 /* Estimate self time of the function after inlining WHAT into TO. */
201 static int
202 cgraph_estimate_size_after_inlining (int times, struct cgraph_node *to,
203 struct cgraph_node *what)
205 int size = (what->global.size - inline_summary (what)->size_inlining_benefit) * times + to->global.size;
206 gcc_assert (size >= 0);
207 return size;
210 /* Scale frequency of NODE edges by FREQ_SCALE and increase loop nest
211 by NEST. */
213 static void
214 update_noncloned_frequencies (struct cgraph_node *node,
215 int freq_scale, int nest)
217 struct cgraph_edge *e;
219 /* We do not want to ignore high loop nest after freq drops to 0. */
220 if (!freq_scale)
221 freq_scale = 1;
222 for (e = node->callees; e; e = e->next_callee)
224 e->loop_nest += nest;
225 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
226 if (e->frequency > CGRAPH_FREQ_MAX)
227 e->frequency = CGRAPH_FREQ_MAX;
228 if (!e->inline_failed)
229 update_noncloned_frequencies (e->callee, freq_scale, nest);
233 /* E is expected to be an edge being inlined. Clone destination node of
234 the edge and redirect it to the new clone.
235 DUPLICATE is used for bookkeeping on whether we are actually creating new
236 clones or re-using node originally representing out-of-line function call.
238 void
239 cgraph_clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
240 bool update_original)
242 HOST_WIDE_INT peak;
244 if (duplicate)
246 /* We may eliminate the need for out-of-line copy to be output.
247 In that case just go ahead and re-use it. */
248 if (!e->callee->callers->next_caller
249 && cgraph_can_remove_if_no_direct_calls_p (e->callee)
250 /* Don't reuse if more than one function shares a comdat group.
251 If the other function(s) are needed, we need to emit even
252 this function out of line. */
253 && !e->callee->same_comdat_group
254 && !cgraph_new_nodes)
256 gcc_assert (!e->callee->global.inlined_to);
257 if (e->callee->analyzed)
259 overall_size -= e->callee->global.size;
260 nfunctions_inlined++;
262 duplicate = false;
263 e->callee->local.externally_visible = false;
264 update_noncloned_frequencies (e->callee, e->frequency, e->loop_nest);
266 else
268 struct cgraph_node *n;
269 n = cgraph_clone_node (e->callee, e->count, e->frequency, e->loop_nest,
270 update_original, NULL);
271 cgraph_redirect_edge_callee (e, n);
275 if (e->caller->global.inlined_to)
276 e->callee->global.inlined_to = e->caller->global.inlined_to;
277 else
278 e->callee->global.inlined_to = e->caller;
279 e->callee->global.stack_frame_offset
280 = e->caller->global.stack_frame_offset
281 + inline_summary (e->caller)->estimated_self_stack_size;
282 peak = e->callee->global.stack_frame_offset
283 + inline_summary (e->callee)->estimated_self_stack_size;
284 if (e->callee->global.inlined_to->global.estimated_stack_size < peak)
285 e->callee->global.inlined_to->global.estimated_stack_size = peak;
287 /* Recursively clone all bodies. */
288 for (e = e->callee->callees; e; e = e->next_callee)
289 if (!e->inline_failed)
290 cgraph_clone_inlined_nodes (e, duplicate, update_original);
293 /* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL
294 specify whether profile of original function should be updated. If any new
295 indirect edges are discovered in the process, add them to NEW_EDGES, unless
296 it is NULL. Return true iff any new callgraph edges were discovered as a
297 result of inlining. */
299 static bool
300 cgraph_mark_inline_edge (struct cgraph_edge *e, bool update_original,
301 VEC (cgraph_edge_p, heap) **new_edges)
303 int old_size = 0, new_size = 0;
304 struct cgraph_node *to = NULL, *what;
305 struct cgraph_edge *curr = e;
306 int freq;
307 bool duplicate = false;
308 int orig_size = e->callee->global.size;
310 gcc_assert (e->inline_failed);
311 e->inline_failed = CIF_OK;
313 if (!e->callee->global.inlined)
314 DECL_POSSIBLY_INLINED (e->callee->decl) = true;
315 e->callee->global.inlined = true;
317 if (e->callee->callers->next_caller
318 || !cgraph_can_remove_if_no_direct_calls_p (e->callee)
319 || e->callee->same_comdat_group)
320 duplicate = true;
321 cgraph_clone_inlined_nodes (e, true, update_original);
323 what = e->callee;
325 freq = e->frequency;
326 /* Now update size of caller and all functions caller is inlined into. */
327 for (;e && !e->inline_failed; e = e->caller->callers)
329 to = e->caller;
330 old_size = e->caller->global.size;
331 new_size = cgraph_estimate_size_after_inlining (1, to, what);
332 to->global.size = new_size;
333 to->global.time = cgraph_estimate_time_after_inlining (freq, to, what);
335 gcc_assert (what->global.inlined_to == to);
336 if (new_size > old_size)
337 overall_size += new_size - old_size;
338 if (!duplicate)
339 overall_size -= orig_size;
340 ncalls_inlined++;
342 if (flag_indirect_inlining)
343 return ipa_propagate_indirect_call_infos (curr, new_edges);
344 else
345 return false;
348 /* Mark all calls of EDGE->CALLEE inlined into EDGE->CALLER.
349 Return following unredirected edge in the list of callers
350 of EDGE->CALLEE */
352 static struct cgraph_edge *
353 cgraph_mark_inline (struct cgraph_edge *edge)
355 struct cgraph_node *to = edge->caller;
356 struct cgraph_node *what = edge->callee;
357 struct cgraph_edge *e, *next;
359 gcc_assert (!edge->call_stmt_cannot_inline_p);
360 /* Look for all calls, mark them inline and clone recursively
361 all inlined functions. */
362 for (e = what->callers; e; e = next)
364 next = e->next_caller;
365 if (e->caller == to && e->inline_failed)
367 cgraph_mark_inline_edge (e, true, NULL);
368 if (e == edge)
369 edge = next;
373 return edge;
376 /* Estimate the growth caused by inlining NODE into all callees. */
378 static int
379 cgraph_estimate_growth (struct cgraph_node *node)
381 int growth = 0;
382 struct cgraph_edge *e;
383 bool self_recursive = false;
385 if (node->global.estimated_growth != INT_MIN)
386 return node->global.estimated_growth;
388 for (e = node->callers; e; e = e->next_caller)
390 if (e->caller == node)
391 self_recursive = true;
392 if (e->inline_failed)
393 growth += (cgraph_estimate_size_after_inlining (1, e->caller, node)
394 - e->caller->global.size);
397 /* ??? Wrong for non-trivially self recursive functions or cases where
398 we decide to not inline for different reasons, but it is not big deal
399 as in that case we will keep the body around, but we will also avoid
400 some inlining. */
401 if (cgraph_only_called_directly_p (node)
402 && !DECL_EXTERNAL (node->decl) && !self_recursive)
403 growth -= node->global.size;
405 node->global.estimated_growth = growth;
406 return growth;
409 /* Return false when inlining WHAT into TO is not good idea
410 as it would cause too large growth of function bodies.
411 When ONE_ONLY is true, assume that only one call site is going
412 to be inlined, otherwise figure out how many call sites in
413 TO calls WHAT and verify that all can be inlined.
416 static bool
417 cgraph_check_inline_limits (struct cgraph_node *to, struct cgraph_node *what,
418 cgraph_inline_failed_t *reason, bool one_only)
420 int times = 0;
421 struct cgraph_edge *e;
422 int newsize;
423 int limit;
424 HOST_WIDE_INT stack_size_limit, inlined_stack;
426 if (one_only)
427 times = 1;
428 else
429 for (e = to->callees; e; e = e->next_callee)
430 if (e->callee == what)
431 times++;
433 if (to->global.inlined_to)
434 to = to->global.inlined_to;
436 /* When inlining large function body called once into small function,
437 take the inlined function as base for limiting the growth. */
438 if (inline_summary (to)->self_size > inline_summary(what)->self_size)
439 limit = inline_summary (to)->self_size;
440 else
441 limit = inline_summary (what)->self_size;
443 limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
445 /* Check the size after inlining against the function limits. But allow
446 the function to shrink if it went over the limits by forced inlining. */
447 newsize = cgraph_estimate_size_after_inlining (times, to, what);
448 if (newsize >= to->global.size
449 && newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
450 && newsize > limit)
452 if (reason)
453 *reason = CIF_LARGE_FUNCTION_GROWTH_LIMIT;
454 return false;
457 stack_size_limit = inline_summary (to)->estimated_self_stack_size;
459 stack_size_limit += stack_size_limit * PARAM_VALUE (PARAM_STACK_FRAME_GROWTH) / 100;
461 inlined_stack = (to->global.stack_frame_offset
462 + inline_summary (to)->estimated_self_stack_size
463 + what->global.estimated_stack_size);
464 if (inlined_stack > stack_size_limit
465 && inlined_stack > PARAM_VALUE (PARAM_LARGE_STACK_FRAME))
467 if (reason)
468 *reason = CIF_LARGE_STACK_FRAME_GROWTH_LIMIT;
469 return false;
471 return true;
474 /* Return true when function N is small enough to be inlined. */
476 static bool
477 cgraph_default_inline_p (struct cgraph_node *n, cgraph_inline_failed_t *reason)
479 tree decl = n->decl;
481 if (!flag_inline_small_functions && !DECL_DECLARED_INLINE_P (decl))
483 if (reason)
484 *reason = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
485 return false;
488 if (!n->analyzed)
490 if (reason)
491 *reason = CIF_BODY_NOT_AVAILABLE;
492 return false;
495 if (DECL_DECLARED_INLINE_P (decl))
497 if (n->global.size >= MAX_INLINE_INSNS_SINGLE)
499 if (reason)
500 *reason = CIF_MAX_INLINE_INSNS_SINGLE_LIMIT;
501 return false;
504 else
506 if (n->global.size >= MAX_INLINE_INSNS_AUTO)
508 if (reason)
509 *reason = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
510 return false;
514 return true;
517 /* Return true when inlining WHAT would create recursive inlining.
518 We call recursive inlining all cases where same function appears more than
519 once in the single recursion nest path in the inline graph. */
521 static bool
522 cgraph_recursive_inlining_p (struct cgraph_node *to,
523 struct cgraph_node *what,
524 cgraph_inline_failed_t *reason)
526 bool recursive;
527 if (to->global.inlined_to)
528 recursive = what->decl == to->global.inlined_to->decl;
529 else
530 recursive = what->decl == to->decl;
531 /* Marking recursive function inline has sane semantic and thus we should
532 not warn on it. */
533 if (recursive && reason)
534 *reason = (what->local.disregard_inline_limits
535 ? CIF_RECURSIVE_INLINING : CIF_UNSPECIFIED);
536 return recursive;
539 /* A cost model driving the inlining heuristics in a way so the edges with
540 smallest badness are inlined first. After each inlining is performed
541 the costs of all caller edges of nodes affected are recomputed so the
542 metrics may accurately depend on values such as number of inlinable callers
543 of the function or function body size. */
545 static int
546 cgraph_edge_badness (struct cgraph_edge *edge)
548 gcov_type badness;
549 int growth =
550 cgraph_estimate_size_after_inlining (1, edge->caller, edge->callee);
552 growth -= edge->caller->global.size;
554 /* Always prefer inlining saving code size. */
555 if (growth <= 0)
556 badness = INT_MIN - growth;
558 /* When profiling is available, base priorities -(#calls / growth).
559 So we optimize for overall number of "executed" inlined calls. */
560 else if (max_count)
561 badness = ((int)((double)edge->count * INT_MIN / max_count / (max_benefit + 1))
562 * (inline_summary (edge->callee)->time_inlining_benefit + 1)) / growth;
564 /* When function local profile is available, base priorities on
565 growth / frequency, so we optimize for overall frequency of inlined
566 calls. This is not too accurate since while the call might be frequent
567 within function, the function itself is infrequent.
569 Other objective to optimize for is number of different calls inlined.
570 We add the estimated growth after inlining all functions to bias the
571 priorities slightly in this direction (so fewer times called functions
572 of the same size gets priority). */
573 else if (flag_guess_branch_prob)
575 int div = edge->frequency * 100 / CGRAPH_FREQ_BASE + 1;
576 badness = growth * 10000;
577 div *= MIN (100 * inline_summary (edge->callee)->time_inlining_benefit
578 / (edge->callee->global.time + 1) + 1, 100);
581 /* Decrease badness if call is nested. */
582 /* Compress the range so we don't overflow. */
583 if (div > 10000)
584 div = 10000 + ceil_log2 (div) - 8;
585 if (div < 1)
586 div = 1;
587 if (badness > 0)
588 badness /= div;
589 badness += cgraph_estimate_growth (edge->callee);
590 if (badness > INT_MAX)
591 badness = INT_MAX;
593 /* When function local profile is not available or it does not give
594 useful information (ie frequency is zero), base the cost on
595 loop nest and overall size growth, so we optimize for overall number
596 of functions fully inlined in program. */
597 else
599 int nest = MIN (edge->loop_nest, 8);
600 badness = cgraph_estimate_growth (edge->callee) * 256;
602 /* Decrease badness if call is nested. */
603 if (badness > 0)
604 badness >>= nest;
605 else
607 badness <<= nest;
610 /* Make recursive inlining happen always after other inlining is done. */
611 if (cgraph_recursive_inlining_p (edge->caller, edge->callee, NULL))
612 return badness + 1;
613 else
614 return badness;
617 /* Recompute heap nodes for each of caller edge. */
619 static void
620 update_caller_keys (fibheap_t heap, struct cgraph_node *node,
621 bitmap updated_nodes)
623 struct cgraph_edge *edge;
624 cgraph_inline_failed_t failed_reason;
626 if (!node->local.inlinable || node->local.disregard_inline_limits
627 || node->global.inlined_to)
628 return;
629 if (bitmap_bit_p (updated_nodes, node->uid))
630 return;
631 bitmap_set_bit (updated_nodes, node->uid);
632 node->global.estimated_growth = INT_MIN;
634 if (!node->local.inlinable)
635 return;
636 /* Prune out edges we won't inline into anymore. */
637 if (!cgraph_default_inline_p (node, &failed_reason))
639 for (edge = node->callers; edge; edge = edge->next_caller)
640 if (edge->aux)
642 fibheap_delete_node (heap, (fibnode_t) edge->aux);
643 edge->aux = NULL;
644 if (edge->inline_failed)
645 edge->inline_failed = failed_reason;
647 return;
650 for (edge = node->callers; edge; edge = edge->next_caller)
651 if (edge->inline_failed)
653 int badness = cgraph_edge_badness (edge);
654 if (edge->aux)
656 fibnode_t n = (fibnode_t) edge->aux;
657 gcc_assert (n->data == edge);
658 if (n->key == badness)
659 continue;
661 /* fibheap_replace_key only increase the keys. */
662 if (fibheap_replace_key (heap, n, badness))
663 continue;
664 fibheap_delete_node (heap, (fibnode_t) edge->aux);
666 edge->aux = fibheap_insert (heap, badness, edge);
670 /* Recompute heap nodes for each of caller edges of each of callees. */
672 static void
673 update_callee_keys (fibheap_t heap, struct cgraph_node *node,
674 bitmap updated_nodes)
676 struct cgraph_edge *e;
677 node->global.estimated_growth = INT_MIN;
679 for (e = node->callees; e; e = e->next_callee)
680 if (e->inline_failed)
681 update_caller_keys (heap, e->callee, updated_nodes);
682 else if (!e->inline_failed)
683 update_callee_keys (heap, e->callee, updated_nodes);
686 /* Enqueue all recursive calls from NODE into priority queue depending on
687 how likely we want to recursively inline the call. */
689 static void
690 lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
691 fibheap_t heap)
693 static int priority;
694 struct cgraph_edge *e;
695 for (e = where->callees; e; e = e->next_callee)
696 if (e->callee == node)
698 /* When profile feedback is available, prioritize by expected number
699 of calls. Without profile feedback we maintain simple queue
700 to order candidates via recursive depths. */
701 fibheap_insert (heap,
702 !max_count ? priority++
703 : -(e->count / ((max_count + (1<<24) - 1) / (1<<24))),
706 for (e = where->callees; e; e = e->next_callee)
707 if (!e->inline_failed)
708 lookup_recursive_calls (node, e->callee, heap);
711 /* Decide on recursive inlining: in the case function has recursive calls,
712 inline until body size reaches given argument. If any new indirect edges
713 are discovered in the process, add them to *NEW_EDGES, unless NEW_EDGES
714 is NULL. */
716 static bool
717 cgraph_decide_recursive_inlining (struct cgraph_node *node,
718 VEC (cgraph_edge_p, heap) **new_edges)
720 int limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO);
721 int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO);
722 int probability = PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY);
723 fibheap_t heap;
724 struct cgraph_edge *e;
725 struct cgraph_node *master_clone, *next;
726 int depth = 0;
727 int n = 0;
729 if (optimize_function_for_size_p (DECL_STRUCT_FUNCTION (node->decl))
730 || (!flag_inline_functions && !DECL_DECLARED_INLINE_P (node->decl)))
731 return false;
733 if (DECL_DECLARED_INLINE_P (node->decl))
735 limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE);
736 max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH);
739 /* Make sure that function is small enough to be considered for inlining. */
740 if (!max_depth
741 || cgraph_estimate_size_after_inlining (1, node, node) >= limit)
742 return false;
743 heap = fibheap_new ();
744 lookup_recursive_calls (node, node, heap);
745 if (fibheap_empty (heap))
747 fibheap_delete (heap);
748 return false;
751 if (dump_file)
752 fprintf (dump_file,
753 " Performing recursive inlining on %s\n",
754 cgraph_node_name (node));
756 /* We need original clone to copy around. */
757 master_clone = cgraph_clone_node (node, node->count, CGRAPH_FREQ_BASE, 1,
758 false, NULL);
759 master_clone->needed = true;
760 for (e = master_clone->callees; e; e = e->next_callee)
761 if (!e->inline_failed)
762 cgraph_clone_inlined_nodes (e, true, false);
764 /* Do the inlining and update list of recursive call during process. */
765 while (!fibheap_empty (heap)
766 && (cgraph_estimate_size_after_inlining (1, node, master_clone)
767 <= limit))
769 struct cgraph_edge *curr
770 = (struct cgraph_edge *) fibheap_extract_min (heap);
771 struct cgraph_node *cnode;
773 depth = 1;
774 for (cnode = curr->caller;
775 cnode->global.inlined_to; cnode = cnode->callers->caller)
776 if (node->decl == curr->callee->decl)
777 depth++;
778 if (depth > max_depth)
780 if (dump_file)
781 fprintf (dump_file,
782 " maximal depth reached\n");
783 continue;
786 if (max_count)
788 if (!cgraph_maybe_hot_edge_p (curr))
790 if (dump_file)
791 fprintf (dump_file, " Not inlining cold call\n");
792 continue;
794 if (curr->count * 100 / node->count < probability)
796 if (dump_file)
797 fprintf (dump_file,
798 " Probability of edge is too small\n");
799 continue;
803 if (dump_file)
805 fprintf (dump_file,
806 " Inlining call of depth %i", depth);
807 if (node->count)
809 fprintf (dump_file, " called approx. %.2f times per call",
810 (double)curr->count / node->count);
812 fprintf (dump_file, "\n");
814 cgraph_redirect_edge_callee (curr, master_clone);
815 cgraph_mark_inline_edge (curr, false, new_edges);
816 lookup_recursive_calls (node, curr->callee, heap);
817 n++;
819 if (!fibheap_empty (heap) && dump_file)
820 fprintf (dump_file, " Recursive inlining growth limit met.\n");
822 fibheap_delete (heap);
823 if (dump_file)
824 fprintf (dump_file,
825 "\n Inlined %i times, body grown from size %i to %i, time %i to %i\n", n,
826 master_clone->global.size, node->global.size,
827 master_clone->global.time, node->global.time);
829 /* Remove master clone we used for inlining. We rely that clones inlined
830 into master clone gets queued just before master clone so we don't
831 need recursion. */
832 for (node = cgraph_nodes; node != master_clone;
833 node = next)
835 next = node->next;
836 if (node->global.inlined_to == master_clone)
837 cgraph_remove_node (node);
839 cgraph_remove_node (master_clone);
840 /* FIXME: Recursive inlining actually reduces number of calls of the
841 function. At this place we should probably walk the function and
842 inline clones and compensate the counts accordingly. This probably
843 doesn't matter much in practice. */
844 return n > 0;
847 /* Set inline_failed for all callers of given function to REASON. */
849 static void
850 cgraph_set_inline_failed (struct cgraph_node *node,
851 cgraph_inline_failed_t reason)
853 struct cgraph_edge *e;
855 if (dump_file)
856 fprintf (dump_file, "Inlining failed: %s\n",
857 cgraph_inline_failed_string (reason));
858 for (e = node->callers; e; e = e->next_caller)
859 if (e->inline_failed)
860 e->inline_failed = reason;
863 /* Given whole compilation unit estimate of INSNS, compute how large we can
864 allow the unit to grow. */
865 static int
866 compute_max_insns (int insns)
868 int max_insns = insns;
869 if (max_insns < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
870 max_insns = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
872 return ((HOST_WIDEST_INT) max_insns
873 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
876 /* Compute badness of all edges in NEW_EDGES and add them to the HEAP. */
877 static void
878 add_new_edges_to_heap (fibheap_t heap, VEC (cgraph_edge_p, heap) *new_edges)
880 while (VEC_length (cgraph_edge_p, new_edges) > 0)
882 struct cgraph_edge *edge = VEC_pop (cgraph_edge_p, new_edges);
884 gcc_assert (!edge->aux);
885 edge->aux = fibheap_insert (heap, cgraph_edge_badness (edge), edge);
890 /* We use greedy algorithm for inlining of small functions:
891 All inline candidates are put into prioritized heap based on estimated
892 growth of the overall number of instructions and then update the estimates.
894 INLINED and INLINED_CALEES are just pointers to arrays large enough
895 to be passed to cgraph_inlined_into and cgraph_inlined_callees. */
897 static void
898 cgraph_decide_inlining_of_small_functions (void)
900 struct cgraph_node *node;
901 struct cgraph_edge *edge;
902 cgraph_inline_failed_t failed_reason;
903 fibheap_t heap = fibheap_new ();
904 bitmap updated_nodes = BITMAP_ALLOC (NULL);
905 int min_size, max_size;
906 VEC (cgraph_edge_p, heap) *new_indirect_edges = NULL;
908 if (flag_indirect_inlining)
909 new_indirect_edges = VEC_alloc (cgraph_edge_p, heap, 8);
911 if (dump_file)
912 fprintf (dump_file, "\nDeciding on smaller functions:\n");
914 /* Put all inline candidates into the heap. */
916 for (node = cgraph_nodes; node; node = node->next)
918 if (!node->local.inlinable || !node->callers
919 || node->local.disregard_inline_limits)
920 continue;
921 if (dump_file)
922 fprintf (dump_file, "Considering inline candidate %s.\n", cgraph_node_name (node));
924 node->global.estimated_growth = INT_MIN;
925 if (!cgraph_default_inline_p (node, &failed_reason))
927 cgraph_set_inline_failed (node, failed_reason);
928 continue;
931 for (edge = node->callers; edge; edge = edge->next_caller)
932 if (edge->inline_failed)
934 gcc_assert (!edge->aux);
935 edge->aux = fibheap_insert (heap, cgraph_edge_badness (edge), edge);
939 max_size = compute_max_insns (overall_size);
940 min_size = overall_size;
942 while (overall_size <= max_size
943 && (edge = (struct cgraph_edge *) fibheap_extract_min (heap)))
945 int old_size = overall_size;
946 struct cgraph_node *where;
947 int growth =
948 cgraph_estimate_size_after_inlining (1, edge->caller, edge->callee);
949 cgraph_inline_failed_t not_good = CIF_OK;
951 growth -= edge->caller->global.size;
953 if (dump_file)
955 fprintf (dump_file,
956 "\nConsidering %s with %i size\n",
957 cgraph_node_name (edge->callee),
958 edge->callee->global.size);
959 fprintf (dump_file,
960 " to be inlined into %s in %s:%i\n"
961 " Estimated growth after inlined into all callees is %+i insns.\n"
962 " Estimated badness is %i, frequency %.2f.\n",
963 cgraph_node_name (edge->caller),
964 gimple_filename ((const_gimple) edge->call_stmt),
965 gimple_lineno ((const_gimple) edge->call_stmt),
966 cgraph_estimate_growth (edge->callee),
967 cgraph_edge_badness (edge),
968 edge->frequency / (double)CGRAPH_FREQ_BASE);
969 if (edge->count)
970 fprintf (dump_file," Called "HOST_WIDEST_INT_PRINT_DEC"x\n", edge->count);
972 gcc_assert (edge->aux);
973 edge->aux = NULL;
974 if (!edge->inline_failed)
975 continue;
977 /* When not having profile info ready we don't weight by any way the
978 position of call in procedure itself. This means if call of
979 function A from function B seems profitable to inline, the recursive
980 call of function A in inline copy of A in B will look profitable too
981 and we end up inlining until reaching maximal function growth. This
982 is not good idea so prohibit the recursive inlining.
984 ??? When the frequencies are taken into account we might not need this
985 restriction.
987 We need to be cureful here, in some testcases, e.g. directivec.c in
988 libcpp, we can estimate self recursive function to have negative growth
989 for inlining completely.
991 if (!edge->count)
993 where = edge->caller;
994 while (where->global.inlined_to)
996 if (where->decl == edge->callee->decl)
997 break;
998 where = where->callers->caller;
1000 if (where->global.inlined_to)
1002 edge->inline_failed
1003 = (edge->callee->local.disregard_inline_limits
1004 ? CIF_RECURSIVE_INLINING : CIF_UNSPECIFIED);
1005 if (dump_file)
1006 fprintf (dump_file, " inline_failed:Recursive inlining performed only for function itself.\n");
1007 continue;
1011 if (!cgraph_maybe_hot_edge_p (edge))
1012 not_good = CIF_UNLIKELY_CALL;
1013 if (!flag_inline_functions
1014 && !DECL_DECLARED_INLINE_P (edge->callee->decl))
1015 not_good = CIF_NOT_DECLARED_INLINED;
1016 if (optimize_function_for_size_p (DECL_STRUCT_FUNCTION(edge->caller->decl)))
1017 not_good = CIF_OPTIMIZING_FOR_SIZE;
1018 if (not_good && growth > 0 && cgraph_estimate_growth (edge->callee) > 0)
1020 if (!cgraph_recursive_inlining_p (edge->caller, edge->callee,
1021 &edge->inline_failed))
1023 edge->inline_failed = not_good;
1024 if (dump_file)
1025 fprintf (dump_file, " inline_failed:%s.\n",
1026 cgraph_inline_failed_string (edge->inline_failed));
1028 continue;
1030 if (!cgraph_default_inline_p (edge->callee, &edge->inline_failed))
1032 if (!cgraph_recursive_inlining_p (edge->caller, edge->callee,
1033 &edge->inline_failed))
1035 if (dump_file)
1036 fprintf (dump_file, " inline_failed:%s.\n",
1037 cgraph_inline_failed_string (edge->inline_failed));
1039 continue;
1041 if (!tree_can_inline_p (edge))
1043 if (dump_file)
1044 fprintf (dump_file, " inline_failed:%s.\n",
1045 cgraph_inline_failed_string (edge->inline_failed));
1046 continue;
1048 if (cgraph_recursive_inlining_p (edge->caller, edge->callee,
1049 &edge->inline_failed))
1051 where = edge->caller;
1052 if (where->global.inlined_to)
1053 where = where->global.inlined_to;
1054 if (!cgraph_decide_recursive_inlining (where,
1055 flag_indirect_inlining
1056 ? &new_indirect_edges : NULL))
1057 continue;
1058 if (flag_indirect_inlining)
1059 add_new_edges_to_heap (heap, new_indirect_edges);
1060 update_callee_keys (heap, where, updated_nodes);
1062 else
1064 struct cgraph_node *callee;
1065 if (edge->call_stmt_cannot_inline_p
1066 || !cgraph_check_inline_limits (edge->caller, edge->callee,
1067 &edge->inline_failed, true))
1069 if (dump_file)
1070 fprintf (dump_file, " Not inlining into %s:%s.\n",
1071 cgraph_node_name (edge->caller),
1072 cgraph_inline_failed_string (edge->inline_failed));
1073 continue;
1075 callee = edge->callee;
1076 cgraph_mark_inline_edge (edge, true, &new_indirect_edges);
1077 if (flag_indirect_inlining)
1078 add_new_edges_to_heap (heap, new_indirect_edges);
1080 update_callee_keys (heap, callee, updated_nodes);
1082 where = edge->caller;
1083 if (where->global.inlined_to)
1084 where = where->global.inlined_to;
1086 /* Our profitability metric can depend on local properties
1087 such as number of inlinable calls and size of the function body.
1088 After inlining these properties might change for the function we
1089 inlined into (since it's body size changed) and for the functions
1090 called by function we inlined (since number of it inlinable callers
1091 might change). */
1092 update_caller_keys (heap, where, updated_nodes);
1093 bitmap_clear (updated_nodes);
1095 if (dump_file)
1097 fprintf (dump_file,
1098 " Inlined into %s which now has size %i and self time %i,"
1099 "net change of %+i.\n",
1100 cgraph_node_name (edge->caller),
1101 edge->caller->global.time,
1102 edge->caller->global.size,
1103 overall_size - old_size);
1105 if (min_size > overall_size)
1107 min_size = overall_size;
1108 max_size = compute_max_insns (min_size);
1110 if (dump_file)
1111 fprintf (dump_file, "New minimal size reached: %i\n", min_size);
1114 while ((edge = (struct cgraph_edge *) fibheap_extract_min (heap)) != NULL)
1116 gcc_assert (edge->aux);
1117 edge->aux = NULL;
1118 if (!edge->callee->local.disregard_inline_limits && edge->inline_failed
1119 && !cgraph_recursive_inlining_p (edge->caller, edge->callee,
1120 &edge->inline_failed))
1121 edge->inline_failed = CIF_INLINE_UNIT_GROWTH_LIMIT;
1124 if (new_indirect_edges)
1125 VEC_free (cgraph_edge_p, heap, new_indirect_edges);
1126 fibheap_delete (heap);
1127 BITMAP_FREE (updated_nodes);
1130 /* Decide on the inlining. We do so in the topological order to avoid
1131 expenses on updating data structures. */
1133 static unsigned int
1134 cgraph_decide_inlining (void)
1136 struct cgraph_node *node;
1137 int nnodes;
1138 struct cgraph_node **order =
1139 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1140 int old_size = 0;
1141 int i;
1142 bool redo_always_inline = true;
1143 int initial_size = 0;
1145 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
1146 if (in_lto_p && flag_indirect_inlining)
1147 ipa_update_after_lto_read ();
1149 max_count = 0;
1150 max_benefit = 0;
1151 for (node = cgraph_nodes; node; node = node->next)
1152 if (node->analyzed)
1154 struct cgraph_edge *e;
1156 gcc_assert (inline_summary (node)->self_size == node->global.size);
1157 initial_size += node->global.size;
1158 for (e = node->callees; e; e = e->next_callee)
1159 if (max_count < e->count)
1160 max_count = e->count;
1161 if (max_benefit < inline_summary (node)->time_inlining_benefit)
1162 max_benefit = inline_summary (node)->time_inlining_benefit;
1164 gcc_assert (in_lto_p
1165 || !max_count
1166 || (profile_info && flag_branch_probabilities));
1167 overall_size = initial_size;
1169 nnodes = cgraph_postorder (order);
1171 if (dump_file)
1172 fprintf (dump_file,
1173 "\nDeciding on inlining. Starting with size %i.\n",
1174 initial_size);
1176 for (node = cgraph_nodes; node; node = node->next)
1177 node->aux = 0;
1179 if (dump_file)
1180 fprintf (dump_file, "\nInlining always_inline functions:\n");
1182 /* In the first pass mark all always_inline edges. Do this with a priority
1183 so none of our later choices will make this impossible. */
1184 while (redo_always_inline)
1186 redo_always_inline = false;
1187 for (i = nnodes - 1; i >= 0; i--)
1189 struct cgraph_edge *e, *next;
1191 node = order[i];
1193 /* Handle nodes to be flattened, but don't update overall unit
1194 size. */
1195 if (lookup_attribute ("flatten",
1196 DECL_ATTRIBUTES (node->decl)) != NULL)
1198 if (dump_file)
1199 fprintf (dump_file,
1200 "Flattening %s\n", cgraph_node_name (node));
1201 cgraph_decide_inlining_incrementally (node, INLINE_ALL, 0);
1204 if (!node->local.disregard_inline_limits)
1205 continue;
1206 if (dump_file)
1207 fprintf (dump_file,
1208 "\nConsidering %s size:%i (always inline)\n",
1209 cgraph_node_name (node), node->global.size);
1210 old_size = overall_size;
1211 for (e = node->callers; e; e = next)
1213 next = e->next_caller;
1214 if (!e->inline_failed || e->call_stmt_cannot_inline_p)
1215 continue;
1216 if (cgraph_recursive_inlining_p (e->caller, e->callee,
1217 &e->inline_failed))
1218 continue;
1219 if (!tree_can_inline_p (e))
1220 continue;
1221 if (cgraph_mark_inline_edge (e, true, NULL))
1222 redo_always_inline = true;
1223 if (dump_file)
1224 fprintf (dump_file,
1225 " Inlined into %s which now has size %i.\n",
1226 cgraph_node_name (e->caller),
1227 e->caller->global.size);
1229 /* Inlining self recursive function might introduce new calls to
1230 themselves we didn't see in the loop above. Fill in the proper
1231 reason why inline failed. */
1232 for (e = node->callers; e; e = e->next_caller)
1233 if (e->inline_failed)
1234 e->inline_failed = CIF_RECURSIVE_INLINING;
1235 if (dump_file)
1236 fprintf (dump_file,
1237 " Inlined for a net change of %+i size.\n",
1238 overall_size - old_size);
1242 cgraph_decide_inlining_of_small_functions ();
1244 if (flag_inline_functions_called_once)
1246 if (dump_file)
1247 fprintf (dump_file, "\nDeciding on functions called once:\n");
1249 /* And finally decide what functions are called once. */
1250 for (i = nnodes - 1; i >= 0; i--)
1252 node = order[i];
1254 if (node->callers
1255 && !node->callers->next_caller
1256 && cgraph_only_called_directly_p (node)
1257 && node->local.inlinable
1258 && node->callers->inline_failed
1259 && node->callers->caller != node
1260 && node->callers->caller->global.inlined_to != node
1261 && !node->callers->call_stmt_cannot_inline_p
1262 && !DECL_EXTERNAL (node->decl)
1263 && !DECL_COMDAT (node->decl))
1265 cgraph_inline_failed_t reason;
1266 old_size = overall_size;
1267 if (dump_file)
1269 fprintf (dump_file,
1270 "\nConsidering %s size %i.\n",
1271 cgraph_node_name (node), node->global.size);
1272 fprintf (dump_file,
1273 " Called once from %s %i insns.\n",
1274 cgraph_node_name (node->callers->caller),
1275 node->callers->caller->global.size);
1278 if (cgraph_check_inline_limits (node->callers->caller, node,
1279 &reason, false))
1281 cgraph_mark_inline (node->callers);
1282 if (dump_file)
1283 fprintf (dump_file,
1284 " Inlined into %s which now has %i size"
1285 " for a net change of %+i size.\n",
1286 cgraph_node_name (node->callers->caller),
1287 node->callers->caller->global.size,
1288 overall_size - old_size);
1290 else
1292 if (dump_file)
1293 fprintf (dump_file,
1294 " Not inlining: %s.\n",
1295 cgraph_inline_failed_string (reason));
1301 /* Free ipa-prop structures if they are no longer needed. */
1302 if (flag_indirect_inlining)
1303 free_all_ipa_structures_after_iinln ();
1305 if (dump_file)
1306 fprintf (dump_file,
1307 "\nInlined %i calls, eliminated %i functions, "
1308 "size %i turned to %i size.\n\n",
1309 ncalls_inlined, nfunctions_inlined, initial_size,
1310 overall_size);
1311 free (order);
1312 return 0;
1315 /* Try to inline edge E from incremental inliner. MODE specifies mode
1316 of inliner.
1318 We are detecting cycles by storing mode of inliner into cgraph_node last
1319 time we visited it in the recursion. In general when mode is set, we have
1320 recursive inlining, but as an special case, we want to try harder inline
1321 ALWAYS_INLINE functions: consider callgraph a->b->c->b, with a being
1322 flatten, b being always inline. Flattening 'a' will collapse
1323 a->b->c before hitting cycle. To accommodate always inline, we however
1324 need to inline a->b->c->b.
1326 So after hitting cycle first time, we switch into ALWAYS_INLINE mode and
1327 stop inlining only after hitting ALWAYS_INLINE in ALWAY_INLINE mode. */
1328 static bool
1329 try_inline (struct cgraph_edge *e, enum inlining_mode mode, int depth)
1331 struct cgraph_node *callee = e->callee;
1332 enum inlining_mode callee_mode = (enum inlining_mode) (size_t) callee->aux;
1333 bool always_inline = e->callee->local.disregard_inline_limits;
1334 bool inlined = false;
1336 /* We've hit cycle? */
1337 if (callee_mode)
1339 /* It is first time we see it and we are not in ALWAY_INLINE only
1340 mode yet. and the function in question is always_inline. */
1341 if (always_inline && mode != INLINE_ALWAYS_INLINE)
1343 if (dump_file)
1345 indent_to (dump_file, depth);
1346 fprintf (dump_file,
1347 "Hit cycle in %s, switching to always inline only.\n",
1348 cgraph_node_name (callee));
1350 mode = INLINE_ALWAYS_INLINE;
1352 /* Otherwise it is time to give up. */
1353 else
1355 if (dump_file)
1357 indent_to (dump_file, depth);
1358 fprintf (dump_file,
1359 "Not inlining %s into %s to avoid cycle.\n",
1360 cgraph_node_name (callee),
1361 cgraph_node_name (e->caller));
1363 e->inline_failed = (e->callee->local.disregard_inline_limits
1364 ? CIF_RECURSIVE_INLINING : CIF_UNSPECIFIED);
1365 return false;
1369 callee->aux = (void *)(size_t) mode;
1370 if (dump_file)
1372 indent_to (dump_file, depth);
1373 fprintf (dump_file, " Inlining %s into %s.\n",
1374 cgraph_node_name (e->callee),
1375 cgraph_node_name (e->caller));
1377 if (e->inline_failed)
1379 cgraph_mark_inline (e);
1381 /* In order to fully inline always_inline functions, we need to
1382 recurse here, since the inlined functions might not be processed by
1383 incremental inlining at all yet.
1385 Also flattening needs to be done recursively. */
1387 if (mode == INLINE_ALL || always_inline)
1388 cgraph_decide_inlining_incrementally (e->callee, mode, depth + 1);
1389 inlined = true;
1391 callee->aux = (void *)(size_t) callee_mode;
1392 return inlined;
1395 /* Return true when N is leaf function. Accept cheap (pure&const) builtins
1396 in leaf functions. */
1397 static bool
1398 leaf_node_p (struct cgraph_node *n)
1400 struct cgraph_edge *e;
1401 for (e = n->callees; e; e = e->next_callee)
1402 if (!DECL_BUILT_IN (e->callee->decl)
1403 || (!TREE_READONLY (e->callee->decl)
1404 || DECL_PURE_P (e->callee->decl)))
1405 return false;
1406 return true;
1409 /* Decide on the inlining. We do so in the topological order to avoid
1410 expenses on updating data structures.
1411 DEPTH is depth of recursion, used only for debug output. */
1413 static bool
1414 cgraph_decide_inlining_incrementally (struct cgraph_node *node,
1415 enum inlining_mode mode,
1416 int depth)
1418 struct cgraph_edge *e;
1419 bool inlined = false;
1420 cgraph_inline_failed_t failed_reason;
1421 enum inlining_mode old_mode;
1423 #ifdef ENABLE_CHECKING
1424 verify_cgraph_node (node);
1425 #endif
1427 old_mode = (enum inlining_mode) (size_t)node->aux;
1429 if (mode != INLINE_ALWAYS_INLINE && mode != INLINE_SIZE_NORECURSIVE
1430 && lookup_attribute ("flatten", DECL_ATTRIBUTES (node->decl)) != NULL)
1432 if (dump_file)
1434 indent_to (dump_file, depth);
1435 fprintf (dump_file, "Flattening %s\n", cgraph_node_name (node));
1437 mode = INLINE_ALL;
1440 node->aux = (void *)(size_t) mode;
1442 /* First of all look for always inline functions. */
1443 if (mode != INLINE_SIZE_NORECURSIVE)
1444 for (e = node->callees; e; e = e->next_callee)
1446 if (!e->callee->local.disregard_inline_limits
1447 && (mode != INLINE_ALL || !e->callee->local.inlinable))
1448 continue;
1449 if (e->call_stmt_cannot_inline_p)
1450 continue;
1451 /* When the edge is already inlined, we just need to recurse into
1452 it in order to fully flatten the leaves. */
1453 if (!e->inline_failed && mode == INLINE_ALL)
1455 inlined |= try_inline (e, mode, depth);
1456 continue;
1458 if (dump_file)
1460 indent_to (dump_file, depth);
1461 fprintf (dump_file,
1462 "Considering to always inline inline candidate %s.\n",
1463 cgraph_node_name (e->callee));
1465 if (cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed))
1467 if (dump_file)
1469 indent_to (dump_file, depth);
1470 fprintf (dump_file, "Not inlining: recursive call.\n");
1472 continue;
1474 if (!tree_can_inline_p (e))
1476 if (dump_file)
1478 indent_to (dump_file, depth);
1479 fprintf (dump_file,
1480 "Not inlining: %s",
1481 cgraph_inline_failed_string (e->inline_failed));
1483 continue;
1485 if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
1486 != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->callee->decl)))
1488 if (dump_file)
1490 indent_to (dump_file, depth);
1491 fprintf (dump_file, "Not inlining: SSA form does not match.\n");
1493 continue;
1495 if (!e->callee->analyzed)
1497 if (dump_file)
1499 indent_to (dump_file, depth);
1500 fprintf (dump_file,
1501 "Not inlining: Function body no longer available.\n");
1503 continue;
1505 inlined |= try_inline (e, mode, depth);
1508 /* Now do the automatic inlining. */
1509 if (mode != INLINE_ALL && mode != INLINE_ALWAYS_INLINE
1510 /* Never inline regular functions into always-inline functions
1511 during incremental inlining. */
1512 && !node->local.disregard_inline_limits)
1513 for (e = node->callees; e; e = e->next_callee)
1515 int allowed_growth = 0;
1516 if (!e->callee->local.inlinable
1517 || !e->inline_failed
1518 || e->callee->local.disregard_inline_limits)
1519 continue;
1520 if (dump_file)
1521 fprintf (dump_file, "Considering inline candidate %s.\n",
1522 cgraph_node_name (e->callee));
1523 if (cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed))
1525 if (dump_file)
1527 indent_to (dump_file, depth);
1528 fprintf (dump_file, "Not inlining: recursive call.\n");
1530 continue;
1532 if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
1533 != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->callee->decl)))
1535 if (dump_file)
1537 indent_to (dump_file, depth);
1538 fprintf (dump_file, "Not inlining: SSA form does not match.\n");
1540 continue;
1543 if (cgraph_maybe_hot_edge_p (e) && leaf_node_p (e->callee)
1544 && optimize_function_for_speed_p (cfun))
1545 allowed_growth = PARAM_VALUE (PARAM_EARLY_INLINING_INSNS);
1547 /* When the function body would grow and inlining the function won't
1548 eliminate the need for offline copy of the function, don't inline.
1550 if (((mode == INLINE_SIZE || mode == INLINE_SIZE_NORECURSIVE)
1551 || (!flag_inline_functions
1552 && !DECL_DECLARED_INLINE_P (e->callee->decl)))
1553 && (cgraph_estimate_size_after_inlining (1, e->caller, e->callee)
1554 > e->caller->global.size + allowed_growth)
1555 && cgraph_estimate_growth (e->callee) > allowed_growth)
1557 if (dump_file)
1559 indent_to (dump_file, depth);
1560 fprintf (dump_file,
1561 "Not inlining: code size would grow by %i.\n",
1562 cgraph_estimate_size_after_inlining (1, e->caller,
1563 e->callee)
1564 - e->caller->global.size);
1566 continue;
1568 if (!cgraph_check_inline_limits (node, e->callee, &e->inline_failed,
1569 false)
1570 || e->call_stmt_cannot_inline_p)
1572 if (dump_file)
1574 indent_to (dump_file, depth);
1575 fprintf (dump_file, "Not inlining: %s.\n",
1576 cgraph_inline_failed_string (e->inline_failed));
1578 continue;
1580 if (!e->callee->analyzed)
1582 if (dump_file)
1584 indent_to (dump_file, depth);
1585 fprintf (dump_file,
1586 "Not inlining: Function body no longer available.\n");
1588 continue;
1590 if (!tree_can_inline_p (e))
1592 if (dump_file)
1594 indent_to (dump_file, depth);
1595 fprintf (dump_file,
1596 "Not inlining: %s.",
1597 cgraph_inline_failed_string (e->inline_failed));
1599 continue;
1601 if (cgraph_default_inline_p (e->callee, &failed_reason))
1602 inlined |= try_inline (e, mode, depth);
1604 node->aux = (void *)(size_t) old_mode;
1605 return inlined;
1608 /* Because inlining might remove no-longer reachable nodes, we need to
1609 keep the array visible to garbage collector to avoid reading collected
1610 out nodes. */
1611 static int nnodes;
1612 static GTY ((length ("nnodes"))) struct cgraph_node **order;
1614 /* Do inlining of small functions. Doing so early helps profiling and other
1615 passes to be somewhat more effective and avoids some code duplication in
1616 later real inlining pass for testcases with very many function calls. */
1617 static unsigned int
1618 cgraph_early_inlining (void)
1620 struct cgraph_node *node = cgraph_node (current_function_decl);
1621 unsigned int todo = 0;
1622 int iterations = 0;
1624 if (sorrycount || errorcount)
1625 return 0;
1626 while (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS)
1627 && cgraph_decide_inlining_incrementally (node,
1628 iterations
1629 ? INLINE_SIZE_NORECURSIVE : INLINE_SIZE, 0))
1631 timevar_push (TV_INTEGRATION);
1632 todo |= optimize_inline_calls (current_function_decl);
1633 iterations++;
1634 timevar_pop (TV_INTEGRATION);
1636 if (dump_file)
1637 fprintf (dump_file, "Iterations: %i\n", iterations);
1638 cfun->always_inline_functions_inlined = true;
1639 return todo;
1642 /* When inlining shall be performed. */
1643 static bool
1644 cgraph_gate_early_inlining (void)
1646 return flag_early_inlining;
1649 struct gimple_opt_pass pass_early_inline =
1652 GIMPLE_PASS,
1653 "einline", /* name */
1654 cgraph_gate_early_inlining, /* gate */
1655 cgraph_early_inlining, /* execute */
1656 NULL, /* sub */
1657 NULL, /* next */
1658 0, /* static_pass_number */
1659 TV_INLINE_HEURISTICS, /* tv_id */
1660 0, /* properties_required */
1661 0, /* properties_provided */
1662 0, /* properties_destroyed */
1663 0, /* todo_flags_start */
1664 TODO_dump_func /* todo_flags_finish */
1668 /* When inlining shall be performed. */
1669 static bool
1670 cgraph_gate_ipa_early_inlining (void)
1672 return (flag_early_inlining
1673 && !in_lto_p
1674 && (flag_branch_probabilities || flag_test_coverage
1675 || profile_arc_flag));
1678 /* IPA pass wrapper for early inlining pass. We need to run early inlining
1679 before tree profiling so we have stand alone IPA pass for doing so. */
1680 struct simple_ipa_opt_pass pass_ipa_early_inline =
1683 SIMPLE_IPA_PASS,
1684 "einline_ipa", /* name */
1685 cgraph_gate_ipa_early_inlining, /* gate */
1686 NULL, /* execute */
1687 NULL, /* sub */
1688 NULL, /* next */
1689 0, /* static_pass_number */
1690 TV_INLINE_HEURISTICS, /* tv_id */
1691 0, /* properties_required */
1692 0, /* properties_provided */
1693 0, /* properties_destroyed */
1694 0, /* todo_flags_start */
1695 TODO_dump_cgraph /* todo_flags_finish */
1699 /* See if statement might disappear after inlining. We are not terribly
1700 sophisficated, basically looking for simple abstraction penalty wrappers. */
1702 static bool
1703 likely_eliminated_by_inlining_p (gimple stmt)
1705 enum gimple_code code = gimple_code (stmt);
1706 switch (code)
1708 case GIMPLE_RETURN:
1709 return true;
1710 case GIMPLE_ASSIGN:
1711 if (gimple_num_ops (stmt) != 2)
1712 return false;
1714 /* Casts of parameters, loads from parameters passed by reference
1715 and stores to return value or parameters are probably free after
1716 inlining. */
1717 if (gimple_assign_rhs_code (stmt) == CONVERT_EXPR
1718 || gimple_assign_rhs_code (stmt) == NOP_EXPR
1719 || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
1720 || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1722 tree rhs = gimple_assign_rhs1 (stmt);
1723 tree lhs = gimple_assign_lhs (stmt);
1724 tree inner_rhs = rhs;
1725 tree inner_lhs = lhs;
1726 bool rhs_free = false;
1727 bool lhs_free = false;
1729 while (handled_component_p (inner_lhs) || TREE_CODE (inner_lhs) == INDIRECT_REF)
1730 inner_lhs = TREE_OPERAND (inner_lhs, 0);
1731 while (handled_component_p (inner_rhs)
1732 || TREE_CODE (inner_rhs) == ADDR_EXPR || TREE_CODE (inner_rhs) == INDIRECT_REF)
1733 inner_rhs = TREE_OPERAND (inner_rhs, 0);
1736 if (TREE_CODE (inner_rhs) == PARM_DECL
1737 || (TREE_CODE (inner_rhs) == SSA_NAME
1738 && SSA_NAME_IS_DEFAULT_DEF (inner_rhs)
1739 && TREE_CODE (SSA_NAME_VAR (inner_rhs)) == PARM_DECL))
1740 rhs_free = true;
1741 if (rhs_free && is_gimple_reg (lhs))
1742 lhs_free = true;
1743 if (((TREE_CODE (inner_lhs) == PARM_DECL
1744 || (TREE_CODE (inner_lhs) == SSA_NAME
1745 && SSA_NAME_IS_DEFAULT_DEF (inner_lhs)
1746 && TREE_CODE (SSA_NAME_VAR (inner_lhs)) == PARM_DECL))
1747 && inner_lhs != lhs)
1748 || TREE_CODE (inner_lhs) == RESULT_DECL
1749 || (TREE_CODE (inner_lhs) == SSA_NAME
1750 && TREE_CODE (SSA_NAME_VAR (inner_lhs)) == RESULT_DECL))
1751 lhs_free = true;
1752 if (lhs_free && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1753 rhs_free = true;
1754 if (lhs_free && rhs_free)
1755 return true;
1757 return false;
1758 default:
1759 return false;
1763 /* Compute function body size parameters for NODE. */
1765 static void
1766 estimate_function_body_sizes (struct cgraph_node *node)
1768 gcov_type time = 0;
1769 gcov_type time_inlining_benefit = 0;
1770 int size = 0;
1771 int size_inlining_benefit = 0;
1772 basic_block bb;
1773 gimple_stmt_iterator bsi;
1774 struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1775 tree arg;
1776 int freq;
1777 tree funtype = TREE_TYPE (node->decl);
1779 if (dump_file)
1780 fprintf (dump_file, "Analyzing function body size: %s\n",
1781 cgraph_node_name (node));
1783 gcc_assert (my_function && my_function->cfg);
1784 FOR_EACH_BB_FN (bb, my_function)
1786 freq = compute_call_stmt_bb_frequency (node->decl, bb);
1787 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1789 gimple stmt = gsi_stmt (bsi);
1790 int this_size = estimate_num_insns (stmt, &eni_size_weights);
1791 int this_time = estimate_num_insns (stmt, &eni_time_weights);
1793 if (dump_file && (dump_flags & TDF_DETAILS))
1795 fprintf (dump_file, " freq:%6i size:%3i time:%3i ",
1796 freq, this_size, this_time);
1797 print_gimple_stmt (dump_file, stmt, 0, 0);
1799 this_time *= freq;
1800 time += this_time;
1801 size += this_size;
1802 if (likely_eliminated_by_inlining_p (stmt))
1804 size_inlining_benefit += this_size;
1805 time_inlining_benefit += this_time;
1806 if (dump_file && (dump_flags & TDF_DETAILS))
1807 fprintf (dump_file, " Likely eliminated\n");
1809 gcc_assert (time >= 0);
1810 gcc_assert (size >= 0);
1813 time = (time + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
1814 time_inlining_benefit = ((time_inlining_benefit + CGRAPH_FREQ_BASE / 2)
1815 / CGRAPH_FREQ_BASE);
1816 if (dump_file)
1817 fprintf (dump_file, "Overall function body time: %i-%i size: %i-%i\n",
1818 (int)time, (int)time_inlining_benefit,
1819 size, size_inlining_benefit);
1820 time_inlining_benefit += eni_time_weights.call_cost;
1821 size_inlining_benefit += eni_size_weights.call_cost;
1822 if (!VOID_TYPE_P (TREE_TYPE (funtype)))
1824 int cost = estimate_move_cost (TREE_TYPE (funtype));
1825 time_inlining_benefit += cost;
1826 size_inlining_benefit += cost;
1828 for (arg = DECL_ARGUMENTS (node->decl); arg; arg = TREE_CHAIN (arg))
1829 if (!VOID_TYPE_P (TREE_TYPE (arg)))
1831 int cost = estimate_move_cost (TREE_TYPE (arg));
1832 time_inlining_benefit += cost;
1833 size_inlining_benefit += cost;
1835 if (time_inlining_benefit > MAX_TIME)
1836 time_inlining_benefit = MAX_TIME;
1837 if (time > MAX_TIME)
1838 time = MAX_TIME;
1839 inline_summary (node)->self_time = time;
1840 inline_summary (node)->self_size = size;
1841 if (dump_file)
1842 fprintf (dump_file, "With function call overhead time: %i-%i size: %i-%i\n",
1843 (int)time, (int)time_inlining_benefit,
1844 size, size_inlining_benefit);
1845 inline_summary (node)->time_inlining_benefit = time_inlining_benefit;
1846 inline_summary (node)->size_inlining_benefit = size_inlining_benefit;
1849 /* Compute parameters of functions used by inliner. */
1850 unsigned int
1851 compute_inline_parameters (struct cgraph_node *node)
1853 HOST_WIDE_INT self_stack_size;
1855 gcc_assert (!node->global.inlined_to);
1857 /* Estimate the stack size for the function. But not at -O0
1858 because estimated_stack_frame_size is a quadratic problem. */
1859 self_stack_size = optimize ? estimated_stack_frame_size () : 0;
1860 inline_summary (node)->estimated_self_stack_size = self_stack_size;
1861 node->global.estimated_stack_size = self_stack_size;
1862 node->global.stack_frame_offset = 0;
1864 /* Can this function be inlined at all? */
1865 node->local.inlinable = tree_inlinable_function_p (node->decl);
1866 if (node->local.inlinable && !node->local.disregard_inline_limits)
1867 node->local.disregard_inline_limits
1868 = DECL_DISREGARD_INLINE_LIMITS (node->decl);
1869 estimate_function_body_sizes (node);
1870 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
1871 node->global.time = inline_summary (node)->self_time;
1872 node->global.size = inline_summary (node)->self_size;
1873 return 0;
1877 /* Compute parameters of functions used by inliner using
1878 current_function_decl. */
1879 static unsigned int
1880 compute_inline_parameters_for_current (void)
1882 compute_inline_parameters (cgraph_node (current_function_decl));
1883 return 0;
1886 struct gimple_opt_pass pass_inline_parameters =
1889 GIMPLE_PASS,
1890 "inline_param", /* name */
1891 NULL, /* gate */
1892 compute_inline_parameters_for_current,/* execute */
1893 NULL, /* sub */
1894 NULL, /* next */
1895 0, /* static_pass_number */
1896 TV_INLINE_HEURISTICS, /* tv_id */
1897 0, /* properties_required */
1898 0, /* properties_provided */
1899 0, /* properties_destroyed */
1900 0, /* todo_flags_start */
1901 0 /* todo_flags_finish */
1905 /* This function performs intraprocedural analyzis in NODE that is required to
1906 inline indirect calls. */
1907 static void
1908 inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
1910 struct cgraph_edge *cs;
1912 if (!flag_ipa_cp)
1914 ipa_initialize_node_params (node);
1915 ipa_detect_param_modifications (node);
1917 ipa_analyze_params_uses (node);
1919 if (!flag_ipa_cp)
1920 for (cs = node->callees; cs; cs = cs->next_callee)
1922 ipa_count_arguments (cs);
1923 ipa_compute_jump_functions (cs);
1926 if (dump_file)
1928 ipa_print_node_params (dump_file, node);
1929 ipa_print_node_jump_functions (dump_file, node);
1933 /* Note function body size. */
1934 static void
1935 analyze_function (struct cgraph_node *node)
1937 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1938 current_function_decl = node->decl;
1940 compute_inline_parameters (node);
1941 if (flag_indirect_inlining)
1942 inline_indirect_intraprocedural_analysis (node);
1944 current_function_decl = NULL;
1945 pop_cfun ();
1948 /* Called when new function is inserted to callgraph late. */
1949 static void
1950 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1952 analyze_function (node);
1955 /* Note function body size. */
1956 static void
1957 inline_generate_summary (void)
1959 struct cgraph_node *node;
1961 function_insertion_hook_holder =
1962 cgraph_add_function_insertion_hook (&add_new_function, NULL);
1964 if (flag_indirect_inlining)
1966 ipa_register_cgraph_hooks ();
1967 ipa_check_create_node_params ();
1968 ipa_check_create_edge_args ();
1971 for (node = cgraph_nodes; node; node = node->next)
1972 if (node->analyzed)
1973 analyze_function (node);
1975 return;
1978 /* Apply inline plan to function. */
1979 static unsigned int
1980 inline_transform (struct cgraph_node *node)
1982 unsigned int todo = 0;
1983 struct cgraph_edge *e;
1985 /* FIXME: Currently the passmanager is adding inline transform more than once to some
1986 clones. This needs revisiting after WPA cleanups. */
1987 if (cfun->after_inlining)
1988 return 0;
1990 /* We might need the body of this function so that we can expand
1991 it inline somewhere else. */
1992 if (cgraph_preserve_function_body_p (node->decl))
1993 save_inline_function_body (node);
1995 for (e = node->callees; e; e = e->next_callee)
1996 if (!e->inline_failed || warn_inline)
1997 break;
1999 if (e)
2001 timevar_push (TV_INTEGRATION);
2002 todo = optimize_inline_calls (current_function_decl);
2003 timevar_pop (TV_INTEGRATION);
2005 cfun->always_inline_functions_inlined = true;
2006 cfun->after_inlining = true;
2007 return todo | execute_fixup_cfg ();
2010 /* Read inline summary. Jump functions are shared among ipa-cp
2011 and inliner, so when ipa-cp is active, we don't need to write them
2012 twice. */
2014 static void
2015 inline_read_summary (void)
2017 if (flag_indirect_inlining)
2019 ipa_register_cgraph_hooks ();
2020 if (!flag_ipa_cp)
2021 ipa_prop_read_jump_functions ();
2023 function_insertion_hook_holder =
2024 cgraph_add_function_insertion_hook (&add_new_function, NULL);
2027 /* Write inline summary for node in SET.
2028 Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
2029 active, we don't need to write them twice. */
2031 static void
2032 inline_write_summary (cgraph_node_set set)
2034 if (flag_indirect_inlining && !flag_ipa_cp)
2035 ipa_prop_write_jump_functions (set);
2038 struct ipa_opt_pass_d pass_ipa_inline =
2041 IPA_PASS,
2042 "inline", /* name */
2043 NULL, /* gate */
2044 cgraph_decide_inlining, /* execute */
2045 NULL, /* sub */
2046 NULL, /* next */
2047 0, /* static_pass_number */
2048 TV_INLINE_HEURISTICS, /* tv_id */
2049 0, /* properties_required */
2050 0, /* properties_provided */
2051 0, /* properties_destroyed */
2052 TODO_remove_functions, /* todo_flags_finish */
2053 TODO_dump_cgraph | TODO_dump_func
2054 | TODO_remove_functions /* todo_flags_finish */
2056 inline_generate_summary, /* generate_summary */
2057 inline_write_summary, /* write_summary */
2058 inline_read_summary, /* read_summary */
2059 NULL, /* function_read_summary */
2060 lto_ipa_fixup_call_notes, /* stmt_fixup */
2061 0, /* TODOs */
2062 inline_transform, /* function_transform */
2063 NULL, /* variable_transform */
2067 #include "gt-ipa-inline.h"