re PR tree-optimization/48063 (ICE: verify_stmts failed: conversion of register to...
[platform/upstream/gcc.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)
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       /* We've hit cycle?  It is time to give up.  */
1341       if (e->callee->aux)
1342         {
1343           if (dump_file)
1344             fprintf (dump_file,
1345                      "Not inlining %s into %s to avoid cycle.\n",
1346                      cgraph_node_name (e->callee),
1347                      cgraph_node_name (e->caller));
1348           e->inline_failed = CIF_RECURSIVE_INLINING;
1349           continue;
1350         }
1351
1352       /* When the edge is already inlined, we just need to recurse into
1353          it in order to fully flatten the leaves.  */
1354       if (!e->inline_failed)
1355         {
1356           cgraph_flatten (e->callee);
1357           continue;
1358         }
1359
1360       if (cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed))
1361         {
1362           if (dump_file)
1363             fprintf (dump_file, "Not inlining: recursive call.\n");
1364           continue;
1365         }
1366
1367       if (!tree_can_inline_p (e))
1368         {
1369           if (dump_file)
1370             fprintf (dump_file, "Not inlining: %s",
1371                      cgraph_inline_failed_string (e->inline_failed));
1372           continue;
1373         }
1374
1375       if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
1376           != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->callee->decl)))
1377         {
1378           if (dump_file)
1379             fprintf (dump_file, "Not inlining: SSA form does not match.\n");
1380           continue;
1381         }
1382
1383       /* Inline the edge and flatten the inline clone.  Avoid
1384          recursing through the original node if the node was cloned.  */
1385       if (dump_file)
1386         fprintf (dump_file, " Inlining %s into %s.\n",
1387                  cgraph_node_name (e->callee),
1388                  cgraph_node_name (e->caller));
1389       orig_callee = e->callee;
1390       cgraph_mark_inline_edge (e, true, NULL);
1391       if (e->callee != orig_callee)
1392         orig_callee->aux = (void *)(size_t) INLINE_ALL;
1393       cgraph_flatten (e->callee);
1394       if (e->callee != orig_callee)
1395         orig_callee->aux = NULL;
1396     }
1397
1398   node->aux = NULL;
1399 }
1400
1401 /* Decide on the inlining.  We do so in the topological order to avoid
1402    expenses on updating data structures.  */
1403
1404 static unsigned int
1405 cgraph_decide_inlining (void)
1406 {
1407   struct cgraph_node *node;
1408   int nnodes;
1409   struct cgraph_node **order =
1410     XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1411   int old_size = 0;
1412   int i;
1413   int initial_size = 0;
1414
1415   cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
1416   if (in_lto_p && flag_indirect_inlining)
1417     ipa_update_after_lto_read ();
1418   if (flag_indirect_inlining)
1419     ipa_create_all_structures_for_iinln ();
1420
1421   max_count = 0;
1422   max_benefit = 0;
1423   for (node = cgraph_nodes; node; node = node->next)
1424     if (node->analyzed)
1425       {
1426         struct cgraph_edge *e;
1427
1428         gcc_assert (inline_summary (node)->self_size == node->global.size);
1429         if (!DECL_EXTERNAL (node->decl))
1430           initial_size += node->global.size;
1431         for (e = node->callees; e; e = e->next_callee)
1432           if (max_count < e->count)
1433             max_count = e->count;
1434         if (max_benefit < inline_summary (node)->time_inlining_benefit)
1435           max_benefit = inline_summary (node)->time_inlining_benefit;
1436       }
1437   gcc_assert (in_lto_p
1438               || !max_count
1439               || (profile_info && flag_branch_probabilities));
1440   overall_size = initial_size;
1441
1442   nnodes = cgraph_postorder (order);
1443
1444   if (dump_file)
1445     fprintf (dump_file,
1446              "\nDeciding on inlining.  Starting with size %i.\n",
1447              initial_size);
1448
1449   for (node = cgraph_nodes; node; node = node->next)
1450     node->aux = 0;
1451
1452   if (dump_file)
1453     fprintf (dump_file, "\nFlattening functions:\n");
1454
1455   /* In the first pass handle functions to be flattened.  Do this with
1456      a priority so none of our later choices will make this impossible.  */
1457   for (i = nnodes - 1; i >= 0; i--)
1458     {
1459       node = order[i];
1460
1461       /* Handle nodes to be flattened, but don't update overall unit
1462          size.  Calling the incremental inliner here is lame,
1463          a simple worklist should be enough.  What should be left
1464          here from the early inliner (if it runs) is cyclic cases.
1465          Ideally when processing callees we stop inlining at the
1466          entry of cycles, possibly cloning that entry point and
1467          try to flatten itself turning it into a self-recursive
1468          function.  */
1469       if (lookup_attribute ("flatten",
1470                             DECL_ATTRIBUTES (node->decl)) != NULL)
1471         {
1472           if (dump_file)
1473             fprintf (dump_file,
1474                      "Flattening %s\n", cgraph_node_name (node));
1475           cgraph_flatten (node);
1476         }
1477     }
1478
1479   cgraph_decide_inlining_of_small_functions ();
1480
1481   if (flag_inline_functions_called_once)
1482     {
1483       if (dump_file)
1484         fprintf (dump_file, "\nDeciding on functions called once:\n");
1485
1486       /* And finally decide what functions are called once.  */
1487       for (i = nnodes - 1; i >= 0; i--)
1488         {
1489           node = order[i];
1490
1491           if (node->callers
1492               && !node->callers->next_caller
1493               && !node->global.inlined_to
1494               && cgraph_will_be_removed_from_program_if_no_direct_calls (node)
1495               && node->local.inlinable
1496               && cgraph_function_body_availability (node) >= AVAIL_AVAILABLE
1497               && node->callers->inline_failed
1498               && node->callers->caller != node
1499               && node->callers->caller->global.inlined_to != node
1500               && !node->callers->call_stmt_cannot_inline_p
1501               && tree_can_inline_p (node->callers)
1502               && !DECL_EXTERNAL (node->decl))
1503             {
1504               cgraph_inline_failed_t reason;
1505               old_size = overall_size;
1506               if (dump_file)
1507                 {
1508                   fprintf (dump_file,
1509                            "\nConsidering %s size %i.\n",
1510                            cgraph_node_name (node), node->global.size);
1511                   fprintf (dump_file,
1512                            " Called once from %s %i insns.\n",
1513                            cgraph_node_name (node->callers->caller),
1514                            node->callers->caller->global.size);
1515                 }
1516
1517               if (cgraph_check_inline_limits (node->callers->caller, node,
1518                                               &reason))
1519                 {
1520                   struct cgraph_node *caller = node->callers->caller;
1521                   cgraph_mark_inline_edge (node->callers, true, NULL);
1522                   if (dump_file)
1523                     fprintf (dump_file,
1524                              " Inlined into %s which now has %i size"
1525                              " for a net change of %+i size.\n",
1526                              cgraph_node_name (caller),
1527                              caller->global.size,
1528                              overall_size - old_size);
1529                 }
1530               else
1531                 {
1532                   if (dump_file)
1533                     fprintf (dump_file,
1534                              " Not inlining: %s.\n",
1535                              cgraph_inline_failed_string (reason));
1536                 }
1537             }
1538         }
1539     }
1540
1541   /* Free ipa-prop structures if they are no longer needed.  */
1542   if (flag_indirect_inlining)
1543     ipa_free_all_structures_after_iinln ();
1544
1545   if (dump_file)
1546     fprintf (dump_file,
1547              "\nInlined %i calls, eliminated %i functions, "
1548              "size %i turned to %i size.\n\n",
1549              ncalls_inlined, nfunctions_inlined, initial_size,
1550              overall_size);
1551   free (order);
1552   return 0;
1553 }
1554
1555 /* Return true when N is leaf function.  Accept cheap builtins
1556    in leaf functions.  */
1557
1558 static bool
1559 leaf_node_p (struct cgraph_node *n)
1560 {
1561   struct cgraph_edge *e;
1562   for (e = n->callees; e; e = e->next_callee)
1563     if (!is_inexpensive_builtin (e->callee->decl))
1564       return false;
1565   return true;
1566 }
1567
1568 /* Decide on the inlining.  We do so in the topological order to avoid
1569    expenses on updating data structures.  */
1570
1571 static bool
1572 cgraph_decide_inlining_incrementally (struct cgraph_node *node,
1573                                       enum inlining_mode mode)
1574 {
1575   struct cgraph_edge *e;
1576   bool inlined = false;
1577   cgraph_inline_failed_t failed_reason;
1578
1579 #ifdef ENABLE_CHECKING
1580   verify_cgraph_node (node);
1581 #endif
1582
1583   if (mode != INLINE_ALWAYS_INLINE && mode != INLINE_SIZE_NORECURSIVE
1584       && lookup_attribute ("flatten", DECL_ATTRIBUTES (node->decl)) != NULL)
1585     {
1586       if (dump_file)
1587         fprintf (dump_file, "Incrementally flattening %s\n",
1588                  cgraph_node_name (node));
1589       mode = INLINE_ALL;
1590     }
1591
1592   /* First of all look for always inline functions.  */
1593   if (mode != INLINE_SIZE_NORECURSIVE)
1594     for (e = node->callees; e; e = e->next_callee)
1595       {
1596         if (!e->callee->local.disregard_inline_limits
1597             && (mode != INLINE_ALL || !e->callee->local.inlinable))
1598           continue;
1599         if (dump_file)
1600           fprintf (dump_file,
1601                    "Considering to always inline inline candidate %s.\n",
1602                    cgraph_node_name (e->callee));
1603         if (cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed))
1604           {
1605             if (dump_file)
1606               fprintf (dump_file, "Not inlining: recursive call.\n");
1607             continue;
1608           }
1609         if (!tree_can_inline_p (e)
1610             || e->call_stmt_cannot_inline_p)
1611           {
1612             if (dump_file)
1613               fprintf (dump_file,
1614                        "Not inlining: %s",
1615                        cgraph_inline_failed_string (e->inline_failed));
1616             continue;
1617           }
1618         if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
1619             != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->callee->decl)))
1620           {
1621             if (dump_file)
1622               fprintf (dump_file, "Not inlining: SSA form does not match.\n");
1623             continue;
1624           }
1625         if (!e->callee->analyzed)
1626           {
1627             if (dump_file)
1628               fprintf (dump_file,
1629                        "Not inlining: Function body no longer available.\n");
1630             continue;
1631           }
1632
1633         if (dump_file)
1634           fprintf (dump_file, " Inlining %s into %s.\n",
1635                    cgraph_node_name (e->callee),
1636                    cgraph_node_name (e->caller));
1637         cgraph_mark_inline_edge (e, true, NULL);
1638         inlined = true;
1639       }
1640
1641   /* Now do the automatic inlining.  */
1642   if (mode != INLINE_ALL && mode != INLINE_ALWAYS_INLINE
1643       /* Never inline regular functions into always-inline functions
1644          during incremental inlining.  */
1645       && !node->local.disregard_inline_limits)
1646     {
1647       for (e = node->callees; e; e = e->next_callee)
1648         {
1649           int allowed_growth = 0;
1650           if (!e->callee->local.inlinable
1651               || !e->inline_failed
1652               || e->callee->local.disregard_inline_limits)
1653             continue;
1654           if (dump_file)
1655             fprintf (dump_file, "Considering inline candidate %s.\n",
1656                      cgraph_node_name (e->callee));
1657           if (cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed))
1658             {
1659               if (dump_file)
1660                 fprintf (dump_file, "Not inlining: recursive call.\n");
1661               continue;
1662             }
1663           if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
1664               != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->callee->decl)))
1665             {
1666               if (dump_file)
1667                 fprintf (dump_file,
1668                          "Not inlining: SSA form does not match.\n");
1669               continue;
1670             }
1671
1672           if (cgraph_maybe_hot_edge_p (e) && leaf_node_p (e->callee)
1673               && optimize_function_for_speed_p (cfun))
1674             allowed_growth = PARAM_VALUE (PARAM_EARLY_INLINING_INSNS);
1675
1676           /* When the function body would grow and inlining the function
1677              won't eliminate the need for offline copy of the function,
1678              don't inline.  */
1679           if (((mode == INLINE_SIZE || mode == INLINE_SIZE_NORECURSIVE)
1680                || (!flag_inline_functions
1681                    && !DECL_DECLARED_INLINE_P (e->callee->decl)))
1682               && (cgraph_estimate_size_after_inlining (e->caller, e->callee)
1683                   > e->caller->global.size + allowed_growth)
1684               && cgraph_estimate_growth (e->callee) > allowed_growth)
1685             {
1686               if (dump_file)
1687                 fprintf (dump_file,
1688                          "Not inlining: code size would grow by %i.\n",
1689                          cgraph_estimate_size_after_inlining (e->caller,
1690                                                               e->callee)
1691                          - e->caller->global.size);
1692               continue;
1693             }
1694           if (e->call_stmt_cannot_inline_p
1695               || !tree_can_inline_p (e))
1696             {
1697               if (dump_file)
1698                 fprintf (dump_file,
1699                          "Not inlining: call site not inlinable.\n");
1700               continue;
1701             }
1702           if (!e->callee->analyzed)
1703             {
1704               if (dump_file)
1705                 fprintf (dump_file,
1706                          "Not inlining: Function body no longer available.\n");
1707               continue;
1708             }
1709           if (!cgraph_check_inline_limits (node, e->callee, &e->inline_failed))
1710             {
1711               if (dump_file)
1712                 fprintf (dump_file, "Not inlining: %s.\n",
1713                          cgraph_inline_failed_string (e->inline_failed));
1714               continue;
1715             }
1716           if (cgraph_default_inline_p (e->callee, &failed_reason))
1717             {
1718               if (dump_file)
1719                 fprintf (dump_file, " Inlining %s into %s.\n",
1720                          cgraph_node_name (e->callee),
1721                          cgraph_node_name (e->caller));
1722               cgraph_mark_inline_edge (e, true, NULL);
1723               inlined = true;
1724             }
1725         }
1726     }
1727   return inlined;
1728 }
1729
1730 /* Because inlining might remove no-longer reachable nodes, we need to
1731    keep the array visible to garbage collector to avoid reading collected
1732    out nodes.  */
1733 static int nnodes;
1734 static GTY ((length ("nnodes"))) struct cgraph_node **order;
1735
1736 /* Do inlining of small functions.  Doing so early helps profiling and other
1737    passes to be somewhat more effective and avoids some code duplication in
1738    later real inlining pass for testcases with very many function calls.  */
1739 static unsigned int
1740 cgraph_early_inlining (void)
1741 {
1742   struct cgraph_node *node = cgraph_node (current_function_decl);
1743   unsigned int todo = 0;
1744   int iterations = 0;
1745
1746   if (seen_error ())
1747     return 0;
1748
1749   if (!optimize
1750       || flag_no_inline
1751       || !flag_early_inlining)
1752     {
1753       /* When not optimizing or not inlining inline only always-inline
1754          functions.  */
1755       cgraph_decide_inlining_incrementally (node, INLINE_ALWAYS_INLINE);
1756       timevar_push (TV_INTEGRATION);
1757       todo |= optimize_inline_calls (current_function_decl);
1758       timevar_pop (TV_INTEGRATION);
1759     }
1760   else
1761     {
1762       if (lookup_attribute ("flatten",
1763                             DECL_ATTRIBUTES (node->decl)) != NULL)
1764         {
1765           if (dump_file)
1766             fprintf (dump_file,
1767                      "Flattening %s\n", cgraph_node_name (node));
1768           cgraph_flatten (node);
1769           timevar_push (TV_INTEGRATION);
1770           todo |= optimize_inline_calls (current_function_decl);
1771           timevar_pop (TV_INTEGRATION);
1772         }
1773       /* We iterate incremental inlining to get trivial cases of indirect
1774          inlining.  */
1775       while (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS)
1776              && cgraph_decide_inlining_incrementally (node,
1777                                                       iterations
1778                                                       ? INLINE_SIZE_NORECURSIVE
1779                                                       : INLINE_SIZE))
1780         {
1781           timevar_push (TV_INTEGRATION);
1782           todo |= optimize_inline_calls (current_function_decl);
1783           iterations++;
1784           timevar_pop (TV_INTEGRATION);
1785         }
1786       if (dump_file)
1787         fprintf (dump_file, "Iterations: %i\n", iterations);
1788     }
1789
1790   cfun->always_inline_functions_inlined = true;
1791
1792   return todo;
1793 }
1794
1795 struct gimple_opt_pass pass_early_inline =
1796 {
1797  {
1798   GIMPLE_PASS,
1799   "einline",                            /* name */
1800   NULL,                                 /* gate */
1801   cgraph_early_inlining,                /* execute */
1802   NULL,                                 /* sub */
1803   NULL,                                 /* next */
1804   0,                                    /* static_pass_number */
1805   TV_INLINE_HEURISTICS,                 /* tv_id */
1806   0,                                    /* properties_required */
1807   0,                                    /* properties_provided */
1808   0,                                    /* properties_destroyed */
1809   0,                                    /* todo_flags_start */
1810   TODO_dump_func                        /* todo_flags_finish */
1811  }
1812 };
1813
1814
1815 /* See if statement might disappear after inlining.
1816    0 - means not eliminated
1817    1 - half of statements goes away
1818    2 - for sure it is eliminated.
1819    We are not terribly sophisticated, basically looking for simple abstraction
1820    penalty wrappers.  */
1821
1822 static int
1823 eliminated_by_inlining_prob (gimple stmt)
1824 {
1825   enum gimple_code code = gimple_code (stmt);
1826   switch (code)
1827     {
1828       case GIMPLE_RETURN:
1829         return 2;
1830       case GIMPLE_ASSIGN:
1831         if (gimple_num_ops (stmt) != 2)
1832           return 0;
1833
1834         /* Casts of parameters, loads from parameters passed by reference
1835            and stores to return value or parameters are often free after
1836            inlining dua to SRA and further combining.
1837            Assume that half of statements goes away.  */
1838         if (gimple_assign_rhs_code (stmt) == CONVERT_EXPR
1839             || gimple_assign_rhs_code (stmt) == NOP_EXPR
1840             || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
1841             || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1842           {
1843             tree rhs = gimple_assign_rhs1 (stmt);
1844             tree lhs = gimple_assign_lhs (stmt);
1845             tree inner_rhs = rhs;
1846             tree inner_lhs = lhs;
1847             bool rhs_free = false;
1848             bool lhs_free = false;
1849
1850             while (handled_component_p (inner_lhs)
1851                    || TREE_CODE (inner_lhs) == MEM_REF)
1852               inner_lhs = TREE_OPERAND (inner_lhs, 0);
1853             while (handled_component_p (inner_rhs)
1854                    || TREE_CODE (inner_rhs) == ADDR_EXPR
1855                    || TREE_CODE (inner_rhs) == MEM_REF)
1856               inner_rhs = TREE_OPERAND (inner_rhs, 0);
1857
1858
1859             if (TREE_CODE (inner_rhs) == PARM_DECL
1860                 || (TREE_CODE (inner_rhs) == SSA_NAME
1861                     && SSA_NAME_IS_DEFAULT_DEF (inner_rhs)
1862                     && TREE_CODE (SSA_NAME_VAR (inner_rhs)) == PARM_DECL))
1863               rhs_free = true;
1864             if (rhs_free && is_gimple_reg (lhs))
1865               lhs_free = true;
1866             if (((TREE_CODE (inner_lhs) == PARM_DECL
1867                   || (TREE_CODE (inner_lhs) == SSA_NAME
1868                       && SSA_NAME_IS_DEFAULT_DEF (inner_lhs)
1869                       && TREE_CODE (SSA_NAME_VAR (inner_lhs)) == PARM_DECL))
1870                  && inner_lhs != lhs)
1871                 || TREE_CODE (inner_lhs) == RESULT_DECL
1872                 || (TREE_CODE (inner_lhs) == SSA_NAME
1873                     && TREE_CODE (SSA_NAME_VAR (inner_lhs)) == RESULT_DECL))
1874               lhs_free = true;
1875             if (lhs_free
1876                 && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1877               rhs_free = true;
1878             if (lhs_free && rhs_free)
1879               return 1;
1880           }
1881         return 0;
1882       default:
1883         return 0;
1884     }
1885 }
1886
1887 /* Compute function body size parameters for NODE.  */
1888
1889 static void
1890 estimate_function_body_sizes (struct cgraph_node *node)
1891 {
1892   gcov_type time = 0;
1893   gcov_type time_inlining_benefit = 0;
1894   /* Estimate static overhead for function prologue/epilogue and alignment. */
1895   int size = 2;
1896   /* Benefits are scaled by probability of elimination that is in range
1897      <0,2>.  */
1898   int size_inlining_benefit = 2 * 2;
1899   basic_block bb;
1900   gimple_stmt_iterator bsi;
1901   struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1902   tree arg;
1903   int freq;
1904   tree funtype = TREE_TYPE (node->decl);
1905
1906   if (dump_file)
1907     fprintf (dump_file, "Analyzing function body size: %s\n",
1908              cgraph_node_name (node));
1909
1910   gcc_assert (my_function && my_function->cfg);
1911   FOR_EACH_BB_FN (bb, my_function)
1912     {
1913       freq = compute_call_stmt_bb_frequency (node->decl, bb);
1914       for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1915         {
1916           gimple stmt = gsi_stmt (bsi);
1917           int this_size = estimate_num_insns (stmt, &eni_size_weights);
1918           int this_time = estimate_num_insns (stmt, &eni_time_weights);
1919           int prob;
1920
1921           if (dump_file && (dump_flags & TDF_DETAILS))
1922             {
1923               fprintf (dump_file, "  freq:%6i size:%3i time:%3i ",
1924                        freq, this_size, this_time);
1925               print_gimple_stmt (dump_file, stmt, 0, 0);
1926             }
1927           this_time *= freq;
1928           time += this_time;
1929           size += this_size;
1930           prob = eliminated_by_inlining_prob (stmt);
1931           if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
1932             fprintf (dump_file, "    50%% will be eliminated by inlining\n");
1933           if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
1934             fprintf (dump_file, "    will eliminated by inlining\n");
1935           size_inlining_benefit += this_size * prob;
1936           time_inlining_benefit += this_time * prob;
1937           gcc_assert (time >= 0);
1938           gcc_assert (size >= 0);
1939         }
1940     }
1941   time = (time + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
1942   time_inlining_benefit = ((time_inlining_benefit + CGRAPH_FREQ_BASE)
1943                            / (CGRAPH_FREQ_BASE * 2));
1944   size_inlining_benefit = (size_inlining_benefit + 1) / 2;
1945   if (dump_file)
1946     fprintf (dump_file, "Overall function body time: %i-%i size: %i-%i\n",
1947              (int)time, (int)time_inlining_benefit,
1948              size, size_inlining_benefit);
1949   time_inlining_benefit += eni_time_weights.call_cost;
1950   size_inlining_benefit += eni_size_weights.call_cost;
1951   if (!VOID_TYPE_P (TREE_TYPE (funtype)))
1952     {
1953       int cost = estimate_move_cost (TREE_TYPE (funtype));
1954       time_inlining_benefit += cost;
1955       size_inlining_benefit += cost;
1956     }
1957   for (arg = DECL_ARGUMENTS (node->decl); arg; arg = DECL_CHAIN (arg))
1958     if (!VOID_TYPE_P (TREE_TYPE (arg)))
1959       {
1960         int cost = estimate_move_cost (TREE_TYPE (arg));
1961         time_inlining_benefit += cost;
1962         size_inlining_benefit += cost;
1963       }
1964   if (time_inlining_benefit > MAX_TIME)
1965     time_inlining_benefit = MAX_TIME;
1966   if (time > MAX_TIME)
1967     time = MAX_TIME;
1968   inline_summary (node)->self_time = time;
1969   inline_summary (node)->self_size = size;
1970   if (dump_file)
1971     fprintf (dump_file, "With function call overhead time: %i-%i size: %i-%i\n",
1972              (int)time, (int)time_inlining_benefit,
1973              size, size_inlining_benefit);
1974   inline_summary (node)->time_inlining_benefit = time_inlining_benefit;
1975   inline_summary (node)->size_inlining_benefit = size_inlining_benefit;
1976 }
1977
1978 /* Compute parameters of functions used by inliner.  */
1979 void
1980 compute_inline_parameters (struct cgraph_node *node)
1981 {
1982   HOST_WIDE_INT self_stack_size;
1983
1984   gcc_assert (!node->global.inlined_to);
1985
1986   /* Estimate the stack size for the function if we're optimizing.  */
1987   self_stack_size = optimize ? estimated_stack_frame_size (node) : 0;
1988   inline_summary (node)->estimated_self_stack_size = self_stack_size;
1989   node->global.estimated_stack_size = self_stack_size;
1990   node->global.stack_frame_offset = 0;
1991
1992   /* Can this function be inlined at all?  */
1993   node->local.inlinable = tree_inlinable_function_p (node->decl);
1994   if (!node->local.inlinable)
1995     node->local.disregard_inline_limits = 0;
1996
1997   /* Inlinable functions always can change signature.  */
1998   if (node->local.inlinable)
1999     node->local.can_change_signature = true;
2000   else
2001     {
2002       struct cgraph_edge *e;
2003
2004       /* Functions calling builtin_apply can not change signature.  */
2005       for (e = node->callees; e; e = e->next_callee)
2006         if (DECL_BUILT_IN (e->callee->decl)
2007             && DECL_BUILT_IN_CLASS (e->callee->decl) == BUILT_IN_NORMAL
2008             && DECL_FUNCTION_CODE (e->callee->decl) == BUILT_IN_APPLY_ARGS)
2009           break;
2010       node->local.can_change_signature = !e;
2011     }
2012   estimate_function_body_sizes (node);
2013   /* Inlining characteristics are maintained by the cgraph_mark_inline.  */
2014   node->global.time = inline_summary (node)->self_time;
2015   node->global.size = inline_summary (node)->self_size;
2016 }
2017
2018
2019 /* Compute parameters of functions used by inliner using
2020    current_function_decl.  */
2021 static unsigned int
2022 compute_inline_parameters_for_current (void)
2023 {
2024   compute_inline_parameters (cgraph_node (current_function_decl));
2025   return 0;
2026 }
2027
2028 struct gimple_opt_pass pass_inline_parameters =
2029 {
2030  {
2031   GIMPLE_PASS,
2032   "inline_param",                       /* name */
2033   NULL,                                 /* gate */
2034   compute_inline_parameters_for_current,/* execute */
2035   NULL,                                 /* sub */
2036   NULL,                                 /* next */
2037   0,                                    /* static_pass_number */
2038   TV_INLINE_HEURISTICS,                 /* tv_id */
2039   0,                                    /* properties_required */
2040   0,                                    /* properties_provided */
2041   0,                                    /* properties_destroyed */
2042   0,                                    /* todo_flags_start */
2043   0                                     /* todo_flags_finish */
2044  }
2045 };
2046
2047 /* This function performs intraprocedural analysis in NODE that is required to
2048    inline indirect calls.  */
2049 static void
2050 inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
2051 {
2052   ipa_analyze_node (node);
2053   if (dump_file && (dump_flags & TDF_DETAILS))
2054     {
2055       ipa_print_node_params (dump_file, node);
2056       ipa_print_node_jump_functions (dump_file, node);
2057     }
2058 }
2059
2060 /* Note function body size.  */
2061 static void
2062 analyze_function (struct cgraph_node *node)
2063 {
2064   push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2065   current_function_decl = node->decl;
2066
2067   compute_inline_parameters (node);
2068   /* FIXME: We should remove the optimize check after we ensure we never run
2069      IPA passes when not optimizing.  */
2070   if (flag_indirect_inlining && optimize)
2071     inline_indirect_intraprocedural_analysis (node);
2072
2073   current_function_decl = NULL;
2074   pop_cfun ();
2075 }
2076
2077 /* Called when new function is inserted to callgraph late.  */
2078 static void
2079 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2080 {
2081   analyze_function (node);
2082 }
2083
2084 /* Note function body size.  */
2085 static void
2086 inline_generate_summary (void)
2087 {
2088   struct cgraph_node *node;
2089
2090   function_insertion_hook_holder =
2091       cgraph_add_function_insertion_hook (&add_new_function, NULL);
2092
2093   if (flag_indirect_inlining)
2094     {
2095       ipa_register_cgraph_hooks ();
2096       ipa_check_create_node_params ();
2097       ipa_check_create_edge_args ();
2098     }
2099
2100   for (node = cgraph_nodes; node; node = node->next)
2101     if (node->analyzed)
2102       analyze_function (node);
2103
2104   return;
2105 }
2106
2107 /* Apply inline plan to function.  */
2108 static unsigned int
2109 inline_transform (struct cgraph_node *node)
2110 {
2111   unsigned int todo = 0;
2112   struct cgraph_edge *e;
2113   bool inline_p = false;
2114
2115   /* FIXME: Currently the pass manager is adding inline transform more than once to some
2116      clones.  This needs revisiting after WPA cleanups.  */
2117   if (cfun->after_inlining)
2118     return 0;
2119
2120   /* We might need the body of this function so that we can expand
2121      it inline somewhere else.  */
2122   if (cgraph_preserve_function_body_p (node->decl))
2123     save_inline_function_body (node);
2124
2125   for (e = node->callees; e; e = e->next_callee)
2126     {
2127       cgraph_redirect_edge_call_stmt_to_callee (e);
2128       if (!e->inline_failed || warn_inline)
2129         inline_p = true;
2130     }
2131
2132   if (inline_p)
2133     {
2134       timevar_push (TV_INTEGRATION);
2135       todo = optimize_inline_calls (current_function_decl);
2136       timevar_pop (TV_INTEGRATION);
2137     }
2138   cfun->always_inline_functions_inlined = true;
2139   cfun->after_inlining = true;
2140   return todo | execute_fixup_cfg ();
2141 }
2142
2143 /* Read inline summary.  Jump functions are shared among ipa-cp
2144    and inliner, so when ipa-cp is active, we don't need to write them
2145    twice.  */
2146
2147 static void
2148 inline_read_summary (void)
2149 {
2150   if (flag_indirect_inlining)
2151     {
2152       ipa_register_cgraph_hooks ();
2153       if (!flag_ipa_cp)
2154         ipa_prop_read_jump_functions ();
2155     }
2156   function_insertion_hook_holder =
2157       cgraph_add_function_insertion_hook (&add_new_function, NULL);
2158 }
2159
2160 /* Write inline summary for node in SET.
2161    Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
2162    active, we don't need to write them twice.  */
2163
2164 static void
2165 inline_write_summary (cgraph_node_set set,
2166                       varpool_node_set vset ATTRIBUTE_UNUSED)
2167 {
2168   if (flag_indirect_inlining && !flag_ipa_cp)
2169     ipa_prop_write_jump_functions (set);
2170 }
2171
2172 /* When to run IPA inlining.  Inlining of always-inline functions
2173    happens during early inlining.  */
2174
2175 static bool
2176 gate_cgraph_decide_inlining (void)
2177 {
2178   /* ???  We'd like to skip this if not optimizing or not inlining as
2179      all always-inline functions have been processed by early
2180      inlining already.  But this at least breaks EH with C++ as
2181      we need to unconditionally run fixup_cfg even at -O0.
2182      So leave it on unconditionally for now.  */
2183   return 1;
2184 }
2185
2186 struct ipa_opt_pass_d pass_ipa_inline =
2187 {
2188  {
2189   IPA_PASS,
2190   "inline",                             /* name */
2191   gate_cgraph_decide_inlining,          /* gate */
2192   cgraph_decide_inlining,               /* execute */
2193   NULL,                                 /* sub */
2194   NULL,                                 /* next */
2195   0,                                    /* static_pass_number */
2196   TV_INLINE_HEURISTICS,                 /* tv_id */
2197   0,                                    /* properties_required */
2198   0,                                    /* properties_provided */
2199   0,                                    /* properties_destroyed */
2200   TODO_remove_functions,                /* todo_flags_finish */
2201   TODO_dump_cgraph | TODO_dump_func
2202   | TODO_remove_functions | TODO_ggc_collect    /* todo_flags_finish */
2203  },
2204  inline_generate_summary,               /* generate_summary */
2205  inline_write_summary,                  /* write_summary */
2206  inline_read_summary,                   /* read_summary */
2207  NULL,                                  /* write_optimization_summary */
2208  NULL,                                  /* read_optimization_summary */
2209  NULL,                                  /* stmt_fixup */
2210  0,                                     /* TODOs */
2211  inline_transform,                      /* function_transform */
2212  NULL,                                  /* variable_transform */
2213 };
2214
2215
2216 #include "gt-ipa-inline.h"