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