ipa-cp.c (ipcp_print_edge_profiles): Test for node->analyzed rather than for DECL_SAV...
[gcc.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 in non-unit-at-a-time mode.
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 This is the main inlining pass in non-unit-at-a-time.
86
87 With unit-at-a-time the pass is run during conversion into SSA form.
88 Only functions already converted into SSA form are inlined, so the
89 conversion must happen in topological order on the callgraph (that is
90 maintained by pass manager). The functions after inlining are early
91 optimized so the early inliner sees unoptimized function itself, but
92 all considered callees are already optimized allowing it to unfold
93 abstraction penalty on C++ effectively and cheaply.
94
95 pass_ipa_early_inlining
96
97 With profiling, the early inlining is also necessary to reduce
98 instrumentation costs on program with high abstraction penalty (doing
99 many redundant calls). This can't happen in parallel with early
100 optimization and profile instrumentation, because we would end up
101 re-instrumenting already instrumented function bodies we brought in via
102 inlining.
103
104 To avoid this, this pass is executed as IPA pass before profiling. It is
105 simple wrapper to pass_early_inlining and ensures first inlining.
106
107 pass_ipa_inline
108
109 This is the main pass implementing simple greedy algorithm to do inlining
110 of small functions that results in overall growth of compilation unit and
111 inlining of functions called once. The pass compute just so called inline
112 plan (representation of inlining to be done in callgraph) and unlike early
113 inlining it is not performing the inlining itself.
114
115 pass_apply_inline
116
117 This pass performs actual inlining according to pass_ipa_inline on given
118 function. Possible the function body before inlining is saved when it is
119 needed for further inlining later.
120 */
121
122 #include "config.h"
123 #include "system.h"
124 #include "coretypes.h"
125 #include "tm.h"
126 #include "tree.h"
127 #include "tree-inline.h"
128 #include "langhooks.h"
129 #include "flags.h"
130 #include "cgraph.h"
131 #include "diagnostic.h"
132 #include "timevar.h"
133 #include "params.h"
134 #include "fibheap.h"
135 #include "intl.h"
136 #include "tree-pass.h"
137 #include "hashtab.h"
138 #include "coverage.h"
139 #include "ggc.h"
140 #include "tree-flow.h"
141 #include "rtl.h"
142 #include "ipa-prop.h"
143
144 /* Mode incremental inliner operate on:
145
146 In ALWAYS_INLINE only functions marked
147 always_inline are inlined. This mode is used after detecting cycle during
148 flattening.
149
150 In SIZE mode, only functions that reduce function body size after inlining
151 are inlined, this is used during early inlining.
152
153 In SPEED mode, all small functions are inlined. This might result in
154 unbounded growth of compilation unit and is used only in non-unit-at-a-time
155 mode.
156
157 in ALL mode, everything is inlined. This is used during flattening. */
158 enum inlining_mode {
159 INLINE_NONE = 0,
160 INLINE_ALWAYS_INLINE,
161 INLINE_SIZE,
162 INLINE_SPEED,
163 INLINE_ALL
164 };
165 static bool
166 cgraph_decide_inlining_incrementally (struct cgraph_node *, enum inlining_mode,
167 int);
168
169
170 /* Statistics we collect about inlining algorithm. */
171 static int ncalls_inlined;
172 static int nfunctions_inlined;
173 static int overall_insns;
174 static gcov_type max_count;
175
176 static inline struct inline_summary *
177 inline_summary (struct cgraph_node *node)
178 {
179 return &node->local.inline_summary;
180 }
181
182 /* Estimate size of the function after inlining WHAT into TO. */
183
184 static int
185 cgraph_estimate_size_after_inlining (int times, struct cgraph_node *to,
186 struct cgraph_node *what)
187 {
188 int size;
189 tree fndecl = what->decl, arg;
190 int call_insns = PARAM_VALUE (PARAM_INLINE_CALL_COST);
191
192 for (arg = DECL_ARGUMENTS (fndecl); arg; arg = TREE_CHAIN (arg))
193 call_insns += estimate_move_cost (TREE_TYPE (arg));
194 size = (what->global.insns - call_insns) * times + to->global.insns;
195 gcc_assert (size >= 0);
196 return size;
197 }
198
199 /* E is expected to be an edge being inlined. Clone destination node of
200 the edge and redirect it to the new clone.
201 DUPLICATE is used for bookkeeping on whether we are actually creating new
202 clones or re-using node originally representing out-of-line function call.
203 */
204 void
205 cgraph_clone_inlined_nodes (struct cgraph_edge *e, bool duplicate, bool update_original)
206 {
207 HOST_WIDE_INT peak;
208 if (duplicate)
209 {
210 /* We may eliminate the need for out-of-line copy to be output.
211 In that case just go ahead and re-use it. */
212 if (!e->callee->callers->next_caller
213 && !e->callee->needed
214 && !cgraph_new_nodes
215 && flag_unit_at_a_time)
216 {
217 gcc_assert (!e->callee->global.inlined_to);
218 if (DECL_SAVED_TREE (e->callee->decl))
219 overall_insns -= e->callee->global.insns, nfunctions_inlined++;
220 duplicate = false;
221 }
222 else
223 {
224 struct cgraph_node *n;
225 n = cgraph_clone_node (e->callee, e->count, e->frequency, e->loop_nest,
226 update_original);
227 cgraph_redirect_edge_callee (e, n);
228 }
229 }
230
231 if (e->caller->global.inlined_to)
232 e->callee->global.inlined_to = e->caller->global.inlined_to;
233 else
234 e->callee->global.inlined_to = e->caller;
235 e->callee->global.stack_frame_offset
236 = e->caller->global.stack_frame_offset
237 + inline_summary (e->caller)->estimated_self_stack_size;
238 peak = e->callee->global.stack_frame_offset
239 + inline_summary (e->callee)->estimated_self_stack_size;
240 if (e->callee->global.inlined_to->global.estimated_stack_size < peak)
241 e->callee->global.inlined_to->global.estimated_stack_size = peak;
242
243 /* Recursively clone all bodies. */
244 for (e = e->callee->callees; e; e = e->next_callee)
245 if (!e->inline_failed)
246 cgraph_clone_inlined_nodes (e, duplicate, update_original);
247 }
248
249 /* Mark edge E as inlined and update callgraph accordingly.
250 UPDATE_ORIGINAL specify whether profile of original function should be
251 updated. */
252
253 void
254 cgraph_mark_inline_edge (struct cgraph_edge *e, bool update_original)
255 {
256 int old_insns = 0, new_insns = 0;
257 struct cgraph_node *to = NULL, *what;
258
259 if (e->callee->inline_decl)
260 cgraph_redirect_edge_callee (e, cgraph_node (e->callee->inline_decl));
261
262 gcc_assert (e->inline_failed);
263 e->inline_failed = NULL;
264
265 if (!e->callee->global.inlined && flag_unit_at_a_time)
266 DECL_POSSIBLY_INLINED (e->callee->decl) = true;
267 e->callee->global.inlined = true;
268
269 cgraph_clone_inlined_nodes (e, true, update_original);
270
271 what = e->callee;
272
273 /* Now update size of caller and all functions caller is inlined into. */
274 for (;e && !e->inline_failed; e = e->caller->callers)
275 {
276 old_insns = e->caller->global.insns;
277 new_insns = cgraph_estimate_size_after_inlining (1, e->caller,
278 what);
279 gcc_assert (new_insns >= 0);
280 to = e->caller;
281 to->global.insns = new_insns;
282 }
283 gcc_assert (what->global.inlined_to == to);
284 if (new_insns > old_insns)
285 overall_insns += new_insns - old_insns;
286 ncalls_inlined++;
287 }
288
289 /* Mark all calls of EDGE->CALLEE inlined into EDGE->CALLER.
290 Return following unredirected edge in the list of callers
291 of EDGE->CALLEE */
292
293 static struct cgraph_edge *
294 cgraph_mark_inline (struct cgraph_edge *edge)
295 {
296 struct cgraph_node *to = edge->caller;
297 struct cgraph_node *what = edge->callee;
298 struct cgraph_edge *e, *next;
299
300 gcc_assert (!CALL_STMT_CANNOT_INLINE_P (edge->call_stmt));
301 /* Look for all calls, mark them inline and clone recursively
302 all inlined functions. */
303 for (e = what->callers; e; e = next)
304 {
305 next = e->next_caller;
306 if (e->caller == to && e->inline_failed)
307 {
308 cgraph_mark_inline_edge (e, true);
309 if (e == edge)
310 edge = next;
311 }
312 }
313
314 return edge;
315 }
316
317 /* Estimate the growth caused by inlining NODE into all callees. */
318
319 static int
320 cgraph_estimate_growth (struct cgraph_node *node)
321 {
322 int growth = 0;
323 struct cgraph_edge *e;
324 if (node->global.estimated_growth != INT_MIN)
325 return node->global.estimated_growth;
326
327 for (e = node->callers; e; e = e->next_caller)
328 if (e->inline_failed)
329 growth += (cgraph_estimate_size_after_inlining (1, e->caller, node)
330 - e->caller->global.insns);
331
332 /* ??? Wrong for self recursive functions or cases where we decide to not
333 inline for different reasons, but it is not big deal as in that case
334 we will keep the body around, but we will also avoid some inlining. */
335 if (!node->needed && !DECL_EXTERNAL (node->decl))
336 growth -= node->global.insns;
337
338 node->global.estimated_growth = growth;
339 return growth;
340 }
341
342 /* Return false when inlining WHAT into TO is not good idea
343 as it would cause too large growth of function bodies.
344 When ONE_ONLY is true, assume that only one call site is going
345 to be inlined, otherwise figure out how many call sites in
346 TO calls WHAT and verify that all can be inlined.
347 */
348
349 static bool
350 cgraph_check_inline_limits (struct cgraph_node *to, struct cgraph_node *what,
351 const char **reason, bool one_only)
352 {
353 int times = 0;
354 struct cgraph_edge *e;
355 int newsize;
356 int limit;
357 HOST_WIDE_INT stack_size_limit, inlined_stack;
358
359 if (one_only)
360 times = 1;
361 else
362 for (e = to->callees; e; e = e->next_callee)
363 if (e->callee == what)
364 times++;
365
366 if (to->global.inlined_to)
367 to = to->global.inlined_to;
368
369 /* When inlining large function body called once into small function,
370 take the inlined function as base for limiting the growth. */
371 if (inline_summary (to)->self_insns > inline_summary(what)->self_insns)
372 limit = inline_summary (to)->self_insns;
373 else
374 limit = inline_summary (what)->self_insns;
375
376 limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
377
378 /* Check the size after inlining against the function limits. But allow
379 the function to shrink if it went over the limits by forced inlining. */
380 newsize = cgraph_estimate_size_after_inlining (times, to, what);
381 if (newsize >= to->global.insns
382 && newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
383 && newsize > limit)
384 {
385 if (reason)
386 *reason = N_("--param large-function-growth limit reached");
387 return false;
388 }
389
390 stack_size_limit = inline_summary (to)->estimated_self_stack_size;
391
392 stack_size_limit += stack_size_limit * PARAM_VALUE (PARAM_STACK_FRAME_GROWTH) / 100;
393
394 inlined_stack = (to->global.stack_frame_offset
395 + inline_summary (to)->estimated_self_stack_size
396 + what->global.estimated_stack_size);
397 if (inlined_stack > stack_size_limit
398 && inlined_stack > PARAM_VALUE (PARAM_LARGE_STACK_FRAME))
399 {
400 if (reason)
401 *reason = N_("--param large-stack-frame-growth limit reached");
402 return false;
403 }
404 return true;
405 }
406
407 /* Return true when function N is small enough to be inlined. */
408
409 bool
410 cgraph_default_inline_p (struct cgraph_node *n, const char **reason)
411 {
412 tree decl = n->decl;
413
414 if (n->inline_decl)
415 decl = n->inline_decl;
416 if (!flag_inline_small_functions && !DECL_DECLARED_INLINE_P (decl))
417 {
418 if (reason)
419 *reason = N_("function not inline candidate");
420 return false;
421 }
422
423 if (!DECL_STRUCT_FUNCTION (decl)->cfg)
424 {
425 if (reason)
426 *reason = N_("function body not available");
427 return false;
428 }
429
430 if (DECL_DECLARED_INLINE_P (decl))
431 {
432 if (n->global.insns >= MAX_INLINE_INSNS_SINGLE)
433 {
434 if (reason)
435 *reason = N_("--param max-inline-insns-single limit reached");
436 return false;
437 }
438 }
439 else
440 {
441 if (n->global.insns >= MAX_INLINE_INSNS_AUTO)
442 {
443 if (reason)
444 *reason = N_("--param max-inline-insns-auto limit reached");
445 return false;
446 }
447 }
448
449 return true;
450 }
451
452 /* Return true when inlining WHAT would create recursive inlining.
453 We call recursive inlining all cases where same function appears more than
454 once in the single recursion nest path in the inline graph. */
455
456 static bool
457 cgraph_recursive_inlining_p (struct cgraph_node *to,
458 struct cgraph_node *what,
459 const char **reason)
460 {
461 bool recursive;
462 if (to->global.inlined_to)
463 recursive = what->decl == to->global.inlined_to->decl;
464 else
465 recursive = what->decl == to->decl;
466 /* Marking recursive function inline has sane semantic and thus we should
467 not warn on it. */
468 if (recursive && reason)
469 *reason = (what->local.disregard_inline_limits
470 ? N_("recursive inlining") : "");
471 return recursive;
472 }
473
474 /* Return true if the call can be hot. */
475 static bool
476 cgraph_maybe_hot_edge_p (struct cgraph_edge *edge)
477 {
478 if (profile_info && flag_branch_probabilities
479 && (edge->count
480 <= profile_info->sum_max / PARAM_VALUE (HOT_BB_COUNT_FRACTION)))
481 return false;
482 if (lookup_attribute ("cold", DECL_ATTRIBUTES (edge->callee->decl))
483 || lookup_attribute ("cold", DECL_ATTRIBUTES (edge->caller->decl)))
484 return false;
485 if (lookup_attribute ("hot", DECL_ATTRIBUTES (edge->caller->decl)))
486 return true;
487 if (flag_guess_branch_prob
488 && edge->frequency < (CGRAPH_FREQ_MAX
489 / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION)))
490 return false;
491 return true;
492 }
493
494 /* A cost model driving the inlining heuristics in a way so the edges with
495 smallest badness are inlined first. After each inlining is performed
496 the costs of all caller edges of nodes affected are recomputed so the
497 metrics may accurately depend on values such as number of inlinable callers
498 of the function or function body size. */
499
500 static int
501 cgraph_edge_badness (struct cgraph_edge *edge)
502 {
503 int badness;
504 int growth =
505 cgraph_estimate_size_after_inlining (1, edge->caller, edge->callee);
506
507 growth -= edge->caller->global.insns;
508
509 /* Always prefer inlining saving code size. */
510 if (growth <= 0)
511 badness = INT_MIN - growth;
512
513 /* When profiling is available, base priorities -(#calls / growth).
514 So we optimize for overall number of "executed" inlined calls. */
515 else if (max_count)
516 badness = ((int)((double)edge->count * INT_MIN / max_count)) / growth;
517
518 /* When function local profile is available, base priorities on
519 growth / frequency, so we optimize for overall frequency of inlined
520 calls. This is not too accurate since while the call might be frequent
521 within function, the function itself is infrequent.
522
523 Other objective to optimize for is number of different calls inlined.
524 We add the estimated growth after inlining all functions to bias the
525 priorities slightly in this direction (so fewer times called functions
526 of the same size gets priority). */
527 else if (flag_guess_branch_prob)
528 {
529 int div = edge->frequency * 100 / CGRAPH_FREQ_BASE;
530 int growth =
531 cgraph_estimate_size_after_inlining (1, edge->caller, edge->callee);
532 growth -= edge->caller->global.insns;
533 badness = growth * 256;
534
535 /* Decrease badness if call is nested. */
536 /* Compress the range so we don't overflow. */
537 if (div > 256)
538 div = 256 + ceil_log2 (div) - 8;
539 if (div < 1)
540 div = 1;
541 if (badness > 0)
542 badness /= div;
543 badness += cgraph_estimate_growth (edge->callee);
544 }
545 /* When function local profile is not available or it does not give
546 useful information (ie frequency is zero), base the cost on
547 loop nest and overall size growth, so we optimize for overall number
548 of functions fully inlined in program. */
549 else
550 {
551 int nest = MIN (edge->loop_nest, 8);
552 badness = cgraph_estimate_growth (edge->callee) * 256;
553
554 /* Decrease badness if call is nested. */
555 if (badness > 0)
556 badness >>= nest;
557 else
558 {
559 badness <<= nest;
560 }
561 }
562 /* Make recursive inlining happen always after other inlining is done. */
563 if (cgraph_recursive_inlining_p (edge->caller, edge->callee, NULL))
564 return badness + 1;
565 else
566 return badness;
567 }
568
569 /* Recompute heap nodes for each of caller edge. */
570
571 static void
572 update_caller_keys (fibheap_t heap, struct cgraph_node *node,
573 bitmap updated_nodes)
574 {
575 struct cgraph_edge *edge;
576 const char *failed_reason;
577
578 if (!node->local.inlinable || node->local.disregard_inline_limits
579 || node->global.inlined_to)
580 return;
581 if (bitmap_bit_p (updated_nodes, node->uid))
582 return;
583 bitmap_set_bit (updated_nodes, node->uid);
584 node->global.estimated_growth = INT_MIN;
585
586 if (!node->local.inlinable)
587 return;
588 /* Prune out edges we won't inline into anymore. */
589 if (!cgraph_default_inline_p (node, &failed_reason))
590 {
591 for (edge = node->callers; edge; edge = edge->next_caller)
592 if (edge->aux)
593 {
594 fibheap_delete_node (heap, (fibnode_t) edge->aux);
595 edge->aux = NULL;
596 if (edge->inline_failed)
597 edge->inline_failed = failed_reason;
598 }
599 return;
600 }
601
602 for (edge = node->callers; edge; edge = edge->next_caller)
603 if (edge->inline_failed)
604 {
605 int badness = cgraph_edge_badness (edge);
606 if (edge->aux)
607 {
608 fibnode_t n = (fibnode_t) edge->aux;
609 gcc_assert (n->data == edge);
610 if (n->key == badness)
611 continue;
612
613 /* fibheap_replace_key only increase the keys. */
614 if (fibheap_replace_key (heap, n, badness))
615 continue;
616 fibheap_delete_node (heap, (fibnode_t) edge->aux);
617 }
618 edge->aux = fibheap_insert (heap, badness, edge);
619 }
620 }
621
622 /* Recompute heap nodes for each of caller edges of each of callees. */
623
624 static void
625 update_callee_keys (fibheap_t heap, struct cgraph_node *node,
626 bitmap updated_nodes)
627 {
628 struct cgraph_edge *e;
629 node->global.estimated_growth = INT_MIN;
630
631 for (e = node->callees; e; e = e->next_callee)
632 if (e->inline_failed)
633 update_caller_keys (heap, e->callee, updated_nodes);
634 else if (!e->inline_failed)
635 update_callee_keys (heap, e->callee, updated_nodes);
636 }
637
638 /* Enqueue all recursive calls from NODE into priority queue depending on
639 how likely we want to recursively inline the call. */
640
641 static void
642 lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
643 fibheap_t heap)
644 {
645 static int priority;
646 struct cgraph_edge *e;
647 for (e = where->callees; e; e = e->next_callee)
648 if (e->callee == node)
649 {
650 /* When profile feedback is available, prioritize by expected number
651 of calls. Without profile feedback we maintain simple queue
652 to order candidates via recursive depths. */
653 fibheap_insert (heap,
654 !max_count ? priority++
655 : -(e->count / ((max_count + (1<<24) - 1) / (1<<24))),
656 e);
657 }
658 for (e = where->callees; e; e = e->next_callee)
659 if (!e->inline_failed)
660 lookup_recursive_calls (node, e->callee, heap);
661 }
662
663 /* Decide on recursive inlining: in the case function has recursive calls,
664 inline until body size reaches given argument. If any new indirect edges
665 are discovered in the process, add them to NEW_EDGES, unless it is NULL. */
666
667 static bool
668 cgraph_decide_recursive_inlining (struct cgraph_node *node,
669 VEC (cgraph_edge_p, heap) *new_edges)
670 {
671 int limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO);
672 int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO);
673 int probability = PARAM_VALUE (PARAM_MIN_INLINE_RECURSIVE_PROBABILITY);
674 fibheap_t heap;
675 struct cgraph_edge *e;
676 struct cgraph_node *master_clone, *next;
677 int depth = 0;
678 int n = 0;
679
680 if (optimize_size
681 || (!flag_inline_functions && !DECL_DECLARED_INLINE_P (node->decl)))
682 return false;
683
684 if (DECL_DECLARED_INLINE_P (node->decl))
685 {
686 limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE);
687 max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH);
688 }
689
690 /* Make sure that function is small enough to be considered for inlining. */
691 if (!max_depth
692 || cgraph_estimate_size_after_inlining (1, node, node) >= limit)
693 return false;
694 heap = fibheap_new ();
695 lookup_recursive_calls (node, node, heap);
696 if (fibheap_empty (heap))
697 {
698 fibheap_delete (heap);
699 return false;
700 }
701
702 if (dump_file)
703 fprintf (dump_file,
704 " Performing recursive inlining on %s\n",
705 cgraph_node_name (node));
706
707 /* We need original clone to copy around. */
708 master_clone = cgraph_clone_node (node, node->count, CGRAPH_FREQ_BASE, 1, false);
709 master_clone->needed = true;
710 for (e = master_clone->callees; e; e = e->next_callee)
711 if (!e->inline_failed)
712 cgraph_clone_inlined_nodes (e, true, false);
713
714 /* Do the inlining and update list of recursive call during process. */
715 while (!fibheap_empty (heap)
716 && (cgraph_estimate_size_after_inlining (1, node, master_clone)
717 <= limit))
718 {
719 struct cgraph_edge *curr
720 = (struct cgraph_edge *) fibheap_extract_min (heap);
721 struct cgraph_node *cnode;
722
723 depth = 1;
724 for (cnode = curr->caller;
725 cnode->global.inlined_to; cnode = cnode->callers->caller)
726 if (node->decl == curr->callee->decl)
727 depth++;
728 if (depth > max_depth)
729 {
730 if (dump_file)
731 fprintf (dump_file,
732 " maximal depth reached\n");
733 continue;
734 }
735
736 if (max_count)
737 {
738 if (!cgraph_maybe_hot_edge_p (curr))
739 {
740 if (dump_file)
741 fprintf (dump_file, " Not inlining cold call\n");
742 continue;
743 }
744 if (curr->count * 100 / node->count < probability)
745 {
746 if (dump_file)
747 fprintf (dump_file,
748 " Probability of edge is too small\n");
749 continue;
750 }
751 }
752
753 if (dump_file)
754 {
755 fprintf (dump_file,
756 " Inlining call of depth %i", depth);
757 if (node->count)
758 {
759 fprintf (dump_file, " called approx. %.2f times per call",
760 (double)curr->count / node->count);
761 }
762 fprintf (dump_file, "\n");
763 }
764 cgraph_redirect_edge_callee (curr, master_clone);
765 cgraph_mark_inline_edge (curr, false);
766 if (flag_indirect_inlining)
767 ipa_propagate_indirect_call_infos (curr, new_edges);
768 lookup_recursive_calls (node, curr->callee, heap);
769 n++;
770 }
771 if (!fibheap_empty (heap) && dump_file)
772 fprintf (dump_file, " Recursive inlining growth limit met.\n");
773
774 fibheap_delete (heap);
775 if (dump_file)
776 fprintf (dump_file,
777 "\n Inlined %i times, body grown from %i to %i insns\n", n,
778 master_clone->global.insns, node->global.insns);
779
780 /* Remove master clone we used for inlining. We rely that clones inlined
781 into master clone gets queued just before master clone so we don't
782 need recursion. */
783 for (node = cgraph_nodes; node != master_clone;
784 node = next)
785 {
786 next = node->next;
787 if (node->global.inlined_to == master_clone)
788 cgraph_remove_node (node);
789 }
790 cgraph_remove_node (master_clone);
791 /* FIXME: Recursive inlining actually reduces number of calls of the
792 function. At this place we should probably walk the function and
793 inline clones and compensate the counts accordingly. This probably
794 doesn't matter much in practice. */
795 return n > 0;
796 }
797
798 /* Set inline_failed for all callers of given function to REASON. */
799
800 static void
801 cgraph_set_inline_failed (struct cgraph_node *node, const char *reason)
802 {
803 struct cgraph_edge *e;
804
805 if (dump_file)
806 fprintf (dump_file, "Inlining failed: %s\n", reason);
807 for (e = node->callers; e; e = e->next_caller)
808 if (e->inline_failed)
809 e->inline_failed = reason;
810 }
811
812 /* Given whole compilation unit estimate of INSNS, compute how large we can
813 allow the unit to grow. */
814 static int
815 compute_max_insns (int insns)
816 {
817 int max_insns = insns;
818 if (max_insns < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
819 max_insns = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
820
821 return ((HOST_WIDEST_INT) max_insns
822 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
823 }
824
825 /* Compute badness of all edges in NEW_EDGES and add them to the HEAP. */
826 static void
827 add_new_edges_to_heap (fibheap_t heap, VEC (cgraph_edge_p, heap) *new_edges)
828 {
829 while (VEC_length (cgraph_edge_p, new_edges) > 0)
830 {
831 struct cgraph_edge *edge = VEC_pop (cgraph_edge_p, new_edges);
832
833 gcc_assert (!edge->aux);
834 edge->aux = fibheap_insert (heap, cgraph_edge_badness (edge), edge);
835 }
836 }
837
838
839 /* We use greedy algorithm for inlining of small functions:
840 All inline candidates are put into prioritized heap based on estimated
841 growth of the overall number of instructions and then update the estimates.
842
843 INLINED and INLINED_CALEES are just pointers to arrays large enough
844 to be passed to cgraph_inlined_into and cgraph_inlined_callees. */
845
846 static void
847 cgraph_decide_inlining_of_small_functions (void)
848 {
849 struct cgraph_node *node;
850 struct cgraph_edge *edge;
851 const char *failed_reason;
852 fibheap_t heap = fibheap_new ();
853 bitmap updated_nodes = BITMAP_ALLOC (NULL);
854 int min_insns, max_insns;
855 VEC (cgraph_edge_p, heap) *new_indirect_edges = NULL;
856
857 if (flag_indirect_inlining)
858 new_indirect_edges = VEC_alloc (cgraph_edge_p, heap, 8);
859
860 if (dump_file)
861 fprintf (dump_file, "\nDeciding on smaller functions:\n");
862
863 /* Put all inline candidates into the heap. */
864
865 for (node = cgraph_nodes; node; node = node->next)
866 {
867 if (!node->local.inlinable || !node->callers
868 || node->local.disregard_inline_limits)
869 continue;
870 if (dump_file)
871 fprintf (dump_file, "Considering inline candidate %s.\n", cgraph_node_name (node));
872
873 node->global.estimated_growth = INT_MIN;
874 if (!cgraph_default_inline_p (node, &failed_reason))
875 {
876 cgraph_set_inline_failed (node, failed_reason);
877 continue;
878 }
879
880 for (edge = node->callers; edge; edge = edge->next_caller)
881 if (edge->inline_failed)
882 {
883 gcc_assert (!edge->aux);
884 edge->aux = fibheap_insert (heap, cgraph_edge_badness (edge), edge);
885 }
886 }
887
888 max_insns = compute_max_insns (overall_insns);
889 min_insns = overall_insns;
890
891 while (overall_insns <= max_insns
892 && (edge = (struct cgraph_edge *) fibheap_extract_min (heap)))
893 {
894 int old_insns = overall_insns;
895 struct cgraph_node *where;
896 int growth =
897 cgraph_estimate_size_after_inlining (1, edge->caller, edge->callee);
898 const char *not_good = NULL;
899
900 growth -= edge->caller->global.insns;
901
902 if (dump_file)
903 {
904 fprintf (dump_file,
905 "\nConsidering %s with %i insns\n",
906 cgraph_node_name (edge->callee),
907 edge->callee->global.insns);
908 fprintf (dump_file,
909 " to be inlined into %s\n"
910 " Estimated growth after inlined into all callees is %+i insns.\n"
911 " Estimated badness is %i, frequency %.2f.\n",
912 cgraph_node_name (edge->caller),
913 cgraph_estimate_growth (edge->callee),
914 cgraph_edge_badness (edge),
915 edge->frequency / (double)CGRAPH_FREQ_BASE);
916 if (edge->count)
917 fprintf (dump_file," Called "HOST_WIDEST_INT_PRINT_DEC"x\n", edge->count);
918 }
919 gcc_assert (edge->aux);
920 edge->aux = NULL;
921 if (!edge->inline_failed)
922 continue;
923
924 /* When not having profile info ready we don't weight by any way the
925 position of call in procedure itself. This means if call of
926 function A from function B seems profitable to inline, the recursive
927 call of function A in inline copy of A in B will look profitable too
928 and we end up inlining until reaching maximal function growth. This
929 is not good idea so prohibit the recursive inlining.
930
931 ??? When the frequencies are taken into account we might not need this
932 restriction. */
933 if (!max_count)
934 {
935 where = edge->caller;
936 while (where->global.inlined_to)
937 {
938 if (where->decl == edge->callee->decl)
939 break;
940 where = where->callers->caller;
941 }
942 if (where->global.inlined_to)
943 {
944 edge->inline_failed
945 = (edge->callee->local.disregard_inline_limits ? N_("recursive inlining") : "");
946 if (dump_file)
947 fprintf (dump_file, " inline_failed:Recursive inlining performed only for function itself.\n");
948 continue;
949 }
950 }
951
952 if (!cgraph_maybe_hot_edge_p (edge))
953 not_good = N_("call is unlikely and code size would grow");
954 if (!flag_inline_functions
955 && !DECL_DECLARED_INLINE_P (edge->callee->decl))
956 not_good = N_("function not declared inline and code size would grow");
957 if (optimize_size)
958 not_good = N_("optimizing for size and code size would grow");
959 if (not_good && growth > 0 && cgraph_estimate_growth (edge->callee) > 0)
960 {
961 if (!cgraph_recursive_inlining_p (edge->caller, edge->callee,
962 &edge->inline_failed))
963 {
964 edge->inline_failed = not_good;
965 if (dump_file)
966 fprintf (dump_file, " inline_failed:%s.\n", edge->inline_failed);
967 }
968 continue;
969 }
970 if (!cgraph_default_inline_p (edge->callee, &edge->inline_failed))
971 {
972 if (!cgraph_recursive_inlining_p (edge->caller, edge->callee,
973 &edge->inline_failed))
974 {
975 if (dump_file)
976 fprintf (dump_file, " inline_failed:%s.\n", edge->inline_failed);
977 }
978 continue;
979 }
980 if (!tree_can_inline_p (edge->caller->decl, edge->callee->decl))
981 {
982 CALL_STMT_CANNOT_INLINE_P (edge->call_stmt) = true;
983 edge->inline_failed = N_("target specific option mismatch");
984 if (dump_file)
985 fprintf (dump_file, " inline_failed:%s.\n", edge->inline_failed);
986 continue;
987 }
988 if (cgraph_recursive_inlining_p (edge->caller, edge->callee,
989 &edge->inline_failed))
990 {
991 where = edge->caller;
992 if (where->global.inlined_to)
993 where = where->global.inlined_to;
994 if (!cgraph_decide_recursive_inlining (where, new_indirect_edges))
995 continue;
996 if (flag_indirect_inlining)
997 add_new_edges_to_heap (heap, new_indirect_edges);
998 update_callee_keys (heap, where, updated_nodes);
999 }
1000 else
1001 {
1002 struct cgraph_node *callee;
1003 if (CALL_STMT_CANNOT_INLINE_P (edge->call_stmt)
1004 || !cgraph_check_inline_limits (edge->caller, edge->callee,
1005 &edge->inline_failed, true))
1006 {
1007 if (dump_file)
1008 fprintf (dump_file, " Not inlining into %s:%s.\n",
1009 cgraph_node_name (edge->caller), edge->inline_failed);
1010 continue;
1011 }
1012 callee = edge->callee;
1013 cgraph_mark_inline_edge (edge, true);
1014 if (flag_indirect_inlining)
1015 {
1016 ipa_propagate_indirect_call_infos (edge, new_indirect_edges);
1017 add_new_edges_to_heap (heap, new_indirect_edges);
1018 }
1019 update_callee_keys (heap, callee, updated_nodes);
1020 }
1021 where = edge->caller;
1022 if (where->global.inlined_to)
1023 where = where->global.inlined_to;
1024
1025 /* Our profitability metric can depend on local properties
1026 such as number of inlinable calls and size of the function body.
1027 After inlining these properties might change for the function we
1028 inlined into (since it's body size changed) and for the functions
1029 called by function we inlined (since number of it inlinable callers
1030 might change). */
1031 update_caller_keys (heap, where, updated_nodes);
1032 bitmap_clear (updated_nodes);
1033
1034 if (dump_file)
1035 {
1036 fprintf (dump_file,
1037 " Inlined into %s which now has %i insns,"
1038 "net change of %+i insns.\n",
1039 cgraph_node_name (edge->caller),
1040 edge->caller->global.insns,
1041 overall_insns - old_insns);
1042 }
1043 if (min_insns > overall_insns)
1044 {
1045 min_insns = overall_insns;
1046 max_insns = compute_max_insns (min_insns);
1047
1048 if (dump_file)
1049 fprintf (dump_file, "New minimal insns reached: %i\n", min_insns);
1050 }
1051 }
1052 while ((edge = (struct cgraph_edge *) fibheap_extract_min (heap)) != NULL)
1053 {
1054 gcc_assert (edge->aux);
1055 edge->aux = NULL;
1056 if (!edge->callee->local.disregard_inline_limits && edge->inline_failed
1057 && !cgraph_recursive_inlining_p (edge->caller, edge->callee,
1058 &edge->inline_failed))
1059 edge->inline_failed = N_("--param inline-unit-growth limit reached");
1060 }
1061
1062 if (new_indirect_edges)
1063 VEC_free (cgraph_edge_p, heap, new_indirect_edges);
1064 fibheap_delete (heap);
1065 BITMAP_FREE (updated_nodes);
1066 }
1067
1068 /* Decide on the inlining. We do so in the topological order to avoid
1069 expenses on updating data structures. */
1070
1071 static unsigned int
1072 cgraph_decide_inlining (void)
1073 {
1074 struct cgraph_node *node;
1075 int nnodes;
1076 struct cgraph_node **order =
1077 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1078 int old_insns = 0;
1079 int i;
1080 int initial_insns = 0;
1081
1082 max_count = 0;
1083 for (node = cgraph_nodes; node; node = node->next)
1084 if (node->analyzed && (node->needed || node->reachable))
1085 {
1086 struct cgraph_edge *e;
1087
1088 initial_insns += inline_summary (node)->self_insns;
1089 gcc_assert (inline_summary (node)->self_insns == node->global.insns);
1090 for (e = node->callees; e; e = e->next_callee)
1091 if (max_count < e->count)
1092 max_count = e->count;
1093 }
1094 overall_insns = initial_insns;
1095 gcc_assert (!max_count || (profile_info && flag_branch_probabilities));
1096
1097 nnodes = cgraph_postorder (order);
1098
1099 if (dump_file)
1100 fprintf (dump_file,
1101 "\nDeciding on inlining. Starting with %i insns.\n",
1102 initial_insns);
1103
1104 for (node = cgraph_nodes; node; node = node->next)
1105 node->aux = 0;
1106
1107 if (dump_file)
1108 fprintf (dump_file, "\nInlining always_inline functions:\n");
1109
1110 /* In the first pass mark all always_inline edges. Do this with a priority
1111 so none of our later choices will make this impossible. */
1112 for (i = nnodes - 1; i >= 0; i--)
1113 {
1114 struct cgraph_edge *e, *next;
1115
1116 node = order[i];
1117
1118 /* Handle nodes to be flattened, but don't update overall unit size. */
1119 if (lookup_attribute ("flatten", DECL_ATTRIBUTES (node->decl)) != NULL)
1120 {
1121 if (dump_file)
1122 fprintf (dump_file,
1123 "Flattening %s\n", cgraph_node_name (node));
1124 cgraph_decide_inlining_incrementally (node, INLINE_ALL, 0);
1125 }
1126
1127 if (!node->local.disregard_inline_limits)
1128 continue;
1129 if (dump_file)
1130 fprintf (dump_file,
1131 "\nConsidering %s %i insns (always inline)\n",
1132 cgraph_node_name (node), node->global.insns);
1133 old_insns = overall_insns;
1134 for (e = node->callers; e; e = next)
1135 {
1136 next = e->next_caller;
1137 if (!e->inline_failed || CALL_STMT_CANNOT_INLINE_P (e->call_stmt))
1138 continue;
1139 if (cgraph_recursive_inlining_p (e->caller, e->callee,
1140 &e->inline_failed))
1141 continue;
1142 if (!tree_can_inline_p (e->caller->decl, e->callee->decl))
1143 {
1144 CALL_STMT_CANNOT_INLINE_P (e->call_stmt) = true;
1145 continue;
1146 }
1147 cgraph_mark_inline_edge (e, true);
1148 if (flag_indirect_inlining)
1149 ipa_propagate_indirect_call_infos (e, NULL);
1150 if (dump_file)
1151 fprintf (dump_file,
1152 " Inlined into %s which now has %i insns.\n",
1153 cgraph_node_name (e->caller),
1154 e->caller->global.insns);
1155 }
1156 /* Inlining self recursive function might introduce new calls to
1157 themselves we didn't see in the loop above. Fill in the proper
1158 reason why inline failed. */
1159 for (e = node->callers; e; e = e->next_caller)
1160 if (e->inline_failed)
1161 e->inline_failed = N_("recursive inlining");
1162 if (dump_file)
1163 fprintf (dump_file,
1164 " Inlined for a net change of %+i insns.\n",
1165 overall_insns - old_insns);
1166 }
1167
1168 if (!flag_really_no_inline)
1169 cgraph_decide_inlining_of_small_functions ();
1170
1171 /* After this point, any edge discovery performed by indirect inlining is no
1172 good so let's give up. */
1173 if (flag_indirect_inlining)
1174 free_all_ipa_structures_after_iinln ();
1175
1176 if (!flag_really_no_inline
1177 && flag_inline_functions_called_once)
1178 {
1179 if (dump_file)
1180 fprintf (dump_file, "\nDeciding on functions called once:\n");
1181
1182 /* And finally decide what functions are called once. */
1183
1184 for (i = nnodes - 1; i >= 0; i--)
1185 {
1186 node = order[i];
1187
1188 if (node->callers && !node->callers->next_caller && !node->needed
1189 && node->local.inlinable && node->callers->inline_failed
1190 && !CALL_STMT_CANNOT_INLINE_P (node->callers->call_stmt)
1191 && !DECL_EXTERNAL (node->decl) && !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 at -O0, 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 (!flag_unit_at_a_time || 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 (CALL_STMT_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 CALL_STMT_CANNOT_INLINE_P (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 (!DECL_SAVED_TREE (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 (!flag_really_no_inline && mode != INLINE_ALL
1414 && mode != INLINE_ALWAYS_INLINE)
1415 for (e = node->callees; e; e = e->next_callee)
1416 {
1417 if (!e->callee->local.inlinable
1418 || !e->inline_failed
1419 || e->callee->local.disregard_inline_limits)
1420 continue;
1421 if (dump_file)
1422 fprintf (dump_file, "Considering inline candidate %s.\n",
1423 cgraph_node_name (e->callee));
1424 if (cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed))
1425 {
1426 if (dump_file)
1427 {
1428 indent_to (dump_file, depth);
1429 fprintf (dump_file, "Not inlining: recursive call.\n");
1430 }
1431 continue;
1432 }
1433 if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
1434 != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->callee->decl)))
1435 {
1436 if (dump_file)
1437 {
1438 indent_to (dump_file, depth);
1439 fprintf (dump_file, "Not inlining: SSA form does not match.\n");
1440 }
1441 continue;
1442 }
1443 /* When the function body would grow and inlining the function won't
1444 eliminate the need for offline copy of the function, don't inline.
1445 */
1446 if ((mode == INLINE_SIZE
1447 || (!flag_inline_functions
1448 && !DECL_DECLARED_INLINE_P (e->callee->decl)))
1449 && (cgraph_estimate_size_after_inlining (1, e->caller, e->callee)
1450 > e->caller->global.insns)
1451 && cgraph_estimate_growth (e->callee) > 0)
1452 {
1453 if (dump_file)
1454 {
1455 indent_to (dump_file, depth);
1456 fprintf (dump_file,
1457 "Not inlining: code size would grow by %i insns.\n",
1458 cgraph_estimate_size_after_inlining (1, e->caller,
1459 e->callee)
1460 - e->caller->global.insns);
1461 }
1462 continue;
1463 }
1464 if (!cgraph_check_inline_limits (node, e->callee, &e->inline_failed,
1465 false)
1466 || CALL_STMT_CANNOT_INLINE_P (e->call_stmt))
1467 {
1468 if (dump_file)
1469 {
1470 indent_to (dump_file, depth);
1471 fprintf (dump_file, "Not inlining: %s.\n", e->inline_failed);
1472 }
1473 continue;
1474 }
1475 if (!DECL_SAVED_TREE (e->callee->decl) && !e->callee->inline_decl)
1476 {
1477 if (dump_file)
1478 {
1479 indent_to (dump_file, depth);
1480 fprintf (dump_file,
1481 "Not inlining: Function body no longer available.\n");
1482 }
1483 continue;
1484 }
1485 if (!tree_can_inline_p (node->decl, e->callee->decl))
1486 {
1487 CALL_STMT_CANNOT_INLINE_P (e->call_stmt) = true;
1488 if (dump_file)
1489 {
1490 indent_to (dump_file, depth);
1491 fprintf (dump_file,
1492 "Not inlining: Target specific option mismatch.\n");
1493 }
1494 continue;
1495 }
1496 if (cgraph_default_inline_p (e->callee, &failed_reason))
1497 inlined |= try_inline (e, mode, depth);
1498 else if (!flag_unit_at_a_time)
1499 e->inline_failed = failed_reason;
1500 }
1501 node->aux = (void *)(size_t) old_mode;
1502 return inlined;
1503 }
1504
1505 /* When inlining shall be performed. */
1506 static bool
1507 cgraph_gate_inlining (void)
1508 {
1509 return flag_inline_trees;
1510 }
1511
1512 /* Because inlining might remove no-longer reachable nodes, we need to
1513 keep the array visible to garbage collector to avoid reading collected
1514 out nodes. */
1515 static int nnodes;
1516 static GTY ((length ("nnodes"))) struct cgraph_node **order;
1517
1518 /* Do inlining of small functions. Doing so early helps profiling and other
1519 passes to be somewhat more effective and avoids some code duplication in
1520 later real inlining pass for testcases with very many function calls. */
1521 static unsigned int
1522 cgraph_early_inlining (void)
1523 {
1524 struct cgraph_node *node = cgraph_node (current_function_decl);
1525 unsigned int todo = 0;
1526
1527 if (sorrycount || errorcount)
1528 return 0;
1529 if (cgraph_decide_inlining_incrementally (node,
1530 flag_unit_at_a_time || optimize_size
1531 ? INLINE_SIZE : INLINE_SPEED, 0))
1532 {
1533 timevar_push (TV_INTEGRATION);
1534 todo = optimize_inline_calls (current_function_decl);
1535 timevar_pop (TV_INTEGRATION);
1536 }
1537 return todo;
1538 }
1539
1540 /* When inlining shall be performed. */
1541 static bool
1542 cgraph_gate_early_inlining (void)
1543 {
1544 return flag_inline_trees && flag_early_inlining;
1545 }
1546
1547 struct gimple_opt_pass pass_early_inline =
1548 {
1549 {
1550 GIMPLE_PASS,
1551 "einline", /* name */
1552 cgraph_gate_early_inlining, /* gate */
1553 cgraph_early_inlining, /* execute */
1554 NULL, /* sub */
1555 NULL, /* next */
1556 0, /* static_pass_number */
1557 TV_INLINE_HEURISTICS, /* tv_id */
1558 0, /* properties_required */
1559 PROP_cfg, /* properties_provided */
1560 0, /* properties_destroyed */
1561 0, /* todo_flags_start */
1562 TODO_dump_func /* todo_flags_finish */
1563 }
1564 };
1565
1566 /* When inlining shall be performed. */
1567 static bool
1568 cgraph_gate_ipa_early_inlining (void)
1569 {
1570 return (flag_inline_trees && flag_early_inlining
1571 && (flag_branch_probabilities || flag_test_coverage
1572 || profile_arc_flag));
1573 }
1574
1575 /* IPA pass wrapper for early inlining pass. We need to run early inlining
1576 before tree profiling so we have stand alone IPA pass for doing so. */
1577 struct simple_ipa_opt_pass pass_ipa_early_inline =
1578 {
1579 {
1580 SIMPLE_IPA_PASS,
1581 "einline_ipa", /* name */
1582 cgraph_gate_ipa_early_inlining, /* gate */
1583 NULL, /* execute */
1584 NULL, /* sub */
1585 NULL, /* next */
1586 0, /* static_pass_number */
1587 TV_INLINE_HEURISTICS, /* tv_id */
1588 0, /* properties_required */
1589 PROP_cfg, /* properties_provided */
1590 0, /* properties_destroyed */
1591 0, /* todo_flags_start */
1592 TODO_dump_cgraph /* todo_flags_finish */
1593 }
1594 };
1595
1596 /* Compute parameters of functions used by inliner. */
1597 unsigned int
1598 compute_inline_parameters (struct cgraph_node *node)
1599 {
1600 gcc_assert (!node->global.inlined_to);
1601 inline_summary (node)->estimated_self_stack_size
1602 = estimated_stack_frame_size ();
1603 node->global.estimated_stack_size
1604 = inline_summary (node)->estimated_self_stack_size;
1605 node->global.stack_frame_offset = 0;
1606 node->local.inlinable = tree_inlinable_function_p (current_function_decl);
1607 inline_summary (node)->self_insns = estimate_num_insns (current_function_decl,
1608 &eni_inlining_weights);
1609 if (node->local.inlinable && !node->local.disregard_inline_limits)
1610 node->local.disregard_inline_limits
1611 = DECL_DISREGARD_INLINE_LIMITS (current_function_decl);
1612 if (flag_really_no_inline && !node->local.disregard_inline_limits)
1613 node->local.inlinable = 0;
1614 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
1615 node->global.insns = inline_summary (node)->self_insns;
1616 return 0;
1617 }
1618
1619
1620 /* Compute parameters of functions used by inliner using
1621 current_function_decl. */
1622 static unsigned int
1623 compute_inline_parameters_for_current (void)
1624 {
1625 compute_inline_parameters (cgraph_node (current_function_decl));
1626 return 0;
1627 }
1628
1629 /* When inlining shall be performed. */
1630 static bool
1631 gate_inline_passes (void)
1632 {
1633 return flag_inline_trees;
1634 }
1635
1636 struct gimple_opt_pass pass_inline_parameters =
1637 {
1638 {
1639 GIMPLE_PASS,
1640 NULL, /* name */
1641 gate_inline_passes, /* gate */
1642 compute_inline_parameters_for_current,/* execute */
1643 NULL, /* sub */
1644 NULL, /* next */
1645 0, /* static_pass_number */
1646 TV_INLINE_HEURISTICS, /* tv_id */
1647 0, /* properties_required */
1648 PROP_cfg, /* properties_provided */
1649 0, /* properties_destroyed */
1650 0, /* todo_flags_start */
1651 0 /* todo_flags_finish */
1652 }
1653 };
1654
1655 /* This function performs intraprocedural analyzis in NODE that is required to
1656 inline indirect calls. */
1657 static void
1658 inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
1659 {
1660 struct cgraph_edge *cs;
1661
1662 ipa_count_formal_params (node);
1663 ipa_create_param_decls_array (node);
1664 ipa_detect_param_modifications (node);
1665 ipa_analyze_params_uses (node);
1666
1667 if (dump_file)
1668 ipa_print_node_param_flags (dump_file, node);
1669
1670 for (cs = node->callees; cs; cs = cs->next_callee)
1671 {
1672 ipa_count_arguments (cs);
1673 ipa_compute_jump_functions (cs);
1674 }
1675
1676 if (dump_file)
1677 ipa_print_node_jump_functions (dump_file, node);
1678 }
1679
1680 /* Note function body size. */
1681 static void
1682 inline_generate_summary (void)
1683 {
1684 struct cgraph_node **order =
1685 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1686 int nnodes = cgraph_postorder (order);
1687 int i;
1688
1689 if (flag_indirect_inlining)
1690 {
1691 ipa_register_cgraph_hooks ();
1692 ipa_check_create_node_params ();
1693 ipa_check_create_edge_args ();
1694 }
1695
1696 for (i = nnodes - 1; i >= 0; i--)
1697 {
1698 struct cgraph_node *node = order[i];
1699
1700 /* Allow possibly removed nodes to be garbage collected. */
1701 order[i] = NULL;
1702 if (node->analyzed && (node->needed || node->reachable))
1703 {
1704 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1705 current_function_decl = node->decl;
1706 compute_inline_parameters (node);
1707
1708 if (flag_indirect_inlining)
1709 inline_indirect_intraprocedural_analysis (node);
1710
1711 pop_cfun ();
1712 }
1713 }
1714
1715 current_function_decl = NULL;
1716 free (order);
1717 return;
1718 }
1719
1720 /* Apply inline plan to function. */
1721 static unsigned int
1722 inline_transform (struct cgraph_node *node)
1723 {
1724 unsigned int todo = 0;
1725 struct cgraph_edge *e;
1726
1727 /* We might need the body of this function so that we can expand
1728 it inline somewhere else. */
1729 if (cgraph_preserve_function_body_p (current_function_decl))
1730 save_inline_function_body (node);
1731
1732 for (e = node->callees; e; e = e->next_callee)
1733 if (!e->inline_failed || warn_inline)
1734 break;
1735 if (e)
1736 {
1737 timevar_push (TV_INTEGRATION);
1738 todo = optimize_inline_calls (current_function_decl);
1739 timevar_pop (TV_INTEGRATION);
1740 }
1741 return todo | execute_fixup_cfg ();
1742 }
1743
1744 struct ipa_opt_pass pass_ipa_inline =
1745 {
1746 {
1747 IPA_PASS,
1748 "inline", /* name */
1749 cgraph_gate_inlining, /* gate */
1750 cgraph_decide_inlining, /* execute */
1751 NULL, /* sub */
1752 NULL, /* next */
1753 0, /* static_pass_number */
1754 TV_INLINE_HEURISTICS, /* tv_id */
1755 0, /* properties_required */
1756 PROP_cfg, /* properties_provided */
1757 0, /* properties_destroyed */
1758 TODO_remove_functions, /* todo_flags_finish */
1759 TODO_dump_cgraph | TODO_dump_func
1760 | TODO_remove_functions /* todo_flags_finish */
1761 },
1762 inline_generate_summary, /* generate_summary */
1763 NULL, /* write_summary */
1764 NULL, /* read_summary */
1765 NULL, /* function_read_summary */
1766 0, /* TODOs */
1767 inline_transform, /* function_transform */
1768 NULL, /* variable_transform */
1769 };
1770
1771
1772 /* When inlining shall be performed. */
1773 static bool
1774 cgraph_gate_O0_always_inline (void)
1775 {
1776 return !flag_unit_at_a_time || !flag_inline_trees;
1777 }
1778
1779 static unsigned int
1780 cgraph_O0_always_inline (void)
1781 {
1782 struct cgraph_node *node = cgraph_node (current_function_decl);
1783 unsigned int todo = 0;
1784 bool inlined;
1785
1786 if (sorrycount || errorcount)
1787 return 0;
1788 inlined = cgraph_decide_inlining_incrementally (node, INLINE_SPEED, 0);
1789 /* We might need the body of this function so that we can expand
1790 it inline somewhere else. */
1791 if (cgraph_preserve_function_body_p (current_function_decl))
1792 save_inline_function_body (node);
1793 if (inlined || warn_inline)
1794 {
1795 timevar_push (TV_INTEGRATION);
1796 todo = optimize_inline_calls (current_function_decl);
1797 timevar_pop (TV_INTEGRATION);
1798 }
1799 /* In non-unit-at-a-time we must mark all referenced functions as needed. */
1800 if (!flag_unit_at_a_time)
1801 {
1802 struct cgraph_edge *e;
1803 for (e = node->callees; e; e = e->next_callee)
1804 if (e->callee->analyzed)
1805 cgraph_mark_needed_node (e->callee);
1806 }
1807 return todo | execute_fixup_cfg ();
1808 }
1809
1810 struct gimple_opt_pass pass_O0_always_inline =
1811 {
1812 {
1813 GIMPLE_PASS,
1814 "always_inline", /* name */
1815 cgraph_gate_O0_always_inline, /* gate */
1816 cgraph_O0_always_inline, /* execute */
1817 NULL, /* sub */
1818 NULL, /* next */
1819 0, /* static_pass_number */
1820 TV_INLINE_HEURISTICS, /* tv_id */
1821 0, /* properties_required */
1822 PROP_cfg, /* properties_provided */
1823 0, /* properties_destroyed */
1824 0, /* todo_flags_start */
1825 TODO_dump_func | TODO_verify_flow
1826 | TODO_verify_stmts /* todo_flags_finish */
1827 }
1828 };
1829
1830 #include "gt-ipa-inline.h"