OSDN Git Service

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