]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ipa-inline-transform.c
re PR debug/54971 (SRA pessimizes debug info by not creating debug stmts for fields...
[thirdparty/gcc.git] / gcc / ipa-inline-transform.c
CommitLineData
fee8b6da
JH
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
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 3, or (at your option) any later
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
21
22/* The inline decisions are stored in callgraph in "inline plan" and
23 applied later.
24
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.
29
30 The inline plan is applied on given function body by inline_transform. */
31
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"
fee8b6da
JH
39#include "intl.h"
40#include "coverage.h"
41#include "ggc.h"
42#include "tree-flow.h"
43#include "ipa-prop.h"
44#include "ipa-inline.h"
45#include "tree-inline.h"
516e0768 46#include "tree-pass.h"
fee8b6da
JH
47
48int ncalls_inlined;
49int nfunctions_inlined;
50
898b8927 51/* Scale frequency of NODE edges by FREQ_SCALE. */
fee8b6da
JH
52
53static void
54update_noncloned_frequencies (struct cgraph_node *node,
898b8927 55 int freq_scale)
fee8b6da
JH
56{
57 struct cgraph_edge *e;
58
59 /* We do not want to ignore high loop nest after freq drops to 0. */
60 if (!freq_scale)
61 freq_scale = 1;
62 for (e = node->callees; e; e = e->next_callee)
63 {
fee8b6da
JH
64 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
65 if (e->frequency > CGRAPH_FREQ_MAX)
66 e->frequency = CGRAPH_FREQ_MAX;
67 if (!e->inline_failed)
898b8927
JH
68 update_noncloned_frequencies (e->callee, freq_scale);
69 }
70 for (e = node->indirect_calls; e; e = e->next_callee)
71 {
72 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
73 if (e->frequency > CGRAPH_FREQ_MAX)
74 e->frequency = CGRAPH_FREQ_MAX;
fee8b6da
JH
75 }
76}
77
a5b1779f
JH
78/* We removed or are going to remove the last call to NODE.
79 Return true if we can and want proactively remove the NODE now.
80 This is important to do, since we want inliner to know when offline
81 copy of function was removed. */
82
83static bool
6c69a029 84can_remove_node_now_p_1 (struct cgraph_node *node)
a5b1779f
JH
85{
86 /* FIXME: When address is taken of DECL_EXTERNAL function we still
87 can remove its offline copy, but we would need to keep unanalyzed node in
88 the callgraph so references can point to it. */
960bfb69
JH
89 return (!node->symbol.address_taken
90 && !ipa_ref_has_aliases_p (&node->symbol.ref_list)
a5b1779f
JH
91 && cgraph_can_remove_if_no_direct_calls_p (node)
92 /* Inlining might enable more devirtualizing, so we want to remove
93 those only after all devirtualizable virtual calls are processed.
94 Lacking may edges in callgraph we just preserve them post
95 inlining. */
960bfb69
JH
96 && (!DECL_VIRTUAL_P (node->symbol.decl)
97 || (!DECL_COMDAT (node->symbol.decl)
98 && !DECL_EXTERNAL (node->symbol.decl)))
a5b1779f
JH
99 /* During early inlining some unanalyzed cgraph nodes might be in the
100 callgraph and they might reffer the function in question. */
101 && !cgraph_new_nodes);
102}
103
6c69a029
JH
104/* We are going to eliminate last direct call to NODE (or alias of it) via edge E.
105 Verify that the NODE can be removed from unit and if it is contained in comdat
106 group that the whole comdat group is removable. */
107
108static bool
109can_remove_node_now_p (struct cgraph_node *node, struct cgraph_edge *e)
110{
111 struct cgraph_node *next;
112 if (!can_remove_node_now_p_1 (node))
113 return false;
114
115 /* When we see same comdat group, we need to be sure that all
116 items can be removed. */
960bfb69 117 if (!node->symbol.same_comdat_group)
6c69a029 118 return true;
960bfb69
JH
119 for (next = cgraph (node->symbol.same_comdat_group);
120 next != node; next = cgraph (next->symbol.same_comdat_group))
aa74f081
JH
121 if ((next->callers && next->callers != e)
122 || !can_remove_node_now_p_1 (next))
6c69a029
JH
123 return false;
124 return true;
125}
126
fee8b6da
JH
127
128/* E is expected to be an edge being inlined. Clone destination node of
129 the edge and redirect it to the new clone.
130 DUPLICATE is used for bookkeeping on whether we are actually creating new
131 clones or re-using node originally representing out-of-line function call.
132 */
133
134void
135clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
136 bool update_original, int *overall_size)
137{
fee8b6da
JH
138 if (duplicate)
139 {
140 /* We may eliminate the need for out-of-line copy to be output.
141 In that case just go ahead and re-use it. This is not just an
142 memory optimization. Making offline copy of fuction disappear
143 from the program will improve future decisions on inlining. */
144 if (!e->callee->callers->next_caller
145 /* Recursive inlining never wants the master clone to
146 be overwritten. */
147 && update_original
6c69a029 148 && can_remove_node_now_p (e->callee, e))
fee8b6da 149 {
6c69a029
JH
150 /* TODO: When callee is in a comdat group, we could remove all of it,
151 including all inline clones inlined into it. That would however
152 need small function inlining to register edge removal hook to
153 maintain the priority queue.
154
155 For now we keep the ohter functions in the group in program until
156 cgraph_remove_unreachable_functions gets rid of them. */
fee8b6da 157 gcc_assert (!e->callee->global.inlined_to);
65d630d4 158 symtab_dissolve_same_comdat_group_list ((symtab_node) e->callee);
960bfb69 159 if (e->callee->analyzed && !DECL_EXTERNAL (e->callee->symbol.decl))
fee8b6da
JH
160 {
161 if (overall_size)
162 *overall_size -= inline_summary (e->callee)->size;
163 nfunctions_inlined++;
164 }
165 duplicate = false;
960bfb69 166 e->callee->symbol.externally_visible = false;
898b8927 167 update_noncloned_frequencies (e->callee, e->frequency);
fee8b6da
JH
168 }
169 else
170 {
171 struct cgraph_node *n;
960bfb69 172 n = cgraph_clone_node (e->callee, e->callee->symbol.decl,
898b8927 173 e->count, e->frequency,
74605a11 174 update_original, NULL, true);
fee8b6da
JH
175 cgraph_redirect_edge_callee (e, n);
176 }
177 }
65d630d4
JH
178 else
179 symtab_dissolve_same_comdat_group_list ((symtab_node) e->callee);
fee8b6da 180
fee8b6da
JH
181 if (e->caller->global.inlined_to)
182 e->callee->global.inlined_to = e->caller->global.inlined_to;
183 else
184 e->callee->global.inlined_to = e->caller;
fee8b6da
JH
185
186 /* Recursively clone all bodies. */
187 for (e = e->callee->callees; e; e = e->next_callee)
188 if (!e->inline_failed)
189 clone_inlined_nodes (e, duplicate, update_original, overall_size);
190}
191
192
193/* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL
194 specify whether profile of original function should be updated. If any new
195 indirect edges are discovered in the process, add them to NEW_EDGES, unless
c170d40f
JH
196 it is NULL. If UPDATE_OVERALL_SUMMARY is false, do not bother to recompute overall
197 size of caller after inlining. Caller is required to eventually do it via
198 inline_update_overall_summary.
199
200 Return true iff any new callgraph edges were discovered as a
fee8b6da
JH
201 result of inlining. */
202
203bool
204inline_call (struct cgraph_edge *e, bool update_original,
205 VEC (cgraph_edge_p, heap) **new_edges,
c170d40f 206 int *overall_size, bool update_overall_summary)
fee8b6da
JH
207{
208 int old_size = 0, new_size = 0;
209 struct cgraph_node *to = NULL;
210 struct cgraph_edge *curr = e;
a5b1779f 211 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
0f378cb5
JH
212 bool new_edges_found = false;
213
214#ifdef ENABLE_CHECKING
215 int estimated_growth = estimate_edge_growth (e);
216 bool predicated = inline_edge_summary (e)->predicate != NULL;
217#endif
fee8b6da
JH
218
219 /* Don't inline inlined edges. */
220 gcc_assert (e->inline_failed);
221 /* Don't even think of inlining inline clone. */
a5b1779f 222 gcc_assert (!callee->global.inlined_to);
fee8b6da
JH
223
224 e->inline_failed = CIF_OK;
960bfb69 225 DECL_POSSIBLY_INLINED (callee->symbol.decl) = true;
fee8b6da 226
632b4f8e
JH
227 to = e->caller;
228 if (to->global.inlined_to)
229 to = to->global.inlined_to;
632b4f8e 230
a5b1779f
JH
231 /* If aliases are involved, redirect edge to the actual destination and
232 possibly remove the aliases. */
233 if (e->callee != callee)
39e2db00
JH
234 {
235 struct cgraph_node *alias = e->callee, *next_alias;
236 cgraph_redirect_edge_callee (e, callee);
237 while (alias && alias != callee)
238 {
239 if (!alias->callers
6c69a029 240 && can_remove_node_now_p (alias, e))
39e2db00
JH
241 {
242 next_alias = cgraph_alias_aliased_node (alias);
243 cgraph_remove_node (alias);
244 alias = next_alias;
245 }
246 else
247 break;
248 }
249 }
a5b1779f 250
fee8b6da
JH
251 clone_inlined_nodes (e, true, update_original, overall_size);
252
fee8b6da 253 gcc_assert (curr->callee->global.inlined_to == to);
898b8927
JH
254
255 old_size = inline_summary (to)->size;
256 inline_merge_summary (e);
0f378cb5
JH
257 if (optimize)
258 new_edges_found = ipa_propagate_indirect_call_infos (curr, new_edges);
c170d40f
JH
259 if (update_overall_summary)
260 inline_update_overall_summary (to);
898b8927 261 new_size = inline_summary (to)->size;
0f378cb5
JH
262#ifdef ENABLE_CHECKING
263 /* Verify that estimated growth match real growth. Allow off-by-one
264 error due to INLINE_SIZE_SCALE roudoff errors. */
265 gcc_assert (!update_overall_summary || !overall_size
266 || abs (estimated_growth - (new_size - old_size)) <= 1
267 /* FIXME: a hack. Edges with false predicate are accounted
268 wrong, we should remove them from callgraph. */
269 || predicated);
270#endif
271
b4c0a884 272 if (overall_size)
fee8b6da
JH
273 *overall_size += new_size - old_size;
274 ncalls_inlined++;
275
25837a2f
JH
276 /* This must happen after inline_merge_summary that rely on jump
277 functions of callee to not be updated. */
0f378cb5 278 return new_edges_found;
fee8b6da
JH
279}
280
281
282/* Copy function body of NODE and redirect all inline clones to it.
283 This is done before inline plan is applied to NODE when there are
284 still some inline clones if it.
285
073a8998 286 This is necessary because inline decisions are not really transitive
fee8b6da
JH
287 and the other inline clones may have different bodies. */
288
289static struct cgraph_node *
290save_inline_function_body (struct cgraph_node *node)
291{
292 struct cgraph_node *first_clone, *n;
293
294 if (dump_file)
295 fprintf (dump_file, "\nSaving body of %s for later reuse\n",
296 cgraph_node_name (node));
297
960bfb69 298 gcc_assert (node == cgraph_get_node (node->symbol.decl));
fee8b6da
JH
299
300 /* first_clone will be turned into real function. */
301 first_clone = node->clones;
960bfb69 302 first_clone->symbol.decl = copy_node (node->symbol.decl);
1ab24192 303 symtab_insert_node_to_hashtable ((symtab_node) first_clone);
960bfb69 304 gcc_assert (first_clone == cgraph_get_node (first_clone->symbol.decl));
fee8b6da
JH
305
306 /* Now reshape the clone tree, so all other clones descends from
307 first_clone. */
308 if (first_clone->next_sibling_clone)
309 {
310 for (n = first_clone->next_sibling_clone; n->next_sibling_clone; n = n->next_sibling_clone)
311 n->clone_of = first_clone;
312 n->clone_of = first_clone;
313 n->next_sibling_clone = first_clone->clones;
314 if (first_clone->clones)
315 first_clone->clones->prev_sibling_clone = n;
316 first_clone->clones = first_clone->next_sibling_clone;
317 first_clone->next_sibling_clone->prev_sibling_clone = NULL;
318 first_clone->next_sibling_clone = NULL;
319 gcc_assert (!first_clone->prev_sibling_clone);
320 }
321 first_clone->clone_of = NULL;
322
323 /* Now node in question has no clones. */
324 node->clones = NULL;
325
1a3118e9
JH
326 /* Inline clones share decl with the function they are cloned
327 from. Walk the whole clone tree and redirect them all to the
328 new decl. */
fee8b6da
JH
329 if (first_clone->clones)
330 for (n = first_clone->clones; n != first_clone;)
331 {
960bfb69
JH
332 gcc_assert (n->symbol.decl == node->symbol.decl);
333 n->symbol.decl = first_clone->symbol.decl;
fee8b6da
JH
334 if (n->clones)
335 n = n->clones;
336 else if (n->next_sibling_clone)
337 n = n->next_sibling_clone;
338 else
339 {
340 while (n != first_clone && !n->next_sibling_clone)
341 n = n->clone_of;
342 if (n != first_clone)
343 n = n->next_sibling_clone;
344 }
345 }
346
347 /* Copy the OLD_VERSION_NODE function tree to the new version. */
960bfb69
JH
348 tree_function_versioning (node->symbol.decl, first_clone->symbol.decl,
349 NULL, true, NULL, false, NULL, NULL);
fee8b6da 350
1a3118e9
JH
351 /* The function will be short lived and removed after we inline all the clones,
352 but make it internal so we won't confuse ourself. */
960bfb69
JH
353 DECL_EXTERNAL (first_clone->symbol.decl) = 0;
354 DECL_COMDAT_GROUP (first_clone->symbol.decl) = NULL_TREE;
355 TREE_PUBLIC (first_clone->symbol.decl) = 0;
356 DECL_COMDAT (first_clone->symbol.decl) = 0;
fee8b6da
JH
357 VEC_free (ipa_opt_pass, heap,
358 first_clone->ipa_transforms_to_apply);
359 first_clone->ipa_transforms_to_apply = NULL;
360
b4e93f45
JH
361 /* When doing recursive inlining, the clone may become unnecessary.
362 This is possible i.e. in the case when the recursive function is proved to be
363 non-throwing and the recursion happens only in the EH landing pad.
364 We can not remove the clone until we are done with saving the body.
365 Remove it now. */
366 if (!first_clone->callers)
367 {
368 cgraph_remove_node_and_inline_clones (first_clone, NULL);
369 first_clone = NULL;
370 }
fee8b6da 371#ifdef ENABLE_CHECKING
b4e93f45
JH
372 else
373 verify_cgraph_node (first_clone);
fee8b6da
JH
374#endif
375 return first_clone;
376}
377
9c8305f8
JH
378/* Return true when function body of DECL still needs to be kept around
379 for later re-use. */
65d630d4 380static bool
9c8305f8
JH
381preserve_function_body_p (struct cgraph_node *node)
382{
383 gcc_assert (cgraph_global_info_ready);
384 gcc_assert (!node->alias && !node->thunk.thunk_p);
385
386 /* Look if there is any clone around. */
387 if (node->clones)
388 return true;
389 return false;
390}
fee8b6da
JH
391
392/* Apply inline plan to function. */
393
394unsigned int
395inline_transform (struct cgraph_node *node)
396{
397 unsigned int todo = 0;
398 struct cgraph_edge *e;
c9fc06dc 399
fee8b6da
JH
400 /* FIXME: Currently the pass manager is adding inline transform more than
401 once to some clones. This needs revisiting after WPA cleanups. */
402 if (cfun->after_inlining)
403 return 0;
404
405 /* We might need the body of this function so that we can expand
406 it inline somewhere else. */
9c8305f8 407 if (preserve_function_body_p (node))
fee8b6da
JH
408 save_inline_function_body (node);
409
410 for (e = node->callees; e; e = e->next_callee)
c9fc06dc
CB
411 cgraph_redirect_edge_call_stmt_to_callee (e);
412
413 timevar_push (TV_INTEGRATION);
414 if (node->callees)
55f01229 415 todo = optimize_inline_calls (current_function_decl);
c9fc06dc
CB
416 timevar_pop (TV_INTEGRATION);
417
f8698b37
RG
418 cfun->always_inline_functions_inlined = true;
419 cfun->after_inlining = true;
420 todo |= execute_fixup_cfg ();
421
55f01229
RG
422 if (!(todo & TODO_update_ssa_any))
423 /* Redirecting edges might lead to a need for vops to be recomputed. */
424 todo |= TODO_update_ssa_only_virtuals;
425
f8698b37 426 return todo;
fee8b6da 427}