gcc/
[official-gcc.git] / gcc / ipa-inline-transform.c
blob1c77e86723a27980f4a97c8a64618dc072de9b04
1 /* Callgraph transformations to handle inlining
2 Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* The inline decisions are stored in callgraph in "inline plan" and
23 applied later.
25 To mark given call inline, use inline_call function.
26 The function marks the edge inlinable and, if necessary, produces
27 virtual clone in the callgraph representing the new copy of callee's
28 function body.
30 The inline plan is applied on given function body by inline_transform. */
32 #include "config.h"
33 #include "system.h"
34 #include "coretypes.h"
35 #include "tm.h"
36 #include "tree.h"
37 #include "langhooks.h"
38 #include "cgraph.h"
39 #include "timevar.h"
40 #include "output.h"
41 #include "intl.h"
42 #include "coverage.h"
43 #include "ggc.h"
44 #include "tree-flow.h"
45 #include "ipa-prop.h"
46 #include "ipa-inline.h"
47 #include "tree-inline.h"
48 #include "tree-pass.h"
50 int ncalls_inlined;
51 int nfunctions_inlined;
53 /* Scale frequency of NODE edges by FREQ_SCALE. */
55 static void
56 update_noncloned_frequencies (struct cgraph_node *node,
57 int freq_scale)
59 struct cgraph_edge *e;
61 /* We do not want to ignore high loop nest after freq drops to 0. */
62 if (!freq_scale)
63 freq_scale = 1;
64 for (e = node->callees; e; e = e->next_callee)
66 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
67 if (e->frequency > CGRAPH_FREQ_MAX)
68 e->frequency = CGRAPH_FREQ_MAX;
69 if (!e->inline_failed)
70 update_noncloned_frequencies (e->callee, freq_scale);
72 for (e = node->indirect_calls; e; e = e->next_callee)
74 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
75 if (e->frequency > CGRAPH_FREQ_MAX)
76 e->frequency = CGRAPH_FREQ_MAX;
80 /* We removed or are going to remove the last call to NODE.
81 Return true if we can and want proactively remove the NODE now.
82 This is important to do, since we want inliner to know when offline
83 copy of function was removed. */
85 static bool
86 can_remove_node_now_p_1 (struct cgraph_node *node)
88 /* FIXME: When address is taken of DECL_EXTERNAL function we still
89 can remove its offline copy, but we would need to keep unanalyzed node in
90 the callgraph so references can point to it. */
91 return (!node->symbol.address_taken
92 && !ipa_ref_has_aliases_p (&node->symbol.ref_list)
93 && cgraph_can_remove_if_no_direct_calls_p (node)
94 /* Inlining might enable more devirtualizing, so we want to remove
95 those only after all devirtualizable virtual calls are processed.
96 Lacking may edges in callgraph we just preserve them post
97 inlining. */
98 && (!DECL_VIRTUAL_P (node->symbol.decl)
99 || (!DECL_COMDAT (node->symbol.decl)
100 && !DECL_EXTERNAL (node->symbol.decl)))
101 /* During early inlining some unanalyzed cgraph nodes might be in the
102 callgraph and they might reffer the function in question. */
103 && !cgraph_new_nodes);
106 /* We are going to eliminate last direct call to NODE (or alias of it) via edge E.
107 Verify that the NODE can be removed from unit and if it is contained in comdat
108 group that the whole comdat group is removable. */
110 static bool
111 can_remove_node_now_p (struct cgraph_node *node, struct cgraph_edge *e)
113 struct cgraph_node *next;
114 if (!can_remove_node_now_p_1 (node))
115 return false;
117 /* When we see same comdat group, we need to be sure that all
118 items can be removed. */
119 if (!node->symbol.same_comdat_group)
120 return true;
121 for (next = cgraph (node->symbol.same_comdat_group);
122 next != node; next = cgraph (next->symbol.same_comdat_group))
123 if ((next->callers && next->callers != e)
124 || !can_remove_node_now_p_1 (next))
125 return false;
126 return true;
130 /* E is expected to be an edge being inlined. Clone destination node of
131 the edge and redirect it to the new clone.
132 DUPLICATE is used for bookkeeping on whether we are actually creating new
133 clones or re-using node originally representing out-of-line function call.
136 void
137 clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
138 bool update_original, int *overall_size)
140 if (duplicate)
142 /* We may eliminate the need for out-of-line copy to be output.
143 In that case just go ahead and re-use it. This is not just an
144 memory optimization. Making offline copy of fuction disappear
145 from the program will improve future decisions on inlining. */
146 if (!e->callee->callers->next_caller
147 /* Recursive inlining never wants the master clone to
148 be overwritten. */
149 && update_original
150 && can_remove_node_now_p (e->callee, e))
152 /* TODO: When callee is in a comdat group, we could remove all of it,
153 including all inline clones inlined into it. That would however
154 need small function inlining to register edge removal hook to
155 maintain the priority queue.
157 For now we keep the ohter functions in the group in program until
158 cgraph_remove_unreachable_functions gets rid of them. */
159 gcc_assert (!e->callee->global.inlined_to);
160 symtab_dissolve_same_comdat_group_list ((symtab_node) e->callee);
161 if (e->callee->analyzed && !DECL_EXTERNAL (e->callee->symbol.decl))
163 if (overall_size)
164 *overall_size -= inline_summary (e->callee)->size;
165 nfunctions_inlined++;
167 duplicate = false;
168 e->callee->symbol.externally_visible = false;
169 update_noncloned_frequencies (e->callee, e->frequency);
171 else
173 struct cgraph_node *n;
174 n = cgraph_clone_node (e->callee, e->callee->symbol.decl,
175 e->count, e->frequency,
176 update_original, NULL, true);
177 cgraph_redirect_edge_callee (e, n);
180 else
181 symtab_dissolve_same_comdat_group_list ((symtab_node) e->callee);
183 if (e->caller->global.inlined_to)
184 e->callee->global.inlined_to = e->caller->global.inlined_to;
185 else
186 e->callee->global.inlined_to = e->caller;
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. Return true iff any new callgraph edges were discovered as a
199 result of inlining. */
201 bool
202 inline_call (struct cgraph_edge *e, bool update_original,
203 VEC (cgraph_edge_p, heap) **new_edges,
204 int *overall_size)
206 int old_size = 0, new_size = 0;
207 struct cgraph_node *to = NULL;
208 struct cgraph_edge *curr = e;
209 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
211 /* Don't inline inlined edges. */
212 gcc_assert (e->inline_failed);
213 /* Don't even think of inlining inline clone. */
214 gcc_assert (!callee->global.inlined_to);
216 e->inline_failed = CIF_OK;
217 DECL_POSSIBLY_INLINED (callee->symbol.decl) = true;
219 to = e->caller;
220 if (to->global.inlined_to)
221 to = to->global.inlined_to;
223 /* If aliases are involved, redirect edge to the actual destination and
224 possibly remove the aliases. */
225 if (e->callee != callee)
227 struct cgraph_node *alias = e->callee, *next_alias;
228 cgraph_redirect_edge_callee (e, callee);
229 while (alias && alias != callee)
231 if (!alias->callers
232 && can_remove_node_now_p (alias, e))
234 next_alias = cgraph_alias_aliased_node (alias);
235 cgraph_remove_node (alias);
236 alias = next_alias;
238 else
239 break;
243 clone_inlined_nodes (e, true, update_original, overall_size);
245 gcc_assert (curr->callee->global.inlined_to == to);
247 old_size = inline_summary (to)->size;
248 inline_merge_summary (e);
249 new_size = inline_summary (to)->size;
250 if (overall_size)
251 *overall_size += new_size - old_size;
252 ncalls_inlined++;
254 /* This must happen after inline_merge_summary that rely on jump
255 functions of callee to not be updated. */
256 if (optimize)
257 return ipa_propagate_indirect_call_infos (curr, new_edges);
258 else
259 return false;
263 /* Copy function body of NODE and redirect all inline clones to it.
264 This is done before inline plan is applied to NODE when there are
265 still some inline clones if it.
267 This is neccesary because inline decisions are not really transitive
268 and the other inline clones may have different bodies. */
270 static struct cgraph_node *
271 save_inline_function_body (struct cgraph_node *node)
273 struct cgraph_node *first_clone, *n;
275 if (dump_file)
276 fprintf (dump_file, "\nSaving body of %s for later reuse\n",
277 cgraph_node_name (node));
279 gcc_assert (node == cgraph_get_node (node->symbol.decl));
281 /* first_clone will be turned into real function. */
282 first_clone = node->clones;
283 first_clone->symbol.decl = copy_node (node->symbol.decl);
284 symtab_insert_node_to_hashtable ((symtab_node) first_clone);
285 gcc_assert (first_clone == cgraph_get_node (first_clone->symbol.decl));
287 /* Now reshape the clone tree, so all other clones descends from
288 first_clone. */
289 if (first_clone->next_sibling_clone)
291 for (n = first_clone->next_sibling_clone; n->next_sibling_clone; n = n->next_sibling_clone)
292 n->clone_of = first_clone;
293 n->clone_of = first_clone;
294 n->next_sibling_clone = first_clone->clones;
295 if (first_clone->clones)
296 first_clone->clones->prev_sibling_clone = n;
297 first_clone->clones = first_clone->next_sibling_clone;
298 first_clone->next_sibling_clone->prev_sibling_clone = NULL;
299 first_clone->next_sibling_clone = NULL;
300 gcc_assert (!first_clone->prev_sibling_clone);
302 first_clone->clone_of = NULL;
304 /* Now node in question has no clones. */
305 node->clones = NULL;
307 /* Inline clones share decl with the function they are cloned
308 from. Walk the whole clone tree and redirect them all to the
309 new decl. */
310 if (first_clone->clones)
311 for (n = first_clone->clones; n != first_clone;)
313 gcc_assert (n->symbol.decl == node->symbol.decl);
314 n->symbol.decl = first_clone->symbol.decl;
315 if (n->clones)
316 n = n->clones;
317 else if (n->next_sibling_clone)
318 n = n->next_sibling_clone;
319 else
321 while (n != first_clone && !n->next_sibling_clone)
322 n = n->clone_of;
323 if (n != first_clone)
324 n = n->next_sibling_clone;
328 /* Copy the OLD_VERSION_NODE function tree to the new version. */
329 tree_function_versioning (node->symbol.decl, first_clone->symbol.decl,
330 NULL, true, NULL, false, NULL, NULL);
332 /* The function will be short lived and removed after we inline all the clones,
333 but make it internal so we won't confuse ourself. */
334 DECL_EXTERNAL (first_clone->symbol.decl) = 0;
335 DECL_COMDAT_GROUP (first_clone->symbol.decl) = NULL_TREE;
336 TREE_PUBLIC (first_clone->symbol.decl) = 0;
337 DECL_COMDAT (first_clone->symbol.decl) = 0;
338 VEC_free (ipa_opt_pass, heap,
339 first_clone->ipa_transforms_to_apply);
340 first_clone->ipa_transforms_to_apply = NULL;
342 /* When doing recursive inlining, the clone may become unnecessary.
343 This is possible i.e. in the case when the recursive function is proved to be
344 non-throwing and the recursion happens only in the EH landing pad.
345 We can not remove the clone until we are done with saving the body.
346 Remove it now. */
347 if (!first_clone->callers)
349 cgraph_remove_node_and_inline_clones (first_clone, NULL);
350 first_clone = NULL;
352 #ifdef ENABLE_CHECKING
353 else
354 verify_cgraph_node (first_clone);
355 #endif
356 return first_clone;
359 /* Return true when function body of DECL still needs to be kept around
360 for later re-use. */
361 static bool
362 preserve_function_body_p (struct cgraph_node *node)
364 gcc_assert (cgraph_global_info_ready);
365 gcc_assert (!node->alias && !node->thunk.thunk_p);
367 /* Look if there is any clone around. */
368 if (node->clones)
369 return true;
370 return false;
373 /* Apply inline plan to function. */
375 unsigned int
376 inline_transform (struct cgraph_node *node)
378 unsigned int todo = 0;
379 struct cgraph_edge *e;
381 /* FIXME: Currently the pass manager is adding inline transform more than
382 once to some clones. This needs revisiting after WPA cleanups. */
383 if (cfun->after_inlining)
384 return 0;
386 /* We might need the body of this function so that we can expand
387 it inline somewhere else. */
388 if (preserve_function_body_p (node))
389 save_inline_function_body (node);
391 for (e = node->callees; e; e = e->next_callee)
392 cgraph_redirect_edge_call_stmt_to_callee (e);
394 timevar_push (TV_INTEGRATION);
395 if (node->callees)
396 todo = optimize_inline_calls (current_function_decl);
397 timevar_pop (TV_INTEGRATION);
399 cfun->always_inline_functions_inlined = true;
400 cfun->after_inlining = true;
401 todo |= execute_fixup_cfg ();
403 if (!(todo & TODO_update_ssa_any))
404 /* Redirecting edges might lead to a need for vops to be recomputed. */
405 todo |= TODO_update_ssa_only_virtuals;
407 return todo;