OSDN Git Service

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