1 /* Callgraph transformations to handle inlining
2 Copyright (C) 2003-2014 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"
36 #include "langhooks.h"
42 #include "ipa-inline.h"
43 #include "tree-inline.h"
44 #include "tree-pass.h"
47 int nfunctions_inlined
;
48 bool speculation_removed
;
50 /* Scale frequency of NODE edges by FREQ_SCALE. */
53 update_noncloned_frequencies (struct cgraph_node
*node
,
56 struct cgraph_edge
*e
;
58 /* We do not want to ignore high loop nest after freq drops to 0. */
61 for (e
= node
->callees
; e
; e
= e
->next_callee
)
63 e
->frequency
= e
->frequency
* (gcov_type
) freq_scale
/ CGRAPH_FREQ_BASE
;
64 if (e
->frequency
> CGRAPH_FREQ_MAX
)
65 e
->frequency
= CGRAPH_FREQ_MAX
;
66 if (!e
->inline_failed
)
67 update_noncloned_frequencies (e
->callee
, freq_scale
);
69 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
71 e
->frequency
= e
->frequency
* (gcov_type
) freq_scale
/ CGRAPH_FREQ_BASE
;
72 if (e
->frequency
> CGRAPH_FREQ_MAX
)
73 e
->frequency
= CGRAPH_FREQ_MAX
;
77 /* We removed or are going to remove the last call to NODE.
78 Return true if we can and want proactively remove the NODE now.
79 This is important to do, since we want inliner to know when offline
80 copy of function was removed. */
83 can_remove_node_now_p_1 (struct cgraph_node
*node
)
85 /* FIXME: When address is taken of DECL_EXTERNAL function we still
86 can remove its offline copy, but we would need to keep unanalyzed node in
87 the callgraph so references can point to it. */
88 return (!node
->address_taken
89 && !ipa_ref_has_aliases_p (&node
->ref_list
)
90 && cgraph_can_remove_if_no_direct_calls_p (node
)
91 /* Inlining might enable more devirtualizing, so we want to remove
92 those only after all devirtualizable virtual calls are processed.
93 Lacking may edges in callgraph we just preserve them post
95 && !DECL_VIRTUAL_P (node
->decl
)
96 /* During early inlining some unanalyzed cgraph nodes might be in the
97 callgraph and they might reffer the function in question. */
98 && !cgraph_new_nodes
);
101 /* We are going to eliminate last direct call to NODE (or alias of it) via edge E.
102 Verify that the NODE can be removed from unit and if it is contained in comdat
103 group that the whole comdat group is removable. */
106 can_remove_node_now_p (struct cgraph_node
*node
, struct cgraph_edge
*e
)
108 struct cgraph_node
*next
;
109 if (!can_remove_node_now_p_1 (node
))
112 /* When we see same comdat group, we need to be sure that all
113 items can be removed. */
114 if (!node
->same_comdat_group
)
116 for (next
= cgraph (node
->same_comdat_group
);
117 next
!= node
; next
= cgraph (next
->same_comdat_group
))
118 if ((next
->callers
&& next
->callers
!= e
)
119 || !can_remove_node_now_p_1 (next
))
125 /* E is expected to be an edge being inlined. Clone destination node of
126 the edge and redirect it to the new clone.
127 DUPLICATE is used for bookkeeping on whether we are actually creating new
128 clones or re-using node originally representing out-of-line function call.
129 By default the offline copy is removed, when it appears dead after inlining.
130 UPDATE_ORIGINAL prevents this transformation.
131 If OVERALL_SIZE is non-NULL, the size is updated to reflect the
133 FREQ_SCALE specify the scaling of frequencies of call sites. */
136 clone_inlined_nodes (struct cgraph_edge
*e
, bool duplicate
,
137 bool update_original
, int *overall_size
, int freq_scale
)
139 struct cgraph_node
*inlining_into
;
140 struct cgraph_edge
*next
;
142 if (e
->caller
->global
.inlined_to
)
143 inlining_into
= e
->caller
->global
.inlined_to
;
145 inlining_into
= e
->caller
;
149 /* We may eliminate the need for out-of-line copy to be output.
150 In that case just go ahead and re-use it. This is not just an
151 memory optimization. Making offline copy of fuction disappear
152 from the program will improve future decisions on inlining. */
153 if (!e
->callee
->callers
->next_caller
154 /* Recursive inlining never wants the master clone to
157 && can_remove_node_now_p (e
->callee
, e
))
159 /* TODO: When callee is in a comdat group, we could remove all of it,
160 including all inline clones inlined into it. That would however
161 need small function inlining to register edge removal hook to
162 maintain the priority queue.
164 For now we keep the ohter functions in the group in program until
165 cgraph_remove_unreachable_functions gets rid of them. */
166 gcc_assert (!e
->callee
->global
.inlined_to
);
167 symtab_dissolve_same_comdat_group_list (e
->callee
);
168 if (e
->callee
->definition
&& !DECL_EXTERNAL (e
->callee
->decl
))
171 *overall_size
-= inline_summary (e
->callee
)->size
;
172 nfunctions_inlined
++;
175 e
->callee
->externally_visible
= false;
176 update_noncloned_frequencies (e
->callee
, e
->frequency
);
180 struct cgraph_node
*n
;
182 if (freq_scale
== -1)
183 freq_scale
= e
->frequency
;
184 n
= cgraph_clone_node (e
->callee
, e
->callee
->decl
,
185 e
->count
, freq_scale
, update_original
,
186 vNULL
, true, inlining_into
, NULL
);
187 n
->used_as_abstract_origin
= e
->callee
->used_as_abstract_origin
;
188 cgraph_redirect_edge_callee (e
, n
);
192 symtab_dissolve_same_comdat_group_list (e
->callee
);
194 e
->callee
->global
.inlined_to
= inlining_into
;
196 /* Recursively clone all bodies. */
197 for (e
= e
->callee
->callees
; e
; e
= next
)
199 next
= e
->next_callee
;
200 if (!e
->inline_failed
)
201 clone_inlined_nodes (e
, duplicate
, update_original
, overall_size
, freq_scale
);
202 if (e
->speculative
&& !speculation_useful_p (e
, true))
204 cgraph_resolve_speculation (e
, NULL
);
205 speculation_removed
= true;
211 /* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL
212 specify whether profile of original function should be updated. If any new
213 indirect edges are discovered in the process, add them to NEW_EDGES, unless
214 it is NULL. If UPDATE_OVERALL_SUMMARY is false, do not bother to recompute overall
215 size of caller after inlining. Caller is required to eventually do it via
216 inline_update_overall_summary.
217 If callee_removed is non-NULL, set it to true if we removed callee node.
219 Return true iff any new callgraph edges were discovered as a
220 result of inlining. */
223 inline_call (struct cgraph_edge
*e
, bool update_original
,
224 vec
<cgraph_edge_p
> *new_edges
,
225 int *overall_size
, bool update_overall_summary
,
226 bool *callee_removed
)
228 int old_size
= 0, new_size
= 0;
229 struct cgraph_node
*to
= NULL
;
230 struct cgraph_edge
*curr
= e
;
231 struct cgraph_node
*callee
= cgraph_function_or_thunk_node (e
->callee
, NULL
);
232 bool new_edges_found
= false;
234 #ifdef ENABLE_CHECKING
235 int estimated_growth
= estimate_edge_growth (e
);
236 bool predicated
= inline_edge_summary (e
)->predicate
!= NULL
;
239 speculation_removed
= false;
240 /* Don't inline inlined edges. */
241 gcc_assert (e
->inline_failed
);
242 /* Don't even think of inlining inline clone. */
243 gcc_assert (!callee
->global
.inlined_to
);
245 e
->inline_failed
= CIF_OK
;
246 DECL_POSSIBLY_INLINED (callee
->decl
) = true;
249 if (to
->global
.inlined_to
)
250 to
= to
->global
.inlined_to
;
252 /* If aliases are involved, redirect edge to the actual destination and
253 possibly remove the aliases. */
254 if (e
->callee
!= callee
)
256 struct cgraph_node
*alias
= e
->callee
, *next_alias
;
257 cgraph_redirect_edge_callee (e
, callee
);
258 while (alias
&& alias
!= callee
)
261 && can_remove_node_now_p (alias
, e
))
263 next_alias
= cgraph_alias_target (alias
);
264 cgraph_remove_node (alias
);
266 *callee_removed
= true;
274 clone_inlined_nodes (e
, true, update_original
, overall_size
, e
->frequency
);
276 gcc_assert (curr
->callee
->global
.inlined_to
== to
);
278 old_size
= inline_summary (to
)->size
;
279 inline_merge_summary (e
);
281 new_edges_found
= ipa_propagate_indirect_call_infos (curr
, new_edges
);
282 if (update_overall_summary
)
283 inline_update_overall_summary (to
);
284 new_size
= inline_summary (to
)->size
;
286 if (callee
->calls_comdat_local
)
287 to
->calls_comdat_local
= true;
288 else if (to
->calls_comdat_local
&& symtab_comdat_local_p (callee
))
290 struct cgraph_edge
*se
= to
->callees
;
291 for (; se
; se
= se
->next_callee
)
292 if (se
->inline_failed
&& symtab_comdat_local_p (se
->callee
))
295 to
->calls_comdat_local
= false;
298 #ifdef ENABLE_CHECKING
299 /* Verify that estimated growth match real growth. Allow off-by-one
300 error due to INLINE_SIZE_SCALE roudoff errors. */
301 gcc_assert (!update_overall_summary
|| !overall_size
|| new_edges_found
302 || abs (estimated_growth
- (new_size
- old_size
)) <= 1
303 || speculation_removed
304 /* FIXME: a hack. Edges with false predicate are accounted
305 wrong, we should remove them from callgraph. */
309 /* Account the change of overall unit size; external functions will be
310 removed and are thus not accounted. */
312 && !DECL_EXTERNAL (to
->decl
))
313 *overall_size
+= new_size
- old_size
;
316 /* This must happen after inline_merge_summary that rely on jump
317 functions of callee to not be updated. */
318 return new_edges_found
;
322 /* Copy function body of NODE and redirect all inline clones to it.
323 This is done before inline plan is applied to NODE when there are
324 still some inline clones if it.
326 This is necessary because inline decisions are not really transitive
327 and the other inline clones may have different bodies. */
329 static struct cgraph_node
*
330 save_inline_function_body (struct cgraph_node
*node
)
332 struct cgraph_node
*first_clone
, *n
;
335 fprintf (dump_file
, "\nSaving body of %s for later reuse\n",
338 gcc_assert (node
== cgraph_get_node (node
->decl
));
340 /* first_clone will be turned into real function. */
341 first_clone
= node
->clones
;
342 first_clone
->decl
= copy_node (node
->decl
);
343 symtab_insert_node_to_hashtable (first_clone
);
344 gcc_assert (first_clone
== cgraph_get_node (first_clone
->decl
));
346 /* Now reshape the clone tree, so all other clones descends from
348 if (first_clone
->next_sibling_clone
)
350 for (n
= first_clone
->next_sibling_clone
; n
->next_sibling_clone
; n
= n
->next_sibling_clone
)
351 n
->clone_of
= first_clone
;
352 n
->clone_of
= first_clone
;
353 n
->next_sibling_clone
= first_clone
->clones
;
354 if (first_clone
->clones
)
355 first_clone
->clones
->prev_sibling_clone
= n
;
356 first_clone
->clones
= first_clone
->next_sibling_clone
;
357 first_clone
->next_sibling_clone
->prev_sibling_clone
= NULL
;
358 first_clone
->next_sibling_clone
= NULL
;
359 gcc_assert (!first_clone
->prev_sibling_clone
);
361 first_clone
->clone_of
= NULL
;
363 /* Now node in question has no clones. */
366 /* Inline clones share decl with the function they are cloned
367 from. Walk the whole clone tree and redirect them all to the
369 if (first_clone
->clones
)
370 for (n
= first_clone
->clones
; n
!= first_clone
;)
372 gcc_assert (n
->decl
== node
->decl
);
373 n
->decl
= first_clone
->decl
;
376 else if (n
->next_sibling_clone
)
377 n
= n
->next_sibling_clone
;
380 while (n
!= first_clone
&& !n
->next_sibling_clone
)
382 if (n
!= first_clone
)
383 n
= n
->next_sibling_clone
;
387 /* Copy the OLD_VERSION_NODE function tree to the new version. */
388 tree_function_versioning (node
->decl
, first_clone
->decl
,
389 NULL
, true, NULL
, false,
392 /* The function will be short lived and removed after we inline all the clones,
393 but make it internal so we won't confuse ourself. */
394 DECL_EXTERNAL (first_clone
->decl
) = 0;
395 TREE_PUBLIC (first_clone
->decl
) = 0;
396 DECL_COMDAT (first_clone
->decl
) = 0;
397 first_clone
->ipa_transforms_to_apply
.release ();
399 /* When doing recursive inlining, the clone may become unnecessary.
400 This is possible i.e. in the case when the recursive function is proved to be
401 non-throwing and the recursion happens only in the EH landing pad.
402 We can not remove the clone until we are done with saving the body.
404 if (!first_clone
->callers
)
406 cgraph_remove_node_and_inline_clones (first_clone
, NULL
);
409 #ifdef ENABLE_CHECKING
411 verify_cgraph_node (first_clone
);
416 /* Return true when function body of DECL still needs to be kept around
419 preserve_function_body_p (struct cgraph_node
*node
)
421 gcc_assert (cgraph_global_info_ready
);
422 gcc_assert (!node
->alias
&& !node
->thunk
.thunk_p
);
424 /* Look if there is any clone around. */
430 /* Apply inline plan to function. */
433 inline_transform (struct cgraph_node
*node
)
435 unsigned int todo
= 0;
436 struct cgraph_edge
*e
, *next
;
438 /* FIXME: Currently the pass manager is adding inline transform more than
439 once to some clones. This needs revisiting after WPA cleanups. */
440 if (cfun
->after_inlining
)
443 /* We might need the body of this function so that we can expand
444 it inline somewhere else. */
445 if (preserve_function_body_p (node
))
446 save_inline_function_body (node
);
448 for (e
= node
->callees
; e
; e
= next
)
450 next
= e
->next_callee
;
451 cgraph_redirect_edge_call_stmt_to_callee (e
);
453 ipa_remove_all_references (&node
->ref_list
);
455 timevar_push (TV_INTEGRATION
);
456 if (node
->callees
&& optimize
)
457 todo
= optimize_inline_calls (current_function_decl
);
458 timevar_pop (TV_INTEGRATION
);
460 cfun
->always_inline_functions_inlined
= true;
461 cfun
->after_inlining
= true;
462 todo
|= execute_fixup_cfg ();
464 if (!(todo
& TODO_update_ssa_any
))
465 /* Redirecting edges might lead to a need for vops to be recomputed. */
466 todo
|= TODO_update_ssa_only_virtuals
;