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