Optimize powerpc*-*-linux* e500 hardfp/soft-fp use.
[official-gcc.git] / gcc / ipa-inline-transform.c
blobdbc56c599b0a4551832938253c12e4903c4f03f0
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
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 "intl.h"
38 #include "coverage.h"
39 #include "ggc.h"
40 #include "tree-cfg.h"
41 #include "vec.h"
42 #include "hash-map.h"
43 #include "is-a.h"
44 #include "plugin-api.h"
45 #include "hashtab.h"
46 #include "hash-set.h"
47 #include "machmode.h"
48 #include "hard-reg-set.h"
49 #include "input.h"
50 #include "function.h"
51 #include "ipa-ref.h"
52 #include "cgraph.h"
53 #include "alloc-pool.h"
54 #include "ipa-prop.h"
55 #include "ipa-inline.h"
56 #include "tree-inline.h"
57 #include "tree-pass.h"
59 int ncalls_inlined;
60 int nfunctions_inlined;
61 bool speculation_removed;
63 /* Scale frequency of NODE edges by FREQ_SCALE. */
65 static void
66 update_noncloned_frequencies (struct cgraph_node *node,
67 int freq_scale)
69 struct cgraph_edge *e;
71 /* We do not want to ignore high loop nest after freq drops to 0. */
72 if (!freq_scale)
73 freq_scale = 1;
74 for (e = node->callees; e; e = e->next_callee)
76 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
77 if (e->frequency > CGRAPH_FREQ_MAX)
78 e->frequency = CGRAPH_FREQ_MAX;
79 if (!e->inline_failed)
80 update_noncloned_frequencies (e->callee, freq_scale);
82 for (e = node->indirect_calls; e; e = e->next_callee)
84 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
85 if (e->frequency > CGRAPH_FREQ_MAX)
86 e->frequency = CGRAPH_FREQ_MAX;
90 /* We removed or are going to remove the last call to NODE.
91 Return true if we can and want proactively remove the NODE now.
92 This is important to do, since we want inliner to know when offline
93 copy of function was removed. */
95 static bool
96 can_remove_node_now_p_1 (struct cgraph_node *node)
98 /* FIXME: When address is taken of DECL_EXTERNAL function we still
99 can remove its offline copy, but we would need to keep unanalyzed node in
100 the callgraph so references can point to it. */
101 return (!node->address_taken
102 && !node->has_aliases_p ()
103 && !node->used_as_abstract_origin
104 && node->can_remove_if_no_direct_calls_p ()
105 /* Inlining might enable more devirtualizing, so we want to remove
106 those only after all devirtualizable virtual calls are processed.
107 Lacking may edges in callgraph we just preserve them post
108 inlining. */
109 && !DECL_VIRTUAL_P (node->decl)
110 /* During early inlining some unanalyzed cgraph nodes might be in the
111 callgraph and they might reffer the function in question. */
112 && !cgraph_new_nodes.exists ());
115 /* We are going to eliminate last direct call to NODE (or alias of it) via edge E.
116 Verify that the NODE can be removed from unit and if it is contained in comdat
117 group that the whole comdat group is removable. */
119 static bool
120 can_remove_node_now_p (struct cgraph_node *node, struct cgraph_edge *e)
122 struct cgraph_node *next;
123 if (!can_remove_node_now_p_1 (node))
124 return false;
126 /* When we see same comdat group, we need to be sure that all
127 items can be removed. */
128 if (!node->same_comdat_group)
129 return true;
130 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
131 next != node; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
132 if ((next->callers && next->callers != e)
133 || !can_remove_node_now_p_1 (next))
134 return false;
135 return true;
138 /* Return true if NODE is a master clone with non-inline clones. */
140 static bool
141 master_clone_with_noninline_clones_p (struct cgraph_node *node)
143 if (node->clone_of)
144 return false;
146 for (struct cgraph_node *n = node->clones; n; n = n->next_sibling_clone)
147 if (n->decl != node->decl)
148 return true;
150 return false;
153 /* E is expected to be an edge being inlined. Clone destination node of
154 the edge and redirect it to the new clone.
155 DUPLICATE is used for bookkeeping on whether we are actually creating new
156 clones or re-using node originally representing out-of-line function call.
157 By default the offline copy is removed, when it appears dead after inlining.
158 UPDATE_ORIGINAL prevents this transformation.
159 If OVERALL_SIZE is non-NULL, the size is updated to reflect the
160 transformation.
161 FREQ_SCALE specify the scaling of frequencies of call sites. */
163 void
164 clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
165 bool update_original, int *overall_size, int freq_scale)
167 struct cgraph_node *inlining_into;
168 struct cgraph_edge *next;
170 if (e->caller->global.inlined_to)
171 inlining_into = e->caller->global.inlined_to;
172 else
173 inlining_into = e->caller;
175 if (duplicate)
177 /* We may eliminate the need for out-of-line copy to be output.
178 In that case just go ahead and re-use it. This is not just an
179 memory optimization. Making offline copy of fuction disappear
180 from the program will improve future decisions on inlining. */
181 if (!e->callee->callers->next_caller
182 /* Recursive inlining never wants the master clone to
183 be overwritten. */
184 && update_original
185 && can_remove_node_now_p (e->callee, e)
186 /* We cannot overwrite a master clone with non-inline clones
187 until after these clones are materialized. */
188 && !master_clone_with_noninline_clones_p (e->callee))
190 /* TODO: When callee is in a comdat group, we could remove all of it,
191 including all inline clones inlined into it. That would however
192 need small function inlining to register edge removal hook to
193 maintain the priority queue.
195 For now we keep the ohter functions in the group in program until
196 cgraph_remove_unreachable_functions gets rid of them. */
197 gcc_assert (!e->callee->global.inlined_to);
198 e->callee->dissolve_same_comdat_group_list ();
199 if (e->callee->definition && !DECL_EXTERNAL (e->callee->decl))
201 if (overall_size)
202 *overall_size -= inline_summary (e->callee)->size;
203 nfunctions_inlined++;
205 duplicate = false;
206 e->callee->externally_visible = false;
207 update_noncloned_frequencies (e->callee, e->frequency);
209 else
211 struct cgraph_node *n;
213 if (freq_scale == -1)
214 freq_scale = e->frequency;
215 n = e->callee->create_clone (e->callee->decl,
216 MIN (e->count, e->callee->count),
217 freq_scale,
218 update_original, vNULL, true,
219 inlining_into,
220 NULL);
221 e->redirect_callee (n);
224 else
225 e->callee->dissolve_same_comdat_group_list ();
227 e->callee->global.inlined_to = inlining_into;
229 /* Recursively clone all bodies. */
230 for (e = e->callee->callees; e; e = next)
232 next = e->next_callee;
233 if (!e->inline_failed)
234 clone_inlined_nodes (e, duplicate, update_original, overall_size, freq_scale);
235 if (e->speculative && !speculation_useful_p (e, true))
237 e->resolve_speculation (NULL);
238 speculation_removed = true;
244 /* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL
245 specify whether profile of original function should be updated. If any new
246 indirect edges are discovered in the process, add them to NEW_EDGES, unless
247 it is NULL. If UPDATE_OVERALL_SUMMARY is false, do not bother to recompute overall
248 size of caller after inlining. Caller is required to eventually do it via
249 inline_update_overall_summary.
250 If callee_removed is non-NULL, set it to true if we removed callee node.
252 Return true iff any new callgraph edges were discovered as a
253 result of inlining. */
255 bool
256 inline_call (struct cgraph_edge *e, bool update_original,
257 vec<cgraph_edge *> *new_edges,
258 int *overall_size, bool update_overall_summary,
259 bool *callee_removed)
261 int old_size = 0, new_size = 0;
262 struct cgraph_node *to = NULL;
263 struct cgraph_edge *curr = e;
264 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
265 bool new_edges_found = false;
267 #ifdef ENABLE_CHECKING
268 int estimated_growth = estimate_edge_growth (e);
269 bool predicated = inline_edge_summary (e)->predicate != NULL;
270 #endif
272 speculation_removed = false;
273 /* Don't inline inlined edges. */
274 gcc_assert (e->inline_failed);
275 /* Don't even think of inlining inline clone. */
276 gcc_assert (!callee->global.inlined_to);
278 e->inline_failed = CIF_OK;
279 DECL_POSSIBLY_INLINED (callee->decl) = true;
281 to = e->caller;
282 if (to->global.inlined_to)
283 to = to->global.inlined_to;
285 /* If aliases are involved, redirect edge to the actual destination and
286 possibly remove the aliases. */
287 if (e->callee != callee)
289 struct cgraph_node *alias = e->callee, *next_alias;
290 e->redirect_callee (callee);
291 while (alias && alias != callee)
293 if (!alias->callers
294 && can_remove_node_now_p (alias, e))
296 next_alias = alias->get_alias_target ();
297 alias->remove ();
298 if (callee_removed)
299 *callee_removed = true;
300 alias = next_alias;
302 else
303 break;
307 clone_inlined_nodes (e, true, update_original, overall_size, e->frequency);
309 gcc_assert (curr->callee->global.inlined_to == to);
311 old_size = inline_summary (to)->size;
312 inline_merge_summary (e);
313 if (optimize)
314 new_edges_found = ipa_propagate_indirect_call_infos (curr, new_edges);
315 if (update_overall_summary)
316 inline_update_overall_summary (to);
317 new_size = inline_summary (to)->size;
319 if (callee->calls_comdat_local)
320 to->calls_comdat_local = true;
321 else if (to->calls_comdat_local && callee->comdat_local_p ())
323 struct cgraph_edge *se = to->callees;
324 for (; se; se = se->next_callee)
325 if (se->inline_failed && se->callee->comdat_local_p ())
326 break;
327 if (se == NULL)
328 to->calls_comdat_local = false;
331 #ifdef ENABLE_CHECKING
332 /* Verify that estimated growth match real growth. Allow off-by-one
333 error due to INLINE_SIZE_SCALE roudoff errors. */
334 gcc_assert (!update_overall_summary || !overall_size || new_edges_found
335 || abs (estimated_growth - (new_size - old_size)) <= 1
336 || speculation_removed
337 /* FIXME: a hack. Edges with false predicate are accounted
338 wrong, we should remove them from callgraph. */
339 || predicated);
340 #endif
342 /* Account the change of overall unit size; external functions will be
343 removed and are thus not accounted. */
344 if (overall_size
345 && !DECL_EXTERNAL (to->decl))
346 *overall_size += new_size - old_size;
347 ncalls_inlined++;
349 /* This must happen after inline_merge_summary that rely on jump
350 functions of callee to not be updated. */
351 return new_edges_found;
355 /* Copy function body of NODE and redirect all inline clones to it.
356 This is done before inline plan is applied to NODE when there are
357 still some inline clones if it.
359 This is necessary because inline decisions are not really transitive
360 and the other inline clones may have different bodies. */
362 static struct cgraph_node *
363 save_inline_function_body (struct cgraph_node *node)
365 struct cgraph_node *first_clone, *n;
367 if (dump_file)
368 fprintf (dump_file, "\nSaving body of %s for later reuse\n",
369 node->name ());
371 gcc_assert (node == cgraph_node::get (node->decl));
373 /* first_clone will be turned into real function. */
374 first_clone = node->clones;
375 first_clone->decl = copy_node (node->decl);
376 first_clone->decl->decl_with_vis.symtab_node = first_clone;
377 gcc_assert (first_clone == cgraph_node::get (first_clone->decl));
379 /* Now reshape the clone tree, so all other clones descends from
380 first_clone. */
381 if (first_clone->next_sibling_clone)
383 for (n = first_clone->next_sibling_clone; n->next_sibling_clone; n = n->next_sibling_clone)
384 n->clone_of = first_clone;
385 n->clone_of = first_clone;
386 n->next_sibling_clone = first_clone->clones;
387 if (first_clone->clones)
388 first_clone->clones->prev_sibling_clone = n;
389 first_clone->clones = first_clone->next_sibling_clone;
390 first_clone->next_sibling_clone->prev_sibling_clone = NULL;
391 first_clone->next_sibling_clone = NULL;
392 gcc_assert (!first_clone->prev_sibling_clone);
394 first_clone->clone_of = NULL;
396 /* Now node in question has no clones. */
397 node->clones = NULL;
399 /* Inline clones share decl with the function they are cloned
400 from. Walk the whole clone tree and redirect them all to the
401 new decl. */
402 if (first_clone->clones)
403 for (n = first_clone->clones; n != first_clone;)
405 gcc_assert (n->decl == node->decl);
406 n->decl = first_clone->decl;
407 if (n->clones)
408 n = n->clones;
409 else if (n->next_sibling_clone)
410 n = n->next_sibling_clone;
411 else
413 while (n != first_clone && !n->next_sibling_clone)
414 n = n->clone_of;
415 if (n != first_clone)
416 n = n->next_sibling_clone;
420 /* Copy the OLD_VERSION_NODE function tree to the new version. */
421 tree_function_versioning (node->decl, first_clone->decl,
422 NULL, true, NULL, false,
423 NULL, NULL);
425 /* The function will be short lived and removed after we inline all the clones,
426 but make it internal so we won't confuse ourself. */
427 DECL_EXTERNAL (first_clone->decl) = 0;
428 TREE_PUBLIC (first_clone->decl) = 0;
429 DECL_COMDAT (first_clone->decl) = 0;
430 first_clone->ipa_transforms_to_apply.release ();
432 /* When doing recursive inlining, the clone may become unnecessary.
433 This is possible i.e. in the case when the recursive function is proved to be
434 non-throwing and the recursion happens only in the EH landing pad.
435 We can not remove the clone until we are done with saving the body.
436 Remove it now. */
437 if (!first_clone->callers)
439 first_clone->remove_symbol_and_inline_clones ();
440 first_clone = NULL;
442 #ifdef ENABLE_CHECKING
443 else
444 first_clone->verify ();
445 #endif
446 return first_clone;
449 /* Return true when function body of DECL still needs to be kept around
450 for later re-use. */
451 static bool
452 preserve_function_body_p (struct cgraph_node *node)
454 gcc_assert (symtab->global_info_ready);
455 gcc_assert (!node->alias && !node->thunk.thunk_p);
457 /* Look if there is any clone around. */
458 if (node->clones)
459 return true;
460 return false;
463 /* Apply inline plan to function. */
465 unsigned int
466 inline_transform (struct cgraph_node *node)
468 unsigned int todo = 0;
469 struct cgraph_edge *e, *next;
471 /* FIXME: Currently the pass manager is adding inline transform more than
472 once to some clones. This needs revisiting after WPA cleanups. */
473 if (cfun->after_inlining)
474 return 0;
476 /* We might need the body of this function so that we can expand
477 it inline somewhere else. */
478 if (preserve_function_body_p (node))
479 save_inline_function_body (node);
481 for (e = node->callees; e; e = next)
483 next = e->next_callee;
484 e->redirect_call_stmt_to_callee ();
486 node->remove_all_references ();
488 timevar_push (TV_INTEGRATION);
489 if (node->callees && optimize)
490 todo = optimize_inline_calls (current_function_decl);
491 timevar_pop (TV_INTEGRATION);
493 cfun->always_inline_functions_inlined = true;
494 cfun->after_inlining = true;
495 todo |= execute_fixup_cfg ();
497 if (!(todo & TODO_update_ssa_any))
498 /* Redirecting edges might lead to a need for vops to be recomputed. */
499 todo |= TODO_update_ssa_only_virtuals;
501 return todo;