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
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
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
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
42 - cgraph_finalize_compilation_unit
44 This function is called once compilation unit is finalized and it will
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.
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
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.
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
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 "coretypes.h"
171 #include "tree-inline.h"
172 #include "langhooks.h"
180 #include "diagnostic.h"
184 #include "c-common.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
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
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. */
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
))
232 /* Constructors and destructors are reachable from the runtime by
234 if (DECL_STATIC_CONSTRUCTOR (decl
) || DECL_STATIC_DESTRUCTOR (decl
))
237 /* If the user told us it is used, then it must be so. */
238 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl
)))
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
)))
248 if (flag_unit_at_a_time
)
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
))
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
))
262 /* We want to emit COMDAT functions only when absolutely necessary. */
263 if (DECL_COMDAT (decl
))
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
))))
276 /* When not doing unit-at-a-time, output all functions enqueued.
277 Return true when such a functions were found. */
280 cgraph_assemble_pending_functions (void)
284 if (flag_unit_at_a_time
)
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
);
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. */
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
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. */
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
)
357 notice_global_symbol (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. */
376 if (!cgraph_assemble_pending_functions ())
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. */
391 record_call_1 (tree
*tp
, int *walk_subtrees
, void *data
)
395 switch (TREE_CODE (t
))
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. */
402 cgraph_varpool_mark_needed_node (cgraph_varpool_node (t
));
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
));
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
,
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
))
446 if ((unsigned int) TREE_CODE (t
) >= LAST_AND_UNUSED_TREE_CODE
)
447 return lang_hooks
.callgraph
.analyze_expr (tp
, walk_subtrees
, data
);
454 /* Create cgraph edges for function calls inside BODY from NODE. */
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
474 verify_cgraph_node_1 (tree
*tp
, int *walk_subtrees
, void *data
)
479 if (TREE_CODE (t
) == CALL_EXPR
&& (decl
= get_callee_fndecl (t
)))
481 struct cgraph_edge
*e
= cgraph_edge (data
, t
);
486 error ("Shared call_expr:");
490 if (e
->callee
->decl
!= cgraph_node (decl
)->decl
)
492 error ("Edge points to wrong declaration:");
493 debug_tree (e
->callee
->decl
);
494 fprintf (stderr
," Instead of:");
501 error ("Missing callgraph edge for call expr:");
507 /* Save some cycles by not walking types and declaration as we
508 won't find anything useful there anyway. */
509 if (DECL_P (*tp
) || TYPE_P (*tp
))
515 /* Verify cgraph nodes of given cgraph node. */
517 verify_cgraph_node (struct cgraph_node
*node
)
519 struct cgraph_edge
*e
;
520 struct cgraph_node
*main_clone
;
522 timevar_push (TV_CGRAPH_VERIFY
);
524 for (e
= node
->callees
; e
; e
= e
->next_callee
)
527 error ("Aux field set for edge %s->%s",
528 cgraph_node_name (e
->caller
), cgraph_node_name (e
->callee
));
531 for (e
= node
->callers
; e
; e
= e
->next_caller
)
533 if (!e
->inline_failed
)
535 if (node
->global
.inlined_to
536 != (e
->caller
->global
.inlined_to
537 ? e
->caller
->global
.inlined_to
: e
->caller
))
539 error ("Inlined_to pointer is wrong");
542 if (node
->callers
->next_caller
)
544 error ("Multiple inline callers");
549 if (node
->global
.inlined_to
)
551 error ("Inlined_to pointer set for noninline callers");
555 if (!node
->callers
&& node
->global
.inlined_to
)
557 error ("Inlined_to pointer is set but no predecesors found");
560 if (node
->global
.inlined_to
== node
)
562 error ("Inlined_to pointer reffers to itself");
566 for (main_clone
= cgraph_node (node
->decl
); main_clone
;
567 main_clone
= main_clone
->next_clone
)
568 if (main_clone
== node
)
572 error ("Node not found in DECL_ASSEMBLER_NAME hash");
577 && DECL_SAVED_TREE (node
->decl
) && !TREE_ASM_WRITTEN (node
->decl
)
578 && (!DECL_EXTERNAL (node
->decl
) || node
->global
.inlined_to
))
580 walk_tree_without_duplicates (&DECL_SAVED_TREE (node
->decl
),
581 verify_cgraph_node_1
, node
);
582 for (e
= node
->callees
; e
; e
= e
->next_callee
)
586 error ("Edge %s->%s has no corresponding call_expr",
587 cgraph_node_name (e
->caller
),
588 cgraph_node_name (e
->callee
));
596 dump_cgraph_node (stderr
, node
);
597 internal_error ("verify_cgraph_node failed.");
599 timevar_pop (TV_CGRAPH_VERIFY
);
602 /* Verify whole cgraph structure. */
606 struct cgraph_node
*node
;
608 for (node
= cgraph_nodes
; node
; node
= node
->next
)
609 verify_cgraph_node (node
);
612 /* Analyze the function scheduled to be output. */
614 cgraph_analyze_function (struct cgraph_node
*node
)
616 tree decl
= node
->decl
;
617 struct cgraph_edge
*e
;
619 current_function_decl
= decl
;
621 /* First kill forward declaration so reverse inlining works properly. */
622 cgraph_create_edges (node
, DECL_SAVED_TREE (decl
));
624 node
->local
.inlinable
= tree_inlinable_function_p (decl
);
625 node
->local
.self_insns
= estimate_num_insns (DECL_SAVED_TREE (decl
));
626 if (node
->local
.inlinable
)
627 node
->local
.disregard_inline_limits
628 = lang_hooks
.tree_inlining
.disregard_inline_limits (decl
);
629 for (e
= node
->callers
; e
; e
= e
->next_caller
)
631 if (node
->local
.redefined_extern_inline
)
632 e
->inline_failed
= N_("redefined extern inline functions are not "
633 "considered for inlining");
634 else if (!node
->local
.inlinable
)
635 e
->inline_failed
= N_("function not inlinable");
637 e
->inline_failed
= N_("function not considered for inlining");
639 if (flag_really_no_inline
&& !node
->local
.disregard_inline_limits
)
640 node
->local
.inlinable
= 0;
641 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
642 node
->global
.insns
= node
->local
.self_insns
;
644 node
->analyzed
= true;
645 current_function_decl
= NULL
;
648 /* Analyze the whole compilation unit once it is parsed completely. */
651 cgraph_finalize_compilation_unit (void)
653 struct cgraph_node
*node
;
655 if (!flag_unit_at_a_time
)
657 cgraph_assemble_pending_functions ();
661 cgraph_varpool_assemble_pending_decls ();
663 fprintf (stderr
, "\nAnalyzing compilation unit\n");
665 timevar_push (TV_CGRAPH
);
666 if (cgraph_dump_file
)
668 fprintf (cgraph_dump_file
, "Initial entry points:");
669 for (node
= cgraph_nodes
; node
; node
= node
->next
)
670 if (node
->needed
&& DECL_SAVED_TREE (node
->decl
))
671 fprintf (cgraph_dump_file
, " %s", cgraph_node_name (node
));
672 fprintf (cgraph_dump_file
, "\n");
675 /* Propagate reachability flag and lower representation of all reachable
676 functions. In the future, lowering will introduce new functions and
677 new entry points on the way (by template instantiation and virtual
678 method table generation for instance). */
679 while (cgraph_nodes_queue
)
681 struct cgraph_edge
*edge
;
682 tree decl
= cgraph_nodes_queue
->decl
;
684 node
= cgraph_nodes_queue
;
685 cgraph_nodes_queue
= cgraph_nodes_queue
->next_needed
;
686 node
->next_needed
= NULL
;
688 /* ??? It is possible to create extern inline function and later using
689 weak alas attribute to kill its body. See
690 gcc.c-torture/compile/20011119-1.c */
691 if (!DECL_SAVED_TREE (decl
))
694 if (node
->analyzed
|| !node
->reachable
|| !DECL_SAVED_TREE (decl
))
697 cgraph_analyze_function (node
);
699 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
700 if (!edge
->callee
->reachable
)
701 cgraph_mark_reachable_node (edge
->callee
);
703 cgraph_varpool_assemble_pending_decls ();
706 /* Collect entry points to the unit. */
708 if (cgraph_dump_file
)
710 fprintf (cgraph_dump_file
, "Unit entry points:");
711 for (node
= cgraph_nodes
; node
; node
= node
->next
)
712 if (node
->needed
&& DECL_SAVED_TREE (node
->decl
))
713 fprintf (cgraph_dump_file
, " %s", cgraph_node_name (node
));
714 fprintf (cgraph_dump_file
, "\n\nInitial ");
715 dump_cgraph (cgraph_dump_file
);
718 if (cgraph_dump_file
)
719 fprintf (cgraph_dump_file
, "\nReclaiming functions:");
721 for (node
= cgraph_nodes
; node
; node
= node
->next
)
723 tree decl
= node
->decl
;
725 if (!node
->reachable
&& DECL_SAVED_TREE (decl
))
727 if (cgraph_dump_file
)
728 fprintf (cgraph_dump_file
, " %s", cgraph_node_name (node
));
729 cgraph_remove_node (node
);
732 node
->next_needed
= NULL
;
734 if (cgraph_dump_file
)
736 fprintf (cgraph_dump_file
, "\n\nReclaimed ");
737 dump_cgraph (cgraph_dump_file
);
740 timevar_pop (TV_CGRAPH
);
742 /* Figure out what functions we want to assemble. */
745 cgraph_mark_functions_to_output (void)
747 struct cgraph_node
*node
;
749 for (node
= cgraph_nodes
; node
; node
= node
->next
)
751 tree decl
= node
->decl
;
752 struct cgraph_edge
*e
;
756 for (e
= node
->callers
; e
; e
= e
->next_caller
)
757 if (e
->inline_failed
)
760 /* We need to output all local functions that are used and not
761 always inlined, as well as those that are reachable from
762 outside the current compilation unit. */
763 if (DECL_SAVED_TREE (decl
)
764 && !node
->global
.inlined_to
766 || (e
&& node
->reachable
))
767 && !TREE_ASM_WRITTEN (decl
)
768 && !DECL_EXTERNAL (decl
))
770 /* We should've reclaimed all functions that are not needed. */
771 else if (!node
->global
.inlined_to
&& DECL_SAVED_TREE (decl
)
772 && !DECL_EXTERNAL (decl
))
774 dump_cgraph_node (stderr
, node
);
780 /* Expand function specified by NODE. */
783 cgraph_expand_function (struct cgraph_node
*node
)
785 tree decl
= node
->decl
;
787 /* We ought to not compile any inline clones. */
788 if (node
->global
.inlined_to
)
791 if (flag_unit_at_a_time
)
792 announce_function (decl
);
794 /* Generate RTL for the body of DECL. Nested functions are expanded
795 via lang_expand_decl_stmt. */
796 lang_hooks
.callgraph
.expand_function (decl
);
798 /* Make sure that BE didn't give up on compiling. */
799 /* ??? Can happen with nested function of extern inline. */
800 if (!TREE_ASM_WRITTEN (node
->decl
))
803 current_function_decl
= NULL
;
804 if (DECL_SAVED_TREE (node
->decl
)
805 && !cgraph_preserve_function_body_p (node
->decl
))
807 DECL_SAVED_TREE (node
->decl
) = NULL
;
808 DECL_STRUCT_FUNCTION (node
->decl
) = NULL
;
809 DECL_INITIAL (node
->decl
) = error_mark_node
;
813 /* Fill array order with all nodes with output flag set in the reverse
814 topological order. */
817 cgraph_postorder (struct cgraph_node
**order
)
819 struct cgraph_node
*node
, *node2
;
822 struct cgraph_edge
*edge
, last
;
824 struct cgraph_node
**stack
=
825 xcalloc (cgraph_n_nodes
, sizeof (struct cgraph_node
*));
827 /* We have to deal with cycles nicely, so use a depth first traversal
828 output algorithm. Ignore the fact that some functions won't need
829 to be output and put them into order as well, so we get dependencies
830 right through intline functions. */
831 for (node
= cgraph_nodes
; node
; node
= node
->next
)
833 for (node
= cgraph_nodes
; node
; node
= node
->next
)
840 node
->aux
= node
->callers
;
843 while (node2
->aux
!= &last
)
846 if (edge
->next_caller
)
847 node2
->aux
= edge
->next_caller
;
850 if (!edge
->caller
->aux
)
852 if (!edge
->caller
->callers
)
853 edge
->caller
->aux
= &last
;
855 edge
->caller
->aux
= edge
->caller
->callers
;
856 stack
[stack_size
++] = node2
;
857 node2
= edge
->caller
;
861 if (node2
->aux
== &last
)
863 order
[order_pos
++] = node2
;
865 node2
= stack
[--stack_size
];
875 /* Perform reachability analysis and reclaim all unreachable nodes.
876 This function also remove unneeded bodies of extern inline functions
877 and thus needs to be done only after inlining decisions has been made. */
879 cgraph_remove_unreachable_nodes (void)
881 struct cgraph_node
*first
= (void *) 1;
882 struct cgraph_node
*node
;
883 bool changed
= false;
886 #ifdef ENABLE_CHECKING
889 if (cgraph_dump_file
)
890 fprintf (cgraph_dump_file
, "\nReclaiming functions:");
891 #ifdef ENABLE_CHECKING
892 for (node
= cgraph_nodes
; node
; node
= node
->next
)
896 for (node
= cgraph_nodes
; node
; node
= node
->next
)
897 if (node
->needed
&& !node
->global
.inlined_to
898 && (!DECL_EXTERNAL (node
->decl
) || !node
->analyzed
))
906 /* Perform reachability analysis. As a special case do not consider
907 extern inline functions not inlined as live because we won't output
909 while (first
!= (void *) 1)
911 struct cgraph_edge
*e
;
915 for (e
= node
->callees
; e
; e
= e
->next_callee
)
918 && (!e
->inline_failed
|| !e
->callee
->analyzed
919 || !DECL_EXTERNAL (e
->callee
->decl
)))
921 e
->callee
->aux
= first
;
926 /* Remove unreachable nodes. Extern inline functions need special care;
927 Unreachable extern inline functions shall be removed.
928 Reachable extern inline functions we never inlined shall get their bodies
930 Reachable extern inline functions we sometimes inlined will be turned into
931 unanalyzed nodes so they look like for true extern functions to the rest
932 of code. Body of such functions is released via remove_node once the
933 inline clones are eliminated. */
934 for (node
= cgraph_nodes
; node
; node
= node
->next
)
939 tree decl
= node
->decl
;
941 node
->global
.inlined_to
= NULL
;
942 if (DECL_STRUCT_FUNCTION (decl
))
943 local_insns
= node
->local
.self_insns
;
946 if (cgraph_dump_file
)
947 fprintf (cgraph_dump_file
, " %s", cgraph_node_name (node
));
948 if (!node
->analyzed
|| !DECL_EXTERNAL (node
->decl
))
949 cgraph_remove_node (node
);
952 struct cgraph_edge
*e
;
954 for (e
= node
->callers
; e
; e
= e
->next_caller
)
957 if (e
|| node
->needed
)
959 struct cgraph_node
*clone
;
961 for (clone
= node
->next_clone
; clone
;
962 clone
= clone
->next_clone
)
967 DECL_SAVED_TREE (node
->decl
) = NULL
;
968 DECL_STRUCT_FUNCTION (node
->decl
) = NULL
;
969 DECL_INITIAL (node
->decl
) = error_mark_node
;
971 while (node
->callees
)
972 cgraph_remove_edge (node
->callees
);
973 node
->analyzed
= false;
976 cgraph_remove_node (node
);
978 if (!DECL_SAVED_TREE (decl
))
979 insns
+= local_insns
;
983 for (node
= cgraph_nodes
; node
; node
= node
->next
)
985 if (cgraph_dump_file
)
986 fprintf (cgraph_dump_file
, "\nReclaimed %i insns", insns
);
990 /* Estimate size of the function after inlining WHAT into TO. */
993 cgraph_estimate_size_after_inlining (int times
, struct cgraph_node
*to
,
994 struct cgraph_node
*what
)
996 return (what
->global
.insns
- INSNS_PER_CALL
) * times
+ to
->global
.insns
;
999 /* Estimate the growth caused by inlining NODE into all callees. */
1002 cgraph_estimate_growth (struct cgraph_node
*node
)
1005 struct cgraph_edge
*e
;
1007 for (e
= node
->callers
; e
; e
= e
->next_caller
)
1008 if (e
->inline_failed
)
1009 growth
+= (cgraph_estimate_size_after_inlining (1, e
->caller
, node
)
1010 - e
->caller
->global
.insns
);
1012 /* ??? Wrong for self recursive functions or cases where we decide to not
1013 inline for different reasons, but it is not big deal as in that case
1014 we will keep the body around, but we will also avoid some inlining. */
1015 if (!node
->needed
&& !DECL_EXTERNAL (node
->decl
))
1016 growth
-= node
->global
.insns
;
1021 /* E is expected to be an edge being inlined. Clone destination node of
1022 the edge and redirect it to the new clone.
1023 DUPLICATE is used for bookkeeping on whether we are actually creating new
1024 clones or re-using node originally representing out-of-line function call.
1027 cgraph_clone_inlined_nodes (struct cgraph_edge
*e
, bool duplicate
)
1029 struct cgraph_node
*n
;
1031 /* We may eliminate the need for out-of-line copy to be output. In that
1032 case just go ahead and re-use it. */
1033 if (!e
->callee
->callers
->next_caller
1034 && (!e
->callee
->needed
|| DECL_EXTERNAL (e
->callee
->decl
))
1036 && flag_unit_at_a_time
)
1038 if (e
->callee
->global
.inlined_to
)
1040 if (!DECL_EXTERNAL (e
->callee
->decl
))
1041 overall_insns
-= e
->callee
->global
.insns
, nfunctions_inlined
++;
1046 n
= cgraph_clone_node (e
->callee
);
1047 cgraph_redirect_edge_callee (e
, n
);
1050 if (e
->caller
->global
.inlined_to
)
1051 e
->callee
->global
.inlined_to
= e
->caller
->global
.inlined_to
;
1053 e
->callee
->global
.inlined_to
= e
->caller
;
1055 /* Recursively clone all bodies. */
1056 for (e
= e
->callee
->callees
; e
; e
= e
->next_callee
)
1057 if (!e
->inline_failed
)
1058 cgraph_clone_inlined_nodes (e
, duplicate
);
1061 /* Mark edge E as inlined and update callgraph accordingly. */
1064 cgraph_mark_inline_edge (struct cgraph_edge
*e
)
1066 int old_insns
= 0, new_insns
= 0;
1067 struct cgraph_node
*to
= NULL
, *what
;
1069 if (!e
->inline_failed
)
1071 e
->inline_failed
= NULL
;
1073 if (!e
->callee
->global
.inlined
&& flag_unit_at_a_time
)
1074 DECL_POSSIBLY_INLINED (e
->callee
->decl
) = true;
1075 e
->callee
->global
.inlined
= true;
1077 cgraph_clone_inlined_nodes (e
, true);
1081 /* Now update size of caller and all functions caller is inlined into. */
1082 for (;e
&& !e
->inline_failed
; e
= e
->caller
->callers
)
1084 old_insns
= e
->caller
->global
.insns
;
1085 new_insns
= cgraph_estimate_size_after_inlining (1, e
->caller
,
1090 to
->global
.insns
= new_insns
;
1092 if (what
->global
.inlined_to
!= to
)
1094 overall_insns
+= new_insns
- old_insns
;
1098 /* Mark all calls of EDGE->CALLEE inlined into EDGE->CALLER.
1099 Return following unredirected edge in the list of callers
1102 static struct cgraph_edge
*
1103 cgraph_mark_inline (struct cgraph_edge
*edge
)
1105 struct cgraph_node
*to
= edge
->caller
;
1106 struct cgraph_node
*what
= edge
->callee
;
1107 struct cgraph_edge
*e
, *next
;
1110 /* Look for all calls, mark them inline and clone recursively
1111 all inlined functions. */
1112 for (e
= what
->callers
; e
; e
= next
)
1114 next
= e
->next_caller
;
1115 if (e
->caller
== to
&& e
->inline_failed
)
1117 cgraph_mark_inline_edge (e
);
1128 /* Return false when inlining WHAT into TO is not good idea
1129 as it would cause too large growth of function bodies. */
1132 cgraph_check_inline_limits (struct cgraph_node
*to
, struct cgraph_node
*what
,
1133 const char **reason
)
1136 struct cgraph_edge
*e
;
1140 if (to
->global
.inlined_to
)
1141 to
= to
->global
.inlined_to
;
1143 for (e
= to
->callees
; e
; e
= e
->next_callee
)
1144 if (e
->callee
== what
)
1147 /* When inlining large function body called once into small function,
1148 take the inlined function as base for limiting the growth. */
1149 if (to
->local
.self_insns
> what
->local
.self_insns
)
1150 limit
= to
->local
.self_insns
;
1152 limit
= what
->local
.self_insns
;
1154 limit
+= limit
* PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH
) / 100;
1156 newsize
= cgraph_estimate_size_after_inlining (times
, to
, what
);
1157 if (newsize
> PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS
)
1161 *reason
= N_("--param large-function-growth limit reached");
1167 /* Return true when function N is small enough to be inlined. */
1170 cgraph_default_inline_p (struct cgraph_node
*n
)
1172 if (!DECL_INLINE (n
->decl
) || !DECL_SAVED_TREE (n
->decl
))
1174 if (DECL_DECLARED_INLINE_P (n
->decl
))
1175 return n
->global
.insns
< MAX_INLINE_INSNS_SINGLE
;
1177 return n
->global
.insns
< MAX_INLINE_INSNS_AUTO
;
1180 /* Return true when inlining WHAT would create recursive inlining.
1181 We call recursive inlining all cases where same function appears more than
1182 once in the single recursion nest path in the inline graph. */
1185 cgraph_recursive_inlining_p (struct cgraph_node
*to
,
1186 struct cgraph_node
*what
,
1187 const char **reason
)
1190 if (to
->global
.inlined_to
)
1191 recursive
= what
->decl
== to
->global
.inlined_to
->decl
;
1193 recursive
= what
->decl
== to
->decl
;
1194 /* Marking recursive function inline has sane semantic and thus we should
1196 if (recursive
&& reason
)
1197 *reason
= (what
->local
.disregard_inline_limits
1198 ? N_("recursive inlining") : "");
1202 /* Recompute heap nodes for each of callees. */
1204 update_callee_keys (fibheap_t heap
, struct fibnode
**heap_node
,
1205 struct cgraph_node
*node
)
1207 struct cgraph_edge
*e
;
1209 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1210 if (e
->inline_failed
&& heap_node
[e
->callee
->uid
])
1211 fibheap_replace_key (heap
, heap_node
[e
->callee
->uid
],
1212 cgraph_estimate_growth (e
->callee
));
1213 else if (!e
->inline_failed
)
1214 update_callee_keys (heap
, heap_node
, e
->callee
);
1217 /* Enqueue all recursive calls from NODE into queue linked via aux pointers
1218 in between FIRST and LAST. WHERE is used for bookkeeping while looking
1219 int calls inlined within NODE. */
1221 lookup_recursive_calls (struct cgraph_node
*node
, struct cgraph_node
*where
,
1222 struct cgraph_edge
**first
, struct cgraph_edge
**last
)
1224 struct cgraph_edge
*e
;
1225 for (e
= where
->callees
; e
; e
= e
->next_callee
)
1226 if (e
->callee
== node
)
1234 for (e
= where
->callees
; e
; e
= e
->next_callee
)
1235 if (!e
->inline_failed
)
1236 lookup_recursive_calls (node
, e
->callee
, first
, last
);
1239 /* Decide on recursive inlining: in the case function has recursive calls,
1240 inline until body size reaches given argument. */
1242 cgraph_decide_recursive_inlining (struct cgraph_node
*node
)
1244 int limit
= PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO
);
1245 int max_depth
= PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO
);
1246 struct cgraph_edge
*first_call
= NULL
, *last_call
= NULL
;
1247 struct cgraph_edge
*last_in_current_depth
;
1248 struct cgraph_edge
*e
;
1249 struct cgraph_node
*master_clone
;
1253 if (DECL_DECLARED_INLINE_P (node
->decl
))
1255 limit
= PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE
);
1256 max_depth
= PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH
);
1259 /* Make sure that function is small enought to be considered for inlining. */
1261 || cgraph_estimate_size_after_inlining (1, node
, node
) >= limit
)
1263 lookup_recursive_calls (node
, node
, &first_call
, &last_call
);
1267 if (cgraph_dump_file
)
1268 fprintf (cgraph_dump_file
,
1269 "\nPerforming recursive inlining on %s\n",
1270 cgraph_node_name (node
));
1272 /* We need original clone to copy around. */
1273 master_clone
= cgraph_clone_node (node
);
1274 master_clone
->needed
= true;
1275 for (e
= master_clone
->callees
; e
; e
= e
->next_callee
)
1276 if (!e
->inline_failed
)
1277 cgraph_clone_inlined_nodes (e
, true);
1279 /* Do the inlining and update list of recursive call during process. */
1280 last_in_current_depth
= last_call
;
1282 && cgraph_estimate_size_after_inlining (1, node
, master_clone
) <= limit
)
1284 struct cgraph_edge
*curr
= first_call
;
1286 first_call
= first_call
->aux
;
1289 cgraph_redirect_edge_callee (curr
, master_clone
);
1290 cgraph_mark_inline_edge (curr
);
1291 lookup_recursive_calls (node
, curr
->callee
, &first_call
, &last_call
);
1293 if (last_in_current_depth
1294 && ++depth
>= max_depth
)
1299 /* Cleanup queue pointers. */
1302 struct cgraph_edge
*next
= first_call
->aux
;
1303 first_call
->aux
= NULL
;
1306 if (cgraph_dump_file
)
1307 fprintf (cgraph_dump_file
,
1308 "\n Inlined %i times, body grown from %i to %i insns\n", n
,
1309 master_clone
->global
.insns
, node
->global
.insns
);
1311 /* Remove master clone we used for inlining. We rely that clones inlined
1312 into master clone gets queued just before master clone so we don't
1314 for (node
= cgraph_nodes
; node
!= master_clone
;
1316 if (node
->global
.inlined_to
== master_clone
)
1317 cgraph_remove_node (node
);
1318 cgraph_remove_node (master_clone
);
1321 /* Set inline_failed for all callers of given function to REASON. */
1324 cgraph_set_inline_failed (struct cgraph_node
*node
, const char *reason
)
1326 struct cgraph_edge
*e
;
1328 if (cgraph_dump_file
)
1329 fprintf (cgraph_dump_file
, "Inlining failed: %s\n", reason
);
1330 for (e
= node
->callers
; e
; e
= e
->next_caller
)
1331 if (e
->inline_failed
)
1332 e
->inline_failed
= reason
;
1335 /* We use greedy algorithm for inlining of small functions:
1336 All inline candidates are put into prioritized heap based on estimated
1337 growth of the overall number of instructions and then update the estimates.
1339 INLINED and INLINED_CALEES are just pointers to arrays large enough
1340 to be passed to cgraph_inlined_into and cgraph_inlined_callees. */
1343 cgraph_decide_inlining_of_small_functions (void)
1345 struct cgraph_node
*node
;
1346 fibheap_t heap
= fibheap_new ();
1347 struct fibnode
**heap_node
=
1348 xcalloc (cgraph_max_uid
, sizeof (struct fibnode
*));
1349 int max_insns
= ((HOST_WIDEST_INT
) initial_insns
1350 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH
)) / 100);
1352 /* Put all inline candidates into the heap. */
1354 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1356 if (!node
->local
.inlinable
|| !node
->callers
1357 || node
->local
.disregard_inline_limits
)
1360 if (!cgraph_default_inline_p (node
))
1362 cgraph_set_inline_failed (node
,
1363 N_("--param max-inline-insns-single limit reached"));
1366 heap_node
[node
->uid
] =
1367 fibheap_insert (heap
, cgraph_estimate_growth (node
), node
);
1370 if (cgraph_dump_file
)
1371 fprintf (cgraph_dump_file
, "\nDeciding on smaller functions:\n");
1372 while (overall_insns
<= max_insns
&& (node
= fibheap_extract_min (heap
)))
1374 struct cgraph_edge
*e
, *next
;
1375 int old_insns
= overall_insns
;
1377 heap_node
[node
->uid
] = NULL
;
1378 if (cgraph_dump_file
)
1379 fprintf (cgraph_dump_file
,
1380 "\nConsidering %s with %i insns\n"
1381 " Estimated growth is %+i insns.\n",
1382 cgraph_node_name (node
), node
->global
.insns
,
1383 cgraph_estimate_growth (node
));
1384 if (!cgraph_default_inline_p (node
))
1386 cgraph_set_inline_failed (node
,
1387 N_("--param max-inline-insns-single limit reached after inlining into the callee"));
1390 for (e
= node
->callers
; e
; e
= next
)
1392 next
= e
->next_caller
;
1393 if (e
->inline_failed
)
1395 struct cgraph_node
*where
;
1397 if (cgraph_recursive_inlining_p (e
->caller
, e
->callee
,
1399 || !cgraph_check_inline_limits (e
->caller
, e
->callee
,
1402 if (cgraph_dump_file
)
1403 fprintf (cgraph_dump_file
, " Not inlining into %s:%s.\n",
1404 cgraph_node_name (e
->caller
), e
->inline_failed
);
1407 next
= cgraph_mark_inline (e
);
1409 if (where
->global
.inlined_to
)
1410 where
= where
->global
.inlined_to
;
1412 if (heap_node
[where
->uid
])
1413 fibheap_replace_key (heap
, heap_node
[where
->uid
],
1414 cgraph_estimate_growth (where
));
1416 if (cgraph_dump_file
)
1417 fprintf (cgraph_dump_file
,
1418 " Inlined into %s which now has %i insns.\n",
1419 cgraph_node_name (e
->caller
),
1420 e
->caller
->global
.insns
);
1424 cgraph_decide_recursive_inlining (node
);
1426 /* Similarly all functions called by the function we just inlined
1427 are now called more times; update keys. */
1428 update_callee_keys (heap
, heap_node
, node
);
1430 if (cgraph_dump_file
)
1431 fprintf (cgraph_dump_file
,
1432 " Inlined for a net change of %+i insns.\n",
1433 overall_insns
- old_insns
);
1435 while ((node
= fibheap_extract_min (heap
)) != NULL
)
1436 if (!node
->local
.disregard_inline_limits
)
1437 cgraph_set_inline_failed (node
, N_("--param inline-unit-growth limit reached"));
1438 fibheap_delete (heap
);
1442 /* Decide on the inlining. We do so in the topological order to avoid
1443 expenses on updating data structures. */
1446 cgraph_decide_inlining (void)
1448 struct cgraph_node
*node
;
1450 struct cgraph_node
**order
=
1451 xcalloc (cgraph_n_nodes
, sizeof (struct cgraph_node
*));
1455 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1456 initial_insns
+= node
->local
.self_insns
;
1457 overall_insns
= initial_insns
;
1459 nnodes
= cgraph_postorder (order
);
1461 if (cgraph_dump_file
)
1462 fprintf (cgraph_dump_file
,
1463 "\nDeciding on inlining. Starting with %i insns.\n",
1466 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1469 if (cgraph_dump_file
)
1470 fprintf (cgraph_dump_file
, "\nInlining always_inline functions:\n");
1472 /* In the first pass mark all always_inline edges. Do this with a priority
1473 so none of our later choices will make this impossible. */
1474 for (i
= nnodes
- 1; i
>= 0; i
--)
1476 struct cgraph_edge
*e
, *next
;
1480 if (!node
->local
.disregard_inline_limits
)
1482 if (cgraph_dump_file
)
1483 fprintf (cgraph_dump_file
,
1484 "\nConsidering %s %i insns (always inline)\n",
1485 cgraph_node_name (node
), node
->global
.insns
);
1486 old_insns
= overall_insns
;
1487 for (e
= node
->callers
; e
; e
= next
)
1489 next
= e
->next_caller
;
1490 if (!e
->inline_failed
)
1492 if (cgraph_recursive_inlining_p (e
->caller
, e
->callee
,
1495 cgraph_mark_inline_edge (e
);
1496 if (cgraph_dump_file
)
1497 fprintf (cgraph_dump_file
,
1498 " Inlined into %s which now has %i insns.\n",
1499 cgraph_node_name (e
->caller
),
1500 e
->caller
->global
.insns
);
1502 if (cgraph_dump_file
)
1503 fprintf (cgraph_dump_file
,
1504 " Inlined for a net change of %+i insns.\n",
1505 overall_insns
- old_insns
);
1508 if (!flag_really_no_inline
)
1510 cgraph_decide_inlining_of_small_functions ();
1512 if (cgraph_dump_file
)
1513 fprintf (cgraph_dump_file
, "\nDeciding on functions called once:\n");
1515 /* And finally decide what functions are called once. */
1517 for (i
= nnodes
- 1; i
>= 0; i
--)
1521 if (node
->callers
&& !node
->callers
->next_caller
&& !node
->needed
1522 && node
->local
.inlinable
&& node
->callers
->inline_failed
1523 && !DECL_EXTERNAL (node
->decl
) && !DECL_COMDAT (node
->decl
))
1526 struct cgraph_node
*node1
;
1528 /* Verify that we won't duplicate the caller. */
1529 for (node1
= node
->callers
->caller
;
1530 node1
->callers
&& !node1
->callers
->inline_failed
1531 && ok
; node1
= node1
->callers
->caller
)
1532 if (node1
->callers
->next_caller
|| node1
->needed
)
1536 if (cgraph_dump_file
)
1537 fprintf (cgraph_dump_file
,
1538 "\nConsidering %s %i insns.\n"
1539 " Called once from %s %i insns.\n",
1540 cgraph_node_name (node
), node
->global
.insns
,
1541 cgraph_node_name (node
->callers
->caller
),
1542 node
->callers
->caller
->global
.insns
);
1544 old_insns
= overall_insns
;
1546 if (cgraph_check_inline_limits (node
->callers
->caller
, node
,
1549 cgraph_mark_inline (node
->callers
);
1550 if (cgraph_dump_file
)
1551 fprintf (cgraph_dump_file
,
1552 " Inlined into %s which now has %i insns"
1553 " for a net change of %+i insns.\n",
1554 cgraph_node_name (node
->callers
->caller
),
1555 node
->callers
->caller
->global
.insns
,
1556 overall_insns
- old_insns
);
1560 if (cgraph_dump_file
)
1561 fprintf (cgraph_dump_file
,
1562 " Inline limit reached, not inlined.\n");
1569 /* We will never output extern functions we didn't inline.
1570 ??? Perhaps we can prevent accounting of growth of external
1571 inline functions. */
1572 cgraph_remove_unreachable_nodes ();
1574 if (cgraph_dump_file
)
1575 fprintf (cgraph_dump_file
,
1576 "\nInlined %i calls, eliminated %i functions, "
1577 "%i insns turned to %i insns.\n\n",
1578 ncalls_inlined
, nfunctions_inlined
, initial_insns
,
1583 /* Decide on the inlining. We do so in the topological order to avoid
1584 expenses on updating data structures. */
1587 cgraph_decide_inlining_incrementally (struct cgraph_node
*node
)
1589 struct cgraph_edge
*e
;
1591 /* First of all look for always inline functions. */
1592 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1593 if (e
->callee
->local
.disregard_inline_limits
1595 && !cgraph_recursive_inlining_p (node
, e
->callee
, &e
->inline_failed
)
1596 /* ??? It is possible that renaming variable removed the function body
1597 in duplicate_decls. See gcc.c-torture/compile/20011119-2.c */
1598 && DECL_SAVED_TREE (e
->callee
->decl
))
1599 cgraph_mark_inline (e
);
1601 /* Now do the automatic inlining. */
1602 if (!flag_really_no_inline
)
1603 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1604 if (e
->callee
->local
.inlinable
1606 && !e
->callee
->local
.disregard_inline_limits
1607 && !cgraph_recursive_inlining_p (node
, e
->callee
, &e
->inline_failed
)
1608 && cgraph_check_inline_limits (node
, e
->callee
, &e
->inline_failed
)
1609 && DECL_SAVED_TREE (e
->callee
->decl
))
1611 if (cgraph_default_inline_p (e
->callee
))
1612 cgraph_mark_inline (e
);
1615 = N_("--param max-inline-insns-single limit reached");
1620 /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */
1623 cgraph_inline_p (struct cgraph_edge
*e
, const char **reason
)
1625 *reason
= e
->inline_failed
;
1626 return !e
->inline_failed
;
1629 /* Expand all functions that must be output.
1631 Attempt to topologically sort the nodes so function is output when
1632 all called functions are already assembled to allow data to be
1633 propagated across the callgraph. Use a stack to get smaller distance
1634 between a function and its callees (later we may choose to use a more
1635 sophisticated algorithm for function reordering; we will likely want
1636 to use subsections to make the output functions appear in top-down
1640 cgraph_expand_all_functions (void)
1642 struct cgraph_node
*node
;
1643 struct cgraph_node
**order
=
1644 xcalloc (cgraph_n_nodes
, sizeof (struct cgraph_node
*));
1645 int order_pos
= 0, new_order_pos
= 0;
1648 cgraph_mark_functions_to_output ();
1650 order_pos
= cgraph_postorder (order
);
1651 if (order_pos
!= cgraph_n_nodes
)
1654 /* Garbage collector may remove inline clones we eliminate during
1655 optimization. So we must be sure to not reference them. */
1656 for (i
= 0; i
< order_pos
; i
++)
1657 if (order
[i
]->output
)
1658 order
[new_order_pos
++] = order
[i
];
1660 for (i
= new_order_pos
- 1; i
>= 0; i
--)
1665 if (!node
->reachable
)
1668 cgraph_expand_function (node
);
1674 /* Mark all local functions.
1676 A local function is one whose calls can occur only in the
1677 current compilation unit and all its calls are explicit,
1678 so we can change its calling convention.
1679 We simply mark all static functions whose address is not taken
1683 cgraph_mark_local_functions (void)
1685 struct cgraph_node
*node
;
1687 if (cgraph_dump_file
)
1688 fprintf (cgraph_dump_file
, "\nMarking local functions:");
1690 /* Figure out functions we want to assemble. */
1691 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1693 node
->local
.local
= (!node
->needed
1694 && DECL_SAVED_TREE (node
->decl
)
1695 && !TREE_PUBLIC (node
->decl
));
1696 if (cgraph_dump_file
&& node
->local
.local
)
1697 fprintf (cgraph_dump_file
, " %s", cgraph_node_name (node
));
1699 if (cgraph_dump_file
)
1700 fprintf (cgraph_dump_file
, "\n\n");
1703 /* Return true when function body of DECL still needs to be kept around
1704 for later re-use. */
1706 cgraph_preserve_function_body_p (tree decl
)
1708 struct cgraph_node
*node
;
1709 /* Keep the body; we're going to dump it. */
1710 if (dump_enabled_p (TDI_all
))
1712 if (!cgraph_global_info_ready
)
1713 return (DECL_INLINE (decl
) && !flag_really_no_inline
);
1714 /* Look if there is any clone around. */
1715 for (node
= cgraph_node (decl
); node
; node
= node
->next_clone
)
1716 if (node
->global
.inlined_to
)
1721 /* Perform simple optimizations based on callgraph. */
1724 cgraph_optimize (void)
1726 #ifdef ENABLE_CHECKING
1729 if (!flag_unit_at_a_time
)
1731 timevar_push (TV_CGRAPHOPT
);
1733 fprintf (stderr
, "Performing intraprocedural optimizations\n");
1735 cgraph_mark_local_functions ();
1736 if (cgraph_dump_file
)
1738 fprintf (cgraph_dump_file
, "Marked ");
1739 dump_cgraph (cgraph_dump_file
);
1742 if (flag_inline_trees
)
1743 cgraph_decide_inlining ();
1744 cgraph_global_info_ready
= true;
1745 if (cgraph_dump_file
)
1747 fprintf (cgraph_dump_file
, "Optimized ");
1748 dump_cgraph (cgraph_dump_file
);
1750 timevar_pop (TV_CGRAPHOPT
);
1752 /* Output everything. */
1754 fprintf (stderr
, "Assembling functions:\n");
1755 #ifdef ENABLE_CHECKING
1758 cgraph_expand_all_functions ();
1759 if (cgraph_dump_file
)
1761 fprintf (cgraph_dump_file
, "\nFinal ");
1762 dump_cgraph (cgraph_dump_file
);
1764 #ifdef ENABLE_CHECKING
1766 /* Double check that all inline clones are gone and that all
1767 function bodies have been released from memory. */
1768 if (flag_unit_at_a_time
1769 && !dump_enabled_p (TDI_all
)
1770 && !(sorrycount
|| errorcount
))
1772 struct cgraph_node
*node
;
1773 bool error_found
= false;
1775 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1777 && (node
->global
.inlined_to
1778 || DECL_SAVED_TREE (node
->decl
)))
1781 dump_cgraph_node (stderr
, node
);
1784 internal_error ("Nodes with no released memory found.");