* dbxout.c (current_file): Also wrap inside DBX_DEBUGGING_INFO ||
[official-gcc.git] / gcc / cgraphunit.c
blob673419fa008fafb376b267918198c6266f21e63b
1 /* Callgraph based intraprocedural optimizations.
2 Copyright (C) 2003 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 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "tree-inline.h"
28 #include "langhooks.h"
29 #include "hashtab.h"
30 #include "toplev.h"
31 #include "flags.h"
32 #include "ggc.h"
33 #include "debug.h"
34 #include "target.h"
35 #include "cgraph.h"
36 #include "diagnostic.h"
37 #include "timevar.h"
38 #include "params.h"
39 #include "fibheap.h"
40 #include "c-common.h"
42 #define INSNS_PER_CALL 10
44 static void cgraph_expand_all_functions (void);
45 static void cgraph_mark_functions_to_output (void);
46 static void cgraph_expand_function (struct cgraph_node *);
47 static tree record_call_1 (tree *, int *, void *);
48 static void cgraph_mark_local_functions (void);
49 static void cgraph_optimize_function (struct cgraph_node *);
50 static bool cgraph_default_inline_p (struct cgraph_node *n);
51 static void cgraph_analyze_function (struct cgraph_node *node);
52 static void cgraph_decide_inlining_incrementally (struct cgraph_node *);
54 /* Statistics we collect about inlining algorithm. */
55 static int ncalls_inlined;
56 static int nfunctions_inlined;
57 static int initial_insns;
58 static int overall_insns;
60 /* Records tree nodes seen in cgraph_create_edges. Simply using
61 walk_tree_without_duplicates doesn't guarantee each node is visited
62 once because it gets a new htab upon each recursive call from
63 record_calls_1. */
64 static htab_t visited_nodes;
66 /* Determine if function DECL is needed. That is, visible to something
67 either outside this translation unit, something magic in the system
68 configury, or (if not doing unit-at-a-time) to something we havn't
69 seen yet. */
71 static bool
72 decide_is_function_needed (struct cgraph_node *node, tree decl)
74 /* If we decided it was needed before, but at the time we didn't have
75 the body of the function available, then it's still needed. We have
76 to go back and re-check its dependencies now. */
77 if (node->needed)
78 return true;
80 /* Externally visible functions must be output. The exception is
81 COMDAT functions that must be output only when they are needed. */
82 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
83 return true;
85 /* Constructors and destructors are reachable from the runtime by
86 some mechanism. */
87 if (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl))
88 return true;
90 /* If the user told us it is used, then it must be so. */
91 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
92 return true;
94 /* ??? If the assembler name is set by hand, it is possible to assemble
95 the name later after finalizing the function and the fact is noticed
96 in assemble_name then. This is arguably a bug. */
97 if (DECL_ASSEMBLER_NAME_SET_P (decl)
98 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
99 return true;
101 if (flag_unit_at_a_time)
102 return false;
104 /* If not doing unit at a time, then we'll only defer this function
105 if its marked for inlining. Otherwise we want to emit it now. */
107 /* "extern inline" functions are never output locally. */
108 if (DECL_EXTERNAL (decl))
109 return false;
110 /* We want to emit COMDAT functions only when absolutely necessary. */
111 if (DECL_COMDAT (decl))
112 return false;
113 if (!DECL_INLINE (decl)
114 || (!node->local.disregard_inline_limits
115 /* When declared inline, defer even the uninlinable functions.
116 This allows them to be eliminated when unused. */
117 && !DECL_DECLARED_INLINE_P (decl)
118 && (!node->local.inlinable || !cgraph_default_inline_p (node))))
119 return true;
121 return false;
124 /* When not doing unit-at-a-time, output all functions enqueued.
125 Return true when such a functions were found. */
127 bool
128 cgraph_assemble_pending_functions (void)
130 bool output = false;
132 if (flag_unit_at_a_time)
133 return false;
135 while (cgraph_nodes_queue)
137 struct cgraph_node *n = cgraph_nodes_queue;
139 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
140 if (!n->origin && !DECL_EXTERNAL (n->decl))
142 cgraph_expand_function (n);
143 output = true;
147 return output;
150 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
151 logic in effect. If NESTED is true, then our caller cannot stand to have
152 the garbage collector run at the moment. We would need to either create
153 a new GC context, or just not compile right now. */
155 void
156 cgraph_finalize_function (tree decl, bool nested)
158 struct cgraph_node *node = cgraph_node (decl);
160 if (node->local.finalized)
162 /* As an GCC extension we allow redefinition of the function. The
163 semantics when both copies of bodies differ is not well defined.
164 We replace the old body with new body so in unit at a time mode
165 we always use new body, while in normal mode we may end up with
166 old body inlined into some functions and new body expanded and
167 inlined in others.
169 ??? It may make more sense to use one body for inlining and other
170 body for expanding the function but this is difficult to do. */
172 /* If node->output is set, then this is a unit-at-a-time compilation
173 and we have already begun whole-unit analysis. This is *not*
174 testing for whether we've already emitted the function. That
175 case can be sort-of legitimately seen with real function
176 redefinition errors. I would argue that the front end should
177 never present us with such a case, but don't enforce that for now. */
178 if (node->output)
179 abort ();
181 /* Reset our datastructures so we can analyze the function again. */
182 memset (&node->local, 0, sizeof (node->local));
183 memset (&node->global, 0, sizeof (node->global));
184 memset (&node->rtl, 0, sizeof (node->rtl));
185 node->analyzed = false;
186 while (node->callees)
187 cgraph_remove_edge (node, node->callees->callee);
189 /* We may need to re-queue the node for assembling in case
190 we already proceeded it and ignored as not needed. */
191 if (node->reachable && !flag_unit_at_a_time)
193 struct cgraph_node *n;
195 for (n = cgraph_nodes_queue; n; n = n->next_needed)
196 if (n == node)
197 break;
198 if (!n)
199 node->reachable = 0;
203 notice_global_symbol (decl);
204 node->decl = decl;
205 node->local.finalized = true;
207 /* If not unit at a time, then we need to create the call graph
208 now, so that called functions can be queued and emitted now. */
209 if (!flag_unit_at_a_time)
211 cgraph_analyze_function (node);
212 cgraph_decide_inlining_incrementally (node);
215 if (decide_is_function_needed (node, decl))
216 cgraph_mark_needed_node (node);
218 /* If not unit at a time, go ahead and emit everything we've found
219 to be reachable at this time. */
220 if (!nested)
221 cgraph_assemble_pending_functions ();
223 /* If we've not yet emitted decl, tell the debug info about it. */
224 if (!TREE_ASM_WRITTEN (decl))
225 (*debug_hooks->deferred_inline_function) (decl);
228 /* Walk tree and record all calls. Called via walk_tree. */
229 static tree
230 record_call_1 (tree *tp, int *walk_subtrees, void *data)
232 tree t = *tp;
234 switch (TREE_CODE (t))
236 case VAR_DECL:
237 /* ??? Really, we should mark this decl as *potentially* referenced
238 by this function and re-examine whether the decl is actually used
239 after rtl has been generated. */
240 if (TREE_STATIC (t))
241 cgraph_varpool_mark_needed_node (cgraph_varpool_node (t));
242 break;
244 case ADDR_EXPR:
245 if (flag_unit_at_a_time)
247 /* Record dereferences to the functions. This makes the
248 functions reachable unconditionally. */
249 tree decl = TREE_OPERAND (*tp, 0);
250 if (TREE_CODE (decl) == FUNCTION_DECL)
251 cgraph_mark_needed_node (cgraph_node (decl));
253 break;
255 case CALL_EXPR:
257 tree decl = get_callee_fndecl (*tp);
258 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
260 if (DECL_BUILT_IN (decl))
261 return NULL;
262 cgraph_record_call (data, decl);
264 /* When we see a function call, we don't want to look at the
265 function reference in the ADDR_EXPR that is hanging from
266 the CALL_EXPR we're examining here, because we would
267 conclude incorrectly that the function's address could be
268 taken by something that is not a function call. So only
269 walk the function parameter list, skip the other subtrees. */
271 walk_tree (&TREE_OPERAND (*tp, 1), record_call_1, data,
272 visited_nodes);
273 *walk_subtrees = 0;
275 break;
278 default:
279 /* Save some cycles by not walking types and declaration as we
280 won't find anything useful there anyway. */
281 if (DECL_P (*tp) || TYPE_P (*tp))
283 *walk_subtrees = 0;
284 break;
287 if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
288 return (*lang_hooks.callgraph.analyze_expr) (tp, walk_subtrees, data);
289 break;
292 return NULL;
295 /* Create cgraph edges for function calls inside BODY from DECL. */
297 void
298 cgraph_create_edges (tree decl, tree body)
300 /* The nodes we're interested in are never shared, so walk
301 the tree ignoring duplicates. */
302 visited_nodes = htab_create (37, htab_hash_pointer,
303 htab_eq_pointer, NULL);
304 walk_tree (&body, record_call_1, decl, visited_nodes);
305 htab_delete (visited_nodes);
306 visited_nodes = NULL;
309 /* Analyze the function scheduled to be output. */
310 static void
311 cgraph_analyze_function (struct cgraph_node *node)
313 tree decl = node->decl;
315 current_function_decl = decl;
317 /* First kill forward declaration so reverse inlining works properly. */
318 cgraph_create_edges (decl, DECL_SAVED_TREE (decl));
320 node->local.inlinable = tree_inlinable_function_p (decl);
321 if (!DECL_ESTIMATED_INSNS (decl))
322 DECL_ESTIMATED_INSNS (decl)
323 = (*lang_hooks.tree_inlining.estimate_num_insns) (decl);
324 node->local.self_insns = DECL_ESTIMATED_INSNS (decl);
325 if (node->local.inlinable)
326 node->local.disregard_inline_limits
327 = (*lang_hooks.tree_inlining.disregard_inline_limits) (decl);
329 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
330 node->global.insns = node->local.self_insns;
331 if (!DECL_EXTERNAL (decl))
333 node->global.cloned_times = 1;
334 node->global.will_be_output = true;
337 node->analyzed = true;
338 current_function_decl = NULL;
341 /* Analyze the whole compilation unit once it is parsed completely. */
343 void
344 cgraph_finalize_compilation_unit (void)
346 struct cgraph_node *node;
348 if (!flag_unit_at_a_time)
350 cgraph_assemble_pending_functions ();
351 return;
354 cgraph_varpool_assemble_pending_decls ();
355 if (!quiet_flag)
356 fprintf (stderr, "\nAnalyzing compilation unit\n");
358 timevar_push (TV_CGRAPH);
359 if (cgraph_dump_file)
361 fprintf (cgraph_dump_file, "Initial entry points:");
362 for (node = cgraph_nodes; node; node = node->next)
363 if (node->needed && DECL_SAVED_TREE (node->decl))
364 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
365 fprintf (cgraph_dump_file, "\n");
368 /* Propagate reachability flag and lower representation of all reachable
369 functions. In the future, lowering will introduce new functions and
370 new entry points on the way (by template instantiation and virtual
371 method table generation for instance). */
372 while (cgraph_nodes_queue)
374 struct cgraph_edge *edge;
375 tree decl = cgraph_nodes_queue->decl;
377 node = cgraph_nodes_queue;
378 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
380 /* ??? It is possible to create extern inline function and later using
381 weak alas attribute to kill it's body. See
382 gcc.c-torture/compile/20011119-1.c */
383 if (!DECL_SAVED_TREE (decl))
384 continue;
386 if (node->analyzed || !node->reachable || !DECL_SAVED_TREE (decl))
387 abort ();
389 cgraph_analyze_function (node);
391 for (edge = node->callees; edge; edge = edge->next_callee)
392 if (!edge->callee->reachable)
393 cgraph_mark_reachable_node (edge->callee);
395 cgraph_varpool_assemble_pending_decls ();
398 /* Collect entry points to the unit. */
400 if (cgraph_dump_file)
402 fprintf (cgraph_dump_file, "Unit entry points:");
403 for (node = cgraph_nodes; node; node = node->next)
404 if (node->needed && DECL_SAVED_TREE (node->decl))
405 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
406 fprintf (cgraph_dump_file, "\n\nInitial ");
407 dump_cgraph (cgraph_dump_file);
410 if (cgraph_dump_file)
411 fprintf (cgraph_dump_file, "\nReclaiming functions:");
413 for (node = cgraph_nodes; node; node = node->next)
415 tree decl = node->decl;
417 if (!node->reachable && DECL_SAVED_TREE (decl))
419 cgraph_remove_node (node);
420 if (cgraph_dump_file)
421 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
424 if (cgraph_dump_file)
426 fprintf (cgraph_dump_file, "\n\nReclaimed ");
427 dump_cgraph (cgraph_dump_file);
429 ggc_collect ();
430 timevar_pop (TV_CGRAPH);
433 /* Figure out what functions we want to assemble. */
435 static void
436 cgraph_mark_functions_to_output (void)
438 struct cgraph_node *node;
440 for (node = cgraph_nodes; node; node = node->next)
442 tree decl = node->decl;
443 struct cgraph_edge *e;
444 if (node->output)
445 abort ();
447 for (e = node->callers; e; e = e->next_caller)
448 if (!e->inline_call)
449 break;
451 /* We need to output all local functions that are used and not
452 always inlined, as well as those that are reachable from
453 outside the current compilation unit. */
454 if (DECL_SAVED_TREE (decl)
455 && (node->needed
456 || (e && node->reachable))
457 && !TREE_ASM_WRITTEN (decl) && !node->origin
458 && !DECL_EXTERNAL (decl))
459 node->output = 1;
463 /* Optimize the function before expansion. */
465 static void
466 cgraph_optimize_function (struct cgraph_node *node)
468 tree decl = node->decl;
470 timevar_push (TV_INTEGRATION);
471 /* optimize_inline_calls avoids inlining of current_function_decl. */
472 current_function_decl = decl;
473 if (flag_inline_trees)
474 optimize_inline_calls (decl);
475 if (node->nested)
477 for (node = node->nested; node; node = node->next_nested)
478 cgraph_optimize_function (node);
480 timevar_pop (TV_INTEGRATION);
483 /* Expand function specified by NODE. */
485 static void
486 cgraph_expand_function (struct cgraph_node *node)
488 tree decl = node->decl;
489 struct cgraph_edge *e;
491 if (flag_unit_at_a_time)
492 announce_function (decl);
494 cgraph_optimize_function (node);
496 /* Generate RTL for the body of DECL. Nested functions are expanded
497 via lang_expand_decl_stmt. */
498 (*lang_hooks.callgraph.expand_function) (decl);
500 if (!flag_unit_at_a_time)
502 if (!node->local.inlinable
503 || (!node->local.disregard_inline_limits
504 && !cgraph_default_inline_p (node)))
505 DECL_SAVED_TREE (node->decl) = NULL;
507 else
509 for (e = node->callers; e; e = e->next_caller)
510 if (e->inline_call)
511 break;
512 if (!e)
513 DECL_SAVED_TREE (decl) = NULL;
515 current_function_decl = NULL;
518 /* Fill array order with all nodes with output flag set in the reverse
519 topological order. */
520 static int
521 cgraph_postorder (struct cgraph_node **order)
523 struct cgraph_node *node, *node2;
524 int stack_size = 0;
525 int order_pos = 0;
526 struct cgraph_edge *edge, last;
528 struct cgraph_node **stack =
529 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
531 /* We have to deal with cycles nicely, so use a depth first traversal
532 output algorithm. Ignore the fact that some functions won't need
533 to be output and put them into order as well, so we get dependencies
534 right through intline functions. */
535 for (node = cgraph_nodes; node; node = node->next)
536 node->aux = NULL;
537 for (node = cgraph_nodes; node; node = node->next)
538 if (!node->aux)
540 node2 = node;
541 if (!node->callers)
542 node->aux = &last;
543 else
544 node->aux = node->callers;
545 while (node2)
547 while (node2->aux != &last)
549 edge = node2->aux;
550 if (edge->next_caller)
551 node2->aux = edge->next_caller;
552 else
553 node2->aux = &last;
554 if (!edge->caller->aux)
556 if (!edge->caller->callers)
557 edge->caller->aux = &last;
558 else
559 edge->caller->aux = edge->caller->callers;
560 stack[stack_size++] = node2;
561 node2 = edge->caller;
562 break;
565 if (node2->aux == &last)
567 order[order_pos++] = node2;
568 if (stack_size)
569 node2 = stack[--stack_size];
570 else
571 node2 = NULL;
575 free (stack);
576 return order_pos;
579 #define INLINED_TIMES(node) ((size_t)(node)->aux)
580 #define SET_INLINED_TIMES(node,times) ((node)->aux = (void *)(times))
582 /* Return list of nodes we decided to inline NODE into, set their output
583 flag and compute INLINED_TIMES.
585 We do simple backtracing to get INLINED_TIMES right. This should not be
586 expensive as we limit the amount of inlining. Alternatively we may first
587 discover set of nodes, topologically sort these and propagate
588 INLINED_TIMES */
590 static int
591 cgraph_inlined_into (struct cgraph_node *node, struct cgraph_node **array)
593 int nfound = 0;
594 struct cgraph_edge **stack;
595 struct cgraph_edge *e, *e1;
596 int sp;
597 int i;
599 /* Fast path: since we traverse in mostly topological order, we will likely
600 find no edges. */
601 for (e = node->callers; e; e = e->next_caller)
602 if (e->inline_call)
603 break;
605 if (!e)
606 return 0;
608 /* Allocate stack for back-tracking up callgraph. */
609 stack = xmalloc ((cgraph_n_nodes + 1) * sizeof (struct cgraph_edge));
610 sp = 0;
612 /* Push the first edge on to the stack. */
613 stack[sp++] = e;
615 while (sp)
617 struct cgraph_node *caller;
619 /* Look at the edge on the top of the stack. */
620 e = stack[sp - 1];
621 caller = e->caller;
623 /* Check if the caller destination has been visited yet. */
624 if (!caller->output)
626 array[nfound++] = e->caller;
627 /* Mark that we have visited the destination. */
628 caller->output = true;
629 SET_INLINED_TIMES (caller, 0);
631 SET_INLINED_TIMES (caller, INLINED_TIMES (caller) + 1);
633 for (e1 = caller->callers; e1; e1 = e1->next_caller)
634 if (e1->inline_call)
635 break;
636 if (e1)
637 stack[sp++] = e1;
638 else
640 while (true)
642 for (e1 = e->next_caller; e1; e1 = e1->next_caller)
643 if (e1->inline_call)
644 break;
646 if (e1)
648 stack[sp - 1] = e1;
649 break;
651 else
653 sp--;
654 if (!sp)
655 break;
656 e = stack[sp - 1];
662 free (stack);
665 if (cgraph_dump_file)
667 fprintf (cgraph_dump_file, " Found inline predecesors of %s:",
668 cgraph_node_name (node));
669 for (i = 0; i < nfound; i++)
671 fprintf (cgraph_dump_file, " %s", cgraph_node_name (array[i]));
672 if (INLINED_TIMES (array[i]) != 1)
673 fprintf (cgraph_dump_file, " (%i times)",
674 (int)INLINED_TIMES (array[i]));
676 fprintf (cgraph_dump_file, "\n");
679 return nfound;
682 /* Return list of nodes we decided to inline into NODE, set their output
683 flag and compute INLINED_TIMES.
685 This function is identical to cgraph_inlined_into with callers and callees
686 nodes swapped. */
688 static int
689 cgraph_inlined_callees (struct cgraph_node *node, struct cgraph_node **array)
691 int nfound = 0;
692 struct cgraph_edge **stack;
693 struct cgraph_edge *e, *e1;
694 int sp;
695 int i;
697 /* Fast path: since we traverse in mostly topological order, we will likely
698 find no edges. */
699 for (e = node->callees; e; e = e->next_callee)
700 if (e->inline_call)
701 break;
703 if (!e)
704 return 0;
706 /* Allocate stack for back-tracking up callgraph. */
707 stack = xmalloc ((cgraph_n_nodes + 1) * sizeof (struct cgraph_edge));
708 sp = 0;
710 /* Push the first edge on to the stack. */
711 stack[sp++] = e;
713 while (sp)
715 struct cgraph_node *callee;
717 /* Look at the edge on the top of the stack. */
718 e = stack[sp - 1];
719 callee = e->callee;
721 /* Check if the callee destination has been visited yet. */
722 if (!callee->output)
724 array[nfound++] = e->callee;
725 /* Mark that we have visited the destination. */
726 callee->output = true;
727 SET_INLINED_TIMES (callee, 0);
729 SET_INLINED_TIMES (callee, INLINED_TIMES (callee) + 1);
731 for (e1 = callee->callees; e1; e1 = e1->next_callee)
732 if (e1->inline_call)
733 break;
734 if (e1)
735 stack[sp++] = e1;
736 else
738 while (true)
740 for (e1 = e->next_callee; e1; e1 = e1->next_callee)
741 if (e1->inline_call)
742 break;
744 if (e1)
746 stack[sp - 1] = e1;
747 break;
749 else
751 sp--;
752 if (!sp)
753 break;
754 e = stack[sp - 1];
760 free (stack);
762 if (cgraph_dump_file)
764 fprintf (cgraph_dump_file, " Found inline successors of %s:",
765 cgraph_node_name (node));
766 for (i = 0; i < nfound; i++)
768 fprintf (cgraph_dump_file, " %s", cgraph_node_name (array[i]));
769 if (INLINED_TIMES (array[i]) != 1)
770 fprintf (cgraph_dump_file, " (%i times)",
771 (int)INLINED_TIMES (array[i]));
773 fprintf (cgraph_dump_file, "\n");
776 return nfound;
779 /* Estimate size of the function after inlining WHAT into TO. */
781 static int
782 cgraph_estimate_size_after_inlining (int times, struct cgraph_node *to,
783 struct cgraph_node *what)
785 return (what->global.insns - INSNS_PER_CALL) * times + to->global.insns;
788 /* Estimate the growth caused by inlining NODE into all callees. */
790 static int
791 cgraph_estimate_growth (struct cgraph_node *node)
793 int growth = 0;
794 int calls_saved = 0;
795 int clones_added = 0;
796 struct cgraph_edge *e;
798 for (e = node->callers; e; e = e->next_caller)
799 if (!e->inline_call)
801 growth += ((cgraph_estimate_size_after_inlining (1, e->caller, node)
803 e->caller->global.insns) *e->caller->global.cloned_times);
804 calls_saved += e->caller->global.cloned_times;
805 clones_added += e->caller->global.cloned_times;
808 /* ??? Wrong for self recursive functions or cases where we decide to not
809 inline for different reasons, but it is not big deal as in that case
810 we will keep the body around, but we will also avoid some inlining. */
811 if (!node->needed && !node->origin && !DECL_EXTERNAL (node->decl))
812 growth -= node->global.insns, clones_added--;
814 if (!calls_saved)
815 calls_saved = 1;
817 return growth;
820 /* Update insn sizes after inlining WHAT into TO that is already inlined into
821 all nodes in INLINED array. */
823 static void
824 cgraph_mark_inline (struct cgraph_node *to, struct cgraph_node *what,
825 struct cgraph_node **inlined, int ninlined,
826 struct cgraph_node **inlined_callees,
827 int ninlined_callees)
829 int i;
830 int times = 0;
831 int clones = 0;
832 struct cgraph_edge *e;
833 bool called = false;
834 int new_insns;
836 what->global.inlined = 1;
837 for (e = what->callers; e; e = e->next_caller)
839 if (e->caller == to)
841 if (e->inline_call)
842 abort ();
843 e->inline_call = true;
844 times++;
845 clones += e->caller->global.cloned_times;
847 else if (!e->inline_call)
848 called = true;
850 if (!times)
851 abort ();
852 ncalls_inlined += times;
854 new_insns = cgraph_estimate_size_after_inlining (times, to, what);
855 if (to->global.will_be_output)
856 overall_insns += new_insns - to->global.insns;
857 to->global.insns = new_insns;
859 if (!called && !what->needed && !what->origin
860 && flag_unit_at_a_time
861 && !DECL_EXTERNAL (what->decl))
863 if (!what->global.will_be_output)
864 abort ();
865 clones--;
866 nfunctions_inlined++;
867 what->global.will_be_output = 0;
868 overall_insns -= what->global.insns;
870 what->global.cloned_times += clones;
871 for (i = 0; i < ninlined; i++)
873 new_insns =
874 cgraph_estimate_size_after_inlining (INLINED_TIMES (inlined[i]) *
875 times, inlined[i], what);
876 if (inlined[i]->global.will_be_output)
877 overall_insns += new_insns - inlined[i]->global.insns;
878 inlined[i]->global.insns = new_insns;
880 for (i = 0; i < ninlined_callees; i++)
882 inlined_callees[i]->global.cloned_times +=
883 INLINED_TIMES (inlined_callees[i]) * clones;
887 /* Return false when inlining WHAT into TO is not good idea as it would cause
888 too large growth of function bodies. */
890 static bool
891 cgraph_check_inline_limits (struct cgraph_node *to, struct cgraph_node *what,
892 struct cgraph_node **inlined, int ninlined)
894 int i;
895 int times = 0;
896 struct cgraph_edge *e;
897 int newsize;
898 int limit;
900 for (e = to->callees; e; e = e->next_callee)
901 if (e->callee == what)
902 times++;
904 /* When inlining large function body called once into small function,
905 take the inlined function as base for limiting the growth. */
906 if (to->local.self_insns > what->local.self_insns)
907 limit = to->local.self_insns;
908 else
909 limit = what->local.self_insns;
911 limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
913 newsize = cgraph_estimate_size_after_inlining (times, to, what);
914 if (newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
915 && newsize > limit)
916 return false;
917 for (i = 0; i < ninlined; i++)
919 newsize =
920 cgraph_estimate_size_after_inlining (INLINED_TIMES (inlined[i]) *
921 times, inlined[i], what);
922 if (newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
923 && newsize >
924 inlined[i]->local.self_insns *
925 (100 + PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH)) / 100)
926 return false;
928 return true;
931 /* Return true when function N is small enough to be inlined. */
933 static bool
934 cgraph_default_inline_p (struct cgraph_node *n)
936 if (!DECL_INLINE (n->decl) || !DECL_SAVED_TREE (n->decl))
937 return false;
938 if (DECL_DECLARED_INLINE_P (n->decl))
939 return n->global.insns < MAX_INLINE_INSNS_SINGLE;
940 else
941 return n->global.insns < MAX_INLINE_INSNS_AUTO;
944 /* We use greedy algorithm for inlining of small functions:
945 All inline candidates are put into prioritized heap based on estimated
946 growth of the overall number of instructions and then update the estimates.
948 INLINED and INLINED_CALEES are just pointers to arrays large enought
949 to be passed to cgraph_inlined_into and cgraph_inlined_callees. */
951 static void
952 cgraph_decide_inlining_of_small_functions (struct cgraph_node **inlined,
953 struct cgraph_node **inlined_callees)
955 int i;
956 struct cgraph_node *node;
957 fibheap_t heap = fibheap_new ();
958 struct fibnode **heap_node =
959 xcalloc (cgraph_max_uid, sizeof (struct fibnode *));
960 int ninlined, ninlined_callees;
961 int max_insns = ((HOST_WIDEST_INT) initial_insns
962 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
964 /* Put all inline candidates into the heap. */
966 for (node = cgraph_nodes; node; node = node->next)
968 struct cgraph_edge *e;
970 if (!node->local.inlinable || !node->callers
971 || !cgraph_default_inline_p (node))
972 continue;
974 /* Rule out always_inline functions we dealt with earlier. */
975 for (e = node->callers; e; e = e->next_caller)
976 if (e->inline_call)
977 break;
978 if (e)
979 continue;
980 heap_node[node->uid] =
981 fibheap_insert (heap, cgraph_estimate_growth (node), node);
984 if (cgraph_dump_file)
985 fprintf (cgraph_dump_file, "\nDeciding on smaller functions:\n");
986 while ((node = fibheap_extract_min (heap)) && overall_insns <= max_insns)
988 struct cgraph_edge *e;
989 int old_insns = overall_insns;
991 heap_node[node->uid] = NULL;
992 if (cgraph_dump_file)
993 fprintf (cgraph_dump_file,
994 "\nConsidering %s with %i insns\n"
995 " Estimated growth is %+i insns.\n",
996 cgraph_node_name (node), node->global.insns,
997 cgraph_estimate_growth (node));
998 if (!cgraph_default_inline_p (node))
1000 if (cgraph_dump_file)
1001 fprintf (cgraph_dump_file, " Function too large.\n");
1002 continue;
1004 ninlined_callees = cgraph_inlined_callees (node, inlined_callees);
1005 for (e = node->callers; e; e = e->next_caller)
1006 if (!e->inline_call && e->caller != node)
1008 ninlined = cgraph_inlined_into (e->caller, inlined);
1009 if (e->callee->output
1010 || !cgraph_check_inline_limits (e->caller, node, inlined,
1011 ninlined))
1013 for (i = 0; i < ninlined; i++)
1014 inlined[i]->output = 0, node->aux = 0;
1015 if (cgraph_dump_file)
1016 fprintf (cgraph_dump_file, " Not inlining into %s.\n",
1017 cgraph_node_name (e->caller));
1018 continue;
1020 cgraph_mark_inline (e->caller, node, inlined, ninlined,
1021 inlined_callees, ninlined_callees);
1022 if (heap_node[e->caller->uid])
1023 fibheap_replace_key (heap, heap_node[e->caller->uid],
1024 cgraph_estimate_growth (e->caller));
1026 /* Size of the functions we updated into has changed, so update
1027 the keys. */
1028 for (i = 0; i < ninlined; i++)
1030 inlined[i]->output = 0, node->aux = 0;
1031 if (heap_node[inlined[i]->uid])
1032 fibheap_replace_key (heap, heap_node[inlined[i]->uid],
1033 cgraph_estimate_growth (inlined[i]));
1035 if (cgraph_dump_file)
1036 fprintf (cgraph_dump_file,
1037 " Inlined into %s which now has %i insns.\n",
1038 cgraph_node_name (e->caller),
1039 e->caller->global.insns);
1043 /* Similarly all functions called by the function we just inlined
1044 are now called more times; update keys. */
1046 for (e = node->callees; e; e = e->next_callee)
1047 if (!e->inline_call && heap_node[e->callee->uid])
1048 fibheap_replace_key (heap, heap_node[e->callee->uid],
1049 cgraph_estimate_growth (e->callee));
1051 for (i = 0; i < ninlined_callees; i++)
1053 struct cgraph_edge *e;
1055 for (e = inlined_callees[i]->callees; e; e = e->next_callee)
1056 if (!e->inline_call && heap_node[e->callee->uid])
1057 fibheap_replace_key (heap, heap_node[e->callee->uid],
1058 cgraph_estimate_growth (e->callee));
1060 inlined_callees[i]->output = 0, node->aux = 0;
1062 if (cgraph_dump_file)
1063 fprintf (cgraph_dump_file,
1064 " Inlined %i times for a net change of %+i insns.\n",
1065 node->global.cloned_times, overall_insns - old_insns);
1067 if (cgraph_dump_file && !fibheap_empty (heap))
1068 fprintf (cgraph_dump_file, "\nReached the inline-unit-growth limit.\n");
1069 fibheap_delete (heap);
1070 free (heap_node);
1073 /* Decide on the inlining. We do so in the topological order to avoid
1074 expenses on updating datastructures. */
1076 static void
1077 cgraph_decide_inlining (void)
1079 struct cgraph_node *node;
1080 int nnodes;
1081 struct cgraph_node **order =
1082 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
1083 struct cgraph_node **inlined =
1084 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
1085 struct cgraph_node **inlined_callees =
1086 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
1087 int ninlined;
1088 int ninlined_callees;
1089 int old_insns = 0;
1090 int i, y;
1092 for (node = cgraph_nodes; node; node = node->next)
1093 initial_insns += node->local.self_insns;
1094 overall_insns = initial_insns;
1096 nnodes = cgraph_postorder (order);
1098 if (cgraph_dump_file)
1099 fprintf (cgraph_dump_file,
1100 "\nDeciding on inlining. Starting with %i insns.\n",
1101 initial_insns);
1103 for (node = cgraph_nodes; node; node = node->next)
1104 node->aux = 0;
1106 if (cgraph_dump_file)
1107 fprintf (cgraph_dump_file, "\nInlining always_inline functions:\n");
1109 /* In the first pass mark all always_inline edges. Do this with a priority
1110 so none of our later choices will make this impossible. */
1111 for (i = nnodes - 1; i >= 0; i--)
1113 struct cgraph_edge *e;
1115 node = order[i];
1117 for (e = node->callees; e; e = e->next_callee)
1118 if (e->callee->local.disregard_inline_limits)
1119 break;
1120 if (!e)
1121 continue;
1122 if (cgraph_dump_file)
1123 fprintf (cgraph_dump_file,
1124 "\nConsidering %s %i insns (always inline)\n",
1125 cgraph_node_name (e->callee), e->callee->global.insns);
1126 ninlined = cgraph_inlined_into (order[i], inlined);
1127 for (; e; e = e->next_callee)
1129 old_insns = overall_insns;
1130 if (e->inline_call || !e->callee->local.disregard_inline_limits)
1131 continue;
1132 if (e->callee->output || e->callee == node)
1133 continue;
1134 ninlined_callees =
1135 cgraph_inlined_callees (e->callee, inlined_callees);
1136 cgraph_mark_inline (node, e->callee, inlined, ninlined,
1137 inlined_callees, ninlined_callees);
1138 for (y = 0; y < ninlined_callees; y++)
1139 inlined_callees[y]->output = 0, node->aux = 0;
1140 if (cgraph_dump_file)
1141 fprintf (cgraph_dump_file,
1142 " Inlined into %s which now has %i insns.\n",
1143 cgraph_node_name (node->callees->caller),
1144 node->callees->caller->global.insns);
1146 if (cgraph_dump_file && node->global.cloned_times > 0)
1147 fprintf (cgraph_dump_file,
1148 " Inlined %i times for a net change of %+i insns.\n",
1149 node->global.cloned_times, overall_insns - old_insns);
1150 for (y = 0; y < ninlined; y++)
1151 inlined[y]->output = 0, node->aux = 0;
1154 cgraph_decide_inlining_of_small_functions (inlined, inlined_callees);
1156 if (cgraph_dump_file)
1157 fprintf (cgraph_dump_file, "\nDeciding on functions called once:\n");
1159 /* And finally decide what functions are called once. */
1161 for (i = nnodes - 1; i >= 0; i--)
1163 node = order[i];
1165 if (node->callers && !node->callers->next_caller && !node->needed
1166 && node->local.inlinable && !node->callers->inline_call
1167 && !DECL_EXTERNAL (node->decl) && !DECL_COMDAT (node->decl))
1169 bool ok = true;
1170 struct cgraph_node *node1;
1172 /* Verify that we won't duplicate the caller. */
1173 for (node1 = node->callers->caller;
1174 node1->callers && node1->callers->inline_call
1175 && ok; node1 = node1->callers->caller)
1176 if (node1->callers->next_caller || node1->needed)
1177 ok = false;
1178 if (ok)
1180 if (cgraph_dump_file)
1181 fprintf (cgraph_dump_file,
1182 "\nConsidering %s %i insns.\n"
1183 " Called once from %s %i insns.\n",
1184 cgraph_node_name (node), node->global.insns,
1185 cgraph_node_name (node->callers->caller),
1186 node->callers->caller->global.insns);
1187 ninlined = cgraph_inlined_into (node->callers->caller, inlined);
1188 old_insns = overall_insns;
1189 if (cgraph_check_inline_limits
1190 (node->callers->caller, node, inlined, ninlined))
1192 ninlined_callees =
1193 cgraph_inlined_callees (node, inlined_callees);
1194 cgraph_mark_inline (node->callers->caller, node, inlined,
1195 ninlined, inlined_callees,
1196 ninlined_callees);
1197 for (y = 0; y < ninlined_callees; y++)
1198 inlined_callees[y]->output = 0, node->aux = 0;
1199 if (cgraph_dump_file)
1200 fprintf (cgraph_dump_file,
1201 " Inlined into %s which now has %i insns"
1202 " for a net change of %+i insns.\n",
1203 cgraph_node_name (node->callers->caller),
1204 node->callers->caller->global.insns,
1205 overall_insns - old_insns);
1207 else
1209 if (cgraph_dump_file)
1210 fprintf (cgraph_dump_file,
1211 " Inline limit reached, not inlined.\n");
1213 for (y = 0; y < ninlined; y++)
1214 inlined[y]->output = 0, node->aux = 0;
1219 if (cgraph_dump_file)
1220 fprintf (cgraph_dump_file,
1221 "\nInlined %i calls, eliminated %i functions, "
1222 "%i insns turned to %i insns.\n\n",
1223 ncalls_inlined, nfunctions_inlined, initial_insns,
1224 overall_insns);
1225 free (order);
1226 free (inlined);
1227 free (inlined_callees);
1230 /* Decide on the inlining. We do so in the topological order to avoid
1231 expenses on updating datastructures. */
1233 static void
1234 cgraph_decide_inlining_incrementally (struct cgraph_node *node)
1236 struct cgraph_edge *e;
1237 struct cgraph_node **inlined =
1238 xmalloc (sizeof (struct cgraph_node *) * cgraph_n_nodes);
1239 struct cgraph_node **inlined_callees =
1240 xmalloc (sizeof (struct cgraph_node *) * cgraph_n_nodes);
1241 int ninlined;
1242 int ninlined_callees;
1243 int y;
1245 ninlined = cgraph_inlined_into (node, inlined);
1247 /* First of all look for always inline functions. */
1248 for (e = node->callees; e; e = e->next_callee)
1249 if (e->callee->local.disregard_inline_limits && !e->callee->output
1250 && e->callee != node && !e->inline_call)
1252 ninlined_callees = cgraph_inlined_callees (e->callee, inlined_callees);
1253 cgraph_mark_inline (node, e->callee, inlined, ninlined,
1254 inlined_callees, ninlined_callees);
1255 for (y = 0; y < ninlined_callees; y++)
1256 inlined_callees[y]->output = 0, node->aux = 0;
1259 /* Now do the automatic inlining. */
1260 for (e = node->callees; e; e = e->next_callee)
1261 if (e->callee->local.inlinable && !e->callee->output
1262 && e->callee != node && !e->inline_call
1263 && cgraph_default_inline_p (e->callee)
1264 && cgraph_check_inline_limits (node, e->callee, inlined,
1265 ninlined))
1267 ninlined_callees = cgraph_inlined_callees (e->callee, inlined_callees);
1268 cgraph_mark_inline (node, e->callee, inlined, ninlined,
1269 inlined_callees, ninlined_callees);
1270 for (y = 0; y < ninlined_callees; y++)
1271 inlined_callees[y]->output = 0, node->aux = 0;
1274 /* Clear the flags set by cgraph_inlined_into. */
1275 for (y = 0; y < ninlined; y++)
1276 inlined[y]->output = 0, node->aux = 0;
1278 free (inlined);
1279 free (inlined_callees);
1283 /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */
1285 bool
1286 cgraph_inline_p (tree caller_decl, tree callee_decl)
1288 struct cgraph_node *caller = cgraph_node (caller_decl);
1289 struct cgraph_node *callee = cgraph_node (callee_decl);
1290 struct cgraph_edge *e;
1292 for (e = caller->callees; e; e = e->next_callee)
1293 if (e->callee == callee)
1294 return e->inline_call;
1295 /* We do not record builtins in the callgraph. Perhaps it would make more
1296 sense to do so and then prune out those not overwritten by explicit
1297 function body. */
1298 return false;
1300 /* Expand all functions that must be output.
1302 Attempt to topologically sort the nodes so function is output when
1303 all called functions are already assembled to allow data to be
1304 propagated across the callgraph. Use a stack to get smaller distance
1305 between a function and it's callees (later we may choose to use a more
1306 sophisticated algorithm for function reordering; we will likely want
1307 to use subsections to make the output functions appear in top-down
1308 order). */
1310 static void
1311 cgraph_expand_all_functions (void)
1313 struct cgraph_node *node;
1314 struct cgraph_node **order =
1315 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
1316 int order_pos = 0;
1317 int i;
1319 cgraph_mark_functions_to_output ();
1321 order_pos = cgraph_postorder (order);
1323 for (i = order_pos - 1; i >= 0; i--)
1325 node = order[i];
1326 if (node->output)
1328 if (!node->reachable)
1329 abort ();
1330 node->output = 0;
1331 cgraph_expand_function (node);
1334 free (order);
1337 /* Mark all local functions.
1339 A local function is one whose calls can occur only in the
1340 current compilation unit, so we change its calling convention.
1341 We simply mark all static functions whose address is not taken
1342 as local. */
1344 static void
1345 cgraph_mark_local_functions (void)
1347 struct cgraph_node *node;
1349 if (cgraph_dump_file)
1350 fprintf (cgraph_dump_file, "\nMarking local functions:");
1352 /* Figure out functions we want to assemble. */
1353 for (node = cgraph_nodes; node; node = node->next)
1355 node->local.local = (!node->needed
1356 && DECL_SAVED_TREE (node->decl)
1357 && !TREE_PUBLIC (node->decl));
1358 if (cgraph_dump_file && node->local.local)
1359 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
1361 if (cgraph_dump_file)
1362 fprintf (cgraph_dump_file, "\n\n");
1365 /* Perform simple optimizations based on callgraph. */
1367 void
1368 cgraph_optimize (void)
1370 if (!flag_unit_at_a_time)
1371 return;
1372 timevar_push (TV_CGRAPHOPT);
1373 if (!quiet_flag)
1374 fprintf (stderr, "Performing intraprocedural optimizations\n");
1376 cgraph_mark_local_functions ();
1377 if (cgraph_dump_file)
1379 fprintf (cgraph_dump_file, "Marked ");
1380 dump_cgraph (cgraph_dump_file);
1383 cgraph_decide_inlining ();
1384 cgraph_global_info_ready = true;
1385 if (cgraph_dump_file)
1387 fprintf (cgraph_dump_file, "Optimized ");
1388 dump_cgraph (cgraph_dump_file);
1390 timevar_pop (TV_CGRAPHOPT);
1392 /* Output everything. */
1393 if (!quiet_flag)
1394 fprintf (stderr, "Assembling functions:\n");
1395 cgraph_expand_all_functions ();
1396 if (cgraph_dump_file)
1398 fprintf (cgraph_dump_file, "\nFinal ");
1399 dump_cgraph (cgraph_dump_file);