* lib/ubsan-dg.exp (check_effective_target_fsanitize_undefined):
[official-gcc.git] / gcc / ipa-inline-transform.c
blobc521f642147ab5787056e3ee61d2c092f15e8703
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 "symbol-summary.h"
55 #include "ipa-prop.h"
56 #include "ipa-inline.h"
57 #include "tree-inline.h"
58 #include "tree-pass.h"
60 int ncalls_inlined;
61 int nfunctions_inlined;
62 bool speculation_removed;
64 /* Scale frequency of NODE edges by FREQ_SCALE. */
66 static void
67 update_noncloned_frequencies (struct cgraph_node *node,
68 int freq_scale)
70 struct cgraph_edge *e;
72 /* We do not want to ignore high loop nest after freq drops to 0. */
73 if (!freq_scale)
74 freq_scale = 1;
75 for (e = node->callees; e; e = e->next_callee)
77 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
78 if (e->frequency > CGRAPH_FREQ_MAX)
79 e->frequency = CGRAPH_FREQ_MAX;
80 if (!e->inline_failed)
81 update_noncloned_frequencies (e->callee, freq_scale);
83 for (e = node->indirect_calls; e; e = e->next_callee)
85 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
86 if (e->frequency > CGRAPH_FREQ_MAX)
87 e->frequency = CGRAPH_FREQ_MAX;
91 /* We removed or are going to remove the last call to NODE.
92 Return true if we can and want proactively remove the NODE now.
93 This is important to do, since we want inliner to know when offline
94 copy of function was removed. */
96 static bool
97 can_remove_node_now_p_1 (struct cgraph_node *node, struct cgraph_edge *e)
99 ipa_ref *ref;
101 FOR_EACH_ALIAS (node, ref)
103 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
104 if ((alias->callers && alias->callers != e)
105 || !can_remove_node_now_p_1 (alias, e))
106 return false;
108 /* FIXME: When address is taken of DECL_EXTERNAL function we still
109 can remove its offline copy, but we would need to keep unanalyzed node in
110 the callgraph so references can point to it. */
111 return (!node->address_taken
112 && node->can_remove_if_no_direct_calls_p ()
113 /* Inlining might enable more devirtualizing, so we want to remove
114 those only after all devirtualizable virtual calls are processed.
115 Lacking may edges in callgraph we just preserve them post
116 inlining. */
117 && (!DECL_VIRTUAL_P (node->decl)
118 || !opt_for_fn (node->decl, flag_devirtualize))
119 /* During early inlining some unanalyzed cgraph nodes might be in the
120 callgraph and they might reffer the function in question. */
121 && !cgraph_new_nodes.exists ());
124 /* We are going to eliminate last direct call to NODE (or alias of it) via edge E.
125 Verify that the NODE can be removed from unit and if it is contained in comdat
126 group that the whole comdat group is removable. */
128 static bool
129 can_remove_node_now_p (struct cgraph_node *node, struct cgraph_edge *e)
131 struct cgraph_node *next;
132 if (!can_remove_node_now_p_1 (node, e))
133 return false;
135 /* When we see same comdat group, we need to be sure that all
136 items can be removed. */
137 if (!node->same_comdat_group)
138 return true;
139 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
140 next != node; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
142 if (next->alias)
143 continue;
144 if ((next->callers && next->callers != e)
145 || !can_remove_node_now_p_1 (next, e))
146 return false;
148 return true;
151 /* Return true if NODE is a master clone with non-inline clones. */
153 static bool
154 master_clone_with_noninline_clones_p (struct cgraph_node *node)
156 if (node->clone_of)
157 return false;
159 for (struct cgraph_node *n = node->clones; n; n = n->next_sibling_clone)
160 if (n->decl != node->decl)
161 return true;
163 return false;
166 /* E is expected to be an edge being inlined. Clone destination node of
167 the edge and redirect it to the new clone.
168 DUPLICATE is used for bookkeeping on whether we are actually creating new
169 clones or re-using node originally representing out-of-line function call.
170 By default the offline copy is removed, when it appears dead after inlining.
171 UPDATE_ORIGINAL prevents this transformation.
172 If OVERALL_SIZE is non-NULL, the size is updated to reflect the
173 transformation.
174 FREQ_SCALE specify the scaling of frequencies of call sites. */
176 void
177 clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
178 bool update_original, int *overall_size, int freq_scale)
180 struct cgraph_node *inlining_into;
181 struct cgraph_edge *next;
183 if (e->caller->global.inlined_to)
184 inlining_into = e->caller->global.inlined_to;
185 else
186 inlining_into = e->caller;
188 if (duplicate)
190 /* We may eliminate the need for out-of-line copy to be output.
191 In that case just go ahead and re-use it. This is not just an
192 memory optimization. Making offline copy of fuction disappear
193 from the program will improve future decisions on inlining. */
194 if (!e->callee->callers->next_caller
195 /* Recursive inlining never wants the master clone to
196 be overwritten. */
197 && update_original
198 && can_remove_node_now_p (e->callee, e)
199 /* We cannot overwrite a master clone with non-inline clones
200 until after these clones are materialized. */
201 && !master_clone_with_noninline_clones_p (e->callee))
203 /* TODO: When callee is in a comdat group, we could remove all of it,
204 including all inline clones inlined into it. That would however
205 need small function inlining to register edge removal hook to
206 maintain the priority queue.
208 For now we keep the ohter functions in the group in program until
209 cgraph_remove_unreachable_functions gets rid of them. */
210 gcc_assert (!e->callee->global.inlined_to);
211 e->callee->dissolve_same_comdat_group_list ();
212 if (e->callee->definition && !DECL_EXTERNAL (e->callee->decl))
214 if (overall_size)
215 *overall_size -= inline_summaries->get (e->callee)->size;
216 nfunctions_inlined++;
218 duplicate = false;
219 e->callee->externally_visible = false;
220 update_noncloned_frequencies (e->callee, e->frequency);
222 else
224 struct cgraph_node *n;
226 if (freq_scale == -1)
227 freq_scale = e->frequency;
228 n = e->callee->create_clone (e->callee->decl,
229 MIN (e->count, e->callee->count),
230 freq_scale,
231 update_original, vNULL, true,
232 inlining_into,
233 NULL);
234 n->used_as_abstract_origin = e->callee->used_as_abstract_origin;
235 e->redirect_callee (n);
238 else
239 e->callee->dissolve_same_comdat_group_list ();
241 e->callee->global.inlined_to = inlining_into;
243 /* Recursively clone all bodies. */
244 for (e = e->callee->callees; e; e = next)
246 next = e->next_callee;
247 if (!e->inline_failed)
248 clone_inlined_nodes (e, duplicate, update_original, overall_size, freq_scale);
249 if (e->speculative && !speculation_useful_p (e, true))
251 e->resolve_speculation (NULL);
252 speculation_removed = true;
258 /* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL
259 specify whether profile of original function should be updated. If any new
260 indirect edges are discovered in the process, add them to NEW_EDGES, unless
261 it is NULL. If UPDATE_OVERALL_SUMMARY is false, do not bother to recompute overall
262 size of caller after inlining. Caller is required to eventually do it via
263 inline_update_overall_summary.
264 If callee_removed is non-NULL, set it to true if we removed callee node.
266 Return true iff any new callgraph edges were discovered as a
267 result of inlining. */
269 bool
270 inline_call (struct cgraph_edge *e, bool update_original,
271 vec<cgraph_edge *> *new_edges,
272 int *overall_size, bool update_overall_summary,
273 bool *callee_removed)
275 int old_size = 0, new_size = 0;
276 struct cgraph_node *to = NULL;
277 struct cgraph_edge *curr = e;
278 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
279 bool new_edges_found = false;
281 #ifdef ENABLE_CHECKING
282 int estimated_growth = estimate_edge_growth (e);
283 bool predicated = inline_edge_summary (e)->predicate != NULL;
284 #endif
286 speculation_removed = false;
287 /* Don't inline inlined edges. */
288 gcc_assert (e->inline_failed);
289 /* Don't even think of inlining inline clone. */
290 gcc_assert (!callee->global.inlined_to);
292 e->inline_failed = CIF_OK;
293 DECL_POSSIBLY_INLINED (callee->decl) = true;
295 to = e->caller;
296 if (to->global.inlined_to)
297 to = to->global.inlined_to;
299 /* If aliases are involved, redirect edge to the actual destination and
300 possibly remove the aliases. */
301 if (e->callee != callee)
303 struct cgraph_node *alias = e->callee, *next_alias;
304 e->redirect_callee (callee);
305 while (alias && alias != callee)
307 if (!alias->callers
308 && can_remove_node_now_p (alias, e))
310 next_alias = alias->get_alias_target ();
311 alias->remove ();
312 if (callee_removed)
313 *callee_removed = true;
314 alias = next_alias;
316 else
317 break;
321 clone_inlined_nodes (e, true, update_original, overall_size, e->frequency);
323 gcc_assert (curr->callee->global.inlined_to == to);
325 old_size = inline_summaries->get (to)->size;
326 inline_merge_summary (e);
327 if (optimize)
328 new_edges_found = ipa_propagate_indirect_call_infos (curr, new_edges);
329 if (update_overall_summary)
330 inline_update_overall_summary (to);
331 new_size = inline_summaries->get (to)->size;
333 if (callee->calls_comdat_local)
334 to->calls_comdat_local = true;
335 else if (to->calls_comdat_local && callee->comdat_local_p ())
337 struct cgraph_edge *se = to->callees;
338 for (; se; se = se->next_callee)
339 if (se->inline_failed && se->callee->comdat_local_p ())
340 break;
341 if (se == NULL)
342 to->calls_comdat_local = false;
345 #ifdef ENABLE_CHECKING
346 /* Verify that estimated growth match real growth. Allow off-by-one
347 error due to INLINE_SIZE_SCALE roudoff errors. */
348 gcc_assert (!update_overall_summary || !overall_size || new_edges_found
349 || abs (estimated_growth - (new_size - old_size)) <= 1
350 || speculation_removed
351 /* FIXME: a hack. Edges with false predicate are accounted
352 wrong, we should remove them from callgraph. */
353 || predicated);
354 #endif
356 /* Account the change of overall unit size; external functions will be
357 removed and are thus not accounted. */
358 if (overall_size
359 && !DECL_EXTERNAL (to->decl))
360 *overall_size += new_size - old_size;
361 ncalls_inlined++;
363 /* This must happen after inline_merge_summary that rely on jump
364 functions of callee to not be updated. */
365 return new_edges_found;
369 /* Copy function body of NODE and redirect all inline clones to it.
370 This is done before inline plan is applied to NODE when there are
371 still some inline clones if it.
373 This is necessary because inline decisions are not really transitive
374 and the other inline clones may have different bodies. */
376 static struct cgraph_node *
377 save_inline_function_body (struct cgraph_node *node)
379 struct cgraph_node *first_clone, *n;
381 if (dump_file)
382 fprintf (dump_file, "\nSaving body of %s for later reuse\n",
383 node->name ());
385 gcc_assert (node == cgraph_node::get (node->decl));
387 /* first_clone will be turned into real function. */
388 first_clone = node->clones;
389 first_clone->decl = copy_node (node->decl);
390 first_clone->decl->decl_with_vis.symtab_node = first_clone;
391 gcc_assert (first_clone == cgraph_node::get (first_clone->decl));
393 /* Now reshape the clone tree, so all other clones descends from
394 first_clone. */
395 if (first_clone->next_sibling_clone)
397 for (n = first_clone->next_sibling_clone; n->next_sibling_clone; n = n->next_sibling_clone)
398 n->clone_of = first_clone;
399 n->clone_of = first_clone;
400 n->next_sibling_clone = first_clone->clones;
401 if (first_clone->clones)
402 first_clone->clones->prev_sibling_clone = n;
403 first_clone->clones = first_clone->next_sibling_clone;
404 first_clone->next_sibling_clone->prev_sibling_clone = NULL;
405 first_clone->next_sibling_clone = NULL;
406 gcc_assert (!first_clone->prev_sibling_clone);
408 first_clone->clone_of = NULL;
410 /* Now node in question has no clones. */
411 node->clones = NULL;
413 /* Inline clones share decl with the function they are cloned
414 from. Walk the whole clone tree and redirect them all to the
415 new decl. */
416 if (first_clone->clones)
417 for (n = first_clone->clones; n != first_clone;)
419 gcc_assert (n->decl == node->decl);
420 n->decl = first_clone->decl;
421 if (n->clones)
422 n = n->clones;
423 else if (n->next_sibling_clone)
424 n = n->next_sibling_clone;
425 else
427 while (n != first_clone && !n->next_sibling_clone)
428 n = n->clone_of;
429 if (n != first_clone)
430 n = n->next_sibling_clone;
434 /* Copy the OLD_VERSION_NODE function tree to the new version. */
435 tree_function_versioning (node->decl, first_clone->decl,
436 NULL, true, NULL, false,
437 NULL, NULL);
439 /* The function will be short lived and removed after we inline all the clones,
440 but make it internal so we won't confuse ourself. */
441 DECL_EXTERNAL (first_clone->decl) = 0;
442 TREE_PUBLIC (first_clone->decl) = 0;
443 DECL_COMDAT (first_clone->decl) = 0;
444 first_clone->ipa_transforms_to_apply.release ();
446 /* When doing recursive inlining, the clone may become unnecessary.
447 This is possible i.e. in the case when the recursive function is proved to be
448 non-throwing and the recursion happens only in the EH landing pad.
449 We can not remove the clone until we are done with saving the body.
450 Remove it now. */
451 if (!first_clone->callers)
453 first_clone->remove_symbol_and_inline_clones ();
454 first_clone = NULL;
456 #ifdef ENABLE_CHECKING
457 else
458 first_clone->verify ();
459 #endif
460 return first_clone;
463 /* Return true when function body of DECL still needs to be kept around
464 for later re-use. */
465 static bool
466 preserve_function_body_p (struct cgraph_node *node)
468 gcc_assert (symtab->global_info_ready);
469 gcc_assert (!node->alias && !node->thunk.thunk_p);
471 /* Look if there is any clone around. */
472 if (node->clones)
473 return true;
474 return false;
477 /* Apply inline plan to function. */
479 unsigned int
480 inline_transform (struct cgraph_node *node)
482 unsigned int todo = 0;
483 struct cgraph_edge *e, *next;
484 bool has_inline = false;
486 /* FIXME: Currently the pass manager is adding inline transform more than
487 once to some clones. This needs revisiting after WPA cleanups. */
488 if (cfun->after_inlining)
489 return 0;
491 /* We might need the body of this function so that we can expand
492 it inline somewhere else. */
493 if (preserve_function_body_p (node))
494 save_inline_function_body (node);
496 for (e = node->callees; e; e = next)
498 if (!e->inline_failed)
499 has_inline = true;
500 next = e->next_callee;
501 e->redirect_call_stmt_to_callee ();
503 node->remove_all_references ();
505 timevar_push (TV_INTEGRATION);
506 if (node->callees && (optimize || has_inline))
507 todo = optimize_inline_calls (current_function_decl);
508 timevar_pop (TV_INTEGRATION);
510 cfun->always_inline_functions_inlined = true;
511 cfun->after_inlining = true;
512 todo |= execute_fixup_cfg ();
514 if (!(todo & TODO_update_ssa_any))
515 /* Redirecting edges might lead to a need for vops to be recomputed. */
516 todo |= TODO_update_ssa_only_virtuals;
518 return todo;