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 "double-int.h"
45 #include "langhooks.h"
52 #include "plugin-api.h"
53 #include "hard-reg-set.h"
58 #include "alloc-pool.h"
59 #include "symbol-summary.h"
61 #include "ipa-inline.h"
62 #include "tree-inline.h"
63 #include "tree-pass.h"
66 int nfunctions_inlined
;
67 bool speculation_removed
;
69 /* Scale frequency of NODE edges by FREQ_SCALE. */
72 update_noncloned_frequencies (struct cgraph_node
*node
,
75 struct cgraph_edge
*e
;
77 /* We do not want to ignore high loop nest after freq drops to 0. */
80 for (e
= node
->callees
; e
; e
= e
->next_callee
)
82 e
->frequency
= e
->frequency
* (gcov_type
) freq_scale
/ CGRAPH_FREQ_BASE
;
83 if (e
->frequency
> CGRAPH_FREQ_MAX
)
84 e
->frequency
= CGRAPH_FREQ_MAX
;
85 if (!e
->inline_failed
)
86 update_noncloned_frequencies (e
->callee
, freq_scale
);
88 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
90 e
->frequency
= e
->frequency
* (gcov_type
) freq_scale
/ CGRAPH_FREQ_BASE
;
91 if (e
->frequency
> CGRAPH_FREQ_MAX
)
92 e
->frequency
= CGRAPH_FREQ_MAX
;
96 /* We removed or are going to remove the last call to NODE.
97 Return true if we can and want proactively remove the NODE now.
98 This is important to do, since we want inliner to know when offline
99 copy of function was removed. */
102 can_remove_node_now_p_1 (struct cgraph_node
*node
, struct cgraph_edge
*e
)
106 FOR_EACH_ALIAS (node
, ref
)
108 cgraph_node
*alias
= dyn_cast
<cgraph_node
*> (ref
->referring
);
109 if ((alias
->callers
&& alias
->callers
!= e
)
110 || !can_remove_node_now_p_1 (alias
, e
))
113 /* FIXME: When address is taken of DECL_EXTERNAL function we still
114 can remove its offline copy, but we would need to keep unanalyzed node in
115 the callgraph so references can point to it. */
116 return (!node
->address_taken
117 && node
->can_remove_if_no_direct_calls_p ()
118 /* Inlining might enable more devirtualizing, so we want to remove
119 those only after all devirtualizable virtual calls are processed.
120 Lacking may edges in callgraph we just preserve them post
122 && (!DECL_VIRTUAL_P (node
->decl
)
123 || !opt_for_fn (node
->decl
, flag_devirtualize
))
124 /* During early inlining some unanalyzed cgraph nodes might be in the
125 callgraph and they might reffer the function in question. */
126 && !cgraph_new_nodes
.exists ());
129 /* We are going to eliminate last direct call to NODE (or alias of it) via edge E.
130 Verify that the NODE can be removed from unit and if it is contained in comdat
131 group that the whole comdat group is removable. */
134 can_remove_node_now_p (struct cgraph_node
*node
, struct cgraph_edge
*e
)
136 struct cgraph_node
*next
;
137 if (!can_remove_node_now_p_1 (node
, e
))
140 /* When we see same comdat group, we need to be sure that all
141 items can be removed. */
142 if (!node
->same_comdat_group
|| !node
->externally_visible
)
144 for (next
= dyn_cast
<cgraph_node
*> (node
->same_comdat_group
);
145 next
!= node
; next
= dyn_cast
<cgraph_node
*> (next
->same_comdat_group
))
149 if ((next
->callers
&& next
->callers
!= e
)
150 || !can_remove_node_now_p_1 (next
, e
))
156 /* Return true if NODE is a master clone with non-inline clones. */
159 master_clone_with_noninline_clones_p (struct cgraph_node
*node
)
164 for (struct cgraph_node
*n
= node
->clones
; n
; n
= n
->next_sibling_clone
)
165 if (n
->decl
!= node
->decl
)
171 /* E is expected to be an edge being inlined. Clone destination node of
172 the edge and redirect it to the new clone.
173 DUPLICATE is used for bookkeeping on whether we are actually creating new
174 clones or re-using node originally representing out-of-line function call.
175 By default the offline copy is removed, when it appears dead after inlining.
176 UPDATE_ORIGINAL prevents this transformation.
177 If OVERALL_SIZE is non-NULL, the size is updated to reflect the
179 FREQ_SCALE specify the scaling of frequencies of call sites. */
182 clone_inlined_nodes (struct cgraph_edge
*e
, bool duplicate
,
183 bool update_original
, int *overall_size
, int freq_scale
)
185 struct cgraph_node
*inlining_into
;
186 struct cgraph_edge
*next
;
188 if (e
->caller
->global
.inlined_to
)
189 inlining_into
= e
->caller
->global
.inlined_to
;
191 inlining_into
= e
->caller
;
195 /* We may eliminate the need for out-of-line copy to be output.
196 In that case just go ahead and re-use it. This is not just an
197 memory optimization. Making offline copy of fuction disappear
198 from the program will improve future decisions on inlining. */
199 if (!e
->callee
->callers
->next_caller
200 /* Recursive inlining never wants the master clone to
203 && can_remove_node_now_p (e
->callee
, e
)
204 /* We cannot overwrite a master clone with non-inline clones
205 until after these clones are materialized. */
206 && !master_clone_with_noninline_clones_p (e
->callee
))
208 /* TODO: When callee is in a comdat group, we could remove all of it,
209 including all inline clones inlined into it. That would however
210 need small function inlining to register edge removal hook to
211 maintain the priority queue.
213 For now we keep the ohter functions in the group in program until
214 cgraph_remove_unreachable_functions gets rid of them. */
215 gcc_assert (!e
->callee
->global
.inlined_to
);
216 e
->callee
->dissolve_same_comdat_group_list ();
217 if (e
->callee
->definition
&& !DECL_EXTERNAL (e
->callee
->decl
))
220 *overall_size
-= inline_summaries
->get (e
->callee
)->size
;
221 nfunctions_inlined
++;
224 e
->callee
->externally_visible
= false;
225 update_noncloned_frequencies (e
->callee
, e
->frequency
);
229 struct cgraph_node
*n
;
231 if (freq_scale
== -1)
232 freq_scale
= e
->frequency
;
233 n
= e
->callee
->create_clone (e
->callee
->decl
,
234 MIN (e
->count
, e
->callee
->count
),
236 update_original
, vNULL
, true,
239 n
->used_as_abstract_origin
= e
->callee
->used_as_abstract_origin
;
240 e
->redirect_callee (n
);
244 e
->callee
->dissolve_same_comdat_group_list ();
246 e
->callee
->global
.inlined_to
= inlining_into
;
248 /* Recursively clone all bodies. */
249 for (e
= e
->callee
->callees
; e
; e
= next
)
251 next
= e
->next_callee
;
252 if (!e
->inline_failed
)
253 clone_inlined_nodes (e
, duplicate
, update_original
, overall_size
, freq_scale
);
254 if (e
->speculative
&& !speculation_useful_p (e
, true))
256 e
->resolve_speculation (NULL
);
257 speculation_removed
= true;
263 /* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL
264 specify whether profile of original function should be updated. If any new
265 indirect edges are discovered in the process, add them to NEW_EDGES, unless
266 it is NULL. If UPDATE_OVERALL_SUMMARY is false, do not bother to recompute overall
267 size of caller after inlining. Caller is required to eventually do it via
268 inline_update_overall_summary.
269 If callee_removed is non-NULL, set it to true if we removed callee node.
271 Return true iff any new callgraph edges were discovered as a
272 result of inlining. */
275 inline_call (struct cgraph_edge
*e
, bool update_original
,
276 vec
<cgraph_edge
*> *new_edges
,
277 int *overall_size
, bool update_overall_summary
,
278 bool *callee_removed
)
280 int old_size
= 0, new_size
= 0;
281 struct cgraph_node
*to
= NULL
;
282 struct cgraph_edge
*curr
= e
;
283 struct cgraph_node
*callee
= e
->callee
->ultimate_alias_target ();
284 bool new_edges_found
= false;
286 #ifdef ENABLE_CHECKING
287 int estimated_growth
= estimate_edge_growth (e
);
288 bool predicated
= inline_edge_summary (e
)->predicate
!= NULL
;
291 speculation_removed
= false;
292 /* Don't inline inlined edges. */
293 gcc_assert (e
->inline_failed
);
294 /* Don't even think of inlining inline clone. */
295 gcc_assert (!callee
->global
.inlined_to
);
297 e
->inline_failed
= CIF_OK
;
298 DECL_POSSIBLY_INLINED (callee
->decl
) = true;
301 if (to
->global
.inlined_to
)
302 to
= to
->global
.inlined_to
;
304 /* If aliases are involved, redirect edge to the actual destination and
305 possibly remove the aliases. */
306 if (e
->callee
!= callee
)
308 struct cgraph_node
*alias
= e
->callee
, *next_alias
;
309 e
->redirect_callee (callee
);
310 while (alias
&& alias
!= callee
)
313 && can_remove_node_now_p (alias
,
314 !e
->next_caller
&& !e
->prev_caller
? e
: NULL
))
316 next_alias
= alias
->get_alias_target ();
319 *callee_removed
= true;
327 clone_inlined_nodes (e
, true, update_original
, overall_size
, e
->frequency
);
329 gcc_assert (curr
->callee
->global
.inlined_to
== to
);
331 old_size
= inline_summaries
->get (to
)->size
;
332 inline_merge_summary (e
);
334 new_edges_found
= ipa_propagate_indirect_call_infos (curr
, new_edges
);
335 if (update_overall_summary
)
336 inline_update_overall_summary (to
);
337 new_size
= inline_summaries
->get (to
)->size
;
339 if (callee
->calls_comdat_local
)
340 to
->calls_comdat_local
= true;
341 else if (to
->calls_comdat_local
&& callee
->comdat_local_p ())
343 struct cgraph_edge
*se
= to
->callees
;
344 for (; se
; se
= se
->next_callee
)
345 if (se
->inline_failed
&& se
->callee
->comdat_local_p ())
348 to
->calls_comdat_local
= false;
351 #ifdef ENABLE_CHECKING
352 /* Verify that estimated growth match real growth. Allow off-by-one
353 error due to INLINE_SIZE_SCALE roudoff errors. */
354 gcc_assert (!update_overall_summary
|| !overall_size
|| new_edges_found
355 || abs (estimated_growth
- (new_size
- old_size
)) <= 1
356 || speculation_removed
357 /* FIXME: a hack. Edges with false predicate are accounted
358 wrong, we should remove them from callgraph. */
362 /* Account the change of overall unit size; external functions will be
363 removed and are thus not accounted. */
365 && !DECL_EXTERNAL (to
->decl
))
366 *overall_size
+= new_size
- old_size
;
369 /* This must happen after inline_merge_summary that rely on jump
370 functions of callee to not be updated. */
371 return new_edges_found
;
375 /* Copy function body of NODE and redirect all inline clones to it.
376 This is done before inline plan is applied to NODE when there are
377 still some inline clones if it.
379 This is necessary because inline decisions are not really transitive
380 and the other inline clones may have different bodies. */
382 static struct cgraph_node
*
383 save_inline_function_body (struct cgraph_node
*node
)
385 struct cgraph_node
*first_clone
, *n
;
388 fprintf (dump_file
, "\nSaving body of %s for later reuse\n",
391 gcc_assert (node
== cgraph_node::get (node
->decl
));
393 /* first_clone will be turned into real function. */
394 first_clone
= node
->clones
;
395 first_clone
->decl
= copy_node (node
->decl
);
396 first_clone
->decl
->decl_with_vis
.symtab_node
= first_clone
;
397 gcc_assert (first_clone
== cgraph_node::get (first_clone
->decl
));
399 /* Now reshape the clone tree, so all other clones descends from
401 if (first_clone
->next_sibling_clone
)
403 for (n
= first_clone
->next_sibling_clone
; n
->next_sibling_clone
; n
= n
->next_sibling_clone
)
404 n
->clone_of
= first_clone
;
405 n
->clone_of
= first_clone
;
406 n
->next_sibling_clone
= first_clone
->clones
;
407 if (first_clone
->clones
)
408 first_clone
->clones
->prev_sibling_clone
= n
;
409 first_clone
->clones
= first_clone
->next_sibling_clone
;
410 first_clone
->next_sibling_clone
->prev_sibling_clone
= NULL
;
411 first_clone
->next_sibling_clone
= NULL
;
412 gcc_assert (!first_clone
->prev_sibling_clone
);
414 first_clone
->clone_of
= NULL
;
416 /* Now node in question has no clones. */
419 /* Inline clones share decl with the function they are cloned
420 from. Walk the whole clone tree and redirect them all to the
422 if (first_clone
->clones
)
423 for (n
= first_clone
->clones
; n
!= first_clone
;)
425 gcc_assert (n
->decl
== node
->decl
);
426 n
->decl
= first_clone
->decl
;
429 else if (n
->next_sibling_clone
)
430 n
= n
->next_sibling_clone
;
433 while (n
!= first_clone
&& !n
->next_sibling_clone
)
435 if (n
!= first_clone
)
436 n
= n
->next_sibling_clone
;
440 /* Copy the OLD_VERSION_NODE function tree to the new version. */
441 tree_function_versioning (node
->decl
, first_clone
->decl
,
442 NULL
, true, NULL
, false,
445 /* The function will be short lived and removed after we inline all the clones,
446 but make it internal so we won't confuse ourself. */
447 DECL_EXTERNAL (first_clone
->decl
) = 0;
448 TREE_PUBLIC (first_clone
->decl
) = 0;
449 DECL_COMDAT (first_clone
->decl
) = 0;
450 first_clone
->ipa_transforms_to_apply
.release ();
452 /* When doing recursive inlining, the clone may become unnecessary.
453 This is possible i.e. in the case when the recursive function is proved to be
454 non-throwing and the recursion happens only in the EH landing pad.
455 We can not remove the clone until we are done with saving the body.
457 if (!first_clone
->callers
)
459 first_clone
->remove_symbol_and_inline_clones ();
462 #ifdef ENABLE_CHECKING
464 first_clone
->verify ();
469 /* Return true when function body of DECL still needs to be kept around
472 preserve_function_body_p (struct cgraph_node
*node
)
474 gcc_assert (symtab
->global_info_ready
);
475 gcc_assert (!node
->alias
&& !node
->thunk
.thunk_p
);
477 /* Look if there is any clone around. */
483 /* Apply inline plan to function. */
486 inline_transform (struct cgraph_node
*node
)
488 unsigned int todo
= 0;
489 struct cgraph_edge
*e
, *next
;
490 bool has_inline
= false;
492 /* FIXME: Currently the pass manager is adding inline transform more than
493 once to some clones. This needs revisiting after WPA cleanups. */
494 if (cfun
->after_inlining
)
497 /* We might need the body of this function so that we can expand
498 it inline somewhere else. */
499 if (preserve_function_body_p (node
))
500 save_inline_function_body (node
);
502 for (e
= node
->callees
; e
; e
= next
)
504 if (!e
->inline_failed
)
506 next
= e
->next_callee
;
507 e
->redirect_call_stmt_to_callee ();
509 node
->remove_all_references ();
511 timevar_push (TV_INTEGRATION
);
512 if (node
->callees
&& (optimize
|| has_inline
))
513 todo
= optimize_inline_calls (current_function_decl
);
514 timevar_pop (TV_INTEGRATION
);
516 cfun
->always_inline_functions_inlined
= true;
517 cfun
->after_inlining
= true;
518 todo
|= execute_fixup_cfg ();
520 if (!(todo
& TODO_update_ssa_any
))
521 /* Redirecting edges might lead to a need for vops to be recomputed. */
522 todo
|= TODO_update_ssa_only_virtuals
;