OSDN Git Service

Add Go frontend, libgo library, and Go testsuite.
[pf3gnuchains/gcc-fork.git] / gcc / ipa-inline.c
1 /* Inlining decision heuristics.
2    Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4    Contributed by Jan Hubicka
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>.  */
21
22 /*  Inlining decision heuristics
23
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.
28
29     There are three major parts of this file:
30
31     cgraph_mark_inline implementation
32
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)
36
37     inlining heuristics limits
38
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).
42
43     inlining heuristics
44
45       This is implementation of IPA pass aiming to get as much of benefit
46       from inlining obeying the limits checked above.
47
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.
53
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.
57
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.
61
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.
65
66    The inliner itself is split into several passes:
67
68    pass_inline_parameters
69
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.
73
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.
78
79    pass_early_inlining
80
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.
85
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.
93
94    pass_ipa_early_inlining
95
96      With profiling, the early inlining is also necessary to reduce
97      instrumentation costs on program with high abstraction penalty (doing
98      many redundant calls).  This can't happen in parallel with early
99      optimization and profile instrumentation, because we would end up
100      re-instrumenting already instrumented function bodies we brought in via
101      inlining.
102
103      To avoid this, this pass is executed as IPA pass before profiling.  It is
104      simple wrapper to pass_early_inlining and ensures first inlining.
105
106    pass_ipa_inline
107
108      This is the main pass implementing simple greedy algorithm to do inlining
109      of small functions that results in overall growth of compilation unit and
110      inlining of functions called once.  The pass compute just so called inline
111      plan (representation of inlining to be done in callgraph) and unlike early
112      inlining it is not performing the inlining itself.
113
114    pass_apply_inline
115
116      This pass performs actual inlining according to pass_ipa_inline on given
117      function.  Possible the function body before inlining is saved when it is
118      needed for further inlining later.
119  */
120
121 #include "config.h"
122 #include "system.h"
123 #include "coretypes.h"
124 #include "tm.h"
125 #include "tree.h"
126 #include "tree-inline.h"
127 #include "langhooks.h"
128 #include "flags.h"
129 #include "cgraph.h"
130 #include "diagnostic.h"
131 #include "gimple-pretty-print.h"
132 #include "timevar.h"
133 #include "params.h"
134 #include "fibheap.h"
135 #include "intl.h"
136 #include "tree-pass.h"
137 #include "hashtab.h"
138 #include "coverage.h"
139 #include "ggc.h"
140 #include "tree-flow.h"
141 #include "rtl.h"
142 #include "ipa-prop.h"
143 #include "except.h"
144
145 #define MAX_TIME 1000000000
146
147 /* Mode incremental inliner operate on:
148
149    In ALWAYS_INLINE only functions marked
150    always_inline are inlined.  This mode is used after detecting cycle during
151    flattening.
152
153    In SIZE mode, only functions that reduce function body size after inlining
154    are inlined, this is used during early inlining.
155
156    in ALL mode, everything is inlined.  This is used during flattening.  */
157 enum inlining_mode {
158   INLINE_NONE = 0,
159   INLINE_ALWAYS_INLINE,
160   INLINE_SIZE_NORECURSIVE,
161   INLINE_SIZE,
162   INLINE_ALL
163 };
164
165 static bool
166 cgraph_decide_inlining_incrementally (struct cgraph_node *, enum inlining_mode);
167 static void cgraph_flatten (struct cgraph_node *node);
168
169
170 /* Statistics we collect about inlining algorithm.  */
171 static int ncalls_inlined;
172 static int nfunctions_inlined;
173 static int overall_size;
174 static gcov_type max_count, max_benefit;
175
176 /* Holders of ipa cgraph hooks: */
177 static struct cgraph_node_hook_list *function_insertion_hook_holder;
178
179 static inline struct inline_summary *
180 inline_summary (struct cgraph_node *node)
181 {
182   return &node->local.inline_summary;
183 }
184
185 /* Estimate self time of the function after inlining WHAT into TO.  */
186
187 static int
188 cgraph_estimate_time_after_inlining (int frequency, struct cgraph_node *to,
189                                      struct cgraph_node *what)
190 {
191   gcov_type time = (((gcov_type)what->global.time
192                      - inline_summary (what)->time_inlining_benefit)
193                     * frequency + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE
194                     + to->global.time;
195   if (time < 0)
196     time = 0;
197   if (time > MAX_TIME)
198     time = MAX_TIME;
199   return time;
200 }
201
202 /* Estimate self time of the function after inlining WHAT into TO.  */
203
204 static inline int
205 cgraph_estimate_size_after_inlining (int times, struct cgraph_node *to,
206                                      struct cgraph_node *what)
207 {
208   int size = ((what->global.size - inline_summary (what)->size_inlining_benefit)
209               * times + to->global.size);
210   gcc_assert (size >= 0);
211   return size;
212 }
213
214 /* Scale frequency of NODE edges by FREQ_SCALE and increase loop nest
215    by NEST.  */
216
217 static void
218 update_noncloned_frequencies (struct cgraph_node *node,
219                               int freq_scale, int nest)
220 {
221   struct cgraph_edge *e;
222
223   /* We do not want to ignore high loop nest after freq drops to 0.  */
224   if (!freq_scale)
225     freq_scale = 1;
226   for (e = node->callees; e; e = e->next_callee)
227     {
228       e->loop_nest += nest;
229       e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
230       if (e->frequency > CGRAPH_FREQ_MAX)
231         e->frequency = CGRAPH_FREQ_MAX;
232       if (!e->inline_failed)
233         update_noncloned_frequencies (e->callee, freq_scale, nest);
234     }
235 }
236
237 /* E is expected to be an edge being inlined.  Clone destination node of
238    the edge and redirect it to the new clone.
239    DUPLICATE is used for bookkeeping on whether we are actually creating new
240    clones or re-using node originally representing out-of-line function call.
241    */
242 void
243 cgraph_clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
244                             bool update_original)
245 {
246   HOST_WIDE_INT peak;
247
248   if (duplicate)
249     {
250       /* We may eliminate the need for out-of-line copy to be output.
251          In that case just go ahead and re-use it.  */
252       if (!e->callee->callers->next_caller
253           /* FIXME: When address is taken of DECL_EXTERNAL function we still can remove its
254              offline copy, but we would need to keep unanalyzed node in the callgraph so
255              references can point to it.  */
256           && !e->callee->address_taken
257           && cgraph_can_remove_if_no_direct_calls_p (e->callee)
258           /* Inlining might enable more devirtualizing, so we want to remove
259              those only after all devirtualizable virtual calls are processed.
260              Lacking may edges in callgraph we just preserve them post
261              inlining.  */
262           && (!DECL_VIRTUAL_P (e->callee->decl)
263               || (!DECL_COMDAT (e->callee->decl) && !DECL_EXTERNAL (e->callee->decl)))
264           /* Don't reuse if more than one function shares a comdat group.
265              If the other function(s) are needed, we need to emit even
266              this function out of line.  */
267           && !e->callee->same_comdat_group
268           && !cgraph_new_nodes)
269         {
270           gcc_assert (!e->callee->global.inlined_to);
271           if (e->callee->analyzed && !DECL_EXTERNAL (e->callee->decl))
272             {
273               overall_size -= e->callee->global.size;
274               nfunctions_inlined++;
275             }
276           duplicate = false;
277           e->callee->local.externally_visible = false;
278           update_noncloned_frequencies (e->callee, e->frequency, e->loop_nest);
279         }
280       else
281         {
282           struct cgraph_node *n;
283           n = cgraph_clone_node (e->callee, e->callee->decl,
284                                  e->count, e->frequency, e->loop_nest,
285                                  update_original, NULL);
286           cgraph_redirect_edge_callee (e, n);
287         }
288     }
289
290   if (e->caller->global.inlined_to)
291     e->callee->global.inlined_to = e->caller->global.inlined_to;
292   else
293     e->callee->global.inlined_to = e->caller;
294   e->callee->global.stack_frame_offset
295     = e->caller->global.stack_frame_offset
296       + inline_summary (e->caller)->estimated_self_stack_size;
297   peak = e->callee->global.stack_frame_offset
298       + inline_summary (e->callee)->estimated_self_stack_size;
299   if (e->callee->global.inlined_to->global.estimated_stack_size < peak)
300     e->callee->global.inlined_to->global.estimated_stack_size = peak;
301   cgraph_propagate_frequency (e->callee);
302
303   /* Recursively clone all bodies.  */
304   for (e = e->callee->callees; e; e = e->next_callee)
305     if (!e->inline_failed)
306       cgraph_clone_inlined_nodes (e, duplicate, update_original);
307 }
308
309 /* Mark edge E as inlined and update callgraph accordingly.  UPDATE_ORIGINAL
310    specify whether profile of original function should be updated.  If any new
311    indirect edges are discovered in the process, add them to NEW_EDGES, unless
312    it is NULL.  Return true iff any new callgraph edges were discovered as a
313    result of inlining.  */
314
315 static bool
316 cgraph_mark_inline_edge (struct cgraph_edge *e, bool update_original,
317                          VEC (cgraph_edge_p, heap) **new_edges)
318 {
319   int old_size = 0, new_size = 0;
320   struct cgraph_node *to = NULL, *what;
321   struct cgraph_edge *curr = e;
322   int freq;
323
324   gcc_assert (e->inline_failed);
325   e->inline_failed = CIF_OK;
326   DECL_POSSIBLY_INLINED (e->callee->decl) = true;
327
328   cgraph_clone_inlined_nodes (e, true, update_original);
329
330   what = e->callee;
331
332   freq = e->frequency;
333   /* Now update size of caller and all functions caller is inlined into.  */
334   for (;e && !e->inline_failed; e = e->caller->callers)
335     {
336       to = e->caller;
337       old_size = e->caller->global.size;
338       new_size = cgraph_estimate_size_after_inlining (1, to, what);
339       to->global.size = new_size;
340       to->global.time = cgraph_estimate_time_after_inlining (freq, to, what);
341     }
342   gcc_assert (what->global.inlined_to == to);
343   if (new_size > old_size)
344     overall_size += new_size - old_size;
345   ncalls_inlined++;
346
347   /* FIXME: We should remove the optimize check after we ensure we never run
348      IPA passes when not optimizng.  */
349   if (flag_indirect_inlining && optimize)
350     return ipa_propagate_indirect_call_infos (curr, new_edges);
351   else
352     return false;
353 }
354
355 /* Mark all calls of EDGE->CALLEE inlined into EDGE->CALLER.  */
356
357 static void
358 cgraph_mark_inline (struct cgraph_edge *edge)
359 {
360   struct cgraph_node *to = edge->caller;
361   struct cgraph_node *what = edge->callee;
362   struct cgraph_edge *e, *next;
363
364   gcc_assert (!edge->call_stmt_cannot_inline_p);
365   /* Look for all calls, mark them inline and clone recursively
366      all inlined functions.  */
367   for (e = what->callers; e; e = next)
368     {
369       next = e->next_caller;
370       if (e->caller == to && e->inline_failed)
371         {
372           cgraph_mark_inline_edge (e, true, NULL);
373           if (e == edge)
374             edge = next;
375         }
376     }
377 }
378
379 /* Estimate the growth caused by inlining NODE into all callees.  */
380
381 static int
382 cgraph_estimate_growth (struct cgraph_node *node)
383 {
384   int growth = 0;
385   struct cgraph_edge *e;
386   bool self_recursive = false;
387
388   if (node->global.estimated_growth != INT_MIN)
389     return node->global.estimated_growth;
390
391   for (e = node->callers; e; e = e->next_caller)
392     {
393       if (e->caller == node)
394         self_recursive = true;
395       if (e->inline_failed)
396         growth += (cgraph_estimate_size_after_inlining (1, e->caller, node)
397                    - e->caller->global.size);
398     }
399
400   /* ??? Wrong for non-trivially self recursive functions or cases where
401      we decide to not inline for different reasons, but it is not big deal
402      as in that case we will keep the body around, but we will also avoid
403      some inlining.  */
404   if (cgraph_will_be_removed_from_program_if_no_direct_calls (node)
405       && !DECL_EXTERNAL (node->decl) && !self_recursive)
406     growth -= node->global.size;
407   /* COMDAT functions are very often not shared across multiple units since they
408      come from various template instantiations.  Take this into account.  */
409   else  if (DECL_COMDAT (node->decl) && !self_recursive
410             && cgraph_can_remove_if_no_direct_calls_p (node))
411     growth -= (node->global.size
412                * (100 - PARAM_VALUE (PARAM_COMDAT_SHARING_PROBABILITY)) + 50) / 100;
413
414   node->global.estimated_growth = growth;
415   return growth;
416 }
417
418 /* Return false when inlining WHAT into TO is not good idea
419    as it would cause too large growth of function bodies.
420    When ONE_ONLY is true, assume that only one call site is going
421    to be inlined, otherwise figure out how many call sites in
422    TO calls WHAT and verify that all can be inlined.
423    */
424
425 static bool
426 cgraph_check_inline_limits (struct cgraph_node *to, struct cgraph_node *what,
427                             cgraph_inline_failed_t *reason, bool one_only)
428 {
429   int times = 0;
430   struct cgraph_edge *e;
431   int newsize;
432   int limit;
433   HOST_WIDE_INT stack_size_limit, inlined_stack;
434
435   if (one_only)
436     times = 1;
437   else
438     for (e = to->callees; e; e = e->next_callee)
439       if (e->callee == what)
440         times++;
441
442   if (to->global.inlined_to)
443     to = to->global.inlined_to;
444
445   /* When inlining large function body called once into small function,
446      take the inlined function as base for limiting the growth.  */
447   if (inline_summary (to)->self_size > inline_summary(what)->self_size)
448     limit = inline_summary (to)->self_size;
449   else
450     limit = inline_summary (what)->self_size;
451
452   limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
453
454   /* Check the size after inlining against the function limits.  But allow
455      the function to shrink if it went over the limits by forced inlining.  */
456   newsize = cgraph_estimate_size_after_inlining (times, to, what);
457   if (newsize >= to->global.size
458       && newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
459       && newsize > limit)
460     {
461       if (reason)
462         *reason = CIF_LARGE_FUNCTION_GROWTH_LIMIT;
463       return false;
464     }
465
466   stack_size_limit = inline_summary (to)->estimated_self_stack_size;
467
468   stack_size_limit += stack_size_limit * PARAM_VALUE (PARAM_STACK_FRAME_GROWTH) / 100;
469
470   inlined_stack = (to->global.stack_frame_offset
471                    + inline_summary (to)->estimated_self_stack_size
472                    + what->global.estimated_stack_size);
473   if (inlined_stack  > stack_size_limit
474       && inlined_stack > PARAM_VALUE (PARAM_LARGE_STACK_FRAME))
475     {
476       if (reason)
477         *reason = CIF_LARGE_STACK_FRAME_GROWTH_LIMIT;
478       return false;
479     }
480   return true;
481 }
482
483 /* Return true when function N is small enough to be inlined.  */
484
485 static bool
486 cgraph_default_inline_p (struct cgraph_node *n, cgraph_inline_failed_t *reason)
487 {
488   tree decl = n->decl;
489
490   if (n->local.disregard_inline_limits)
491     return true;
492
493   if (!flag_inline_small_functions && !DECL_DECLARED_INLINE_P (decl))
494     {
495       if (reason)
496         *reason = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
497       return false;
498     }
499   if (!n->analyzed)
500     {
501       if (reason)
502         *reason = CIF_BODY_NOT_AVAILABLE;
503       return false;
504     }
505   if (cgraph_function_body_availability (n) <= AVAIL_OVERWRITABLE)
506     {
507       if (reason)
508         *reason = CIF_OVERWRITABLE;
509       return false;
510     }
511
512
513   if (DECL_DECLARED_INLINE_P (decl))
514     {
515       if (n->global.size >= MAX_INLINE_INSNS_SINGLE)
516         {
517           if (reason)
518             *reason = CIF_MAX_INLINE_INSNS_SINGLE_LIMIT;
519           return false;
520         }
521     }
522   else
523     {
524       if (n->global.size >= MAX_INLINE_INSNS_AUTO)
525         {
526           if (reason)
527             *reason = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
528           return false;
529         }
530     }
531
532   return true;
533 }
534
535 /* Return true when inlining WHAT would create recursive inlining.
536    We call recursive inlining all cases where same function appears more than
537    once in the single recursion nest path in the inline graph.  */
538
539 static inline bool
540 cgraph_recursive_inlining_p (struct cgraph_node *to,
541                              struct cgraph_node *what,
542                              cgraph_inline_failed_t *reason)
543 {
544   bool recursive;
545   if (to->global.inlined_to)
546     recursive = what->decl == to->global.inlined_to->decl;
547   else
548     recursive = what->decl == to->decl;
549   /* Marking recursive function inline has sane semantic and thus we should
550      not warn on it.  */
551   if (recursive && reason)
552     *reason = (what->local.disregard_inline_limits
553                ? CIF_RECURSIVE_INLINING : CIF_UNSPECIFIED);
554   return recursive;
555 }
556
557 /* A cost model driving the inlining heuristics in a way so the edges with
558    smallest badness are inlined first.  After each inlining is performed
559    the costs of all caller edges of nodes affected are recomputed so the
560    metrics may accurately depend on values such as number of inlinable callers
561    of the function or function body size.  */
562
563 static int
564 cgraph_edge_badness (struct cgraph_edge *edge, bool dump)
565 {
566   gcov_type badness;
567   int growth =
568     (cgraph_estimate_size_after_inlining (1, edge->caller, edge->callee)
569      - edge->caller->global.size);
570
571   if (edge->callee->local.disregard_inline_limits)
572     return INT_MIN;
573
574   if (dump)
575     {
576       fprintf (dump_file, "    Badness calculcation for %s -> %s\n",
577                cgraph_node_name (edge->caller),
578                cgraph_node_name (edge->callee));
579       fprintf (dump_file, "      growth %i, time %i-%i, size %i-%i\n",
580                growth,
581                edge->callee->global.time,
582                inline_summary (edge->callee)->time_inlining_benefit,
583                edge->callee->global.size,
584                inline_summary (edge->callee)->size_inlining_benefit);
585     }
586
587   /* Always prefer inlining saving code size.  */
588   if (growth <= 0)
589     {
590       badness = INT_MIN - growth;
591       if (dump)
592         fprintf (dump_file, "      %i: Growth %i < 0\n", (int) badness,
593                  growth);
594     }
595
596   /* When profiling is available, base priorities -(#calls / growth).
597      So we optimize for overall number of "executed" inlined calls.  */
598   else if (max_count)
599     {
600       badness =
601         ((int)
602          ((double) edge->count * INT_MIN / max_count / (max_benefit + 1)) *
603          (inline_summary (edge->callee)->time_inlining_benefit + 1)) / growth;
604       if (dump)
605         {
606           fprintf (dump_file,
607                    "      %i (relative %f): profile info. Relative count %f"
608                    " * Relative benefit %f\n",
609                    (int) badness, (double) badness / INT_MIN,
610                    (double) edge->count / max_count,
611                    (double) (inline_summary (edge->callee)->
612                              time_inlining_benefit + 1) / (max_benefit + 1));
613         }
614     }
615
616   /* When function local profile is available, base priorities on
617      growth / frequency, so we optimize for overall frequency of inlined
618      calls.  This is not too accurate since while the call might be frequent
619      within function, the function itself is infrequent.
620
621      Other objective to optimize for is number of different calls inlined.
622      We add the estimated growth after inlining all functions to bias the
623      priorities slightly in this direction (so fewer times called functions
624      of the same size gets priority).  */
625   else if (flag_guess_branch_prob)
626     {
627       int div = edge->frequency * 100 / CGRAPH_FREQ_BASE + 1;
628       int benefitperc;
629       int growth_for_all;
630       badness = growth * 10000;
631       benefitperc =
632         MIN (100 * inline_summary (edge->callee)->time_inlining_benefit /
633              (edge->callee->global.time + 1) +1, 100);
634       div *= benefitperc;
635
636
637       /* Decrease badness if call is nested.  */
638       /* Compress the range so we don't overflow.  */
639       if (div > 10000)
640         div = 10000 + ceil_log2 (div) - 8;
641       if (div < 1)
642         div = 1;
643       if (badness > 0)
644         badness /= div;
645       growth_for_all = cgraph_estimate_growth (edge->callee);
646       badness += growth_for_all;
647       if (badness > INT_MAX)
648         badness = INT_MAX;
649       if (dump)
650         {
651           fprintf (dump_file,
652                    "      %i: guessed profile. frequency %i, overall growth %i,"
653                    " benefit %i%%, divisor %i\n",
654                    (int) badness, edge->frequency, growth_for_all, benefitperc, div);
655         }
656     }
657   /* When function local profile is not available or it does not give
658      useful information (ie frequency is zero), base the cost on
659      loop nest and overall size growth, so we optimize for overall number
660      of functions fully inlined in program.  */
661   else
662     {
663       int nest = MIN (edge->loop_nest, 8);
664       badness = cgraph_estimate_growth (edge->callee) * 256;
665
666       /* Decrease badness if call is nested.  */
667       if (badness > 0)
668         badness >>= nest;
669       else
670         {
671           badness <<= nest;
672         }
673       if (dump)
674         fprintf (dump_file, "      %i: no profile. nest %i\n", (int) badness,
675                  nest);
676     }
677
678   /* Ensure that we did not overflow in all the fixed point math above.  */
679   gcc_assert (badness >= INT_MIN);
680   gcc_assert (badness <= INT_MAX - 1);
681   /* Make recursive inlining happen always after other inlining is done.  */
682   if (cgraph_recursive_inlining_p (edge->caller, edge->callee, NULL))
683     return badness + 1;
684   else
685     return badness;
686 }
687
688 /* Recompute badness of EDGE and update its key in HEAP if needed.  */
689 static void
690 update_edge_key (fibheap_t heap, struct cgraph_edge *edge)
691 {
692   int badness = cgraph_edge_badness (edge, false);
693   if (edge->aux)
694     {
695       fibnode_t n = (fibnode_t) edge->aux;
696       gcc_checking_assert (n->data == edge);
697
698       /* fibheap_replace_key only decrease the keys.
699          When we increase the key we do not update heap
700          and instead re-insert the element once it becomes
701          a minium of heap.  */
702       if (badness < n->key)
703         {
704           fibheap_replace_key (heap, n, badness);
705           gcc_checking_assert (n->key == badness);
706         }
707     }
708   else
709     edge->aux = fibheap_insert (heap, badness, edge);
710 }
711
712 /* Recompute heap nodes for each of caller edge.  */
713
714 static void
715 update_caller_keys (fibheap_t heap, struct cgraph_node *node,
716                     bitmap updated_nodes)
717 {
718   struct cgraph_edge *edge;
719   cgraph_inline_failed_t failed_reason;
720
721   if (!node->local.inlinable
722       || cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE
723       || node->global.inlined_to)
724     return;
725   if (!bitmap_set_bit (updated_nodes, node->uid))
726     return;
727   node->global.estimated_growth = INT_MIN;
728
729   /* See if there is something to do.  */
730   for (edge = node->callers; edge; edge = edge->next_caller)
731     if (edge->inline_failed)
732       break;
733   if (!edge)
734     return;
735   /* Prune out edges we won't inline into anymore.  */
736   if (!cgraph_default_inline_p (node, &failed_reason))
737     {
738       for (; edge; edge = edge->next_caller)
739         if (edge->aux)
740           {
741             fibheap_delete_node (heap, (fibnode_t) edge->aux);
742             edge->aux = NULL;
743             if (edge->inline_failed)
744               edge->inline_failed = failed_reason;
745           }
746       return;
747     }
748
749   for (; edge; edge = edge->next_caller)
750     if (edge->inline_failed)
751       update_edge_key (heap, edge);
752 }
753
754 /* Recompute heap nodes for each uninlined call.
755    This is used when we know that edge badnesses are going only to increase
756    (we introduced new call site) and thus all we need is to insert newly
757    created edges into heap.  */
758
759 static void
760 update_callee_keys (fibheap_t heap, struct cgraph_node *node,
761                     bitmap updated_nodes)
762 {
763   struct cgraph_edge *e = node->callees;
764   node->global.estimated_growth = INT_MIN;
765
766   if (!e)
767     return;
768   while (true)
769     if (!e->inline_failed && e->callee->callees)
770       e = e->callee->callees;
771     else
772       {
773         if (e->inline_failed
774             && e->callee->local.inlinable
775             && cgraph_function_body_availability (e->callee) >= AVAIL_AVAILABLE
776             && !bitmap_bit_p (updated_nodes, e->callee->uid))
777           {
778             node->global.estimated_growth = INT_MIN;
779             /* If function becomes uninlinable, we need to remove it from the heap.  */
780             if (!cgraph_default_inline_p (e->callee, &e->inline_failed))
781               update_caller_keys (heap, e->callee, updated_nodes);
782             else
783             /* Otherwise update just edge E.  */
784               update_edge_key (heap, e);
785           }
786         if (e->next_callee)
787           e = e->next_callee;
788         else
789           {
790             do
791               {
792                 if (e->caller == node)
793                   return;
794                 e = e->caller->callers;
795               }
796             while (!e->next_callee);
797             e = e->next_callee;
798           }
799       }
800 }
801
802 /* Recompute heap nodes for each of caller edges of each of callees.
803    Walk recursively into all inline clones.  */
804
805 static void
806 update_all_callee_keys (fibheap_t heap, struct cgraph_node *node,
807                         bitmap updated_nodes)
808 {
809   struct cgraph_edge *e = node->callees;
810   node->global.estimated_growth = INT_MIN;
811
812   if (!e)
813     return;
814   while (true)
815     if (!e->inline_failed && e->callee->callees)
816       e = e->callee->callees;
817     else
818       {
819         if (e->inline_failed)
820           update_caller_keys (heap, e->callee, updated_nodes);
821         if (e->next_callee)
822           e = e->next_callee;
823         else
824           {
825             do
826               {
827                 if (e->caller == node)
828                   return;
829                 e = e->caller->callers;
830               }
831             while (!e->next_callee);
832             e = e->next_callee;
833           }
834       }
835 }
836
837 /* Enqueue all recursive calls from NODE into priority queue depending on
838    how likely we want to recursively inline the call.  */
839
840 static void
841 lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
842                         fibheap_t heap)
843 {
844   static int priority;
845   struct cgraph_edge *e;
846   for (e = where->callees; e; e = e->next_callee)
847     if (e->callee == node)
848       {
849         /* When profile feedback is available, prioritize by expected number
850            of calls.  Without profile feedback we maintain simple queue
851            to order candidates via recursive depths.  */
852         fibheap_insert (heap,
853                         !max_count ? priority++
854                         : -(e->count / ((max_count + (1<<24) - 1) / (1<<24))),
855                         e);
856       }
857   for (e = where->callees; e; e = e->next_callee)
858     if (!e->inline_failed)
859       lookup_recursive_calls (node, e->callee, heap);
860 }
861
862 /* Decide on recursive inlining: in the case function has recursive calls,
863    inline until body size reaches given argument.  If any new indirect edges
864    are discovered in the process, add them to *NEW_EDGES, unless NEW_EDGES
865    is NULL.  */
866
867 static bool
868 cgraph_decide_recursive_inlining (struct cgraph_node *node,
869                                   VEC (cgraph_edge_p, heap) **new_edges)
870 {
871   int limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO);
872   int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO);
873   int probability = PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY);
874   fibheap_t heap;
875   struct cgraph_edge *e;
876   struct cgraph_node *master_clone, *next;
877   int depth = 0;
878   int n = 0;
879
880   /* It does not make sense to recursively inline always-inline functions
881      as we are going to sorry() on the remaining calls anyway.  */
882   if (node->local.disregard_inline_limits
883       && lookup_attribute ("always_inline", DECL_ATTRIBUTES (node->decl)))
884     return false;
885
886   if (optimize_function_for_size_p (DECL_STRUCT_FUNCTION (node->decl))
887       || (!flag_inline_functions && !DECL_DECLARED_INLINE_P (node->decl)))
888     return false;
889
890   if (DECL_DECLARED_INLINE_P (node->decl))
891     {
892       limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE);
893       max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH);
894     }
895
896   /* Make sure that function is small enough to be considered for inlining.  */
897   if (!max_depth
898       || cgraph_estimate_size_after_inlining (1, node, node)  >= limit)
899     return false;
900   heap = fibheap_new ();
901   lookup_recursive_calls (node, node, heap);
902   if (fibheap_empty (heap))
903     {
904       fibheap_delete (heap);
905       return false;
906     }
907
908   if (dump_file)
909     fprintf (dump_file,
910              "  Performing recursive inlining on %s\n",
911              cgraph_node_name (node));
912
913   /* We need original clone to copy around.  */
914   master_clone = cgraph_clone_node (node, node->decl,
915                                     node->count, CGRAPH_FREQ_BASE, 1,
916                                     false, NULL);
917   master_clone->needed = true;
918   for (e = master_clone->callees; e; e = e->next_callee)
919     if (!e->inline_failed)
920       cgraph_clone_inlined_nodes (e, true, false);
921
922   /* Do the inlining and update list of recursive call during process.  */
923   while (!fibheap_empty (heap)
924          && (cgraph_estimate_size_after_inlining (1, node, master_clone)
925              <= limit))
926     {
927       struct cgraph_edge *curr
928         = (struct cgraph_edge *) fibheap_extract_min (heap);
929       struct cgraph_node *cnode;
930
931       depth = 1;
932       for (cnode = curr->caller;
933            cnode->global.inlined_to; cnode = cnode->callers->caller)
934         if (node->decl == curr->callee->decl)
935           depth++;
936       if (depth > max_depth)
937         {
938           if (dump_file)
939             fprintf (dump_file,
940                      "   maximal depth reached\n");
941           continue;
942         }
943
944       if (max_count)
945         {
946           if (!cgraph_maybe_hot_edge_p (curr))
947             {
948               if (dump_file)
949                 fprintf (dump_file, "   Not inlining cold call\n");
950               continue;
951             }
952           if (curr->count * 100 / node->count < probability)
953             {
954               if (dump_file)
955                 fprintf (dump_file,
956                          "   Probability of edge is too small\n");
957               continue;
958             }
959         }
960
961       if (dump_file)
962         {
963           fprintf (dump_file,
964                    "   Inlining call of depth %i", depth);
965           if (node->count)
966             {
967               fprintf (dump_file, " called approx. %.2f times per call",
968                        (double)curr->count / node->count);
969             }
970           fprintf (dump_file, "\n");
971         }
972       cgraph_redirect_edge_callee (curr, master_clone);
973       cgraph_mark_inline_edge (curr, false, new_edges);
974       lookup_recursive_calls (node, curr->callee, heap);
975       n++;
976     }
977   if (!fibheap_empty (heap) && dump_file)
978     fprintf (dump_file, "    Recursive inlining growth limit met.\n");
979
980   fibheap_delete (heap);
981   if (dump_file)
982     fprintf (dump_file,
983              "\n   Inlined %i times, body grown from size %i to %i, time %i to %i\n", n,
984              master_clone->global.size, node->global.size,
985              master_clone->global.time, node->global.time);
986
987   /* Remove master clone we used for inlining.  We rely that clones inlined
988      into master clone gets queued just before master clone so we don't
989      need recursion.  */
990   for (node = cgraph_nodes; node != master_clone;
991        node = next)
992     {
993       next = node->next;
994       if (node->global.inlined_to == master_clone)
995         cgraph_remove_node (node);
996     }
997   cgraph_remove_node (master_clone);
998   /* FIXME: Recursive inlining actually reduces number of calls of the
999      function.  At this place we should probably walk the function and
1000      inline clones and compensate the counts accordingly.  This probably
1001      doesn't matter much in practice.  */
1002   return n > 0;
1003 }
1004
1005 /* Set inline_failed for all callers of given function to REASON.  */
1006
1007 static void
1008 cgraph_set_inline_failed (struct cgraph_node *node,
1009                           cgraph_inline_failed_t reason)
1010 {
1011   struct cgraph_edge *e;
1012
1013   if (dump_file)
1014     fprintf (dump_file, "Inlining failed: %s\n",
1015              cgraph_inline_failed_string (reason));
1016   for (e = node->callers; e; e = e->next_caller)
1017     if (e->inline_failed)
1018       e->inline_failed = reason;
1019 }
1020
1021 /* Given whole compilation unit estimate of INSNS, compute how large we can
1022    allow the unit to grow.  */
1023 static int
1024 compute_max_insns (int insns)
1025 {
1026   int max_insns = insns;
1027   if (max_insns < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
1028     max_insns = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
1029
1030   return ((HOST_WIDEST_INT) max_insns
1031           * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
1032 }
1033
1034 /* Compute badness of all edges in NEW_EDGES and add them to the HEAP.  */
1035 static void
1036 add_new_edges_to_heap (fibheap_t heap, VEC (cgraph_edge_p, heap) *new_edges)
1037 {
1038   while (VEC_length (cgraph_edge_p, new_edges) > 0)
1039     {
1040       struct cgraph_edge *edge = VEC_pop (cgraph_edge_p, new_edges);
1041
1042       gcc_assert (!edge->aux);
1043       if (edge->callee->local.inlinable
1044           && edge->inline_failed
1045           && cgraph_default_inline_p (edge->callee, &edge->inline_failed))
1046         edge->aux = fibheap_insert (heap, cgraph_edge_badness (edge, false), edge);
1047     }
1048 }
1049
1050
1051 /* We use greedy algorithm for inlining of small functions:
1052    All inline candidates are put into prioritized heap based on estimated
1053    growth of the overall number of instructions and then update the estimates.
1054
1055    INLINED and INLINED_CALEES are just pointers to arrays large enough
1056    to be passed to cgraph_inlined_into and cgraph_inlined_callees.  */
1057
1058 static void
1059 cgraph_decide_inlining_of_small_functions (void)
1060 {
1061   struct cgraph_node *node;
1062   struct cgraph_edge *edge;
1063   cgraph_inline_failed_t failed_reason;
1064   fibheap_t heap = fibheap_new ();
1065   bitmap updated_nodes = BITMAP_ALLOC (NULL);
1066   int min_size, max_size;
1067   VEC (cgraph_edge_p, heap) *new_indirect_edges = NULL;
1068
1069   if (flag_indirect_inlining)
1070     new_indirect_edges = VEC_alloc (cgraph_edge_p, heap, 8);
1071
1072   if (dump_file)
1073     fprintf (dump_file, "\nDeciding on smaller functions:\n");
1074
1075   /* Put all inline candidates into the heap.  */
1076
1077   for (node = cgraph_nodes; node; node = node->next)
1078     {
1079       if (!node->local.inlinable || !node->callers)
1080         continue;
1081       if (dump_file)
1082         fprintf (dump_file, "Considering inline candidate %s.\n", cgraph_node_name (node));
1083
1084       node->global.estimated_growth = INT_MIN;
1085       if (!cgraph_default_inline_p (node, &failed_reason))
1086         {
1087           cgraph_set_inline_failed (node, failed_reason);
1088           continue;
1089         }
1090
1091       for (edge = node->callers; edge; edge = edge->next_caller)
1092         if (edge->inline_failed)
1093           {
1094             gcc_assert (!edge->aux);
1095             edge->aux = fibheap_insert (heap, cgraph_edge_badness (edge, false), edge);
1096           }
1097     }
1098
1099   max_size = compute_max_insns (overall_size);
1100   min_size = overall_size;
1101
1102   while (overall_size <= max_size
1103          && !fibheap_empty (heap))
1104     {
1105       int old_size = overall_size;
1106       struct cgraph_node *where, *callee;
1107       int badness = fibheap_min_key (heap);
1108       int current_badness;
1109       int growth;
1110       cgraph_inline_failed_t not_good = CIF_OK;
1111
1112       edge = (struct cgraph_edge *) fibheap_extract_min (heap);
1113       gcc_assert (edge->aux);
1114       edge->aux = NULL;
1115       if (!edge->inline_failed)
1116         continue;
1117
1118       /* When updating the edge costs, we only decrease badness in the keys.
1119          When the badness increase, we keep the heap as it is and re-insert
1120          key now.  */
1121       current_badness = cgraph_edge_badness (edge, false);
1122       gcc_assert (current_badness >= badness);
1123       if (current_badness != badness)
1124         {
1125           edge->aux = fibheap_insert (heap, current_badness, edge);
1126           continue;
1127         }
1128       
1129       callee = edge->callee;
1130
1131       growth = (cgraph_estimate_size_after_inlining (1, edge->caller, edge->callee)
1132                 - edge->caller->global.size);
1133
1134       if (dump_file)
1135         {
1136           fprintf (dump_file,
1137                    "\nConsidering %s with %i size\n",
1138                    cgraph_node_name (edge->callee),
1139                    edge->callee->global.size);
1140           fprintf (dump_file,
1141                    " to be inlined into %s in %s:%i\n"
1142                    " Estimated growth after inlined into all callees is %+i insns.\n"
1143                    " Estimated badness is %i, frequency %.2f.\n",
1144                    cgraph_node_name (edge->caller),
1145                    flag_wpa ? "unknown"
1146                    : gimple_filename ((const_gimple) edge->call_stmt),
1147                    flag_wpa ? -1 : gimple_lineno ((const_gimple) edge->call_stmt),
1148                    cgraph_estimate_growth (edge->callee),
1149                    badness,
1150                    edge->frequency / (double)CGRAPH_FREQ_BASE);
1151           if (edge->count)
1152             fprintf (dump_file," Called "HOST_WIDEST_INT_PRINT_DEC"x\n", edge->count);
1153           if (dump_flags & TDF_DETAILS)
1154             cgraph_edge_badness (edge, true);
1155         }
1156
1157       /* When not having profile info ready we don't weight by any way the
1158          position of call in procedure itself.  This means if call of
1159          function A from function B seems profitable to inline, the recursive
1160          call of function A in inline copy of A in B will look profitable too
1161          and we end up inlining until reaching maximal function growth.  This
1162          is not good idea so prohibit the recursive inlining.
1163
1164          ??? When the frequencies are taken into account we might not need this
1165          restriction.
1166
1167          We need to be cureful here, in some testcases, e.g. directivec.c in
1168          libcpp, we can estimate self recursive function to have negative growth
1169          for inlining completely.
1170          */
1171       if (!edge->count)
1172         {
1173           where = edge->caller;
1174           while (where->global.inlined_to)
1175             {
1176               if (where->decl == edge->callee->decl)
1177                 break;
1178               where = where->callers->caller;
1179             }
1180           if (where->global.inlined_to)
1181             {
1182               edge->inline_failed
1183                 = (edge->callee->local.disregard_inline_limits
1184                    ? CIF_RECURSIVE_INLINING : CIF_UNSPECIFIED);
1185               if (dump_file)
1186                 fprintf (dump_file, " inline_failed:Recursive inlining performed only for function itself.\n");
1187               continue;
1188             }
1189         }
1190
1191       if (edge->callee->local.disregard_inline_limits)
1192         ;
1193       else if (!cgraph_maybe_hot_edge_p (edge))
1194         not_good = CIF_UNLIKELY_CALL;
1195       else if (!flag_inline_functions
1196           && !DECL_DECLARED_INLINE_P (edge->callee->decl))
1197         not_good = CIF_NOT_DECLARED_INLINED;
1198       else if (optimize_function_for_size_p (DECL_STRUCT_FUNCTION(edge->caller->decl)))
1199         not_good = CIF_OPTIMIZING_FOR_SIZE;
1200       if (not_good && growth > 0 && cgraph_estimate_growth (edge->callee) > 0)
1201         {
1202           if (!cgraph_recursive_inlining_p (edge->caller, edge->callee,
1203                                             &edge->inline_failed))
1204             {
1205               edge->inline_failed = not_good;
1206               if (dump_file)
1207                 fprintf (dump_file, " inline_failed:%s.\n",
1208                          cgraph_inline_failed_string (edge->inline_failed));
1209             }
1210           continue;
1211         }
1212       if (!cgraph_default_inline_p (edge->callee, &edge->inline_failed))
1213         {
1214           if (!cgraph_recursive_inlining_p (edge->caller, edge->callee,
1215                                             &edge->inline_failed))
1216             {
1217               if (dump_file)
1218                 fprintf (dump_file, " inline_failed:%s.\n",
1219                          cgraph_inline_failed_string (edge->inline_failed));
1220             }
1221           continue;
1222         }
1223       if (!tree_can_inline_p (edge))
1224         {
1225           if (dump_file)
1226             fprintf (dump_file, " inline_failed:%s.\n",
1227                      cgraph_inline_failed_string (edge->inline_failed));
1228           continue;
1229         }
1230       if (cgraph_recursive_inlining_p (edge->caller, edge->callee,
1231                                        &edge->inline_failed))
1232         {
1233           where = edge->caller;
1234           if (where->global.inlined_to)
1235             where = where->global.inlined_to;
1236           if (!cgraph_decide_recursive_inlining (where,
1237                                                  flag_indirect_inlining
1238                                                  ? &new_indirect_edges : NULL))
1239             continue;
1240           if (flag_indirect_inlining)
1241             add_new_edges_to_heap (heap, new_indirect_edges);
1242           update_all_callee_keys (heap, where, updated_nodes);
1243         }
1244       else
1245         {
1246           struct cgraph_node *callee;
1247           if (edge->call_stmt_cannot_inline_p
1248               || !cgraph_check_inline_limits (edge->caller, edge->callee,
1249                                               &edge->inline_failed, true))
1250             {
1251               if (dump_file)
1252                 fprintf (dump_file, " Not inlining into %s:%s.\n",
1253                          cgraph_node_name (edge->caller),
1254                          cgraph_inline_failed_string (edge->inline_failed));
1255               continue;
1256             }
1257           callee = edge->callee;
1258           gcc_checking_assert (!callee->global.inlined_to);
1259           cgraph_mark_inline_edge (edge, true, &new_indirect_edges);
1260           if (flag_indirect_inlining)
1261             add_new_edges_to_heap (heap, new_indirect_edges);
1262
1263           /* We inlined last offline copy to the body.  This might lead
1264              to callees of function having fewer call sites and thus they
1265              may need updating.  */
1266           if (callee->global.inlined_to)
1267             update_all_callee_keys (heap, callee, updated_nodes);
1268           else
1269             update_callee_keys (heap, edge->callee, updated_nodes);
1270         }
1271       where = edge->caller;
1272       if (where->global.inlined_to)
1273         where = where->global.inlined_to;
1274
1275       /* Our profitability metric can depend on local properties
1276          such as number of inlinable calls and size of the function body.
1277          After inlining these properties might change for the function we
1278          inlined into (since it's body size changed) and for the functions
1279          called by function we inlined (since number of it inlinable callers
1280          might change).  */
1281       update_caller_keys (heap, where, updated_nodes);
1282
1283       /* We removed one call of the function we just inlined.  If offline
1284          copy is still needed, be sure to update the keys.  */
1285       if (callee != where && !callee->global.inlined_to)
1286         update_caller_keys (heap, callee, updated_nodes);
1287       bitmap_clear (updated_nodes);
1288
1289       if (dump_file)
1290         {
1291           fprintf (dump_file,
1292                    " Inlined into %s which now has size %i and self time %i,"
1293                    "net change of %+i.\n",
1294                    cgraph_node_name (edge->caller),
1295                    edge->caller->global.time,
1296                    edge->caller->global.size,
1297                    overall_size - old_size);
1298         }
1299       if (min_size > overall_size)
1300         {
1301           min_size = overall_size;
1302           max_size = compute_max_insns (min_size);
1303
1304           if (dump_file)
1305             fprintf (dump_file, "New minimal size reached: %i\n", min_size);
1306         }
1307     }
1308   while (!fibheap_empty (heap))
1309     {
1310       int badness = fibheap_min_key (heap);
1311
1312       edge = (struct cgraph_edge *) fibheap_extract_min (heap);
1313       gcc_assert (edge->aux);
1314       edge->aux = NULL;
1315       if (!edge->inline_failed)
1316         continue;
1317 #ifdef ENABLE_CHECKING
1318       gcc_assert (cgraph_edge_badness (edge, false) >= badness);
1319 #endif
1320       if (dump_file)
1321         {
1322           fprintf (dump_file,
1323                    "\nSkipping %s with %i size\n",
1324                    cgraph_node_name (edge->callee),
1325                    edge->callee->global.size);
1326           fprintf (dump_file,
1327                    " called by %s in %s:%i\n"
1328                    " Estimated growth after inlined into all callees is %+i insns.\n"
1329                    " Estimated badness is %i, frequency %.2f.\n",
1330                    cgraph_node_name (edge->caller),
1331                    flag_wpa ? "unknown"
1332                    : gimple_filename ((const_gimple) edge->call_stmt),
1333                    flag_wpa ? -1 : gimple_lineno ((const_gimple) edge->call_stmt),
1334                    cgraph_estimate_growth (edge->callee),
1335                    badness,
1336                    edge->frequency / (double)CGRAPH_FREQ_BASE);
1337           if (edge->count)
1338             fprintf (dump_file," Called "HOST_WIDEST_INT_PRINT_DEC"x\n", edge->count);
1339           if (dump_flags & TDF_DETAILS)
1340             cgraph_edge_badness (edge, true);
1341         }
1342       if (!edge->callee->local.disregard_inline_limits && edge->inline_failed
1343           && !cgraph_recursive_inlining_p (edge->caller, edge->callee,
1344                                            &edge->inline_failed))
1345         edge->inline_failed = CIF_INLINE_UNIT_GROWTH_LIMIT;
1346     }
1347
1348   if (new_indirect_edges)
1349     VEC_free (cgraph_edge_p, heap, new_indirect_edges);
1350   fibheap_delete (heap);
1351   BITMAP_FREE (updated_nodes);
1352 }
1353
1354 /* Flatten NODE from the IPA inliner.  */
1355
1356 static void
1357 cgraph_flatten (struct cgraph_node *node)
1358 {
1359   struct cgraph_edge *e;
1360
1361   /* We shouldn't be called recursively when we are being processed.  */
1362   gcc_assert (node->aux == NULL);
1363
1364   node->aux = (void *)(size_t) INLINE_ALL;
1365
1366   for (e = node->callees; e; e = e->next_callee)
1367     {
1368       struct cgraph_node *orig_callee;
1369
1370       if (e->call_stmt_cannot_inline_p)
1371         continue;
1372
1373       if (!e->callee->analyzed)
1374         {
1375           if (dump_file)
1376             fprintf (dump_file,
1377                      "Not inlining: Function body not available.\n");
1378           continue;
1379         }
1380
1381       /* We've hit cycle?  It is time to give up.  */
1382       if (e->callee->aux)
1383         {
1384           if (dump_file)
1385             fprintf (dump_file,
1386                      "Not inlining %s into %s to avoid cycle.\n",
1387                      cgraph_node_name (e->callee),
1388                      cgraph_node_name (e->caller));
1389           e->inline_failed = CIF_RECURSIVE_INLINING;
1390           continue;
1391         }
1392
1393       /* When the edge is already inlined, we just need to recurse into
1394          it in order to fully flatten the leaves.  */
1395       if (!e->inline_failed)
1396         {
1397           cgraph_flatten (e->callee);
1398           continue;
1399         }
1400
1401       if (cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed))
1402         {
1403           if (dump_file)
1404             fprintf (dump_file, "Not inlining: recursive call.\n");
1405           continue;
1406         }
1407
1408       if (!tree_can_inline_p (e))
1409         {
1410           if (dump_file)
1411             fprintf (dump_file, "Not inlining: %s",
1412                      cgraph_inline_failed_string (e->inline_failed));
1413           continue;
1414         }
1415
1416       if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
1417           != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->callee->decl)))
1418         {
1419           if (dump_file)
1420             fprintf (dump_file, "Not inlining: SSA form does not match.\n");
1421           continue;
1422         }
1423
1424       /* Inline the edge and flatten the inline clone.  Avoid
1425          recursing through the original node if the node was cloned.  */
1426       if (dump_file)
1427         fprintf (dump_file, " Inlining %s into %s.\n",
1428                  cgraph_node_name (e->callee),
1429                  cgraph_node_name (e->caller));
1430       orig_callee = e->callee;
1431       cgraph_mark_inline_edge (e, true, NULL);
1432       if (e->callee != orig_callee)
1433         orig_callee->aux = (void *)(size_t) INLINE_ALL;
1434       cgraph_flatten (e->callee);
1435       if (e->callee != orig_callee)
1436         orig_callee->aux = NULL;
1437     }
1438
1439   node->aux = NULL;
1440 }
1441
1442 /* Decide on the inlining.  We do so in the topological order to avoid
1443    expenses on updating data structures.  */
1444
1445 static unsigned int
1446 cgraph_decide_inlining (void)
1447 {
1448   struct cgraph_node *node;
1449   int nnodes;
1450   struct cgraph_node **order =
1451     XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1452   int old_size = 0;
1453   int i;
1454   int initial_size = 0;
1455
1456   cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
1457   if (in_lto_p && flag_indirect_inlining)
1458     ipa_update_after_lto_read ();
1459   if (flag_indirect_inlining)
1460     ipa_create_all_structures_for_iinln ();
1461
1462   max_count = 0;
1463   max_benefit = 0;
1464   for (node = cgraph_nodes; node; node = node->next)
1465     if (node->analyzed)
1466       {
1467         struct cgraph_edge *e;
1468
1469         gcc_assert (inline_summary (node)->self_size == node->global.size);
1470         if (!DECL_EXTERNAL (node->decl))
1471           initial_size += node->global.size;
1472         for (e = node->callees; e; e = e->next_callee)
1473           if (max_count < e->count)
1474             max_count = e->count;
1475         if (max_benefit < inline_summary (node)->time_inlining_benefit)
1476           max_benefit = inline_summary (node)->time_inlining_benefit;
1477       }
1478   gcc_assert (in_lto_p
1479               || !max_count
1480               || (profile_info && flag_branch_probabilities));
1481   overall_size = initial_size;
1482
1483   nnodes = cgraph_postorder (order);
1484
1485   if (dump_file)
1486     fprintf (dump_file,
1487              "\nDeciding on inlining.  Starting with size %i.\n",
1488              initial_size);
1489
1490   for (node = cgraph_nodes; node; node = node->next)
1491     node->aux = 0;
1492
1493   if (dump_file)
1494     fprintf (dump_file, "\nFlattening functions:\n");
1495
1496   /* In the first pass handle functions to be flattened.  Do this with
1497      a priority so none of our later choices will make this impossible.  */
1498   for (i = nnodes - 1; i >= 0; i--)
1499     {
1500       node = order[i];
1501
1502       /* Handle nodes to be flattened, but don't update overall unit
1503          size.  Calling the incremental inliner here is lame,
1504          a simple worklist should be enough.  What should be left
1505          here from the early inliner (if it runs) is cyclic cases.
1506          Ideally when processing callees we stop inlining at the
1507          entry of cycles, possibly cloning that entry point and
1508          try to flatten itself turning it into a self-recursive
1509          function.  */
1510       if (lookup_attribute ("flatten",
1511                             DECL_ATTRIBUTES (node->decl)) != NULL)
1512         {
1513           if (dump_file)
1514             fprintf (dump_file,
1515                      "Flattening %s\n", cgraph_node_name (node));
1516           cgraph_flatten (node);
1517         }
1518     }
1519
1520   cgraph_decide_inlining_of_small_functions ();
1521
1522   if (flag_inline_functions_called_once)
1523     {
1524       if (dump_file)
1525         fprintf (dump_file, "\nDeciding on functions called once:\n");
1526
1527       /* And finally decide what functions are called once.  */
1528       for (i = nnodes - 1; i >= 0; i--)
1529         {
1530           node = order[i];
1531
1532           if (node->callers
1533               && !node->callers->next_caller
1534               && !node->global.inlined_to
1535               && cgraph_will_be_removed_from_program_if_no_direct_calls (node)
1536               && node->local.inlinable
1537               && cgraph_function_body_availability (node) >= AVAIL_AVAILABLE
1538               && node->callers->inline_failed
1539               && node->callers->caller != node
1540               && node->callers->caller->global.inlined_to != node
1541               && !node->callers->call_stmt_cannot_inline_p
1542               && !DECL_EXTERNAL (node->decl))
1543             {
1544               cgraph_inline_failed_t reason;
1545               old_size = overall_size;
1546               if (dump_file)
1547                 {
1548                   fprintf (dump_file,
1549                            "\nConsidering %s size %i.\n",
1550                            cgraph_node_name (node), node->global.size);
1551                   fprintf (dump_file,
1552                            " Called once from %s %i insns.\n",
1553                            cgraph_node_name (node->callers->caller),
1554                            node->callers->caller->global.size);
1555                 }
1556
1557               if (cgraph_check_inline_limits (node->callers->caller, node,
1558                                               &reason, false))
1559                 {
1560                   struct cgraph_node *caller = node->callers->caller;
1561                   cgraph_mark_inline (node->callers);
1562                   if (dump_file)
1563                     fprintf (dump_file,
1564                              " Inlined into %s which now has %i size"
1565                              " for a net change of %+i size.\n",
1566                              cgraph_node_name (caller),
1567                              caller->global.size,
1568                              overall_size - old_size);
1569                 }
1570               else
1571                 {
1572                   if (dump_file)
1573                     fprintf (dump_file,
1574                              " Not inlining: %s.\n",
1575                              cgraph_inline_failed_string (reason));
1576                 }
1577             }
1578         }
1579     }
1580
1581   /* Free ipa-prop structures if they are no longer needed.  */
1582   if (flag_indirect_inlining)
1583     ipa_free_all_structures_after_iinln ();
1584
1585   if (dump_file)
1586     fprintf (dump_file,
1587              "\nInlined %i calls, eliminated %i functions, "
1588              "size %i turned to %i size.\n\n",
1589              ncalls_inlined, nfunctions_inlined, initial_size,
1590              overall_size);
1591   free (order);
1592   return 0;
1593 }
1594
1595 /* Return true when N is leaf function.  Accept cheap builtins
1596    in leaf functions.  */
1597
1598 static bool
1599 leaf_node_p (struct cgraph_node *n)
1600 {
1601   struct cgraph_edge *e;
1602   for (e = n->callees; e; e = e->next_callee)
1603     if (!is_inexpensive_builtin (e->callee->decl))
1604       return false;
1605   return true;
1606 }
1607
1608 /* Decide on the inlining.  We do so in the topological order to avoid
1609    expenses on updating data structures.  */
1610
1611 static bool
1612 cgraph_decide_inlining_incrementally (struct cgraph_node *node,
1613                                       enum inlining_mode mode)
1614 {
1615   struct cgraph_edge *e;
1616   bool inlined = false;
1617   cgraph_inline_failed_t failed_reason;
1618
1619 #ifdef ENABLE_CHECKING
1620   verify_cgraph_node (node);
1621 #endif
1622
1623   if (mode != INLINE_ALWAYS_INLINE && mode != INLINE_SIZE_NORECURSIVE
1624       && lookup_attribute ("flatten", DECL_ATTRIBUTES (node->decl)) != NULL)
1625     {
1626       if (dump_file)
1627         fprintf (dump_file, "Incrementally flattening %s\n",
1628                  cgraph_node_name (node));
1629       mode = INLINE_ALL;
1630     }
1631
1632   /* First of all look for always inline functions.  */
1633   if (mode != INLINE_SIZE_NORECURSIVE)
1634     for (e = node->callees; e; e = e->next_callee)
1635       {
1636         if (!e->callee->local.disregard_inline_limits
1637             && (mode != INLINE_ALL || !e->callee->local.inlinable))
1638           continue;
1639         if (e->call_stmt_cannot_inline_p)
1640           continue;
1641         if (dump_file)
1642           fprintf (dump_file,
1643                    "Considering to always inline inline candidate %s.\n",
1644                    cgraph_node_name (e->callee));
1645         if (cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed))
1646           {
1647             if (dump_file)
1648               fprintf (dump_file, "Not inlining: recursive call.\n");
1649             continue;
1650           }
1651         if (!tree_can_inline_p (e))
1652           {
1653             if (dump_file)
1654               fprintf (dump_file,
1655                        "Not inlining: %s",
1656                        cgraph_inline_failed_string (e->inline_failed));
1657             continue;
1658           }
1659         if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
1660             != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->callee->decl)))
1661           {
1662             if (dump_file)
1663               fprintf (dump_file, "Not inlining: SSA form does not match.\n");
1664             continue;
1665           }
1666         if (!e->callee->analyzed)
1667           {
1668             if (dump_file)
1669               fprintf (dump_file,
1670                        "Not inlining: Function body no longer available.\n");
1671             continue;
1672           }
1673
1674         if (dump_file)
1675           fprintf (dump_file, " Inlining %s into %s.\n",
1676                    cgraph_node_name (e->callee),
1677                    cgraph_node_name (e->caller));
1678         cgraph_mark_inline (e);
1679         inlined = true;
1680       }
1681
1682   /* Now do the automatic inlining.  */
1683   if (mode != INLINE_ALL && mode != INLINE_ALWAYS_INLINE
1684       /* Never inline regular functions into always-inline functions
1685          during incremental inlining.  */
1686       && !node->local.disregard_inline_limits)
1687     {
1688       bitmap visited = BITMAP_ALLOC (NULL);
1689       for (e = node->callees; e; e = e->next_callee)
1690         {
1691           int allowed_growth = 0;
1692           if (!e->callee->local.inlinable
1693               || !e->inline_failed
1694               || e->callee->local.disregard_inline_limits)
1695             continue;
1696           /* We are inlining a function to all call-sites in node
1697              or to none.  So visit each candidate only once.  */
1698           if (!bitmap_set_bit (visited, e->callee->uid))
1699             continue;
1700           if (dump_file)
1701             fprintf (dump_file, "Considering inline candidate %s.\n",
1702                      cgraph_node_name (e->callee));
1703           if (cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed))
1704             {
1705               if (dump_file)
1706                 fprintf (dump_file, "Not inlining: recursive call.\n");
1707               continue;
1708             }
1709           if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
1710               != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->callee->decl)))
1711             {
1712               if (dump_file)
1713                 fprintf (dump_file,
1714                          "Not inlining: SSA form does not match.\n");
1715               continue;
1716             }
1717
1718           if (cgraph_maybe_hot_edge_p (e) && leaf_node_p (e->callee)
1719               && optimize_function_for_speed_p (cfun))
1720             allowed_growth = PARAM_VALUE (PARAM_EARLY_INLINING_INSNS);
1721
1722           /* When the function body would grow and inlining the function
1723              won't eliminate the need for offline copy of the function,
1724              don't inline.  */
1725           if (((mode == INLINE_SIZE || mode == INLINE_SIZE_NORECURSIVE)
1726                || (!flag_inline_functions
1727                    && !DECL_DECLARED_INLINE_P (e->callee->decl)))
1728               && (cgraph_estimate_size_after_inlining (1, e->caller, e->callee)
1729                   > e->caller->global.size + allowed_growth)
1730               && cgraph_estimate_growth (e->callee) > allowed_growth)
1731             {
1732               if (dump_file)
1733                 fprintf (dump_file,
1734                          "Not inlining: code size would grow by %i.\n",
1735                          cgraph_estimate_size_after_inlining (1, e->caller,
1736                                                               e->callee)
1737                          - e->caller->global.size);
1738               continue;
1739             }
1740           if (!cgraph_check_inline_limits (node, e->callee, &e->inline_failed,
1741                                            false)
1742               || e->call_stmt_cannot_inline_p)
1743             {
1744               if (dump_file)
1745                 fprintf (dump_file, "Not inlining: %s.\n",
1746                          cgraph_inline_failed_string (e->inline_failed));
1747               continue;
1748             }
1749           if (!e->callee->analyzed)
1750             {
1751               if (dump_file)
1752                 fprintf (dump_file,
1753                          "Not inlining: Function body no longer available.\n");
1754               continue;
1755             }
1756           if (!tree_can_inline_p (e))
1757             {
1758               if (dump_file)
1759                 fprintf (dump_file,
1760                          "Not inlining: %s.",
1761                          cgraph_inline_failed_string (e->inline_failed));
1762               continue;
1763             }
1764           if (cgraph_default_inline_p (e->callee, &failed_reason))
1765             {
1766               if (dump_file)
1767                 fprintf (dump_file, " Inlining %s into %s.\n",
1768                          cgraph_node_name (e->callee),
1769                          cgraph_node_name (e->caller));
1770               cgraph_mark_inline (e);
1771               inlined = true;
1772             }
1773         }
1774       BITMAP_FREE (visited);
1775     }
1776   return inlined;
1777 }
1778
1779 /* Because inlining might remove no-longer reachable nodes, we need to
1780    keep the array visible to garbage collector to avoid reading collected
1781    out nodes.  */
1782 static int nnodes;
1783 static GTY ((length ("nnodes"))) struct cgraph_node **order;
1784
1785 /* Do inlining of small functions.  Doing so early helps profiling and other
1786    passes to be somewhat more effective and avoids some code duplication in
1787    later real inlining pass for testcases with very many function calls.  */
1788 static unsigned int
1789 cgraph_early_inlining (void)
1790 {
1791   struct cgraph_node *node = cgraph_node (current_function_decl);
1792   unsigned int todo = 0;
1793   int iterations = 0;
1794
1795   if (seen_error ())
1796     return 0;
1797
1798   if (!optimize
1799       || flag_no_inline
1800       || !flag_early_inlining)
1801     {
1802       /* When not optimizing or not inlining inline only always-inline
1803          functions.  */
1804       cgraph_decide_inlining_incrementally (node, INLINE_ALWAYS_INLINE);
1805       timevar_push (TV_INTEGRATION);
1806       todo |= optimize_inline_calls (current_function_decl);
1807       timevar_pop (TV_INTEGRATION);
1808     }
1809   else
1810     {
1811       if (lookup_attribute ("flatten",
1812                             DECL_ATTRIBUTES (node->decl)) != NULL)
1813         {
1814           if (dump_file)
1815             fprintf (dump_file,
1816                      "Flattening %s\n", cgraph_node_name (node));
1817           cgraph_flatten (node);
1818           timevar_push (TV_INTEGRATION);
1819           todo |= optimize_inline_calls (current_function_decl);
1820           timevar_pop (TV_INTEGRATION);
1821         }
1822       /* We iterate incremental inlining to get trivial cases of indirect
1823          inlining.  */
1824       while (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS)
1825              && cgraph_decide_inlining_incrementally (node,
1826                                                       iterations
1827                                                       ? INLINE_SIZE_NORECURSIVE
1828                                                       : INLINE_SIZE))
1829         {
1830           timevar_push (TV_INTEGRATION);
1831           todo |= optimize_inline_calls (current_function_decl);
1832           iterations++;
1833           timevar_pop (TV_INTEGRATION);
1834         }
1835       if (dump_file)
1836         fprintf (dump_file, "Iterations: %i\n", iterations);
1837     }
1838
1839   cfun->always_inline_functions_inlined = true;
1840
1841   return todo;
1842 }
1843
1844 struct gimple_opt_pass pass_early_inline =
1845 {
1846  {
1847   GIMPLE_PASS,
1848   "einline",                            /* name */
1849   NULL,                                 /* gate */
1850   cgraph_early_inlining,                /* execute */
1851   NULL,                                 /* sub */
1852   NULL,                                 /* next */
1853   0,                                    /* static_pass_number */
1854   TV_INLINE_HEURISTICS,                 /* tv_id */
1855   0,                                    /* properties_required */
1856   0,                                    /* properties_provided */
1857   0,                                    /* properties_destroyed */
1858   0,                                    /* todo_flags_start */
1859   TODO_dump_func                        /* todo_flags_finish */
1860  }
1861 };
1862
1863
1864 /* See if statement might disappear after inlining.
1865    0 - means not eliminated
1866    1 - half of statements goes away
1867    2 - for sure it is eliminated.
1868    We are not terribly sophisficated, basically looking for simple abstraction
1869    penalty wrappers.  */
1870
1871 static int
1872 eliminated_by_inlining_prob (gimple stmt)
1873 {
1874   enum gimple_code code = gimple_code (stmt);
1875   switch (code)
1876     {
1877       case GIMPLE_RETURN:
1878         return 2;
1879       case GIMPLE_ASSIGN:
1880         if (gimple_num_ops (stmt) != 2)
1881           return 0;
1882
1883         /* Casts of parameters, loads from parameters passed by reference
1884            and stores to return value or parameters are often free after
1885            inlining dua to SRA and further combining.
1886            Assume that half of statements goes away.  */
1887         if (gimple_assign_rhs_code (stmt) == CONVERT_EXPR
1888             || gimple_assign_rhs_code (stmt) == NOP_EXPR
1889             || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
1890             || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1891           {
1892             tree rhs = gimple_assign_rhs1 (stmt);
1893             tree lhs = gimple_assign_lhs (stmt);
1894             tree inner_rhs = rhs;
1895             tree inner_lhs = lhs;
1896             bool rhs_free = false;
1897             bool lhs_free = false;
1898
1899             while (handled_component_p (inner_lhs)
1900                    || TREE_CODE (inner_lhs) == MEM_REF)
1901               inner_lhs = TREE_OPERAND (inner_lhs, 0);
1902             while (handled_component_p (inner_rhs)
1903                    || TREE_CODE (inner_rhs) == ADDR_EXPR
1904                    || TREE_CODE (inner_rhs) == MEM_REF)
1905               inner_rhs = TREE_OPERAND (inner_rhs, 0);
1906
1907
1908             if (TREE_CODE (inner_rhs) == PARM_DECL
1909                 || (TREE_CODE (inner_rhs) == SSA_NAME
1910                     && SSA_NAME_IS_DEFAULT_DEF (inner_rhs)
1911                     && TREE_CODE (SSA_NAME_VAR (inner_rhs)) == PARM_DECL))
1912               rhs_free = true;
1913             if (rhs_free && is_gimple_reg (lhs))
1914               lhs_free = true;
1915             if (((TREE_CODE (inner_lhs) == PARM_DECL
1916                   || (TREE_CODE (inner_lhs) == SSA_NAME
1917                       && SSA_NAME_IS_DEFAULT_DEF (inner_lhs)
1918                       && TREE_CODE (SSA_NAME_VAR (inner_lhs)) == PARM_DECL))
1919                  && inner_lhs != lhs)
1920                 || TREE_CODE (inner_lhs) == RESULT_DECL
1921                 || (TREE_CODE (inner_lhs) == SSA_NAME
1922                     && TREE_CODE (SSA_NAME_VAR (inner_lhs)) == RESULT_DECL))
1923               lhs_free = true;
1924             if (lhs_free
1925                 && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1926               rhs_free = true;
1927             if (lhs_free && rhs_free)
1928               return 1;
1929           }
1930         return 0;
1931       default:
1932         return 0;
1933     }
1934 }
1935
1936 /* Compute function body size parameters for NODE.  */
1937
1938 static void
1939 estimate_function_body_sizes (struct cgraph_node *node)
1940 {
1941   gcov_type time = 0;
1942   gcov_type time_inlining_benefit = 0;
1943   /* Estimate static overhead for function prologue/epilogue and alignment. */
1944   int size = 2;
1945   /* Benefits are scaled by probability of elimination that is in range
1946      <0,2>.  */
1947   int size_inlining_benefit = 2 * 2;
1948   basic_block bb;
1949   gimple_stmt_iterator bsi;
1950   struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1951   tree arg;
1952   int freq;
1953   tree funtype = TREE_TYPE (node->decl);
1954
1955   if (dump_file)
1956     fprintf (dump_file, "Analyzing function body size: %s\n",
1957              cgraph_node_name (node));
1958
1959   gcc_assert (my_function && my_function->cfg);
1960   FOR_EACH_BB_FN (bb, my_function)
1961     {
1962       freq = compute_call_stmt_bb_frequency (node->decl, bb);
1963       for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1964         {
1965           gimple stmt = gsi_stmt (bsi);
1966           int this_size = estimate_num_insns (stmt, &eni_size_weights);
1967           int this_time = estimate_num_insns (stmt, &eni_time_weights);
1968           int prob;
1969
1970           if (dump_file && (dump_flags & TDF_DETAILS))
1971             {
1972               fprintf (dump_file, "  freq:%6i size:%3i time:%3i ",
1973                        freq, this_size, this_time);
1974               print_gimple_stmt (dump_file, stmt, 0, 0);
1975             }
1976           this_time *= freq;
1977           time += this_time;
1978           size += this_size;
1979           prob = eliminated_by_inlining_prob (stmt);
1980           if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
1981             fprintf (dump_file, "    50%% will be eliminated by inlining\n");
1982           if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
1983             fprintf (dump_file, "    will eliminated by inlining\n");
1984           size_inlining_benefit += this_size * prob;
1985           time_inlining_benefit += this_time * prob;
1986           gcc_assert (time >= 0);
1987           gcc_assert (size >= 0);
1988         }
1989     }
1990   time = (time + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
1991   time_inlining_benefit = ((time_inlining_benefit + CGRAPH_FREQ_BASE)
1992                            / (CGRAPH_FREQ_BASE * 2));
1993   size_inlining_benefit = (size_inlining_benefit + 1) / 2;
1994   if (dump_file)
1995     fprintf (dump_file, "Overall function body time: %i-%i size: %i-%i\n",
1996              (int)time, (int)time_inlining_benefit,
1997              size, size_inlining_benefit);
1998   time_inlining_benefit += eni_time_weights.call_cost;
1999   size_inlining_benefit += eni_size_weights.call_cost;
2000   if (!VOID_TYPE_P (TREE_TYPE (funtype)))
2001     {
2002       int cost = estimate_move_cost (TREE_TYPE (funtype));
2003       time_inlining_benefit += cost;
2004       size_inlining_benefit += cost;
2005     }
2006   for (arg = DECL_ARGUMENTS (node->decl); arg; arg = DECL_CHAIN (arg))
2007     if (!VOID_TYPE_P (TREE_TYPE (arg)))
2008       {
2009         int cost = estimate_move_cost (TREE_TYPE (arg));
2010         time_inlining_benefit += cost;
2011         size_inlining_benefit += cost;
2012       }
2013   if (time_inlining_benefit > MAX_TIME)
2014     time_inlining_benefit = MAX_TIME;
2015   if (time > MAX_TIME)
2016     time = MAX_TIME;
2017   inline_summary (node)->self_time = time;
2018   inline_summary (node)->self_size = size;
2019   if (dump_file)
2020     fprintf (dump_file, "With function call overhead time: %i-%i size: %i-%i\n",
2021              (int)time, (int)time_inlining_benefit,
2022              size, size_inlining_benefit);
2023   inline_summary (node)->time_inlining_benefit = time_inlining_benefit;
2024   inline_summary (node)->size_inlining_benefit = size_inlining_benefit;
2025 }
2026
2027 /* Compute parameters of functions used by inliner.  */
2028 unsigned int
2029 compute_inline_parameters (struct cgraph_node *node)
2030 {
2031   HOST_WIDE_INT self_stack_size;
2032
2033   gcc_assert (!node->global.inlined_to);
2034
2035   /* Estimate the stack size for the function.  But not at -O0
2036      because estimated_stack_frame_size is a quadratic problem.  */
2037   self_stack_size = optimize ? estimated_stack_frame_size (node->decl) : 0;
2038   inline_summary (node)->estimated_self_stack_size = self_stack_size;
2039   node->global.estimated_stack_size = self_stack_size;
2040   node->global.stack_frame_offset = 0;
2041
2042   /* Can this function be inlined at all?  */
2043   node->local.inlinable = tree_inlinable_function_p (node->decl);
2044   if (node->local.inlinable && !node->local.disregard_inline_limits)
2045     node->local.disregard_inline_limits
2046       = DECL_DISREGARD_INLINE_LIMITS (node->decl);
2047   estimate_function_body_sizes (node);
2048   /* Inlining characteristics are maintained by the cgraph_mark_inline.  */
2049   node->global.time = inline_summary (node)->self_time;
2050   node->global.size = inline_summary (node)->self_size;
2051   return 0;
2052 }
2053
2054
2055 /* Compute parameters of functions used by inliner using
2056    current_function_decl.  */
2057 static unsigned int
2058 compute_inline_parameters_for_current (void)
2059 {
2060   compute_inline_parameters (cgraph_node (current_function_decl));
2061   return 0;
2062 }
2063
2064 struct gimple_opt_pass pass_inline_parameters =
2065 {
2066  {
2067   GIMPLE_PASS,
2068   "inline_param",                       /* name */
2069   NULL,                                 /* gate */
2070   compute_inline_parameters_for_current,/* execute */
2071   NULL,                                 /* sub */
2072   NULL,                                 /* next */
2073   0,                                    /* static_pass_number */
2074   TV_INLINE_HEURISTICS,                 /* tv_id */
2075   0,                                    /* properties_required */
2076   0,                                    /* properties_provided */
2077   0,                                    /* properties_destroyed */
2078   0,                                    /* todo_flags_start */
2079   0                                     /* todo_flags_finish */
2080  }
2081 };
2082
2083 /* This function performs intraprocedural analyzis in NODE that is required to
2084    inline indirect calls.  */
2085 static void
2086 inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
2087 {
2088   ipa_analyze_node (node);
2089   if (dump_file && (dump_flags & TDF_DETAILS))
2090     {
2091       ipa_print_node_params (dump_file, node);
2092       ipa_print_node_jump_functions (dump_file, node);
2093     }
2094 }
2095
2096 /* Note function body size.  */
2097 static void
2098 analyze_function (struct cgraph_node *node)
2099 {
2100   push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2101   current_function_decl = node->decl;
2102
2103   compute_inline_parameters (node);
2104   /* FIXME: We should remove the optimize check after we ensure we never run
2105      IPA passes when not optimizng.  */
2106   if (flag_indirect_inlining && optimize)
2107     inline_indirect_intraprocedural_analysis (node);
2108
2109   current_function_decl = NULL;
2110   pop_cfun ();
2111 }
2112
2113 /* Called when new function is inserted to callgraph late.  */
2114 static void
2115 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2116 {
2117   analyze_function (node);
2118 }
2119
2120 /* Note function body size.  */
2121 static void
2122 inline_generate_summary (void)
2123 {
2124   struct cgraph_node *node;
2125
2126   function_insertion_hook_holder =
2127       cgraph_add_function_insertion_hook (&add_new_function, NULL);
2128
2129   if (flag_indirect_inlining)
2130     {
2131       ipa_register_cgraph_hooks ();
2132       ipa_check_create_node_params ();
2133       ipa_check_create_edge_args ();
2134     }
2135
2136   for (node = cgraph_nodes; node; node = node->next)
2137     if (node->analyzed)
2138       analyze_function (node);
2139
2140   return;
2141 }
2142
2143 /* Apply inline plan to function.  */
2144 static unsigned int
2145 inline_transform (struct cgraph_node *node)
2146 {
2147   unsigned int todo = 0;
2148   struct cgraph_edge *e;
2149   bool inline_p = false;
2150
2151   /* FIXME: Currently the passmanager is adding inline transform more than once to some
2152      clones.  This needs revisiting after WPA cleanups.  */
2153   if (cfun->after_inlining)
2154     return 0;
2155
2156   /* We might need the body of this function so that we can expand
2157      it inline somewhere else.  */
2158   if (cgraph_preserve_function_body_p (node->decl))
2159     save_inline_function_body (node);
2160
2161   for (e = node->callees; e; e = e->next_callee)
2162     {
2163       cgraph_redirect_edge_call_stmt_to_callee (e);
2164       if (!e->inline_failed || warn_inline)
2165         inline_p = true;
2166     }
2167
2168   if (inline_p)
2169     {
2170       timevar_push (TV_INTEGRATION);
2171       todo = optimize_inline_calls (current_function_decl);
2172       timevar_pop (TV_INTEGRATION);
2173     }
2174   cfun->always_inline_functions_inlined = true;
2175   cfun->after_inlining = true;
2176   return todo | execute_fixup_cfg ();
2177 }
2178
2179 /* Read inline summary.  Jump functions are shared among ipa-cp
2180    and inliner, so when ipa-cp is active, we don't need to write them
2181    twice.  */
2182
2183 static void
2184 inline_read_summary (void)
2185 {
2186   if (flag_indirect_inlining)
2187     {
2188       ipa_register_cgraph_hooks ();
2189       if (!flag_ipa_cp)
2190         ipa_prop_read_jump_functions ();
2191     }
2192   function_insertion_hook_holder =
2193       cgraph_add_function_insertion_hook (&add_new_function, NULL);
2194 }
2195
2196 /* Write inline summary for node in SET.
2197    Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
2198    active, we don't need to write them twice.  */
2199
2200 static void
2201 inline_write_summary (cgraph_node_set set,
2202                       varpool_node_set vset ATTRIBUTE_UNUSED)
2203 {
2204   if (flag_indirect_inlining && !flag_ipa_cp)
2205     ipa_prop_write_jump_functions (set);
2206 }
2207
2208 /* When to run IPA inlining.  Inlining of always-inline functions
2209    happens during early inlining.  */
2210
2211 static bool
2212 gate_cgraph_decide_inlining (void)
2213 {
2214   /* ???  We'd like to skip this if not optimizing or not inlining as
2215      all always-inline functions have been processed by early
2216      inlining already.  But this at least breaks EH with C++ as
2217      we need to unconditionally run fixup_cfg even at -O0.
2218      So leave it on unconditionally for now.  */
2219   return 1;
2220 }
2221
2222 struct ipa_opt_pass_d pass_ipa_inline =
2223 {
2224  {
2225   IPA_PASS,
2226   "inline",                             /* name */
2227   gate_cgraph_decide_inlining,          /* gate */
2228   cgraph_decide_inlining,               /* execute */
2229   NULL,                                 /* sub */
2230   NULL,                                 /* next */
2231   0,                                    /* static_pass_number */
2232   TV_INLINE_HEURISTICS,                 /* tv_id */
2233   0,                                    /* properties_required */
2234   0,                                    /* properties_provided */
2235   0,                                    /* properties_destroyed */
2236   TODO_remove_functions,                /* todo_flags_finish */
2237   TODO_dump_cgraph | TODO_dump_func
2238   | TODO_remove_functions | TODO_ggc_collect    /* todo_flags_finish */
2239  },
2240  inline_generate_summary,               /* generate_summary */
2241  inline_write_summary,                  /* write_summary */
2242  inline_read_summary,                   /* read_summary */
2243  NULL,                                  /* write_optimization_summary */
2244  NULL,                                  /* read_optimization_summary */
2245  NULL,                                  /* stmt_fixup */
2246  0,                                     /* TODOs */
2247  inline_transform,                      /* function_transform */
2248  NULL,                                  /* variable_transform */
2249 };
2250
2251
2252 #include "gt-ipa-inline.h"