PR preprocessor/63831
[official-gcc.git] / gcc / ipa-inline-transform.c
blob063cd94cda67ac6e1a63e7332a8b0aa363249e28
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, struct cgraph_edge *e)
98 ipa_ref *ref;
100 FOR_EACH_ALIAS (node, ref)
102 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
103 if ((alias->callers && alias->callers != e)
104 || !can_remove_node_now_p_1 (alias, e))
105 return false;
107 /* FIXME: When address is taken of DECL_EXTERNAL function we still
108 can remove its offline copy, but we would need to keep unanalyzed node in
109 the callgraph so references can point to it. */
110 return (!node->address_taken
111 && node->can_remove_if_no_direct_calls_p ()
112 /* Inlining might enable more devirtualizing, so we want to remove
113 those only after all devirtualizable virtual calls are processed.
114 Lacking may edges in callgraph we just preserve them post
115 inlining. */
116 && (!DECL_VIRTUAL_P (node->decl)
117 || !opt_for_fn (node->decl, flag_devirtualize))
118 /* During early inlining some unanalyzed cgraph nodes might be in the
119 callgraph and they might reffer the function in question. */
120 && !cgraph_new_nodes.exists ());
123 /* We are going to eliminate last direct call to NODE (or alias of it) via edge E.
124 Verify that the NODE can be removed from unit and if it is contained in comdat
125 group that the whole comdat group is removable. */
127 static bool
128 can_remove_node_now_p (struct cgraph_node *node, struct cgraph_edge *e)
130 struct cgraph_node *next;
131 if (!can_remove_node_now_p_1 (node, e))
132 return false;
134 /* When we see same comdat group, we need to be sure that all
135 items can be removed. */
136 if (!node->same_comdat_group)
137 return true;
138 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
139 next != node; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
141 if (next->alias)
142 continue;
143 if ((next->callers && next->callers != e)
144 || !can_remove_node_now_p_1 (next, e))
145 return false;
147 return true;
150 /* Return true if NODE is a master clone with non-inline clones. */
152 static bool
153 master_clone_with_noninline_clones_p (struct cgraph_node *node)
155 if (node->clone_of)
156 return false;
158 for (struct cgraph_node *n = node->clones; n; n = n->next_sibling_clone)
159 if (n->decl != node->decl)
160 return true;
162 return false;
165 /* E is expected to be an edge being inlined. Clone destination node of
166 the edge and redirect it to the new clone.
167 DUPLICATE is used for bookkeeping on whether we are actually creating new
168 clones or re-using node originally representing out-of-line function call.
169 By default the offline copy is removed, when it appears dead after inlining.
170 UPDATE_ORIGINAL prevents this transformation.
171 If OVERALL_SIZE is non-NULL, the size is updated to reflect the
172 transformation.
173 FREQ_SCALE specify the scaling of frequencies of call sites. */
175 void
176 clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
177 bool update_original, int *overall_size, int freq_scale)
179 struct cgraph_node *inlining_into;
180 struct cgraph_edge *next;
182 if (e->caller->global.inlined_to)
183 inlining_into = e->caller->global.inlined_to;
184 else
185 inlining_into = e->caller;
187 if (duplicate)
189 /* We may eliminate the need for out-of-line copy to be output.
190 In that case just go ahead and re-use it. This is not just an
191 memory optimization. Making offline copy of fuction disappear
192 from the program will improve future decisions on inlining. */
193 if (!e->callee->callers->next_caller
194 /* Recursive inlining never wants the master clone to
195 be overwritten. */
196 && update_original
197 && can_remove_node_now_p (e->callee, e)
198 /* We cannot overwrite a master clone with non-inline clones
199 until after these clones are materialized. */
200 && !master_clone_with_noninline_clones_p (e->callee))
202 /* TODO: When callee is in a comdat group, we could remove all of it,
203 including all inline clones inlined into it. That would however
204 need small function inlining to register edge removal hook to
205 maintain the priority queue.
207 For now we keep the ohter functions in the group in program until
208 cgraph_remove_unreachable_functions gets rid of them. */
209 gcc_assert (!e->callee->global.inlined_to);
210 e->callee->dissolve_same_comdat_group_list ();
211 if (e->callee->definition && !DECL_EXTERNAL (e->callee->decl))
213 if (overall_size)
214 *overall_size -= inline_summary (e->callee)->size;
215 nfunctions_inlined++;
217 duplicate = false;
218 e->callee->externally_visible = false;
219 update_noncloned_frequencies (e->callee, e->frequency);
221 else
223 struct cgraph_node *n;
225 if (freq_scale == -1)
226 freq_scale = e->frequency;
227 n = e->callee->create_clone (e->callee->decl,
228 MIN (e->count, e->callee->count),
229 freq_scale,
230 update_original, vNULL, true,
231 inlining_into,
232 NULL);
233 n->used_as_abstract_origin = e->callee->used_as_abstract_origin;
234 e->redirect_callee (n);
237 else
238 e->callee->dissolve_same_comdat_group_list ();
240 e->callee->global.inlined_to = inlining_into;
242 /* Recursively clone all bodies. */
243 for (e = e->callee->callees; e; e = next)
245 next = e->next_callee;
246 if (!e->inline_failed)
247 clone_inlined_nodes (e, duplicate, update_original, overall_size, freq_scale);
248 if (e->speculative && !speculation_useful_p (e, true))
250 e->resolve_speculation (NULL);
251 speculation_removed = true;
257 /* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL
258 specify whether profile of original function should be updated. If any new
259 indirect edges are discovered in the process, add them to NEW_EDGES, unless
260 it is NULL. If UPDATE_OVERALL_SUMMARY is false, do not bother to recompute overall
261 size of caller after inlining. Caller is required to eventually do it via
262 inline_update_overall_summary.
263 If callee_removed is non-NULL, set it to true if we removed callee node.
265 Return true iff any new callgraph edges were discovered as a
266 result of inlining. */
268 bool
269 inline_call (struct cgraph_edge *e, bool update_original,
270 vec<cgraph_edge *> *new_edges,
271 int *overall_size, bool update_overall_summary,
272 bool *callee_removed)
274 int old_size = 0, new_size = 0;
275 struct cgraph_node *to = NULL;
276 struct cgraph_edge *curr = e;
277 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
278 bool new_edges_found = false;
280 #ifdef ENABLE_CHECKING
281 int estimated_growth = estimate_edge_growth (e);
282 bool predicated = inline_edge_summary (e)->predicate != NULL;
283 #endif
285 speculation_removed = false;
286 /* Don't inline inlined edges. */
287 gcc_assert (e->inline_failed);
288 /* Don't even think of inlining inline clone. */
289 gcc_assert (!callee->global.inlined_to);
291 e->inline_failed = CIF_OK;
292 DECL_POSSIBLY_INLINED (callee->decl) = true;
294 to = e->caller;
295 if (to->global.inlined_to)
296 to = to->global.inlined_to;
298 /* If aliases are involved, redirect edge to the actual destination and
299 possibly remove the aliases. */
300 if (e->callee != callee)
302 struct cgraph_node *alias = e->callee, *next_alias;
303 e->redirect_callee (callee);
304 while (alias && alias != callee)
306 if (!alias->callers
307 && can_remove_node_now_p (alias, e))
309 next_alias = alias->get_alias_target ();
310 alias->remove ();
311 if (callee_removed)
312 *callee_removed = true;
313 alias = next_alias;
315 else
316 break;
320 clone_inlined_nodes (e, true, update_original, overall_size, e->frequency);
322 gcc_assert (curr->callee->global.inlined_to == to);
324 old_size = inline_summary (to)->size;
325 inline_merge_summary (e);
326 if (optimize)
327 new_edges_found = ipa_propagate_indirect_call_infos (curr, new_edges);
328 if (update_overall_summary)
329 inline_update_overall_summary (to);
330 new_size = inline_summary (to)->size;
332 if (callee->calls_comdat_local)
333 to->calls_comdat_local = true;
334 else if (to->calls_comdat_local && callee->comdat_local_p ())
336 struct cgraph_edge *se = to->callees;
337 for (; se; se = se->next_callee)
338 if (se->inline_failed && se->callee->comdat_local_p ())
339 break;
340 if (se == NULL)
341 to->calls_comdat_local = false;
344 #ifdef ENABLE_CHECKING
345 /* Verify that estimated growth match real growth. Allow off-by-one
346 error due to INLINE_SIZE_SCALE roudoff errors. */
347 gcc_assert (!update_overall_summary || !overall_size || new_edges_found
348 || abs (estimated_growth - (new_size - old_size)) <= 1
349 || speculation_removed
350 /* FIXME: a hack. Edges with false predicate are accounted
351 wrong, we should remove them from callgraph. */
352 || predicated);
353 #endif
355 /* Account the change of overall unit size; external functions will be
356 removed and are thus not accounted. */
357 if (overall_size
358 && !DECL_EXTERNAL (to->decl))
359 *overall_size += new_size - old_size;
360 ncalls_inlined++;
362 /* This must happen after inline_merge_summary that rely on jump
363 functions of callee to not be updated. */
364 return new_edges_found;
368 /* Copy function body of NODE and redirect all inline clones to it.
369 This is done before inline plan is applied to NODE when there are
370 still some inline clones if it.
372 This is necessary because inline decisions are not really transitive
373 and the other inline clones may have different bodies. */
375 static struct cgraph_node *
376 save_inline_function_body (struct cgraph_node *node)
378 struct cgraph_node *first_clone, *n;
380 if (dump_file)
381 fprintf (dump_file, "\nSaving body of %s for later reuse\n",
382 node->name ());
384 gcc_assert (node == cgraph_node::get (node->decl));
386 /* first_clone will be turned into real function. */
387 first_clone = node->clones;
388 first_clone->decl = copy_node (node->decl);
389 first_clone->decl->decl_with_vis.symtab_node = first_clone;
390 gcc_assert (first_clone == cgraph_node::get (first_clone->decl));
392 /* Now reshape the clone tree, so all other clones descends from
393 first_clone. */
394 if (first_clone->next_sibling_clone)
396 for (n = first_clone->next_sibling_clone; n->next_sibling_clone; n = n->next_sibling_clone)
397 n->clone_of = first_clone;
398 n->clone_of = first_clone;
399 n->next_sibling_clone = first_clone->clones;
400 if (first_clone->clones)
401 first_clone->clones->prev_sibling_clone = n;
402 first_clone->clones = first_clone->next_sibling_clone;
403 first_clone->next_sibling_clone->prev_sibling_clone = NULL;
404 first_clone->next_sibling_clone = NULL;
405 gcc_assert (!first_clone->prev_sibling_clone);
407 first_clone->clone_of = NULL;
409 /* Now node in question has no clones. */
410 node->clones = NULL;
412 /* Inline clones share decl with the function they are cloned
413 from. Walk the whole clone tree and redirect them all to the
414 new decl. */
415 if (first_clone->clones)
416 for (n = first_clone->clones; n != first_clone;)
418 gcc_assert (n->decl == node->decl);
419 n->decl = first_clone->decl;
420 if (n->clones)
421 n = n->clones;
422 else if (n->next_sibling_clone)
423 n = n->next_sibling_clone;
424 else
426 while (n != first_clone && !n->next_sibling_clone)
427 n = n->clone_of;
428 if (n != first_clone)
429 n = n->next_sibling_clone;
433 /* Copy the OLD_VERSION_NODE function tree to the new version. */
434 tree_function_versioning (node->decl, first_clone->decl,
435 NULL, true, NULL, false,
436 NULL, NULL);
438 /* The function will be short lived and removed after we inline all the clones,
439 but make it internal so we won't confuse ourself. */
440 DECL_EXTERNAL (first_clone->decl) = 0;
441 TREE_PUBLIC (first_clone->decl) = 0;
442 DECL_COMDAT (first_clone->decl) = 0;
443 first_clone->ipa_transforms_to_apply.release ();
445 /* When doing recursive inlining, the clone may become unnecessary.
446 This is possible i.e. in the case when the recursive function is proved to be
447 non-throwing and the recursion happens only in the EH landing pad.
448 We can not remove the clone until we are done with saving the body.
449 Remove it now. */
450 if (!first_clone->callers)
452 first_clone->remove_symbol_and_inline_clones ();
453 first_clone = NULL;
455 #ifdef ENABLE_CHECKING
456 else
457 first_clone->verify ();
458 #endif
459 return first_clone;
462 /* Return true when function body of DECL still needs to be kept around
463 for later re-use. */
464 static bool
465 preserve_function_body_p (struct cgraph_node *node)
467 gcc_assert (symtab->global_info_ready);
468 gcc_assert (!node->alias && !node->thunk.thunk_p);
470 /* Look if there is any clone around. */
471 if (node->clones)
472 return true;
473 return false;
476 /* Apply inline plan to function. */
478 unsigned int
479 inline_transform (struct cgraph_node *node)
481 unsigned int todo = 0;
482 struct cgraph_edge *e, *next;
483 bool has_inline = false;
485 /* FIXME: Currently the pass manager is adding inline transform more than
486 once to some clones. This needs revisiting after WPA cleanups. */
487 if (cfun->after_inlining)
488 return 0;
490 /* We might need the body of this function so that we can expand
491 it inline somewhere else. */
492 if (preserve_function_body_p (node))
493 save_inline_function_body (node);
495 for (e = node->callees; e; e = next)
497 if (!e->inline_failed)
498 has_inline = true;
499 next = e->next_callee;
500 e->redirect_call_stmt_to_callee ();
502 node->remove_all_references ();
504 timevar_push (TV_INTEGRATION);
505 if (node->callees && (optimize || has_inline))
506 todo = optimize_inline_calls (current_function_decl);
507 timevar_pop (TV_INTEGRATION);
509 cfun->always_inline_functions_inlined = true;
510 cfun->after_inlining = true;
511 todo |= execute_fixup_cfg ();
513 if (!(todo & TODO_update_ssa_any))
514 /* Redirecting edges might lead to a need for vops to be recomputed. */
515 todo |= TODO_update_ssa_only_virtuals;
517 return todo;