2003-09-04 Eric Christopher <echristo@redhat.com>
[official-gcc.git] / gcc / cgraphunit.c
blob7188501c71972eade9e36e6bac72417d4ac9b6cf
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_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);
53 /* Statistics we collect about inlining algorithm. */
54 static int ncalls_inlined;
55 static int nfunctions_inlined;
56 static int initial_insns;
57 static int overall_insns;
59 /* Records tree nodes seen in cgraph_create_edges. Simply using
60 walk_tree_without_duplicates doesn't guarantee each node is visited
61 once because it gets a new htab upon each recursive call from
62 record_calls_1. */
63 static htab_t visited_nodes;
65 /* Analyze function once it is parsed. Set up the local information
66 available - create cgraph edges for function calls via BODY. */
68 void
69 cgraph_finalize_function (tree decl, tree body ATTRIBUTE_UNUSED)
71 struct cgraph_node *node = cgraph_node (decl);
73 node->decl = decl;
74 node->local.finalized = true;
76 /* Function now has DECL_SAVED_TREE set. Enqueue it into cgraph_nodes_queue
77 if needed. */
78 if (node->needed)
79 cgraph_mark_needed_node (node, 0);
80 if (!flag_unit_at_a_time)
81 cgraph_analyze_function (node);
82 if (/* Externally visible functions must be output. The exception are
83 COMDAT functions that must be output only when they are needed.
84 Similarly are handled deferred functions and
85 external functions (GCC extension "extern inline") */
86 (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
87 /* ??? Constructors and destructors not called otherwise can be inlined
88 into single construction/destruction function per section to save some
89 resources. For now just mark it as reachable. */
90 || DECL_STATIC_CONSTRUCTOR (decl)
91 || DECL_STATIC_DESTRUCTOR (decl)
92 /* Function whose name is output to the assembler file must be produced.
93 It is possible to assemble the name later after finalizing the function
94 and the fact is noticed in assemble_name then. */
95 || (DECL_ASSEMBLER_NAME_SET_P (decl)
96 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
97 || lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
98 cgraph_mark_needed_node (node, 1);
99 /* When not doing unit-at-a-time deffer only inline functions. */
100 else if (!flag_unit_at_a_time
101 && !DECL_EXTERNAL (decl)
102 && !node->origin
103 && (!DECL_INLINE (decl)
104 || (!node->local.disregard_inline_limits
105 /* When declared inline, deffer even the uninlinable functions.
106 This allows them to be elliminated when unused. */
107 && !DECL_DECLARED_INLINE_P (decl)
108 && (node->local.inlinable
109 || !cgraph_default_inline_p (node)))))
110 cgraph_mark_needed_node (node, 1);
112 if (!flag_unit_at_a_time)
113 while (cgraph_nodes_queue)
115 struct cgraph_node *n = cgraph_nodes_queue;
116 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
117 if (!n->origin)
118 cgraph_expand_function (n);
121 (*debug_hooks->deferred_inline_function) (decl);
124 /* Walk tree and record all calls. Called via walk_tree. */
125 static tree
126 record_call_1 (tree *tp, int *walk_subtrees, void *data)
128 if (TREE_CODE (*tp) == VAR_DECL && TREE_STATIC (*tp))
129 cgraph_varpool_mark_needed_node (cgraph_varpool_node (*tp));
130 /* Record dereferences to the functions. This makes the functions
131 reachable unconditionally. */
132 else if (TREE_CODE (*tp) == ADDR_EXPR)
134 tree decl = TREE_OPERAND (*tp, 0);
135 if (TREE_CODE (decl) == FUNCTION_DECL)
136 cgraph_mark_needed_node (cgraph_node (decl), 1);
138 else if (TREE_CODE (*tp) == CALL_EXPR)
140 tree decl = get_callee_fndecl (*tp);
141 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
143 if (DECL_BUILT_IN (decl))
144 return NULL;
145 cgraph_record_call (data, decl);
147 /* When we see a function call, we don't want to look at the
148 function reference in the ADDR_EXPR that is hanging from
149 the CALL_EXPR we're examining here, because we would
150 conclude incorrectly that the function's address could be
151 taken by something that is not a function call. So only
152 walk the function parameter list, skip the other subtrees. */
154 walk_tree (&TREE_OPERAND (*tp, 1), record_call_1, data,
155 visited_nodes);
156 *walk_subtrees = 0;
159 /* Save some cycles by not walking types and declaration as we won't find anything
160 usefull there anyway. */
161 if (DECL_P (*tp) || TYPE_P (*tp))
162 *walk_subtrees = 0;
163 return NULL;
166 /* Create cgraph edges for function calls inside BODY from DECL. */
168 void
169 cgraph_create_edges (tree decl, tree body)
171 /* The nodes we're interested in are never shared, so walk
172 the tree ignoring duplicates. */
173 visited_nodes = htab_create (37, htab_hash_pointer,
174 htab_eq_pointer, NULL);
175 walk_tree (&body, record_call_1, decl, visited_nodes);
176 htab_delete (visited_nodes);
177 visited_nodes = NULL;
180 /* Analyze the function scheduled to be output. */
181 static void
182 cgraph_analyze_function (struct cgraph_node *node)
184 tree decl = node->decl;
186 if (lang_hooks.callgraph.lower_function)
187 (*lang_hooks.callgraph.lower_function) (decl);
189 current_function_decl = node->decl;
191 /* First kill forward declaration so reverse inlining works properly. */
192 cgraph_create_edges (decl, DECL_SAVED_TREE (decl));
194 node->local.inlinable = tree_inlinable_function_p (decl);
195 if (!DECL_ESTIMATED_INSNS (decl))
196 DECL_ESTIMATED_INSNS (decl)
197 = (*lang_hooks.tree_inlining.estimate_num_insns) (decl);
198 node->local.self_insns = DECL_ESTIMATED_INSNS (decl);
199 if (node->local.inlinable)
200 node->local.disregard_inline_limits
201 = (*lang_hooks.tree_inlining.disregard_inline_limits) (decl);
203 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
204 node->global.insns = node->local.self_insns;
205 if (!DECL_EXTERNAL (node->decl))
207 node->global.cloned_times = 1;
208 node->global.will_be_output = true;
211 node->lowered = true;
214 /* Analyze the whole compilation unit once it is parsed completely. */
216 void
217 cgraph_finalize_compilation_unit (void)
219 struct cgraph_node *node;
221 if (!flag_unit_at_a_time)
222 return;
224 cgraph_varpool_assemble_pending_decls ();
225 if (!quiet_flag)
226 fprintf (stderr, "\nAnalyzing compilation unit\n");
228 timevar_push (TV_CGRAPH);
229 if (cgraph_dump_file)
231 fprintf (cgraph_dump_file, "\nInitial entry points:");
232 for (node = cgraph_nodes; node; node = node->next)
233 if (node->needed && DECL_SAVED_TREE (node->decl))
234 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
235 fprintf (cgraph_dump_file, "\n");
238 /* Propagate reachability flag and lower representation of all reachable
239 functions. In the future, lowering will introduce new functions and
240 new entry points on the way (by template instantiation and virtual
241 method table generation for instance). */
242 while (cgraph_nodes_queue)
244 struct cgraph_edge *edge;
245 tree decl = cgraph_nodes_queue->decl;
247 node = cgraph_nodes_queue;
248 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
250 if (node->lowered || !node->reachable || !DECL_SAVED_TREE (decl))
251 abort ();
253 cgraph_analyze_function (node);
254 for (edge = node->callees; edge; edge = edge->next_callee)
256 if (!edge->callee->reachable)
257 cgraph_mark_needed_node (edge->callee, 0);
259 cgraph_varpool_assemble_pending_decls ();
261 /* Collect entry points to the unit. */
263 if (cgraph_dump_file)
265 fprintf (cgraph_dump_file, "\nUnit entry points:");
266 for (node = cgraph_nodes; node; node = node->next)
267 if (node->needed && DECL_SAVED_TREE (node->decl))
268 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
269 fprintf (cgraph_dump_file, "\n");
270 dump_cgraph (cgraph_dump_file);
273 if (cgraph_dump_file)
274 fprintf (cgraph_dump_file, "\nReclaiming functions:");
276 for (node = cgraph_nodes; node; node = node->next)
278 tree decl = node->decl;
280 if (!node->reachable && DECL_SAVED_TREE (decl))
282 cgraph_remove_node (node);
283 if (cgraph_dump_file)
284 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
287 if (cgraph_dump_file)
288 fprintf (cgraph_dump_file, "\n");
289 ggc_collect ();
290 timevar_pop (TV_CGRAPH);
293 /* Figure out what functions we want to assemble. */
295 static void
296 cgraph_mark_functions_to_output (void)
298 struct cgraph_node *node;
300 for (node = cgraph_nodes; node; node = node->next)
302 tree decl = node->decl;
303 struct cgraph_edge *e;
304 if (node->output)
305 abort ();
307 for (e = node->callers; e; e = e->next_caller)
308 if (!e->inline_call)
309 break;
311 /* We need to output all local functions that are used and not
312 always inlined, as well as those that are reachable from
313 outside the current compilation unit. */
314 if (DECL_SAVED_TREE (decl)
315 && (node->needed
316 || (e && node->reachable))
317 && !TREE_ASM_WRITTEN (decl) && !node->origin
318 && !DECL_EXTERNAL (decl))
319 node->output = 1;
323 /* Optimize the function before expansion. */
325 static void
326 cgraph_optimize_function (struct cgraph_node *node)
328 tree decl = node->decl;
330 timevar_push (TV_INTEGRATION);
331 /* optimize_inline_calls avoids inlining of current_function_decl. */
332 current_function_decl = decl;
333 if (flag_inline_trees)
334 optimize_inline_calls (decl);
335 if (node->nested)
337 for (node = node->nested; node; node = node->next_nested)
338 cgraph_optimize_function (node);
340 timevar_pop (TV_INTEGRATION);
343 /* Expand function specified by NODE. */
345 static void
346 cgraph_expand_function (struct cgraph_node *node)
348 tree decl = node->decl;
349 struct cgraph_edge *e;
351 announce_function (decl);
353 cgraph_optimize_function (node);
355 /* Generate RTL for the body of DECL. Nested functions are expanded
356 via lang_expand_decl_stmt. */
357 (*lang_hooks.callgraph.expand_function) (decl);
359 if (!flag_unit_at_a_time)
361 if (!node->local.inlinable
362 || (!node->local.disregard_inline_limits
363 && !cgraph_default_inline_p (node)))
364 DECL_SAVED_TREE (node->decl) = NULL;
366 else
368 for (e = node->callers; e; e = e->next_caller)
369 if (e->inline_call)
370 break;
371 if (!e)
372 DECL_SAVED_TREE (decl) = NULL;
374 current_function_decl = NULL;
377 /* Fill array order with all nodes with output flag set in the reverse
378 topological order. */
379 static int
380 cgraph_postorder (struct cgraph_node **order)
382 struct cgraph_node *node, *node2;
383 int stack_size = 0;
384 int order_pos = 0;
385 struct cgraph_edge *edge, last;
387 struct cgraph_node **stack =
388 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
390 /* We have to deal with cycles nicely, so use a depth first traversal
391 output algorithm. Ignore the fact that some functions won't need
392 to be output and put them into order as well, so we get dependencies
393 right through intline functions. */
394 for (node = cgraph_nodes; node; node = node->next)
395 node->aux = NULL;
396 for (node = cgraph_nodes; node; node = node->next)
397 if (!node->aux)
399 node2 = node;
400 if (!node->callers)
401 node->aux = &last;
402 else
403 node->aux = node->callers;
404 while (node2)
406 while (node2->aux != &last)
408 edge = node2->aux;
409 if (edge->next_caller)
410 node2->aux = edge->next_caller;
411 else
412 node2->aux = &last;
413 if (!edge->caller->aux)
415 if (!edge->caller->callers)
416 edge->caller->aux = &last;
417 else
418 edge->caller->aux = edge->caller->callers;
419 stack[stack_size++] = node2;
420 node2 = edge->caller;
421 break;
424 if (node2->aux == &last)
426 order[order_pos++] = node2;
427 if (stack_size)
428 node2 = stack[--stack_size];
429 else
430 node2 = NULL;
434 free (stack);
435 return order_pos;
438 #define INLINED_TIMES(node) ((size_t)(node)->aux)
439 #define SET_INLINED_TIMES(node,times) ((node)->aux = (void *)(times))
441 /* Return list of nodes we decided to inline NODE into, set their output
442 flag and compute INLINED_TIMES.
444 We do simple backtracing to get INLINED_TIMES right. This should not be
445 expensive as we limit the amount of inlining. Alternatively we may first
446 discover set of nodes, topologically sort these and propagate
447 INLINED_TIMES */
449 static int
450 cgraph_inlined_into (struct cgraph_node *node, struct cgraph_node **array)
452 int nfound = 0;
453 struct cgraph_edge **stack;
454 struct cgraph_edge *e, *e1;
455 int sp;
456 int i;
458 /* Fast path: since we traverse in mostly topological order, we will likely
459 find no edges. */
460 for (e = node->callers; e; e = e->next_caller)
461 if (e->inline_call)
462 break;
464 if (!e)
465 return 0;
467 /* Allocate stack for back-tracking up callgraph. */
468 stack = xmalloc ((cgraph_n_nodes + 1) * sizeof (struct cgraph_edge));
469 sp = 0;
471 /* Push the first edge on to the stack. */
472 stack[sp++] = e;
474 while (sp)
476 struct cgraph_node *caller;
478 /* Look at the edge on the top of the stack. */
479 e = stack[sp - 1];
480 caller = e->caller;
482 /* Check if the caller destination has been visited yet. */
483 if (!caller->output)
485 array[nfound++] = e->caller;
486 /* Mark that we have visited the destination. */
487 caller->output = true;
488 SET_INLINED_TIMES (caller, 0);
490 SET_INLINED_TIMES (caller, INLINED_TIMES (caller) + 1);
492 for (e1 = caller->callers; e1; e1 = e1->next_caller)
493 if (e1->inline_call)
494 break;
495 if (e1)
496 stack[sp++] = e1;
497 else
499 while (true)
501 for (e1 = e->next_caller; e1; e1 = e1->next_caller)
502 if (e1->inline_call)
503 break;
505 if (e1)
507 stack[sp - 1] = e1;
508 break;
510 else
512 sp--;
513 if (!sp)
514 break;
515 e = stack[sp - 1];
521 free (stack);
524 if (cgraph_dump_file)
526 fprintf (cgraph_dump_file, "Found inline predecesors of %s:",
527 cgraph_node_name (node));
528 for (i = 0; i < nfound; i++)
530 fprintf (cgraph_dump_file, " %s", cgraph_node_name (array[i]));
531 if (INLINED_TIMES (array[i]) != 1)
532 fprintf (cgraph_dump_file, " (%i times)",
533 (int)INLINED_TIMES (array[i]));
535 fprintf (cgraph_dump_file, "\n");
538 return nfound;
541 /* Return list of nodes we decided to inline into NODE, set their output
542 flag and compute INLINED_TIMES.
544 This function is identical to cgraph_inlined_into with callers and callees
545 nodes swapped. */
547 static int
548 cgraph_inlined_callees (struct cgraph_node *node, struct cgraph_node **array)
550 int nfound = 0;
551 struct cgraph_edge **stack;
552 struct cgraph_edge *e, *e1;
553 int sp;
554 int i;
556 /* Fast path: since we traverse in mostly topological order, we will likely
557 find no edges. */
558 for (e = node->callees; e; e = e->next_callee)
559 if (e->inline_call)
560 break;
562 if (!e)
563 return 0;
565 /* Allocate stack for back-tracking up callgraph. */
566 stack = xmalloc ((cgraph_n_nodes + 1) * sizeof (struct cgraph_edge));
567 sp = 0;
569 /* Push the first edge on to the stack. */
570 stack[sp++] = e;
572 while (sp)
574 struct cgraph_node *callee;
576 /* Look at the edge on the top of the stack. */
577 e = stack[sp - 1];
578 callee = e->callee;
580 /* Check if the callee destination has been visited yet. */
581 if (!callee->output)
583 array[nfound++] = e->callee;
584 /* Mark that we have visited the destination. */
585 callee->output = true;
586 SET_INLINED_TIMES (callee, 0);
588 SET_INLINED_TIMES (callee, INLINED_TIMES (callee) + 1);
590 for (e1 = callee->callees; e1; e1 = e1->next_callee)
591 if (e1->inline_call)
592 break;
593 if (e1)
594 stack[sp++] = e1;
595 else
597 while (true)
599 for (e1 = e->next_callee; e1; e1 = e1->next_callee)
600 if (e1->inline_call)
601 break;
603 if (e1)
605 stack[sp - 1] = e1;
606 break;
608 else
610 sp--;
611 if (!sp)
612 break;
613 e = stack[sp - 1];
619 free (stack);
621 if (cgraph_dump_file)
623 fprintf (cgraph_dump_file, "Found inline successors of %s:",
624 cgraph_node_name (node));
625 for (i = 0; i < nfound; i++)
627 fprintf (cgraph_dump_file, " %s", cgraph_node_name (array[i]));
628 if (INLINED_TIMES (array[i]) != 1)
629 fprintf (cgraph_dump_file, " (%i times)",
630 (int)INLINED_TIMES (array[i]));
632 fprintf (cgraph_dump_file, "\n");
635 return nfound;
638 /* Estimate size of the function after inlining WHAT into TO. */
640 static int
641 cgraph_estimate_size_after_inlining (int times, struct cgraph_node *to,
642 struct cgraph_node *what)
644 return (what->global.insns - INSNS_PER_CALL) *times + to->global.insns;
647 /* Estimate the growth caused by inlining NODE into all callees. */
649 static int
650 cgraph_estimate_growth (struct cgraph_node *node)
652 int growth = 0;
653 int calls_saved = 0;
654 int clones_added = 0;
655 struct cgraph_edge *e;
657 for (e = node->callers; e; e = e->next_caller)
658 if (!e->inline_call)
660 growth += ((cgraph_estimate_size_after_inlining (1, e->caller, node)
662 e->caller->global.insns) *e->caller->global.cloned_times);
663 calls_saved += e->caller->global.cloned_times;
664 clones_added += e->caller->global.cloned_times;
667 /* ??? Wrong for self recursive functions or cases where we decide to not
668 inline for different reasons, but it is not big deal as in that case
669 we will keep the body around, but we will also avoid some inlining. */
670 if (!node->needed && !node->origin && !DECL_EXTERNAL (node->decl))
671 growth -= node->global.insns, clones_added--;
673 if (!calls_saved)
674 calls_saved = 1;
676 return growth;
679 /* Update insn sizes after inlining WHAT into TO that is already inlined into
680 all nodes in INLINED array. */
682 static void
683 cgraph_mark_inline (struct cgraph_node *to, struct cgraph_node *what,
684 struct cgraph_node **inlined, int ninlined,
685 struct cgraph_node **inlined_callees,
686 int ninlined_callees)
688 int i;
689 int times = 0;
690 int clones = 0;
691 struct cgraph_edge *e;
692 bool called = false;
693 int new_insns;
695 for (e = what->callers; e; e = e->next_caller)
697 if (e->caller == to)
699 if (e->inline_call)
700 abort ();
701 e->inline_call = true;
702 times++;
703 clones += e->caller->global.cloned_times;
705 else if (!e->inline_call)
706 called = true;
708 if (!times)
709 abort ();
710 ncalls_inlined += times;
712 new_insns = cgraph_estimate_size_after_inlining (times, to, what);
713 if (to->global.will_be_output)
714 overall_insns += new_insns - to->global.insns;
715 to->global.insns = new_insns;
717 if (!called && !what->needed && !what->origin
718 && !DECL_EXTERNAL (what->decl))
720 if (!what->global.will_be_output)
721 abort ();
722 clones--;
723 nfunctions_inlined++;
724 what->global.will_be_output = 0;
725 overall_insns -= what->global.insns;
727 what->global.cloned_times += clones;
728 for (i = 0; i < ninlined; i++)
730 new_insns =
731 cgraph_estimate_size_after_inlining (INLINED_TIMES (inlined[i]) *
732 times, inlined[i], what);
733 if (inlined[i]->global.will_be_output)
734 overall_insns += new_insns - inlined[i]->global.insns;
735 inlined[i]->global.insns = new_insns;
737 for (i = 0; i < ninlined_callees; i++)
739 inlined_callees[i]->global.cloned_times +=
740 INLINED_TIMES (inlined_callees[i]) * clones;
744 /* Return false when inlining WHAT into TO is not good idea as it would cause
745 too large growth of function bodies. */
747 static bool
748 cgraph_check_inline_limits (struct cgraph_node *to, struct cgraph_node *what,
749 struct cgraph_node **inlined, int ninlined)
751 int i;
752 int times = 0;
753 struct cgraph_edge *e;
754 int newsize;
755 int limit;
757 for (e = to->callees; e; e = e->next_callee)
758 if (e->callee == what)
759 times++;
761 /* When inlining large function body called once into small function,
762 take the inlined function as base for limiting the growth. */
763 if (to->local.self_insns > what->local.self_insns)
764 limit = to->local.self_insns;
765 else
766 limit = what->local.self_insns;
768 limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
770 newsize = cgraph_estimate_size_after_inlining (times, to, what);
771 if (newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
772 && newsize > limit)
773 return false;
774 for (i = 0; i < ninlined; i++)
776 newsize =
777 cgraph_estimate_size_after_inlining (INLINED_TIMES (inlined[i]) *
778 times, inlined[i], what);
779 if (newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
780 && newsize >
781 inlined[i]->local.self_insns *
782 (100 + PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH)) / 100)
783 return false;
785 return true;
788 /* Return true when function N is small enought to be inlined. */
790 static bool
791 cgraph_default_inline_p (struct cgraph_node *n)
793 if (!DECL_INLINE (n->decl) || !DECL_SAVED_TREE (n->decl))
794 return false;
795 if (DECL_DECLARED_INLINE_P (n->decl))
796 return n->global.insns < MAX_INLINE_INSNS_SINGLE;
797 else
798 return n->global.insns < MAX_INLINE_INSNS_AUTO;
801 /* We use greedy algorithm for inlining of small functions:
802 All inline candidates are put into prioritized heap based on estimated
803 growth of the overall number of instructions and then update the estimates.
805 INLINED and INLINED_CALEES are just pointers to arrays large enought
806 to be passed to cgraph_inlined_into and cgraph_inlined_callees. */
808 static void
809 cgraph_decide_inlining_of_small_functions (struct cgraph_node **inlined,
810 struct cgraph_node **inlined_callees)
812 int i;
813 struct cgraph_node *node;
814 fibheap_t heap = fibheap_new ();
815 struct fibnode **heap_node =
816 xcalloc (cgraph_max_uid, sizeof (struct fibnode *));
817 int ninlined, ninlined_callees;
818 int max_insns = ((HOST_WIDEST_INT) initial_insns
819 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
821 /* Put all inline candidates into the heap. */
823 for (node = cgraph_nodes; node; node = node->next)
825 struct cgraph_edge *e;
827 if (!node->local.inlinable || !node->callers
828 || !cgraph_default_inline_p (node))
829 continue;
831 /* Rule out always_inline functions we dealt with earlier. */
832 for (e = node->callers; e; e = e->next_caller)
833 if (e->inline_call)
834 break;
835 if (e)
836 continue;
837 heap_node[node->uid] =
838 fibheap_insert (heap, cgraph_estimate_growth (node), node);
841 if (cgraph_dump_file)
842 fprintf (cgraph_dump_file, "\n\nDeciding on inlining: ");
843 while ((node = fibheap_extract_min (heap)) && overall_insns <= max_insns)
845 struct cgraph_edge *e;
846 int old_insns = overall_insns;
848 heap_node[node->uid] = NULL;
849 if (cgraph_dump_file)
850 fprintf (cgraph_dump_file, "Considering %s %i insns, growth %i.\n",
851 cgraph_node_name (node), node->global.insns,
852 cgraph_estimate_growth (node));
853 if (!cgraph_default_inline_p (node))
855 if (cgraph_dump_file)
856 fprintf (cgraph_dump_file, "Function too large.\n");
857 continue;
859 ninlined_callees = cgraph_inlined_callees (node, inlined_callees);
860 for (e = node->callers; e; e = e->next_caller)
861 if (!e->inline_call && e->caller != node)
863 ninlined = cgraph_inlined_into (e->caller, inlined);
864 if (e->callee->output
865 || !cgraph_check_inline_limits (e->caller, node, inlined,
866 ninlined))
868 for (i = 0; i < ninlined; i++)
869 inlined[i]->output = 0, node->aux = 0;
870 if (cgraph_dump_file)
871 fprintf (cgraph_dump_file, "Not inlining into %s\n",
872 cgraph_node_name (e->caller));
873 continue;
875 cgraph_mark_inline (e->caller, node, inlined, ninlined,
876 inlined_callees, ninlined_callees);
877 if (heap_node[e->caller->uid])
878 fibheap_replace_key (heap, heap_node[e->caller->uid],
879 cgraph_estimate_growth (e->caller));
881 /* Size of the functions we updated into has changed, so update
882 the keys. */
883 for (i = 0; i < ninlined; i++)
885 inlined[i]->output = 0, node->aux = 0;
886 if (heap_node[inlined[i]->uid])
887 fibheap_replace_key (heap, heap_node[inlined[i]->uid],
888 cgraph_estimate_growth (inlined[i]));
892 /* Similarly all functions called by function we just inlined
893 are now called more times; update keys. */
895 for (e = node->callees; e; e = e->next_callee)
896 if (!e->inline_call && heap_node[e->callee->uid])
897 fibheap_replace_key (heap, heap_node[e->callee->uid],
898 cgraph_estimate_growth (e->callee));
900 for (i = 0; i < ninlined_callees; i++)
902 struct cgraph_edge *e;
904 for (e = inlined_callees[i]->callees; e; e = e->next_callee)
905 if (!e->inline_call && heap_node[e->callee->uid])
906 fibheap_replace_key (heap, heap_node[e->callee->uid],
907 cgraph_estimate_growth (e->callee));
909 inlined_callees[i]->output = 0, node->aux = 0;
911 if (cgraph_dump_file)
912 fprintf (cgraph_dump_file,
913 "Created %i clones, Num insns:%i (%+i), %.2f%%.\n\n",
914 node->global.cloned_times - 1,
915 overall_insns, overall_insns - old_insns,
916 overall_insns * 100.0 / initial_insns);
918 if (cgraph_dump_file && !fibheap_empty (heap))
919 fprintf (cgraph_dump_file, "inline-unit-growth limit reached.\n");
920 fibheap_delete (heap);
921 free (heap_node);
924 /* Decide on the inlining. We do so in the topological order to avoid
925 expenses on updating datastructures. */
927 static void
928 cgraph_decide_inlining (void)
930 struct cgraph_node *node;
931 int nnodes;
932 struct cgraph_node **order =
933 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
934 struct cgraph_node **inlined =
935 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
936 struct cgraph_node **inlined_callees =
937 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
938 int ninlined;
939 int ninlined_callees;
940 int i, y;
942 for (node = cgraph_nodes; node; node = node->next)
943 initial_insns += node->local.self_insns;
944 overall_insns = initial_insns;
946 nnodes = cgraph_postorder (order);
948 for (node = cgraph_nodes; node; node = node->next)
949 node->aux = 0;
951 if (cgraph_dump_file)
952 fprintf (cgraph_dump_file, "\n\nDeciding on always_inline functions:\n");
954 /* In the first pass mark all always_inline edges. Do this with a priority
955 so no our decisions makes this impossible. */
956 for (i = nnodes - 1; i >= 0; i--)
958 struct cgraph_edge *e;
960 node = order[i];
962 for (e = node->callees; e; e = e->next_callee)
963 if (e->callee->local.disregard_inline_limits)
964 break;
965 if (!e)
966 continue;
967 if (cgraph_dump_file)
968 fprintf (cgraph_dump_file,
969 "Considering %s %i insns (always inline)\n",
970 cgraph_node_name (node), node->global.insns);
971 ninlined = cgraph_inlined_into (order[i], inlined);
972 for (; e; e = e->next_callee)
974 if (e->inline_call || !e->callee->local.disregard_inline_limits)
975 continue;
976 if (e->callee->output || e->callee == node)
977 continue;
978 ninlined_callees =
979 cgraph_inlined_callees (e->callee, inlined_callees);
980 cgraph_mark_inline (node, e->callee, inlined, ninlined,
981 inlined_callees, ninlined_callees);
982 for (y = 0; y < ninlined_callees; y++)
983 inlined_callees[y]->output = 0, node->aux = 0;
984 if (cgraph_dump_file)
985 fprintf (cgraph_dump_file, "Inlined %i times. Now %i insns\n\n",
986 node->global.cloned_times, overall_insns);
988 for (y = 0; y < ninlined; y++)
989 inlined[y]->output = 0, node->aux = 0;
992 cgraph_decide_inlining_of_small_functions (inlined, inlined_callees);
994 if (cgraph_dump_file)
995 fprintf (cgraph_dump_file, "\n\nFunctions to inline once:\n");
997 /* And finally decide what functions are called once. */
999 for (i = nnodes - 1; i >= 0; i--)
1001 node = order[i];
1003 if (node->callers && !node->callers->next_caller && !node->needed
1004 && node->local.inlinable && !node->callers->inline_call
1005 && !DECL_EXTERNAL (node->decl) && !DECL_COMDAT (node->decl))
1007 bool ok = true;
1008 struct cgraph_node *node1;
1010 /* Verify that we won't duplicate the caller. */
1011 for (node1 = node->callers->caller;
1012 node1->callers && node1->callers->inline_call
1013 && ok; node1 = node1->callers->caller)
1014 if (node1->callers->next_caller || node1->needed)
1015 ok = false;
1016 if (ok)
1018 if (cgraph_dump_file)
1019 fprintf (cgraph_dump_file,
1020 "Considering %s %i insns (called once)\n",
1021 cgraph_node_name (node), node->global.insns);
1022 ninlined = cgraph_inlined_into (node->callers->caller, inlined);
1023 if (cgraph_check_inline_limits
1024 (node->callers->caller, node, inlined, ninlined))
1026 ninlined_callees =
1027 cgraph_inlined_callees (node, inlined_callees);
1028 cgraph_mark_inline (node->callers->caller, node, inlined,
1029 ninlined, inlined_callees,
1030 ninlined_callees);
1031 for (y = 0; y < ninlined_callees; y++)
1032 inlined_callees[y]->output = 0, node->aux = 0;
1033 if (cgraph_dump_file)
1034 fprintf (cgraph_dump_file, "Inlined. Now %i insns\n\n", overall_insns);
1036 for (y = 0; y < ninlined; y++)
1037 inlined[y]->output = 0, node->aux = 0;
1042 if (cgraph_dump_file)
1043 fprintf (cgraph_dump_file,
1044 "\nInlined %i calls, elliminated %i functions, %i insns turned to %i insns.\n",
1045 ncalls_inlined, nfunctions_inlined, initial_insns,
1046 overall_insns);
1047 free (order);
1048 free (inlined);
1049 free (inlined_callees);
1052 /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */
1054 bool
1055 cgraph_inline_p (tree caller_decl, tree callee_decl)
1057 struct cgraph_node *caller = cgraph_node (caller_decl);
1058 struct cgraph_node *callee = cgraph_node (callee_decl);
1059 struct cgraph_edge *e;
1061 for (e = caller->callees; e; e = e->next_callee)
1062 if (e->callee == callee)
1063 return e->inline_call;
1064 /* We do not record builtins in the callgraph. Perhaps it would make more
1065 sense to do so and then prune out those not overwritten by explicit
1066 function body. */
1067 return false;
1069 /* Expand all functions that must be output.
1071 Attempt to topologically sort the nodes so function is output when
1072 all called functions are already assembled to allow data to be
1073 propagated across the callgraph. Use a stack to get smaller distance
1074 between a function and it's callees (later we may choose to use a more
1075 sophisticated algorithm for function reordering; we will likely want
1076 to use subsections to make the output functions appear in top-down
1077 order). */
1079 static void
1080 cgraph_expand_functions (void)
1082 struct cgraph_node *node;
1083 struct cgraph_node **order =
1084 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
1085 int order_pos = 0;
1086 int i;
1088 cgraph_mark_functions_to_output ();
1090 order_pos = cgraph_postorder (order);
1092 for (i = order_pos - 1; i >= 0; i--)
1094 node = order[i];
1095 if (node->output)
1097 if (!node->reachable)
1098 abort ();
1099 node->output = 0;
1100 cgraph_expand_function (node);
1103 free (order);
1106 /* Mark all local functions.
1108 A local function is one whose calls can occur only in the
1109 current compilation unit, so we change its calling convention.
1110 We simply mark all static functions whose address is not taken
1111 as local. */
1113 static void
1114 cgraph_mark_local_functions (void)
1116 struct cgraph_node *node;
1118 if (cgraph_dump_file)
1119 fprintf (cgraph_dump_file, "Marking local functions:");
1121 /* Figure out functions we want to assemble. */
1122 for (node = cgraph_nodes; node; node = node->next)
1124 node->local.local = (!node->needed
1125 && DECL_SAVED_TREE (node->decl)
1126 && !TREE_PUBLIC (node->decl));
1127 if (cgraph_dump_file && node->local.local)
1128 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
1130 if (cgraph_dump_file)
1131 fprintf (cgraph_dump_file, "\n");
1134 /* Perform simple optimizations based on callgraph. */
1136 void
1137 cgraph_optimize (void)
1139 if (!flag_unit_at_a_time)
1140 return;
1141 timevar_push (TV_CGRAPHOPT);
1142 if (!quiet_flag)
1143 fprintf (stderr, "Performing intraprocedural optimizations\n");
1144 if (cgraph_dump_file)
1146 fprintf (cgraph_dump_file, "Initial callgraph:");
1147 dump_cgraph (cgraph_dump_file);
1149 cgraph_mark_local_functions ();
1151 cgraph_decide_inlining ();
1153 cgraph_global_info_ready = true;
1154 if (cgraph_dump_file)
1156 fprintf (cgraph_dump_file, "Optimized callgraph:");
1157 dump_cgraph (cgraph_dump_file);
1159 timevar_pop (TV_CGRAPHOPT);
1160 if (!quiet_flag)
1161 fprintf (stderr, "Assembling functions:");
1163 /* Output everything. */
1164 cgraph_expand_functions ();
1165 if (cgraph_dump_file)
1167 fprintf (cgraph_dump_file, "Final callgraph:");
1168 dump_cgraph (cgraph_dump_file);