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"
172 #include "tree-inline.h"
173 #include "langhooks.h"
181 #include "diagnostic.h"
185 #include "c-common.h"
187 #include "function.h"
189 #define INSNS_PER_CALL 10
191 static void cgraph_expand_all_functions (void);
192 static void cgraph_mark_functions_to_output (void);
193 static void cgraph_expand_function (struct cgraph_node *);
194 static tree record_call_1 (tree *, int *, void *);
195 static void cgraph_mark_local_functions (void);
196 static bool cgraph_default_inline_p (struct cgraph_node *n);
197 static void cgraph_analyze_function (struct cgraph_node *node);
198 static void cgraph_decide_inlining_incrementally (struct cgraph_node *);
200 /* Statistics we collect about inlining algorithm. */
201 static int ncalls_inlined;
202 static int nfunctions_inlined;
203 static int initial_insns;
204 static int overall_insns;
206 /* Records tree nodes seen in cgraph_create_edges. Simply using
207 walk_tree_without_duplicates doesn't guarantee each node is visited
208 once because it gets a new htab upon each recursive call from
210 static htab_t visited_nodes;
212 /* Determine if function DECL is needed. That is, visible to something
213 either outside this translation unit, something magic in the system
214 configury, or (if not doing unit-at-a-time) to something we havn't
218 decide_is_function_needed (struct cgraph_node *node, tree decl)
220 struct cgraph_node *origin;
222 /* If we decided it was needed before, but at the time we didn't have
223 the body of the function available, then it's still needed. We have
224 to go back and re-check its dependencies now. */
228 /* Externally visible functions must be output. The exception is
229 COMDAT functions that must be output only when they are needed. */
230 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
233 /* Constructors and destructors are reachable from the runtime by
235 if (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl))
238 /* If the user told us it is used, then it must be so. */
239 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
242 /* ??? If the assembler name is set by hand, it is possible to assemble
243 the name later after finalizing the function and the fact is noticed
244 in assemble_name then. This is arguably a bug. */
245 if (DECL_ASSEMBLER_NAME_SET_P (decl)
246 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
249 if (flag_unit_at_a_time)
252 /* If not doing unit at a time, then we'll only defer this function
253 if its marked for inlining. Otherwise we want to emit it now. */
255 /* "extern inline" functions are never output locally. */
256 if (DECL_EXTERNAL (decl))
258 /* Nested functions of extern inline function shall not be emit unless
259 we inlined the origin. */
260 for (origin = node->origin; origin; origin = origin->origin)
261 if (DECL_EXTERNAL (origin->decl))
263 /* We want to emit COMDAT functions only when absolutely necessary. */
264 if (DECL_COMDAT (decl))
266 if (!DECL_INLINE (decl)
267 || (!node->local.disregard_inline_limits
268 /* When declared inline, defer even the uninlinable functions.
269 This allows them to be eliminated when unused. */
270 && !DECL_DECLARED_INLINE_P (decl)
271 && (!node->local.inlinable || !cgraph_default_inline_p (node))))
277 /* When not doing unit-at-a-time, output all functions enqueued.
278 Return true when such a functions were found. */
281 cgraph_assemble_pending_functions (void)
285 if (flag_unit_at_a_time)
288 while (cgraph_nodes_queue)
290 struct cgraph_node *n = cgraph_nodes_queue;
292 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
293 n->next_needed = NULL;
294 if (!n->global.inlined_to && !DECL_EXTERNAL (n->decl))
296 cgraph_expand_function (n);
304 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
305 logic in effect. If NESTED is true, then our caller cannot stand to have
306 the garbage collector run at the moment. We would need to either create
307 a new GC context, or just not compile right now. */
310 cgraph_finalize_function (tree decl, bool nested)
312 struct cgraph_node *node = cgraph_node (decl);
314 if (node->local.finalized)
316 /* As an GCC extension we allow redefinition of the function. The
317 semantics when both copies of bodies differ is not well defined.
318 We replace the old body with new body so in unit at a time mode
319 we always use new body, while in normal mode we may end up with
320 old body inlined into some functions and new body expanded and
323 ??? It may make more sense to use one body for inlining and other
324 body for expanding the function but this is difficult to do. */
326 /* If node->output is set, then this is a unit-at-a-time compilation
327 and we have already begun whole-unit analysis. This is *not*
328 testing for whether we've already emitted the function. That
329 case can be sort-of legitimately seen with real function
330 redefinition errors. I would argue that the front end should
331 never present us with such a case, but don't enforce that for now. */
332 gcc_assert (!node->output);
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. */
403 cgraph_varpool_mark_needed_node (cgraph_varpool_node (t));
404 if (lang_hooks.callgraph.analyze_expr)
405 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees,
411 if (flag_unit_at_a_time)
413 /* Record dereferences to the functions. This makes the
414 functions reachable unconditionally. */
415 tree decl = TREE_OPERAND (*tp, 0);
416 if (TREE_CODE (decl) == FUNCTION_DECL)
417 cgraph_mark_needed_node (cgraph_node (decl));
423 tree decl = get_callee_fndecl (*tp);
424 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
426 cgraph_create_edge (data, cgraph_node (decl), *tp);
428 /* When we see a function call, we don't want to look at the
429 function reference in the ADDR_EXPR that is hanging from
430 the CALL_EXPR we're examining here, because we would
431 conclude incorrectly that the function's address could be
432 taken by something that is not a function call. So only
433 walk the function parameter list, skip the other subtrees. */
435 walk_tree (&TREE_OPERAND (*tp, 1), record_call_1, data,
443 /* Save some cycles by not walking types and declaration as we
444 won't find anything useful there anyway. */
445 if (DECL_P (*tp) || TYPE_P (*tp))
451 if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
452 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees, data);
459 /* Create cgraph edges for function calls inside BODY from NODE. */
462 cgraph_create_edges (struct cgraph_node *node, tree body)
464 /* The nodes we're interested in are never shared, so walk
465 the tree ignoring duplicates. */
466 visited_nodes = htab_create (37, htab_hash_pointer,
467 htab_eq_pointer, NULL);
468 walk_tree (&body, record_call_1, node, visited_nodes);
469 htab_delete (visited_nodes);
470 visited_nodes = NULL;
473 static bool error_found;
475 /* Callbrack of verify_cgraph_node. Check that all call_exprs have cgraph
479 verify_cgraph_node_1 (tree *tp, int *walk_subtrees, void *data)
484 if (TREE_CODE (t) == CALL_EXPR && (decl = get_callee_fndecl (t)))
486 struct cgraph_edge *e = cgraph_edge (data, t);
491 error ("Shared call_expr:");
495 if (e->callee->decl != cgraph_node (decl)->decl)
497 error ("Edge points to wrong declaration:");
498 debug_tree (e->callee->decl);
499 fprintf (stderr," Instead of:");
506 error ("Missing callgraph edge for call expr:");
512 /* Save some cycles by not walking types and declaration as we
513 won't find anything useful there anyway. */
514 if (DECL_P (*tp) || TYPE_P (*tp))
520 /* Verify cgraph nodes of given cgraph node. */
522 verify_cgraph_node (struct cgraph_node *node)
524 struct cgraph_edge *e;
525 struct cgraph_node *main_clone;
527 timevar_push (TV_CGRAPH_VERIFY);
529 for (e = node->callees; e; e = e->next_callee)
532 error ("Aux field set for edge %s->%s",
533 cgraph_node_name (e->caller), cgraph_node_name (e->callee));
536 for (e = node->callers; e; e = e->next_caller)
538 if (!e->inline_failed)
540 if (node->global.inlined_to
541 != (e->caller->global.inlined_to
542 ? e->caller->global.inlined_to : e->caller))
544 error ("Inlined_to pointer is wrong");
547 if (node->callers->next_caller)
549 error ("Multiple inline callers");
554 if (node->global.inlined_to)
556 error ("Inlined_to pointer set for noninline callers");
560 if (!node->callers && node->global.inlined_to)
562 error ("Inlined_to pointer is set but no predecesors found");
565 if (node->global.inlined_to == node)
567 error ("Inlined_to pointer reffers to itself");
571 for (main_clone = cgraph_node (node->decl); main_clone;
572 main_clone = main_clone->next_clone)
573 if (main_clone == node)
577 error ("Node not found in DECL_ASSEMBLER_NAME hash");
582 && DECL_SAVED_TREE (node->decl) && !TREE_ASM_WRITTEN (node->decl)
583 && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to))
585 walk_tree_without_duplicates (&DECL_SAVED_TREE (node->decl),
586 verify_cgraph_node_1, node);
587 for (e = node->callees; e; e = e->next_callee)
591 error ("Edge %s->%s has no corresponding call_expr",
592 cgraph_node_name (e->caller),
593 cgraph_node_name (e->callee));
601 dump_cgraph_node (stderr, node);
602 internal_error ("verify_cgraph_node failed.");
604 timevar_pop (TV_CGRAPH_VERIFY);
607 /* Verify whole cgraph structure. */
611 struct cgraph_node *node;
613 if (sorrycount || errorcount)
616 for (node = cgraph_nodes; node; node = node->next)
617 verify_cgraph_node (node);
620 /* Analyze the function scheduled to be output. */
622 cgraph_analyze_function (struct cgraph_node *node)
624 tree decl = node->decl;
625 struct cgraph_edge *e;
627 current_function_decl = decl;
629 /* First kill forward declaration so reverse inlining works properly. */
630 cgraph_create_edges (node, DECL_SAVED_TREE (decl));
632 node->local.inlinable = tree_inlinable_function_p (decl);
633 node->local.self_insns = estimate_num_insns (DECL_SAVED_TREE (decl));
634 if (node->local.inlinable)
635 node->local.disregard_inline_limits
636 = lang_hooks.tree_inlining.disregard_inline_limits (decl);
637 for (e = node->callers; e; e = e->next_caller)
639 if (node->local.redefined_extern_inline)
640 e->inline_failed = N_("redefined extern inline functions are not "
641 "considered for inlining");
642 else if (!node->local.inlinable)
643 e->inline_failed = N_("function not inlinable");
645 e->inline_failed = N_("function not considered for inlining");
647 if (flag_really_no_inline && !node->local.disregard_inline_limits)
648 node->local.inlinable = 0;
649 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
650 node->global.insns = node->local.self_insns;
652 node->analyzed = true;
653 current_function_decl = NULL;
656 /* Analyze the whole compilation unit once it is parsed completely. */
659 cgraph_finalize_compilation_unit (void)
661 struct cgraph_node *node;
663 if (!flag_unit_at_a_time)
665 cgraph_assemble_pending_functions ();
669 cgraph_varpool_assemble_pending_decls ();
671 fprintf (stderr, "\nAnalyzing compilation unit\n");
673 timevar_push (TV_CGRAPH);
674 if (cgraph_dump_file)
676 fprintf (cgraph_dump_file, "Initial entry points:");
677 for (node = cgraph_nodes; node; node = node->next)
678 if (node->needed && DECL_SAVED_TREE (node->decl))
679 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
680 fprintf (cgraph_dump_file, "\n");
683 /* Propagate reachability flag and lower representation of all reachable
684 functions. In the future, lowering will introduce new functions and
685 new entry points on the way (by template instantiation and virtual
686 method table generation for instance). */
687 while (cgraph_nodes_queue)
689 struct cgraph_edge *edge;
690 tree decl = cgraph_nodes_queue->decl;
692 node = cgraph_nodes_queue;
693 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
694 node->next_needed = NULL;
696 /* ??? It is possible to create extern inline function and later using
697 weak alas attribute to kill its body. See
698 gcc.c-torture/compile/20011119-1.c */
699 if (!DECL_SAVED_TREE (decl))
702 gcc_assert (!node->analyzed && node->reachable);
703 gcc_assert (DECL_SAVED_TREE (decl));
705 cgraph_analyze_function (node);
707 for (edge = node->callees; edge; edge = edge->next_callee)
708 if (!edge->callee->reachable)
709 cgraph_mark_reachable_node (edge->callee);
711 cgraph_varpool_assemble_pending_decls ();
714 /* Collect entry points to the unit. */
716 if (cgraph_dump_file)
718 fprintf (cgraph_dump_file, "Unit entry points:");
719 for (node = cgraph_nodes; node; node = node->next)
720 if (node->needed && DECL_SAVED_TREE (node->decl))
721 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
722 fprintf (cgraph_dump_file, "\n\nInitial ");
723 dump_cgraph (cgraph_dump_file);
726 if (cgraph_dump_file)
727 fprintf (cgraph_dump_file, "\nReclaiming functions:");
729 for (node = cgraph_nodes; node; node = node->next)
731 tree decl = node->decl;
733 if (!node->reachable && DECL_SAVED_TREE (decl))
735 if (cgraph_dump_file)
736 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
737 cgraph_remove_node (node);
740 node->next_needed = NULL;
742 if (cgraph_dump_file)
744 fprintf (cgraph_dump_file, "\n\nReclaimed ");
745 dump_cgraph (cgraph_dump_file);
748 timevar_pop (TV_CGRAPH);
750 /* Figure out what functions we want to assemble. */
753 cgraph_mark_functions_to_output (void)
755 struct cgraph_node *node;
757 for (node = cgraph_nodes; node; node = node->next)
759 tree decl = node->decl;
760 struct cgraph_edge *e;
762 gcc_assert (!node->output);
764 for (e = node->callers; e; e = e->next_caller)
765 if (e->inline_failed)
768 /* We need to output all local functions that are used and not
769 always inlined, as well as those that are reachable from
770 outside the current compilation unit. */
771 if (DECL_SAVED_TREE (decl)
772 && !node->global.inlined_to
774 || (e && node->reachable))
775 && !TREE_ASM_WRITTEN (decl)
776 && !DECL_EXTERNAL (decl))
779 /* We should've reclaimed all functions that are not needed. */
780 gcc_assert (node->global.inlined_to || !DECL_SAVED_TREE (decl)
781 || DECL_EXTERNAL (decl));
785 /* Expand function specified by NODE. */
788 cgraph_expand_function (struct cgraph_node *node)
790 tree decl = node->decl;
792 /* We ought to not compile any inline clones. */
793 gcc_assert (!node->global.inlined_to);
795 if (flag_unit_at_a_time)
796 announce_function (decl);
798 /* Generate RTL for the body of DECL. */
799 lang_hooks.callgraph.expand_function (decl);
801 /* Make sure that BE didn't give up on compiling. */
802 /* ??? Can happen with nested function of extern inline. */
803 gcc_assert (TREE_ASM_WRITTEN (node->decl));
805 current_function_decl = NULL;
806 if (!cgraph_preserve_function_body_p (node->decl))
808 DECL_SAVED_TREE (node->decl) = NULL;
809 DECL_STRUCT_FUNCTION (node->decl) = NULL;
810 DECL_INITIAL (node->decl) = error_mark_node;
811 /* Elliminate all call edges. This is important so the call_expr no longer
812 points to the dead function body. */
813 while (node->callees)
814 cgraph_remove_edge (node->callees);
818 /* Fill array order with all nodes with output flag set in the reverse
819 topological order. */
822 cgraph_postorder (struct cgraph_node **order)
824 struct cgraph_node *node, *node2;
827 struct cgraph_edge *edge, last;
829 struct cgraph_node **stack =
830 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
832 /* We have to deal with cycles nicely, so use a depth first traversal
833 output algorithm. Ignore the fact that some functions won't need
834 to be output and put them into order as well, so we get dependencies
835 right through intline functions. */
836 for (node = cgraph_nodes; node; node = node->next)
838 for (node = cgraph_nodes; node; node = node->next)
845 node->aux = node->callers;
848 while (node2->aux != &last)
851 if (edge->next_caller)
852 node2->aux = edge->next_caller;
855 if (!edge->caller->aux)
857 if (!edge->caller->callers)
858 edge->caller->aux = &last;
860 edge->caller->aux = edge->caller->callers;
861 stack[stack_size++] = node2;
862 node2 = edge->caller;
866 if (node2->aux == &last)
868 order[order_pos++] = node2;
870 node2 = stack[--stack_size];
880 /* Perform reachability analysis and reclaim all unreachable nodes.
881 This function also remove unneeded bodies of extern inline functions
882 and thus needs to be done only after inlining decisions has been made. */
884 cgraph_remove_unreachable_nodes (void)
886 struct cgraph_node *first = (void *) 1;
887 struct cgraph_node *node;
888 bool changed = false;
891 #ifdef ENABLE_CHECKING
894 if (cgraph_dump_file)
895 fprintf (cgraph_dump_file, "\nReclaiming functions:");
896 #ifdef ENABLE_CHECKING
897 for (node = cgraph_nodes; node; node = node->next)
898 gcc_assert (!node->aux);
900 for (node = cgraph_nodes; node; node = node->next)
901 if (node->needed && !node->global.inlined_to
902 && (!DECL_EXTERNAL (node->decl) || !node->analyzed))
908 gcc_assert (!node->aux);
910 /* Perform reachability analysis. As a special case do not consider
911 extern inline functions not inlined as live because we won't output
913 while (first != (void *) 1)
915 struct cgraph_edge *e;
919 for (e = node->callees; e; e = e->next_callee)
922 && (!e->inline_failed || !e->callee->analyzed
923 || !DECL_EXTERNAL (e->callee->decl)))
925 e->callee->aux = first;
930 /* Remove unreachable nodes. Extern inline functions need special care;
931 Unreachable extern inline functions shall be removed.
932 Reachable extern inline functions we never inlined shall get their bodies
934 Reachable extern inline functions we sometimes inlined will be turned into
935 unanalyzed nodes so they look like for true extern functions to the rest
936 of code. Body of such functions is released via remove_node once the
937 inline clones are eliminated. */
938 for (node = cgraph_nodes; node; node = node->next)
943 tree decl = node->decl;
945 node->global.inlined_to = NULL;
946 if (DECL_STRUCT_FUNCTION (decl))
947 local_insns = node->local.self_insns;
950 if (cgraph_dump_file)
951 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
952 if (!node->analyzed || !DECL_EXTERNAL (node->decl))
953 cgraph_remove_node (node);
956 struct cgraph_edge *e;
958 for (e = node->callers; e; e = e->next_caller)
961 if (e || node->needed)
963 struct cgraph_node *clone;
965 for (clone = node->next_clone; clone;
966 clone = clone->next_clone)
971 DECL_SAVED_TREE (node->decl) = NULL;
972 DECL_STRUCT_FUNCTION (node->decl) = NULL;
973 DECL_INITIAL (node->decl) = error_mark_node;
975 while (node->callees)
976 cgraph_remove_edge (node->callees);
977 node->analyzed = false;
980 cgraph_remove_node (node);
982 if (!DECL_SAVED_TREE (decl))
983 insns += local_insns;
987 for (node = cgraph_nodes; node; node = node->next)
989 if (cgraph_dump_file)
990 fprintf (cgraph_dump_file, "\nReclaimed %i insns", insns);
994 /* Estimate size of the function after inlining WHAT into TO. */
997 cgraph_estimate_size_after_inlining (int times, struct cgraph_node *to,
998 struct cgraph_node *what)
1000 return (what->global.insns - INSNS_PER_CALL) * times + to->global.insns;
1003 /* Estimate the growth caused by inlining NODE into all callees. */
1006 cgraph_estimate_growth (struct cgraph_node *node)
1009 struct cgraph_edge *e;
1011 for (e = node->callers; e; e = e->next_caller)
1012 if (e->inline_failed)
1013 growth += (cgraph_estimate_size_after_inlining (1, e->caller, node)
1014 - e->caller->global.insns);
1016 /* ??? Wrong for self recursive functions or cases where we decide to not
1017 inline for different reasons, but it is not big deal as in that case
1018 we will keep the body around, but we will also avoid some inlining. */
1019 if (!node->needed && !DECL_EXTERNAL (node->decl))
1020 growth -= node->global.insns;
1025 /* E is expected to be an edge being inlined. Clone destination node of
1026 the edge and redirect it to the new clone.
1027 DUPLICATE is used for bookkeeping on whether we are actually creating new
1028 clones or re-using node originally representing out-of-line function call.
1031 cgraph_clone_inlined_nodes (struct cgraph_edge *e, bool duplicate)
1033 struct cgraph_node *n;
1035 /* We may eliminate the need for out-of-line copy to be output. In that
1036 case just go ahead and re-use it. */
1037 if (!e->callee->callers->next_caller
1038 && (!e->callee->needed || DECL_EXTERNAL (e->callee->decl))
1040 && flag_unit_at_a_time)
1042 gcc_assert (!e->callee->global.inlined_to);
1043 if (!DECL_EXTERNAL (e->callee->decl))
1044 overall_insns -= e->callee->global.insns, nfunctions_inlined++;
1049 n = cgraph_clone_node (e->callee);
1050 cgraph_redirect_edge_callee (e, n);
1053 if (e->caller->global.inlined_to)
1054 e->callee->global.inlined_to = e->caller->global.inlined_to;
1056 e->callee->global.inlined_to = e->caller;
1058 /* Recursively clone all bodies. */
1059 for (e = e->callee->callees; e; e = e->next_callee)
1060 if (!e->inline_failed)
1061 cgraph_clone_inlined_nodes (e, duplicate);
1064 /* Mark edge E as inlined and update callgraph accordingly. */
1067 cgraph_mark_inline_edge (struct cgraph_edge *e)
1069 int old_insns = 0, new_insns = 0;
1070 struct cgraph_node *to = NULL, *what;
1072 gcc_assert (e->inline_failed);
1073 e->inline_failed = NULL;
1075 if (!e->callee->global.inlined && flag_unit_at_a_time)
1076 DECL_POSSIBLY_INLINED (e->callee->decl) = true;
1077 e->callee->global.inlined = true;
1079 cgraph_clone_inlined_nodes (e, true);
1083 /* Now update size of caller and all functions caller is inlined into. */
1084 for (;e && !e->inline_failed; e = e->caller->callers)
1086 old_insns = e->caller->global.insns;
1087 new_insns = cgraph_estimate_size_after_inlining (1, e->caller,
1089 gcc_assert (new_insns >= 0);
1091 to->global.insns = new_insns;
1093 gcc_assert (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);
1127 /* Return false when inlining WHAT into TO is not good idea
1128 as it would cause too large growth of function bodies. */
1131 cgraph_check_inline_limits (struct cgraph_node *to, struct cgraph_node *what,
1132 const char **reason)
1135 struct cgraph_edge *e;
1139 if (to->global.inlined_to)
1140 to = to->global.inlined_to;
1142 for (e = to->callees; e; e = e->next_callee)
1143 if (e->callee == what)
1146 /* When inlining large function body called once into small function,
1147 take the inlined function as base for limiting the growth. */
1148 if (to->local.self_insns > what->local.self_insns)
1149 limit = to->local.self_insns;
1151 limit = what->local.self_insns;
1153 limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
1155 newsize = cgraph_estimate_size_after_inlining (times, to, what);
1156 if (newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
1160 *reason = N_("--param large-function-growth limit reached");
1166 /* Return true when function N is small enough to be inlined. */
1169 cgraph_default_inline_p (struct cgraph_node *n)
1171 if (!DECL_INLINE (n->decl) || !DECL_SAVED_TREE (n->decl))
1173 if (DECL_DECLARED_INLINE_P (n->decl))
1174 return n->global.insns < MAX_INLINE_INSNS_SINGLE;
1176 return n->global.insns < MAX_INLINE_INSNS_AUTO;
1179 /* Return true when inlining WHAT would create recursive inlining.
1180 We call recursive inlining all cases where same function appears more than
1181 once in the single recursion nest path in the inline graph. */
1184 cgraph_recursive_inlining_p (struct cgraph_node *to,
1185 struct cgraph_node *what,
1186 const char **reason)
1189 if (to->global.inlined_to)
1190 recursive = what->decl == to->global.inlined_to->decl;
1192 recursive = what->decl == to->decl;
1193 /* Marking recursive function inline has sane semantic and thus we should
1195 if (recursive && reason)
1196 *reason = (what->local.disregard_inline_limits
1197 ? N_("recursive inlining") : "");
1201 /* Recompute heap nodes for each of callees. */
1203 update_callee_keys (fibheap_t heap, struct fibnode **heap_node,
1204 struct cgraph_node *node)
1206 struct cgraph_edge *e;
1208 for (e = node->callees; e; e = e->next_callee)
1209 if (e->inline_failed && heap_node[e->callee->uid])
1210 fibheap_replace_key (heap, heap_node[e->callee->uid],
1211 cgraph_estimate_growth (e->callee));
1212 else if (!e->inline_failed)
1213 update_callee_keys (heap, heap_node, e->callee);
1216 /* Enqueue all recursive calls from NODE into queue linked via aux pointers
1217 in between FIRST and LAST. WHERE is used for bookkeeping while looking
1218 int calls inlined within NODE. */
1220 lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
1221 struct cgraph_edge **first, struct cgraph_edge **last)
1223 struct cgraph_edge *e;
1224 for (e = where->callees; e; e = e->next_callee)
1225 if (e->callee == node)
1233 for (e = where->callees; e; e = e->next_callee)
1234 if (!e->inline_failed)
1235 lookup_recursive_calls (node, e->callee, first, last);
1238 /* Decide on recursive inlining: in the case function has recursive calls,
1239 inline until body size reaches given argument. */
1241 cgraph_decide_recursive_inlining (struct cgraph_node *node)
1243 int limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO);
1244 int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO);
1245 struct cgraph_edge *first_call = NULL, *last_call = NULL;
1246 struct cgraph_edge *last_in_current_depth;
1247 struct cgraph_edge *e;
1248 struct cgraph_node *master_clone;
1252 if (DECL_DECLARED_INLINE_P (node->decl))
1254 limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE);
1255 max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH);
1258 /* Make sure that function is small enough to be considered for inlining. */
1260 || cgraph_estimate_size_after_inlining (1, node, node) >= limit)
1262 lookup_recursive_calls (node, node, &first_call, &last_call);
1266 if (cgraph_dump_file)
1267 fprintf (cgraph_dump_file,
1268 "\nPerforming recursive inlining on %s\n",
1269 cgraph_node_name (node));
1271 /* We need original clone to copy around. */
1272 master_clone = cgraph_clone_node (node);
1273 master_clone->needed = true;
1274 for (e = master_clone->callees; e; e = e->next_callee)
1275 if (!e->inline_failed)
1276 cgraph_clone_inlined_nodes (e, true);
1278 /* Do the inlining and update list of recursive call during process. */
1279 last_in_current_depth = last_call;
1281 && cgraph_estimate_size_after_inlining (1, node, master_clone) <= limit)
1283 struct cgraph_edge *curr = first_call;
1285 first_call = first_call->aux;
1288 cgraph_redirect_edge_callee (curr, master_clone);
1289 cgraph_mark_inline_edge (curr);
1290 lookup_recursive_calls (node, curr->callee, &first_call, &last_call);
1292 if (last_in_current_depth
1293 && ++depth >= max_depth)
1298 /* Cleanup queue pointers. */
1301 struct cgraph_edge *next = first_call->aux;
1302 first_call->aux = NULL;
1305 if (cgraph_dump_file)
1306 fprintf (cgraph_dump_file,
1307 "\n Inlined %i times, body grown from %i to %i insns\n", n,
1308 master_clone->global.insns, node->global.insns);
1310 /* Remove master clone we used for inlining. We rely that clones inlined
1311 into master clone gets queued just before master clone so we don't
1313 for (node = cgraph_nodes; node != master_clone;
1315 if (node->global.inlined_to == master_clone)
1316 cgraph_remove_node (node);
1317 cgraph_remove_node (master_clone);
1320 /* Set inline_failed for all callers of given function to REASON. */
1323 cgraph_set_inline_failed (struct cgraph_node *node, const char *reason)
1325 struct cgraph_edge *e;
1327 if (cgraph_dump_file)
1328 fprintf (cgraph_dump_file, "Inlining failed: %s\n", reason);
1329 for (e = node->callers; e; e = e->next_caller)
1330 if (e->inline_failed)
1331 e->inline_failed = reason;
1334 /* We use greedy algorithm for inlining of small functions:
1335 All inline candidates are put into prioritized heap based on estimated
1336 growth of the overall number of instructions and then update the estimates.
1338 INLINED and INLINED_CALEES are just pointers to arrays large enough
1339 to be passed to cgraph_inlined_into and cgraph_inlined_callees. */
1342 cgraph_decide_inlining_of_small_functions (void)
1344 struct cgraph_node *node;
1345 fibheap_t heap = fibheap_new ();
1346 struct fibnode **heap_node =
1347 xcalloc (cgraph_max_uid, sizeof (struct fibnode *));
1348 int max_insns = ((HOST_WIDEST_INT) initial_insns
1349 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
1351 /* Put all inline candidates into the heap. */
1353 for (node = cgraph_nodes; node; node = node->next)
1355 if (!node->local.inlinable || !node->callers
1356 || node->local.disregard_inline_limits)
1359 if (!cgraph_default_inline_p (node))
1361 cgraph_set_inline_failed (node,
1362 N_("--param max-inline-insns-single limit reached"));
1365 heap_node[node->uid] =
1366 fibheap_insert (heap, cgraph_estimate_growth (node), node);
1369 if (cgraph_dump_file)
1370 fprintf (cgraph_dump_file, "\nDeciding on smaller functions:\n");
1371 while (overall_insns <= max_insns && (node = fibheap_extract_min (heap)))
1373 struct cgraph_edge *e, *next;
1374 int old_insns = overall_insns;
1376 heap_node[node->uid] = NULL;
1377 if (cgraph_dump_file)
1378 fprintf (cgraph_dump_file,
1379 "\nConsidering %s with %i insns\n"
1380 " Estimated growth is %+i insns.\n",
1381 cgraph_node_name (node), node->global.insns,
1382 cgraph_estimate_growth (node));
1383 if (!cgraph_default_inline_p (node))
1385 cgraph_set_inline_failed (node,
1386 N_("--param max-inline-insns-single limit reached after inlining into the callee"));
1389 for (e = node->callers; e; e = next)
1391 next = e->next_caller;
1392 if (e->inline_failed)
1394 struct cgraph_node *where;
1396 if (cgraph_recursive_inlining_p (e->caller, e->callee,
1398 || !cgraph_check_inline_limits (e->caller, e->callee,
1401 if (cgraph_dump_file)
1402 fprintf (cgraph_dump_file, " Not inlining into %s:%s.\n",
1403 cgraph_node_name (e->caller), e->inline_failed);
1406 next = cgraph_mark_inline (e);
1408 if (where->global.inlined_to)
1409 where = where->global.inlined_to;
1411 if (heap_node[where->uid])
1412 fibheap_replace_key (heap, heap_node[where->uid],
1413 cgraph_estimate_growth (where));
1415 if (cgraph_dump_file)
1416 fprintf (cgraph_dump_file,
1417 " Inlined into %s which now has %i insns.\n",
1418 cgraph_node_name (e->caller),
1419 e->caller->global.insns);
1423 cgraph_decide_recursive_inlining (node);
1425 /* Similarly all functions called by the function we just inlined
1426 are now called more times; update keys. */
1427 update_callee_keys (heap, heap_node, node);
1429 if (cgraph_dump_file)
1430 fprintf (cgraph_dump_file,
1431 " Inlined for a net change of %+i insns.\n",
1432 overall_insns - old_insns);
1434 while ((node = fibheap_extract_min (heap)) != NULL)
1435 if (!node->local.disregard_inline_limits)
1436 cgraph_set_inline_failed (node, N_("--param inline-unit-growth limit reached"));
1437 fibheap_delete (heap);
1441 /* Decide on the inlining. We do so in the topological order to avoid
1442 expenses on updating data structures. */
1445 cgraph_decide_inlining (void)
1447 struct cgraph_node *node;
1449 struct cgraph_node **order =
1450 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
1454 for (node = cgraph_nodes; node; node = node->next)
1455 initial_insns += node->local.self_insns;
1456 overall_insns = initial_insns;
1458 nnodes = cgraph_postorder (order);
1460 if (cgraph_dump_file)
1461 fprintf (cgraph_dump_file,
1462 "\nDeciding on inlining. Starting with %i insns.\n",
1465 for (node = cgraph_nodes; node; node = node->next)
1468 if (cgraph_dump_file)
1469 fprintf (cgraph_dump_file, "\nInlining always_inline functions:\n");
1471 /* In the first pass mark all always_inline edges. Do this with a priority
1472 so none of our later choices will make this impossible. */
1473 for (i = nnodes - 1; i >= 0; i--)
1475 struct cgraph_edge *e, *next;
1479 if (!node->local.disregard_inline_limits)
1481 if (cgraph_dump_file)
1482 fprintf (cgraph_dump_file,
1483 "\nConsidering %s %i insns (always inline)\n",
1484 cgraph_node_name (node), node->global.insns);
1485 old_insns = overall_insns;
1486 for (e = node->callers; e; e = next)
1488 next = e->next_caller;
1489 if (!e->inline_failed)
1491 if (cgraph_recursive_inlining_p (e->caller, e->callee,
1494 cgraph_mark_inline_edge (e);
1495 if (cgraph_dump_file)
1496 fprintf (cgraph_dump_file,
1497 " Inlined into %s which now has %i insns.\n",
1498 cgraph_node_name (e->caller),
1499 e->caller->global.insns);
1501 if (cgraph_dump_file)
1502 fprintf (cgraph_dump_file,
1503 " Inlined for a net change of %+i insns.\n",
1504 overall_insns - old_insns);
1507 if (!flag_really_no_inline)
1509 cgraph_decide_inlining_of_small_functions ();
1511 if (cgraph_dump_file)
1512 fprintf (cgraph_dump_file, "\nDeciding on functions called once:\n");
1514 /* And finally decide what functions are called once. */
1516 for (i = nnodes - 1; i >= 0; i--)
1520 if (node->callers && !node->callers->next_caller && !node->needed
1521 && node->local.inlinable && node->callers->inline_failed
1522 && !DECL_EXTERNAL (node->decl) && !DECL_COMDAT (node->decl))
1525 struct cgraph_node *node1;
1527 /* Verify that we won't duplicate the caller. */
1528 for (node1 = node->callers->caller;
1529 node1->callers && !node1->callers->inline_failed
1530 && ok; node1 = node1->callers->caller)
1531 if (node1->callers->next_caller || node1->needed)
1535 if (cgraph_dump_file)
1536 fprintf (cgraph_dump_file,
1537 "\nConsidering %s %i insns.\n"
1538 " Called once from %s %i insns.\n",
1539 cgraph_node_name (node), node->global.insns,
1540 cgraph_node_name (node->callers->caller),
1541 node->callers->caller->global.insns);
1543 old_insns = overall_insns;
1545 if (cgraph_check_inline_limits (node->callers->caller, node,
1548 cgraph_mark_inline (node->callers);
1549 if (cgraph_dump_file)
1550 fprintf (cgraph_dump_file,
1551 " Inlined into %s which now has %i insns"
1552 " for a net change of %+i insns.\n",
1553 cgraph_node_name (node->callers->caller),
1554 node->callers->caller->global.insns,
1555 overall_insns - old_insns);
1559 if (cgraph_dump_file)
1560 fprintf (cgraph_dump_file,
1561 " Inline limit reached, not inlined.\n");
1568 /* We will never output extern functions we didn't inline.
1569 ??? Perhaps we can prevent accounting of growth of external
1570 inline functions. */
1571 cgraph_remove_unreachable_nodes ();
1573 if (cgraph_dump_file)
1574 fprintf (cgraph_dump_file,
1575 "\nInlined %i calls, eliminated %i functions, "
1576 "%i insns turned to %i insns.\n\n",
1577 ncalls_inlined, nfunctions_inlined, initial_insns,
1582 /* Decide on the inlining. We do so in the topological order to avoid
1583 expenses on updating data structures. */
1586 cgraph_decide_inlining_incrementally (struct cgraph_node *node)
1588 struct cgraph_edge *e;
1590 /* First of all look for always inline functions. */
1591 for (e = node->callees; e; e = e->next_callee)
1592 if (e->callee->local.disregard_inline_limits
1594 && !cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed)
1595 /* ??? It is possible that renaming variable removed the function body
1596 in duplicate_decls. See gcc.c-torture/compile/20011119-2.c */
1597 && DECL_SAVED_TREE (e->callee->decl))
1598 cgraph_mark_inline (e);
1600 /* Now do the automatic inlining. */
1601 if (!flag_really_no_inline)
1602 for (e = node->callees; e; e = e->next_callee)
1603 if (e->callee->local.inlinable
1605 && !e->callee->local.disregard_inline_limits
1606 && !cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed)
1607 && cgraph_check_inline_limits (node, e->callee, &e->inline_failed)
1608 && DECL_SAVED_TREE (e->callee->decl))
1610 if (cgraph_default_inline_p (e->callee))
1611 cgraph_mark_inline (e);
1614 = N_("--param max-inline-insns-single limit reached");
1619 /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */
1622 cgraph_inline_p (struct cgraph_edge *e, const char **reason)
1624 *reason = e->inline_failed;
1625 return !e->inline_failed;
1628 /* Expand all functions that must be output.
1630 Attempt to topologically sort the nodes so function is output when
1631 all called functions are already assembled to allow data to be
1632 propagated across the callgraph. Use a stack to get smaller distance
1633 between a function and its callees (later we may choose to use a more
1634 sophisticated algorithm for function reordering; we will likely want
1635 to use subsections to make the output functions appear in top-down
1639 cgraph_expand_all_functions (void)
1641 struct cgraph_node *node;
1642 struct cgraph_node **order =
1643 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
1644 int order_pos = 0, new_order_pos = 0;
1647 cgraph_mark_functions_to_output ();
1649 order_pos = cgraph_postorder (order);
1650 gcc_assert (order_pos == cgraph_n_nodes);
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--)
1663 gcc_assert (node->reachable);
1665 cgraph_expand_function (node);
1671 /* Mark all local functions.
1673 A local function is one whose calls can occur only in the
1674 current compilation unit and all its calls are explicit,
1675 so we can change its calling convention.
1676 We simply mark all static functions whose address is not taken
1680 cgraph_mark_local_functions (void)
1682 struct cgraph_node *node;
1684 if (cgraph_dump_file)
1685 fprintf (cgraph_dump_file, "\nMarking local functions:");
1687 /* Figure out functions we want to assemble. */
1688 for (node = cgraph_nodes; node; node = node->next)
1690 node->local.local = (!node->needed
1691 && DECL_SAVED_TREE (node->decl)
1692 && !TREE_PUBLIC (node->decl));
1693 if (cgraph_dump_file && node->local.local)
1694 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
1696 if (cgraph_dump_file)
1697 fprintf (cgraph_dump_file, "\n\n");
1700 /* Return true when function body of DECL still needs to be kept around
1701 for later re-use. */
1703 cgraph_preserve_function_body_p (tree decl)
1705 struct cgraph_node *node;
1706 /* Keep the body; we're going to dump it. */
1707 if (dump_enabled_p (TDI_tree_all))
1709 if (!cgraph_global_info_ready)
1710 return (DECL_INLINE (decl) && !flag_really_no_inline);
1711 /* Look if there is any clone around. */
1712 for (node = cgraph_node (decl); node; node = node->next_clone)
1713 if (node->global.inlined_to)
1718 /* Perform simple optimizations based on callgraph. */
1721 cgraph_optimize (void)
1723 #ifdef ENABLE_CHECKING
1726 if (!flag_unit_at_a_time)
1728 timevar_push (TV_CGRAPHOPT);
1730 fprintf (stderr, "Performing intraprocedural optimizations\n");
1732 cgraph_mark_local_functions ();
1733 if (cgraph_dump_file)
1735 fprintf (cgraph_dump_file, "Marked ");
1736 dump_cgraph (cgraph_dump_file);
1739 if (flag_inline_trees)
1740 cgraph_decide_inlining ();
1741 cgraph_global_info_ready = true;
1742 if (cgraph_dump_file)
1744 fprintf (cgraph_dump_file, "Optimized ");
1745 dump_cgraph (cgraph_dump_file);
1747 timevar_pop (TV_CGRAPHOPT);
1749 /* Output everything. */
1751 fprintf (stderr, "Assembling functions:\n");
1752 #ifdef ENABLE_CHECKING
1755 cgraph_expand_all_functions ();
1756 if (cgraph_dump_file)
1758 fprintf (cgraph_dump_file, "\nFinal ");
1759 dump_cgraph (cgraph_dump_file);
1761 #ifdef ENABLE_CHECKING
1763 /* Double check that all inline clones are gone and that all
1764 function bodies have been released from memory. */
1765 if (flag_unit_at_a_time
1766 && !dump_enabled_p (TDI_tree_all)
1767 && !(sorrycount || errorcount))
1769 struct cgraph_node *node;
1770 bool error_found = false;
1772 for (node = cgraph_nodes; node; node = node->next)
1774 && (node->global.inlined_to
1775 || DECL_SAVED_TREE (node->decl)))
1778 dump_cgraph_node (stderr, node);
1781 internal_error ("Nodes with no released memory found.");
1786 /* Generate and emit a static constructor or destructor. WHICH must be
1787 one of 'I' or 'D'. BODY should be a STATEMENT_LIST containing
1788 GENERIC statements. */
1791 cgraph_build_static_cdtor (char which, tree body, int priority)
1793 static int counter = 0;
1795 tree decl, name, resdecl;
1797 sprintf (which_buf, "%c_%d", which, counter++);
1798 name = get_file_function_name_long (which_buf);
1800 decl = build_decl (FUNCTION_DECL, name,
1801 build_function_type (void_type_node, void_list_node));
1802 current_function_decl = decl;
1804 resdecl = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
1805 DECL_ARTIFICIAL (resdecl) = 1;
1806 DECL_IGNORED_P (resdecl) = 1;
1807 DECL_RESULT (decl) = resdecl;
1809 allocate_struct_function (decl);
1811 TREE_STATIC (decl) = 1;
1812 TREE_USED (decl) = 1;
1813 DECL_ARTIFICIAL (decl) = 1;
1814 DECL_IGNORED_P (decl) = 1;
1815 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1816 DECL_SAVED_TREE (decl) = body;
1817 TREE_PUBLIC (decl) = ! targetm.have_ctors_dtors;
1818 DECL_UNINLINABLE (decl) = 1;
1820 DECL_INITIAL (decl) = make_node (BLOCK);
1821 TREE_USED (DECL_INITIAL (decl)) = 1;
1823 DECL_SOURCE_LOCATION (decl) = input_location;
1824 cfun->function_end_locus = input_location;
1829 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1832 DECL_STATIC_DESTRUCTOR (decl) = 1;
1838 gimplify_function_tree (decl);
1840 /* ??? We will get called LATE in the compilation process. */
1841 if (cgraph_global_info_ready)
1842 tree_rest_of_compilation (decl, false);
1844 cgraph_finalize_function (decl, 0);
1846 if (targetm.have_ctors_dtors)
1848 void (*fn) (rtx, int);
1851 fn = targetm.asm_out.constructor;
1853 fn = targetm.asm_out.destructor;
1854 fn (XEXP (DECL_RTL (decl), 0), priority);