1 /* Callgraph transformations to handle inlining
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
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
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
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/>. */
21 /* The inline decisions are stored in callgraph in "inline plan" and
24 To mark given call inline, use inline_call function.
25 The function marks the edge inlinable and, if necessary, produces
26 virtual clone in the callgraph representing the new copy of callee's
29 The inline plan is applied on given function body by inline_transform. */
33 #include "coretypes.h"
38 #include "langhooks.h"
42 #include "plugin-api.h"
43 #include "hard-reg-set.h"
47 #include "alloc-pool.h"
48 #include "symbol-summary.h"
50 #include "ipa-inline.h"
51 #include "tree-inline.h"
52 #include "tree-pass.h"
55 int nfunctions_inlined
;
57 /* Scale frequency of NODE edges by FREQ_SCALE. */
60 update_noncloned_frequencies (struct cgraph_node
*node
,
63 struct cgraph_edge
*e
;
65 /* We do not want to ignore high loop nest after freq drops to 0. */
68 for (e
= node
->callees
; e
; e
= e
->next_callee
)
70 e
->frequency
= e
->frequency
* (gcov_type
) freq_scale
/ CGRAPH_FREQ_BASE
;
71 if (e
->frequency
> CGRAPH_FREQ_MAX
)
72 e
->frequency
= CGRAPH_FREQ_MAX
;
73 if (!e
->inline_failed
)
74 update_noncloned_frequencies (e
->callee
, freq_scale
);
76 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
78 e
->frequency
= e
->frequency
* (gcov_type
) freq_scale
/ CGRAPH_FREQ_BASE
;
79 if (e
->frequency
> CGRAPH_FREQ_MAX
)
80 e
->frequency
= CGRAPH_FREQ_MAX
;
84 /* We removed or are going to remove the last call to NODE.
85 Return true if we can and want proactively remove the NODE now.
86 This is important to do, since we want inliner to know when offline
87 copy of function was removed. */
90 can_remove_node_now_p_1 (struct cgraph_node
*node
, struct cgraph_edge
*e
)
94 FOR_EACH_ALIAS (node
, ref
)
96 cgraph_node
*alias
= dyn_cast
<cgraph_node
*> (ref
->referring
);
97 if ((alias
->callers
&& alias
->callers
!= e
)
98 || !can_remove_node_now_p_1 (alias
, e
))
101 /* FIXME: When address is taken of DECL_EXTERNAL function we still
102 can remove its offline copy, but we would need to keep unanalyzed node in
103 the callgraph so references can point to it.
105 Also for comdat group we can ignore references inside a group as we
106 want to prove the group as a whole to be dead. */
107 return (!node
->address_taken
108 && node
->can_remove_if_no_direct_calls_and_refs_p ()
109 /* Inlining might enable more devirtualizing, so we want to remove
110 those only after all devirtualizable virtual calls are processed.
111 Lacking may edges in callgraph we just preserve them post
113 && (!DECL_VIRTUAL_P (node
->decl
)
114 || !opt_for_fn (node
->decl
, flag_devirtualize
))
115 /* During early inlining some unanalyzed cgraph nodes might be in the
116 callgraph and they might reffer the function in question. */
117 && !cgraph_new_nodes
.exists ());
120 /* We are going to eliminate last direct call to NODE (or alias of it) via edge E.
121 Verify that the NODE can be removed from unit and if it is contained in comdat
122 group that the whole comdat group is removable. */
125 can_remove_node_now_p (struct cgraph_node
*node
, struct cgraph_edge
*e
)
127 struct cgraph_node
*next
;
128 if (!can_remove_node_now_p_1 (node
, e
))
131 /* When we see same comdat group, we need to be sure that all
132 items can be removed. */
133 if (!node
->same_comdat_group
|| !node
->externally_visible
)
135 for (next
= dyn_cast
<cgraph_node
*> (node
->same_comdat_group
);
136 next
!= node
; next
= dyn_cast
<cgraph_node
*> (next
->same_comdat_group
))
140 if ((next
->callers
&& next
->callers
!= e
)
141 || !can_remove_node_now_p_1 (next
, e
))
147 /* Return true if NODE is a master clone with non-inline clones. */
150 master_clone_with_noninline_clones_p (struct cgraph_node
*node
)
155 for (struct cgraph_node
*n
= node
->clones
; n
; n
= n
->next_sibling_clone
)
156 if (n
->decl
!= node
->decl
)
162 /* E is expected to be an edge being inlined. Clone destination node of
163 the edge and redirect it to the new clone.
164 DUPLICATE is used for bookkeeping on whether we are actually creating new
165 clones or re-using node originally representing out-of-line function call.
166 By default the offline copy is removed, when it appears dead after inlining.
167 UPDATE_ORIGINAL prevents this transformation.
168 If OVERALL_SIZE is non-NULL, the size is updated to reflect the
170 FREQ_SCALE specify the scaling of frequencies of call sites. */
173 clone_inlined_nodes (struct cgraph_edge
*e
, bool duplicate
,
174 bool update_original
, int *overall_size
, int freq_scale
)
176 struct cgraph_node
*inlining_into
;
177 struct cgraph_edge
*next
;
179 if (e
->caller
->global
.inlined_to
)
180 inlining_into
= e
->caller
->global
.inlined_to
;
182 inlining_into
= e
->caller
;
186 /* We may eliminate the need for out-of-line copy to be output.
187 In that case just go ahead and re-use it. This is not just an
188 memory optimization. Making offline copy of fuction disappear
189 from the program will improve future decisions on inlining. */
190 if (!e
->callee
->callers
->next_caller
191 /* Recursive inlining never wants the master clone to
194 && can_remove_node_now_p (e
->callee
, e
)
195 /* We cannot overwrite a master clone with non-inline clones
196 until after these clones are materialized. */
197 && !master_clone_with_noninline_clones_p (e
->callee
))
199 /* TODO: When callee is in a comdat group, we could remove all of it,
200 including all inline clones inlined into it. That would however
201 need small function inlining to register edge removal hook to
202 maintain the priority queue.
204 For now we keep the ohter functions in the group in program until
205 cgraph_remove_unreachable_functions gets rid of them. */
206 gcc_assert (!e
->callee
->global
.inlined_to
);
207 e
->callee
->remove_from_same_comdat_group ();
208 if (e
->callee
->definition
209 && inline_account_function_p (e
->callee
))
211 gcc_assert (!e
->callee
->alias
);
213 *overall_size
-= inline_summaries
->get (e
->callee
)->size
;
214 nfunctions_inlined
++;
217 e
->callee
->externally_visible
= false;
218 update_noncloned_frequencies (e
->callee
, e
->frequency
);
222 struct cgraph_node
*n
;
224 if (freq_scale
== -1)
225 freq_scale
= e
->frequency
;
226 n
= e
->callee
->create_clone (e
->callee
->decl
,
227 MIN (e
->count
, e
->callee
->count
),
229 update_original
, vNULL
, true,
232 n
->used_as_abstract_origin
= e
->callee
->used_as_abstract_origin
;
233 e
->redirect_callee (n
);
237 e
->callee
->remove_from_same_comdat_group ();
239 e
->callee
->global
.inlined_to
= inlining_into
;
241 /* Recursively clone all bodies. */
242 for (e
= e
->callee
->callees
; e
; e
= next
)
244 next
= e
->next_callee
;
245 if (!e
->inline_failed
)
246 clone_inlined_nodes (e
, duplicate
, update_original
, overall_size
, freq_scale
);
250 /* Check all speculations in N and resolve them if they seems useless. */
253 check_speculations (cgraph_node
*n
)
255 bool speculation_removed
= false;
258 for (cgraph_edge
*e
= n
->callees
; e
; e
= next
)
260 next
= e
->next_callee
;
261 if (e
->speculative
&& !speculation_useful_p (e
, true))
263 e
->resolve_speculation (NULL
);
264 speculation_removed
= true;
266 else if (!e
->inline_failed
)
267 speculation_removed
|= check_speculations (e
->callee
);
269 return speculation_removed
;
272 /* Mark all call graph edges coming out of NODE and all nodes that have been
273 inlined to it as in_polymorphic_cdtor. */
276 mark_all_inlined_calls_cdtor (cgraph_node
*node
)
278 for (cgraph_edge
*cs
= node
->callees
; cs
; cs
= cs
->next_callee
)
280 cs
->in_polymorphic_cdtor
= true;
281 if (!cs
->inline_failed
)
282 mark_all_inlined_calls_cdtor (cs
->callee
);
284 for (cgraph_edge
*cs
= node
->indirect_calls
; cs
; cs
= cs
->next_callee
)
285 cs
->in_polymorphic_cdtor
= true;
289 /* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL
290 specify whether profile of original function should be updated. If any new
291 indirect edges are discovered in the process, add them to NEW_EDGES, unless
292 it is NULL. If UPDATE_OVERALL_SUMMARY is false, do not bother to recompute overall
293 size of caller after inlining. Caller is required to eventually do it via
294 inline_update_overall_summary.
295 If callee_removed is non-NULL, set it to true if we removed callee node.
297 Return true iff any new callgraph edges were discovered as a
298 result of inlining. */
301 inline_call (struct cgraph_edge
*e
, bool update_original
,
302 vec
<cgraph_edge
*> *new_edges
,
303 int *overall_size
, bool update_overall_summary
,
304 bool *callee_removed
)
306 int old_size
= 0, new_size
= 0;
307 struct cgraph_node
*to
= NULL
;
308 struct cgraph_edge
*curr
= e
;
309 struct cgraph_node
*callee
= e
->callee
->ultimate_alias_target ();
310 bool new_edges_found
= false;
312 /* This is used only for assert bellow. */
314 int estimated_growth
= estimate_edge_growth (e
);
315 bool predicated
= inline_edge_summary (e
)->predicate
!= NULL
;
318 /* Don't inline inlined edges. */
319 gcc_assert (e
->inline_failed
);
320 /* Don't even think of inlining inline clone. */
321 gcc_assert (!callee
->global
.inlined_to
);
323 e
->inline_failed
= CIF_OK
;
324 DECL_POSSIBLY_INLINED (callee
->decl
) = true;
327 if (to
->global
.inlined_to
)
328 to
= to
->global
.inlined_to
;
330 if (DECL_FUNCTION_PERSONALITY (callee
->decl
))
331 DECL_FUNCTION_PERSONALITY (to
->decl
)
332 = DECL_FUNCTION_PERSONALITY (callee
->decl
);
334 /* If aliases are involved, redirect edge to the actual destination and
335 possibly remove the aliases. */
336 if (e
->callee
!= callee
)
338 struct cgraph_node
*alias
= e
->callee
, *next_alias
;
339 e
->redirect_callee (callee
);
340 while (alias
&& alias
!= callee
)
343 && can_remove_node_now_p (alias
,
344 !e
->next_caller
&& !e
->prev_caller
? e
: NULL
))
346 next_alias
= alias
->get_alias_target ();
349 *callee_removed
= true;
357 clone_inlined_nodes (e
, true, update_original
, overall_size
, e
->frequency
);
359 gcc_assert (curr
->callee
->global
.inlined_to
== to
);
361 old_size
= inline_summaries
->get (to
)->size
;
362 inline_merge_summary (e
);
363 if (e
->in_polymorphic_cdtor
)
364 mark_all_inlined_calls_cdtor (e
->callee
);
365 if (opt_for_fn (e
->caller
->decl
, optimize
))
366 new_edges_found
= ipa_propagate_indirect_call_infos (curr
, new_edges
);
367 check_speculations (e
->callee
);
368 if (update_overall_summary
)
369 inline_update_overall_summary (to
);
370 new_size
= inline_summaries
->get (to
)->size
;
372 if (callee
->calls_comdat_local
)
373 to
->calls_comdat_local
= true;
374 else if (to
->calls_comdat_local
&& callee
->comdat_local_p ())
376 struct cgraph_edge
*se
= to
->callees
;
377 for (; se
; se
= se
->next_callee
)
378 if (se
->inline_failed
&& se
->callee
->comdat_local_p ())
381 to
->calls_comdat_local
= false;
384 /* FIXME: This assert suffers from roundoff errors, disable it for GCC 5
385 and revisit it after conversion to sreals in GCC 6.
388 /* Verify that estimated growth match real growth. Allow off-by-one
389 error due to INLINE_SIZE_SCALE roudoff errors. */
390 gcc_assert (!update_overall_summary
|| !overall_size
|| new_edges_found
391 || abs (estimated_growth
- (new_size
- old_size
)) <= 1
392 || speculation_removed
393 /* FIXME: a hack. Edges with false predicate are accounted
394 wrong, we should remove them from callgraph. */
398 /* Account the change of overall unit size; external functions will be
399 removed and are thus not accounted. */
400 if (overall_size
&& inline_account_function_p (to
))
401 *overall_size
+= new_size
- old_size
;
404 /* This must happen after inline_merge_summary that rely on jump
405 functions of callee to not be updated. */
406 return new_edges_found
;
410 /* Copy function body of NODE and redirect all inline clones to it.
411 This is done before inline plan is applied to NODE when there are
412 still some inline clones if it.
414 This is necessary because inline decisions are not really transitive
415 and the other inline clones may have different bodies. */
417 static struct cgraph_node
*
418 save_inline_function_body (struct cgraph_node
*node
)
420 struct cgraph_node
*first_clone
, *n
;
423 fprintf (dump_file
, "\nSaving body of %s for later reuse\n",
426 gcc_assert (node
== cgraph_node::get (node
->decl
));
428 /* first_clone will be turned into real function. */
429 first_clone
= node
->clones
;
430 first_clone
->decl
= copy_node (node
->decl
);
431 first_clone
->decl
->decl_with_vis
.symtab_node
= first_clone
;
432 gcc_assert (first_clone
== cgraph_node::get (first_clone
->decl
));
434 /* Now reshape the clone tree, so all other clones descends from
436 if (first_clone
->next_sibling_clone
)
438 for (n
= first_clone
->next_sibling_clone
; n
->next_sibling_clone
; n
= n
->next_sibling_clone
)
439 n
->clone_of
= first_clone
;
440 n
->clone_of
= first_clone
;
441 n
->next_sibling_clone
= first_clone
->clones
;
442 if (first_clone
->clones
)
443 first_clone
->clones
->prev_sibling_clone
= n
;
444 first_clone
->clones
= first_clone
->next_sibling_clone
;
445 first_clone
->next_sibling_clone
->prev_sibling_clone
= NULL
;
446 first_clone
->next_sibling_clone
= NULL
;
447 gcc_assert (!first_clone
->prev_sibling_clone
);
449 first_clone
->clone_of
= NULL
;
451 /* Now node in question has no clones. */
454 /* Inline clones share decl with the function they are cloned
455 from. Walk the whole clone tree and redirect them all to the
457 if (first_clone
->clones
)
458 for (n
= first_clone
->clones
; n
!= first_clone
;)
460 gcc_assert (n
->decl
== node
->decl
);
461 n
->decl
= first_clone
->decl
;
464 else if (n
->next_sibling_clone
)
465 n
= n
->next_sibling_clone
;
468 while (n
!= first_clone
&& !n
->next_sibling_clone
)
470 if (n
!= first_clone
)
471 n
= n
->next_sibling_clone
;
475 /* Copy the OLD_VERSION_NODE function tree to the new version. */
476 tree_function_versioning (node
->decl
, first_clone
->decl
,
477 NULL
, true, NULL
, false,
480 /* The function will be short lived and removed after we inline all the clones,
481 but make it internal so we won't confuse ourself. */
482 DECL_EXTERNAL (first_clone
->decl
) = 0;
483 TREE_PUBLIC (first_clone
->decl
) = 0;
484 DECL_COMDAT (first_clone
->decl
) = 0;
485 first_clone
->ipa_transforms_to_apply
.release ();
487 /* When doing recursive inlining, the clone may become unnecessary.
488 This is possible i.e. in the case when the recursive function is proved to be
489 non-throwing and the recursion happens only in the EH landing pad.
490 We can not remove the clone until we are done with saving the body.
492 if (!first_clone
->callers
)
494 first_clone
->remove_symbol_and_inline_clones ();
497 #ifdef ENABLE_CHECKING
499 first_clone
->verify ();
504 /* Return true when function body of DECL still needs to be kept around
507 preserve_function_body_p (struct cgraph_node
*node
)
509 gcc_assert (symtab
->global_info_ready
);
510 gcc_assert (!node
->alias
&& !node
->thunk
.thunk_p
);
512 /* Look if there is any clone around. */
518 /* Apply inline plan to function. */
521 inline_transform (struct cgraph_node
*node
)
523 unsigned int todo
= 0;
524 struct cgraph_edge
*e
, *next
;
525 bool has_inline
= false;
527 /* FIXME: Currently the pass manager is adding inline transform more than
528 once to some clones. This needs revisiting after WPA cleanups. */
529 if (cfun
->after_inlining
)
532 /* We might need the body of this function so that we can expand
533 it inline somewhere else. */
534 if (preserve_function_body_p (node
))
535 save_inline_function_body (node
);
537 for (e
= node
->callees
; e
; e
= next
)
539 if (!e
->inline_failed
)
541 next
= e
->next_callee
;
542 e
->redirect_call_stmt_to_callee ();
544 node
->remove_all_references ();
546 timevar_push (TV_INTEGRATION
);
547 if (node
->callees
&& (opt_for_fn (node
->decl
, optimize
) || has_inline
))
548 todo
= optimize_inline_calls (current_function_decl
);
549 timevar_pop (TV_INTEGRATION
);
551 cfun
->always_inline_functions_inlined
= true;
552 cfun
->after_inlining
= true;
553 todo
|= execute_fixup_cfg ();
555 if (!(todo
& TODO_update_ssa_any
))
556 /* Redirecting edges might lead to a need for vops to be recomputed. */
557 todo
|= TODO_update_ssa_only_virtuals
;