2005-03-23 Daniel Berlin <dberlin@dberlin.org>
[official-gcc.git] / gcc / cgraphunit.c
blob6e864d5e243fcbdfd0a051543926819aa83ee02e
1 /* Callgraph based intraprocedural optimizations.
2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
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 2, or (at your option) any later
10 version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* This module implements main driver of compilation process as well as
23 few basic intraprocedural optimizers.
25 The main scope of this file is to act as an interface in between
26 tree based frontends and the backend (and middle end)
28 The front-end is supposed to use following functionality:
30 - cgraph_finalize_function
32 This function is called once front-end has parsed whole body of function
33 and it is certain that the function body nor the declaration will change.
35 (There is one exception needed for implementing GCC extern inline function.)
37 - cgraph_varpool_finalize_variable
39 This function has same behavior as the above but is used for static
40 variables.
42 - cgraph_finalize_compilation_unit
44 This function is called once compilation unit is finalized and it will
45 no longer change.
47 In the unit-at-a-time the call-graph construction and local function
48 analysis takes place here. Bodies of unreachable functions are released
49 to conserve memory usage.
51 ??? The compilation unit in this point of view should be compilation
52 unit as defined by the language - for instance C frontend allows multiple
53 compilation units to be parsed at once and it should call function each
54 time parsing is done so we save memory.
56 - cgraph_optimize
58 In this unit-at-a-time compilation the intra procedural analysis takes
59 place here. In particular the static functions whose address is never
60 taken are marked as local. Backend can then use this information to
61 modify calling conventions, do better inlining or similar optimizations.
63 - cgraph_assemble_pending_functions
64 - cgraph_varpool_assemble_pending_variables
66 In non-unit-at-a-time mode these functions can be used to force compilation
67 of functions or variables that are known to be needed at given stage
68 of compilation
70 - cgraph_mark_needed_node
71 - cgraph_varpool_mark_needed_node
73 When function or variable is referenced by some hidden way (for instance
74 via assembly code and marked by attribute "used"), the call-graph data structure
75 must be updated accordingly by this function.
77 - analyze_expr callback
79 This function is responsible for lowering tree nodes not understood by
80 generic code into understandable ones or alternatively marking
81 callgraph and varpool nodes referenced by the as needed.
83 ??? On the tree-ssa genericizing should take place here and we will avoid
84 need for these hooks (replacing them by genericizing hook)
86 - expand_function callback
88 This function is used to expand function and pass it into RTL back-end.
89 Front-end should not make any assumptions about when this function can be
90 called. In particular cgraph_assemble_pending_functions,
91 cgraph_varpool_assemble_pending_variables, cgraph_finalize_function,
92 cgraph_varpool_finalize_function, cgraph_optimize can cause arbitrarily
93 previously finalized functions to be expanded.
95 We implement two compilation modes.
97 - unit-at-a-time: In this mode analyzing of all functions is deferred
98 to cgraph_finalize_compilation_unit and expansion into cgraph_optimize.
100 In cgraph_finalize_compilation_unit the reachable functions are
101 analyzed. During analysis the call-graph edges from reachable
102 functions are constructed and their destinations are marked as
103 reachable. References to functions and variables are discovered too
104 and variables found to be needed output to the assembly file. Via
105 mark_referenced call in assemble_variable functions referenced by
106 static variables are noticed too.
108 The intra-procedural information is produced and its existence
109 indicated by global_info_ready. Once this flag is set it is impossible
110 to change function from !reachable to reachable and thus
111 assemble_variable no longer call mark_referenced.
113 Finally the call-graph is topologically sorted and all reachable functions
114 that has not been completely inlined or are not external are output.
116 ??? It is possible that reference to function or variable is optimized
117 out. We can not deal with this nicely because topological order is not
118 suitable for it. For tree-ssa we may consider another pass doing
119 optimization and re-discovering reachable functions.
121 ??? Reorganize code so variables are output very last and only if they
122 really has been referenced by produced code, so we catch more cases
123 where reference has been optimized out.
125 - non-unit-at-a-time
127 All functions are variables are output as early as possible to conserve
128 memory consumption. This may or may not result in less memory used but
129 it is still needed for some legacy code that rely on particular ordering
130 of things output from the compiler.
132 Varpool data structures are not used and variables are output directly.
134 Functions are output early using call of
135 cgraph_assemble_pending_function from cgraph_finalize_function. The
136 decision on whether function is needed is made more conservative so
137 uninlininable static functions are needed too. During the call-graph
138 construction the edge destinations are not marked as reachable and it
139 is completely relied upn assemble_variable to mark them.
141 Inlining decision heuristics
142 ??? Move this to separate file after tree-ssa merge.
144 We separate inlining decisions from the inliner itself and store it
145 inside callgraph as so called inline plan. Refer to cgraph.c
146 documentation about particular representation of inline plans in the
147 callgraph
149 The implementation of particular heuristics is separated from
150 the rest of code to make it easier to replace it with more complicated
151 implementation in the future. The rest of inlining code acts as a
152 library aimed to modify the callgraph and verify that the parameters
153 on code size growth fits.
155 To mark given call inline, use cgraph_mark_inline function, the
156 verification is performed by cgraph_default_inline_p and
157 cgraph_check_inline_limits.
159 The heuristics implements simple knapsack style algorithm ordering
160 all functions by their "profitability" (estimated by code size growth)
161 and inlining them in priority order.
163 cgraph_decide_inlining implements heuristics taking whole callgraph
164 into account, while cgraph_decide_inlining_incrementally considers
165 only one function at a time and is used in non-unit-at-a-time mode. */
168 #include "config.h"
169 #include "system.h"
170 #include "coretypes.h"
171 #include "tm.h"
172 #include "tree.h"
173 #include "rtl.h"
174 #include "tree-flow.h"
175 #include "tree-inline.h"
176 #include "langhooks.h"
177 #include "pointer-set.h"
178 #include "toplev.h"
179 #include "flags.h"
180 #include "ggc.h"
181 #include "debug.h"
182 #include "target.h"
183 #include "cgraph.h"
184 #include "diagnostic.h"
185 #include "timevar.h"
186 #include "params.h"
187 #include "fibheap.h"
188 #include "c-common.h"
189 #include "intl.h"
190 #include "function.h"
191 #include "tree-gimple.h"
193 #define INSNS_PER_CALL 10
195 static void cgraph_expand_all_functions (void);
196 static void cgraph_mark_functions_to_output (void);
197 static void cgraph_expand_function (struct cgraph_node *);
198 static tree record_call_1 (tree *, int *, void *);
199 static void cgraph_mark_local_functions (void);
200 static bool cgraph_default_inline_p (struct cgraph_node *n);
201 static void cgraph_analyze_function (struct cgraph_node *node);
202 static void cgraph_decide_inlining_incrementally (struct cgraph_node *);
204 /* Statistics we collect about inlining algorithm. */
205 static int ncalls_inlined;
206 static int nfunctions_inlined;
207 static int initial_insns;
208 static int overall_insns;
210 /* Records tree nodes seen in cgraph_create_edges. Simply using
211 walk_tree_without_duplicates doesn't guarantee each node is visited
212 once because it gets a new htab upon each recursive call from
213 record_calls_1. */
214 static struct pointer_set_t *visited_nodes;
216 static FILE *cgraph_dump_file;
218 /* Determine if function DECL is needed. That is, visible to something
219 either outside this translation unit, something magic in the system
220 configury, or (if not doing unit-at-a-time) to something we havn't
221 seen yet. */
223 static bool
224 decide_is_function_needed (struct cgraph_node *node, tree decl)
226 tree origin;
228 /* If we decided it was needed before, but at the time we didn't have
229 the body of the function available, then it's still needed. We have
230 to go back and re-check its dependencies now. */
231 if (node->needed)
232 return true;
234 /* Externally visible functions must be output. The exception is
235 COMDAT functions that must be output only when they are needed. */
236 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
237 return true;
239 /* Constructors and destructors are reachable from the runtime by
240 some mechanism. */
241 if (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl))
242 return true;
244 /* If the user told us it is used, then it must be so. */
245 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
246 return true;
248 /* ??? If the assembler name is set by hand, it is possible to assemble
249 the name later after finalizing the function and the fact is noticed
250 in assemble_name then. This is arguably a bug. */
251 if (DECL_ASSEMBLER_NAME_SET_P (decl)
252 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
253 return true;
255 if (flag_unit_at_a_time)
256 return false;
258 /* If not doing unit at a time, then we'll only defer this function
259 if its marked for inlining. Otherwise we want to emit it now. */
261 /* "extern inline" functions are never output locally. */
262 if (DECL_EXTERNAL (decl))
263 return false;
264 /* Nested functions of extern inline function shall not be emit unless
265 we inlined the origin. */
266 for (origin = decl_function_context (decl); origin;
267 origin = decl_function_context (origin))
268 if (DECL_EXTERNAL (origin))
269 return false;
270 /* We want to emit COMDAT functions only when absolutely necessary. */
271 if (DECL_COMDAT (decl))
272 return false;
273 if (!DECL_INLINE (decl)
274 || (!node->local.disregard_inline_limits
275 /* When declared inline, defer even the uninlinable functions.
276 This allows them to be eliminated when unused. */
277 && !DECL_DECLARED_INLINE_P (decl)
278 && (!node->local.inlinable || !cgraph_default_inline_p (node))))
279 return true;
281 return false;
286 /* When not doing unit-at-a-time, output all functions enqueued.
287 Return true when such a functions were found. */
289 bool
290 cgraph_assemble_pending_functions (void)
292 bool output = false;
294 if (flag_unit_at_a_time)
295 return false;
297 while (cgraph_nodes_queue)
299 struct cgraph_node *n = cgraph_nodes_queue;
301 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
302 n->next_needed = NULL;
303 if (!n->global.inlined_to && !DECL_EXTERNAL (n->decl))
305 cgraph_expand_function (n);
306 output = true;
310 return output;
313 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
314 logic in effect. If NESTED is true, then our caller cannot stand to have
315 the garbage collector run at the moment. We would need to either create
316 a new GC context, or just not compile right now. */
318 void
319 cgraph_finalize_function (tree decl, bool nested)
321 struct cgraph_node *node = cgraph_node (decl);
323 if (node->local.finalized)
325 /* As an GCC extension we allow redefinition of the function. The
326 semantics when both copies of bodies differ is not well defined.
327 We replace the old body with new body so in unit at a time mode
328 we always use new body, while in normal mode we may end up with
329 old body inlined into some functions and new body expanded and
330 inlined in others.
332 ??? It may make more sense to use one body for inlining and other
333 body for expanding the function but this is difficult to do. */
335 /* If node->output is set, then this is a unit-at-a-time compilation
336 and we have already begun whole-unit analysis. This is *not*
337 testing for whether we've already emitted the function. That
338 case can be sort-of legitimately seen with real function
339 redefinition errors. I would argue that the front end should
340 never present us with such a case, but don't enforce that for now. */
341 gcc_assert (!node->output);
343 /* Reset our data structures so we can analyze the function again. */
344 memset (&node->local, 0, sizeof (node->local));
345 memset (&node->global, 0, sizeof (node->global));
346 memset (&node->rtl, 0, sizeof (node->rtl));
347 node->analyzed = false;
348 node->local.redefined_extern_inline = true;
350 if (!flag_unit_at_a_time)
352 struct cgraph_node *n;
354 for (n = cgraph_nodes; n; n = n->next)
355 if (n->global.inlined_to == node)
356 cgraph_remove_node (n);
359 cgraph_node_remove_callees (node);
361 /* We may need to re-queue the node for assembling in case
362 we already proceeded it and ignored as not needed. */
363 if (node->reachable && !flag_unit_at_a_time)
365 struct cgraph_node *n;
367 for (n = cgraph_nodes_queue; n; n = n->next_needed)
368 if (n == node)
369 break;
370 if (!n)
371 node->reachable = 0;
375 notice_global_symbol (decl);
376 node->decl = decl;
377 node->local.finalized = true;
378 if (node->nested)
379 lower_nested_functions (decl);
380 gcc_assert (!node->nested);
382 /* If not unit at a time, then we need to create the call graph
383 now, so that called functions can be queued and emitted now. */
384 if (!flag_unit_at_a_time)
386 cgraph_analyze_function (node);
387 cgraph_decide_inlining_incrementally (node);
390 if (decide_is_function_needed (node, decl))
391 cgraph_mark_needed_node (node);
393 /* If not unit at a time, go ahead and emit everything we've found
394 to be reachable at this time. */
395 if (!nested)
397 if (!cgraph_assemble_pending_functions ())
398 ggc_collect ();
401 /* If we've not yet emitted decl, tell the debug info about it. */
402 if (!TREE_ASM_WRITTEN (decl))
403 (*debug_hooks->deferred_inline_function) (decl);
405 /* Possibly warn about unused parameters. */
406 if (warn_unused_parameter)
407 do_warn_unused_parameter (decl);
410 /* Walk tree and record all calls. Called via walk_tree. */
411 static tree
412 record_call_1 (tree *tp, int *walk_subtrees, void *data)
414 tree t = *tp;
416 switch (TREE_CODE (t))
418 case VAR_DECL:
419 /* ??? Really, we should mark this decl as *potentially* referenced
420 by this function and re-examine whether the decl is actually used
421 after rtl has been generated. */
422 if (TREE_STATIC (t))
424 cgraph_varpool_mark_needed_node (cgraph_varpool_node (t));
425 if (lang_hooks.callgraph.analyze_expr)
426 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees,
427 data);
429 break;
431 case ADDR_EXPR:
432 if (flag_unit_at_a_time)
434 /* Record dereferences to the functions. This makes the
435 functions reachable unconditionally. */
436 tree decl = TREE_OPERAND (*tp, 0);
437 if (TREE_CODE (decl) == FUNCTION_DECL)
438 cgraph_mark_needed_node (cgraph_node (decl));
440 break;
442 case CALL_EXPR:
444 tree decl = get_callee_fndecl (*tp);
445 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
447 cgraph_create_edge (data, cgraph_node (decl), *tp);
449 /* When we see a function call, we don't want to look at the
450 function reference in the ADDR_EXPR that is hanging from
451 the CALL_EXPR we're examining here, because we would
452 conclude incorrectly that the function's address could be
453 taken by something that is not a function call. So only
454 walk the function parameter list, skip the other subtrees. */
456 walk_tree (&TREE_OPERAND (*tp, 1), record_call_1, data,
457 visited_nodes);
458 *walk_subtrees = 0;
460 break;
463 default:
464 /* Save some cycles by not walking types and declaration as we
465 won't find anything useful there anyway. */
466 if (IS_TYPE_OR_DECL_P (*tp))
468 *walk_subtrees = 0;
469 break;
472 if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
473 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees, data);
474 break;
477 return NULL;
480 /* Create cgraph edges for function calls inside BODY from NODE. */
482 void
483 cgraph_create_edges (struct cgraph_node *node, tree body)
485 /* The nodes we're interested in are never shared, so walk
486 the tree ignoring duplicates. */
487 visited_nodes = pointer_set_create ();
488 walk_tree (&body, record_call_1, node, visited_nodes);
489 pointer_set_destroy (visited_nodes);
490 visited_nodes = NULL;
493 static bool error_found;
495 /* Callback of verify_cgraph_node. Check that all call_exprs have
496 cgraph nodes. */
498 static tree
499 verify_cgraph_node_1 (tree *tp, int *walk_subtrees, void *data)
501 tree t = *tp;
502 tree decl;
504 if (TREE_CODE (t) == CALL_EXPR && (decl = get_callee_fndecl (t)))
506 struct cgraph_edge *e = cgraph_edge (data, t);
507 if (e)
509 if (e->aux)
511 error ("Shared call_expr:");
512 debug_tree (t);
513 error_found = true;
515 if (e->callee->decl != cgraph_node (decl)->decl)
517 error ("Edge points to wrong declaration:");
518 debug_tree (e->callee->decl);
519 fprintf (stderr," Instead of:");
520 debug_tree (decl);
522 e->aux = (void *)1;
524 else
526 error ("Missing callgraph edge for call expr:");
527 debug_tree (t);
528 error_found = true;
532 /* Save some cycles by not walking types and declaration as we
533 won't find anything useful there anyway. */
534 if (IS_TYPE_OR_DECL_P (*tp))
535 *walk_subtrees = 0;
537 return NULL_TREE;
540 /* Verify cgraph nodes of given cgraph node. */
541 void
542 verify_cgraph_node (struct cgraph_node *node)
544 struct cgraph_edge *e;
545 struct cgraph_node *main_clone;
547 timevar_push (TV_CGRAPH_VERIFY);
548 error_found = false;
549 for (e = node->callees; e; e = e->next_callee)
550 if (e->aux)
552 error ("Aux field set for edge %s->%s",
553 cgraph_node_name (e->caller), cgraph_node_name (e->callee));
554 error_found = true;
556 for (e = node->callers; e; e = e->next_caller)
558 if (!e->inline_failed)
560 if (node->global.inlined_to
561 != (e->caller->global.inlined_to
562 ? e->caller->global.inlined_to : e->caller))
564 error ("Inlined_to pointer is wrong");
565 error_found = true;
567 if (node->callers->next_caller)
569 error ("Multiple inline callers");
570 error_found = true;
573 else
574 if (node->global.inlined_to)
576 error ("Inlined_to pointer set for noninline callers");
577 error_found = true;
580 if (!node->callers && node->global.inlined_to)
582 error ("Inlined_to pointer is set but no predecesors found");
583 error_found = true;
585 if (node->global.inlined_to == node)
587 error ("Inlined_to pointer reffers to itself");
588 error_found = true;
591 for (main_clone = cgraph_node (node->decl); main_clone;
592 main_clone = main_clone->next_clone)
593 if (main_clone == node)
594 break;
595 if (!node)
597 error ("Node not found in DECL_ASSEMBLER_NAME hash");
598 error_found = true;
601 if (node->analyzed
602 && DECL_SAVED_TREE (node->decl) && !TREE_ASM_WRITTEN (node->decl)
603 && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to))
605 walk_tree_without_duplicates (&DECL_SAVED_TREE (node->decl),
606 verify_cgraph_node_1, node);
607 for (e = node->callees; e; e = e->next_callee)
609 if (!e->aux)
611 error ("Edge %s->%s has no corresponding call_expr",
612 cgraph_node_name (e->caller),
613 cgraph_node_name (e->callee));
614 error_found = true;
616 e->aux = 0;
619 if (error_found)
621 dump_cgraph_node (stderr, node);
622 internal_error ("verify_cgraph_node failed.");
624 timevar_pop (TV_CGRAPH_VERIFY);
627 /* Verify whole cgraph structure. */
628 void
629 verify_cgraph (void)
631 struct cgraph_node *node;
633 if (sorrycount || errorcount)
634 return;
636 for (node = cgraph_nodes; node; node = node->next)
637 verify_cgraph_node (node);
640 /* Analyze the function scheduled to be output. */
641 static void
642 cgraph_analyze_function (struct cgraph_node *node)
644 tree decl = node->decl;
645 struct cgraph_edge *e;
647 current_function_decl = decl;
649 /* First kill forward declaration so reverse inlining works properly. */
650 cgraph_create_edges (node, DECL_SAVED_TREE (decl));
652 node->local.inlinable = tree_inlinable_function_p (decl);
653 node->local.self_insns = estimate_num_insns (DECL_SAVED_TREE (decl));
654 if (node->local.inlinable)
655 node->local.disregard_inline_limits
656 = lang_hooks.tree_inlining.disregard_inline_limits (decl);
657 for (e = node->callers; e; e = e->next_caller)
659 if (node->local.redefined_extern_inline)
660 e->inline_failed = N_("redefined extern inline functions are not "
661 "considered for inlining");
662 else if (!node->local.inlinable)
663 e->inline_failed = N_("function not inlinable");
664 else
665 e->inline_failed = N_("function not considered for inlining");
667 if (flag_really_no_inline && !node->local.disregard_inline_limits)
668 node->local.inlinable = 0;
669 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
670 node->global.insns = node->local.self_insns;
672 node->analyzed = true;
673 current_function_decl = NULL;
676 /* Analyze the whole compilation unit once it is parsed completely. */
678 void
679 cgraph_finalize_compilation_unit (void)
681 struct cgraph_node *node;
683 finish_aliases_1 ();
685 if (!flag_unit_at_a_time)
687 cgraph_assemble_pending_functions ();
688 return;
691 cgraph_varpool_assemble_pending_decls ();
692 if (!quiet_flag)
693 fprintf (stderr, "\nAnalyzing compilation unit\n");
695 timevar_push (TV_CGRAPH);
696 if (cgraph_dump_file)
698 fprintf (cgraph_dump_file, "Initial entry points:");
699 for (node = cgraph_nodes; node; node = node->next)
700 if (node->needed && DECL_SAVED_TREE (node->decl))
701 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
702 fprintf (cgraph_dump_file, "\n");
705 /* Propagate reachability flag and lower representation of all reachable
706 functions. In the future, lowering will introduce new functions and
707 new entry points on the way (by template instantiation and virtual
708 method table generation for instance). */
709 while (cgraph_nodes_queue)
711 struct cgraph_edge *edge;
712 tree decl = cgraph_nodes_queue->decl;
714 node = cgraph_nodes_queue;
715 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
716 node->next_needed = NULL;
718 /* ??? It is possible to create extern inline function and later using
719 weak alas attribute to kill its body. See
720 gcc.c-torture/compile/20011119-1.c */
721 if (!DECL_SAVED_TREE (decl))
722 continue;
724 gcc_assert (!node->analyzed && node->reachable);
725 gcc_assert (DECL_SAVED_TREE (decl));
727 cgraph_analyze_function (node);
729 for (edge = node->callees; edge; edge = edge->next_callee)
730 if (!edge->callee->reachable)
731 cgraph_mark_reachable_node (edge->callee);
733 cgraph_varpool_assemble_pending_decls ();
736 /* Collect entry points to the unit. */
738 if (cgraph_dump_file)
740 fprintf (cgraph_dump_file, "Unit entry points:");
741 for (node = cgraph_nodes; node; node = node->next)
742 if (node->needed && DECL_SAVED_TREE (node->decl))
743 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
744 fprintf (cgraph_dump_file, "\n\nInitial ");
745 dump_cgraph (cgraph_dump_file);
748 if (cgraph_dump_file)
749 fprintf (cgraph_dump_file, "\nReclaiming functions:");
751 for (node = cgraph_nodes; node; node = node->next)
753 tree decl = node->decl;
755 if (!node->reachable && DECL_SAVED_TREE (decl))
757 if (cgraph_dump_file)
758 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
759 cgraph_remove_node (node);
761 else
762 node->next_needed = NULL;
764 if (cgraph_dump_file)
766 fprintf (cgraph_dump_file, "\n\nReclaimed ");
767 dump_cgraph (cgraph_dump_file);
769 ggc_collect ();
770 timevar_pop (TV_CGRAPH);
772 /* Figure out what functions we want to assemble. */
774 static void
775 cgraph_mark_functions_to_output (void)
777 struct cgraph_node *node;
779 for (node = cgraph_nodes; node; node = node->next)
781 tree decl = node->decl;
782 struct cgraph_edge *e;
784 gcc_assert (!node->output);
786 for (e = node->callers; e; e = e->next_caller)
787 if (e->inline_failed)
788 break;
790 /* We need to output all local functions that are used and not
791 always inlined, as well as those that are reachable from
792 outside the current compilation unit. */
793 if (DECL_SAVED_TREE (decl)
794 && !node->global.inlined_to
795 && (node->needed
796 || (e && node->reachable))
797 && !TREE_ASM_WRITTEN (decl)
798 && !DECL_EXTERNAL (decl))
799 node->output = 1;
800 else
802 /* We should've reclaimed all functions that are not needed. */
803 #ifdef ENABLE_CHECKING
804 if (!node->global.inlined_to && DECL_SAVED_TREE (decl)
805 && !DECL_EXTERNAL (decl))
807 dump_cgraph_node (stderr, node);
808 internal_error ("failed to reclaim unneeded function");
810 #endif
811 gcc_assert (node->global.inlined_to || !DECL_SAVED_TREE (decl)
812 || DECL_EXTERNAL (decl));
819 /* Expand function specified by NODE. */
821 static void
822 cgraph_expand_function (struct cgraph_node *node)
824 tree decl = node->decl;
826 /* We ought to not compile any inline clones. */
827 gcc_assert (!node->global.inlined_to);
829 if (flag_unit_at_a_time)
830 announce_function (decl);
832 /* Generate RTL for the body of DECL. */
833 lang_hooks.callgraph.expand_function (decl);
835 /* Make sure that BE didn't give up on compiling. */
836 /* ??? Can happen with nested function of extern inline. */
837 gcc_assert (TREE_ASM_WRITTEN (node->decl));
839 current_function_decl = NULL;
840 if (!cgraph_preserve_function_body_p (node->decl))
842 DECL_SAVED_TREE (node->decl) = NULL;
843 DECL_STRUCT_FUNCTION (node->decl) = NULL;
844 DECL_INITIAL (node->decl) = error_mark_node;
845 /* Eliminate all call edges. This is important so the call_expr no longer
846 points to the dead function body. */
847 cgraph_node_remove_callees (node);
851 /* Fill array order with all nodes with output flag set in the reverse
852 topological order. */
854 static int
855 cgraph_postorder (struct cgraph_node **order)
857 struct cgraph_node *node, *node2;
858 int stack_size = 0;
859 int order_pos = 0;
860 struct cgraph_edge *edge, last;
862 struct cgraph_node **stack =
863 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
865 /* We have to deal with cycles nicely, so use a depth first traversal
866 output algorithm. Ignore the fact that some functions won't need
867 to be output and put them into order as well, so we get dependencies
868 right through intline functions. */
869 for (node = cgraph_nodes; node; node = node->next)
870 node->aux = NULL;
871 for (node = cgraph_nodes; node; node = node->next)
872 if (!node->aux)
874 node2 = node;
875 if (!node->callers)
876 node->aux = &last;
877 else
878 node->aux = node->callers;
879 while (node2)
881 while (node2->aux != &last)
883 edge = node2->aux;
884 if (edge->next_caller)
885 node2->aux = edge->next_caller;
886 else
887 node2->aux = &last;
888 if (!edge->caller->aux)
890 if (!edge->caller->callers)
891 edge->caller->aux = &last;
892 else
893 edge->caller->aux = edge->caller->callers;
894 stack[stack_size++] = node2;
895 node2 = edge->caller;
896 break;
899 if (node2->aux == &last)
901 order[order_pos++] = node2;
902 if (stack_size)
903 node2 = stack[--stack_size];
904 else
905 node2 = NULL;
909 free (stack);
910 return order_pos;
914 /* Perform reachability analysis and reclaim all unreachable nodes.
915 This function also remove unneeded bodies of extern inline functions
916 and thus needs to be done only after inlining decisions has been made. */
917 static bool
918 cgraph_remove_unreachable_nodes (void)
920 struct cgraph_node *first = (void *) 1;
921 struct cgraph_node *node;
922 bool changed = false;
923 int insns = 0;
925 #ifdef ENABLE_CHECKING
926 verify_cgraph ();
927 #endif
928 if (cgraph_dump_file)
929 fprintf (cgraph_dump_file, "\nReclaiming functions:");
930 #ifdef ENABLE_CHECKING
931 for (node = cgraph_nodes; node; node = node->next)
932 gcc_assert (!node->aux);
933 #endif
934 for (node = cgraph_nodes; node; node = node->next)
935 if (node->needed && !node->global.inlined_to
936 && (!DECL_EXTERNAL (node->decl) || !node->analyzed))
938 node->aux = first;
939 first = node;
941 else
942 gcc_assert (!node->aux);
944 /* Perform reachability analysis. As a special case do not consider
945 extern inline functions not inlined as live because we won't output
946 them at all. */
947 while (first != (void *) 1)
949 struct cgraph_edge *e;
950 node = first;
951 first = first->aux;
953 for (e = node->callees; e; e = e->next_callee)
954 if (!e->callee->aux
955 && node->analyzed
956 && (!e->inline_failed || !e->callee->analyzed
957 || !DECL_EXTERNAL (e->callee->decl)))
959 e->callee->aux = first;
960 first = e->callee;
964 /* Remove unreachable nodes. Extern inline functions need special care;
965 Unreachable extern inline functions shall be removed.
966 Reachable extern inline functions we never inlined shall get their bodies
967 eliminated.
968 Reachable extern inline functions we sometimes inlined will be turned into
969 unanalyzed nodes so they look like for true extern functions to the rest
970 of code. Body of such functions is released via remove_node once the
971 inline clones are eliminated. */
972 for (node = cgraph_nodes; node; node = node->next)
974 if (!node->aux)
976 int local_insns;
977 tree decl = node->decl;
979 node->global.inlined_to = NULL;
980 if (DECL_STRUCT_FUNCTION (decl))
981 local_insns = node->local.self_insns;
982 else
983 local_insns = 0;
984 if (cgraph_dump_file)
985 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
986 if (!node->analyzed || !DECL_EXTERNAL (node->decl))
987 cgraph_remove_node (node);
988 else
990 struct cgraph_edge *e;
992 for (e = node->callers; e; e = e->next_caller)
993 if (e->caller->aux)
994 break;
995 if (e || node->needed)
997 struct cgraph_node *clone;
999 for (clone = node->next_clone; clone;
1000 clone = clone->next_clone)
1001 if (clone->aux)
1002 break;
1003 if (!clone)
1005 DECL_SAVED_TREE (node->decl) = NULL;
1006 DECL_STRUCT_FUNCTION (node->decl) = NULL;
1007 DECL_INITIAL (node->decl) = error_mark_node;
1009 cgraph_node_remove_callees (node);
1010 node->analyzed = false;
1012 else
1013 cgraph_remove_node (node);
1015 if (!DECL_SAVED_TREE (decl))
1016 insns += local_insns;
1017 changed = true;
1020 for (node = cgraph_nodes; node; node = node->next)
1021 node->aux = NULL;
1022 if (cgraph_dump_file)
1023 fprintf (cgraph_dump_file, "\nReclaimed %i insns", insns);
1024 return changed;
1027 /* Estimate size of the function after inlining WHAT into TO. */
1029 static int
1030 cgraph_estimate_size_after_inlining (int times, struct cgraph_node *to,
1031 struct cgraph_node *what)
1033 tree fndecl = what->decl;
1034 tree arg;
1035 int call_insns = PARAM_VALUE (PARAM_INLINE_CALL_COST);
1036 for (arg = DECL_ARGUMENTS (fndecl); arg; arg = TREE_CHAIN (arg))
1037 call_insns += estimate_move_cost (TREE_TYPE (arg));
1038 return (what->global.insns - call_insns) * times + to->global.insns;
1041 /* Estimate the growth caused by inlining NODE into all callees. */
1043 static int
1044 cgraph_estimate_growth (struct cgraph_node *node)
1046 int growth = 0;
1047 struct cgraph_edge *e;
1049 for (e = node->callers; e; e = e->next_caller)
1050 if (e->inline_failed)
1051 growth += (cgraph_estimate_size_after_inlining (1, e->caller, node)
1052 - e->caller->global.insns);
1054 /* ??? Wrong for self recursive functions or cases where we decide to not
1055 inline for different reasons, but it is not big deal as in that case
1056 we will keep the body around, but we will also avoid some inlining. */
1057 if (!node->needed && !DECL_EXTERNAL (node->decl))
1058 growth -= node->global.insns;
1060 return growth;
1063 /* E is expected to be an edge being inlined. Clone destination node of
1064 the edge and redirect it to the new clone.
1065 DUPLICATE is used for bookkeeping on whether we are actually creating new
1066 clones or re-using node originally representing out-of-line function call.
1068 void
1069 cgraph_clone_inlined_nodes (struct cgraph_edge *e, bool duplicate)
1071 struct cgraph_node *n;
1073 /* We may eliminate the need for out-of-line copy to be output. In that
1074 case just go ahead and re-use it. */
1075 if (!e->callee->callers->next_caller
1076 && (!e->callee->needed || DECL_EXTERNAL (e->callee->decl))
1077 && duplicate
1078 && flag_unit_at_a_time)
1080 gcc_assert (!e->callee->global.inlined_to);
1081 if (!DECL_EXTERNAL (e->callee->decl))
1082 overall_insns -= e->callee->global.insns, nfunctions_inlined++;
1083 duplicate = 0;
1085 else if (duplicate)
1087 n = cgraph_clone_node (e->callee);
1088 cgraph_redirect_edge_callee (e, n);
1091 if (e->caller->global.inlined_to)
1092 e->callee->global.inlined_to = e->caller->global.inlined_to;
1093 else
1094 e->callee->global.inlined_to = e->caller;
1096 /* Recursively clone all bodies. */
1097 for (e = e->callee->callees; e; e = e->next_callee)
1098 if (!e->inline_failed)
1099 cgraph_clone_inlined_nodes (e, duplicate);
1102 /* Mark edge E as inlined and update callgraph accordingly. */
1104 void
1105 cgraph_mark_inline_edge (struct cgraph_edge *e)
1107 int old_insns = 0, new_insns = 0;
1108 struct cgraph_node *to = NULL, *what;
1110 gcc_assert (e->inline_failed);
1111 e->inline_failed = NULL;
1113 if (!e->callee->global.inlined && flag_unit_at_a_time)
1114 DECL_POSSIBLY_INLINED (e->callee->decl) = true;
1115 e->callee->global.inlined = true;
1117 cgraph_clone_inlined_nodes (e, true);
1119 what = e->callee;
1121 /* Now update size of caller and all functions caller is inlined into. */
1122 for (;e && !e->inline_failed; e = e->caller->callers)
1124 old_insns = e->caller->global.insns;
1125 new_insns = cgraph_estimate_size_after_inlining (1, e->caller,
1126 what);
1127 gcc_assert (new_insns >= 0);
1128 to = e->caller;
1129 to->global.insns = new_insns;
1131 gcc_assert (what->global.inlined_to == to);
1132 if (new_insns > old_insns)
1133 overall_insns += new_insns - old_insns;
1134 ncalls_inlined++;
1137 /* Mark all calls of EDGE->CALLEE inlined into EDGE->CALLER.
1138 Return following unredirected edge in the list of callers
1139 of EDGE->CALLEE */
1141 static struct cgraph_edge *
1142 cgraph_mark_inline (struct cgraph_edge *edge)
1144 struct cgraph_node *to = edge->caller;
1145 struct cgraph_node *what = edge->callee;
1146 struct cgraph_edge *e, *next;
1147 int times = 0;
1149 /* Look for all calls, mark them inline and clone recursively
1150 all inlined functions. */
1151 for (e = what->callers; e; e = next)
1153 next = e->next_caller;
1154 if (e->caller == to && e->inline_failed)
1156 cgraph_mark_inline_edge (e);
1157 if (e == edge)
1158 edge = next;
1159 times++;
1162 gcc_assert (times);
1163 return edge;
1166 /* Return false when inlining WHAT into TO is not good idea
1167 as it would cause too large growth of function bodies. */
1169 static bool
1170 cgraph_check_inline_limits (struct cgraph_node *to, struct cgraph_node *what,
1171 const char **reason)
1173 int times = 0;
1174 struct cgraph_edge *e;
1175 int newsize;
1176 int limit;
1178 if (to->global.inlined_to)
1179 to = to->global.inlined_to;
1181 for (e = to->callees; e; e = e->next_callee)
1182 if (e->callee == what)
1183 times++;
1185 /* When inlining large function body called once into small function,
1186 take the inlined function as base for limiting the growth. */
1187 if (to->local.self_insns > what->local.self_insns)
1188 limit = to->local.self_insns;
1189 else
1190 limit = what->local.self_insns;
1192 limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
1194 newsize = cgraph_estimate_size_after_inlining (times, to, what);
1195 if (newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
1196 && newsize > limit)
1198 if (reason)
1199 *reason = N_("--param large-function-growth limit reached");
1200 return false;
1202 return true;
1205 /* Return true when function N is small enough to be inlined. */
1207 static bool
1208 cgraph_default_inline_p (struct cgraph_node *n)
1210 if (!DECL_INLINE (n->decl) || !DECL_SAVED_TREE (n->decl))
1211 return false;
1212 if (DECL_DECLARED_INLINE_P (n->decl))
1213 return n->global.insns < MAX_INLINE_INSNS_SINGLE;
1214 else
1215 return n->global.insns < MAX_INLINE_INSNS_AUTO;
1218 /* Return true when inlining WHAT would create recursive inlining.
1219 We call recursive inlining all cases where same function appears more than
1220 once in the single recursion nest path in the inline graph. */
1222 static bool
1223 cgraph_recursive_inlining_p (struct cgraph_node *to,
1224 struct cgraph_node *what,
1225 const char **reason)
1227 bool recursive;
1228 if (to->global.inlined_to)
1229 recursive = what->decl == to->global.inlined_to->decl;
1230 else
1231 recursive = what->decl == to->decl;
1232 /* Marking recursive function inline has sane semantic and thus we should
1233 not warn on it. */
1234 if (recursive && reason)
1235 *reason = (what->local.disregard_inline_limits
1236 ? N_("recursive inlining") : "");
1237 return recursive;
1240 /* Recompute heap nodes for each of callees. */
1241 static void
1242 update_callee_keys (fibheap_t heap, struct fibnode **heap_node,
1243 struct cgraph_node *node)
1245 struct cgraph_edge *e;
1247 for (e = node->callees; e; e = e->next_callee)
1248 if (e->inline_failed && heap_node[e->callee->uid])
1249 fibheap_replace_key (heap, heap_node[e->callee->uid],
1250 cgraph_estimate_growth (e->callee));
1251 else if (!e->inline_failed)
1252 update_callee_keys (heap, heap_node, e->callee);
1255 /* Enqueue all recursive calls from NODE into queue linked via aux pointers
1256 in between FIRST and LAST. WHERE is used for bookkeeping while looking
1257 int calls inlined within NODE. */
1258 static void
1259 lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
1260 struct cgraph_edge **first, struct cgraph_edge **last)
1262 struct cgraph_edge *e;
1263 for (e = where->callees; e; e = e->next_callee)
1264 if (e->callee == node)
1266 if (!*first)
1267 *first = e;
1268 else
1269 (*last)->aux = e;
1270 *last = e;
1272 for (e = where->callees; e; e = e->next_callee)
1273 if (!e->inline_failed)
1274 lookup_recursive_calls (node, e->callee, first, last);
1277 /* Decide on recursive inlining: in the case function has recursive calls,
1278 inline until body size reaches given argument. */
1279 static void
1280 cgraph_decide_recursive_inlining (struct cgraph_node *node)
1282 int limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO);
1283 int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO);
1284 struct cgraph_edge *first_call = NULL, *last_call = NULL;
1285 struct cgraph_edge *last_in_current_depth;
1286 struct cgraph_edge *e;
1287 struct cgraph_node *master_clone;
1288 int depth = 0;
1289 int n = 0;
1291 if (DECL_DECLARED_INLINE_P (node->decl))
1293 limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE);
1294 max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH);
1297 /* Make sure that function is small enough to be considered for inlining. */
1298 if (!max_depth
1299 || cgraph_estimate_size_after_inlining (1, node, node) >= limit)
1300 return;
1301 lookup_recursive_calls (node, node, &first_call, &last_call);
1302 if (!first_call)
1303 return;
1305 if (cgraph_dump_file)
1306 fprintf (cgraph_dump_file,
1307 "\nPerforming recursive inlining on %s\n",
1308 cgraph_node_name (node));
1310 /* We need original clone to copy around. */
1311 master_clone = cgraph_clone_node (node);
1312 master_clone->needed = true;
1313 for (e = master_clone->callees; e; e = e->next_callee)
1314 if (!e->inline_failed)
1315 cgraph_clone_inlined_nodes (e, true);
1317 /* Do the inlining and update list of recursive call during process. */
1318 last_in_current_depth = last_call;
1319 while (first_call
1320 && cgraph_estimate_size_after_inlining (1, node, master_clone) <= limit)
1322 struct cgraph_edge *curr = first_call;
1324 first_call = first_call->aux;
1325 curr->aux = NULL;
1327 cgraph_redirect_edge_callee (curr, master_clone);
1328 cgraph_mark_inline_edge (curr);
1329 lookup_recursive_calls (node, curr->callee, &first_call, &last_call);
1331 if (last_in_current_depth
1332 && ++depth >= max_depth)
1333 break;
1334 n++;
1337 /* Cleanup queue pointers. */
1338 while (first_call)
1340 struct cgraph_edge *next = first_call->aux;
1341 first_call->aux = NULL;
1342 first_call = next;
1344 if (cgraph_dump_file)
1345 fprintf (cgraph_dump_file,
1346 "\n Inlined %i times, body grown from %i to %i insns\n", n,
1347 master_clone->global.insns, node->global.insns);
1349 /* Remove master clone we used for inlining. We rely that clones inlined
1350 into master clone gets queued just before master clone so we don't
1351 need recursion. */
1352 for (node = cgraph_nodes; node != master_clone;
1353 node = node->next)
1354 if (node->global.inlined_to == master_clone)
1355 cgraph_remove_node (node);
1356 cgraph_remove_node (master_clone);
1359 /* Set inline_failed for all callers of given function to REASON. */
1361 static void
1362 cgraph_set_inline_failed (struct cgraph_node *node, const char *reason)
1364 struct cgraph_edge *e;
1366 if (cgraph_dump_file)
1367 fprintf (cgraph_dump_file, "Inlining failed: %s\n", reason);
1368 for (e = node->callers; e; e = e->next_caller)
1369 if (e->inline_failed)
1370 e->inline_failed = reason;
1373 /* We use greedy algorithm for inlining of small functions:
1374 All inline candidates are put into prioritized heap based on estimated
1375 growth of the overall number of instructions and then update the estimates.
1377 INLINED and INLINED_CALEES are just pointers to arrays large enough
1378 to be passed to cgraph_inlined_into and cgraph_inlined_callees. */
1380 static void
1381 cgraph_decide_inlining_of_small_functions (void)
1383 struct cgraph_node *node;
1384 fibheap_t heap = fibheap_new ();
1385 struct fibnode **heap_node =
1386 xcalloc (cgraph_max_uid, sizeof (struct fibnode *));
1387 int max_insns = ((HOST_WIDEST_INT) initial_insns
1388 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
1390 /* Put all inline candidates into the heap. */
1392 for (node = cgraph_nodes; node; node = node->next)
1394 if (!node->local.inlinable || !node->callers
1395 || node->local.disregard_inline_limits)
1396 continue;
1398 if (!cgraph_default_inline_p (node))
1400 cgraph_set_inline_failed (node,
1401 N_("--param max-inline-insns-single limit reached"));
1402 continue;
1404 heap_node[node->uid] =
1405 fibheap_insert (heap, cgraph_estimate_growth (node), node);
1408 if (cgraph_dump_file)
1409 fprintf (cgraph_dump_file, "\nDeciding on smaller functions:\n");
1410 while (overall_insns <= max_insns && (node = fibheap_extract_min (heap)))
1412 struct cgraph_edge *e, *next;
1413 int old_insns = overall_insns;
1415 heap_node[node->uid] = NULL;
1416 if (cgraph_dump_file)
1417 fprintf (cgraph_dump_file,
1418 "\nConsidering %s with %i insns\n"
1419 " Estimated growth is %+i insns.\n",
1420 cgraph_node_name (node), node->global.insns,
1421 cgraph_estimate_growth (node));
1422 if (!cgraph_default_inline_p (node))
1424 cgraph_set_inline_failed (node,
1425 N_("--param max-inline-insns-single limit reached after inlining into the callee"));
1426 continue;
1428 for (e = node->callers; e; e = next)
1430 next = e->next_caller;
1431 if (e->inline_failed)
1433 struct cgraph_node *where;
1435 if (cgraph_recursive_inlining_p (e->caller, e->callee,
1436 &e->inline_failed)
1437 || !cgraph_check_inline_limits (e->caller, e->callee,
1438 &e->inline_failed))
1440 if (cgraph_dump_file)
1441 fprintf (cgraph_dump_file, " Not inlining into %s:%s.\n",
1442 cgraph_node_name (e->caller), e->inline_failed);
1443 continue;
1445 next = cgraph_mark_inline (e);
1446 where = e->caller;
1447 if (where->global.inlined_to)
1448 where = where->global.inlined_to;
1450 if (heap_node[where->uid])
1451 fibheap_replace_key (heap, heap_node[where->uid],
1452 cgraph_estimate_growth (where));
1454 if (cgraph_dump_file)
1455 fprintf (cgraph_dump_file,
1456 " Inlined into %s which now has %i insns.\n",
1457 cgraph_node_name (e->caller),
1458 e->caller->global.insns);
1462 cgraph_decide_recursive_inlining (node);
1464 /* Similarly all functions called by the function we just inlined
1465 are now called more times; update keys. */
1466 update_callee_keys (heap, heap_node, node);
1468 if (cgraph_dump_file)
1469 fprintf (cgraph_dump_file,
1470 " Inlined for a net change of %+i insns.\n",
1471 overall_insns - old_insns);
1473 while ((node = fibheap_extract_min (heap)) != NULL)
1474 if (!node->local.disregard_inline_limits)
1475 cgraph_set_inline_failed (node, N_("--param inline-unit-growth limit reached"));
1476 fibheap_delete (heap);
1477 free (heap_node);
1480 /* Decide on the inlining. We do so in the topological order to avoid
1481 expenses on updating data structures. */
1483 static void
1484 cgraph_decide_inlining (void)
1486 struct cgraph_node *node;
1487 int nnodes;
1488 struct cgraph_node **order =
1489 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
1490 int old_insns = 0;
1491 int i;
1493 for (node = cgraph_nodes; node; node = node->next)
1494 initial_insns += node->local.self_insns;
1495 overall_insns = initial_insns;
1497 nnodes = cgraph_postorder (order);
1499 if (cgraph_dump_file)
1500 fprintf (cgraph_dump_file,
1501 "\nDeciding on inlining. Starting with %i insns.\n",
1502 initial_insns);
1504 for (node = cgraph_nodes; node; node = node->next)
1505 node->aux = 0;
1507 if (cgraph_dump_file)
1508 fprintf (cgraph_dump_file, "\nInlining always_inline functions:\n");
1510 /* In the first pass mark all always_inline edges. Do this with a priority
1511 so none of our later choices will make this impossible. */
1512 for (i = nnodes - 1; i >= 0; i--)
1514 struct cgraph_edge *e, *next;
1516 node = order[i];
1518 if (!node->local.disregard_inline_limits)
1519 continue;
1520 if (cgraph_dump_file)
1521 fprintf (cgraph_dump_file,
1522 "\nConsidering %s %i insns (always inline)\n",
1523 cgraph_node_name (node), node->global.insns);
1524 old_insns = overall_insns;
1525 for (e = node->callers; e; e = next)
1527 next = e->next_caller;
1528 if (!e->inline_failed)
1529 continue;
1530 if (cgraph_recursive_inlining_p (e->caller, e->callee,
1531 &e->inline_failed))
1532 continue;
1533 cgraph_mark_inline_edge (e);
1534 if (cgraph_dump_file)
1535 fprintf (cgraph_dump_file,
1536 " Inlined into %s which now has %i insns.\n",
1537 cgraph_node_name (e->caller),
1538 e->caller->global.insns);
1540 if (cgraph_dump_file)
1541 fprintf (cgraph_dump_file,
1542 " Inlined for a net change of %+i insns.\n",
1543 overall_insns - old_insns);
1546 if (!flag_really_no_inline)
1548 cgraph_decide_inlining_of_small_functions ();
1550 if (cgraph_dump_file)
1551 fprintf (cgraph_dump_file, "\nDeciding on functions called once:\n");
1553 /* And finally decide what functions are called once. */
1555 for (i = nnodes - 1; i >= 0; i--)
1557 node = order[i];
1559 if (node->callers && !node->callers->next_caller && !node->needed
1560 && node->local.inlinable && node->callers->inline_failed
1561 && !DECL_EXTERNAL (node->decl) && !DECL_COMDAT (node->decl))
1563 bool ok = true;
1564 struct cgraph_node *node1;
1566 /* Verify that we won't duplicate the caller. */
1567 for (node1 = node->callers->caller;
1568 node1->callers && !node1->callers->inline_failed
1569 && ok; node1 = node1->callers->caller)
1570 if (node1->callers->next_caller || node1->needed)
1571 ok = false;
1572 if (ok)
1574 if (cgraph_dump_file)
1575 fprintf (cgraph_dump_file,
1576 "\nConsidering %s %i insns.\n"
1577 " Called once from %s %i insns.\n",
1578 cgraph_node_name (node), node->global.insns,
1579 cgraph_node_name (node->callers->caller),
1580 node->callers->caller->global.insns);
1582 old_insns = overall_insns;
1584 if (cgraph_check_inline_limits (node->callers->caller, node,
1585 NULL))
1587 cgraph_mark_inline (node->callers);
1588 if (cgraph_dump_file)
1589 fprintf (cgraph_dump_file,
1590 " Inlined into %s which now has %i insns"
1591 " for a net change of %+i insns.\n",
1592 cgraph_node_name (node->callers->caller),
1593 node->callers->caller->global.insns,
1594 overall_insns - old_insns);
1596 else
1598 if (cgraph_dump_file)
1599 fprintf (cgraph_dump_file,
1600 " Inline limit reached, not inlined.\n");
1607 /* We will never output extern functions we didn't inline.
1608 ??? Perhaps we can prevent accounting of growth of external
1609 inline functions. */
1610 cgraph_remove_unreachable_nodes ();
1612 if (cgraph_dump_file)
1613 fprintf (cgraph_dump_file,
1614 "\nInlined %i calls, eliminated %i functions, "
1615 "%i insns turned to %i insns.\n\n",
1616 ncalls_inlined, nfunctions_inlined, initial_insns,
1617 overall_insns);
1618 free (order);
1621 /* Decide on the inlining. We do so in the topological order to avoid
1622 expenses on updating data structures. */
1624 static void
1625 cgraph_decide_inlining_incrementally (struct cgraph_node *node)
1627 struct cgraph_edge *e;
1629 /* First of all look for always inline functions. */
1630 for (e = node->callees; e; e = e->next_callee)
1631 if (e->callee->local.disregard_inline_limits
1632 && e->inline_failed
1633 && !cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed)
1634 /* ??? It is possible that renaming variable removed the function body
1635 in duplicate_decls. See gcc.c-torture/compile/20011119-2.c */
1636 && DECL_SAVED_TREE (e->callee->decl))
1637 cgraph_mark_inline (e);
1639 /* Now do the automatic inlining. */
1640 if (!flag_really_no_inline)
1641 for (e = node->callees; e; e = e->next_callee)
1642 if (e->callee->local.inlinable
1643 && e->inline_failed
1644 && !e->callee->local.disregard_inline_limits
1645 && !cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed)
1646 && cgraph_check_inline_limits (node, e->callee, &e->inline_failed)
1647 && DECL_SAVED_TREE (e->callee->decl))
1649 if (cgraph_default_inline_p (e->callee))
1650 cgraph_mark_inline (e);
1651 else
1652 e->inline_failed
1653 = N_("--param max-inline-insns-single limit reached");
1658 /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */
1660 bool
1661 cgraph_inline_p (struct cgraph_edge *e, const char **reason)
1663 *reason = e->inline_failed;
1664 return !e->inline_failed;
1669 /* Expand all functions that must be output.
1671 Attempt to topologically sort the nodes so function is output when
1672 all called functions are already assembled to allow data to be
1673 propagated across the callgraph. Use a stack to get smaller distance
1674 between a function and its callees (later we may choose to use a more
1675 sophisticated algorithm for function reordering; we will likely want
1676 to use subsections to make the output functions appear in top-down
1677 order). */
1679 static void
1680 cgraph_expand_all_functions (void)
1682 struct cgraph_node *node;
1683 struct cgraph_node **order =
1684 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
1685 int order_pos = 0, new_order_pos = 0;
1686 int i;
1688 order_pos = cgraph_postorder (order);
1689 gcc_assert (order_pos == cgraph_n_nodes);
1691 /* Garbage collector may remove inline clones we eliminate during
1692 optimization. So we must be sure to not reference them. */
1693 for (i = 0; i < order_pos; i++)
1694 if (order[i]->output)
1695 order[new_order_pos++] = order[i];
1697 for (i = new_order_pos - 1; i >= 0; i--)
1699 node = order[i];
1700 if (node->output)
1702 gcc_assert (node->reachable);
1703 node->output = 0;
1704 cgraph_expand_function (node);
1707 free (order);
1710 /* Mark all local functions.
1712 A local function is one whose calls can occur only in the current
1713 compilation unit and all its calls are explicit, so we can change
1714 its calling convention. We simply mark all static functions whose
1715 address is not taken as local. */
1717 static void
1718 cgraph_mark_local_functions (void)
1720 struct cgraph_node *node;
1722 /* Figure out functions we want to assemble. */
1723 for (node = cgraph_nodes; node; node = node->next)
1725 node->local.local = (!node->needed
1726 && DECL_SAVED_TREE (node->decl)
1727 && !TREE_PUBLIC (node->decl));
1730 if (cgraph_dump_file)
1732 fprintf (cgraph_dump_file, "\nMarking local functions:");
1733 for (node = cgraph_nodes; node; node = node->next)
1734 if (node->local.local)
1735 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
1736 fprintf (cgraph_dump_file, "\n\n");
1740 /* Return true when function body of DECL still needs to be kept around
1741 for later re-use. */
1742 bool
1743 cgraph_preserve_function_body_p (tree decl)
1745 struct cgraph_node *node;
1746 /* Keep the body; we're going to dump it. */
1747 if (dump_enabled_p (TDI_tree_all))
1748 return true;
1749 if (!cgraph_global_info_ready)
1750 return (DECL_INLINE (decl) && !flag_really_no_inline);
1751 /* Look if there is any clone around. */
1752 for (node = cgraph_node (decl); node; node = node->next_clone)
1753 if (node->global.inlined_to)
1754 return true;
1755 return false;
1758 /* Perform simple optimizations based on callgraph. */
1760 void
1761 cgraph_optimize (void)
1763 #ifdef ENABLE_CHECKING
1764 verify_cgraph ();
1765 #endif
1766 if (!flag_unit_at_a_time)
1767 return;
1769 process_pending_assemble_externals ();
1771 timevar_push (TV_CGRAPHOPT);
1772 if (!quiet_flag)
1773 fprintf (stderr, "Performing intraprocedural optimizations\n");
1775 cgraph_mark_local_functions ();
1776 if (cgraph_dump_file)
1778 fprintf (cgraph_dump_file, "Marked ");
1779 dump_cgraph (cgraph_dump_file);
1782 if (flag_inline_trees)
1783 cgraph_decide_inlining ();
1784 cgraph_global_info_ready = true;
1785 if (cgraph_dump_file)
1787 fprintf (cgraph_dump_file, "Optimized ");
1788 dump_cgraph (cgraph_dump_file);
1790 timevar_pop (TV_CGRAPHOPT);
1792 /* Output everything. */
1793 if (!quiet_flag)
1794 fprintf (stderr, "Assembling functions:\n");
1795 #ifdef ENABLE_CHECKING
1796 verify_cgraph ();
1797 #endif
1799 cgraph_mark_functions_to_output ();
1801 cgraph_expand_all_functions ();
1802 if (cgraph_dump_file)
1804 fprintf (cgraph_dump_file, "\nFinal ");
1805 dump_cgraph (cgraph_dump_file);
1807 #ifdef ENABLE_CHECKING
1808 verify_cgraph ();
1809 /* Double check that all inline clones are gone and that all
1810 function bodies have been released from memory. */
1811 if (flag_unit_at_a_time
1812 && !dump_enabled_p (TDI_tree_all)
1813 && !(sorrycount || errorcount))
1815 struct cgraph_node *node;
1816 bool error_found = false;
1818 for (node = cgraph_nodes; node; node = node->next)
1819 if (node->analyzed
1820 && (node->global.inlined_to
1821 || DECL_SAVED_TREE (node->decl)))
1823 error_found = true;
1824 dump_cgraph_node (stderr, node);
1826 if (error_found)
1827 internal_error ("Nodes with no released memory found.");
1829 #endif
1832 /* Generate and emit a static constructor or destructor. WHICH must be
1833 one of 'I' or 'D'. BODY should be a STATEMENT_LIST containing
1834 GENERIC statements. */
1836 void
1837 cgraph_build_static_cdtor (char which, tree body, int priority)
1839 static int counter = 0;
1840 char which_buf[16];
1841 tree decl, name, resdecl;
1843 sprintf (which_buf, "%c_%d", which, counter++);
1844 name = get_file_function_name_long (which_buf);
1846 decl = build_decl (FUNCTION_DECL, name,
1847 build_function_type (void_type_node, void_list_node));
1848 current_function_decl = decl;
1850 resdecl = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
1851 DECL_ARTIFICIAL (resdecl) = 1;
1852 DECL_IGNORED_P (resdecl) = 1;
1853 DECL_RESULT (decl) = resdecl;
1855 allocate_struct_function (decl);
1857 TREE_STATIC (decl) = 1;
1858 TREE_USED (decl) = 1;
1859 DECL_ARTIFICIAL (decl) = 1;
1860 DECL_IGNORED_P (decl) = 1;
1861 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1862 DECL_SAVED_TREE (decl) = body;
1863 TREE_PUBLIC (decl) = ! targetm.have_ctors_dtors;
1864 DECL_UNINLINABLE (decl) = 1;
1866 DECL_INITIAL (decl) = make_node (BLOCK);
1867 TREE_USED (DECL_INITIAL (decl)) = 1;
1869 DECL_SOURCE_LOCATION (decl) = input_location;
1870 cfun->function_end_locus = input_location;
1872 switch (which)
1874 case 'I':
1875 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1876 break;
1877 case 'D':
1878 DECL_STATIC_DESTRUCTOR (decl) = 1;
1879 break;
1880 default:
1881 gcc_unreachable ();
1884 gimplify_function_tree (decl);
1886 /* ??? We will get called LATE in the compilation process. */
1887 if (cgraph_global_info_ready)
1888 tree_rest_of_compilation (decl);
1889 else
1890 cgraph_finalize_function (decl, 0);
1892 if (targetm.have_ctors_dtors)
1894 void (*fn) (rtx, int);
1896 if (which == 'I')
1897 fn = targetm.asm_out.constructor;
1898 else
1899 fn = targetm.asm_out.destructor;
1900 fn (XEXP (DECL_RTL (decl), 0), priority);
1904 void
1905 init_cgraph (void)
1907 cgraph_dump_file = dump_begin (TDI_cgraph, NULL);