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