* cp-tree.h (enum cp_storage_class): Remove trailing comma.
[official-gcc.git] / gcc / cgraphunit.c
blob67001f0dd127f72294dbd336657c7c35bd67e7b1
1 /* Callgraph based intraprocedural optimizations.
2 Copyright (C) 2003, 2004 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 it's 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. Reffer 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. */
166 #include "config.h"
167 #include "system.h"
168 #include "coretypes.h"
169 #include "tm.h"
170 #include "tree.h"
171 #include "tree-inline.h"
172 #include "langhooks.h"
173 #include "hashtab.h"
174 #include "toplev.h"
175 #include "flags.h"
176 #include "ggc.h"
177 #include "debug.h"
178 #include "target.h"
179 #include "cgraph.h"
180 #include "diagnostic.h"
181 #include "timevar.h"
182 #include "params.h"
183 #include "fibheap.h"
184 #include "c-common.h"
185 #include "intl.h"
186 #include "function.h"
188 #define INSNS_PER_CALL 10
190 static void cgraph_expand_all_functions (void);
191 static void cgraph_mark_functions_to_output (void);
192 static void cgraph_expand_function (struct cgraph_node *);
193 static tree record_call_1 (tree *, int *, void *);
194 static void cgraph_mark_local_functions (void);
195 static bool cgraph_default_inline_p (struct cgraph_node *n);
196 static void cgraph_analyze_function (struct cgraph_node *node);
197 static void cgraph_decide_inlining_incrementally (struct cgraph_node *);
199 /* Statistics we collect about inlining algorithm. */
200 static int ncalls_inlined;
201 static int nfunctions_inlined;
202 static int initial_insns;
203 static int overall_insns;
205 /* Records tree nodes seen in cgraph_create_edges. Simply using
206 walk_tree_without_duplicates doesn't guarantee each node is visited
207 once because it gets a new htab upon each recursive call from
208 record_calls_1. */
209 static htab_t visited_nodes;
211 /* Determine if function DECL is needed. That is, visible to something
212 either outside this translation unit, something magic in the system
213 configury, or (if not doing unit-at-a-time) to something we havn't
214 seen yet. */
216 static bool
217 decide_is_function_needed (struct cgraph_node *node, tree decl)
219 struct cgraph_node *origin;
221 /* If we decided it was needed before, but at the time we didn't have
222 the body of the function available, then it's still needed. We have
223 to go back and re-check its dependencies now. */
224 if (node->needed)
225 return true;
227 /* Externally visible functions must be output. The exception is
228 COMDAT functions that must be output only when they are needed. */
229 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
230 return true;
232 /* Constructors and destructors are reachable from the runtime by
233 some mechanism. */
234 if (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl))
235 return true;
237 /* If the user told us it is used, then it must be so. */
238 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
239 return true;
241 /* ??? If the assembler name is set by hand, it is possible to assemble
242 the name later after finalizing the function and the fact is noticed
243 in assemble_name then. This is arguably a bug. */
244 if (DECL_ASSEMBLER_NAME_SET_P (decl)
245 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
246 return true;
248 if (flag_unit_at_a_time)
249 return false;
251 /* If not doing unit at a time, then we'll only defer this function
252 if its marked for inlining. Otherwise we want to emit it now. */
254 /* "extern inline" functions are never output locally. */
255 if (DECL_EXTERNAL (decl))
256 return false;
257 /* Nested functions of extern inline function shall not be emit unless
258 we inlined the origin. */
259 for (origin = node->origin; origin; origin = origin->origin)
260 if (DECL_EXTERNAL (origin->decl))
261 return false;
262 /* We want to emit COMDAT functions only when absolutely necessary. */
263 if (DECL_COMDAT (decl))
264 return false;
265 if (!DECL_INLINE (decl)
266 || (!node->local.disregard_inline_limits
267 /* When declared inline, defer even the uninlinable functions.
268 This allows them to be eliminated when unused. */
269 && !DECL_DECLARED_INLINE_P (decl)
270 && (!node->local.inlinable || !cgraph_default_inline_p (node))))
271 return true;
273 return false;
276 /* When not doing unit-at-a-time, output all functions enqueued.
277 Return true when such a functions were found. */
279 bool
280 cgraph_assemble_pending_functions (void)
282 bool output = false;
284 if (flag_unit_at_a_time)
285 return false;
287 while (cgraph_nodes_queue)
289 struct cgraph_node *n = cgraph_nodes_queue;
291 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
292 n->next_needed = NULL;
293 if (!n->global.inlined_to && !DECL_EXTERNAL (n->decl))
295 cgraph_expand_function (n);
296 output = true;
300 return output;
303 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
304 logic in effect. If NESTED is true, then our caller cannot stand to have
305 the garbage collector run at the moment. We would need to either create
306 a new GC context, or just not compile right now. */
308 void
309 cgraph_finalize_function (tree decl, bool nested)
311 struct cgraph_node *node = cgraph_node (decl);
313 if (node->local.finalized)
315 /* As an GCC extension we allow redefinition of the function. The
316 semantics when both copies of bodies differ is not well defined.
317 We replace the old body with new body so in unit at a time mode
318 we always use new body, while in normal mode we may end up with
319 old body inlined into some functions and new body expanded and
320 inlined in others.
322 ??? It may make more sense to use one body for inlining and other
323 body for expanding the function but this is difficult to do. */
325 /* If node->output is set, then this is a unit-at-a-time compilation
326 and we have already begun whole-unit analysis. This is *not*
327 testing for whether we've already emitted the function. That
328 case can be sort-of legitimately seen with real function
329 redefinition errors. I would argue that the front end should
330 never present us with such a case, but don't enforce that for now. */
331 if (node->output)
332 abort ();
334 /* Reset our data structures so we can analyze the function again. */
335 memset (&node->local, 0, sizeof (node->local));
336 memset (&node->global, 0, sizeof (node->global));
337 memset (&node->rtl, 0, sizeof (node->rtl));
338 node->analyzed = false;
339 node->local.redefined_extern_inline = true;
340 while (node->callees)
341 cgraph_remove_edge (node->callees);
343 /* We may need to re-queue the node for assembling in case
344 we already proceeded it and ignored as not needed. */
345 if (node->reachable && !flag_unit_at_a_time)
347 struct cgraph_node *n;
349 for (n = cgraph_nodes_queue; n; n = n->next_needed)
350 if (n == node)
351 break;
352 if (!n)
353 node->reachable = 0;
357 notice_global_symbol (decl);
358 node->decl = decl;
359 node->local.finalized = true;
361 /* If not unit at a time, then we need to create the call graph
362 now, so that called functions can be queued and emitted now. */
363 if (!flag_unit_at_a_time)
365 cgraph_analyze_function (node);
366 cgraph_decide_inlining_incrementally (node);
369 if (decide_is_function_needed (node, decl))
370 cgraph_mark_needed_node (node);
372 /* If not unit at a time, go ahead and emit everything we've found
373 to be reachable at this time. */
374 if (!nested)
376 if (!cgraph_assemble_pending_functions ())
377 ggc_collect ();
380 /* If we've not yet emitted decl, tell the debug info about it. */
381 if (!TREE_ASM_WRITTEN (decl))
382 (*debug_hooks->deferred_inline_function) (decl);
384 /* Possibly warn about unused parameters. */
385 if (warn_unused_parameter)
386 do_warn_unused_parameter (decl);
389 /* Walk tree and record all calls. Called via walk_tree. */
390 static tree
391 record_call_1 (tree *tp, int *walk_subtrees, void *data)
393 tree t = *tp;
395 switch (TREE_CODE (t))
397 case VAR_DECL:
398 /* ??? Really, we should mark this decl as *potentially* referenced
399 by this function and re-examine whether the decl is actually used
400 after rtl has been generated. */
401 if (TREE_STATIC (t))
402 cgraph_varpool_mark_needed_node (cgraph_varpool_node (t));
403 break;
405 case ADDR_EXPR:
406 if (flag_unit_at_a_time)
408 /* Record dereferences to the functions. This makes the
409 functions reachable unconditionally. */
410 tree decl = TREE_OPERAND (*tp, 0);
411 if (TREE_CODE (decl) == FUNCTION_DECL)
412 cgraph_mark_needed_node (cgraph_node (decl));
414 break;
416 case CALL_EXPR:
418 tree decl = get_callee_fndecl (*tp);
419 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
421 cgraph_create_edge (data, cgraph_node (decl), *tp);
423 /* When we see a function call, we don't want to look at the
424 function reference in the ADDR_EXPR that is hanging from
425 the CALL_EXPR we're examining here, because we would
426 conclude incorrectly that the function's address could be
427 taken by something that is not a function call. So only
428 walk the function parameter list, skip the other subtrees. */
430 walk_tree (&TREE_OPERAND (*tp, 1), record_call_1, data,
431 visited_nodes);
432 *walk_subtrees = 0;
434 break;
437 default:
438 /* Save some cycles by not walking types and declaration as we
439 won't find anything useful there anyway. */
440 if (DECL_P (*tp) || TYPE_P (*tp))
442 *walk_subtrees = 0;
443 break;
446 if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
447 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees, data);
448 break;
451 return NULL;
454 /* Create cgraph edges for function calls inside BODY from NODE. */
456 void
457 cgraph_create_edges (struct cgraph_node *node, tree body)
459 /* The nodes we're interested in are never shared, so walk
460 the tree ignoring duplicates. */
461 visited_nodes = htab_create (37, htab_hash_pointer,
462 htab_eq_pointer, NULL);
463 walk_tree (&body, record_call_1, node, visited_nodes);
464 htab_delete (visited_nodes);
465 visited_nodes = NULL;
468 static bool error_found;
470 /* Callbrack of verify_cgraph_node. Check that all call_exprs have cgraph nodes. */
471 static tree
472 verify_cgraph_node_1 (tree *tp, int *walk_subtrees, void *data)
474 tree t = *tp;
475 tree decl;
477 if (TREE_CODE (t) == CALL_EXPR && (decl = get_callee_fndecl (t)))
479 struct cgraph_edge *e = cgraph_edge (data, t);
480 if (e)
482 if (e->aux)
484 error ("Shared call_expr:");
485 debug_tree (t);
486 error_found = true;
488 if (e->callee->decl != cgraph_node (decl)->decl)
490 error ("Edge points to wrong declaration:");
491 debug_tree (e->callee->decl);
492 fprintf (stderr," Instead of:");
493 debug_tree (decl);
495 e->aux = (void *)1;
497 else
499 error ("Missing callgraph edge for call expr:");
500 debug_tree (t);
501 error_found = true;
504 /* Save some cycles by not walking types and declaration as we
505 won't find anything useful there anyway. */
506 if (DECL_P (*tp) || TYPE_P (*tp))
508 *walk_subtrees = 0;
510 return NULL_TREE;
513 /* Verify cgraph nodes of given cgraph node. */
514 void
515 verify_cgraph_node (struct cgraph_node *node)
517 struct cgraph_edge *e;
518 struct cgraph_node *main_clone;
520 timevar_push (TV_CGRAPH_VERIFY);
521 error_found = false;
522 for (e = node->callees; e; e = e->next_callee)
523 if (e->aux)
525 error ("Aux field set for edge %s->%s",
526 cgraph_node_name (e->caller), cgraph_node_name (e->callee));
527 error_found = true;
529 for (e = node->callers; e; e = e->next_caller)
531 if (!e->inline_failed)
533 if (node->global.inlined_to
534 != (e->caller->global.inlined_to
535 ? e->caller->global.inlined_to : e->caller))
537 error ("Inlined_to pointer is wrong");
538 error_found = true;
540 if (node->callers->next_caller)
542 error ("Multiple inline callers");
543 error_found = true;
546 else
547 if (node->global.inlined_to)
549 error ("Inlined_to pointer set for noninline callers");
550 error_found = true;
553 if (!node->callers && node->global.inlined_to)
555 error ("Inlined_to pointer is set but no predecesors found");
556 error_found = true;
558 if (node->global.inlined_to == node)
560 error ("Inlined_to pointer reffers to itself");
561 error_found = true;
564 for (main_clone = cgraph_node (node->decl); main_clone;
565 main_clone = main_clone->next_clone)
566 if (main_clone == node)
567 break;
568 if (!node)
570 error ("Node not found in DECL_ASSEMBLER_NAME hash");
571 error_found = true;
574 if (node->analyzed
575 && DECL_SAVED_TREE (node->decl) && !TREE_ASM_WRITTEN (node->decl)
576 && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to))
578 walk_tree_without_duplicates (&DECL_SAVED_TREE (node->decl),
579 verify_cgraph_node_1, node);
580 for (e = node->callees; e; e = e->next_callee)
582 if (!e->aux)
584 error ("Edge %s->%s has no corresponding call_expr",
585 cgraph_node_name (e->caller),
586 cgraph_node_name (e->callee));
587 error_found = true;
589 e->aux = 0;
592 if (error_found)
594 dump_cgraph_node (stderr, node);
595 internal_error ("verify_cgraph_node failed.");
597 timevar_pop (TV_CGRAPH_VERIFY);
600 /* Verify whole cgraph structure. */
601 void
602 verify_cgraph (void)
604 struct cgraph_node *node;
606 for (node = cgraph_nodes; node; node = node->next)
607 verify_cgraph_node (node);
610 /* Analyze the function scheduled to be output. */
611 static void
612 cgraph_analyze_function (struct cgraph_node *node)
614 tree decl = node->decl;
615 struct cgraph_edge *e;
617 current_function_decl = decl;
619 /* First kill forward declaration so reverse inlining works properly. */
620 cgraph_create_edges (node, DECL_SAVED_TREE (decl));
622 node->local.inlinable = tree_inlinable_function_p (decl);
623 node->local.self_insns = estimate_num_insns (DECL_SAVED_TREE (decl));
624 if (node->local.inlinable)
625 node->local.disregard_inline_limits
626 = lang_hooks.tree_inlining.disregard_inline_limits (decl);
627 for (e = node->callers; e; e = e->next_caller)
629 if (node->local.redefined_extern_inline)
630 e->inline_failed = N_("redefined extern inline functions are not "
631 "considered for inlining");
632 else if (!node->local.inlinable)
633 e->inline_failed = N_("function not inlinable");
634 else
635 e->inline_failed = N_("function not considered for inlining");
637 if (flag_really_no_inline && !node->local.disregard_inline_limits)
638 node->local.inlinable = 0;
639 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
640 node->global.insns = node->local.self_insns;
642 node->analyzed = true;
643 current_function_decl = NULL;
646 /* Analyze the whole compilation unit once it is parsed completely. */
648 void
649 cgraph_finalize_compilation_unit (void)
651 struct cgraph_node *node;
653 if (!flag_unit_at_a_time)
655 cgraph_assemble_pending_functions ();
656 return;
659 cgraph_varpool_assemble_pending_decls ();
660 if (!quiet_flag)
661 fprintf (stderr, "\nAnalyzing compilation unit\n");
663 timevar_push (TV_CGRAPH);
664 if (cgraph_dump_file)
666 fprintf (cgraph_dump_file, "Initial entry points:");
667 for (node = cgraph_nodes; node; node = node->next)
668 if (node->needed && DECL_SAVED_TREE (node->decl))
669 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
670 fprintf (cgraph_dump_file, "\n");
673 /* Propagate reachability flag and lower representation of all reachable
674 functions. In the future, lowering will introduce new functions and
675 new entry points on the way (by template instantiation and virtual
676 method table generation for instance). */
677 while (cgraph_nodes_queue)
679 struct cgraph_edge *edge;
680 tree decl = cgraph_nodes_queue->decl;
682 node = cgraph_nodes_queue;
683 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
684 node->next_needed = NULL;
686 /* ??? It is possible to create extern inline function and later using
687 weak alas attribute to kill its body. See
688 gcc.c-torture/compile/20011119-1.c */
689 if (!DECL_SAVED_TREE (decl))
690 continue;
692 if (node->analyzed || !node->reachable || !DECL_SAVED_TREE (decl))
693 abort ();
695 cgraph_analyze_function (node);
697 for (edge = node->callees; edge; edge = edge->next_callee)
698 if (!edge->callee->reachable)
699 cgraph_mark_reachable_node (edge->callee);
701 cgraph_varpool_assemble_pending_decls ();
704 /* Collect entry points to the unit. */
706 if (cgraph_dump_file)
708 fprintf (cgraph_dump_file, "Unit entry points:");
709 for (node = cgraph_nodes; node; node = node->next)
710 if (node->needed && DECL_SAVED_TREE (node->decl))
711 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
712 fprintf (cgraph_dump_file, "\n\nInitial ");
713 dump_cgraph (cgraph_dump_file);
716 if (cgraph_dump_file)
717 fprintf (cgraph_dump_file, "\nReclaiming functions:");
719 for (node = cgraph_nodes; node; node = node->next)
721 tree decl = node->decl;
723 if (!node->reachable && DECL_SAVED_TREE (decl))
725 if (cgraph_dump_file)
726 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
727 cgraph_remove_node (node);
729 else
730 node->next_needed = NULL;
732 if (cgraph_dump_file)
734 fprintf (cgraph_dump_file, "\n\nReclaimed ");
735 dump_cgraph (cgraph_dump_file);
737 ggc_collect ();
738 timevar_pop (TV_CGRAPH);
740 /* Figure out what functions we want to assemble. */
742 static void
743 cgraph_mark_functions_to_output (void)
745 struct cgraph_node *node;
747 for (node = cgraph_nodes; node; node = node->next)
749 tree decl = node->decl;
750 struct cgraph_edge *e;
751 if (node->output)
752 abort ();
754 for (e = node->callers; e; e = e->next_caller)
755 if (e->inline_failed)
756 break;
758 /* We need to output all local functions that are used and not
759 always inlined, as well as those that are reachable from
760 outside the current compilation unit. */
761 if (DECL_SAVED_TREE (decl)
762 && !node->global.inlined_to
763 && (node->needed
764 || (e && node->reachable))
765 && !TREE_ASM_WRITTEN (decl)
766 && !DECL_EXTERNAL (decl))
767 node->output = 1;
768 /* We should've reclaimed all functions that are not needed. */
769 else if (!node->global.inlined_to && DECL_SAVED_TREE (decl)
770 && !DECL_EXTERNAL (decl))
772 dump_cgraph_node (stderr, node);
773 abort ();
778 /* Expand function specified by NODE. */
780 static void
781 cgraph_expand_function (struct cgraph_node *node)
783 tree decl = node->decl;
785 /* We ought to not compile any inline clones. */
786 if (node->global.inlined_to)
787 abort ();
789 if (flag_unit_at_a_time)
790 announce_function (decl);
792 /* Generate RTL for the body of DECL. Nested functions are expanded
793 via lang_expand_decl_stmt. */
794 lang_hooks.callgraph.expand_function (decl);
796 /* Make sure that BE didn't give up on compiling. */
797 /* ??? Can happen with nested function of extern inline. */
798 if (!TREE_ASM_WRITTEN (node->decl))
799 abort ();
801 current_function_decl = NULL;
802 if (DECL_SAVED_TREE (node->decl)
803 && !cgraph_preserve_function_body_p (node->decl))
805 DECL_SAVED_TREE (node->decl) = NULL;
806 DECL_STRUCT_FUNCTION (node->decl) = NULL;
807 DECL_INITIAL (node->decl) = error_mark_node;
811 /* Fill array order with all nodes with output flag set in the reverse
812 topological order. */
814 static int
815 cgraph_postorder (struct cgraph_node **order)
817 struct cgraph_node *node, *node2;
818 int stack_size = 0;
819 int order_pos = 0;
820 struct cgraph_edge *edge, last;
822 struct cgraph_node **stack =
823 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
825 /* We have to deal with cycles nicely, so use a depth first traversal
826 output algorithm. Ignore the fact that some functions won't need
827 to be output and put them into order as well, so we get dependencies
828 right through intline functions. */
829 for (node = cgraph_nodes; node; node = node->next)
830 node->aux = NULL;
831 for (node = cgraph_nodes; node; node = node->next)
832 if (!node->aux)
834 node2 = node;
835 if (!node->callers)
836 node->aux = &last;
837 else
838 node->aux = node->callers;
839 while (node2)
841 while (node2->aux != &last)
843 edge = node2->aux;
844 if (edge->next_caller)
845 node2->aux = edge->next_caller;
846 else
847 node2->aux = &last;
848 if (!edge->caller->aux)
850 if (!edge->caller->callers)
851 edge->caller->aux = &last;
852 else
853 edge->caller->aux = edge->caller->callers;
854 stack[stack_size++] = node2;
855 node2 = edge->caller;
856 break;
859 if (node2->aux == &last)
861 order[order_pos++] = node2;
862 if (stack_size)
863 node2 = stack[--stack_size];
864 else
865 node2 = NULL;
869 free (stack);
870 return order_pos;
873 /* Perform reachability analysis and reclaim all unreachable nodes.
874 This function also remove unneeded bodies of extern inline functions
875 and thus needs to be done only after inlining decisions has been made. */
876 static bool
877 cgraph_remove_unreachable_nodes (void)
879 struct cgraph_node *first = (void *) 1;
880 struct cgraph_node *node;
881 bool changed = false;
882 int insns = 0;
884 #ifdef ENABLE_CHECKING
885 verify_cgraph ();
886 #endif
887 if (cgraph_dump_file)
888 fprintf (cgraph_dump_file, "\nReclaiming functions:");
889 #ifdef ENABLE_CHECKING
890 for (node = cgraph_nodes; node; node = node->next)
891 if (node->aux)
892 abort ();
893 #endif
894 for (node = cgraph_nodes; node; node = node->next)
895 if (node->needed && !node->global.inlined_to
896 && (!DECL_EXTERNAL (node->decl) || !node->analyzed))
898 node->aux = first;
899 first = node;
901 else if (node->aux)
902 abort ();
904 /* Perform reachability analysis. As a special case do not consider
905 extern inline functions not inlined as live because we won't output
906 them at all. */
907 while (first != (void *) 1)
909 struct cgraph_edge *e;
910 node = first;
911 first = first->aux;
913 for (e = node->callees; e; e = e->next_callee)
914 if (!e->callee->aux
915 && node->analyzed
916 && (!e->inline_failed || !e->callee->analyzed
917 || !DECL_EXTERNAL (e->callee->decl)))
919 e->callee->aux = first;
920 first = e->callee;
924 /* Remove unreachable nodes. Extern inline functions need special care;
925 Unreachable extern inline functions shall be removed.
926 Reachable extern inline functions we never inlined shall get their bodies
927 eliminated.
928 Reachable extern inline functions we sometimes inlined will be turned into
929 unanalyzed nodes so they look like for true extern functions to the rest
930 of code. Body of such functions is released via remove_node once the
931 inline clones are eliminated. */
932 for (node = cgraph_nodes; node; node = node->next)
934 if (!node->aux)
936 int local_insns;
937 tree decl = node->decl;
939 node->global.inlined_to = NULL;
940 if (DECL_STRUCT_FUNCTION (decl))
941 local_insns = node->local.self_insns;
942 else
943 local_insns = 0;
944 if (cgraph_dump_file)
945 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
946 if (!node->analyzed || !DECL_EXTERNAL (node->decl))
947 cgraph_remove_node (node);
948 else
950 struct cgraph_edge *e;
952 for (e = node->callers; e; e = e->next_caller)
953 if (e->caller->aux)
954 break;
955 if (e || node->needed)
957 struct cgraph_node *clone;
959 for (clone = node->next_clone; clone;
960 clone = clone->next_clone)
961 if (clone->aux)
962 break;
963 if (!clone)
965 DECL_SAVED_TREE (node->decl) = NULL;
966 DECL_STRUCT_FUNCTION (node->decl) = NULL;
967 DECL_INITIAL (node->decl) = error_mark_node;
969 while (node->callees)
970 cgraph_remove_edge (node->callees);
971 node->analyzed = false;
973 else
974 cgraph_remove_node (node);
976 if (!DECL_SAVED_TREE (decl))
977 insns += local_insns;
978 changed = true;
981 for (node = cgraph_nodes; node; node = node->next)
982 node->aux = NULL;
983 if (cgraph_dump_file)
984 fprintf (cgraph_dump_file, "\nReclaimed %i insns", insns);
985 return changed;
988 /* Estimate size of the function after inlining WHAT into TO. */
990 static int
991 cgraph_estimate_size_after_inlining (int times, struct cgraph_node *to,
992 struct cgraph_node *what)
994 return (what->global.insns - INSNS_PER_CALL) * times + to->global.insns;
997 /* Estimate the growth caused by inlining NODE into all callees. */
999 static int
1000 cgraph_estimate_growth (struct cgraph_node *node)
1002 int growth = 0;
1003 struct cgraph_edge *e;
1005 for (e = node->callers; e; e = e->next_caller)
1006 if (e->inline_failed)
1007 growth += (cgraph_estimate_size_after_inlining (1, e->caller, node)
1008 - e->caller->global.insns);
1010 /* ??? Wrong for self recursive functions or cases where we decide to not
1011 inline for different reasons, but it is not big deal as in that case
1012 we will keep the body around, but we will also avoid some inlining. */
1013 if (!node->needed && !DECL_EXTERNAL (node->decl))
1014 growth -= node->global.insns;
1016 return growth;
1019 /* E is expected to be an edge being inlined. Clone destination node of
1020 the edge and redirect it to the new clone.
1021 DUPLICATE is used for bookkeeping on whether we are actually creating new
1022 clones or re-using node originally representing out-of-line function call.
1024 void
1025 cgraph_clone_inlined_nodes (struct cgraph_edge *e, bool duplicate)
1027 struct cgraph_node *n;
1029 /* We may eliminate the need for out-of-line copy to be output. In that
1030 case just go ahead and re-use it. */
1031 if (!e->callee->callers->next_caller
1032 && (!e->callee->needed || DECL_EXTERNAL (e->callee->decl))
1033 && duplicate
1034 && flag_unit_at_a_time)
1036 if (e->callee->global.inlined_to)
1037 abort ();
1038 if (!DECL_EXTERNAL (e->callee->decl))
1039 overall_insns -= e->callee->global.insns, nfunctions_inlined++;
1040 duplicate = 0;
1042 else if (duplicate)
1044 n = cgraph_clone_node (e->callee);
1045 cgraph_redirect_edge_callee (e, n);
1048 if (e->caller->global.inlined_to)
1049 e->callee->global.inlined_to = e->caller->global.inlined_to;
1050 else
1051 e->callee->global.inlined_to = e->caller;
1053 /* Recursively clone all bodies. */
1054 for (e = e->callee->callees; e; e = e->next_callee)
1055 if (!e->inline_failed)
1056 cgraph_clone_inlined_nodes (e, duplicate);
1059 /* Mark edge E as inlined and update callgraph accordingly. */
1061 void
1062 cgraph_mark_inline_edge (struct cgraph_edge *e)
1064 int old_insns = 0, new_insns = 0;
1065 struct cgraph_node *to = NULL, *what;
1067 if (!e->inline_failed)
1068 abort ();
1069 e->inline_failed = NULL;
1071 if (!e->callee->global.inlined && flag_unit_at_a_time)
1072 DECL_POSSIBLY_INLINED (e->callee->decl) = true;
1073 e->callee->global.inlined = true;
1075 cgraph_clone_inlined_nodes (e, true);
1077 what = e->callee;
1079 /* Now update size of caller and all functions caller is inlined into. */
1080 for (;e && !e->inline_failed; e = e->caller->callers)
1082 old_insns = e->caller->global.insns;
1083 new_insns = cgraph_estimate_size_after_inlining (1, e->caller,
1084 what);
1085 if (new_insns < 0)
1086 abort ();
1087 to = e->caller;
1088 to->global.insns = new_insns;
1090 if (what->global.inlined_to != to)
1091 abort ();
1092 overall_insns += new_insns - old_insns;
1093 ncalls_inlined++;
1096 /* Mark all calls of EDGE->CALLEE inlined into EDGE->CALLER.
1097 Return following unredirected edge in the list of callers
1098 of EDGE->CALLEE */
1100 static struct cgraph_edge *
1101 cgraph_mark_inline (struct cgraph_edge *edge)
1103 struct cgraph_node *to = edge->caller;
1104 struct cgraph_node *what = edge->callee;
1105 struct cgraph_edge *e, *next;
1106 int times = 0;
1108 /* Look for all calls, mark them inline and clone recursively
1109 all inlined functions. */
1110 for (e = what->callers; e; e = next)
1112 next = e->next_caller;
1113 if (e->caller == to && e->inline_failed)
1115 cgraph_mark_inline_edge (e);
1116 if (e == edge)
1117 edge = next;
1118 times ++;
1121 if (!times)
1122 abort ();
1123 return edge;
1126 /* Return false when inlining WHAT into TO is not good idea
1127 as it would cause too large growth of function bodies. */
1129 static bool
1130 cgraph_check_inline_limits (struct cgraph_node *to, struct cgraph_node *what,
1131 const char **reason)
1133 int times = 0;
1134 struct cgraph_edge *e;
1135 int newsize;
1136 int limit;
1138 if (to->global.inlined_to)
1139 to = to->global.inlined_to;
1141 for (e = to->callees; e; e = e->next_callee)
1142 if (e->callee == what)
1143 times++;
1145 /* When inlining large function body called once into small function,
1146 take the inlined function as base for limiting the growth. */
1147 if (to->local.self_insns > what->local.self_insns)
1148 limit = to->local.self_insns;
1149 else
1150 limit = what->local.self_insns;
1152 limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
1154 newsize = cgraph_estimate_size_after_inlining (times, to, what);
1155 if (newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
1156 && newsize > limit)
1158 if (reason)
1159 *reason = N_("--param large-function-growth limit reached");
1160 return false;
1162 return true;
1165 /* Return true when function N is small enough to be inlined. */
1167 static bool
1168 cgraph_default_inline_p (struct cgraph_node *n)
1170 if (!DECL_INLINE (n->decl) || !DECL_SAVED_TREE (n->decl))
1171 return false;
1172 if (DECL_DECLARED_INLINE_P (n->decl))
1173 return n->global.insns < MAX_INLINE_INSNS_SINGLE;
1174 else
1175 return n->global.insns < MAX_INLINE_INSNS_AUTO;
1178 /* Return true when inlining WHAT would create recursive inlining.
1179 We call recursive inlining all cases where same function appears more than
1180 once in the single recursion nest path in the inline graph. */
1182 static bool
1183 cgraph_recursive_inlining_p (struct cgraph_node *to,
1184 struct cgraph_node *what,
1185 const char **reason)
1187 bool recursive;
1188 if (to->global.inlined_to)
1189 recursive = what->decl == to->global.inlined_to->decl;
1190 else
1191 recursive = what->decl == to->decl;
1192 /* Marking recursive function inline has sane semantic and thus we should
1193 not warn on it. */
1194 if (recursive && reason)
1195 *reason = (what->local.disregard_inline_limits
1196 ? N_("recursive inlining") : "");
1197 return recursive;
1200 /* Recompute heap nodes for each of callees. */
1201 static void
1202 update_callee_keys (fibheap_t heap, struct fibnode **heap_node,
1203 struct cgraph_node *node)
1205 struct cgraph_edge *e;
1207 for (e = node->callees; e; e = e->next_callee)
1208 if (e->inline_failed && heap_node[e->callee->uid])
1209 fibheap_replace_key (heap, heap_node[e->callee->uid],
1210 cgraph_estimate_growth (e->callee));
1211 else if (!e->inline_failed)
1212 update_callee_keys (heap, heap_node, e->callee);
1215 /* Enqueue all recursive calls from NODE into queue linked via aux pointers
1216 in between FIRST and LAST. WHERE is used for bookkeeping while looking
1217 int calls inlined within NODE. */
1218 static void
1219 lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
1220 struct cgraph_edge **first, struct cgraph_edge **last)
1222 struct cgraph_edge *e;
1223 for (e = where->callees; e; e = e->next_callee)
1224 if (e->callee == node)
1226 if (!*first)
1227 *first = e;
1228 else
1229 (*last)->aux = e;
1230 *last = e;
1232 for (e = where->callees; e; e = e->next_callee)
1233 if (!e->inline_failed)
1234 lookup_recursive_calls (node, e->callee, first, last);
1237 /* Decide on recursive inlining: in the case function has recursive calls,
1238 inline until body size reaches given argument. */
1239 static void
1240 cgraph_decide_recursive_inlining (struct cgraph_node *node)
1242 int limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO);
1243 int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO);
1244 struct cgraph_edge *first_call = NULL, *last_call = NULL;
1245 struct cgraph_edge *last_in_current_depth;
1246 struct cgraph_edge *e;
1247 struct cgraph_node *master_clone;
1248 int depth = 0;
1249 int n = 0;
1251 if (DECL_DECLARED_INLINE_P (node->decl))
1253 limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE);
1254 max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH);
1257 /* Make sure that function is small enought to be considered for inlining. */
1258 if (!max_depth
1259 || cgraph_estimate_size_after_inlining (1, node, node) >= limit)
1260 return;
1261 lookup_recursive_calls (node, node, &first_call, &last_call);
1262 if (!first_call)
1263 return;
1265 if (cgraph_dump_file)
1266 fprintf (cgraph_dump_file,
1267 "\nPerforming recursive inlining on %s\n",
1268 cgraph_node_name (node));
1270 /* We need original clone to copy around. */
1271 master_clone = cgraph_clone_node (node);
1272 master_clone->needed = true;
1273 for (e = master_clone->callees; e; e = e->next_callee)
1274 if (!e->inline_failed)
1275 cgraph_clone_inlined_nodes (e, true);
1277 /* Do the inlining and update list of recursive call during process. */
1278 last_in_current_depth = last_call;
1279 while (first_call
1280 && cgraph_estimate_size_after_inlining (1, node, master_clone) <= limit)
1282 struct cgraph_edge *curr = first_call;
1284 first_call = first_call->aux;
1285 curr->aux = NULL;
1287 cgraph_redirect_edge_callee (curr, master_clone);
1288 cgraph_mark_inline_edge (curr);
1289 lookup_recursive_calls (node, curr->callee, &first_call, &last_call);
1291 if (last_in_current_depth
1292 && ++depth >= max_depth)
1293 break;
1294 n++;
1297 /* Cleanup queue pointers. */
1298 while (first_call)
1300 struct cgraph_edge *next = first_call->aux;
1301 first_call->aux = NULL;
1302 first_call = next;
1304 if (cgraph_dump_file)
1305 fprintf (cgraph_dump_file,
1306 "\n Inlined %i times, body grown from %i to %i insns\n", n,
1307 master_clone->global.insns, node->global.insns);
1309 /* Remove master clone we used for inlining. We rely that clones inlined
1310 into master clone gets queued just before master clone so we don't
1311 need recursion. */
1312 for (node = cgraph_nodes; node != master_clone;
1313 node = node->next)
1314 if (node->global.inlined_to == master_clone)
1315 cgraph_remove_node (node);
1316 cgraph_remove_node (master_clone);
1319 /* Set inline_failed for all callers of given function to REASON. */
1321 static void
1322 cgraph_set_inline_failed (struct cgraph_node *node, const char *reason)
1324 struct cgraph_edge *e;
1326 if (cgraph_dump_file)
1327 fprintf (cgraph_dump_file, "Inlining failed: %s\n", reason);
1328 for (e = node->callers; e; e = e->next_caller)
1329 if (e->inline_failed)
1330 e->inline_failed = reason;
1333 /* We use greedy algorithm for inlining of small functions:
1334 All inline candidates are put into prioritized heap based on estimated
1335 growth of the overall number of instructions and then update the estimates.
1337 INLINED and INLINED_CALEES are just pointers to arrays large enough
1338 to be passed to cgraph_inlined_into and cgraph_inlined_callees. */
1340 static void
1341 cgraph_decide_inlining_of_small_functions (void)
1343 struct cgraph_node *node;
1344 fibheap_t heap = fibheap_new ();
1345 struct fibnode **heap_node =
1346 xcalloc (cgraph_max_uid, sizeof (struct fibnode *));
1347 int max_insns = ((HOST_WIDEST_INT) initial_insns
1348 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
1350 /* Put all inline candidates into the heap. */
1352 for (node = cgraph_nodes; node; node = node->next)
1354 if (!node->local.inlinable || !node->callers
1355 || node->local.disregard_inline_limits)
1356 continue;
1358 if (!cgraph_default_inline_p (node))
1360 cgraph_set_inline_failed (node,
1361 N_("--param max-inline-insns-single limit reached"));
1362 continue;
1364 heap_node[node->uid] =
1365 fibheap_insert (heap, cgraph_estimate_growth (node), node);
1368 if (cgraph_dump_file)
1369 fprintf (cgraph_dump_file, "\nDeciding on smaller functions:\n");
1370 while (overall_insns <= max_insns && (node = fibheap_extract_min (heap)))
1372 struct cgraph_edge *e, *next;
1373 int old_insns = overall_insns;
1375 heap_node[node->uid] = NULL;
1376 if (cgraph_dump_file)
1377 fprintf (cgraph_dump_file,
1378 "\nConsidering %s with %i insns\n"
1379 " Estimated growth is %+i insns.\n",
1380 cgraph_node_name (node), node->global.insns,
1381 cgraph_estimate_growth (node));
1382 if (!cgraph_default_inline_p (node))
1384 cgraph_set_inline_failed (node,
1385 N_("--param max-inline-insns-single limit reached after inlining into the callee"));
1386 continue;
1388 for (e = node->callers; e; e = next)
1390 next = e->next_caller;
1391 if (e->inline_failed)
1393 struct cgraph_node *where;
1395 if (cgraph_recursive_inlining_p (e->caller, e->callee,
1396 &e->inline_failed)
1397 || !cgraph_check_inline_limits (e->caller, e->callee,
1398 &e->inline_failed))
1400 if (cgraph_dump_file)
1401 fprintf (cgraph_dump_file, " Not inlining into %s:%s.\n",
1402 cgraph_node_name (e->caller), e->inline_failed);
1403 continue;
1405 next = cgraph_mark_inline (e);
1406 where = e->caller;
1407 if (where->global.inlined_to)
1408 where = where->global.inlined_to;
1410 if (heap_node[where->uid])
1411 fibheap_replace_key (heap, heap_node[where->uid],
1412 cgraph_estimate_growth (where));
1414 if (cgraph_dump_file)
1415 fprintf (cgraph_dump_file,
1416 " Inlined into %s which now has %i insns.\n",
1417 cgraph_node_name (e->caller),
1418 e->caller->global.insns);
1422 cgraph_decide_recursive_inlining (node);
1424 /* Similarly all functions called by the function we just inlined
1425 are now called more times; update keys. */
1426 update_callee_keys (heap, heap_node, node);
1428 if (cgraph_dump_file)
1429 fprintf (cgraph_dump_file,
1430 " Inlined for a net change of %+i insns.\n",
1431 overall_insns - old_insns);
1433 while ((node = fibheap_extract_min (heap)) != NULL)
1434 if (!node->local.disregard_inline_limits)
1435 cgraph_set_inline_failed (node, N_("--param inline-unit-growth limit reached"));
1436 fibheap_delete (heap);
1437 free (heap_node);
1440 /* Decide on the inlining. We do so in the topological order to avoid
1441 expenses on updating data structures. */
1443 static void
1444 cgraph_decide_inlining (void)
1446 struct cgraph_node *node;
1447 int nnodes;
1448 struct cgraph_node **order =
1449 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
1450 int old_insns = 0;
1451 int i;
1453 for (node = cgraph_nodes; node; node = node->next)
1454 initial_insns += node->local.self_insns;
1455 overall_insns = initial_insns;
1457 nnodes = cgraph_postorder (order);
1459 if (cgraph_dump_file)
1460 fprintf (cgraph_dump_file,
1461 "\nDeciding on inlining. Starting with %i insns.\n",
1462 initial_insns);
1464 for (node = cgraph_nodes; node; node = node->next)
1465 node->aux = 0;
1467 if (cgraph_dump_file)
1468 fprintf (cgraph_dump_file, "\nInlining always_inline functions:\n");
1470 /* In the first pass mark all always_inline edges. Do this with a priority
1471 so none of our later choices will make this impossible. */
1472 for (i = nnodes - 1; i >= 0; i--)
1474 struct cgraph_edge *e, *next;
1476 node = order[i];
1478 if (!node->local.disregard_inline_limits)
1479 continue;
1480 if (cgraph_dump_file)
1481 fprintf (cgraph_dump_file,
1482 "\nConsidering %s %i insns (always inline)\n",
1483 cgraph_node_name (node), node->global.insns);
1484 old_insns = overall_insns;
1485 for (e = node->callers; e; e = next)
1487 next = e->next_caller;
1488 if (!e->inline_failed)
1489 continue;
1490 if (cgraph_recursive_inlining_p (e->caller, e->callee,
1491 &e->inline_failed))
1492 continue;
1493 cgraph_mark_inline_edge (e);
1494 if (cgraph_dump_file)
1495 fprintf (cgraph_dump_file,
1496 " Inlined into %s which now has %i insns.\n",
1497 cgraph_node_name (e->caller),
1498 e->caller->global.insns);
1500 if (cgraph_dump_file)
1501 fprintf (cgraph_dump_file,
1502 " Inlined for a net change of %+i insns.\n",
1503 overall_insns - old_insns);
1506 if (!flag_really_no_inline)
1508 cgraph_decide_inlining_of_small_functions ();
1510 if (cgraph_dump_file)
1511 fprintf (cgraph_dump_file, "\nDeciding on functions called once:\n");
1513 /* And finally decide what functions are called once. */
1515 for (i = nnodes - 1; i >= 0; i--)
1517 node = order[i];
1519 if (node->callers && !node->callers->next_caller && !node->needed
1520 && node->local.inlinable && node->callers->inline_failed
1521 && !DECL_EXTERNAL (node->decl) && !DECL_COMDAT (node->decl))
1523 bool ok = true;
1524 struct cgraph_node *node1;
1526 /* Verify that we won't duplicate the caller. */
1527 for (node1 = node->callers->caller;
1528 node1->callers && !node1->callers->inline_failed
1529 && ok; node1 = node1->callers->caller)
1530 if (node1->callers->next_caller || node1->needed)
1531 ok = false;
1532 if (ok)
1534 if (cgraph_dump_file)
1535 fprintf (cgraph_dump_file,
1536 "\nConsidering %s %i insns.\n"
1537 " Called once from %s %i insns.\n",
1538 cgraph_node_name (node), node->global.insns,
1539 cgraph_node_name (node->callers->caller),
1540 node->callers->caller->global.insns);
1542 old_insns = overall_insns;
1544 if (cgraph_check_inline_limits (node->callers->caller, node,
1545 NULL))
1547 cgraph_mark_inline (node->callers);
1548 if (cgraph_dump_file)
1549 fprintf (cgraph_dump_file,
1550 " Inlined into %s which now has %i insns"
1551 " for a net change of %+i insns.\n",
1552 cgraph_node_name (node->callers->caller),
1553 node->callers->caller->global.insns,
1554 overall_insns - old_insns);
1556 else
1558 if (cgraph_dump_file)
1559 fprintf (cgraph_dump_file,
1560 " Inline limit reached, not inlined.\n");
1567 /* We will never output extern functions we didn't inline.
1568 ??? Perhaps we can prevent accounting of growth of external
1569 inline functions. */
1570 cgraph_remove_unreachable_nodes ();
1572 if (cgraph_dump_file)
1573 fprintf (cgraph_dump_file,
1574 "\nInlined %i calls, eliminated %i functions, "
1575 "%i insns turned to %i insns.\n\n",
1576 ncalls_inlined, nfunctions_inlined, initial_insns,
1577 overall_insns);
1578 free (order);
1581 /* Decide on the inlining. We do so in the topological order to avoid
1582 expenses on updating data structures. */
1584 static void
1585 cgraph_decide_inlining_incrementally (struct cgraph_node *node)
1587 struct cgraph_edge *e;
1589 /* First of all look for always inline functions. */
1590 for (e = node->callees; e; e = e->next_callee)
1591 if (e->callee->local.disregard_inline_limits
1592 && e->inline_failed
1593 && !cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed)
1594 /* ??? It is possible that renaming variable removed the function body
1595 in duplicate_decls. See gcc.c-torture/compile/20011119-2.c */
1596 && DECL_SAVED_TREE (e->callee->decl))
1597 cgraph_mark_inline (e);
1599 /* Now do the automatic inlining. */
1600 if (!flag_really_no_inline)
1601 for (e = node->callees; e; e = e->next_callee)
1602 if (e->callee->local.inlinable
1603 && e->inline_failed
1604 && !e->callee->local.disregard_inline_limits
1605 && !cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed)
1606 && cgraph_check_inline_limits (node, e->callee, &e->inline_failed)
1607 && DECL_SAVED_TREE (e->callee->decl))
1609 if (cgraph_default_inline_p (e->callee))
1610 cgraph_mark_inline (e);
1611 else
1612 e->inline_failed
1613 = N_("--param max-inline-insns-single limit reached");
1618 /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */
1620 bool
1621 cgraph_inline_p (struct cgraph_edge *e, const char **reason)
1623 *reason = e->inline_failed;
1624 return !e->inline_failed;
1627 /* Expand all functions that must be output.
1629 Attempt to topologically sort the nodes so function is output when
1630 all called functions are already assembled to allow data to be
1631 propagated across the callgraph. Use a stack to get smaller distance
1632 between a function and its callees (later we may choose to use a more
1633 sophisticated algorithm for function reordering; we will likely want
1634 to use subsections to make the output functions appear in top-down
1635 order). */
1637 static void
1638 cgraph_expand_all_functions (void)
1640 struct cgraph_node *node;
1641 struct cgraph_node **order =
1642 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
1643 int order_pos = 0, new_order_pos = 0;
1644 int i;
1646 cgraph_mark_functions_to_output ();
1648 order_pos = cgraph_postorder (order);
1649 if (order_pos != cgraph_n_nodes)
1650 abort ();
1652 /* Garbage collector may remove inline clones we eliminate during
1653 optimization. So we must be sure to not reference them. */
1654 for (i = 0; i < order_pos; i++)
1655 if (order[i]->output)
1656 order[new_order_pos++] = order[i];
1658 for (i = new_order_pos - 1; i >= 0; i--)
1660 node = order[i];
1661 if (node->output)
1663 if (!node->reachable)
1664 abort ();
1665 node->output = 0;
1666 cgraph_expand_function (node);
1669 free (order);
1672 /* Mark all local functions.
1674 A local function is one whose calls can occur only in the
1675 current compilation unit and all its calls are explicit,
1676 so we can change its calling convention.
1677 We simply mark all static functions whose address is not taken
1678 as local. */
1680 static void
1681 cgraph_mark_local_functions (void)
1683 struct cgraph_node *node;
1685 if (cgraph_dump_file)
1686 fprintf (cgraph_dump_file, "\nMarking local functions:");
1688 /* Figure out functions we want to assemble. */
1689 for (node = cgraph_nodes; node; node = node->next)
1691 node->local.local = (!node->needed
1692 && DECL_SAVED_TREE (node->decl)
1693 && !TREE_PUBLIC (node->decl));
1694 if (cgraph_dump_file && node->local.local)
1695 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
1697 if (cgraph_dump_file)
1698 fprintf (cgraph_dump_file, "\n\n");
1701 /* Return true when function body of DECL still needs to be kept around
1702 for later re-use. */
1703 bool
1704 cgraph_preserve_function_body_p (tree decl)
1706 struct cgraph_node *node;
1707 /* Keep the body; we're going to dump it. */
1708 if (dump_enabled_p (TDI_all))
1709 return true;
1710 if (!cgraph_global_info_ready)
1711 return (DECL_INLINE (decl) && !flag_really_no_inline);
1712 /* Look if there is any clone around. */
1713 for (node = cgraph_node (decl); node; node = node->next_clone)
1714 if (node->global.inlined_to)
1715 return true;
1716 return false;
1719 /* Perform simple optimizations based on callgraph. */
1721 void
1722 cgraph_optimize (void)
1724 #ifdef ENABLE_CHECKING
1725 verify_cgraph ();
1726 #endif
1727 if (!flag_unit_at_a_time)
1728 return;
1729 timevar_push (TV_CGRAPHOPT);
1730 if (!quiet_flag)
1731 fprintf (stderr, "Performing intraprocedural optimizations\n");
1733 cgraph_mark_local_functions ();
1734 if (cgraph_dump_file)
1736 fprintf (cgraph_dump_file, "Marked ");
1737 dump_cgraph (cgraph_dump_file);
1740 if (flag_inline_trees)
1741 cgraph_decide_inlining ();
1742 cgraph_global_info_ready = true;
1743 if (cgraph_dump_file)
1745 fprintf (cgraph_dump_file, "Optimized ");
1746 dump_cgraph (cgraph_dump_file);
1748 timevar_pop (TV_CGRAPHOPT);
1750 /* Output everything. */
1751 if (!quiet_flag)
1752 fprintf (stderr, "Assembling functions:\n");
1753 #ifdef ENABLE_CHECKING
1754 verify_cgraph ();
1755 #endif
1756 cgraph_expand_all_functions ();
1757 if (cgraph_dump_file)
1759 fprintf (cgraph_dump_file, "\nFinal ");
1760 dump_cgraph (cgraph_dump_file);
1762 #ifdef ENABLE_CHECKING
1763 verify_cgraph ();
1764 /* Double check that all inline clones are gone and that all
1765 function bodies have been released from memory. */
1766 if (flag_unit_at_a_time
1767 && !dump_enabled_p (TDI_all)
1768 && !(sorrycount || errorcount))
1770 struct cgraph_node *node;
1771 bool error_found = false;
1773 for (node = cgraph_nodes; node; node = node->next)
1774 if (node->analyzed
1775 && (node->global.inlined_to
1776 || DECL_SAVED_TREE (node->decl)))
1778 error_found = true;
1779 dump_cgraph_node (stderr, node);
1781 if (error_found)
1782 internal_error ("Nodes with no released memory found.");
1784 #endif