Branch data Line data Source code
1 : : /* Callgraph transformations to handle inlining
2 : : Copyright (C) 2003-2025 Free Software Foundation, Inc.
3 : : Contributed by Jan Hubicka
4 : :
5 : : This file is part of GCC.
6 : :
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.
11 : :
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.
16 : :
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/>. */
20 : :
21 : : /* The inline decisions are stored in callgraph in "inline plan" and
22 : : applied later.
23 : :
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.
28 : :
29 : : The inline plan is applied on given function body by inline_transform. */
30 : :
31 : : #define INCLUDE_ALGORITHM
32 : : #include "config.h"
33 : : #include "system.h"
34 : : #include "coretypes.h"
35 : : #include "tm.h"
36 : : #include "function.h"
37 : : #include "tree.h"
38 : : #include "alloc-pool.h"
39 : : #include "tree-pass.h"
40 : : #include "cgraph.h"
41 : : #include "tree-cfg.h"
42 : : #include "symbol-summary.h"
43 : : #include "tree-vrp.h"
44 : : #include "sreal.h"
45 : : #include "ipa-cp.h"
46 : : #include "ipa-prop.h"
47 : : #include "ipa-fnsummary.h"
48 : : #include "ipa-inline.h"
49 : : #include "tree-inline.h"
50 : : #include "function.h"
51 : : #include "cfg.h"
52 : : #include "basic-block.h"
53 : : #include "ipa-utils.h"
54 : : #include "ipa-modref-tree.h"
55 : : #include "ipa-modref.h"
56 : : #include "symtab-thunks.h"
57 : : #include "symtab-clones.h"
58 : :
59 : : int ncalls_inlined;
60 : : int nfunctions_inlined;
61 : :
62 : : /* We removed or are going to remove the last call to NODE.
63 : : Return true if we can and want proactively remove the NODE now.
64 : : This is important to do, since we want inliner to know when offline
65 : : copy of function was removed. */
66 : :
67 : : static bool
68 : 2817483 : can_remove_node_now_p_1 (struct cgraph_node *node, struct cgraph_edge *e)
69 : : {
70 : 2817483 : ipa_ref *ref;
71 : :
72 : 3098896 : FOR_EACH_ALIAS (node, ref)
73 : : {
74 : 813200 : cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
75 : 518504 : if ((alias->callers && alias->callers != e)
76 : 813200 : || !can_remove_node_now_p_1 (alias, e))
77 : 531787 : return false;
78 : : }
79 : : /* FIXME: When address is taken of DECL_EXTERNAL function we still
80 : : can remove its offline copy, but we would need to keep unanalyzed node in
81 : : the callgraph so references can point to it.
82 : :
83 : : Also for comdat group we can ignore references inside a group as we
84 : : want to prove the group as a whole to be dead. */
85 : 2285696 : return (!node->address_taken
86 : 2250878 : && node->can_remove_if_no_direct_calls_and_refs_p ()
87 : : /* Inlining might enable more devirtualizing, so we want to remove
88 : : those only after all devirtualizable virtual calls are processed.
89 : : Lacking may edges in callgraph we just preserve them post
90 : : inlining. */
91 : 2183861 : && (!DECL_VIRTUAL_P (node->decl)
92 : 6570 : || !opt_for_fn (node->decl, flag_devirtualize))
93 : : /* During early inlining some unanalyzed cgraph nodes might be in the
94 : : callgraph and they might refer the function in question. */
95 : 4463081 : && !cgraph_new_nodes.exists ());
96 : : }
97 : :
98 : : /* We are going to eliminate last direct call to NODE (or alias of it) via edge E.
99 : : Verify that the NODE can be removed from unit and if it is contained in comdat
100 : : group that the whole comdat group is removable. */
101 : :
102 : : static bool
103 : 2323538 : can_remove_node_now_p (struct cgraph_node *node, struct cgraph_edge *e)
104 : : {
105 : 2323538 : struct cgraph_node *next;
106 : 2323538 : if (!can_remove_node_now_p_1 (node, e))
107 : : return false;
108 : :
109 : : /* When we see same comdat group, we need to be sure that all
110 : : items can be removed. */
111 : 1696749 : if (!node->same_comdat_group || !node->externally_visible)
112 : : return true;
113 : 284169 : for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
114 : 847645 : next != node; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
115 : : {
116 : 284488 : if (next->alias)
117 : 82515 : continue;
118 : 201601 : if ((next->callers && next->callers != e)
119 : 400850 : || !can_remove_node_now_p_1 (next, e))
120 : : return false;
121 : : }
122 : : return true;
123 : : }
124 : :
125 : : /* Return true if NODE is a master clone with non-inline clones. */
126 : :
127 : : static bool
128 : 1468336 : master_clone_with_noninline_clones_p (struct cgraph_node *node)
129 : : {
130 : 1468336 : if (node->clone_of)
131 : : return false;
132 : :
133 : 1864808 : for (struct cgraph_node *n = node->clones; n; n = n->next_sibling_clone)
134 : 496064 : if (n->decl != node->decl)
135 : : return true;
136 : :
137 : : return false;
138 : : }
139 : :
140 : : /* E is expected to be an edge being inlined. Clone destination node of
141 : : the edge and redirect it to the new clone.
142 : : DUPLICATE is used for bookkeeping on whether we are actually creating new
143 : : clones or re-using node originally representing out-of-line function call.
144 : : By default the offline copy is removed, when it appears dead after inlining.
145 : : KEEP_OFFLINE_COPY prevents this transformation.
146 : : If UPDATE_ORIGINAL is set, clones profile is subtracted from the offline version.
147 : : If OVERALL_SIZE is non-NULL, the size is updated to reflect the
148 : : transformation. */
149 : :
150 : : void
151 : 4682649 : clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
152 : : bool keep_offline_copy,
153 : : bool update_original, int *overall_size)
154 : : {
155 : 4682649 : struct cgraph_node *inlining_into;
156 : 4682649 : struct cgraph_edge *next;
157 : :
158 : 4682649 : if (e->caller->inlined_to)
159 : : inlining_into = e->caller->inlined_to;
160 : : else
161 : 3791909 : inlining_into = e->caller;
162 : :
163 : 4682649 : if (duplicate)
164 : : {
165 : : /* We may eliminate the need for out-of-line copy to be output.
166 : : In that case just go ahead and re-use it. This is not just an
167 : : memory optimization. Making offline copy of function disappear
168 : : from the program will improve future decisions on inlining. */
169 : 4476822 : if (!e->callee->callers->next_caller
170 : : /* Recursive inlining never wants the master clone to
171 : : be overwritten. */
172 : 2097267 : && !keep_offline_copy
173 : 2085724 : && can_remove_node_now_p (e->callee, e)
174 : : /* We cannot overwrite a master clone with non-inline clones
175 : : until after these clones are materialized. */
176 : 5945158 : && !master_clone_with_noninline_clones_p (e->callee))
177 : : {
178 : : /* TODO: When callee is in a comdat group, we could remove all of it,
179 : : including all inline clones inlined into it. That would however
180 : : need small function inlining to register edge removal hook to
181 : : maintain the priority queue.
182 : :
183 : : For now we keep the other functions in the group in program until
184 : : cgraph_remove_unreachable_functions gets rid of them. */
185 : 1468230 : gcc_assert (!e->callee->inlined_to);
186 : 1468230 : e->callee->remove_from_same_comdat_group ();
187 : 1468230 : if (e->callee->definition
188 : 1468230 : && inline_account_function_p (e->callee))
189 : : {
190 : 1311232 : gcc_assert (!e->callee->alias);
191 : 1311232 : if (overall_size)
192 : 259881 : *overall_size -= ipa_size_summaries->get (e->callee)->size;
193 : 1311232 : nfunctions_inlined++;
194 : : }
195 : 1468230 : duplicate = false;
196 : 1468230 : e->callee->externally_visible = false;
197 : 1468230 : profile_count num = e->count;
198 : 1468230 : profile_count den = e->callee->count;
199 : 1468230 : profile_count::adjust_for_ipa_scaling (&num, &den);
200 : 1468230 : e->callee->apply_scale (num, den);
201 : :
202 : 1468230 : dump_callgraph_transformation (e->callee, inlining_into,
203 : : "inlining to");
204 : : }
205 : : else
206 : : {
207 : 3008592 : struct cgraph_node *n;
208 : :
209 : 3008592 : n = e->callee->create_clone (e->callee->decl,
210 : : e->count,
211 : 3008592 : update_original, vNULL, true,
212 : : inlining_into,
213 : : NULL, NULL);
214 : 3008592 : n->used_as_abstract_origin = e->callee->used_as_abstract_origin;
215 : 3008592 : e->redirect_callee (n);
216 : : }
217 : : }
218 : : else
219 : 205827 : e->callee->remove_from_same_comdat_group ();
220 : :
221 : 4682649 : e->callee->inlined_to = inlining_into;
222 : 4682649 : if (e->callee->ipa_transforms_to_apply.length ())
223 : : {
224 : 301309 : e->callee->ipa_transforms_to_apply.release ();
225 : 301309 : e->callee->ipa_transforms_to_apply = vNULL;
226 : : }
227 : :
228 : : /* Recursively clone all bodies. */
229 : 8642076 : for (e = e->callee->callees; e; e = next)
230 : : {
231 : 3959427 : next = e->next_callee;
232 : 3959427 : if (!e->inline_failed)
233 : 658428 : clone_inlined_nodes (e, duplicate, keep_offline_copy,
234 : : update_original, overall_size);
235 : : }
236 : 4682649 : }
237 : :
238 : : /* Check all speculations in N and if any seem useless, resolve them. When a
239 : : first edge is resolved, pop all edges from NEW_EDGES and insert them to
240 : : EDGE_SET. Then remove each resolved edge from EDGE_SET, if it is there. */
241 : :
242 : : static bool
243 : 4680645 : check_speculations_1 (cgraph_node *n, vec<cgraph_edge *> *new_edges,
244 : : hash_set <cgraph_edge *> *edge_set)
245 : : {
246 : 4680645 : bool speculation_removed = false;
247 : 4680645 : cgraph_edge *next;
248 : :
249 : 8639846 : for (cgraph_edge *e = n->callees; e; e = next)
250 : : {
251 : 3959201 : next = e->next_callee;
252 : 3959201 : if (e->speculative && !speculation_useful_p (e, true))
253 : : {
254 : 52 : while (new_edges && !new_edges->is_empty ())
255 : 6 : edge_set->add (new_edges->pop ());
256 : 46 : edge_set->remove (e);
257 : :
258 : 46 : cgraph_edge::resolve_speculation (e, NULL);
259 : 46 : speculation_removed = true;
260 : : }
261 : 3959155 : else if (!e->inline_failed)
262 : 656916 : speculation_removed |= check_speculations_1 (e->callee, new_edges,
263 : : edge_set);
264 : : }
265 : 4680645 : return speculation_removed;
266 : : }
267 : :
268 : : /* Push E to NEW_EDGES. Called from hash_set traverse method, which
269 : : unfortunately means this function has to have external linkage, otherwise
270 : : the code will not compile with gcc 4.8. */
271 : :
272 : : bool
273 : 0 : push_all_edges_in_set_to_vec (cgraph_edge * const &e,
274 : : vec<cgraph_edge *> *new_edges)
275 : : {
276 : 0 : new_edges->safe_push (e);
277 : 0 : return true;
278 : : }
279 : :
280 : : /* Check all speculations in N and if any seem useless, resolve them and remove
281 : : them from NEW_EDGES. */
282 : :
283 : : static bool
284 : 4023729 : check_speculations (cgraph_node *n, vec<cgraph_edge *> *new_edges)
285 : : {
286 : 4023729 : hash_set <cgraph_edge *> edge_set;
287 : 4023729 : bool res = check_speculations_1 (n, new_edges, &edge_set);
288 : 4023729 : if (!edge_set.is_empty ())
289 : 0 : edge_set.traverse <vec<cgraph_edge *> *,
290 : 0 : push_all_edges_in_set_to_vec> (new_edges);
291 : 4023729 : return res;
292 : 4023729 : }
293 : :
294 : : /* Mark all call graph edges coming out of NODE and all nodes that have been
295 : : inlined to it as in_polymorphic_cdtor. */
296 : :
297 : : static void
298 : 129313 : mark_all_inlined_calls_cdtor (cgraph_node *node)
299 : : {
300 : 266496 : for (cgraph_edge *cs = node->callees; cs; cs = cs->next_callee)
301 : : {
302 : 137183 : cs->in_polymorphic_cdtor = true;
303 : 137183 : if (!cs->inline_failed)
304 : 25564 : mark_all_inlined_calls_cdtor (cs->callee);
305 : : }
306 : 137028 : for (cgraph_edge *cs = node->indirect_calls; cs; cs = cs->next_callee)
307 : 7715 : cs->in_polymorphic_cdtor = true;
308 : 129313 : }
309 : :
310 : :
311 : : /* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL
312 : : specify whether profile of original function should be updated and whether
313 : : offline copy should be removed if unnecesary. If any new
314 : : indirect edges are discovered in the process, add them to NEW_EDGES, unless
315 : : it is NULL. If UPDATE_OVERALL_SUMMARY is false, do not bother to recompute overall
316 : : size of caller after inlining. Caller is required to eventually do it via
317 : : ipa_update_overall_fn_summary.
318 : : If callee_removed is non-NULL, set it to true if we removed callee node.
319 : :
320 : : Return true iff any new callgraph edges were discovered as a
321 : : result of inlining. */
322 : :
323 : : bool
324 : 4023729 : inline_call (struct cgraph_edge *e, bool update_original,
325 : : vec<cgraph_edge *> *new_edges,
326 : : int *overall_size, bool update_overall_summary,
327 : : bool *callee_removed)
328 : : {
329 : 4023729 : int old_size = 0, new_size = 0;
330 : 4023729 : struct cgraph_node *to = NULL;
331 : 4023729 : struct cgraph_edge *curr = e;
332 : 4023729 : bool comdat_local = e->callee->comdat_local_p ();
333 : 4023729 : struct cgraph_node *callee = e->callee->ultimate_alias_target ();
334 : 4023729 : bool new_edges_found = false;
335 : 4023729 : bool keep_offline_copy = !update_original;
336 : :
337 : 4023729 : int estimated_growth = 0;
338 : 4023729 : if (! update_overall_summary)
339 : 3133615 : estimated_growth = estimate_edge_growth (e);
340 : : /* This is used only for assert below. */
341 : : #if 0
342 : : bool predicated = inline_edge_summary (e)->predicate != NULL;
343 : : #endif
344 : :
345 : : /* Don't inline inlined edges. */
346 : 4023729 : gcc_assert (e->inline_failed);
347 : : /* Don't even think of inlining inline clone. */
348 : 4023729 : gcc_assert (!callee->inlined_to);
349 : :
350 : 4023729 : to = e->caller;
351 : 4023729 : if (to->inlined_to)
352 : 232312 : to = to->inlined_to;
353 : :
354 : : /* In case callee has AFDO profile but caller has GLOBAL0 we need
355 : : to re-scale it so it can have non-zero AFDO profile. */
356 : 4023729 : if (callee->count.quality () == AFDO
357 : 0 : && e->count.nonzero_p ()
358 : 4023729 : && (to->count.quality () == GUESSED_GLOBAL0_AFDO
359 : 0 : || to->count.quality () == GUESSED_GLOBAL0_ADJUSTED))
360 : : {
361 : 0 : profile_count num = callee->count;
362 : 0 : profile_count den = e->count;
363 : 0 : profile_count::adjust_for_ipa_scaling (&num, &den);
364 : 0 : if (dump_file)
365 : : {
366 : 0 : fprintf (dump_file, "Rescalling profile of caller %s "
367 : : "to allow non-zero AFDO counts:",
368 : : to->dump_name ());
369 : 0 : den.dump (dump_file);
370 : 0 : fprintf (dump_file, " -> ");
371 : 0 : num.dump (dump_file);
372 : 0 : fprintf (dump_file, "\n");
373 : : }
374 : 0 : to->apply_scale (num, den);
375 : 0 : to->frequency = std::max (to->frequency, callee->frequency);
376 : : /* Do not update original, so possible additional calls of callee
377 : : are handled reasonably well. */
378 : 0 : update_original = false;
379 : 0 : gcc_checking_assert (to->count.quality () == AFDO);
380 : 0 : if (dump_file)
381 : : {
382 : 0 : fprintf (dump_file, "Scaled profile of %s: ", to->dump_name ());
383 : 0 : to->count.dump (dump_file);
384 : 0 : fprintf (dump_file, "\n");
385 : : }
386 : : }
387 : : /* Do sanity checking of the profile and in case of inconsistencies do not
388 : : update profile of original. This reduces the chances that inlining
389 : : turns callee cold while in reality it is still hot. */
390 : 4023729 : if (!(callee->count.ipa ().force_nonzero () == callee->count.ipa ()))
391 : : {
392 : 7314 : if (dump_file)
393 : 0 : fprintf (dump_file, "Callee count is 0; not updating callee profile\n");
394 : : update_original = false;
395 : : }
396 : 4016415 : else if (e->count.ipa ().quality () == AFDO
397 : 4016415 : && !(e->count.ipa ().force_nonzero () == e->count.ipa ()))
398 : : {
399 : 0 : if (dump_file)
400 : 0 : fprintf (dump_file, "Edge count is AFDO 0; not updating callee profile\n");
401 : : update_original = false;
402 : : }
403 : 4023729 : if (e->count.ipa () > callee->count.ipa ().apply_scale (9, 8))
404 : : {
405 : 0 : if (dump_file)
406 : 0 : fprintf (dump_file, "Calee count is too small (profile is inconsistent);"
407 : : " not updating callee profile\n");
408 : : update_original = false;
409 : : }
410 : 4023729 : if (to->thunk)
411 : : {
412 : 1159 : struct cgraph_node *target = to->callees->callee;
413 : 1159 : thunk_expansion = true;
414 : :
415 : : /* Remove all annotations, but keep thunk info. */
416 : 1159 : thunk_info info = *thunk_info::get (to);
417 : 1159 : symtab->call_cgraph_removal_hooks (to);
418 : 1159 : *thunk_info::get_create (to) = info;
419 : 1159 : if (in_lto_p)
420 : 49 : to->get_untransformed_body ();
421 : 1159 : expand_thunk (to, false, true);
422 : : /* When thunk is instrumented we may have multiple callees. */
423 : 1159 : for (e = to->callees; e && e->callee != target; e = e->next_callee)
424 : : ;
425 : 1159 : symtab->call_cgraph_insertion_hooks (to);
426 : 1159 : thunk_expansion = false;
427 : 1159 : gcc_assert (e);
428 : : }
429 : :
430 : :
431 : 4023729 : e->inline_failed = CIF_OK;
432 : 4023729 : DECL_POSSIBLY_INLINED (callee->decl) = true;
433 : :
434 : 4023729 : if (DECL_FUNCTION_PERSONALITY (callee->decl))
435 : 268204 : DECL_FUNCTION_PERSONALITY (to->decl)
436 : 134102 : = DECL_FUNCTION_PERSONALITY (callee->decl);
437 : :
438 : 4023729 : bool reload_optimization_node = false;
439 : 4023729 : bool remove_strict_aliasing
440 : 4023729 : = (!opt_for_fn (callee->decl, flag_strict_aliasing)
441 : 4023729 : && opt_for_fn (to->decl, flag_strict_aliasing));
442 : 4023729 : bool remove_assume_sane_operators_new_delete
443 : 4023729 : = (!opt_for_fn (callee->decl, flag_assume_sane_operators_new_delete)
444 : 4023729 : && opt_for_fn (to->decl, flag_assume_sane_operators_new_delete));
445 : 4023711 : if (remove_strict_aliasing || remove_assume_sane_operators_new_delete)
446 : : {
447 : 31 : struct gcc_options opts = global_options;
448 : 31 : struct gcc_options opts_set = global_options_set;
449 : :
450 : 31 : cl_optimization_restore (&opts, &opts_set, opts_for_fn (to->decl));
451 : 31 : if (remove_strict_aliasing)
452 : : {
453 : 13 : opts.x_flag_strict_aliasing = false;
454 : 13 : if (dump_file)
455 : 0 : fprintf (dump_file, "Dropping flag_strict_aliasing on %s\n",
456 : : to->dump_name ());
457 : : }
458 : 31 : if (remove_assume_sane_operators_new_delete)
459 : : {
460 : 18 : opts.x_flag_assume_sane_operators_new_delete = false;
461 : 18 : if (dump_file)
462 : 0 : fprintf (dump_file,
463 : : "Dropping flag_assume_sane_operators_new_delete on %s\n",
464 : : to->dump_name ());
465 : : }
466 : 31 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (to->decl)
467 : 31 : = build_optimization_node (&opts, &opts_set);
468 : 31 : reload_optimization_node = true;
469 : : }
470 : :
471 : 4023729 : ipa_fn_summary *caller_info = ipa_fn_summaries->get (to);
472 : 4023729 : ipa_fn_summary *callee_info = ipa_fn_summaries->get (callee);
473 : 4023729 : if (!caller_info->fp_expressions && callee_info->fp_expressions)
474 : : {
475 : 15792 : caller_info->fp_expressions = true;
476 : 15792 : if (opt_for_fn (callee->decl, flag_rounding_math)
477 : 15792 : != opt_for_fn (to->decl, flag_rounding_math)
478 : 15792 : || opt_for_fn (callee->decl, flag_trapping_math)
479 : 15792 : != opt_for_fn (to->decl, flag_trapping_math)
480 : 15785 : || opt_for_fn (callee->decl, flag_unsafe_math_optimizations)
481 : 15785 : != opt_for_fn (to->decl, flag_unsafe_math_optimizations)
482 : 15785 : || opt_for_fn (callee->decl, flag_finite_math_only)
483 : 15785 : != opt_for_fn (to->decl, flag_finite_math_only)
484 : 15785 : || opt_for_fn (callee->decl, flag_signaling_nans)
485 : 15785 : != opt_for_fn (to->decl, flag_signaling_nans)
486 : 15785 : || opt_for_fn (callee->decl, flag_complex_method)
487 : 15785 : != opt_for_fn (to->decl, flag_complex_method)
488 : 15785 : || opt_for_fn (callee->decl, flag_signed_zeros)
489 : 15785 : != opt_for_fn (to->decl, flag_signed_zeros)
490 : 15785 : || opt_for_fn (callee->decl, flag_associative_math)
491 : 15785 : != opt_for_fn (to->decl, flag_associative_math)
492 : 15784 : || opt_for_fn (callee->decl, flag_reciprocal_math)
493 : 15784 : != opt_for_fn (to->decl, flag_reciprocal_math)
494 : 15784 : || opt_for_fn (callee->decl, flag_fp_int_builtin_inexact)
495 : 15784 : != opt_for_fn (to->decl, flag_fp_int_builtin_inexact)
496 : 15792 : || opt_for_fn (callee->decl, flag_errno_math)
497 : 15784 : != opt_for_fn (to->decl, flag_errno_math))
498 : : {
499 : 8 : struct gcc_options opts = global_options;
500 : 8 : struct gcc_options opts_set = global_options_set;
501 : :
502 : 8 : cl_optimization_restore (&opts, &opts_set, opts_for_fn (to->decl));
503 : 8 : opts.x_flag_rounding_math
504 : 8 : = opt_for_fn (callee->decl, flag_rounding_math);
505 : 8 : opts.x_flag_trapping_math
506 : 8 : = opt_for_fn (callee->decl, flag_trapping_math);
507 : 8 : opts.x_flag_unsafe_math_optimizations
508 : 8 : = opt_for_fn (callee->decl, flag_unsafe_math_optimizations);
509 : 8 : opts.x_flag_finite_math_only
510 : 8 : = opt_for_fn (callee->decl, flag_finite_math_only);
511 : 8 : opts.x_flag_signaling_nans
512 : 8 : = opt_for_fn (callee->decl, flag_signaling_nans);
513 : 8 : opts.x_flag_complex_method
514 : 8 : = opt_for_fn (callee->decl, flag_complex_method);
515 : 8 : opts.x_flag_signed_zeros
516 : 8 : = opt_for_fn (callee->decl, flag_signed_zeros);
517 : 8 : opts.x_flag_associative_math
518 : 8 : = opt_for_fn (callee->decl, flag_associative_math);
519 : 8 : opts.x_flag_reciprocal_math
520 : 8 : = opt_for_fn (callee->decl, flag_reciprocal_math);
521 : 8 : opts.x_flag_fp_int_builtin_inexact
522 : 8 : = opt_for_fn (callee->decl, flag_fp_int_builtin_inexact);
523 : 8 : opts.x_flag_errno_math
524 : 8 : = opt_for_fn (callee->decl, flag_errno_math);
525 : 8 : if (dump_file)
526 : 0 : fprintf (dump_file, "Copying FP flags from %s to %s\n",
527 : : callee->dump_name (), to->dump_name ());
528 : 8 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (to->decl)
529 : 8 : = build_optimization_node (&opts, &opts_set);
530 : 8 : reload_optimization_node = true;
531 : : }
532 : : }
533 : :
534 : : /* Reload global optimization flags. */
535 : 4023729 : if (reload_optimization_node && DECL_STRUCT_FUNCTION (to->decl) == cfun)
536 : 34 : set_cfun (cfun, true);
537 : :
538 : : /* If aliases are involved, redirect edge to the actual destination and
539 : : possibly remove the aliases. */
540 : 4023729 : if (e->callee != callee)
541 : : {
542 : 748840 : struct cgraph_node *alias = e->callee, *next_alias;
543 : 748840 : e->redirect_callee (callee);
544 : 1723343 : while (alias && alias != callee)
545 : : {
546 : 748859 : if (!alias->callers
547 : 986673 : && can_remove_node_now_p (alias,
548 : 237814 : !e->next_caller && !e->prev_caller ? e : NULL))
549 : : {
550 : 225663 : next_alias = alias->get_alias_target ();
551 : 225663 : alias->remove ();
552 : 225663 : if (callee_removed)
553 : 1205 : *callee_removed = true;
554 : : alias = next_alias;
555 : : }
556 : : else
557 : : break;
558 : : }
559 : : }
560 : :
561 : 4023729 : clone_inlined_nodes (e, true, keep_offline_copy,
562 : : update_original, overall_size);
563 : :
564 : 4023729 : gcc_assert (curr->callee->inlined_to == to);
565 : :
566 : 4023729 : old_size = ipa_size_summaries->get (to)->size;
567 : 4023729 : ipa_merge_modref_summary_after_inlining (e);
568 : 4023729 : ipa_merge_fn_summary_after_inlining (e);
569 : 4023729 : if (e->in_polymorphic_cdtor)
570 : 103749 : mark_all_inlined_calls_cdtor (e->callee);
571 : 4023729 : if (opt_for_fn (e->caller->decl, optimize))
572 : 3997069 : new_edges_found = ipa_propagate_indirect_call_infos (curr, new_edges);
573 : 4023729 : bool removed_p = check_speculations (e->callee, new_edges);
574 : 4023729 : if (update_overall_summary)
575 : 890114 : ipa_update_overall_fn_summary (to, new_edges_found || removed_p);
576 : : else
577 : : /* Update self size by the estimate so overall function growth limits
578 : : work for further inlining into this function. Before inlining
579 : : the function we inlined to again we expect the caller to update
580 : : the overall summary. */
581 : 3133615 : ipa_size_summaries->get (to)->size += estimated_growth;
582 : 4023729 : new_size = ipa_size_summaries->get (to)->size;
583 : :
584 : 4023729 : if (callee->calls_comdat_local)
585 : 0 : to->calls_comdat_local = true;
586 : 4023729 : else if (to->calls_comdat_local && comdat_local)
587 : 3493 : to->calls_comdat_local = to->check_calls_comdat_local_p ();
588 : :
589 : : /* FIXME: This assert suffers from roundoff errors, disable it for GCC 5
590 : : and revisit it after conversion to sreals in GCC 6.
591 : : See PR 65654. */
592 : : #if 0
593 : : /* Verify that estimated growth match real growth. Allow off-by-one
594 : : error due to ipa_fn_summary::size_scale roudoff errors. */
595 : : gcc_assert (!update_overall_summary || !overall_size || new_edges_found
596 : : || abs (estimated_growth - (new_size - old_size)) <= 1
597 : : || speculation_removed
598 : : /* FIXME: a hack. Edges with false predicate are accounted
599 : : wrong, we should remove them from callgraph. */
600 : : || predicated);
601 : : #endif
602 : :
603 : : /* Account the change of overall unit size; external functions will be
604 : : removed and are thus not accounted. */
605 : 4023729 : if (overall_size && inline_account_function_p (to))
606 : 875009 : *overall_size += new_size - old_size;
607 : 4023729 : ncalls_inlined++;
608 : :
609 : : /* This must happen after ipa_merge_fn_summary_after_inlining that rely on jump
610 : : functions of callee to not be updated. */
611 : 4023729 : return new_edges_found;
612 : : }
613 : :
614 : : /* For each node that was made the holder of function body by
615 : : save_inline_function_body, this summary contains pointer to the previous
616 : : holder of the body. */
617 : :
618 : : function_summary <tree *> *ipa_saved_clone_sources;
619 : :
620 : : /* Copy function body of NODE and redirect all inline clones to it.
621 : : This is done before inline plan is applied to NODE when there are
622 : : still some inline clones if it.
623 : :
624 : : This is necessary because inline decisions are not really transitive
625 : : and the other inline clones may have different bodies. */
626 : :
627 : : static struct cgraph_node *
628 : 47733 : save_inline_function_body (struct cgraph_node *node)
629 : : {
630 : 47733 : struct cgraph_node *first_clone, *n;
631 : :
632 : 47733 : if (dump_file)
633 : 147 : fprintf (dump_file, "\nSaving body of %s for later reuse\n",
634 : : node->dump_name ());
635 : :
636 : 47733 : gcc_assert (node == cgraph_node::get (node->decl));
637 : :
638 : : /* first_clone will be turned into real function. */
639 : 47733 : first_clone = node->clones;
640 : :
641 : : /* Arrange first clone to not be thunk as those do not have bodies. */
642 : 47733 : if (first_clone->thunk)
643 : : {
644 : 0 : while (first_clone->thunk)
645 : 0 : first_clone = first_clone->next_sibling_clone;
646 : 0 : first_clone->prev_sibling_clone->next_sibling_clone
647 : 0 : = first_clone->next_sibling_clone;
648 : 0 : if (first_clone->next_sibling_clone)
649 : 0 : first_clone->next_sibling_clone->prev_sibling_clone
650 : 0 : = first_clone->prev_sibling_clone;
651 : 0 : first_clone->next_sibling_clone = node->clones;
652 : 0 : first_clone->prev_sibling_clone = NULL;
653 : 0 : node->clones->prev_sibling_clone = first_clone;
654 : 0 : node->clones = first_clone;
655 : : }
656 : 47733 : first_clone->decl = copy_node (node->decl);
657 : 47733 : first_clone->decl->decl_with_vis.symtab_node = first_clone;
658 : 47733 : gcc_assert (first_clone == cgraph_node::get (first_clone->decl));
659 : :
660 : : /* Now reshape the clone tree, so all other clones descends from
661 : : first_clone. */
662 : 47733 : if (first_clone->next_sibling_clone)
663 : : {
664 : 188601 : for (n = first_clone->next_sibling_clone; n->next_sibling_clone;
665 : 164483 : n = n->next_sibling_clone)
666 : 164483 : n->clone_of = first_clone;
667 : 24118 : n->clone_of = first_clone;
668 : 24118 : n->next_sibling_clone = first_clone->clones;
669 : 24118 : if (first_clone->clones)
670 : 679 : first_clone->clones->prev_sibling_clone = n;
671 : 24118 : first_clone->clones = first_clone->next_sibling_clone;
672 : 24118 : first_clone->next_sibling_clone->prev_sibling_clone = NULL;
673 : 24118 : first_clone->next_sibling_clone = NULL;
674 : 24118 : gcc_assert (!first_clone->prev_sibling_clone);
675 : : }
676 : :
677 : 47733 : tree prev_body_holder = node->decl;
678 : 47733 : if (!ipa_saved_clone_sources)
679 : : {
680 : 13965 : ipa_saved_clone_sources = new function_summary <tree *> (symtab);
681 : 13965 : ipa_saved_clone_sources->disable_insertion_hook ();
682 : : }
683 : : else
684 : : {
685 : 33768 : tree *p = ipa_saved_clone_sources->get (node);
686 : 33768 : if (p)
687 : : {
688 : 0 : prev_body_holder = *p;
689 : 0 : gcc_assert (prev_body_holder);
690 : : }
691 : : }
692 : 47733 : *ipa_saved_clone_sources->get_create (first_clone) = prev_body_holder;
693 : 47733 : first_clone->former_clone_of
694 : 47733 : = node->former_clone_of ? node->former_clone_of : node->decl;
695 : 47733 : first_clone->clone_of = NULL;
696 : :
697 : : /* Now node in question has no clones. */
698 : 47733 : node->clones = NULL;
699 : :
700 : : /* Inline clones share decl with the function they are cloned
701 : : from. Walk the whole clone tree and redirect them all to the
702 : : new decl. */
703 : 47733 : if (first_clone->clones)
704 : 278528 : for (n = first_clone->clones; n != first_clone;)
705 : : {
706 : 251961 : gcc_assert (n->decl == node->decl);
707 : 251961 : n->decl = first_clone->decl;
708 : 251961 : if (n->clones)
709 : : n = n->clones;
710 : 244970 : else if (n->next_sibling_clone)
711 : : n = n->next_sibling_clone;
712 : : else
713 : : {
714 : 64156 : while (n != first_clone && !n->next_sibling_clone)
715 : 33558 : n = n->clone_of;
716 : 30598 : if (n != first_clone)
717 : 4031 : n = n->next_sibling_clone;
718 : : }
719 : : }
720 : :
721 : : /* Copy the OLD_VERSION_NODE function tree to the new version. */
722 : 47733 : tree_function_versioning (node->decl, first_clone->decl,
723 : : NULL, NULL, true, NULL, NULL);
724 : :
725 : : /* The function will be short lived and removed after we inline all the
726 : : clones, but make it internal so we won't confuse ourself. */
727 : 47733 : DECL_EXTERNAL (first_clone->decl) = 0;
728 : 47733 : TREE_PUBLIC (first_clone->decl) = 0;
729 : 47733 : DECL_COMDAT (first_clone->decl) = 0;
730 : 47733 : first_clone->ipa_transforms_to_apply.release ();
731 : :
732 : : /* When doing recursive inlining, the clone may become unnecessary.
733 : : This is possible i.e. in the case when the recursive function is proved to
734 : : be non-throwing and the recursion happens only in the EH landing pad.
735 : : We cannot remove the clone until we are done with saving the body.
736 : : Remove it now. */
737 : 47733 : if (!first_clone->callers)
738 : : {
739 : 0 : first_clone->remove_symbol_and_inline_clones ();
740 : 0 : first_clone = NULL;
741 : : }
742 : 47733 : else if (flag_checking)
743 : 47733 : first_clone->verify ();
744 : :
745 : 47733 : return first_clone;
746 : : }
747 : :
748 : : /* Return true when function body of DECL still needs to be kept around
749 : : for later re-use. */
750 : : static bool
751 : 1460645 : preserve_function_body_p (struct cgraph_node *node)
752 : : {
753 : 1460645 : gcc_assert (symtab->global_info_ready);
754 : 1460645 : gcc_assert (!node->alias && !node->thunk);
755 : :
756 : : /* Look if there is any non-thunk clone around. */
757 : 1460685 : for (node = node->clones; node; node = node->next_sibling_clone)
758 : 47773 : if (!node->thunk)
759 : : return true;
760 : : return false;
761 : : }
762 : :
763 : : /* tree-inline can not recurse; materialize all function bodie we will need
764 : : during inlining. This includes inlined functions, but also called functions
765 : : with param manipulation because IPA param manipulation attaches debug
766 : : statements to PARM_DECLs of called clone. Materialize them if needed.
767 : :
768 : : FIXME: This is somehwat broken by design because it does not play well
769 : : with partitioning. */
770 : :
771 : : static void
772 : 2813842 : maybe_materialize_called_clones (cgraph_node *node)
773 : : {
774 : 11409203 : for (cgraph_edge *e = node->callees; e; e = e->next_callee)
775 : : {
776 : 8595361 : clone_info *info;
777 : :
778 : 8595361 : if (!e->inline_failed)
779 : 1353197 : maybe_materialize_called_clones (e->callee);
780 : :
781 : 8595361 : cgraph_node *callee = cgraph_node::get (e->callee->decl);
782 : 8595361 : if (callee->clone_of
783 : 8595361 : && (info = clone_info::get (callee)) && info->param_adjustments)
784 : 94003 : callee->get_untransformed_body ();
785 : : }
786 : 2813842 : }
787 : :
788 : : /* Apply inline plan to function. */
789 : :
790 : : unsigned int
791 : 1460645 : inline_transform (struct cgraph_node *node)
792 : : {
793 : 1460645 : unsigned int todo = 0;
794 : 1460645 : struct cgraph_edge *e, *next;
795 : 1460645 : bool has_inline = false;
796 : :
797 : : /* FIXME: Currently the pass manager is adding inline transform more than
798 : : once to some clones. This needs revisiting after WPA cleanups. */
799 : 1460645 : if (cfun->after_inlining)
800 : : return 0;
801 : :
802 : 1460645 : cgraph_node *next_clone;
803 : 1697019 : for (cgraph_node *n = node->clones; n; n = next_clone)
804 : : {
805 : 236374 : next_clone = n->next_sibling_clone;
806 : 236374 : if (n->decl != node->decl)
807 : 0 : n->materialize_clone ();
808 : : }
809 : 1460645 : node->clear_stmts_in_references ();
810 : :
811 : : /* We might need the body of this function so that we can expand
812 : : it inline somewhere else. */
813 : 1460645 : if (preserve_function_body_p (node))
814 : 47733 : save_inline_function_body (node);
815 : :
816 : 1460645 : profile_count num = node->count;
817 : 1460645 : profile_count den = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
818 : 1460645 : bool scale = num.initialized_p () && !(num == den);
819 : 46 : if (scale)
820 : : {
821 : 46 : profile_count::adjust_for_ipa_scaling (&num, &den);
822 : 46 : if (dump_file)
823 : : {
824 : 0 : fprintf (dump_file, "Applying count scale ");
825 : 0 : num.dump (dump_file);
826 : 0 : fprintf (dump_file, "/");
827 : 0 : den.dump (dump_file);
828 : 0 : fprintf (dump_file, "\n");
829 : : }
830 : :
831 : 46 : basic_block bb;
832 : 46 : cfun->cfg->count_max = profile_count::uninitialized ();
833 : 359 : FOR_ALL_BB_FN (bb, cfun)
834 : : {
835 : 313 : bb->count = bb->count.apply_scale (num, den);
836 : 313 : cfun->cfg->count_max = profile_count::max_prefer_initialized
837 : 313 : (cfun->cfg->count_max, bb->count);
838 : : }
839 : 46 : ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = node->count;
840 : : }
841 : :
842 : 1460645 : maybe_materialize_called_clones (node);
843 : 7380824 : for (e = node->callees; e; e = next)
844 : : {
845 : 5920179 : if (!e->inline_failed)
846 : 559379 : has_inline = true;
847 : 5920179 : next = e->next_callee;
848 : 5920179 : if (e->has_callback)
849 : : {
850 : : /* Redirect callback edges when redirecting their carrying edge. */
851 : 8 : cgraph_edge *cbe;
852 : 8 : cgraph_edge::redirect_call_stmt_to_callee (e);
853 : 8 : for (cbe = e->first_callback_edge (); cbe;
854 : 0 : cbe = cbe->next_callback_edge ())
855 : 0 : cgraph_edge::redirect_call_stmt_to_callee (cbe);
856 : : }
857 : : else
858 : 5920171 : cgraph_edge::redirect_call_stmt_to_callee (e);
859 : : }
860 : 1460645 : node->remove_all_references ();
861 : :
862 : 1460645 : timevar_push (TV_INTEGRATION);
863 : 1460645 : if (node->callees && (opt_for_fn (node->decl, optimize) || has_inline))
864 : : {
865 : 807401 : todo = optimize_inline_calls (current_function_decl);
866 : : }
867 : 1460645 : timevar_pop (TV_INTEGRATION);
868 : :
869 : 1460645 : cfun->always_inline_functions_inlined = true;
870 : 1460645 : cfun->after_inlining = true;
871 : 1460645 : todo |= execute_fixup_cfg ();
872 : :
873 : 1460645 : if (!(todo & TODO_update_ssa_any))
874 : : /* Redirecting edges might lead to a need for vops to be recomputed. */
875 : 1088688 : todo |= TODO_update_ssa_only_virtuals;
876 : :
877 : : return todo;
878 : : }
|