This patch suppresses the messages printed when the primary module is not found.
[official-gcc.git] / gcc-4_7 / gcc / ipa-inline-transform.c
blobf450bc0b1f3bdbca06f1e8c643307d63953fe8a7
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
6 This file is part of GCC.
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.
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.
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/>. */
22 /* The inline decisions are stored in callgraph in "inline plan" and
23 applied later.
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.
30 The inline plan is applied on given function body by inline_transform. */
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 "timevar.h"
40 #include "output.h"
41 #include "intl.h"
42 #include "coverage.h"
43 #include "ggc.h"
44 #include "tree-flow.h"
45 #include "ipa-prop.h"
46 #include "ipa-inline.h"
47 #include "tree-inline.h"
48 #include "tree-pass.h"
49 #include "l-ipo.h"
50 #include "auto-profile.h"
51 #include "diagnostic-core.h"
52 #include "params.h"
54 int ncalls_inlined;
55 int nfunctions_inlined;
57 /* Scale frequency of NODE edges by FREQ_SCALE. */
59 static void
60 update_noncloned_frequencies (struct cgraph_node *node,
61 int freq_scale)
63 struct cgraph_edge *e;
65 /* We do not want to ignore high loop nest after freq drops to 0. */
66 if (!freq_scale)
67 freq_scale = 1;
68 for (e = node->callees; e; e = e->next_callee)
70 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
71 if (e->frequency > CGRAPH_FREQ_MAX)
72 e->frequency = CGRAPH_FREQ_MAX;
73 if (!e->inline_failed)
74 update_noncloned_frequencies (e->callee, freq_scale);
76 for (e = node->indirect_calls; e; e = e->next_callee)
78 e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
79 if (e->frequency > CGRAPH_FREQ_MAX)
80 e->frequency = CGRAPH_FREQ_MAX;
84 /* We removed or are going to remove the last call to NODE.
85 Return true if we can and want proactively remove the NODE now.
86 This is important to do, since we want inliner to know when offline
87 copy of function was removed. */
89 static bool
90 can_remove_node_now_p_1 (struct cgraph_node *node)
92 /* FIXME: When address is taken of DECL_EXTERNAL function we still
93 can remove its offline copy, but we would need to keep unanalyzed node in
94 the callgraph so references can point to it. */
95 return (!node->address_taken
96 && !ipa_ref_has_aliases_p (&node->ref_list)
97 && cgraph_can_remove_if_no_direct_calls_p (node)
98 /* Inlining might enable more devirtualizing, so we want to remove
99 those only after all devirtualizable virtual calls are processed.
100 Lacking may edges in callgraph we just preserve them post
101 inlining. */
102 && !DECL_VIRTUAL_P (node->decl)
103 /* During early inlining some unanalyzed cgraph nodes might be in the
104 callgraph and they might reffer the function in question. */
105 && !cgraph_new_nodes);
108 /* We are going to eliminate last direct call to NODE (or alias of it) via edge E.
109 Verify that the NODE can be removed from unit and if it is contained in comdat
110 group that the whole comdat group is removable. */
112 static bool
113 can_remove_node_now_p (struct cgraph_node *node, struct cgraph_edge *e)
115 struct cgraph_node *next;
116 if (!can_remove_node_now_p_1 (node))
117 return false;
119 /* When we see same comdat group, we need to be sure that all
120 items can be removed. */
121 if (!node->same_comdat_group)
122 return true;
123 for (next = node->same_comdat_group;
124 next != node; next = next->same_comdat_group)
125 if ((next->callers && next->callers != e)
126 || !can_remove_node_now_p_1 (next))
127 return false;
128 return true;
132 /* E is expected to be an edge being inlined. Clone destination node of
133 the edge and redirect it to the new clone.
134 DUPLICATE is used for bookkeeping on whether we are actually creating new
135 clones or re-using node originally representing out-of-line function call.
138 void
139 clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
140 bool update_original, int *overall_size)
142 bool has_callsite_profile = false;
143 gcov_type callsite_total_count, callsite_max_count;
145 if (flag_auto_profile)
147 has_callsite_profile =
148 afdo_get_callsite_count (e, &callsite_total_count,
149 &callsite_max_count, true);
150 /* If the callsite is inlined in the profile-collection build,
151 i.e. the cloned callee has its separate profile, we will use
152 this separate profile to annotate the callee, and the real
153 callee body will not be affected. Thus here we need to disable
154 update_original. */
155 if (has_callsite_profile)
156 update_original = false;
159 if (duplicate)
161 /* We may eliminate the need for out-of-line copy to be output.
162 In that case just go ahead and re-use it. This is not just an
163 memory optimization. Making offline copy of fuction disappear
164 from the program will improve future decisions on inlining. */
165 if (!e->callee->callers->next_caller
166 /* Recursive inlining never wants the master clone to
167 be overwritten. */
168 && update_original
169 && can_remove_node_now_p (e->callee, e))
171 /* TODO: When callee is in a comdat group, we could remove all of it,
172 including all inline clones inlined into it. That would however
173 need small function inlining to register edge removal hook to
174 maintain the priority queue.
176 For now we keep the ohter functions in the group in program until
177 cgraph_remove_unreachable_functions gets rid of them. */
178 gcc_assert (!e->callee->global.inlined_to);
179 if (e->callee->analyzed && !DECL_EXTERNAL (e->callee->decl))
181 if (overall_size)
182 *overall_size -= inline_summary (e->callee)->size;
183 nfunctions_inlined++;
185 duplicate = false;
186 e->callee->local.externally_visible = false;
187 update_noncloned_frequencies (e->callee, e->frequency);
189 else
191 struct cgraph_node *n;
192 n = cgraph_clone_node (e->callee, e->callee->decl,
193 e->count, e->frequency,
194 update_original, NULL, true);
195 cgraph_redirect_edge_callee (e, n);
199 if (flag_auto_profile && has_callsite_profile)
201 /* The callee's total count will be non-zero if the callsite
202 was inlined in the profile-collection build, In this case,
203 the original callee may be label unlikely_executed, which
204 may prevent its callees being inlined. Thus we need to reset
205 its frequency to normal. */
206 if (e->callee->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
207 e->callee->frequency = NODE_FREQUENCY_NORMAL;
208 /* we do not have enough information to calculate the node count
209 and max_bb_count. Thus we set them to the same value to make
210 other optimizations aware that they are from cloned inline
211 instances. */
212 e->callee->count = callsite_total_count;
213 e->callee->max_bb_count = callsite_max_count;
216 if (e->caller->global.inlined_to)
217 e->callee->global.inlined_to = e->caller->global.inlined_to;
218 else
219 e->callee->global.inlined_to = e->caller;
221 /* Pessimistically assume no sharing of stack space. That is, the
222 frame size of a function is estimated as the original frame size
223 plus the sum of the frame sizes of all inlined callees. */
224 e->callee->global.inlined_to->global.estimated_stack_size +=
225 inline_summary (e->callee)->estimated_self_stack_size;
227 /* Recursively clone all bodies. */
228 for (e = e->callee->callees; e; e = e->next_callee)
229 if (!e->inline_failed)
230 clone_inlined_nodes (e, duplicate, update_original, overall_size);
233 #define MAX_INT_LENGTH 16
235 /* Return NODE's name and aux info. The output is controled by OPT_INFO
236 level. */
238 static const char *
239 cgraph_node_opt_info (struct cgraph_node *node, bool emit_mod_info)
241 char *buf;
242 size_t buf_size;
243 const char *bfd_name = lang_hooks.dwarf_name (node->decl, 0);
244 const char *mod_name = 0;
245 unsigned int mod_id = 0;
246 int funcdef_no = -1;
247 const char *primary_tag = 0;
249 if (!bfd_name)
250 bfd_name = "unknown";
252 buf_size = strlen (bfd_name) + 1;
253 if (profile_info)
254 buf_size += (MAX_INT_LENGTH + 3);
256 if (L_IPO_COMP_MODE && emit_mod_info)
258 mod_id = cgraph_get_module_id (node->decl);
259 gcc_assert (mod_id);
260 mod_name = get_module_name (mod_id);
261 primary_tag = (mod_id == primary_module_id ? "*" :"");
262 buf_size += (4 + strlen (mod_name));
263 if (PARAM_VALUE (PARAM_INLINE_DUMP_MODULE_ID))
265 struct function *func = DECL_STRUCT_FUNCTION (node->decl);
266 if (func)
267 funcdef_no = func->funcdef_no;
268 buf_size += (2 * MAX_INT_LENGTH + 1);
272 buf = (char *) xmalloc (buf_size);
274 strcpy (buf, bfd_name);
276 if (L_IPO_COMP_MODE && emit_mod_info)
278 if (PARAM_VALUE (PARAM_INLINE_DUMP_MODULE_ID))
279 sprintf (buf, "%s [%d:%d %s%s]", buf, mod_id, funcdef_no,
280 primary_tag, mod_name);
281 else
282 sprintf (buf, "%s [%s%s]", buf, primary_tag, mod_name);
285 if (profile_info)
286 sprintf (buf, "%s ("HOST_WIDEST_INT_PRINT_DEC")", buf, node->count);
287 return buf;
290 /* Return CALLER's inlined call chain. Save the cgraph_node of the ultimate
291 function that the caller is inlined to in FINAL_CALLER. */
293 static const char *
294 cgraph_node_call_chain (struct cgraph_node *caller,
295 struct cgraph_node **final_caller)
297 struct cgraph_node *node;
298 const char *via_str = " (via inline instance";
299 size_t current_string_len = strlen (via_str) + 1;
300 size_t buf_size = current_string_len;
301 char *buf = (char *) xmalloc (buf_size);
303 buf[0] = 0;
304 gcc_assert (caller->global.inlined_to != NULL);
305 strcat (buf, via_str);
306 for (node = caller; node->global.inlined_to != NULL;
307 node = node->callers->caller)
309 const char *name = cgraph_node_opt_info (node, false);
310 current_string_len += (strlen (name) + 1);
311 if (current_string_len >= buf_size)
313 buf_size = current_string_len * 2;
314 buf = (char *) xrealloc (buf, buf_size);
316 strcat (buf, " ");
317 strcat (buf, name);
319 strcat (buf, ")");
320 *final_caller = node;
321 return buf;
324 /* Dump the inline decision of EDGE to stderr. */
326 static void
327 dump_inline_decision (struct cgraph_edge *edge)
329 location_t locus;
330 const char *inline_chain_text;
331 const char *call_count_text;
332 struct cgraph_node *final_caller = edge->caller;
334 if (flag_opt_info < OPT_INFO_MED && !is_in_ipa_inline)
335 return;
336 if (final_caller->global.inlined_to != NULL)
337 inline_chain_text = cgraph_node_call_chain (final_caller, &final_caller);
338 else
339 inline_chain_text = "";
341 if (edge->count > 0)
343 const char *call_count_str = " with call count ";
344 char *buf = (char *) xmalloc (strlen (call_count_str) + MAX_INT_LENGTH);
345 sprintf (buf, "%s"HOST_WIDEST_INT_PRINT_DEC, call_count_str,
346 edge->count);
347 call_count_text = buf;
349 else
351 call_count_text = "";
354 locus = gimple_location (edge->call_stmt);
355 inform (locus, "%s inlined into %s%s%s",
356 cgraph_node_opt_info (edge->callee, true),
357 cgraph_node_opt_info (final_caller, true),
358 call_count_text,
359 inline_chain_text);
362 /* Mark edge E as inlined and update callgraph accordingly. UPDATE_ORIGINAL
363 specify whether profile of original function should be updated. If any new
364 indirect edges are discovered in the process, add them to NEW_EDGES, unless
365 it is NULL. Return true iff any new callgraph edges were discovered as a
366 result of inlining. */
368 bool
369 inline_call (struct cgraph_edge *e, bool update_original,
370 VEC (cgraph_edge_p, heap) **new_edges,
371 int *overall_size)
373 int old_size = 0, new_size = 0;
374 struct cgraph_node *to = NULL;
375 struct cgraph_edge *curr = e;
376 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
378 /* Skip fake edge. */
379 if (L_IPO_COMP_MODE && !e->call_stmt)
380 return false;
382 if (flag_opt_info >= OPT_INFO_MIN)
383 dump_inline_decision (e);
385 /* Don't inline inlined edges. */
386 gcc_assert (e->inline_failed);
387 /* Don't even think of inlining inline clone. */
388 gcc_assert (!callee->global.inlined_to);
390 e->inline_failed = CIF_OK;
391 DECL_POSSIBLY_INLINED (callee->decl) = true;
393 to = e->caller;
394 if (to->global.inlined_to)
395 to = to->global.inlined_to;
397 /* If aliases are involved, redirect edge to the actual destination and
398 possibly remove the aliases. */
399 if (e->callee != callee)
401 struct cgraph_node *alias = e->callee, *next_alias;
403 cgraph_redirect_edge_callee (e, callee);
404 while (alias && alias != callee)
406 if (!alias->callers
407 && can_remove_node_now_p (alias, e))
409 next_alias = cgraph_alias_aliased_node (alias);
410 cgraph_remove_node (alias);
411 alias = next_alias;
413 else
414 break;
418 clone_inlined_nodes (e, true, update_original, overall_size);
420 if (flag_auto_profile)
421 afdo_add_copy_scale (e);
423 gcc_assert (curr->callee->global.inlined_to == to);
425 old_size = inline_summary (to)->size;
426 inline_merge_summary (e);
427 new_size = inline_summary (to)->size;
428 if (to->max_bb_count < e->callee->max_bb_count)
429 to->max_bb_count = e->callee->max_bb_count;
430 if (overall_size)
431 *overall_size += new_size - old_size;
432 ncalls_inlined++;
434 /* This must happen after inline_merge_summary that rely on jump
435 functions of callee to not be updated. */
436 if (optimize)
437 return ipa_propagate_indirect_call_infos (curr, new_edges);
438 else
439 return false;
443 /* Copy function body of NODE and redirect all inline clones to it.
444 This is done before inline plan is applied to NODE when there are
445 still some inline clones if it.
447 This is neccesary because inline decisions are not really transitive
448 and the other inline clones may have different bodies. */
450 static struct cgraph_node *
451 save_inline_function_body (struct cgraph_node *node)
453 struct cgraph_node *first_clone, *n;
455 if (dump_file)
456 fprintf (dump_file, "\nSaving body of %s for later reuse\n",
457 cgraph_node_name (node));
459 gcc_assert (node == cgraph_get_node (node->decl));
461 /* first_clone will be turned into real function. */
462 first_clone = node->clones;
463 first_clone->decl = copy_node (node->decl);
464 cgraph_insert_node_to_hashtable (first_clone);
465 gcc_assert (first_clone == cgraph_get_node (first_clone->decl));
467 /* Now reshape the clone tree, so all other clones descends from
468 first_clone. */
469 if (first_clone->next_sibling_clone)
471 for (n = first_clone->next_sibling_clone; n->next_sibling_clone; n = n->next_sibling_clone)
472 n->clone_of = first_clone;
473 n->clone_of = first_clone;
474 n->next_sibling_clone = first_clone->clones;
475 if (first_clone->clones)
476 first_clone->clones->prev_sibling_clone = n;
477 first_clone->clones = first_clone->next_sibling_clone;
478 first_clone->next_sibling_clone->prev_sibling_clone = NULL;
479 first_clone->next_sibling_clone = NULL;
480 gcc_assert (!first_clone->prev_sibling_clone);
482 first_clone->clone_of = NULL;
484 /* Now node in question has no clones. */
485 node->clones = NULL;
487 /* Inline clones share decl with the function they are cloned
488 from. Walk the whole clone tree and redirect them all to the
489 new decl. */
490 if (first_clone->clones)
491 for (n = first_clone->clones; n != first_clone;)
493 gcc_assert (n->decl == node->decl);
494 n->decl = first_clone->decl;
495 if (n->clones)
496 n = n->clones;
497 else if (n->next_sibling_clone)
498 n = n->next_sibling_clone;
499 else
501 while (n != first_clone && !n->next_sibling_clone)
502 n = n->clone_of;
503 if (n != first_clone)
504 n = n->next_sibling_clone;
508 /* Copy the OLD_VERSION_NODE function tree to the new version. */
509 tree_function_versioning (node->decl, first_clone->decl, NULL, true, NULL,
510 false, NULL, NULL);
512 /* The function will be short lived and removed after we inline all the clones,
513 but make it internal so we won't confuse ourself. */
514 DECL_EXTERNAL (first_clone->decl) = 0;
515 DECL_COMDAT_GROUP (first_clone->decl) = NULL_TREE;
516 TREE_PUBLIC (first_clone->decl) = 0;
517 DECL_COMDAT (first_clone->decl) = 0;
518 VEC_free (ipa_opt_pass, heap,
519 first_clone->ipa_transforms_to_apply);
520 first_clone->ipa_transforms_to_apply = NULL;
522 #ifdef ENABLE_CHECKING
523 verify_cgraph_node (first_clone);
524 #endif
525 return first_clone;
529 /* Apply inline plan to function. */
531 unsigned int
532 inline_transform (struct cgraph_node *node)
534 unsigned int todo = 0;
535 struct cgraph_edge *e;
537 /* FIXME: Currently the pass manager is adding inline transform more than
538 once to some clones. This needs revisiting after WPA cleanups. */
539 if (cfun->after_inlining)
540 return 0;
542 /* We might need the body of this function so that we can expand
543 it inline somewhere else. */
544 if (cgraph_preserve_function_body_p (node))
545 save_inline_function_body (node);
547 for (e = node->callees; e; e = e->next_callee)
548 cgraph_redirect_edge_call_stmt_to_callee (e);
550 timevar_push (TV_INTEGRATION);
551 if (node->callees)
552 todo = optimize_inline_calls (current_function_decl);
553 timevar_pop (TV_INTEGRATION);
555 cfun->always_inline_functions_inlined = true;
556 cfun->after_inlining = true;
557 todo |= execute_fixup_cfg ();
559 if (!(todo & TODO_update_ssa_any))
560 /* Redirecting edges might lead to a need for vops to be recomputed. */
561 todo |= TODO_update_ssa_only_virtuals;
563 return todo;