* varasm.c (assemble_start_function): Remove reset of in_section.
[official-gcc.git] / gcc / cgraphunit.c
blob9b16e92997807570b3311cd98c95907d8a303546
1 /* Callgraph based intraprocedural optimizations.
2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* This module implements main driver of compilation process as well as
23 few basic intraprocedural optimizers.
25 The main scope of this file is to act as an interface in between
26 tree based frontends and the backend (and middle end)
28 The front-end is supposed to use following functionality:
30 - cgraph_finalize_function
32 This function is called once front-end has parsed whole body of function
33 and it is certain that the function body nor the declaration will change.
35 (There is one exception needed for implementing GCC extern inline function.)
37 - cgraph_varpool_finalize_variable
39 This function has same behavior as the above but is used for static
40 variables.
42 - cgraph_finalize_compilation_unit
44 This function is called once compilation unit is finalized and it will
45 no longer change.
47 In the unit-at-a-time the call-graph construction and local function
48 analysis takes place here. Bodies of unreachable functions are released
49 to conserve memory usage.
51 ??? The compilation unit in this point of view should be compilation
52 unit as defined by the language - for instance C frontend allows multiple
53 compilation units to be parsed at once and it should call function each
54 time parsing is done so we save memory.
56 - cgraph_optimize
58 In this unit-at-a-time compilation the intra procedural analysis takes
59 place here. In particular the static functions whose address is never
60 taken are marked as local. Backend can then use this information to
61 modify calling conventions, do better inlining or similar optimizations.
63 - cgraph_assemble_pending_functions
64 - cgraph_varpool_assemble_pending_variables
66 In non-unit-at-a-time mode these functions can be used to force compilation
67 of functions or variables that are known to be needed at given stage
68 of compilation
70 - cgraph_mark_needed_node
71 - cgraph_varpool_mark_needed_node
73 When function or variable is referenced by some hidden way (for instance
74 via assembly code and marked by attribute "used"), the call-graph data structure
75 must be updated accordingly by this function.
77 - analyze_expr callback
79 This function is responsible for lowering tree nodes not understood by
80 generic code into understandable ones or alternatively marking
81 callgraph and varpool nodes referenced by the as needed.
83 ??? On the tree-ssa genericizing should take place here and we will avoid
84 need for these hooks (replacing them by genericizing hook)
86 - expand_function callback
88 This function is used to expand function and pass it into RTL back-end.
89 Front-end should not make any assumptions about when this function can be
90 called. In particular cgraph_assemble_pending_functions,
91 cgraph_varpool_assemble_pending_variables, cgraph_finalize_function,
92 cgraph_varpool_finalize_function, cgraph_optimize can cause arbitrarily
93 previously finalized functions to be expanded.
95 We implement two compilation modes.
97 - unit-at-a-time: In this mode analyzing of all functions is deferred
98 to cgraph_finalize_compilation_unit and expansion into cgraph_optimize.
100 In cgraph_finalize_compilation_unit the reachable functions are
101 analyzed. During analysis the call-graph edges from reachable
102 functions are constructed and their destinations are marked as
103 reachable. References to functions and variables are discovered too
104 and variables found to be needed output to the assembly file. Via
105 mark_referenced call in assemble_variable functions referenced by
106 static variables are noticed too.
108 The intra-procedural information is produced and its existence
109 indicated by global_info_ready. Once this flag is set it is impossible
110 to change function from !reachable to reachable and thus
111 assemble_variable no longer call mark_referenced.
113 Finally the call-graph is topologically sorted and all reachable functions
114 that has not been completely inlined or are not external are output.
116 ??? It is possible that reference to function or variable is optimized
117 out. We can not deal with this nicely because topological order is not
118 suitable for it. For tree-ssa we may consider another pass doing
119 optimization and re-discovering reachable functions.
121 ??? Reorganize code so variables are output very last and only if they
122 really has been referenced by produced code, so we catch more cases
123 where reference has been optimized out.
125 - non-unit-at-a-time
127 All functions are variables are output as early as possible to conserve
128 memory consumption. This may or may not result in less memory used but
129 it is still needed for some legacy code that rely on particular ordering
130 of things output from the compiler.
132 Varpool data structures are not used and variables are output directly.
134 Functions are output early using call of
135 cgraph_assemble_pending_function from cgraph_finalize_function. The
136 decision on whether function is needed is made more conservative so
137 uninlininable static functions are needed too. During the call-graph
138 construction the edge destinations are not marked as reachable and it
139 is completely relied upn assemble_variable to mark them.
141 Inlining decision heuristics
142 ??? Move this to separate file after tree-ssa merge.
144 We separate inlining decisions from the inliner itself and store it
145 inside callgraph as so called inline plan. Refer to cgraph.c
146 documentation about particular representation of inline plans in the
147 callgraph
149 The implementation of particular heuristics is separated from
150 the rest of code to make it easier to replace it with more complicated
151 implementation in the future. The rest of inlining code acts as a
152 library aimed to modify the callgraph and verify that the parameters
153 on code size growth fits.
155 To mark given call inline, use cgraph_mark_inline function, the
156 verification is performed by cgraph_default_inline_p and
157 cgraph_check_inline_limits.
159 The heuristics implements simple knapsack style algorithm ordering
160 all functions by their "profitability" (estimated by code size growth)
161 and inlining them in priority order.
163 cgraph_decide_inlining implements heuristics taking whole callgraph
164 into account, while cgraph_decide_inlining_incrementally considers
165 only one function at a time and is used in non-unit-at-a-time mode. */
168 #include "config.h"
169 #include "system.h"
170 #include "coretypes.h"
171 #include "tm.h"
172 #include "tree.h"
173 #include "rtl.h"
174 #include "tree-flow.h"
175 #include "tree-inline.h"
176 #include "langhooks.h"
177 #include "pointer-set.h"
178 #include "toplev.h"
179 #include "flags.h"
180 #include "ggc.h"
181 #include "debug.h"
182 #include "target.h"
183 #include "cgraph.h"
184 #include "diagnostic.h"
185 #include "timevar.h"
186 #include "params.h"
187 #include "fibheap.h"
188 #include "c-common.h"
189 #include "intl.h"
190 #include "function.h"
191 #include "tree-gimple.h"
192 #include "output.h"
194 static void cgraph_expand_all_functions (void);
195 static void cgraph_mark_functions_to_output (void);
196 static void cgraph_expand_function (struct cgraph_node *);
197 static tree record_call_1 (tree *, int *, void *);
198 static void cgraph_mark_local_functions (void);
199 static bool cgraph_default_inline_p (struct cgraph_node *n);
200 static void cgraph_analyze_function (struct cgraph_node *node);
201 static void cgraph_decide_inlining_incrementally (struct cgraph_node *);
203 /* Statistics we collect about inlining algorithm. */
204 static int ncalls_inlined;
205 static int nfunctions_inlined;
206 static int initial_insns;
207 static int overall_insns;
209 /* Records tree nodes seen in cgraph_create_edges. Simply using
210 walk_tree_without_duplicates doesn't guarantee each node is visited
211 once because it gets a new htab upon each recursive call from
212 record_calls_1. */
213 static struct pointer_set_t *visited_nodes;
215 static FILE *cgraph_dump_file;
217 /* Determine if function DECL is needed. That is, visible to something
218 either outside this translation unit, something magic in the system
219 configury, or (if not doing unit-at-a-time) to something we havn't
220 seen yet. */
222 static bool
223 decide_is_function_needed (struct cgraph_node *node, tree decl)
225 tree origin;
227 /* If we decided it was needed before, but at the time we didn't have
228 the body of the function available, then it's still needed. We have
229 to go back and re-check its dependencies now. */
230 if (node->needed)
231 return true;
233 /* Externally visible functions must be output. The exception is
234 COMDAT functions that must be output only when they are needed. */
235 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
236 return true;
238 /* Constructors and destructors are reachable from the runtime by
239 some mechanism. */
240 if (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl))
241 return true;
243 /* If the user told us it is used, then it must be so. */
244 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
245 return true;
247 /* ??? If the assembler name is set by hand, it is possible to assemble
248 the name later after finalizing the function and the fact is noticed
249 in assemble_name then. This is arguably a bug. */
250 if (DECL_ASSEMBLER_NAME_SET_P (decl)
251 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
252 return true;
254 if (flag_unit_at_a_time)
255 return false;
257 /* If not doing unit at a time, then we'll only defer this function
258 if its marked for inlining. Otherwise we want to emit it now. */
260 /* "extern inline" functions are never output locally. */
261 if (DECL_EXTERNAL (decl))
262 return false;
263 /* Nested functions of extern inline function shall not be emit unless
264 we inlined the origin. */
265 for (origin = decl_function_context (decl); origin;
266 origin = decl_function_context (origin))
267 if (DECL_EXTERNAL (origin))
268 return false;
269 /* We want to emit COMDAT functions only when absolutely necessary. */
270 if (DECL_COMDAT (decl))
271 return false;
272 if (!DECL_INLINE (decl)
273 || (!node->local.disregard_inline_limits
274 /* When declared inline, defer even the uninlinable functions.
275 This allows them to be eliminated when unused. */
276 && !DECL_DECLARED_INLINE_P (decl)
277 && (!node->local.inlinable || !cgraph_default_inline_p (node))))
278 return true;
280 return false;
283 /* Walk the decls we marked as necessary and see if they reference new
284 variables or functions and add them into the worklists. */
285 static bool
286 cgraph_varpool_analyze_pending_decls (void)
288 bool changed = false;
289 timevar_push (TV_CGRAPH);
291 while (cgraph_varpool_first_unanalyzed_node)
293 tree decl = cgraph_varpool_first_unanalyzed_node->decl;
295 cgraph_varpool_first_unanalyzed_node->analyzed = true;
297 cgraph_varpool_first_unanalyzed_node = cgraph_varpool_first_unanalyzed_node->next_needed;
299 if (DECL_INITIAL (decl))
300 cgraph_create_edges (NULL, DECL_INITIAL (decl));
301 changed = true;
303 timevar_pop (TV_CGRAPH);
304 return changed;
307 /* Optimization of function bodies might've rendered some variables as
308 unnecessary so we want to avoid these from being compiled.
310 This is done by prunning the queue and keeping only the variables that
311 really appear needed (ie they are either externally visible or referenced
312 by compiled function). Re-doing the reachability analysis on variables
313 brings back the remaining variables referenced by these. */
314 static void
315 cgraph_varpool_remove_unreferenced_decls (void)
317 struct cgraph_varpool_node *next, *node = cgraph_varpool_nodes_queue;
319 cgraph_varpool_reset_queue ();
321 if (errorcount || sorrycount)
322 return;
324 while (node)
326 tree decl = node->decl;
327 next = node->next_needed;
328 node->needed = 0;
330 if (node->finalized
331 && ((DECL_ASSEMBLER_NAME_SET_P (decl)
332 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
333 || node->force_output
334 || decide_is_variable_needed (node, decl)))
335 cgraph_varpool_mark_needed_node (node);
337 node = next;
339 cgraph_varpool_analyze_pending_decls ();
343 /* When not doing unit-at-a-time, output all functions enqueued.
344 Return true when such a functions were found. */
346 bool
347 cgraph_assemble_pending_functions (void)
349 bool output = false;
351 if (flag_unit_at_a_time)
352 return false;
354 while (cgraph_nodes_queue)
356 struct cgraph_node *n = cgraph_nodes_queue;
358 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
359 n->next_needed = NULL;
360 if (!n->global.inlined_to
361 && !n->alias
362 && !DECL_EXTERNAL (n->decl))
364 cgraph_expand_function (n);
365 output = true;
369 return output;
372 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
373 logic in effect. If NESTED is true, then our caller cannot stand to have
374 the garbage collector run at the moment. We would need to either create
375 a new GC context, or just not compile right now. */
377 void
378 cgraph_finalize_function (tree decl, bool nested)
380 struct cgraph_node *node = cgraph_node (decl);
382 if (node->local.finalized)
384 /* As an GCC extension we allow redefinition of the function. The
385 semantics when both copies of bodies differ is not well defined.
386 We replace the old body with new body so in unit at a time mode
387 we always use new body, while in normal mode we may end up with
388 old body inlined into some functions and new body expanded and
389 inlined in others.
391 ??? It may make more sense to use one body for inlining and other
392 body for expanding the function but this is difficult to do. */
394 /* If node->output is set, then this is a unit-at-a-time compilation
395 and we have already begun whole-unit analysis. This is *not*
396 testing for whether we've already emitted the function. That
397 case can be sort-of legitimately seen with real function
398 redefinition errors. I would argue that the front end should
399 never present us with such a case, but don't enforce that for now. */
400 gcc_assert (!node->output);
402 /* Reset our data structures so we can analyze the function again. */
403 memset (&node->local, 0, sizeof (node->local));
404 memset (&node->global, 0, sizeof (node->global));
405 memset (&node->rtl, 0, sizeof (node->rtl));
406 node->analyzed = false;
407 node->local.redefined_extern_inline = true;
409 if (!flag_unit_at_a_time)
411 struct cgraph_node *n;
413 for (n = cgraph_nodes; n; n = n->next)
414 if (n->global.inlined_to == node)
415 cgraph_remove_node (n);
418 cgraph_node_remove_callees (node);
420 /* We may need to re-queue the node for assembling in case
421 we already proceeded it and ignored as not needed. */
422 if (node->reachable && !flag_unit_at_a_time)
424 struct cgraph_node *n;
426 for (n = cgraph_nodes_queue; n; n = n->next_needed)
427 if (n == node)
428 break;
429 if (!n)
430 node->reachable = 0;
434 notice_global_symbol (decl);
435 node->decl = decl;
436 node->local.finalized = true;
437 if (node->nested)
438 lower_nested_functions (decl);
439 gcc_assert (!node->nested);
441 /* If not unit at a time, then we need to create the call graph
442 now, so that called functions can be queued and emitted now. */
443 if (!flag_unit_at_a_time)
445 cgraph_analyze_function (node);
446 cgraph_decide_inlining_incrementally (node);
449 if (decide_is_function_needed (node, decl))
450 cgraph_mark_needed_node (node);
452 /* If not unit at a time, go ahead and emit everything we've found
453 to be reachable at this time. */
454 if (!nested)
456 if (!cgraph_assemble_pending_functions ())
457 ggc_collect ();
460 /* If we've not yet emitted decl, tell the debug info about it. */
461 if (!TREE_ASM_WRITTEN (decl))
462 (*debug_hooks->deferred_inline_function) (decl);
464 /* Possibly warn about unused parameters. */
465 if (warn_unused_parameter)
466 do_warn_unused_parameter (decl);
469 /* Walk tree and record all calls. Called via walk_tree. */
470 static tree
471 record_call_1 (tree *tp, int *walk_subtrees, void *data)
473 tree t = *tp;
475 switch (TREE_CODE (t))
477 case VAR_DECL:
478 /* ??? Really, we should mark this decl as *potentially* referenced
479 by this function and re-examine whether the decl is actually used
480 after rtl has been generated. */
481 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
483 cgraph_varpool_mark_needed_node (cgraph_varpool_node (t));
484 if (lang_hooks.callgraph.analyze_expr)
485 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees,
486 data);
488 break;
490 case FDESC_EXPR:
491 case ADDR_EXPR:
492 if (flag_unit_at_a_time)
494 /* Record dereferences to the functions. This makes the
495 functions reachable unconditionally. */
496 tree decl = TREE_OPERAND (*tp, 0);
497 if (TREE_CODE (decl) == FUNCTION_DECL)
498 cgraph_mark_needed_node (cgraph_node (decl));
500 break;
502 case CALL_EXPR:
504 tree decl = get_callee_fndecl (*tp);
505 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
507 cgraph_create_edge (data, cgraph_node (decl), *tp);
509 /* When we see a function call, we don't want to look at the
510 function reference in the ADDR_EXPR that is hanging from
511 the CALL_EXPR we're examining here, because we would
512 conclude incorrectly that the function's address could be
513 taken by something that is not a function call. So only
514 walk the function parameter list, skip the other subtrees. */
516 walk_tree (&TREE_OPERAND (*tp, 1), record_call_1, data,
517 visited_nodes);
518 *walk_subtrees = 0;
520 break;
523 default:
524 /* Save some cycles by not walking types and declaration as we
525 won't find anything useful there anyway. */
526 if (IS_TYPE_OR_DECL_P (*tp))
528 *walk_subtrees = 0;
529 break;
532 if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
533 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees, data);
534 break;
537 return NULL;
540 /* Create cgraph edges for function calls inside BODY from NODE. */
542 void
543 cgraph_create_edges (struct cgraph_node *node, tree body)
545 /* The nodes we're interested in are never shared, so walk
546 the tree ignoring duplicates. */
547 visited_nodes = pointer_set_create ();
548 walk_tree (&body, record_call_1, node, visited_nodes);
549 pointer_set_destroy (visited_nodes);
550 visited_nodes = NULL;
553 static bool error_found;
555 /* Callback of verify_cgraph_node. Check that all call_exprs have
556 cgraph nodes. */
558 static tree
559 verify_cgraph_node_1 (tree *tp, int *walk_subtrees, void *data)
561 tree t = *tp;
562 tree decl;
564 if (TREE_CODE (t) == CALL_EXPR && (decl = get_callee_fndecl (t)))
566 struct cgraph_edge *e = cgraph_edge (data, t);
567 if (e)
569 if (e->aux)
571 error ("Shared call_expr:");
572 debug_tree (t);
573 error_found = true;
575 if (e->callee->decl != cgraph_node (decl)->decl)
577 error ("Edge points to wrong declaration:");
578 debug_tree (e->callee->decl);
579 fprintf (stderr," Instead of:");
580 debug_tree (decl);
582 e->aux = (void *)1;
584 else
586 error ("Missing callgraph edge for call expr:");
587 debug_tree (t);
588 error_found = true;
592 /* Save some cycles by not walking types and declaration as we
593 won't find anything useful there anyway. */
594 if (IS_TYPE_OR_DECL_P (*tp))
595 *walk_subtrees = 0;
597 return NULL_TREE;
600 /* Verify cgraph nodes of given cgraph node. */
601 void
602 verify_cgraph_node (struct cgraph_node *node)
604 struct cgraph_edge *e;
605 struct cgraph_node *main_clone;
607 timevar_push (TV_CGRAPH_VERIFY);
608 error_found = false;
609 for (e = node->callees; e; e = e->next_callee)
610 if (e->aux)
612 error ("Aux field set for edge %s->%s",
613 cgraph_node_name (e->caller), cgraph_node_name (e->callee));
614 error_found = true;
616 for (e = node->callers; e; e = e->next_caller)
618 if (!e->inline_failed)
620 if (node->global.inlined_to
621 != (e->caller->global.inlined_to
622 ? e->caller->global.inlined_to : e->caller))
624 error ("Inlined_to pointer is wrong");
625 error_found = true;
627 if (node->callers->next_caller)
629 error ("Multiple inline callers");
630 error_found = true;
633 else
634 if (node->global.inlined_to)
636 error ("Inlined_to pointer set for noninline callers");
637 error_found = true;
640 if (!node->callers && node->global.inlined_to)
642 error ("Inlined_to pointer is set but no predecesors found");
643 error_found = true;
645 if (node->global.inlined_to == node)
647 error ("Inlined_to pointer reffers to itself");
648 error_found = true;
651 for (main_clone = cgraph_node (node->decl); main_clone;
652 main_clone = main_clone->next_clone)
653 if (main_clone == node)
654 break;
655 if (!node)
657 error ("Node not found in DECL_ASSEMBLER_NAME hash");
658 error_found = true;
661 if (node->analyzed
662 && DECL_SAVED_TREE (node->decl) && !TREE_ASM_WRITTEN (node->decl)
663 && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to))
665 walk_tree_without_duplicates (&DECL_SAVED_TREE (node->decl),
666 verify_cgraph_node_1, node);
667 for (e = node->callees; e; e = e->next_callee)
669 if (!e->aux)
671 error ("Edge %s->%s has no corresponding call_expr",
672 cgraph_node_name (e->caller),
673 cgraph_node_name (e->callee));
674 error_found = true;
676 e->aux = 0;
679 if (error_found)
681 dump_cgraph_node (stderr, node);
682 internal_error ("verify_cgraph_node failed.");
684 timevar_pop (TV_CGRAPH_VERIFY);
687 /* Verify whole cgraph structure. */
688 void
689 verify_cgraph (void)
691 struct cgraph_node *node;
693 if (sorrycount || errorcount)
694 return;
696 for (node = cgraph_nodes; node; node = node->next)
697 verify_cgraph_node (node);
701 /* Output all variables enqueued to be assembled. */
702 bool
703 cgraph_varpool_assemble_pending_decls (void)
705 bool changed = false;
707 if (errorcount || sorrycount)
708 return false;
710 /* EH might mark decls as needed during expansion. This should be safe since
711 we don't create references to new function, but it should not be used
712 elsewhere. */
713 cgraph_varpool_analyze_pending_decls ();
715 while (cgraph_varpool_nodes_queue)
717 tree decl = cgraph_varpool_nodes_queue->decl;
718 struct cgraph_varpool_node *node = cgraph_varpool_nodes_queue;
720 cgraph_varpool_nodes_queue = cgraph_varpool_nodes_queue->next_needed;
721 if (!TREE_ASM_WRITTEN (decl) && !node->alias && !DECL_EXTERNAL (decl))
723 assemble_variable (decl, 0, 1, 0);
724 changed = true;
726 node->next_needed = NULL;
728 return changed;
731 /* Analyze the function scheduled to be output. */
732 static void
733 cgraph_analyze_function (struct cgraph_node *node)
735 tree decl = node->decl;
736 struct cgraph_edge *e;
738 current_function_decl = decl;
740 /* First kill forward declaration so reverse inlining works properly. */
741 cgraph_create_edges (node, DECL_SAVED_TREE (decl));
743 node->local.inlinable = tree_inlinable_function_p (decl);
744 node->local.self_insns = estimate_num_insns (DECL_SAVED_TREE (decl));
745 if (node->local.inlinable)
746 node->local.disregard_inline_limits
747 = lang_hooks.tree_inlining.disregard_inline_limits (decl);
748 for (e = node->callers; e; e = e->next_caller)
750 if (node->local.redefined_extern_inline)
751 e->inline_failed = N_("redefined extern inline functions are not "
752 "considered for inlining");
753 else if (!node->local.inlinable)
754 e->inline_failed = N_("function not inlinable");
755 else
756 e->inline_failed = N_("function not considered for inlining");
758 if (flag_really_no_inline && !node->local.disregard_inline_limits)
759 node->local.inlinable = 0;
760 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
761 node->global.insns = node->local.self_insns;
763 node->analyzed = true;
764 current_function_decl = NULL;
767 /* Analyze the whole compilation unit once it is parsed completely. */
769 void
770 cgraph_finalize_compilation_unit (void)
772 struct cgraph_node *node;
773 /* Keep track of already processed nodes when called multiple times for
774 intermodule optimization. */
775 static struct cgraph_node *first_analyzed;
777 finish_aliases_1 ();
779 if (!flag_unit_at_a_time)
781 cgraph_assemble_pending_functions ();
782 return;
785 if (!quiet_flag)
787 fprintf (stderr, "\nAnalyzing compilation unit");
788 fflush (stderr);
791 timevar_push (TV_CGRAPH);
792 cgraph_varpool_analyze_pending_decls ();
793 if (cgraph_dump_file)
795 fprintf (cgraph_dump_file, "Initial entry points:");
796 for (node = cgraph_nodes; node != first_analyzed; node = node->next)
797 if (node->needed && DECL_SAVED_TREE (node->decl))
798 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
799 fprintf (cgraph_dump_file, "\n");
802 /* Propagate reachability flag and lower representation of all reachable
803 functions. In the future, lowering will introduce new functions and
804 new entry points on the way (by template instantiation and virtual
805 method table generation for instance). */
806 while (cgraph_nodes_queue)
808 struct cgraph_edge *edge;
809 tree decl = cgraph_nodes_queue->decl;
811 node = cgraph_nodes_queue;
812 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
813 node->next_needed = NULL;
815 /* ??? It is possible to create extern inline function and later using
816 weak alias attribute to kill its body. See
817 gcc.c-torture/compile/20011119-1.c */
818 if (!DECL_SAVED_TREE (decl))
819 continue;
821 gcc_assert (!node->analyzed && node->reachable);
822 gcc_assert (DECL_SAVED_TREE (decl));
824 cgraph_analyze_function (node);
826 for (edge = node->callees; edge; edge = edge->next_callee)
827 if (!edge->callee->reachable)
828 cgraph_mark_reachable_node (edge->callee);
830 cgraph_varpool_analyze_pending_decls ();
833 /* Collect entry points to the unit. */
835 if (cgraph_dump_file)
837 fprintf (cgraph_dump_file, "Unit entry points:");
838 for (node = cgraph_nodes; node != first_analyzed; node = node->next)
839 if (node->needed && DECL_SAVED_TREE (node->decl))
840 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
841 fprintf (cgraph_dump_file, "\n\nInitial ");
842 dump_cgraph (cgraph_dump_file);
845 if (cgraph_dump_file)
846 fprintf (cgraph_dump_file, "\nReclaiming functions:");
848 for (node = cgraph_nodes; node != first_analyzed; node = node->next)
850 tree decl = node->decl;
852 if (!node->reachable && DECL_SAVED_TREE (decl))
854 if (cgraph_dump_file)
855 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
856 cgraph_remove_node (node);
858 else
859 node->next_needed = NULL;
861 if (cgraph_dump_file)
863 fprintf (cgraph_dump_file, "\n\nReclaimed ");
864 dump_cgraph (cgraph_dump_file);
866 first_analyzed = cgraph_nodes;
867 ggc_collect ();
868 timevar_pop (TV_CGRAPH);
870 /* Figure out what functions we want to assemble. */
872 static void
873 cgraph_mark_functions_to_output (void)
875 struct cgraph_node *node;
877 for (node = cgraph_nodes; node; node = node->next)
879 tree decl = node->decl;
880 struct cgraph_edge *e;
882 gcc_assert (!node->output);
884 for (e = node->callers; e; e = e->next_caller)
885 if (e->inline_failed)
886 break;
888 /* We need to output all local functions that are used and not
889 always inlined, as well as those that are reachable from
890 outside the current compilation unit. */
891 if (DECL_SAVED_TREE (decl)
892 && !node->global.inlined_to
893 && (node->needed
894 || (e && node->reachable))
895 && !TREE_ASM_WRITTEN (decl)
896 && !DECL_EXTERNAL (decl))
897 node->output = 1;
898 else
900 /* We should've reclaimed all functions that are not needed. */
901 #ifdef ENABLE_CHECKING
902 if (!node->global.inlined_to && DECL_SAVED_TREE (decl)
903 && !DECL_EXTERNAL (decl))
905 dump_cgraph_node (stderr, node);
906 internal_error ("failed to reclaim unneeded function");
908 #endif
909 gcc_assert (node->global.inlined_to || !DECL_SAVED_TREE (decl)
910 || DECL_EXTERNAL (decl));
917 /* Expand function specified by NODE. */
919 static void
920 cgraph_expand_function (struct cgraph_node *node)
922 tree decl = node->decl;
924 /* We ought to not compile any inline clones. */
925 gcc_assert (!node->global.inlined_to);
927 if (flag_unit_at_a_time)
928 announce_function (decl);
930 /* Generate RTL for the body of DECL. */
931 lang_hooks.callgraph.expand_function (decl);
933 /* Make sure that BE didn't give up on compiling. */
934 /* ??? Can happen with nested function of extern inline. */
935 gcc_assert (TREE_ASM_WRITTEN (node->decl));
937 current_function_decl = NULL;
938 if (!cgraph_preserve_function_body_p (node->decl))
940 DECL_SAVED_TREE (node->decl) = NULL;
941 DECL_STRUCT_FUNCTION (node->decl) = NULL;
942 DECL_INITIAL (node->decl) = error_mark_node;
943 /* Eliminate all call edges. This is important so the call_expr no longer
944 points to the dead function body. */
945 cgraph_node_remove_callees (node);
949 /* Fill array order with all nodes with output flag set in the reverse
950 topological order. */
952 static int
953 cgraph_postorder (struct cgraph_node **order)
955 struct cgraph_node *node, *node2;
956 int stack_size = 0;
957 int order_pos = 0;
958 struct cgraph_edge *edge, last;
960 struct cgraph_node **stack =
961 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
963 /* We have to deal with cycles nicely, so use a depth first traversal
964 output algorithm. Ignore the fact that some functions won't need
965 to be output and put them into order as well, so we get dependencies
966 right through intline functions. */
967 for (node = cgraph_nodes; node; node = node->next)
968 node->aux = NULL;
969 for (node = cgraph_nodes; node; node = node->next)
970 if (!node->aux)
972 node2 = node;
973 if (!node->callers)
974 node->aux = &last;
975 else
976 node->aux = node->callers;
977 while (node2)
979 while (node2->aux != &last)
981 edge = node2->aux;
982 if (edge->next_caller)
983 node2->aux = edge->next_caller;
984 else
985 node2->aux = &last;
986 if (!edge->caller->aux)
988 if (!edge->caller->callers)
989 edge->caller->aux = &last;
990 else
991 edge->caller->aux = edge->caller->callers;
992 stack[stack_size++] = node2;
993 node2 = edge->caller;
994 break;
997 if (node2->aux == &last)
999 order[order_pos++] = node2;
1000 if (stack_size)
1001 node2 = stack[--stack_size];
1002 else
1003 node2 = NULL;
1007 free (stack);
1008 return order_pos;
1012 /* Perform reachability analysis and reclaim all unreachable nodes.
1013 This function also remove unneeded bodies of extern inline functions
1014 and thus needs to be done only after inlining decisions has been made. */
1015 static bool
1016 cgraph_remove_unreachable_nodes (void)
1018 struct cgraph_node *first = (void *) 1;
1019 struct cgraph_node *node;
1020 bool changed = false;
1021 int insns = 0;
1023 #ifdef ENABLE_CHECKING
1024 verify_cgraph ();
1025 #endif
1026 if (cgraph_dump_file)
1027 fprintf (cgraph_dump_file, "\nReclaiming functions:");
1028 #ifdef ENABLE_CHECKING
1029 for (node = cgraph_nodes; node; node = node->next)
1030 gcc_assert (!node->aux);
1031 #endif
1032 for (node = cgraph_nodes; node; node = node->next)
1033 if (node->needed && !node->global.inlined_to
1034 && (!DECL_EXTERNAL (node->decl) || !node->analyzed))
1036 node->aux = first;
1037 first = node;
1039 else
1040 gcc_assert (!node->aux);
1042 /* Perform reachability analysis. As a special case do not consider
1043 extern inline functions not inlined as live because we won't output
1044 them at all. */
1045 while (first != (void *) 1)
1047 struct cgraph_edge *e;
1048 node = first;
1049 first = first->aux;
1051 for (e = node->callees; e; e = e->next_callee)
1052 if (!e->callee->aux
1053 && node->analyzed
1054 && (!e->inline_failed || !e->callee->analyzed
1055 || !DECL_EXTERNAL (e->callee->decl)))
1057 e->callee->aux = first;
1058 first = e->callee;
1062 /* Remove unreachable nodes. Extern inline functions need special care;
1063 Unreachable extern inline functions shall be removed.
1064 Reachable extern inline functions we never inlined shall get their bodies
1065 eliminated.
1066 Reachable extern inline functions we sometimes inlined will be turned into
1067 unanalyzed nodes so they look like for true extern functions to the rest
1068 of code. Body of such functions is released via remove_node once the
1069 inline clones are eliminated. */
1070 for (node = cgraph_nodes; node; node = node->next)
1072 if (!node->aux)
1074 int local_insns;
1075 tree decl = node->decl;
1077 node->global.inlined_to = NULL;
1078 if (DECL_STRUCT_FUNCTION (decl))
1079 local_insns = node->local.self_insns;
1080 else
1081 local_insns = 0;
1082 if (cgraph_dump_file)
1083 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
1084 if (!node->analyzed || !DECL_EXTERNAL (node->decl))
1085 cgraph_remove_node (node);
1086 else
1088 struct cgraph_edge *e;
1090 for (e = node->callers; e; e = e->next_caller)
1091 if (e->caller->aux)
1092 break;
1093 if (e || node->needed)
1095 struct cgraph_node *clone;
1097 for (clone = node->next_clone; clone;
1098 clone = clone->next_clone)
1099 if (clone->aux)
1100 break;
1101 if (!clone)
1103 DECL_SAVED_TREE (node->decl) = NULL;
1104 DECL_STRUCT_FUNCTION (node->decl) = NULL;
1105 DECL_INITIAL (node->decl) = error_mark_node;
1107 cgraph_node_remove_callees (node);
1108 node->analyzed = false;
1110 else
1111 cgraph_remove_node (node);
1113 if (!DECL_SAVED_TREE (decl))
1114 insns += local_insns;
1115 changed = true;
1118 for (node = cgraph_nodes; node; node = node->next)
1119 node->aux = NULL;
1120 if (cgraph_dump_file)
1121 fprintf (cgraph_dump_file, "\nReclaimed %i insns", insns);
1122 return changed;
1125 /* Estimate size of the function after inlining WHAT into TO. */
1127 static int
1128 cgraph_estimate_size_after_inlining (int times, struct cgraph_node *to,
1129 struct cgraph_node *what)
1131 tree fndecl = what->decl;
1132 tree arg;
1133 int call_insns = PARAM_VALUE (PARAM_INLINE_CALL_COST);
1134 for (arg = DECL_ARGUMENTS (fndecl); arg; arg = TREE_CHAIN (arg))
1135 call_insns += estimate_move_cost (TREE_TYPE (arg));
1136 return (what->global.insns - call_insns) * times + to->global.insns;
1139 /* Estimate the growth caused by inlining NODE into all callees. */
1141 static int
1142 cgraph_estimate_growth (struct cgraph_node *node)
1144 int growth = 0;
1145 struct cgraph_edge *e;
1147 for (e = node->callers; e; e = e->next_caller)
1148 if (e->inline_failed)
1149 growth += (cgraph_estimate_size_after_inlining (1, e->caller, node)
1150 - e->caller->global.insns);
1152 /* ??? Wrong for self recursive functions or cases where we decide to not
1153 inline for different reasons, but it is not big deal as in that case
1154 we will keep the body around, but we will also avoid some inlining. */
1155 if (!node->needed && !DECL_EXTERNAL (node->decl))
1156 growth -= node->global.insns;
1158 return growth;
1161 /* E is expected to be an edge being inlined. Clone destination node of
1162 the edge and redirect it to the new clone.
1163 DUPLICATE is used for bookkeeping on whether we are actually creating new
1164 clones or re-using node originally representing out-of-line function call.
1166 void
1167 cgraph_clone_inlined_nodes (struct cgraph_edge *e, bool duplicate)
1169 struct cgraph_node *n;
1171 /* We may eliminate the need for out-of-line copy to be output. In that
1172 case just go ahead and re-use it. */
1173 if (!e->callee->callers->next_caller
1174 && (!e->callee->needed || DECL_EXTERNAL (e->callee->decl))
1175 && duplicate
1176 && flag_unit_at_a_time)
1178 gcc_assert (!e->callee->global.inlined_to);
1179 if (!DECL_EXTERNAL (e->callee->decl))
1180 overall_insns -= e->callee->global.insns, nfunctions_inlined++;
1181 duplicate = 0;
1183 else if (duplicate)
1185 n = cgraph_clone_node (e->callee);
1186 cgraph_redirect_edge_callee (e, n);
1189 if (e->caller->global.inlined_to)
1190 e->callee->global.inlined_to = e->caller->global.inlined_to;
1191 else
1192 e->callee->global.inlined_to = e->caller;
1194 /* Recursively clone all bodies. */
1195 for (e = e->callee->callees; e; e = e->next_callee)
1196 if (!e->inline_failed)
1197 cgraph_clone_inlined_nodes (e, duplicate);
1200 /* Mark edge E as inlined and update callgraph accordingly. */
1202 void
1203 cgraph_mark_inline_edge (struct cgraph_edge *e)
1205 int old_insns = 0, new_insns = 0;
1206 struct cgraph_node *to = NULL, *what;
1208 gcc_assert (e->inline_failed);
1209 e->inline_failed = NULL;
1211 if (!e->callee->global.inlined && flag_unit_at_a_time)
1212 DECL_POSSIBLY_INLINED (e->callee->decl) = true;
1213 e->callee->global.inlined = true;
1215 cgraph_clone_inlined_nodes (e, true);
1217 what = e->callee;
1219 /* Now update size of caller and all functions caller is inlined into. */
1220 for (;e && !e->inline_failed; e = e->caller->callers)
1222 old_insns = e->caller->global.insns;
1223 new_insns = cgraph_estimate_size_after_inlining (1, e->caller,
1224 what);
1225 gcc_assert (new_insns >= 0);
1226 to = e->caller;
1227 to->global.insns = new_insns;
1229 gcc_assert (what->global.inlined_to == to);
1230 if (new_insns > old_insns)
1231 overall_insns += new_insns - old_insns;
1232 ncalls_inlined++;
1235 /* Mark all calls of EDGE->CALLEE inlined into EDGE->CALLER.
1236 Return following unredirected edge in the list of callers
1237 of EDGE->CALLEE */
1239 static struct cgraph_edge *
1240 cgraph_mark_inline (struct cgraph_edge *edge)
1242 struct cgraph_node *to = edge->caller;
1243 struct cgraph_node *what = edge->callee;
1244 struct cgraph_edge *e, *next;
1245 int times = 0;
1247 /* Look for all calls, mark them inline and clone recursively
1248 all inlined functions. */
1249 for (e = what->callers; e; e = next)
1251 next = e->next_caller;
1252 if (e->caller == to && e->inline_failed)
1254 cgraph_mark_inline_edge (e);
1255 if (e == edge)
1256 edge = next;
1257 times++;
1260 gcc_assert (times);
1261 return edge;
1264 /* Return false when inlining WHAT into TO is not good idea
1265 as it would cause too large growth of function bodies. */
1267 static bool
1268 cgraph_check_inline_limits (struct cgraph_node *to, struct cgraph_node *what,
1269 const char **reason)
1271 int times = 0;
1272 struct cgraph_edge *e;
1273 int newsize;
1274 int limit;
1276 if (to->global.inlined_to)
1277 to = to->global.inlined_to;
1279 for (e = to->callees; e; e = e->next_callee)
1280 if (e->callee == what)
1281 times++;
1283 /* When inlining large function body called once into small function,
1284 take the inlined function as base for limiting the growth. */
1285 if (to->local.self_insns > what->local.self_insns)
1286 limit = to->local.self_insns;
1287 else
1288 limit = what->local.self_insns;
1290 limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
1292 newsize = cgraph_estimate_size_after_inlining (times, to, what);
1293 if (newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
1294 && newsize > limit)
1296 if (reason)
1297 *reason = N_("--param large-function-growth limit reached");
1298 return false;
1300 return true;
1303 /* Return true when function N is small enough to be inlined. */
1305 static bool
1306 cgraph_default_inline_p (struct cgraph_node *n)
1308 if (!DECL_INLINE (n->decl) || !DECL_SAVED_TREE (n->decl))
1309 return false;
1310 if (DECL_DECLARED_INLINE_P (n->decl))
1311 return n->global.insns < MAX_INLINE_INSNS_SINGLE;
1312 else
1313 return n->global.insns < MAX_INLINE_INSNS_AUTO;
1316 /* Return true when inlining WHAT would create recursive inlining.
1317 We call recursive inlining all cases where same function appears more than
1318 once in the single recursion nest path in the inline graph. */
1320 static bool
1321 cgraph_recursive_inlining_p (struct cgraph_node *to,
1322 struct cgraph_node *what,
1323 const char **reason)
1325 bool recursive;
1326 if (to->global.inlined_to)
1327 recursive = what->decl == to->global.inlined_to->decl;
1328 else
1329 recursive = what->decl == to->decl;
1330 /* Marking recursive function inline has sane semantic and thus we should
1331 not warn on it. */
1332 if (recursive && reason)
1333 *reason = (what->local.disregard_inline_limits
1334 ? N_("recursive inlining") : "");
1335 return recursive;
1338 /* Recompute heap nodes for each of callees. */
1339 static void
1340 update_callee_keys (fibheap_t heap, struct fibnode **heap_node,
1341 struct cgraph_node *node)
1343 struct cgraph_edge *e;
1345 for (e = node->callees; e; e = e->next_callee)
1346 if (e->inline_failed && heap_node[e->callee->uid])
1347 fibheap_replace_key (heap, heap_node[e->callee->uid],
1348 cgraph_estimate_growth (e->callee));
1349 else if (!e->inline_failed)
1350 update_callee_keys (heap, heap_node, e->callee);
1353 /* Enqueue all recursive calls from NODE into queue linked via aux pointers
1354 in between FIRST and LAST. WHERE is used for bookkeeping while looking
1355 int calls inlined within NODE. */
1356 static void
1357 lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
1358 struct cgraph_edge **first, struct cgraph_edge **last)
1360 struct cgraph_edge *e;
1361 for (e = where->callees; e; e = e->next_callee)
1362 if (e->callee == node)
1364 if (!*first)
1365 *first = e;
1366 else
1367 (*last)->aux = e;
1368 *last = e;
1370 for (e = where->callees; e; e = e->next_callee)
1371 if (!e->inline_failed)
1372 lookup_recursive_calls (node, e->callee, first, last);
1375 /* Decide on recursive inlining: in the case function has recursive calls,
1376 inline until body size reaches given argument. */
1377 static void
1378 cgraph_decide_recursive_inlining (struct cgraph_node *node)
1380 int limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO);
1381 int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO);
1382 struct cgraph_edge *first_call = NULL, *last_call = NULL;
1383 struct cgraph_edge *last_in_current_depth;
1384 struct cgraph_edge *e;
1385 struct cgraph_node *master_clone;
1386 int depth = 0;
1387 int n = 0;
1389 if (DECL_DECLARED_INLINE_P (node->decl))
1391 limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE);
1392 max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH);
1395 /* Make sure that function is small enough to be considered for inlining. */
1396 if (!max_depth
1397 || cgraph_estimate_size_after_inlining (1, node, node) >= limit)
1398 return;
1399 lookup_recursive_calls (node, node, &first_call, &last_call);
1400 if (!first_call)
1401 return;
1403 if (cgraph_dump_file)
1404 fprintf (cgraph_dump_file,
1405 "\nPerforming recursive inlining on %s\n",
1406 cgraph_node_name (node));
1408 /* We need original clone to copy around. */
1409 master_clone = cgraph_clone_node (node);
1410 master_clone->needed = true;
1411 for (e = master_clone->callees; e; e = e->next_callee)
1412 if (!e->inline_failed)
1413 cgraph_clone_inlined_nodes (e, true);
1415 /* Do the inlining and update list of recursive call during process. */
1416 last_in_current_depth = last_call;
1417 while (first_call
1418 && cgraph_estimate_size_after_inlining (1, node, master_clone) <= limit)
1420 struct cgraph_edge *curr = first_call;
1422 first_call = first_call->aux;
1423 curr->aux = NULL;
1425 cgraph_redirect_edge_callee (curr, master_clone);
1426 cgraph_mark_inline_edge (curr);
1427 lookup_recursive_calls (node, curr->callee, &first_call, &last_call);
1429 if (last_in_current_depth
1430 && ++depth >= max_depth)
1431 break;
1432 n++;
1435 /* Cleanup queue pointers. */
1436 while (first_call)
1438 struct cgraph_edge *next = first_call->aux;
1439 first_call->aux = NULL;
1440 first_call = next;
1442 if (cgraph_dump_file)
1443 fprintf (cgraph_dump_file,
1444 "\n Inlined %i times, body grown from %i to %i insns\n", n,
1445 master_clone->global.insns, node->global.insns);
1447 /* Remove master clone we used for inlining. We rely that clones inlined
1448 into master clone gets queued just before master clone so we don't
1449 need recursion. */
1450 for (node = cgraph_nodes; node != master_clone;
1451 node = node->next)
1452 if (node->global.inlined_to == master_clone)
1453 cgraph_remove_node (node);
1454 cgraph_remove_node (master_clone);
1457 /* Set inline_failed for all callers of given function to REASON. */
1459 static void
1460 cgraph_set_inline_failed (struct cgraph_node *node, const char *reason)
1462 struct cgraph_edge *e;
1464 if (cgraph_dump_file)
1465 fprintf (cgraph_dump_file, "Inlining failed: %s\n", reason);
1466 for (e = node->callers; e; e = e->next_caller)
1467 if (e->inline_failed)
1468 e->inline_failed = reason;
1471 /* We use greedy algorithm for inlining of small functions:
1472 All inline candidates are put into prioritized heap based on estimated
1473 growth of the overall number of instructions and then update the estimates.
1475 INLINED and INLINED_CALEES are just pointers to arrays large enough
1476 to be passed to cgraph_inlined_into and cgraph_inlined_callees. */
1478 static void
1479 cgraph_decide_inlining_of_small_functions (void)
1481 struct cgraph_node *node;
1482 fibheap_t heap = fibheap_new ();
1483 struct fibnode **heap_node =
1484 xcalloc (cgraph_max_uid, sizeof (struct fibnode *));
1485 int max_insns = ((HOST_WIDEST_INT) initial_insns
1486 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
1488 /* Put all inline candidates into the heap. */
1490 for (node = cgraph_nodes; node; node = node->next)
1492 if (!node->local.inlinable || !node->callers
1493 || node->local.disregard_inline_limits)
1494 continue;
1496 if (!cgraph_default_inline_p (node))
1498 cgraph_set_inline_failed (node,
1499 N_("--param max-inline-insns-single limit reached"));
1500 continue;
1502 heap_node[node->uid] =
1503 fibheap_insert (heap, cgraph_estimate_growth (node), node);
1506 if (cgraph_dump_file)
1507 fprintf (cgraph_dump_file, "\nDeciding on smaller functions:\n");
1508 while (overall_insns <= max_insns && (node = fibheap_extract_min (heap)))
1510 struct cgraph_edge *e, *next;
1511 int old_insns = overall_insns;
1513 heap_node[node->uid] = NULL;
1514 if (cgraph_dump_file)
1515 fprintf (cgraph_dump_file,
1516 "\nConsidering %s with %i insns\n"
1517 " Estimated growth is %+i insns.\n",
1518 cgraph_node_name (node), node->global.insns,
1519 cgraph_estimate_growth (node));
1520 if (!cgraph_default_inline_p (node))
1522 cgraph_set_inline_failed (node,
1523 N_("--param max-inline-insns-single limit reached after inlining into the callee"));
1524 continue;
1526 for (e = node->callers; e; e = next)
1528 next = e->next_caller;
1529 if (e->inline_failed)
1531 struct cgraph_node *where;
1533 if (cgraph_recursive_inlining_p (e->caller, e->callee,
1534 &e->inline_failed)
1535 || !cgraph_check_inline_limits (e->caller, e->callee,
1536 &e->inline_failed))
1538 if (cgraph_dump_file)
1539 fprintf (cgraph_dump_file, " Not inlining into %s:%s.\n",
1540 cgraph_node_name (e->caller), e->inline_failed);
1541 continue;
1543 next = cgraph_mark_inline (e);
1544 where = e->caller;
1545 if (where->global.inlined_to)
1546 where = where->global.inlined_to;
1548 if (heap_node[where->uid])
1549 fibheap_replace_key (heap, heap_node[where->uid],
1550 cgraph_estimate_growth (where));
1552 if (cgraph_dump_file)
1553 fprintf (cgraph_dump_file,
1554 " Inlined into %s which now has %i insns.\n",
1555 cgraph_node_name (e->caller),
1556 e->caller->global.insns);
1560 cgraph_decide_recursive_inlining (node);
1562 /* Similarly all functions called by the function we just inlined
1563 are now called more times; update keys. */
1564 update_callee_keys (heap, heap_node, node);
1566 if (cgraph_dump_file)
1567 fprintf (cgraph_dump_file,
1568 " Inlined for a net change of %+i insns.\n",
1569 overall_insns - old_insns);
1571 while ((node = fibheap_extract_min (heap)) != NULL)
1572 if (!node->local.disregard_inline_limits)
1573 cgraph_set_inline_failed (node, N_("--param inline-unit-growth limit reached"));
1574 fibheap_delete (heap);
1575 free (heap_node);
1578 /* Decide on the inlining. We do so in the topological order to avoid
1579 expenses on updating data structures. */
1581 static void
1582 cgraph_decide_inlining (void)
1584 struct cgraph_node *node;
1585 int nnodes;
1586 struct cgraph_node **order =
1587 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
1588 int old_insns = 0;
1589 int i;
1591 for (node = cgraph_nodes; node; node = node->next)
1592 initial_insns += node->local.self_insns;
1593 overall_insns = initial_insns;
1595 nnodes = cgraph_postorder (order);
1597 if (cgraph_dump_file)
1598 fprintf (cgraph_dump_file,
1599 "\nDeciding on inlining. Starting with %i insns.\n",
1600 initial_insns);
1602 for (node = cgraph_nodes; node; node = node->next)
1603 node->aux = 0;
1605 if (cgraph_dump_file)
1606 fprintf (cgraph_dump_file, "\nInlining always_inline functions:\n");
1608 /* In the first pass mark all always_inline edges. Do this with a priority
1609 so none of our later choices will make this impossible. */
1610 for (i = nnodes - 1; i >= 0; i--)
1612 struct cgraph_edge *e, *next;
1614 node = order[i];
1616 if (!node->local.disregard_inline_limits)
1617 continue;
1618 if (cgraph_dump_file)
1619 fprintf (cgraph_dump_file,
1620 "\nConsidering %s %i insns (always inline)\n",
1621 cgraph_node_name (node), node->global.insns);
1622 old_insns = overall_insns;
1623 for (e = node->callers; e; e = next)
1625 next = e->next_caller;
1626 if (!e->inline_failed)
1627 continue;
1628 if (cgraph_recursive_inlining_p (e->caller, e->callee,
1629 &e->inline_failed))
1630 continue;
1631 cgraph_mark_inline_edge (e);
1632 if (cgraph_dump_file)
1633 fprintf (cgraph_dump_file,
1634 " Inlined into %s which now has %i insns.\n",
1635 cgraph_node_name (e->caller),
1636 e->caller->global.insns);
1638 if (cgraph_dump_file)
1639 fprintf (cgraph_dump_file,
1640 " Inlined for a net change of %+i insns.\n",
1641 overall_insns - old_insns);
1644 if (!flag_really_no_inline)
1646 cgraph_decide_inlining_of_small_functions ();
1648 if (cgraph_dump_file)
1649 fprintf (cgraph_dump_file, "\nDeciding on functions called once:\n");
1651 /* And finally decide what functions are called once. */
1653 for (i = nnodes - 1; i >= 0; i--)
1655 node = order[i];
1657 if (node->callers && !node->callers->next_caller && !node->needed
1658 && node->local.inlinable && node->callers->inline_failed
1659 && !DECL_EXTERNAL (node->decl) && !DECL_COMDAT (node->decl))
1661 bool ok = true;
1662 struct cgraph_node *node1;
1664 /* Verify that we won't duplicate the caller. */
1665 for (node1 = node->callers->caller;
1666 node1->callers && !node1->callers->inline_failed
1667 && ok; node1 = node1->callers->caller)
1668 if (node1->callers->next_caller || node1->needed)
1669 ok = false;
1670 if (ok)
1672 if (cgraph_dump_file)
1673 fprintf (cgraph_dump_file,
1674 "\nConsidering %s %i insns.\n"
1675 " Called once from %s %i insns.\n",
1676 cgraph_node_name (node), node->global.insns,
1677 cgraph_node_name (node->callers->caller),
1678 node->callers->caller->global.insns);
1680 old_insns = overall_insns;
1682 if (cgraph_check_inline_limits (node->callers->caller, node,
1683 NULL))
1685 cgraph_mark_inline (node->callers);
1686 if (cgraph_dump_file)
1687 fprintf (cgraph_dump_file,
1688 " Inlined into %s which now has %i insns"
1689 " for a net change of %+i insns.\n",
1690 cgraph_node_name (node->callers->caller),
1691 node->callers->caller->global.insns,
1692 overall_insns - old_insns);
1694 else
1696 if (cgraph_dump_file)
1697 fprintf (cgraph_dump_file,
1698 " Inline limit reached, not inlined.\n");
1705 /* We will never output extern functions we didn't inline.
1706 ??? Perhaps we can prevent accounting of growth of external
1707 inline functions. */
1708 cgraph_remove_unreachable_nodes ();
1710 if (cgraph_dump_file)
1711 fprintf (cgraph_dump_file,
1712 "\nInlined %i calls, eliminated %i functions, "
1713 "%i insns turned to %i insns.\n\n",
1714 ncalls_inlined, nfunctions_inlined, initial_insns,
1715 overall_insns);
1716 free (order);
1719 /* Decide on the inlining. We do so in the topological order to avoid
1720 expenses on updating data structures. */
1722 static void
1723 cgraph_decide_inlining_incrementally (struct cgraph_node *node)
1725 struct cgraph_edge *e;
1727 /* First of all look for always inline functions. */
1728 for (e = node->callees; e; e = e->next_callee)
1729 if (e->callee->local.disregard_inline_limits
1730 && e->inline_failed
1731 && !cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed)
1732 /* ??? It is possible that renaming variable removed the function body
1733 in duplicate_decls. See gcc.c-torture/compile/20011119-2.c */
1734 && DECL_SAVED_TREE (e->callee->decl))
1735 cgraph_mark_inline (e);
1737 /* Now do the automatic inlining. */
1738 if (!flag_really_no_inline)
1739 for (e = node->callees; e; e = e->next_callee)
1740 if (e->callee->local.inlinable
1741 && e->inline_failed
1742 && !e->callee->local.disregard_inline_limits
1743 && !cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed)
1744 && cgraph_check_inline_limits (node, e->callee, &e->inline_failed)
1745 && DECL_SAVED_TREE (e->callee->decl))
1747 if (cgraph_default_inline_p (e->callee))
1748 cgraph_mark_inline (e);
1749 else
1750 e->inline_failed
1751 = N_("--param max-inline-insns-single limit reached");
1756 /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */
1758 bool
1759 cgraph_inline_p (struct cgraph_edge *e, const char **reason)
1761 *reason = e->inline_failed;
1762 return !e->inline_failed;
1767 /* Expand all functions that must be output.
1769 Attempt to topologically sort the nodes so function is output when
1770 all called functions are already assembled to allow data to be
1771 propagated across the callgraph. Use a stack to get smaller distance
1772 between a function and its callees (later we may choose to use a more
1773 sophisticated algorithm for function reordering; we will likely want
1774 to use subsections to make the output functions appear in top-down
1775 order). */
1777 static void
1778 cgraph_expand_all_functions (void)
1780 struct cgraph_node *node;
1781 struct cgraph_node **order =
1782 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
1783 int order_pos = 0, new_order_pos = 0;
1784 int i;
1786 order_pos = cgraph_postorder (order);
1787 gcc_assert (order_pos == cgraph_n_nodes);
1789 /* Garbage collector may remove inline clones we eliminate during
1790 optimization. So we must be sure to not reference them. */
1791 for (i = 0; i < order_pos; i++)
1792 if (order[i]->output)
1793 order[new_order_pos++] = order[i];
1795 for (i = new_order_pos - 1; i >= 0; i--)
1797 node = order[i];
1798 if (node->output)
1800 gcc_assert (node->reachable);
1801 node->output = 0;
1802 cgraph_expand_function (node);
1805 free (order);
1808 /* Mark all local functions.
1810 A local function is one whose calls can occur only in the current
1811 compilation unit and all its calls are explicit, so we can change
1812 its calling convention. We simply mark all static functions whose
1813 address is not taken as local. */
1815 static void
1816 cgraph_mark_local_functions (void)
1818 struct cgraph_node *node;
1820 /* Figure out functions we want to assemble. */
1821 for (node = cgraph_nodes; node; node = node->next)
1823 node->local.local = (!node->needed
1824 && DECL_SAVED_TREE (node->decl)
1825 && !TREE_PUBLIC (node->decl));
1828 if (cgraph_dump_file)
1830 fprintf (cgraph_dump_file, "\nMarking local functions:");
1831 for (node = cgraph_nodes; node; node = node->next)
1832 if (node->local.local)
1833 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
1834 fprintf (cgraph_dump_file, "\n\n");
1838 /* Return true when function body of DECL still needs to be kept around
1839 for later re-use. */
1840 bool
1841 cgraph_preserve_function_body_p (tree decl)
1843 struct cgraph_node *node;
1844 /* Keep the body; we're going to dump it. */
1845 if (dump_enabled_p (TDI_tree_all))
1846 return true;
1847 if (!cgraph_global_info_ready)
1848 return (DECL_INLINE (decl) && !flag_really_no_inline);
1849 /* Look if there is any clone around. */
1850 for (node = cgraph_node (decl); node; node = node->next_clone)
1851 if (node->global.inlined_to)
1852 return true;
1853 return false;
1856 /* Perform simple optimizations based on callgraph. */
1858 void
1859 cgraph_optimize (void)
1861 #ifdef ENABLE_CHECKING
1862 verify_cgraph ();
1863 #endif
1864 if (!flag_unit_at_a_time)
1866 cgraph_varpool_assemble_pending_decls ();
1867 return;
1870 process_pending_assemble_externals ();
1872 /* Frontend may output common variables after the unit has been finalized.
1873 It is safe to deal with them here as they are always zero initialized. */
1874 cgraph_varpool_analyze_pending_decls ();
1876 timevar_push (TV_CGRAPHOPT);
1877 if (!quiet_flag)
1878 fprintf (stderr, "Performing intraprocedural optimizations\n");
1880 cgraph_mark_local_functions ();
1881 if (cgraph_dump_file)
1883 fprintf (cgraph_dump_file, "Marked ");
1884 dump_cgraph (cgraph_dump_file);
1887 if (flag_inline_trees)
1888 cgraph_decide_inlining ();
1889 cgraph_global_info_ready = true;
1890 if (cgraph_dump_file)
1892 fprintf (cgraph_dump_file, "Optimized ");
1893 dump_cgraph (cgraph_dump_file);
1894 dump_varpool (cgraph_dump_file);
1896 timevar_pop (TV_CGRAPHOPT);
1898 /* Output everything. */
1899 if (!quiet_flag)
1900 fprintf (stderr, "Assembling functions:\n");
1901 #ifdef ENABLE_CHECKING
1902 verify_cgraph ();
1903 #endif
1905 cgraph_mark_functions_to_output ();
1906 cgraph_expand_all_functions ();
1907 cgraph_varpool_remove_unreferenced_decls ();
1909 cgraph_varpool_assemble_pending_decls ();
1911 if (cgraph_dump_file)
1913 fprintf (cgraph_dump_file, "\nFinal ");
1914 dump_cgraph (cgraph_dump_file);
1916 #ifdef ENABLE_CHECKING
1917 verify_cgraph ();
1918 /* Double check that all inline clones are gone and that all
1919 function bodies have been released from memory. */
1920 if (flag_unit_at_a_time
1921 && !dump_enabled_p (TDI_tree_all)
1922 && !(sorrycount || errorcount))
1924 struct cgraph_node *node;
1925 bool error_found = false;
1927 for (node = cgraph_nodes; node; node = node->next)
1928 if (node->analyzed
1929 && (node->global.inlined_to
1930 || DECL_SAVED_TREE (node->decl)))
1932 error_found = true;
1933 dump_cgraph_node (stderr, node);
1935 if (error_found)
1936 internal_error ("Nodes with no released memory found.");
1938 #endif
1941 /* Generate and emit a static constructor or destructor. WHICH must be
1942 one of 'I' or 'D'. BODY should be a STATEMENT_LIST containing
1943 GENERIC statements. */
1945 void
1946 cgraph_build_static_cdtor (char which, tree body, int priority)
1948 static int counter = 0;
1949 char which_buf[16];
1950 tree decl, name, resdecl;
1952 sprintf (which_buf, "%c_%d", which, counter++);
1953 name = get_file_function_name_long (which_buf);
1955 decl = build_decl (FUNCTION_DECL, name,
1956 build_function_type (void_type_node, void_list_node));
1957 current_function_decl = decl;
1959 resdecl = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
1960 DECL_ARTIFICIAL (resdecl) = 1;
1961 DECL_IGNORED_P (resdecl) = 1;
1962 DECL_RESULT (decl) = resdecl;
1964 allocate_struct_function (decl);
1966 TREE_STATIC (decl) = 1;
1967 TREE_USED (decl) = 1;
1968 DECL_ARTIFICIAL (decl) = 1;
1969 DECL_IGNORED_P (decl) = 1;
1970 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1971 DECL_SAVED_TREE (decl) = body;
1972 TREE_PUBLIC (decl) = ! targetm.have_ctors_dtors;
1973 DECL_UNINLINABLE (decl) = 1;
1975 DECL_INITIAL (decl) = make_node (BLOCK);
1976 TREE_USED (DECL_INITIAL (decl)) = 1;
1978 DECL_SOURCE_LOCATION (decl) = input_location;
1979 cfun->function_end_locus = input_location;
1981 switch (which)
1983 case 'I':
1984 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1985 break;
1986 case 'D':
1987 DECL_STATIC_DESTRUCTOR (decl) = 1;
1988 break;
1989 default:
1990 gcc_unreachable ();
1993 gimplify_function_tree (decl);
1995 /* ??? We will get called LATE in the compilation process. */
1996 if (cgraph_global_info_ready)
1997 tree_rest_of_compilation (decl);
1998 else
1999 cgraph_finalize_function (decl, 0);
2001 if (targetm.have_ctors_dtors)
2003 void (*fn) (rtx, int);
2005 if (which == 'I')
2006 fn = targetm.asm_out.constructor;
2007 else
2008 fn = targetm.asm_out.destructor;
2009 fn (XEXP (DECL_RTL (decl), 0), priority);
2013 void
2014 init_cgraph (void)
2016 cgraph_dump_file = dump_begin (TDI_cgraph, NULL);