]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ipa-inline-transform.c
Update Copyright years for files modified in 2011 and/or 2012.
[thirdparty/gcc.git] / gcc / ipa-inline-transform.c
1 /* Callgraph transformations to handle inlining
2 Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along 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"
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"
46 #include "tree-pass.h"
47
48 int ncalls_inlined;
49 int nfunctions_inlined;
50
51 /* Scale frequency of NODE edges by FREQ_SCALE. */
52
53 static void
54 update_noncloned_frequencies (struct cgraph_node *node,
55 int freq_scale)
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 {
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)
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;
75 }
76 }
77
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
83 static bool
84 can_remove_node_now_p_1 (struct cgraph_node *node)
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. */
89 return (!node->symbol.address_taken
90 && !ipa_ref_has_aliases_p (&node->symbol.ref_list)
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. */
96 && (!DECL_VIRTUAL_P (node->symbol.decl)
97 || (!DECL_COMDAT (node->symbol.decl)
98 && !DECL_EXTERNAL (node->symbol.decl)))
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
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
108 static bool
109 can_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. */
117 if (!node->symbol.same_comdat_group)
118 return true;
119 for (next = cgraph (node->symbol.same_comdat_group);
120 next != node; next = cgraph (next->symbol.same_comdat_group))
121 if ((next->callers && next->callers != e)
122 || !can_remove_node_now_p_1 (next))
123 return false;
124 return true;
125 }
126
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
134 void
135 clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
136 bool update_original, int *overall_size)
137 {
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
148 && can_remove_node_now_p (e->callee, e))
149 {
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. */
157 gcc_assert (!e->callee->global.inlined_to);
158 symtab_dissolve_same_comdat_group_list ((symtab_node) e->callee);
159 if (e->callee->analyzed && !DECL_EXTERNAL (e->callee->symbol.decl))
160 {
161 if (overall_size)
162 *overall_size -= inline_summary (e->callee)->size;
163 nfunctions_inlined++;
164 }
165 duplicate = false;
166 e->callee->symbol.externally_visible = false;
167 update_noncloned_frequencies (e->callee, e->frequency);
168 }
169 else
170 {
171 struct cgraph_node *n;
172 n = cgraph_clone_node (e->callee, e->callee->symbol.decl,
173 e->count, e->frequency,
174 update_original, vNULL, true);
175 cgraph_redirect_edge_callee (e, n);
176 }
177 }
178 else
179 symtab_dissolve_same_comdat_group_list ((symtab_node) e->callee);
180
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;
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
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
201 result of inlining. */
202
203 bool
204 inline_call (struct cgraph_edge *e, bool update_original,
205 vec<cgraph_edge_p> *new_edges,
206 int *overall_size, bool update_overall_summary)
207 {
208 int old_size = 0, new_size = 0;
209 struct cgraph_node *to = NULL;
210 struct cgraph_edge *curr = e;
211 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
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
218
219 /* Don't inline inlined edges. */
220 gcc_assert (e->inline_failed);
221 /* Don't even think of inlining inline clone. */
222 gcc_assert (!callee->global.inlined_to);
223
224 e->inline_failed = CIF_OK;
225 DECL_POSSIBLY_INLINED (callee->symbol.decl) = true;
226
227 to = e->caller;
228 if (to->global.inlined_to)
229 to = to->global.inlined_to;
230
231 /* If aliases are involved, redirect edge to the actual destination and
232 possibly remove the aliases. */
233 if (e->callee != callee)
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
240 && can_remove_node_now_p (alias, e))
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 }
250
251 clone_inlined_nodes (e, true, update_original, overall_size);
252
253 gcc_assert (curr->callee->global.inlined_to == to);
254
255 old_size = inline_summary (to)->size;
256 inline_merge_summary (e);
257 if (optimize)
258 new_edges_found = ipa_propagate_indirect_call_infos (curr, new_edges);
259 if (update_overall_summary)
260 inline_update_overall_summary (to);
261 new_size = inline_summary (to)->size;
262
263 #ifdef ENABLE_CHECKING
264 /* Verify that estimated growth match real growth. Allow off-by-one
265 error due to INLINE_SIZE_SCALE roudoff errors. */
266 gcc_assert (!update_overall_summary || !overall_size
267 || abs (estimated_growth - (new_size - old_size)) <= 1
268 /* FIXME: a hack. Edges with false predicate are accounted
269 wrong, we should remove them from callgraph. */
270 || predicated);
271 #endif
272
273 /* Account the change of overall unit size; external functions will be
274 removed and are thus not accounted. */
275 if (overall_size
276 && !DECL_EXTERNAL (to->symbol.decl))
277 *overall_size += new_size - old_size;
278 ncalls_inlined++;
279
280 /* This must happen after inline_merge_summary that rely on jump
281 functions of callee to not be updated. */
282 return new_edges_found;
283 }
284
285
286 /* Copy function body of NODE and redirect all inline clones to it.
287 This is done before inline plan is applied to NODE when there are
288 still some inline clones if it.
289
290 This is necessary because inline decisions are not really transitive
291 and the other inline clones may have different bodies. */
292
293 static struct cgraph_node *
294 save_inline_function_body (struct cgraph_node *node)
295 {
296 struct cgraph_node *first_clone, *n;
297
298 if (dump_file)
299 fprintf (dump_file, "\nSaving body of %s for later reuse\n",
300 cgraph_node_name (node));
301
302 gcc_assert (node == cgraph_get_node (node->symbol.decl));
303
304 /* first_clone will be turned into real function. */
305 first_clone = node->clones;
306 first_clone->symbol.decl = copy_node (node->symbol.decl);
307 symtab_insert_node_to_hashtable ((symtab_node) first_clone);
308 gcc_assert (first_clone == cgraph_get_node (first_clone->symbol.decl));
309
310 /* Now reshape the clone tree, so all other clones descends from
311 first_clone. */
312 if (first_clone->next_sibling_clone)
313 {
314 for (n = first_clone->next_sibling_clone; n->next_sibling_clone; n = n->next_sibling_clone)
315 n->clone_of = first_clone;
316 n->clone_of = first_clone;
317 n->next_sibling_clone = first_clone->clones;
318 if (first_clone->clones)
319 first_clone->clones->prev_sibling_clone = n;
320 first_clone->clones = first_clone->next_sibling_clone;
321 first_clone->next_sibling_clone->prev_sibling_clone = NULL;
322 first_clone->next_sibling_clone = NULL;
323 gcc_assert (!first_clone->prev_sibling_clone);
324 }
325 first_clone->clone_of = NULL;
326
327 /* Now node in question has no clones. */
328 node->clones = NULL;
329
330 /* Inline clones share decl with the function they are cloned
331 from. Walk the whole clone tree and redirect them all to the
332 new decl. */
333 if (first_clone->clones)
334 for (n = first_clone->clones; n != first_clone;)
335 {
336 gcc_assert (n->symbol.decl == node->symbol.decl);
337 n->symbol.decl = first_clone->symbol.decl;
338 if (n->clones)
339 n = n->clones;
340 else if (n->next_sibling_clone)
341 n = n->next_sibling_clone;
342 else
343 {
344 while (n != first_clone && !n->next_sibling_clone)
345 n = n->clone_of;
346 if (n != first_clone)
347 n = n->next_sibling_clone;
348 }
349 }
350
351 /* Copy the OLD_VERSION_NODE function tree to the new version. */
352 tree_function_versioning (node->symbol.decl, first_clone->symbol.decl,
353 NULL, true, NULL, false,
354 NULL, NULL);
355
356 /* The function will be short lived and removed after we inline all the clones,
357 but make it internal so we won't confuse ourself. */
358 DECL_EXTERNAL (first_clone->symbol.decl) = 0;
359 DECL_COMDAT_GROUP (first_clone->symbol.decl) = NULL_TREE;
360 TREE_PUBLIC (first_clone->symbol.decl) = 0;
361 DECL_COMDAT (first_clone->symbol.decl) = 0;
362 first_clone->ipa_transforms_to_apply.release ();
363
364 /* When doing recursive inlining, the clone may become unnecessary.
365 This is possible i.e. in the case when the recursive function is proved to be
366 non-throwing and the recursion happens only in the EH landing pad.
367 We can not remove the clone until we are done with saving the body.
368 Remove it now. */
369 if (!first_clone->callers)
370 {
371 cgraph_remove_node_and_inline_clones (first_clone, NULL);
372 first_clone = NULL;
373 }
374 #ifdef ENABLE_CHECKING
375 else
376 verify_cgraph_node (first_clone);
377 #endif
378 return first_clone;
379 }
380
381 /* Return true when function body of DECL still needs to be kept around
382 for later re-use. */
383 static bool
384 preserve_function_body_p (struct cgraph_node *node)
385 {
386 gcc_assert (cgraph_global_info_ready);
387 gcc_assert (!node->alias && !node->thunk.thunk_p);
388
389 /* Look if there is any clone around. */
390 if (node->clones)
391 return true;
392 return false;
393 }
394
395 /* Apply inline plan to function. */
396
397 unsigned int
398 inline_transform (struct cgraph_node *node)
399 {
400 unsigned int todo = 0;
401 struct cgraph_edge *e;
402
403 /* FIXME: Currently the pass manager is adding inline transform more than
404 once to some clones. This needs revisiting after WPA cleanups. */
405 if (cfun->after_inlining)
406 return 0;
407
408 /* We might need the body of this function so that we can expand
409 it inline somewhere else. */
410 if (preserve_function_body_p (node))
411 save_inline_function_body (node);
412
413 for (e = node->callees; e; e = e->next_callee)
414 cgraph_redirect_edge_call_stmt_to_callee (e);
415
416 timevar_push (TV_INTEGRATION);
417 if (node->callees)
418 todo = optimize_inline_calls (current_function_decl);
419 timevar_pop (TV_INTEGRATION);
420
421 cfun->always_inline_functions_inlined = true;
422 cfun->after_inlining = true;
423 todo |= execute_fixup_cfg ();
424
425 if (!(todo & TODO_update_ssa_any))
426 /* Redirecting edges might lead to a need for vops to be recomputed. */
427 todo |= TODO_update_ssa_only_virtuals;
428
429 return todo;
430 }