OSDN Git Service

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