OSDN Git Service

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