Merge from trunk: 215733-215743
[official-gcc.git] / gcc-4_6_3-mobile / gcc / ipa-inline.c
blob154d8051d45a92f4af4170d2070023c79e12bb6d
1 /* Inlining decision heuristics.
2 Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Inlining decision heuristics
24 We separate inlining decisions from the inliner itself and store it
25 inside callgraph as so called inline plan. Refer to cgraph.c
26 documentation about particular representation of inline plans in the
27 callgraph.
29 There are three major parts of this file:
31 cgraph_mark_inline_edge implementation
33 This function allows to mark given call inline and performs necessary
34 modifications of cgraph (production of the clones and updating overall
35 statistics)
37 inlining heuristics limits
39 These functions allow to check that particular inlining is allowed
40 by the limits specified by user (allowed function growth, overall unit
41 growth and so on).
43 inlining heuristics
45 This is implementation of IPA pass aiming to get as much of benefit
46 from inlining obeying the limits checked above.
48 The implementation of particular heuristics is separated from
49 the rest of code to make it easier to replace it with more complicated
50 implementation in the future. The rest of inlining code acts as a
51 library aimed to modify the callgraph and verify that the parameters
52 on code size growth fits.
54 To mark given call inline, use cgraph_mark_inline function, the
55 verification is performed by cgraph_default_inline_p and
56 cgraph_check_inline_limits.
58 The heuristics implements simple knapsack style algorithm ordering
59 all functions by their "profitability" (estimated by code size growth)
60 and inlining them in priority order.
62 cgraph_decide_inlining implements heuristics taking whole callgraph
63 into account, while cgraph_decide_inlining_incrementally considers
64 only one function at a time and is used by early inliner.
66 The inliner itself is split into several passes:
68 pass_inline_parameters
70 This pass computes local properties of functions that are used by inliner:
71 estimated function body size, whether function is inlinable at all and
72 stack frame consumption.
74 Before executing any of inliner passes, this local pass has to be applied
75 to each function in the callgraph (ie run as subpass of some earlier
76 IPA pass). The results are made out of date by any optimization applied
77 on the function body.
79 pass_early_inlining
81 Simple local inlining pass inlining callees into current function. This
82 pass makes no global whole compilation unit analysis and this when allowed
83 to do inlining expanding code size it might result in unbounded growth of
84 whole unit.
86 The pass is run during conversion into SSA form. Only functions already
87 converted into SSA form are inlined, so the conversion must happen in
88 topological order on the callgraph (that is maintained by pass manager).
89 The functions after inlining are early optimized so the early inliner sees
90 unoptimized function itself, but all considered callees are already
91 optimized allowing it to unfold abstraction penalty on C++ effectively and
92 cheaply.
94 pass_ipa_inline
96 This is the main pass implementing simple greedy algorithm to do inlining
97 of small functions that results in overall growth of compilation unit and
98 inlining of functions called once. The pass compute just so called inline
99 plan (representation of inlining to be done in callgraph) and unlike early
100 inlining it is not performing the inlining itself.
103 #include "config.h"
104 #include "system.h"
105 #include "coretypes.h"
106 #include "tm.h"
107 #include "tree.h"
108 #include "tree-inline.h"
109 #include "langhooks.h"
110 #include "flags.h"
111 #include "cgraph.h"
112 #include "diagnostic.h"
113 #include "gimple-pretty-print.h"
114 #include "timevar.h"
115 #include "params.h"
116 #include "fibheap.h"
117 #include "intl.h"
118 #include "tree-pass.h"
119 #include "hashtab.h"
120 #include "coverage.h"
121 #include "ggc.h"
122 #include "tree-flow.h"
123 #include "rtl.h"
124 #include "ipa-prop.h"
125 #include "basic-block.h"
126 #include "toplev.h"
127 #include "dbgcnt.h"
128 #include "except.h"
129 #include "l-ipo.h"
131 #define MAX_TIME 1000000000
133 /* Mode incremental inliner operate on:
135 In ALWAYS_INLINE only functions marked
136 always_inline are inlined. This mode is used after detecting cycle during
137 flattening.
139 In SIZE mode, only functions that reduce function body size after inlining
140 are inlined, this is used during early inlining.
142 in ALL mode, everything is inlined. This is used during flattening. */
143 enum inlining_mode {
144 INLINE_NONE = 0,
145 INLINE_ALWAYS_INLINE,
146 INLINE_SIZE_NORECURSIVE,
147 INLINE_SIZE,
148 INLINE_ALL
151 static bool
152 cgraph_decide_inlining_incrementally (struct cgraph_node *, enum inlining_mode);
153 static void cgraph_flatten (struct cgraph_node *node);
156 /* Statistics we collect about inlining algorithm. */
157 static int ncalls_inlined;
158 static int nfunctions_inlined;
159 static int overall_size;
160 static gcov_type max_count, max_benefit;
162 /* Holders of ipa cgraph hooks: */
163 static struct cgraph_node_hook_list *function_insertion_hook_holder;
165 static inline struct inline_summary *
166 inline_summary (struct cgraph_node *node)
168 return &node->local.inline_summary;
171 /* Estimate self time of the function after inlining WHAT into TO. */
173 static int
174 cgraph_estimate_time_after_inlining (int frequency, struct cgraph_node *to,
175 struct cgraph_node *what)
177 gcov_type time = (((gcov_type)what->global.time
178 - inline_summary (what)->time_inlining_benefit)
179 * frequency + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE
180 + to->global.time;
181 if (time < 0)
182 time = 0;
183 if (time > MAX_TIME)
184 time = MAX_TIME;
185 return time;
188 /* Estimate self size of the function after inlining WHAT into TO. */
190 static inline int
191 cgraph_estimate_size_after_inlining (struct cgraph_node *to,
192 struct cgraph_node *what)
194 int size = ((what->global.size - inline_summary (what)->size_inlining_benefit)
195 + to->global.size);
196 gcc_assert (size >= 0);
197 return size;
200 /* Scale frequency of NODE edges by FREQ_SCALE and increase loop nest
201 by NEST. */
203 static void
204 update_noncloned_frequencies (struct cgraph_node *node,
205 int freq_scale, int nest)
207 struct cgraph_edge *e;
209 /* We do not want to ignore high loop nest after freq drops to 0. */
210 if (!freq_scale)
211 freq_scale = 1;
212 for (e = node->callees; e; e = e->next_callee)
214 e->loop_nest += nest;
215 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
216 if (e->frequency > CGRAPH_FREQ_MAX)
217 e->frequency = CGRAPH_FREQ_MAX;
218 if (!e->inline_failed)
219 update_noncloned_frequencies (e->callee, freq_scale, nest);
223 /* E is expected to be an edge being inlined. Clone destination node of
224 the edge and redirect it to the new clone.
225 DUPLICATE is used for bookkeeping on whether we are actually creating new
226 clones or re-using node originally representing out-of-line function call.
228 void
229 cgraph_clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
230 bool update_original)
232 if (duplicate)
234 /* We may eliminate the need for out-of-line copy to be output.
235 In that case just go ahead and re-use it. */
236 if (!e->callee->callers->next_caller
237 /* Recursive inlining never wants the master clone to be overwritten. */
238 && update_original
239 /* FIXME: When address is taken of DECL_EXTERNAL function we still can remove its
240 offline copy, but we would need to keep unanalyzed node in the callgraph so
241 references can point to it. */
242 && !e->callee->address_taken
243 && cgraph_can_remove_if_no_direct_calls_p (e->callee)
244 /* Inlining might enable more devirtualizing, so we want to remove
245 those only after all devirtualizable virtual calls are processed.
246 Lacking may edges in callgraph we just preserve them post
247 inlining. */
248 && (!DECL_VIRTUAL_P (e->callee->decl)
249 || (!DECL_COMDAT (e->callee->decl) && !DECL_EXTERNAL (e->callee->decl)))
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 && !DECL_EXTERNAL (e->callee->decl))
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->callee->decl,
270 e->count, e->frequency, e->loop_nest,
271 update_original, NULL);
272 cgraph_redirect_edge_callee (e, n);
276 if (e->caller->global.inlined_to)
277 e->callee->global.inlined_to = e->caller->global.inlined_to;
278 else
279 e->callee->global.inlined_to = e->caller;
281 /* Pessimistically assume no sharing of stack space. That is, the
282 frame size of a function is estimated as the original frame size
283 plus the sum of the frame sizes of all inlined callees. */
284 e->callee->global.inlined_to->global.estimated_stack_size +=
285 inline_summary (e->callee)->estimated_self_stack_size;
287 cgraph_propagate_frequency (e->callee);
289 /* Recursively clone all bodies. */
290 for (e = e->callee->callees; e; e = e->next_callee)
291 if (!e->inline_failed)
292 cgraph_clone_inlined_nodes (e, duplicate, update_original);
295 #define MAX_INT_LENGTH 16
297 /* Return NODE's name and aux info. The output is controled by OPT_INFO
298 level. */
300 static const char *
301 cgraph_node_opt_info (struct cgraph_node *node)
303 char *buf;
304 size_t buf_size;
305 const char *bfd_name = lang_hooks.dwarf_name (node->decl, 0);
307 if (!bfd_name)
308 bfd_name = "unknown";
310 buf_size = strlen (bfd_name) + 1;
311 if (profile_info)
312 buf_size += (2 * MAX_INT_LENGTH + 5);
313 buf = (char *) xmalloc (buf_size);
315 strcpy (buf, bfd_name);
316 if (profile_info)
317 sprintf (buf,
318 "%s ("HOST_WIDEST_INT_PRINT_DEC", "HOST_WIDEST_INT_PRINT_DEC")",
319 buf, node->count, node->max_bb_count);
320 return buf;
323 /* Return CALLER's inlined call chain. Save the cgraph_node of the ultimate
324 function that the caller is inlined to in FINAL_CALLER. */
326 static const char *
327 cgraph_node_call_chain (struct cgraph_node *caller,
328 struct cgraph_node **final_caller)
330 struct cgraph_node *node;
331 const char *via_str = " (via inline instance";
332 size_t current_string_len = strlen (via_str) + 1;
333 size_t buf_size = current_string_len;
334 char *buf = (char *) xmalloc (buf_size);
336 buf[0] = 0;
337 gcc_assert (caller->global.inlined_to != NULL);
338 strcat (buf, via_str);
339 for (node = caller; node->global.inlined_to != NULL;
340 node = node->callers->caller)
342 const char *name = cgraph_node_opt_info (node);
343 current_string_len += (strlen (name) + 1);
344 if (current_string_len >= buf_size)
346 buf_size = current_string_len * 2;
347 buf = (char *) xrealloc (buf, buf_size);
349 strcat (buf, " ");
350 strcat (buf, name);
352 strcat (buf, ")");
353 *final_caller = node;
354 return buf;
357 /* File static variable to denote if it is in ipa-inline pass. */
358 static bool is_in_ipa_inline = false;
360 /* Dump the inline decision of EDGE to stderr. */
362 static void
363 dump_inline_decision (struct cgraph_edge *edge)
365 location_t locus;
366 const char *inline_chain_text;
367 const char *call_count_text;
368 struct cgraph_node *final_caller = edge->caller;
370 if (flag_opt_info < OPT_INFO_MED && !is_in_ipa_inline)
371 return;
372 if (final_caller->global.inlined_to != NULL)
373 inline_chain_text = cgraph_node_call_chain (final_caller, &final_caller);
374 else
375 inline_chain_text = "";
377 if (edge->count > 0)
379 const char *call_count_str = " with call count ";
380 char *buf = (char *) xmalloc (strlen (call_count_str) + MAX_INT_LENGTH);
381 sprintf (buf, "%s"HOST_WIDEST_INT_PRINT_DEC, call_count_str,
382 edge->count);
383 call_count_text = buf;
385 else
387 call_count_text = "";
390 locus = gimple_location (edge->call_stmt);
391 inform (locus, "%s inlined into %s%s%s",
392 cgraph_node_opt_info (edge->callee),
393 cgraph_node_opt_info (final_caller),
394 call_count_text,
395 inline_chain_text);
398 /* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL
399 specify whether profile of original function should be updated. If any new
400 indirect edges are discovered in the process, add them to NEW_EDGES, unless
401 it is NULL. Return true iff any new callgraph edges were discovered as a
402 result of inlining. */
404 static bool
405 cgraph_mark_inline_edge (struct cgraph_edge *e, bool update_original,
406 VEC (cgraph_edge_p, heap) **new_edges)
408 int old_size = 0, new_size = 0;
409 struct cgraph_node *to = NULL, *what;
410 struct cgraph_edge *curr = e;
411 int freq;
413 /* Skip fake edge. */
414 if (L_IPO_COMP_MODE && !e->call_stmt)
415 return false;
417 if (flag_opt_info >= OPT_INFO_MIN)
418 dump_inline_decision (e);
420 /* Don't inline inlined edges. */
421 gcc_assert (e->inline_failed);
422 /* Don't even think of inlining inline clone. */
423 gcc_assert (!e->callee->global.inlined_to);
425 e->inline_failed = CIF_OK;
426 DECL_POSSIBLY_INLINED (e->callee->decl) = true;
428 cgraph_clone_inlined_nodes (e, true, update_original);
430 what = e->callee;
432 freq = e->frequency;
433 /* Now update size of caller and all functions caller is inlined into. */
434 for (;e && !e->inline_failed; e = e->caller->callers)
436 to = e->caller;
437 old_size = e->caller->global.size;
438 new_size = cgraph_estimate_size_after_inlining (to, what);
439 to->global.size = new_size;
440 to->global.time = cgraph_estimate_time_after_inlining (freq, to, what);
442 if (to->max_bb_count < e->callee->max_bb_count)
443 to->max_bb_count = e->callee->max_bb_count;
445 gcc_assert (what->global.inlined_to == to);
446 if (new_size > old_size)
447 overall_size += new_size - old_size;
448 ncalls_inlined++;
450 /* FIXME: We should remove the optimize check after we ensure we never run
451 IPA passes when not optimizing. */
452 if (flag_indirect_inlining && optimize)
453 return ipa_propagate_indirect_call_infos (curr, new_edges);
454 else
455 return false;
458 /* Estimate the growth caused by inlining NODE into all callees. */
460 static int
461 cgraph_estimate_growth (struct cgraph_node *node)
463 int growth = 0;
464 struct cgraph_edge *e;
465 bool self_recursive = false;
467 if (node->global.estimated_growth != INT_MIN)
468 return node->global.estimated_growth;
470 for (e = node->callers; e; e = e->next_caller)
472 if (e->caller == node)
473 self_recursive = true;
474 if (e->inline_failed)
475 growth += (cgraph_estimate_size_after_inlining (e->caller, node)
476 - e->caller->global.size);
479 /* ??? Wrong for non-trivially self recursive functions or cases where
480 we decide to not inline for different reasons, but it is not big deal
481 as in that case we will keep the body around, but we will also avoid
482 some inlining. */
483 if (cgraph_will_be_removed_from_program_if_no_direct_calls (node)
484 && !DECL_EXTERNAL (node->decl) && !self_recursive)
485 growth -= node->global.size;
486 /* COMDAT functions are very often not shared across multiple units since they
487 come from various template instantiations. Take this into account. */
488 else if (DECL_COMDAT (node->decl) && !self_recursive
489 && cgraph_can_remove_if_no_direct_calls_p (node))
490 growth -= (node->global.size
491 * (100 - PARAM_VALUE (PARAM_COMDAT_SHARING_PROBABILITY)) + 50) / 100;
493 node->global.estimated_growth = growth;
494 return growth;
497 /* Return false when inlining WHAT into TO is not good idea
498 as it would cause too large growth of function bodies.
499 When ONE_ONLY is true, assume that only one call site is going
500 to be inlined, otherwise figure out how many call sites in
501 TO calls WHAT and verify that all can be inlined.
504 static bool
505 cgraph_check_inline_limits (struct cgraph_node *to, struct cgraph_node *what,
506 cgraph_inline_failed_t *reason)
508 int newsize;
509 int limit;
510 HOST_WIDE_INT stack_size_limit, inlined_stack;
512 if (to->global.inlined_to)
513 to = to->global.inlined_to;
515 /* When inlining large function body called once into small function,
516 take the inlined function as base for limiting the growth. */
517 if (inline_summary (to)->self_size > inline_summary(what)->self_size)
518 limit = inline_summary (to)->self_size;
519 else
520 limit = inline_summary (what)->self_size;
522 limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
524 /* Check the size after inlining against the function limits. But allow
525 the function to shrink if it went over the limits by forced inlining. */
526 newsize = cgraph_estimate_size_after_inlining (to, what);
527 if (newsize >= to->global.size
528 && newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
529 && newsize > limit)
531 if (reason)
532 *reason = CIF_LARGE_FUNCTION_GROWTH_LIMIT;
533 return false;
536 stack_size_limit = inline_summary (to)->estimated_self_stack_size;
538 stack_size_limit += stack_size_limit * PARAM_VALUE (PARAM_STACK_FRAME_GROWTH) / 100;
540 inlined_stack = (to->global.estimated_stack_size
541 + what->global.estimated_stack_size);
542 if (inlined_stack > stack_size_limit
543 && inlined_stack > PARAM_VALUE (PARAM_LARGE_STACK_FRAME))
545 if (reason)
546 *reason = CIF_LARGE_STACK_FRAME_GROWTH_LIMIT;
547 return false;
549 return true;
552 /* Return true when function N is small enough to be inlined. */
554 static bool
555 cgraph_default_inline_p (struct cgraph_node *n, cgraph_inline_failed_t *reason)
557 tree decl = n->decl;
559 if (n->local.disregard_inline_limits)
560 return true;
562 if (!flag_inline_small_functions && !DECL_DECLARED_INLINE_P (decl))
564 if (reason)
565 *reason = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
566 return false;
568 if (!n->analyzed)
570 if (reason)
571 *reason = CIF_BODY_NOT_AVAILABLE;
572 return false;
574 if (cgraph_function_body_availability (n) <= AVAIL_OVERWRITABLE)
576 if (reason)
577 *reason = CIF_OVERWRITABLE;
578 return false;
582 if (DECL_DECLARED_INLINE_P (decl))
584 if (n->global.size >= MAX_INLINE_INSNS_SINGLE)
586 if (reason)
587 *reason = CIF_MAX_INLINE_INSNS_SINGLE_LIMIT;
588 return false;
591 else
593 if (n->global.size >= MAX_INLINE_INSNS_AUTO)
595 if (reason)
596 *reason = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
597 return false;
601 return true;
604 /* Return true when inlining WHAT would create recursive inlining.
605 We call recursive inlining all cases where same function appears more than
606 once in the single recursion nest path in the inline graph. */
608 static inline bool
609 cgraph_recursive_inlining_p (struct cgraph_node *to,
610 struct cgraph_node *what,
611 cgraph_inline_failed_t *reason)
613 bool recursive;
614 if (to->global.inlined_to)
615 recursive = what->decl == to->global.inlined_to->decl;
616 else
617 recursive = what->decl == to->decl;
618 /* Marking recursive function inline has sane semantic and thus we should
619 not warn on it. */
620 if (recursive && reason)
621 *reason = (what->local.disregard_inline_limits
622 ? CIF_RECURSIVE_INLINING : CIF_UNSPECIFIED);
623 return recursive;
626 /* Return true if FUNCDECL is a function with fixed
627 argument list. */
629 static bool
630 fixed_arg_function_p (tree fndecl)
632 tree fntype = TREE_TYPE (fndecl);
633 return (TYPE_ARG_TYPES (fntype) == 0
634 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
635 == void_type_node));
638 /* For profile collection with flag_dyn_ipa (LIPO), we always
639 want to inline comdat functions for the following reasons:
640 1) Functions in comdat may be actually defined in a different
641 module (depending on how linker picks). This results in a edge
642 from one module to another module in the dynamic callgraph.
643 The edge is false and result in unnecessary module grouping.
644 2) The profile counters in comdat functions are not 'comdated'
645 -- which means each copy of the same comdat function has its
646 own set of counters. With inlining, we are actually splitting
647 the counters and make the profile information 'context sensitive',
648 which is a good thing.
649 3) During profile-use pass of LIPO (flag_dyn_ipa == 1),
650 the pre-tree_profile inline decisions have to be the same as the
651 profile-gen pass (otherwise coverage mismatch will occur). Due to
652 this reason, it is better for each module to 'use' the comdat copy
653 of its own. The only way to get profile data for the copy is to
654 inline the copy in profile-gen phase.
655 TODO: For indirectly called comdat functions, the above issues
656 still exist. */
658 static bool
659 better_inline_comdat_function_p (struct cgraph_node *node)
661 return (profile_arc_flag && flag_dyn_ipa
662 && DECL_COMDAT (node->decl)
663 && node->global.size <= PARAM_VALUE (PARAM_MAX_INLINE_INSNS_SINGLE)
664 && fixed_arg_function_p (node->decl));
668 /* A cost model driving the inlining heuristics in a way so the edges with
669 smallest badness are inlined first. After each inlining is performed
670 the costs of all caller edges of nodes affected are recomputed so the
671 metrics may accurately depend on values such as number of inlinable callers
672 of the function or function body size. */
674 static int
675 cgraph_edge_badness (struct cgraph_edge *edge, bool dump)
677 gcov_type badness;
678 int growth =
679 (cgraph_estimate_size_after_inlining (edge->caller, edge->callee)
680 - edge->caller->global.size);
682 if (edge->callee->local.disregard_inline_limits)
683 return INT_MIN;
685 if (dump)
687 fprintf (dump_file, " Badness calculation for %s -> %s\n",
688 cgraph_node_name (edge->caller),
689 cgraph_node_name (edge->callee));
690 fprintf (dump_file, " growth %i, time %i-%i, size %i-%i\n",
691 growth,
692 edge->callee->global.time,
693 inline_summary (edge->callee)->time_inlining_benefit,
694 edge->callee->global.size,
695 inline_summary (edge->callee)->size_inlining_benefit);
698 /* Always prefer inlining saving code size. */
699 if (growth <= 0)
701 badness = INT_MIN - growth;
702 if (dump)
703 fprintf (dump_file, " %i: Growth %i < 0\n", (int) badness,
704 growth);
707 /* When profiling is available, base priorities -(#calls / growth).
708 So we optimize for overall number of "executed" inlined calls. */
709 else if (max_count)
711 badness =
712 ((int)
713 ((double) edge->count * INT_MIN / max_count / (max_benefit + 1)) *
714 (inline_summary (edge->callee)->time_inlining_benefit + 1)) / growth;
715 if (dump)
717 fprintf (dump_file,
718 " %i (relative %f): profile info. Relative count %f"
719 " * Relative benefit %f\n",
720 (int) badness, (double) badness / INT_MIN,
721 (double) edge->count / max_count,
722 (double) (inline_summary (edge->callee)->
723 time_inlining_benefit + 1) / (max_benefit + 1));
727 /* When function local profile is available, base priorities on
728 growth / frequency, so we optimize for overall frequency of inlined
729 calls. This is not too accurate since while the call might be frequent
730 within function, the function itself is infrequent.
732 Other objective to optimize for is number of different calls inlined.
733 We add the estimated growth after inlining all functions to bias the
734 priorities slightly in this direction (so fewer times called functions
735 of the same size gets priority). */
736 else if (flag_guess_branch_prob)
738 int div = edge->frequency * 100 / CGRAPH_FREQ_BASE + 1;
739 int benefitperc;
740 int growth_for_all;
741 badness = growth * 10000;
742 benefitperc =
743 MIN (100 * inline_summary (edge->callee)->time_inlining_benefit /
744 (edge->callee->global.time + 1) +1, 100);
745 div *= benefitperc;
748 /* Decrease badness if call is nested. */
749 /* Compress the range so we don't overflow. */
750 if (div > 10000)
751 div = 10000 + ceil_log2 (div) - 8;
752 if (div < 1)
753 div = 1;
754 if (badness > 0)
755 badness /= div;
756 growth_for_all = cgraph_estimate_growth (edge->callee);
757 badness += growth_for_all;
758 if (badness > INT_MAX)
759 badness = INT_MAX;
760 if (dump)
762 fprintf (dump_file,
763 " %i: guessed profile. frequency %i, overall growth %i,"
764 " benefit %i%%, divisor %i\n",
765 (int) badness, edge->frequency, growth_for_all, benefitperc, div);
768 /* When function local profile is not available or it does not give
769 useful information (ie frequency is zero), base the cost on
770 loop nest and overall size growth, so we optimize for overall number
771 of functions fully inlined in program. */
772 else
774 int nest = MIN (edge->loop_nest, 8);
775 badness = cgraph_estimate_growth (edge->callee) * 256;
777 /* Decrease badness if call is nested. */
778 if (badness > 0)
779 badness >>= nest;
780 else
782 badness <<= nest;
784 if (dump)
785 fprintf (dump_file, " %i: no profile. nest %i\n", (int) badness,
786 nest);
789 /* Ensure that we did not overflow in all the fixed point math above. */
790 gcc_assert (badness >= INT_MIN);
791 gcc_assert (badness <= INT_MAX - 1);
792 /* Make recursive inlining happen always after other inlining is done. */
793 if (cgraph_recursive_inlining_p (edge->caller, edge->callee, NULL))
794 return badness + 1;
795 else
797 if (better_inline_comdat_function_p (edge->callee))
798 return INT_MIN + 1;
799 else
800 return badness;
804 /* Recompute badness of EDGE and update its key in HEAP if needed. */
805 static void
806 update_edge_key (fibheap_t heap, struct cgraph_edge *edge)
808 int badness = cgraph_edge_badness (edge, false);
809 if (edge->aux)
811 fibnode_t n = (fibnode_t) edge->aux;
812 gcc_checking_assert (n->data == edge);
814 /* fibheap_replace_key only decrease the keys.
815 When we increase the key we do not update heap
816 and instead re-insert the element once it becomes
817 a minimum of heap. */
818 if (badness < n->key)
820 fibheap_replace_key (heap, n, badness);
821 gcc_checking_assert (n->key == badness);
824 else
825 edge->aux = fibheap_insert (heap, badness, edge);
828 /* Recompute heap nodes for each of caller edge. */
830 static void
831 update_caller_keys (fibheap_t heap, struct cgraph_node *node,
832 bitmap updated_nodes)
834 struct cgraph_edge *edge;
835 cgraph_inline_failed_t failed_reason;
837 if (!node->local.inlinable
838 || cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE
839 || node->global.inlined_to)
840 return;
841 if (!bitmap_set_bit (updated_nodes, node->uid))
842 return;
843 node->global.estimated_growth = INT_MIN;
845 /* See if there is something to do. */
846 for (edge = node->callers; edge; edge = edge->next_caller)
847 if (edge->inline_failed)
848 break;
849 if (!edge)
850 return;
851 /* Prune out edges we won't inline into anymore. */
852 if (!cgraph_default_inline_p (node, &failed_reason)
853 && !better_inline_comdat_function_p (node))
855 for (; edge; edge = edge->next_caller)
856 if (edge->aux)
858 fibheap_delete_node (heap, (fibnode_t) edge->aux);
859 edge->aux = NULL;
860 if (edge->inline_failed)
861 edge->inline_failed = failed_reason;
863 return;
866 for (; edge; edge = edge->next_caller)
867 if (edge->inline_failed)
868 update_edge_key (heap, edge);
871 /* Recompute heap nodes for each uninlined call.
872 This is used when we know that edge badnesses are going only to increase
873 (we introduced new call site) and thus all we need is to insert newly
874 created edges into heap. */
876 static void
877 update_callee_keys (fibheap_t heap, struct cgraph_node *node,
878 bitmap updated_nodes)
880 struct cgraph_edge *e = node->callees;
881 node->global.estimated_growth = INT_MIN;
883 if (!e)
884 return;
885 while (true)
886 if (!e->inline_failed && e->callee->callees)
887 e = e->callee->callees;
888 else
890 if (e->inline_failed
891 && e->callee->local.inlinable
892 && cgraph_function_body_availability (e->callee) >= AVAIL_AVAILABLE
893 && !bitmap_bit_p (updated_nodes, e->callee->uid))
895 node->global.estimated_growth = INT_MIN;
896 /* If function becomes uninlinable, we need to remove it from the heap. */
897 if (!cgraph_default_inline_p (e->callee, &e->inline_failed))
898 update_caller_keys (heap, e->callee, updated_nodes);
899 else
900 /* Otherwise update just edge E. */
901 update_edge_key (heap, e);
903 if (e->next_callee)
904 e = e->next_callee;
905 else
909 if (e->caller == node)
910 return;
911 e = e->caller->callers;
913 while (!e->next_callee);
914 e = e->next_callee;
919 /* Recompute heap nodes for each of caller edges of each of callees.
920 Walk recursively into all inline clones. */
922 static void
923 update_all_callee_keys (fibheap_t heap, struct cgraph_node *node,
924 bitmap updated_nodes)
926 struct cgraph_edge *e = node->callees;
927 node->global.estimated_growth = INT_MIN;
929 if (!e)
930 return;
931 while (true)
932 if (!e->inline_failed && e->callee->callees)
933 e = e->callee->callees;
934 else
936 if (e->inline_failed)
937 update_caller_keys (heap, e->callee, updated_nodes);
938 if (e->next_callee)
939 e = e->next_callee;
940 else
944 if (e->caller == node)
945 return;
946 e = e->caller->callers;
948 while (!e->next_callee);
949 e = e->next_callee;
954 /* Enqueue all recursive calls from NODE into priority queue depending on
955 how likely we want to recursively inline the call. */
957 static void
958 lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
959 fibheap_t heap)
961 static int priority;
962 struct cgraph_edge *e;
963 for (e = where->callees; e; e = e->next_callee)
964 if (e->callee == node)
966 /* When profile feedback is available, prioritize by expected number
967 of calls. Without profile feedback we maintain simple queue
968 to order candidates via recursive depths. */
969 fibheap_insert (heap,
970 !max_count ? priority++
971 : -(e->count / ((max_count + (1<<24) - 1) / (1<<24))),
974 for (e = where->callees; e; e = e->next_callee)
975 if (!e->inline_failed)
976 lookup_recursive_calls (node, e->callee, heap);
979 /* Decide on recursive inlining: in the case function has recursive calls,
980 inline until body size reaches given argument. If any new indirect edges
981 are discovered in the process, add them to *NEW_EDGES, unless NEW_EDGES
982 is NULL. */
984 static bool
985 cgraph_decide_recursive_inlining (struct cgraph_node *node,
986 VEC (cgraph_edge_p, heap) **new_edges)
988 int limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO);
989 int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO);
990 int probability = PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY);
991 fibheap_t heap;
992 struct cgraph_edge *e;
993 struct cgraph_node *master_clone, *next;
994 int depth = 0;
995 int n = 0;
997 /* It does not make sense to recursively inline always-inline functions
998 as we are going to sorry() on the remaining calls anyway. */
999 if (node->local.disregard_inline_limits
1000 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (node->decl)))
1001 return false;
1003 if (optimize_function_for_size_p (DECL_STRUCT_FUNCTION (node->decl))
1004 || (!flag_inline_functions && !DECL_DECLARED_INLINE_P (node->decl)))
1005 return false;
1007 if (DECL_DECLARED_INLINE_P (node->decl))
1009 limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE);
1010 max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH);
1013 /* Make sure that function is small enough to be considered for inlining. */
1014 if (!max_depth
1015 || cgraph_estimate_size_after_inlining (node, node) >= limit)
1016 return false;
1017 heap = fibheap_new ();
1018 lookup_recursive_calls (node, node, heap);
1019 if (fibheap_empty (heap))
1021 fibheap_delete (heap);
1022 return false;
1025 if (dump_file)
1026 fprintf (dump_file,
1027 " Performing recursive inlining on %s\n",
1028 cgraph_node_name (node));
1030 /* We need original clone to copy around. */
1031 master_clone = cgraph_clone_node (node, node->decl,
1032 node->count, CGRAPH_FREQ_BASE, 1,
1033 false, NULL);
1034 for (e = master_clone->callees; e; e = e->next_callee)
1035 if (!e->inline_failed)
1036 cgraph_clone_inlined_nodes (e, true, false);
1038 /* Do the inlining and update list of recursive call during process. */
1039 while (!fibheap_empty (heap)
1040 && (cgraph_estimate_size_after_inlining (node, master_clone)
1041 <= limit))
1043 struct cgraph_edge *curr
1044 = (struct cgraph_edge *) fibheap_extract_min (heap);
1045 struct cgraph_node *cnode;
1047 depth = 1;
1048 for (cnode = curr->caller;
1049 cnode->global.inlined_to; cnode = cnode->callers->caller)
1050 if (node->decl == curr->callee->decl)
1051 depth++;
1052 if (depth > max_depth)
1054 if (dump_file)
1055 fprintf (dump_file,
1056 " maximal depth reached\n");
1057 continue;
1060 if (max_count && node->count)
1062 if (!cgraph_maybe_hot_edge_p (curr))
1064 if (dump_file)
1065 fprintf (dump_file, " Not inlining cold call\n");
1066 continue;
1068 if (node->count == 0 || curr->count * 100 / node->count < probability)
1070 if (dump_file)
1071 fprintf (dump_file,
1072 " Probability of edge is too small\n");
1073 continue;
1077 if (!dbg_cnt (inl))
1078 continue;
1080 if (dump_file)
1082 fprintf (dump_file,
1083 " Inlining call of depth %i", depth);
1084 if (node->count)
1086 fprintf (dump_file, " called approx. %.2f times per call",
1087 (double)curr->count / node->count);
1089 fprintf (dump_file, "\n");
1091 cgraph_redirect_edge_callee (curr, master_clone);
1092 cgraph_mark_inline_edge (curr, false, new_edges);
1093 lookup_recursive_calls (node, curr->callee, heap);
1094 n++;
1096 if (!fibheap_empty (heap) && dump_file)
1097 fprintf (dump_file, " Recursive inlining growth limit met.\n");
1099 fibheap_delete (heap);
1100 if (dump_file)
1101 fprintf (dump_file,
1102 "\n Inlined %i times, body grown from size %i to %i, time %i to %i\n", n,
1103 master_clone->global.size, node->global.size,
1104 master_clone->global.time, node->global.time);
1106 /* Remove master clone we used for inlining. We rely that clones inlined
1107 into master clone gets queued just before master clone so we don't
1108 need recursion. */
1109 for (node = cgraph_nodes; node != master_clone;
1110 node = next)
1112 next = node->next;
1113 if (node->global.inlined_to == master_clone)
1114 cgraph_remove_node (node);
1116 cgraph_remove_node (master_clone);
1117 /* FIXME: Recursive inlining actually reduces number of calls of the
1118 function. At this place we should probably walk the function and
1119 inline clones and compensate the counts accordingly. This probably
1120 doesn't matter much in practice. */
1121 return n > 0;
1124 /* Set inline_failed for all callers of given function to REASON. */
1126 static void
1127 cgraph_set_inline_failed (struct cgraph_node *node,
1128 cgraph_inline_failed_t reason)
1130 struct cgraph_edge *e;
1132 if (dump_file)
1133 fprintf (dump_file, "Inlining failed: %s\n",
1134 cgraph_inline_failed_string (reason));
1135 for (e = node->callers; e; e = e->next_caller)
1136 if (e->inline_failed)
1137 e->inline_failed = reason;
1140 /* Given whole compilation unit estimate of INSNS, compute how large we can
1141 allow the unit to grow. */
1142 static int
1143 compute_max_insns (int insns)
1145 int max_insns = insns;
1146 if (max_insns < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
1147 max_insns = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
1149 return ((HOST_WIDEST_INT) max_insns
1150 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
1153 /* Compute badness of all edges in NEW_EDGES and add them to the HEAP. */
1154 static void
1155 add_new_edges_to_heap (fibheap_t heap, VEC (cgraph_edge_p, heap) *new_edges)
1157 while (VEC_length (cgraph_edge_p, new_edges) > 0)
1159 struct cgraph_edge *edge = VEC_pop (cgraph_edge_p, new_edges);
1161 gcc_assert (!edge->aux);
1162 if (edge->callee->local.inlinable
1163 && edge->inline_failed
1164 && cgraph_default_inline_p (edge->callee, &edge->inline_failed))
1165 edge->aux = fibheap_insert (heap, cgraph_edge_badness (edge, false), edge);
1169 /* Returns true if an edge or its caller are hot enough to
1170 be considered for inlining. */
1172 static bool
1173 edge_hot_enough_p (struct cgraph_edge *edge)
1175 if (cgraph_maybe_hot_edge_p (edge))
1176 return true;
1177 if (flag_inline_hot_caller && maybe_hot_count_p (edge->caller->max_bb_count))
1178 return true;
1179 return false;
1183 /* We use greedy algorithm for inlining of small functions:
1184 All inline candidates are put into prioritized heap based on estimated
1185 growth of the overall number of instructions and then update the estimates.
1187 INLINED and INLINED_CALLEES are just pointers to arrays large enough
1188 to be passed to cgraph_inlined_into and cgraph_inlined_callees. */
1190 static void
1191 cgraph_decide_inlining_of_small_functions (void)
1193 struct cgraph_node *node;
1194 struct cgraph_edge *edge;
1195 cgraph_inline_failed_t failed_reason;
1196 fibheap_t heap = fibheap_new ();
1197 bitmap updated_nodes = BITMAP_ALLOC (NULL);
1198 int min_size, max_size;
1199 VEC (cgraph_edge_p, heap) *new_indirect_edges = NULL;
1201 is_in_ipa_inline = true;
1203 if (flag_indirect_inlining)
1204 new_indirect_edges = VEC_alloc (cgraph_edge_p, heap, 8);
1206 if (dump_file)
1207 fprintf (dump_file, "\nDeciding on smaller functions:\n");
1209 /* Put all inline candidates into the heap. */
1211 for (node = cgraph_nodes; node; node = node->next)
1213 if (!node->local.inlinable || !node->callers)
1214 continue;
1215 if (dump_file)
1216 fprintf (dump_file, "Considering inline candidate %s.\n", cgraph_node_name (node));
1218 node->global.estimated_growth = INT_MIN;
1219 if (!cgraph_default_inline_p (node, &failed_reason) &&
1220 !better_inline_comdat_function_p (node))
1222 cgraph_set_inline_failed (node, failed_reason);
1223 continue;
1226 for (edge = node->callers; edge; edge = edge->next_caller)
1227 if (edge->inline_failed)
1229 gcc_assert (!edge->aux);
1230 edge->aux = fibheap_insert (heap, cgraph_edge_badness (edge, false), edge);
1234 max_size = compute_max_insns (overall_size);
1235 min_size = overall_size;
1237 while (overall_size <= max_size
1238 && !fibheap_empty (heap))
1240 int old_size = overall_size;
1241 struct cgraph_node *where, *callee;
1242 int badness = fibheap_min_key (heap);
1243 int current_badness;
1244 int growth;
1245 cgraph_inline_failed_t not_good = CIF_OK;
1247 edge = (struct cgraph_edge *) fibheap_extract_min (heap);
1248 gcc_assert (edge->aux);
1249 edge->aux = NULL;
1250 if (!edge->inline_failed)
1251 continue;
1253 /* When updating the edge costs, we only decrease badness in the keys.
1254 When the badness increase, we keep the heap as it is and re-insert
1255 key now. */
1256 current_badness = cgraph_edge_badness (edge, false);
1257 gcc_assert (current_badness >= badness);
1258 if (current_badness != badness)
1260 edge->aux = fibheap_insert (heap, current_badness, edge);
1261 continue;
1264 callee = edge->callee;
1266 growth = (cgraph_estimate_size_after_inlining (edge->caller, edge->callee)
1267 - edge->caller->global.size);
1269 if (dump_file)
1271 fprintf (dump_file,
1272 "\nConsidering %s with %i size\n",
1273 cgraph_node_name (edge->callee),
1274 edge->callee->global.size);
1275 fprintf (dump_file,
1276 " to be inlined into %s in %s:%i\n"
1277 " Estimated growth after inlined into all callees is %+i insns.\n"
1278 " Estimated badness is %i, frequency %.2f.\n",
1279 cgraph_node_name (edge->caller),
1280 flag_wpa ? "unknown"
1281 : gimple_filename ((const_gimple) edge->call_stmt),
1282 flag_wpa ? -1 : gimple_lineno ((const_gimple) edge->call_stmt),
1283 cgraph_estimate_growth (edge->callee),
1284 badness,
1285 edge->frequency / (double)CGRAPH_FREQ_BASE);
1286 if (edge->count)
1287 fprintf (dump_file," Called "HOST_WIDEST_INT_PRINT_DEC"x\n", edge->count);
1288 if (dump_flags & TDF_DETAILS)
1289 cgraph_edge_badness (edge, true);
1292 /* When not having profile info ready we don't weight by any way the
1293 position of call in procedure itself. This means if call of
1294 function A from function B seems profitable to inline, the recursive
1295 call of function A in inline copy of A in B will look profitable too
1296 and we end up inlining until reaching maximal function growth. This
1297 is not good idea so prohibit the recursive inlining.
1299 ??? When the frequencies are taken into account we might not need this
1300 restriction.
1302 We need to be careful here, in some testcases, e.g. directives.c in
1303 libcpp, we can estimate self recursive function to have negative growth
1304 for inlining completely.
1306 if (!edge->count)
1308 where = edge->caller;
1309 while (where->global.inlined_to)
1311 if (where->decl == edge->callee->decl)
1312 break;
1313 where = where->callers->caller;
1315 if (where->global.inlined_to)
1317 edge->inline_failed
1318 = (edge->callee->local.disregard_inline_limits
1319 ? CIF_RECURSIVE_INLINING : CIF_UNSPECIFIED);
1320 if (dump_file)
1321 fprintf (dump_file, " inline_failed:Recursive inlining performed only for function itself.\n");
1322 continue;
1326 if (edge->callee->local.disregard_inline_limits)
1328 else if (!edge_hot_enough_p (edge))
1329 not_good = CIF_UNLIKELY_CALL;
1330 else if (!flag_inline_functions
1331 && !DECL_DECLARED_INLINE_P (edge->callee->decl))
1332 not_good = CIF_NOT_DECLARED_INLINED;
1333 else if (optimize_function_for_size_p (DECL_STRUCT_FUNCTION(edge->caller->decl)))
1334 not_good = CIF_OPTIMIZING_FOR_SIZE;
1335 if (not_good && growth > 0 && cgraph_estimate_growth (edge->callee) > 0)
1337 if (!cgraph_recursive_inlining_p (edge->caller, edge->callee,
1338 &edge->inline_failed))
1340 edge->inline_failed = not_good;
1341 if (dump_file)
1342 fprintf (dump_file, " inline_failed:%s.\n",
1343 cgraph_inline_failed_string (edge->inline_failed));
1345 continue;
1347 if (!cgraph_default_inline_p (edge->callee, &edge->inline_failed)
1348 && !better_inline_comdat_function_p (edge->callee))
1350 if (!cgraph_recursive_inlining_p (edge->caller, edge->callee,
1351 &edge->inline_failed))
1353 if (dump_file)
1354 fprintf (dump_file, " inline_failed:%s.\n",
1355 cgraph_inline_failed_string (edge->inline_failed));
1357 continue;
1359 if (!tree_can_inline_p (edge)
1360 || edge->call_stmt_cannot_inline_p)
1362 if (dump_file)
1363 fprintf (dump_file, " inline_failed:%s.\n",
1364 cgraph_inline_failed_string (edge->inline_failed));
1365 continue;
1367 if (cgraph_recursive_inlining_p (edge->caller, edge->callee,
1368 &edge->inline_failed))
1370 where = edge->caller;
1371 if (where->global.inlined_to)
1372 where = where->global.inlined_to;
1373 if (!cgraph_decide_recursive_inlining (where,
1374 flag_indirect_inlining
1375 ? &new_indirect_edges : NULL))
1376 continue;
1377 if (flag_indirect_inlining)
1378 add_new_edges_to_heap (heap, new_indirect_edges);
1379 update_all_callee_keys (heap, where, updated_nodes);
1381 else
1383 struct cgraph_node *callee;
1384 if (!cgraph_check_inline_limits (edge->caller, edge->callee,
1385 &edge->inline_failed))
1387 if (dump_file)
1388 fprintf (dump_file, " Not inlining into %s:%s.\n",
1389 cgraph_node_name (edge->caller),
1390 cgraph_inline_failed_string (edge->inline_failed));
1391 continue;
1393 if (!dbg_cnt (inl))
1394 continue;
1396 callee = edge->callee;
1397 gcc_checking_assert (!callee->global.inlined_to);
1398 cgraph_mark_inline_edge (edge, true, &new_indirect_edges);
1399 if (flag_indirect_inlining)
1400 add_new_edges_to_heap (heap, new_indirect_edges);
1402 /* We inlined last offline copy to the body. This might lead
1403 to callees of function having fewer call sites and thus they
1404 may need updating. */
1405 if (callee->global.inlined_to)
1406 update_all_callee_keys (heap, callee, updated_nodes);
1407 else
1408 update_callee_keys (heap, edge->callee, updated_nodes);
1410 where = edge->caller;
1411 if (where->global.inlined_to)
1412 where = where->global.inlined_to;
1414 /* Our profitability metric can depend on local properties
1415 such as number of inlinable calls and size of the function body.
1416 After inlining these properties might change for the function we
1417 inlined into (since it's body size changed) and for the functions
1418 called by function we inlined (since number of it inlinable callers
1419 might change). */
1420 update_caller_keys (heap, where, updated_nodes);
1422 /* We removed one call of the function we just inlined. If offline
1423 copy is still needed, be sure to update the keys. */
1424 if (callee != where && !callee->global.inlined_to)
1425 update_caller_keys (heap, callee, updated_nodes);
1426 bitmap_clear (updated_nodes);
1428 if (dump_file)
1430 fprintf (dump_file,
1431 "INFO: %s Inlined into %s which now has time %i and size %i,"
1432 "net change of %+i.\n",
1433 cgraph_node_name (edge->callee),
1434 cgraph_node_name (edge->caller),
1435 edge->caller->global.time,
1436 edge->caller->global.size,
1437 overall_size - old_size);
1439 if (min_size > overall_size)
1441 min_size = overall_size;
1442 max_size = compute_max_insns (min_size);
1444 if (dump_file)
1445 fprintf (dump_file, "New minimal size reached: %i\n", min_size);
1448 while (!fibheap_empty (heap))
1450 int badness = fibheap_min_key (heap);
1452 edge = (struct cgraph_edge *) fibheap_extract_min (heap);
1453 gcc_assert (edge->aux);
1454 edge->aux = NULL;
1455 if (!edge->inline_failed)
1456 continue;
1457 #ifdef ENABLE_CHECKING
1458 gcc_assert (cgraph_edge_badness (edge, false) >= badness);
1459 #endif
1460 if (dump_file)
1462 fprintf (dump_file,
1463 "\nSkipping %s with %i size\n",
1464 cgraph_node_name (edge->callee),
1465 edge->callee->global.size);
1466 fprintf (dump_file,
1467 " called by %s in %s:%i\n"
1468 " Estimated growth after inlined into all callees is %+i insns.\n"
1469 " Estimated badness is %i, frequency %.2f.\n",
1470 cgraph_node_name (edge->caller),
1471 flag_wpa ? "unknown"
1472 : gimple_filename ((const_gimple) edge->call_stmt),
1473 flag_wpa ? -1 : gimple_lineno ((const_gimple) edge->call_stmt),
1474 cgraph_estimate_growth (edge->callee),
1475 badness,
1476 edge->frequency / (double)CGRAPH_FREQ_BASE);
1477 if (edge->count)
1478 fprintf (dump_file," Called "HOST_WIDEST_INT_PRINT_DEC"x\n", edge->count);
1479 if (dump_flags & TDF_DETAILS)
1480 cgraph_edge_badness (edge, true);
1482 if (!edge->callee->local.disregard_inline_limits && edge->inline_failed
1483 && !cgraph_recursive_inlining_p (edge->caller, edge->callee,
1484 &edge->inline_failed))
1485 edge->inline_failed = CIF_INLINE_UNIT_GROWTH_LIMIT;
1488 if (new_indirect_edges)
1489 VEC_free (cgraph_edge_p, heap, new_indirect_edges);
1490 fibheap_delete (heap);
1491 BITMAP_FREE (updated_nodes);
1494 /* Flatten NODE from the IPA inliner. */
1496 static void
1497 cgraph_flatten (struct cgraph_node *node)
1499 struct cgraph_edge *e;
1501 /* We shouldn't be called recursively when we are being processed. */
1502 gcc_assert (node->aux == NULL);
1504 node->aux = (void *)(size_t) INLINE_ALL;
1506 for (e = node->callees; e; e = e->next_callee)
1508 struct cgraph_node *orig_callee;
1510 if (e->call_stmt_cannot_inline_p)
1512 if (dump_file)
1513 fprintf (dump_file, "Not inlining: %s",
1514 cgraph_inline_failed_string (e->inline_failed));
1515 continue;
1518 if (!e->callee->analyzed)
1520 if (dump_file)
1521 fprintf (dump_file,
1522 "Not inlining: Function body not available.\n");
1523 continue;
1526 if (!e->callee->local.inlinable)
1527 continue;
1529 /* We've hit cycle? It is time to give up. */
1530 if (e->callee->aux)
1532 if (dump_file)
1533 fprintf (dump_file,
1534 "Not inlining %s into %s to avoid cycle.\n",
1535 cgraph_node_name (e->callee),
1536 cgraph_node_name (e->caller));
1537 e->inline_failed = CIF_RECURSIVE_INLINING;
1538 continue;
1541 /* When the edge is already inlined, we just need to recurse into
1542 it in order to fully flatten the leaves. */
1543 if (!e->inline_failed)
1545 cgraph_flatten (e->callee);
1546 continue;
1549 if (cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed))
1551 if (dump_file)
1552 fprintf (dump_file, "Not inlining: recursive call.\n");
1553 continue;
1556 if (!tree_can_inline_p (e))
1558 if (dump_file)
1559 fprintf (dump_file, "Not inlining: %s",
1560 cgraph_inline_failed_string (e->inline_failed));
1561 continue;
1564 if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
1565 != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->callee->decl)))
1567 if (dump_file)
1568 fprintf (dump_file, "Not inlining: SSA form does not match.\n");
1569 continue;
1572 /* Inline the edge and flatten the inline clone. Avoid
1573 recursing through the original node if the node was cloned. */
1574 if (dump_file)
1575 fprintf (dump_file, " Inlining %s into %s.\n",
1576 cgraph_node_name (e->callee),
1577 cgraph_node_name (e->caller));
1578 orig_callee = e->callee;
1579 cgraph_mark_inline_edge (e, true, NULL);
1580 if (e->callee != orig_callee)
1581 orig_callee->aux = (void *)(size_t) INLINE_ALL;
1582 cgraph_flatten (e->callee);
1583 if (e->callee != orig_callee)
1584 orig_callee->aux = NULL;
1587 node->aux = NULL;
1590 /* Decide on the inlining. We do so in the topological order to avoid
1591 expenses on updating data structures. */
1593 static unsigned int
1594 cgraph_decide_inlining (void)
1596 struct cgraph_node *node;
1597 int nnodes;
1598 struct cgraph_node **order =
1599 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1600 int old_size = 0;
1601 int i;
1602 int initial_size = 0;
1604 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
1605 if (in_lto_p && flag_indirect_inlining)
1606 ipa_update_after_lto_read ();
1607 if (flag_indirect_inlining)
1608 ipa_create_all_structures_for_iinln ();
1610 max_count = 0;
1611 max_benefit = 0;
1612 for (node = cgraph_nodes; node; node = node->next)
1613 if (node->analyzed)
1615 struct cgraph_edge *e;
1617 gcc_assert (inline_summary (node)->self_size == node->global.size);
1618 if (!DECL_EXTERNAL (node->decl))
1619 initial_size += node->global.size;
1620 for (e = node->callees; e; e = e->next_callee)
1621 if (max_count < e->count)
1622 max_count = e->count;
1623 if (max_benefit < inline_summary (node)->time_inlining_benefit)
1624 max_benefit = inline_summary (node)->time_inlining_benefit;
1626 gcc_assert (in_lto_p
1627 || !max_count
1628 || (profile_info && flag_branch_probabilities));
1629 overall_size = initial_size;
1631 nnodes = cgraph_postorder (order);
1633 if (dump_file)
1634 fprintf (dump_file,
1635 "\nDeciding on inlining. Starting with size %i.\n",
1636 initial_size);
1638 for (node = cgraph_nodes; node; node = node->next)
1639 node->aux = 0;
1641 if (dump_file)
1642 fprintf (dump_file, "\nFlattening functions:\n");
1644 /* In the first pass handle functions to be flattened. Do this with
1645 a priority so none of our later choices will make this impossible. */
1646 for (i = nnodes - 1; i >= 0; i--)
1648 node = order[i];
1650 /* Handle nodes to be flattened, but don't update overall unit
1651 size. Calling the incremental inliner here is lame,
1652 a simple worklist should be enough. What should be left
1653 here from the early inliner (if it runs) is cyclic cases.
1654 Ideally when processing callees we stop inlining at the
1655 entry of cycles, possibly cloning that entry point and
1656 try to flatten itself turning it into a self-recursive
1657 function. */
1658 if (lookup_attribute ("flatten",
1659 DECL_ATTRIBUTES (node->decl)) != NULL)
1661 if (dump_file)
1662 fprintf (dump_file,
1663 "Flattening %s\n", cgraph_node_name (node));
1664 cgraph_flatten (node);
1668 cgraph_decide_inlining_of_small_functions ();
1670 if (flag_inline_functions_called_once)
1672 if (dump_file)
1673 fprintf (dump_file, "\nDeciding on functions called once:\n");
1675 /* And finally decide what functions are called once. */
1676 for (i = nnodes - 1; i >= 0; i--)
1678 node = order[i];
1680 if (node->callers
1681 && !node->callers->next_caller
1682 && !node->global.inlined_to
1683 && cgraph_will_be_removed_from_program_if_no_direct_calls (node)
1684 && node->local.inlinable
1685 && cgraph_function_body_availability (node) >= AVAIL_AVAILABLE
1686 && node->callers->inline_failed
1687 && node->callers->caller != node
1688 && node->callers->caller->global.inlined_to != node
1689 && !node->callers->call_stmt_cannot_inline_p
1690 && tree_can_inline_p (node->callers)
1691 && !DECL_EXTERNAL (node->decl))
1693 cgraph_inline_failed_t reason;
1694 old_size = overall_size;
1695 if (dump_file)
1697 fprintf (dump_file,
1698 "\nConsidering %s size %i.\n",
1699 cgraph_node_name (node), node->global.size);
1700 fprintf (dump_file,
1701 " Called once from %s %i insns.\n",
1702 cgraph_node_name (node->callers->caller),
1703 node->callers->caller->global.size);
1706 if (cgraph_check_inline_limits (node->callers->caller, node,
1707 &reason))
1709 struct cgraph_node *caller = node->callers->caller;
1710 cgraph_mark_inline_edge (node->callers, true, NULL);
1711 if (dump_file)
1712 fprintf (dump_file,
1713 "INFO: Inlined into %s which now has %i size"
1714 " for a net change of %+i size.\n",
1715 cgraph_node_name (caller),
1716 caller->global.size,
1717 overall_size - old_size);
1719 else
1721 if (dump_file)
1722 fprintf (dump_file,
1723 " Not inlining: %s.\n",
1724 cgraph_inline_failed_string (reason));
1730 /* Free ipa-prop structures if they are no longer needed. */
1731 if (flag_indirect_inlining)
1732 ipa_free_all_structures_after_iinln ();
1734 if (dump_file)
1735 fprintf (dump_file,
1736 "\nInlined %i calls, eliminated %i functions, "
1737 "size %i turned to %i size.\n\n",
1738 ncalls_inlined, nfunctions_inlined, initial_size,
1739 overall_size);
1740 free (order);
1741 return 0;
1744 /* Return true when N is leaf function. Accept cheap builtins
1745 in leaf functions. */
1747 static bool
1748 leaf_node_p (struct cgraph_node *n)
1750 struct cgraph_edge *e;
1751 /* The following is buggy -- indirect call is not considered. */
1752 for (e = n->callees; e; e = e->next_callee)
1753 if (e->call_stmt /* Only exisit in profile use pass in LIPO */
1754 && !is_inexpensive_builtin (e->callee->decl))
1755 return false;
1756 return true;
1759 /* Decide on the inlining. We do so in the topological order to avoid
1760 expenses on updating data structures. */
1762 static bool
1763 cgraph_decide_inlining_incrementally (struct cgraph_node *node,
1764 enum inlining_mode mode)
1766 struct cgraph_edge *e;
1767 bool inlined = false;
1768 cgraph_inline_failed_t failed_reason;
1770 #ifdef ENABLE_CHECKING
1771 verify_cgraph_node (node);
1772 #endif
1774 if (mode != INLINE_ALWAYS_INLINE && mode != INLINE_SIZE_NORECURSIVE
1775 && lookup_attribute ("flatten", DECL_ATTRIBUTES (node->decl)) != NULL)
1777 if (dump_file)
1778 fprintf (dump_file, "Incrementally flattening %s\n",
1779 cgraph_node_name (node));
1780 mode = INLINE_ALL;
1783 /* First of all look for always inline functions. */
1784 if (mode != INLINE_SIZE_NORECURSIVE)
1785 for (e = node->callees; e; e = e->next_callee)
1787 if (!e->callee->local.disregard_inline_limits
1788 && (mode != INLINE_ALL || !e->callee->local.inlinable))
1789 continue;
1790 if (dump_file)
1791 fprintf (dump_file,
1792 "Considering to always inline inline candidate %s.\n",
1793 cgraph_node_name (e->callee));
1794 if (cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed))
1796 if (dump_file)
1797 fprintf (dump_file, "Not inlining: recursive call.\n");
1798 continue;
1800 if (!tree_can_inline_p (e)
1801 || e->call_stmt_cannot_inline_p)
1803 if (dump_file)
1804 fprintf (dump_file,
1805 "Not inlining: %s",
1806 cgraph_inline_failed_string (e->inline_failed));
1807 continue;
1809 if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
1810 != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->callee->decl)))
1812 if (dump_file)
1813 fprintf (dump_file, "Not inlining: SSA form does not match.\n");
1814 continue;
1816 if (!e->callee->analyzed)
1818 if (dump_file)
1819 fprintf (dump_file,
1820 "Not inlining: Function body no longer available.\n");
1821 continue;
1824 if (dump_file)
1825 fprintf (dump_file, " Inlining %s into %s.\n",
1826 cgraph_node_name (e->callee),
1827 cgraph_node_name (e->caller));
1828 cgraph_mark_inline_edge (e, true, NULL);
1829 inlined = true;
1832 /* Now do the automatic inlining. */
1833 if (mode != INLINE_ALL && mode != INLINE_ALWAYS_INLINE
1834 /* Never inline regular functions into always-inline functions
1835 during incremental inlining. */
1836 && !node->local.disregard_inline_limits)
1838 for (e = node->callees; e; e = e->next_callee)
1840 int allowed_growth = 0;
1841 if (!e->callee->local.inlinable
1842 || !e->inline_failed
1843 || e->callee->local.disregard_inline_limits)
1844 continue;
1846 if (dump_file)
1847 fprintf (dump_file, "Considering inline candidate %s.\n",
1848 cgraph_node_name (e->callee));
1849 if (cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed))
1851 if (dump_file)
1852 fprintf (dump_file, "Not inlining: recursive call.\n");
1853 continue;
1855 if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
1856 != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->callee->decl)))
1858 if (dump_file)
1859 fprintf (dump_file,
1860 "Not inlining: SSA form does not match.\n");
1861 continue;
1864 if (cgraph_maybe_hot_edge_p (e) && leaf_node_p (e->callee)
1865 && optimize_function_for_speed_p (cfun))
1866 allowed_growth = PARAM_VALUE (PARAM_EARLY_INLINING_INSNS);
1868 /* When the function body would grow and inlining the function
1869 won't eliminate the need for offline copy of the function,
1870 don't inline. */
1871 if (((mode == INLINE_SIZE || mode == INLINE_SIZE_NORECURSIVE)
1872 || (!flag_inline_functions
1873 && !DECL_DECLARED_INLINE_P (e->callee->decl)))
1874 && (cgraph_estimate_size_after_inlining (e->caller, e->callee)
1875 > e->caller->global.size + allowed_growth)
1876 && (cgraph_estimate_growth (e->callee) > allowed_growth))
1878 if (dump_file)
1879 fprintf (dump_file,
1880 "Not inlining: code size would grow by %i.\n",
1881 cgraph_estimate_size_after_inlining (e->caller,
1882 e->callee)
1883 - e->caller->global.size);
1884 continue;
1886 if (e->call_stmt_cannot_inline_p
1887 || !tree_can_inline_p (e))
1889 if (dump_file)
1890 fprintf (dump_file,
1891 "Not inlining: call site not inlinable.\n");
1892 continue;
1894 if (!e->callee->analyzed)
1896 if (dump_file)
1897 fprintf (dump_file,
1898 "Not inlining: Function body no longer available.\n");
1899 continue;
1901 if (!cgraph_check_inline_limits (node, e->callee, &e->inline_failed))
1903 if (dump_file)
1904 fprintf (dump_file, "Not inlining: %s.\n",
1905 cgraph_inline_failed_string (e->inline_failed));
1906 continue;
1908 if (cgraph_default_inline_p (e->callee, &failed_reason))
1910 if (dump_file)
1911 fprintf (dump_file, " Inlining %s into %s.\n",
1912 cgraph_node_name (e->callee),
1913 cgraph_node_name (e->caller));
1914 cgraph_mark_inline_edge (e, true, NULL);
1915 inlined = true;
1919 return inlined;
1922 /* Because inlining might remove no-longer reachable nodes, we need to
1923 keep the array visible to garbage collector to avoid reading collected
1924 out nodes. */
1925 static int nnodes;
1926 static GTY ((length ("nnodes"))) struct cgraph_node **order;
1928 /* Do inlining of small functions. Doing so early helps profiling and other
1929 passes to be somewhat more effective and avoids some code duplication in
1930 later real inlining pass for testcases with very many function calls. */
1931 static unsigned int
1932 cgraph_early_inlining (void)
1934 struct cgraph_node *node = cgraph_node (current_function_decl);
1935 unsigned int todo = 0;
1936 int iterations = 0;
1938 if (seen_error ())
1939 return 0;
1941 if (!optimize
1942 || flag_no_inline
1943 || !flag_early_inlining)
1945 /* When not optimizing or not inlining inline only always-inline
1946 functions. */
1947 cgraph_decide_inlining_incrementally (node, INLINE_ALWAYS_INLINE);
1948 timevar_push (TV_INTEGRATION);
1949 todo |= optimize_inline_calls (current_function_decl);
1950 timevar_pop (TV_INTEGRATION);
1952 else
1954 if (lookup_attribute ("flatten",
1955 DECL_ATTRIBUTES (node->decl)) != NULL)
1957 if (dump_file)
1958 fprintf (dump_file,
1959 "Flattening %s\n", cgraph_node_name (node));
1960 cgraph_flatten (node);
1961 timevar_push (TV_INTEGRATION);
1962 todo |= optimize_inline_calls (current_function_decl);
1963 timevar_pop (TV_INTEGRATION);
1965 /* We iterate incremental inlining to get trivial cases of indirect
1966 inlining. */
1967 while (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS)
1968 && cgraph_decide_inlining_incrementally (node,
1969 iterations
1970 ? INLINE_SIZE_NORECURSIVE
1971 : INLINE_SIZE))
1973 timevar_push (TV_INTEGRATION);
1974 todo |= optimize_inline_calls (current_function_decl);
1975 iterations++;
1976 timevar_pop (TV_INTEGRATION);
1978 if (dump_file)
1979 fprintf (dump_file, "Iterations: %i\n", iterations);
1982 cfun->always_inline_functions_inlined = true;
1984 return todo;
1987 struct gimple_opt_pass pass_early_inline =
1990 GIMPLE_PASS,
1991 "einline", /* name */
1992 NULL, /* gate */
1993 cgraph_early_inlining, /* execute */
1994 NULL, /* sub */
1995 NULL, /* next */
1996 0, /* static_pass_number */
1997 TV_INLINE_HEURISTICS, /* tv_id */
1998 0, /* properties_required */
1999 0, /* properties_provided */
2000 0, /* properties_destroyed */
2001 0, /* todo_flags_start */
2002 TODO_dump_func /* todo_flags_finish */
2007 /* See if statement might disappear after inlining.
2008 0 - means not eliminated
2009 1 - half of statements goes away
2010 2 - for sure it is eliminated.
2011 We are not terribly sophisticated, basically looking for simple abstraction
2012 penalty wrappers. */
2014 static int
2015 eliminated_by_inlining_prob (gimple stmt)
2017 enum gimple_code code = gimple_code (stmt);
2018 switch (code)
2020 case GIMPLE_RETURN:
2021 return 2;
2022 case GIMPLE_ASSIGN:
2023 if (gimple_num_ops (stmt) != 2)
2024 return 0;
2026 /* Casts of parameters, loads from parameters passed by reference
2027 and stores to return value or parameters are often free after
2028 inlining dua to SRA and further combining.
2029 Assume that half of statements goes away. */
2030 if (gimple_assign_rhs_code (stmt) == CONVERT_EXPR
2031 || gimple_assign_rhs_code (stmt) == NOP_EXPR
2032 || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
2033 || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
2035 tree rhs = gimple_assign_rhs1 (stmt);
2036 tree lhs = gimple_assign_lhs (stmt);
2037 tree inner_rhs = rhs;
2038 tree inner_lhs = lhs;
2039 bool rhs_free = false;
2040 bool lhs_free = false;
2042 while (handled_component_p (inner_lhs)
2043 || TREE_CODE (inner_lhs) == MEM_REF)
2044 inner_lhs = TREE_OPERAND (inner_lhs, 0);
2045 while (handled_component_p (inner_rhs)
2046 || TREE_CODE (inner_rhs) == ADDR_EXPR
2047 || TREE_CODE (inner_rhs) == MEM_REF)
2048 inner_rhs = TREE_OPERAND (inner_rhs, 0);
2051 if (TREE_CODE (inner_rhs) == PARM_DECL
2052 || (TREE_CODE (inner_rhs) == SSA_NAME
2053 && SSA_NAME_IS_DEFAULT_DEF (inner_rhs)
2054 && TREE_CODE (SSA_NAME_VAR (inner_rhs)) == PARM_DECL))
2055 rhs_free = true;
2056 if (rhs_free && is_gimple_reg (lhs))
2057 lhs_free = true;
2058 if (((TREE_CODE (inner_lhs) == PARM_DECL
2059 || (TREE_CODE (inner_lhs) == SSA_NAME
2060 && SSA_NAME_IS_DEFAULT_DEF (inner_lhs)
2061 && TREE_CODE (SSA_NAME_VAR (inner_lhs)) == PARM_DECL))
2062 && inner_lhs != lhs)
2063 || TREE_CODE (inner_lhs) == RESULT_DECL
2064 || (TREE_CODE (inner_lhs) == SSA_NAME
2065 && TREE_CODE (SSA_NAME_VAR (inner_lhs)) == RESULT_DECL))
2066 lhs_free = true;
2067 if (lhs_free
2068 && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
2069 rhs_free = true;
2070 if (lhs_free && rhs_free)
2071 return 1;
2073 return 0;
2074 default:
2075 return 0;
2079 /* Compute function body size parameters for NODE. */
2081 static void
2082 estimate_function_body_sizes (struct cgraph_node *node)
2084 gcov_type time = 0;
2085 gcov_type time_inlining_benefit = 0;
2086 /* Estimate static overhead for function prologue/epilogue and alignment. */
2087 int size = PARAM_VALUE (PARAM_INLINE_FUNCTION_OVERHEAD_SIZE);
2088 /* Benefits are scaled by probability of elimination that is in range
2089 <0,2>. */
2090 int size_inlining_benefit =
2091 PARAM_VALUE (PARAM_INLINE_FUNCTION_OVERHEAD_SIZE) * 2;
2092 basic_block bb;
2093 gimple_stmt_iterator bsi;
2094 struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
2095 tree arg;
2096 int freq;
2097 tree funtype = TREE_TYPE (node->decl);
2099 if (dump_file)
2100 fprintf (dump_file, "Analyzing function body size: %s\n",
2101 cgraph_node_name (node));
2103 gcc_assert (my_function && my_function->cfg);
2104 FOR_EACH_BB_FN (bb, my_function)
2106 freq = compute_call_stmt_bb_frequency (node->decl, bb);
2107 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2109 gimple stmt = gsi_stmt (bsi);
2110 int this_size = estimate_num_insns (stmt, &eni_size_weights);
2111 int this_time = estimate_num_insns (stmt, &eni_time_weights);
2112 int prob;
2114 if (dump_file && (dump_flags & TDF_DETAILS))
2116 fprintf (dump_file, " freq:%6i size:%3i time:%3i ",
2117 freq, this_size, this_time);
2118 print_gimple_stmt (dump_file, stmt, 0, 0);
2120 if (!dbg_cnt (inl))
2121 continue;
2123 if (dump_file)
2125 fprintf (dump_file, " freq:%6i size:%3i time:%3i ", freq, this_size, this_time);
2126 print_gimple_stmt (dump_file, gsi_stmt (bsi), 0, 0);
2128 this_time *= freq;
2129 time += this_time;
2130 size += this_size;
2131 prob = eliminated_by_inlining_prob (stmt);
2132 if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
2133 fprintf (dump_file, " 50%% will be eliminated by inlining\n");
2134 if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
2135 fprintf (dump_file, " will eliminated by inlining\n");
2136 size_inlining_benefit += this_size * prob;
2137 time_inlining_benefit += this_time * prob;
2138 gcc_assert (time >= 0);
2139 gcc_assert (size >= 0);
2142 time = (time + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
2143 time_inlining_benefit = ((time_inlining_benefit + CGRAPH_FREQ_BASE)
2144 / (CGRAPH_FREQ_BASE * 2));
2145 size_inlining_benefit = (size_inlining_benefit + 1) / 2;
2146 if (dump_file)
2147 fprintf (dump_file, "Overall function body time: %i-%i size: %i-%i\n",
2148 (int)time, (int)time_inlining_benefit,
2149 size, size_inlining_benefit);
2150 time_inlining_benefit += eni_time_weights.call_cost;
2151 size_inlining_benefit += eni_size_weights.call_cost;
2152 if (!VOID_TYPE_P (TREE_TYPE (funtype)))
2154 int cost = estimate_move_cost (TREE_TYPE (funtype));
2155 time_inlining_benefit += cost;
2156 size_inlining_benefit += cost;
2158 for (arg = DECL_ARGUMENTS (node->decl); arg; arg = DECL_CHAIN (arg))
2159 if (!VOID_TYPE_P (TREE_TYPE (arg)))
2161 int cost = estimate_move_cost (TREE_TYPE (arg));
2162 time_inlining_benefit += cost;
2163 size_inlining_benefit += cost;
2165 if (time_inlining_benefit > MAX_TIME)
2166 time_inlining_benefit = MAX_TIME;
2167 if (time > MAX_TIME)
2168 time = MAX_TIME;
2169 inline_summary (node)->self_time = time;
2170 inline_summary (node)->self_size = size;
2171 if (dump_file)
2172 fprintf (dump_file, "With function call overhead time: %i-%i size: %i-%i\n",
2173 (int)time, (int)time_inlining_benefit,
2174 size, size_inlining_benefit);
2175 inline_summary (node)->time_inlining_benefit = time_inlining_benefit;
2176 inline_summary (node)->size_inlining_benefit = size_inlining_benefit;
2179 /* Compute parameters of functions used by inliner. */
2180 void
2181 compute_inline_parameters (struct cgraph_node *node)
2183 HOST_WIDE_INT self_stack_size;
2185 gcc_assert (!node->global.inlined_to);
2187 /* Estimate the stack size for the function if we're optimizing. */
2188 self_stack_size = optimize ? estimated_stack_frame_size (node) : 0;
2189 inline_summary (node)->estimated_self_stack_size = self_stack_size;
2190 node->global.estimated_stack_size = self_stack_size;
2192 /* Can this function be inlined at all? */
2193 node->local.inlinable = tree_inlinable_function_p (node->decl);
2194 if (!node->local.inlinable)
2195 node->local.disregard_inline_limits = 0;
2197 /* Inlinable functions always can change signature. */
2198 if (node->local.inlinable)
2199 node->local.can_change_signature = true;
2200 else
2202 struct cgraph_edge *e;
2204 /* Functions calling builtin_apply can not change signature. */
2205 for (e = node->callees; e; e = e->next_callee)
2206 if (DECL_BUILT_IN (e->callee->decl)
2207 && DECL_BUILT_IN_CLASS (e->callee->decl) == BUILT_IN_NORMAL
2208 && DECL_FUNCTION_CODE (e->callee->decl) == BUILT_IN_APPLY_ARGS)
2209 break;
2210 node->local.can_change_signature = !e;
2212 estimate_function_body_sizes (node);
2213 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
2214 node->global.time = inline_summary (node)->self_time;
2215 node->global.size = inline_summary (node)->self_size;
2219 /* Compute parameters of functions used by inliner using
2220 current_function_decl. */
2221 static unsigned int
2222 compute_inline_parameters_for_current (void)
2224 compute_inline_parameters (cgraph_node (current_function_decl));
2225 return 0;
2228 struct gimple_opt_pass pass_inline_parameters =
2231 GIMPLE_PASS,
2232 "inline_param", /* name */
2233 NULL, /* gate */
2234 compute_inline_parameters_for_current,/* execute */
2235 NULL, /* sub */
2236 NULL, /* next */
2237 0, /* static_pass_number */
2238 TV_INLINE_HEURISTICS, /* tv_id */
2239 0, /* properties_required */
2240 0, /* properties_provided */
2241 0, /* properties_destroyed */
2242 0, /* todo_flags_start */
2243 0 /* todo_flags_finish */
2247 /* This function performs intraprocedural analysis in NODE that is required to
2248 inline indirect calls. */
2249 static void
2250 inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
2252 ipa_analyze_node (node);
2253 if (dump_file && (dump_flags & TDF_DETAILS))
2255 ipa_print_node_params (dump_file, node);
2256 ipa_print_node_jump_functions (dump_file, node);
2260 /* Note function body size. */
2261 static void
2262 analyze_function (struct cgraph_node *node)
2264 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2265 current_function_decl = node->decl;
2267 compute_inline_parameters (node);
2268 /* FIXME: We should remove the optimize check after we ensure we never run
2269 IPA passes when not optimizing. */
2270 if (flag_indirect_inlining && optimize)
2271 inline_indirect_intraprocedural_analysis (node);
2273 current_function_decl = NULL;
2274 pop_cfun ();
2277 /* Called when new function is inserted to callgraph late. */
2278 static void
2279 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2281 analyze_function (node);
2284 /* Note function body size. */
2285 static void
2286 inline_generate_summary (void)
2288 struct cgraph_node *node;
2290 function_insertion_hook_holder =
2291 cgraph_add_function_insertion_hook (&add_new_function, NULL);
2293 if (flag_indirect_inlining)
2294 ipa_register_cgraph_hooks ();
2296 for (node = cgraph_nodes; node; node = node->next)
2297 if (node->analyzed)
2298 analyze_function (node);
2300 return;
2303 /* Apply inline plan to function. */
2304 static unsigned int
2305 inline_transform (struct cgraph_node *node)
2307 unsigned int todo = 0;
2308 struct cgraph_edge *e;
2309 bool inline_p = false;
2311 /* FIXME: Currently the pass manager is adding inline transform more than once to some
2312 clones. This needs revisiting after WPA cleanups. */
2313 if (cfun->after_inlining)
2314 return 0;
2316 /* We might need the body of this function so that we can expand
2317 it inline somewhere else. */
2318 if (cgraph_preserve_function_body_p (node->decl))
2319 save_inline_function_body (node);
2321 for (e = node->callees; e; e = e->next_callee)
2323 cgraph_redirect_edge_call_stmt_to_callee (e);
2324 if (!e->inline_failed || warn_inline)
2325 inline_p = true;
2328 if (inline_p)
2330 timevar_push (TV_INTEGRATION);
2331 todo = optimize_inline_calls (current_function_decl);
2332 timevar_pop (TV_INTEGRATION);
2334 cfun->always_inline_functions_inlined = true;
2335 cfun->after_inlining = true;
2336 return todo | execute_fixup_cfg ();
2339 /* Read inline summary. Jump functions are shared among ipa-cp
2340 and inliner, so when ipa-cp is active, we don't need to write them
2341 twice. */
2343 static void
2344 inline_read_summary (void)
2346 if (flag_indirect_inlining)
2348 ipa_register_cgraph_hooks ();
2349 if (!flag_ipa_cp)
2350 ipa_prop_read_jump_functions ();
2352 function_insertion_hook_holder =
2353 cgraph_add_function_insertion_hook (&add_new_function, NULL);
2356 /* Write inline summary for node in SET.
2357 Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
2358 active, we don't need to write them twice. */
2360 static void
2361 inline_write_summary (cgraph_node_set set,
2362 varpool_node_set vset ATTRIBUTE_UNUSED)
2364 if (flag_indirect_inlining && !flag_ipa_cp)
2365 ipa_prop_write_jump_functions (set);
2368 /* When to run IPA inlining. Inlining of always-inline functions
2369 happens during early inlining. */
2371 static bool
2372 gate_cgraph_decide_inlining (void)
2374 /* ??? We'd like to skip this if not optimizing or not inlining as
2375 all always-inline functions have been processed by early
2376 inlining already. But this at least breaks EH with C++ as
2377 we need to unconditionally run fixup_cfg even at -O0.
2378 So leave it on unconditionally for now. */
2379 return 1;
2382 struct ipa_opt_pass_d pass_ipa_inline =
2385 IPA_PASS,
2386 "inline", /* name */
2387 gate_cgraph_decide_inlining, /* gate */
2388 cgraph_decide_inlining, /* execute */
2389 NULL, /* sub */
2390 NULL, /* next */
2391 0, /* static_pass_number */
2392 TV_INLINE_HEURISTICS, /* tv_id */
2393 0, /* properties_required */
2394 0, /* properties_provided */
2395 0, /* properties_destroyed */
2396 TODO_remove_functions, /* todo_flags_finish */
2397 TODO_dump_cgraph | TODO_dump_func
2398 | TODO_remove_functions | TODO_ggc_collect /* todo_flags_finish */
2400 inline_generate_summary, /* generate_summary */
2401 inline_write_summary, /* write_summary */
2402 inline_read_summary, /* read_summary */
2403 NULL, /* write_optimization_summary */
2404 NULL, /* read_optimization_summary */
2405 NULL, /* stmt_fixup */
2406 0, /* TODOs */
2407 inline_transform, /* function_transform */
2408 NULL, /* variable_transform */
2412 #include "gt-ipa-inline.h"