re PR rtl-optimization/53589 (ICE in maybe_record_trace_start with asm goto)
[official-gcc.git] / gcc / ipa-inline-transform.c
blob9fdfbb91b2437c2226cf13a20fa3d332f05cc49a
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 "intl.h"
41 #include "coverage.h"
42 #include "ggc.h"
43 #include "tree-flow.h"
44 #include "ipa-prop.h"
45 #include "ipa-inline.h"
46 #include "tree-inline.h"
47 #include "tree-pass.h"
49 int ncalls_inlined;
50 int nfunctions_inlined;
52 /* Scale frequency of NODE edges by FREQ_SCALE. */
54 static void
55 update_noncloned_frequencies (struct cgraph_node *node,
56 int freq_scale)
58 struct cgraph_edge *e;
60 /* We do not want to ignore high loop nest after freq drops to 0. */
61 if (!freq_scale)
62 freq_scale = 1;
63 for (e = node->callees; e; e = e->next_callee)
65 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
66 if (e->frequency > CGRAPH_FREQ_MAX)
67 e->frequency = CGRAPH_FREQ_MAX;
68 if (!e->inline_failed)
69 update_noncloned_frequencies (e->callee, freq_scale);
71 for (e = node->indirect_calls; e; e = e->next_callee)
73 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
74 if (e->frequency > CGRAPH_FREQ_MAX)
75 e->frequency = CGRAPH_FREQ_MAX;
79 /* We removed or are going to remove the last call to NODE.
80 Return true if we can and want proactively remove the NODE now.
81 This is important to do, since we want inliner to know when offline
82 copy of function was removed. */
84 static bool
85 can_remove_node_now_p_1 (struct cgraph_node *node)
87 /* FIXME: When address is taken of DECL_EXTERNAL function we still
88 can remove its offline copy, but we would need to keep unanalyzed node in
89 the callgraph so references can point to it. */
90 return (!node->symbol.address_taken
91 && !ipa_ref_has_aliases_p (&node->symbol.ref_list)
92 && cgraph_can_remove_if_no_direct_calls_p (node)
93 /* Inlining might enable more devirtualizing, so we want to remove
94 those only after all devirtualizable virtual calls are processed.
95 Lacking may edges in callgraph we just preserve them post
96 inlining. */
97 && (!DECL_VIRTUAL_P (node->symbol.decl)
98 || (!DECL_COMDAT (node->symbol.decl)
99 && !DECL_EXTERNAL (node->symbol.decl)))
100 /* During early inlining some unanalyzed cgraph nodes might be in the
101 callgraph and they might reffer the function in question. */
102 && !cgraph_new_nodes);
105 /* We are going to eliminate last direct call to NODE (or alias of it) via edge E.
106 Verify that the NODE can be removed from unit and if it is contained in comdat
107 group that the whole comdat group is removable. */
109 static bool
110 can_remove_node_now_p (struct cgraph_node *node, struct cgraph_edge *e)
112 struct cgraph_node *next;
113 if (!can_remove_node_now_p_1 (node))
114 return false;
116 /* When we see same comdat group, we need to be sure that all
117 items can be removed. */
118 if (!node->symbol.same_comdat_group)
119 return true;
120 for (next = cgraph (node->symbol.same_comdat_group);
121 next != node; next = cgraph (next->symbol.same_comdat_group))
122 if ((next->callers && next->callers != e)
123 || !can_remove_node_now_p_1 (next))
124 return false;
125 return true;
129 /* E is expected to be an edge being inlined. Clone destination node of
130 the edge and redirect it to the new clone.
131 DUPLICATE is used for bookkeeping on whether we are actually creating new
132 clones or re-using node originally representing out-of-line function call.
135 void
136 clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
137 bool update_original, int *overall_size)
139 if (duplicate)
141 /* We may eliminate the need for out-of-line copy to be output.
142 In that case just go ahead and re-use it. This is not just an
143 memory optimization. Making offline copy of fuction disappear
144 from the program will improve future decisions on inlining. */
145 if (!e->callee->callers->next_caller
146 /* Recursive inlining never wants the master clone to
147 be overwritten. */
148 && update_original
149 && can_remove_node_now_p (e->callee, e))
151 /* TODO: When callee is in a comdat group, we could remove all of it,
152 including all inline clones inlined into it. That would however
153 need small function inlining to register edge removal hook to
154 maintain the priority queue.
156 For now we keep the ohter functions in the group in program until
157 cgraph_remove_unreachable_functions gets rid of them. */
158 gcc_assert (!e->callee->global.inlined_to);
159 symtab_dissolve_same_comdat_group_list ((symtab_node) e->callee);
160 if (e->callee->analyzed && !DECL_EXTERNAL (e->callee->symbol.decl))
162 if (overall_size)
163 *overall_size -= inline_summary (e->callee)->size;
164 nfunctions_inlined++;
166 duplicate = false;
167 e->callee->symbol.externally_visible = false;
168 update_noncloned_frequencies (e->callee, e->frequency);
170 else
172 struct cgraph_node *n;
173 n = cgraph_clone_node (e->callee, e->callee->symbol.decl,
174 e->count, e->frequency,
175 update_original, NULL, true);
176 cgraph_redirect_edge_callee (e, n);
179 else
180 symtab_dissolve_same_comdat_group_list ((symtab_node) e->callee);
182 if (e->caller->global.inlined_to)
183 e->callee->global.inlined_to = e->caller->global.inlined_to;
184 else
185 e->callee->global.inlined_to = e->caller;
187 /* Recursively clone all bodies. */
188 for (e = e->callee->callees; e; e = e->next_callee)
189 if (!e->inline_failed)
190 clone_inlined_nodes (e, duplicate, update_original, overall_size);
194 /* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL
195 specify whether profile of original function should be updated. If any new
196 indirect edges are discovered in the process, add them to NEW_EDGES, unless
197 it is NULL. Return true iff any new callgraph edges were discovered as a
198 result of inlining. */
200 bool
201 inline_call (struct cgraph_edge *e, bool update_original,
202 VEC (cgraph_edge_p, heap) **new_edges,
203 int *overall_size)
205 int old_size = 0, new_size = 0;
206 struct cgraph_node *to = NULL;
207 struct cgraph_edge *curr = e;
208 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
210 /* Don't inline inlined edges. */
211 gcc_assert (e->inline_failed);
212 /* Don't even think of inlining inline clone. */
213 gcc_assert (!callee->global.inlined_to);
215 e->inline_failed = CIF_OK;
216 DECL_POSSIBLY_INLINED (callee->symbol.decl) = true;
218 to = e->caller;
219 if (to->global.inlined_to)
220 to = to->global.inlined_to;
222 /* If aliases are involved, redirect edge to the actual destination and
223 possibly remove the aliases. */
224 if (e->callee != callee)
226 struct cgraph_node *alias = e->callee, *next_alias;
227 cgraph_redirect_edge_callee (e, callee);
228 while (alias && alias != callee)
230 if (!alias->callers
231 && can_remove_node_now_p (alias, e))
233 next_alias = cgraph_alias_aliased_node (alias);
234 cgraph_remove_node (alias);
235 alias = next_alias;
237 else
238 break;
242 clone_inlined_nodes (e, true, update_original, overall_size);
244 gcc_assert (curr->callee->global.inlined_to == to);
246 old_size = inline_summary (to)->size;
247 inline_merge_summary (e);
248 new_size = inline_summary (to)->size;
249 if (overall_size)
250 *overall_size += new_size - old_size;
251 ncalls_inlined++;
253 /* This must happen after inline_merge_summary that rely on jump
254 functions of callee to not be updated. */
255 if (optimize)
256 return ipa_propagate_indirect_call_infos (curr, new_edges);
257 else
258 return false;
262 /* Copy function body of NODE and redirect all inline clones to it.
263 This is done before inline plan is applied to NODE when there are
264 still some inline clones if it.
266 This is necessary because inline decisions are not really transitive
267 and the other inline clones may have different bodies. */
269 static struct cgraph_node *
270 save_inline_function_body (struct cgraph_node *node)
272 struct cgraph_node *first_clone, *n;
274 if (dump_file)
275 fprintf (dump_file, "\nSaving body of %s for later reuse\n",
276 cgraph_node_name (node));
278 gcc_assert (node == cgraph_get_node (node->symbol.decl));
280 /* first_clone will be turned into real function. */
281 first_clone = node->clones;
282 first_clone->symbol.decl = copy_node (node->symbol.decl);
283 symtab_insert_node_to_hashtable ((symtab_node) first_clone);
284 gcc_assert (first_clone == cgraph_get_node (first_clone->symbol.decl));
286 /* Now reshape the clone tree, so all other clones descends from
287 first_clone. */
288 if (first_clone->next_sibling_clone)
290 for (n = first_clone->next_sibling_clone; n->next_sibling_clone; n = n->next_sibling_clone)
291 n->clone_of = first_clone;
292 n->clone_of = first_clone;
293 n->next_sibling_clone = first_clone->clones;
294 if (first_clone->clones)
295 first_clone->clones->prev_sibling_clone = n;
296 first_clone->clones = first_clone->next_sibling_clone;
297 first_clone->next_sibling_clone->prev_sibling_clone = NULL;
298 first_clone->next_sibling_clone = NULL;
299 gcc_assert (!first_clone->prev_sibling_clone);
301 first_clone->clone_of = NULL;
303 /* Now node in question has no clones. */
304 node->clones = NULL;
306 /* Inline clones share decl with the function they are cloned
307 from. Walk the whole clone tree and redirect them all to the
308 new decl. */
309 if (first_clone->clones)
310 for (n = first_clone->clones; n != first_clone;)
312 gcc_assert (n->symbol.decl == node->symbol.decl);
313 n->symbol.decl = first_clone->symbol.decl;
314 if (n->clones)
315 n = n->clones;
316 else if (n->next_sibling_clone)
317 n = n->next_sibling_clone;
318 else
320 while (n != first_clone && !n->next_sibling_clone)
321 n = n->clone_of;
322 if (n != first_clone)
323 n = n->next_sibling_clone;
327 /* Copy the OLD_VERSION_NODE function tree to the new version. */
328 tree_function_versioning (node->symbol.decl, first_clone->symbol.decl,
329 NULL, true, NULL, false, NULL, NULL);
331 /* The function will be short lived and removed after we inline all the clones,
332 but make it internal so we won't confuse ourself. */
333 DECL_EXTERNAL (first_clone->symbol.decl) = 0;
334 DECL_COMDAT_GROUP (first_clone->symbol.decl) = NULL_TREE;
335 TREE_PUBLIC (first_clone->symbol.decl) = 0;
336 DECL_COMDAT (first_clone->symbol.decl) = 0;
337 VEC_free (ipa_opt_pass, heap,
338 first_clone->ipa_transforms_to_apply);
339 first_clone->ipa_transforms_to_apply = NULL;
341 /* When doing recursive inlining, the clone may become unnecessary.
342 This is possible i.e. in the case when the recursive function is proved to be
343 non-throwing and the recursion happens only in the EH landing pad.
344 We can not remove the clone until we are done with saving the body.
345 Remove it now. */
346 if (!first_clone->callers)
348 cgraph_remove_node_and_inline_clones (first_clone, NULL);
349 first_clone = NULL;
351 #ifdef ENABLE_CHECKING
352 else
353 verify_cgraph_node (first_clone);
354 #endif
355 return first_clone;
358 /* Return true when function body of DECL still needs to be kept around
359 for later re-use. */
360 static bool
361 preserve_function_body_p (struct cgraph_node *node)
363 gcc_assert (cgraph_global_info_ready);
364 gcc_assert (!node->alias && !node->thunk.thunk_p);
366 /* Look if there is any clone around. */
367 if (node->clones)
368 return true;
369 return false;
372 /* Apply inline plan to function. */
374 unsigned int
375 inline_transform (struct cgraph_node *node)
377 unsigned int todo = 0;
378 struct cgraph_edge *e;
380 /* FIXME: Currently the pass manager is adding inline transform more than
381 once to some clones. This needs revisiting after WPA cleanups. */
382 if (cfun->after_inlining)
383 return 0;
385 /* We might need the body of this function so that we can expand
386 it inline somewhere else. */
387 if (preserve_function_body_p (node))
388 save_inline_function_body (node);
390 for (e = node->callees; e; e = e->next_callee)
391 cgraph_redirect_edge_call_stmt_to_callee (e);
393 timevar_push (TV_INTEGRATION);
394 if (node->callees)
395 todo = optimize_inline_calls (current_function_decl);
396 timevar_pop (TV_INTEGRATION);
398 cfun->always_inline_functions_inlined = true;
399 cfun->after_inlining = true;
400 todo |= execute_fixup_cfg ();
402 if (!(todo & TODO_update_ssa_any))
403 /* Redirecting edges might lead to a need for vops to be recomputed. */
404 todo |= TODO_update_ssa_only_virtuals;
406 return todo;