2013-08-02 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / ipa-inline-transform.c
blob5e666aee3871c7c4c7efc9e9fc1c68e212031cdf
1 /* Callgraph transformations to handle inlining
2 Copyright (C) 2003-2013 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
10 version.
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.
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
22 applied later.
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
27 function body.
29 The inline plan is applied on given function body by inline_transform. */
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "tree.h"
36 #include "langhooks.h"
37 #include "cgraph.h"
38 #include "intl.h"
39 #include "coverage.h"
40 #include "ggc.h"
41 #include "tree-flow.h"
42 #include "ipa-prop.h"
43 #include "ipa-inline.h"
44 #include "tree-inline.h"
45 #include "tree-pass.h"
47 int ncalls_inlined;
48 int nfunctions_inlined;
50 /* Scale frequency of NODE edges by FREQ_SCALE. */
52 static void
53 update_noncloned_frequencies (struct cgraph_node *node,
54 int freq_scale)
56 struct cgraph_edge *e;
58 /* We do not want to ignore high loop nest after freq drops to 0. */
59 if (!freq_scale)
60 freq_scale = 1;
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. */
82 static bool
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->symbol.address_taken
89 && !ipa_ref_has_aliases_p (&node->symbol.ref_list)
90 && !node->used_as_abstract_origin
91 && cgraph_can_remove_if_no_direct_calls_p (node)
92 /* Inlining might enable more devirtualizing, so we want to remove
93 those only after all devirtualizable virtual calls are processed.
94 Lacking may edges in callgraph we just preserve them post
95 inlining. */
96 && !DECL_VIRTUAL_P (node->symbol.decl)
97 /* During early inlining some unanalyzed cgraph nodes might be in the
98 callgraph and they might reffer the function in question. */
99 && !cgraph_new_nodes);
102 /* We are going to eliminate last direct call to NODE (or alias of it) via edge E.
103 Verify that the NODE can be removed from unit and if it is contained in comdat
104 group that the whole comdat group is removable. */
106 static bool
107 can_remove_node_now_p (struct cgraph_node *node, struct cgraph_edge *e)
109 struct cgraph_node *next;
110 if (!can_remove_node_now_p_1 (node))
111 return false;
113 /* When we see same comdat group, we need to be sure that all
114 items can be removed. */
115 if (!node->symbol.same_comdat_group)
116 return true;
117 for (next = cgraph (node->symbol.same_comdat_group);
118 next != node; next = cgraph (next->symbol.same_comdat_group))
119 if ((next->callers && next->callers != e)
120 || !can_remove_node_now_p_1 (next))
121 return false;
122 return true;
126 /* E is expected to be an edge being inlined. Clone destination node of
127 the edge and redirect it to the new clone.
128 DUPLICATE is used for bookkeeping on whether we are actually creating new
129 clones or re-using node originally representing out-of-line function call.
132 void
133 clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
134 bool update_original, int *overall_size)
136 struct cgraph_node *inlining_into;
138 if (e->caller->global.inlined_to)
139 inlining_into = e->caller->global.inlined_to;
140 else
141 inlining_into = e->caller;
143 if (duplicate)
145 /* We may eliminate the need for out-of-line copy to be output.
146 In that case just go ahead and re-use it. This is not just an
147 memory optimization. Making offline copy of fuction disappear
148 from the program will improve future decisions on inlining. */
149 if (!e->callee->callers->next_caller
150 /* Recursive inlining never wants the master clone to
151 be overwritten. */
152 && update_original
153 && can_remove_node_now_p (e->callee, e))
155 /* TODO: When callee is in a comdat group, we could remove all of it,
156 including all inline clones inlined into it. That would however
157 need small function inlining to register edge removal hook to
158 maintain the priority queue.
160 For now we keep the ohter functions in the group in program until
161 cgraph_remove_unreachable_functions gets rid of them. */
162 gcc_assert (!e->callee->global.inlined_to);
163 symtab_dissolve_same_comdat_group_list ((symtab_node) e->callee);
164 if (e->callee->symbol.definition && !DECL_EXTERNAL (e->callee->symbol.decl))
166 if (overall_size)
167 *overall_size -= inline_summary (e->callee)->size;
168 nfunctions_inlined++;
170 duplicate = false;
171 e->callee->symbol.externally_visible = false;
172 update_noncloned_frequencies (e->callee, e->frequency);
174 else
176 struct cgraph_node *n;
177 n = cgraph_clone_node (e->callee, e->callee->symbol.decl,
178 e->count, e->frequency, update_original,
179 vNULL, true, inlining_into);
180 cgraph_redirect_edge_callee (e, n);
183 else
184 symtab_dissolve_same_comdat_group_list ((symtab_node) e->callee);
186 e->callee->global.inlined_to = inlining_into;
188 /* Recursively clone all bodies. */
189 for (e = e->callee->callees; e; e = e->next_callee)
190 if (!e->inline_failed)
191 clone_inlined_nodes (e, duplicate, update_original, overall_size);
195 /* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL
196 specify whether profile of original function should be updated. If any new
197 indirect edges are discovered in the process, add them to NEW_EDGES, unless
198 it is NULL. If UPDATE_OVERALL_SUMMARY is false, do not bother to recompute overall
199 size of caller after inlining. Caller is required to eventually do it via
200 inline_update_overall_summary.
202 Return true iff any new callgraph edges were discovered as a
203 result of inlining. */
205 bool
206 inline_call (struct cgraph_edge *e, bool update_original,
207 vec<cgraph_edge_p> *new_edges,
208 int *overall_size, bool update_overall_summary)
210 int old_size = 0, new_size = 0;
211 struct cgraph_node *to = NULL;
212 struct cgraph_edge *curr = e;
213 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
214 bool new_edges_found = false;
216 #ifdef ENABLE_CHECKING
217 int estimated_growth = estimate_edge_growth (e);
218 bool predicated = inline_edge_summary (e)->predicate != NULL;
219 #endif
221 /* Don't inline inlined edges. */
222 gcc_assert (e->inline_failed);
223 /* Don't even think of inlining inline clone. */
224 gcc_assert (!callee->global.inlined_to);
226 e->inline_failed = CIF_OK;
227 DECL_POSSIBLY_INLINED (callee->symbol.decl) = true;
229 to = e->caller;
230 if (to->global.inlined_to)
231 to = to->global.inlined_to;
233 /* If aliases are involved, redirect edge to the actual destination and
234 possibly remove the aliases. */
235 if (e->callee != callee)
237 struct cgraph_node *alias = e->callee, *next_alias;
238 cgraph_redirect_edge_callee (e, callee);
239 while (alias && alias != callee)
241 if (!alias->callers
242 && can_remove_node_now_p (alias, e))
244 next_alias = cgraph_alias_target (alias);
245 cgraph_remove_node (alias);
246 alias = next_alias;
248 else
249 break;
253 clone_inlined_nodes (e, true, update_original, overall_size);
255 gcc_assert (curr->callee->global.inlined_to == to);
257 old_size = inline_summary (to)->size;
258 inline_merge_summary (e);
259 if (optimize)
260 new_edges_found = ipa_propagate_indirect_call_infos (curr, new_edges);
261 if (update_overall_summary)
262 inline_update_overall_summary (to);
263 new_size = inline_summary (to)->size;
265 #ifdef ENABLE_CHECKING
266 /* Verify that estimated growth match real growth. Allow off-by-one
267 error due to INLINE_SIZE_SCALE roudoff errors. */
268 gcc_assert (!update_overall_summary || !overall_size || new_edges_found
269 || abs (estimated_growth - (new_size - old_size)) <= 1
270 /* FIXME: a hack. Edges with false predicate are accounted
271 wrong, we should remove them from callgraph. */
272 || predicated);
273 #endif
275 /* Account the change of overall unit size; external functions will be
276 removed and are thus not accounted. */
277 if (overall_size
278 && !DECL_EXTERNAL (to->symbol.decl))
279 *overall_size += new_size - old_size;
280 ncalls_inlined++;
282 /* This must happen after inline_merge_summary that rely on jump
283 functions of callee to not be updated. */
284 return new_edges_found;
288 /* Copy function body of NODE and redirect all inline clones to it.
289 This is done before inline plan is applied to NODE when there are
290 still some inline clones if it.
292 This is necessary because inline decisions are not really transitive
293 and the other inline clones may have different bodies. */
295 static struct cgraph_node *
296 save_inline_function_body (struct cgraph_node *node)
298 struct cgraph_node *first_clone, *n;
300 if (dump_file)
301 fprintf (dump_file, "\nSaving body of %s for later reuse\n",
302 cgraph_node_name (node));
304 gcc_assert (node == cgraph_get_node (node->symbol.decl));
306 /* first_clone will be turned into real function. */
307 first_clone = node->clones;
308 first_clone->symbol.decl = copy_node (node->symbol.decl);
309 symtab_insert_node_to_hashtable ((symtab_node) first_clone);
310 gcc_assert (first_clone == cgraph_get_node (first_clone->symbol.decl));
312 /* Now reshape the clone tree, so all other clones descends from
313 first_clone. */
314 if (first_clone->next_sibling_clone)
316 for (n = first_clone->next_sibling_clone; n->next_sibling_clone; n = n->next_sibling_clone)
317 n->clone_of = first_clone;
318 n->clone_of = first_clone;
319 n->next_sibling_clone = first_clone->clones;
320 if (first_clone->clones)
321 first_clone->clones->prev_sibling_clone = n;
322 first_clone->clones = first_clone->next_sibling_clone;
323 first_clone->next_sibling_clone->prev_sibling_clone = NULL;
324 first_clone->next_sibling_clone = NULL;
325 gcc_assert (!first_clone->prev_sibling_clone);
327 first_clone->clone_of = NULL;
329 /* Now node in question has no clones. */
330 node->clones = NULL;
332 /* Inline clones share decl with the function they are cloned
333 from. Walk the whole clone tree and redirect them all to the
334 new decl. */
335 if (first_clone->clones)
336 for (n = first_clone->clones; n != first_clone;)
338 gcc_assert (n->symbol.decl == node->symbol.decl);
339 n->symbol.decl = first_clone->symbol.decl;
340 if (n->clones)
341 n = n->clones;
342 else if (n->next_sibling_clone)
343 n = n->next_sibling_clone;
344 else
346 while (n != first_clone && !n->next_sibling_clone)
347 n = n->clone_of;
348 if (n != first_clone)
349 n = n->next_sibling_clone;
353 /* Copy the OLD_VERSION_NODE function tree to the new version. */
354 tree_function_versioning (node->symbol.decl, first_clone->symbol.decl,
355 NULL, true, NULL, false,
356 NULL, NULL);
358 /* The function will be short lived and removed after we inline all the clones,
359 but make it internal so we won't confuse ourself. */
360 DECL_EXTERNAL (first_clone->symbol.decl) = 0;
361 DECL_COMDAT_GROUP (first_clone->symbol.decl) = NULL_TREE;
362 TREE_PUBLIC (first_clone->symbol.decl) = 0;
363 DECL_COMDAT (first_clone->symbol.decl) = 0;
364 first_clone->ipa_transforms_to_apply.release ();
366 /* When doing recursive inlining, the clone may become unnecessary.
367 This is possible i.e. in the case when the recursive function is proved to be
368 non-throwing and the recursion happens only in the EH landing pad.
369 We can not remove the clone until we are done with saving the body.
370 Remove it now. */
371 if (!first_clone->callers)
373 cgraph_remove_node_and_inline_clones (first_clone, NULL);
374 first_clone = NULL;
376 #ifdef ENABLE_CHECKING
377 else
378 verify_cgraph_node (first_clone);
379 #endif
380 return first_clone;
383 /* Return true when function body of DECL still needs to be kept around
384 for later re-use. */
385 static bool
386 preserve_function_body_p (struct cgraph_node *node)
388 gcc_assert (cgraph_global_info_ready);
389 gcc_assert (!node->symbol.alias && !node->thunk.thunk_p);
391 /* Look if there is any clone around. */
392 if (node->clones)
393 return true;
394 return false;
397 /* Apply inline plan to function. */
399 unsigned int
400 inline_transform (struct cgraph_node *node)
402 unsigned int todo = 0;
403 struct cgraph_edge *e;
405 /* FIXME: Currently the pass manager is adding inline transform more than
406 once to some clones. This needs revisiting after WPA cleanups. */
407 if (cfun->after_inlining)
408 return 0;
410 /* We might need the body of this function so that we can expand
411 it inline somewhere else. */
412 if (preserve_function_body_p (node))
413 save_inline_function_body (node);
415 for (e = node->callees; e; e = e->next_callee)
416 cgraph_redirect_edge_call_stmt_to_callee (e);
418 timevar_push (TV_INTEGRATION);
419 if (node->callees)
420 todo = optimize_inline_calls (current_function_decl);
421 timevar_pop (TV_INTEGRATION);
423 cfun->always_inline_functions_inlined = true;
424 cfun->after_inlining = true;
425 todo |= execute_fixup_cfg ();
427 if (!(todo & TODO_update_ssa_any))
428 /* Redirecting edges might lead to a need for vops to be recomputed. */
429 todo |= TODO_update_ssa_only_virtuals;
431 return todo;