* diagnostic.c (announce_function): Move to toplev.c.
[official-gcc.git] / gcc / cgraphunit.c
blob1476c8b3b23ca04649f59e15d34bd0e93ce79140
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 /* Determine if function DECL is needed. That is, visible to something
66 either outside this translation unit, something magic in the system
67 configury, or (if not doing unit-at-a-time) to something we havn't
68 seen yet. */
70 static bool
71 decide_is_function_needed (struct cgraph_node *node, tree decl)
73 /* If we decided it was needed before, but at the time we didn't have
74 the body of the function available, then it's still needed. We have
75 to go back and re-check its dependencies now. */
76 if (node->needed)
77 return true;
79 /* Externally visible functions must be output. The exception is
80 COMDAT functions that must be output only when they are needed. */
81 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
82 return true;
84 /* Constructors and destructors are reachable from the runtime by
85 some mechanism. */
86 if (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl))
87 return true;
89 /* If the user told us it is used, then it must be so. */
90 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
91 return true;
93 /* ??? If the assembler name is set by hand, it is possible to assemble
94 the name later after finalizing the function and the fact is noticed
95 in assemble_name then. This is arguably a bug. */
96 if (DECL_ASSEMBLER_NAME_SET_P (decl)
97 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
98 return true;
100 if (flag_unit_at_a_time)
101 return false;
103 /* If not doing unit at a time, then we'll only defer this function
104 if its marked for inlining. Otherwise we want to emit it now. */
106 /* "extern inline" functions are never output locally. */
107 if (DECL_EXTERNAL (decl))
108 return false;
109 /* ??? */
110 if (node->origin)
111 return false;
112 if (!DECL_INLINE (decl)
113 || (!node->local.disregard_inline_limits
114 /* When declared inline, defer even the uninlinable functions.
115 This allows them to be elliminated when unused. */
116 && !DECL_DECLARED_INLINE_P (decl)
117 && (node->local.inlinable || !cgraph_default_inline_p (node))))
118 return true;
120 return false;
123 /* Analyze function once it is parsed. Set up the local information
124 available - create cgraph edges for function calls via BODY. */
126 void
127 cgraph_finalize_function (tree decl, tree body ATTRIBUTE_UNUSED)
129 struct cgraph_node *node = cgraph_node (decl);
131 node->decl = decl;
132 node->local.finalized = true;
134 /* If not unit at a time, then we need to create the call graph
135 now, so that called functions can be queued and emitted now. */
136 if (!flag_unit_at_a_time)
137 cgraph_analyze_function (node);
139 if (decide_is_function_needed (node, decl))
140 cgraph_mark_needed_node (node);
142 /* If not unit at a time, go ahead and emit everything we've
143 found to be reachable at this time. */
144 if (!flag_unit_at_a_time)
145 while (cgraph_nodes_queue)
147 struct cgraph_node *n = cgraph_nodes_queue;
148 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
149 if (!n->origin)
150 cgraph_expand_function (n);
153 /* If we've not yet emitted decl, tell the debug info about it. */
154 if (flag_unit_at_a_time || !node->reachable)
155 (*debug_hooks->deferred_inline_function) (decl);
158 /* Walk tree and record all calls. Called via walk_tree. */
159 static tree
160 record_call_1 (tree *tp, int *walk_subtrees, void *data)
162 if (TREE_CODE (*tp) == VAR_DECL && TREE_STATIC (*tp))
163 cgraph_varpool_mark_needed_node (cgraph_varpool_node (*tp));
164 /* Record dereferences to the functions. This makes the functions
165 reachable unconditionally. */
166 else if (TREE_CODE (*tp) == ADDR_EXPR)
168 tree decl = TREE_OPERAND (*tp, 0);
169 if (TREE_CODE (decl) == FUNCTION_DECL)
170 cgraph_mark_needed_node (cgraph_node (decl));
172 else if (TREE_CODE (*tp) == CALL_EXPR)
174 tree decl = get_callee_fndecl (*tp);
175 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
177 if (DECL_BUILT_IN (decl))
178 return NULL;
179 cgraph_record_call (data, decl);
181 /* When we see a function call, we don't want to look at the
182 function reference in the ADDR_EXPR that is hanging from
183 the CALL_EXPR we're examining here, because we would
184 conclude incorrectly that the function's address could be
185 taken by something that is not a function call. So only
186 walk the function parameter list, skip the other subtrees. */
188 walk_tree (&TREE_OPERAND (*tp, 1), record_call_1, data,
189 visited_nodes);
190 *walk_subtrees = 0;
193 /* Save some cycles by not walking types and declaration as we won't find anything
194 usefull there anyway. */
195 if (DECL_P (*tp) || TYPE_P (*tp))
196 *walk_subtrees = 0;
197 return NULL;
200 /* Create cgraph edges for function calls inside BODY from DECL. */
202 void
203 cgraph_create_edges (tree decl, tree body)
205 /* The nodes we're interested in are never shared, so walk
206 the tree ignoring duplicates. */
207 visited_nodes = htab_create (37, htab_hash_pointer,
208 htab_eq_pointer, NULL);
209 walk_tree (&body, record_call_1, decl, visited_nodes);
210 htab_delete (visited_nodes);
211 visited_nodes = NULL;
214 /* Analyze the function scheduled to be output. */
215 static void
216 cgraph_analyze_function (struct cgraph_node *node)
218 tree decl = node->decl;
220 if (lang_hooks.callgraph.lower_function)
221 (*lang_hooks.callgraph.lower_function) (decl);
223 current_function_decl = node->decl;
225 /* First kill forward declaration so reverse inlining works properly. */
226 cgraph_create_edges (decl, DECL_SAVED_TREE (decl));
228 node->local.inlinable = tree_inlinable_function_p (decl);
229 if (!DECL_ESTIMATED_INSNS (decl))
230 DECL_ESTIMATED_INSNS (decl)
231 = (*lang_hooks.tree_inlining.estimate_num_insns) (decl);
232 node->local.self_insns = DECL_ESTIMATED_INSNS (decl);
233 if (node->local.inlinable)
234 node->local.disregard_inline_limits
235 = (*lang_hooks.tree_inlining.disregard_inline_limits) (decl);
237 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
238 node->global.insns = node->local.self_insns;
239 if (!DECL_EXTERNAL (node->decl))
241 node->global.cloned_times = 1;
242 node->global.will_be_output = true;
245 node->lowered = true;
248 /* Analyze the whole compilation unit once it is parsed completely. */
250 void
251 cgraph_finalize_compilation_unit (void)
253 struct cgraph_node *node;
255 if (!flag_unit_at_a_time)
256 return;
258 cgraph_varpool_assemble_pending_decls ();
259 if (!quiet_flag)
260 fprintf (stderr, "\nAnalyzing compilation unit\n");
262 timevar_push (TV_CGRAPH);
263 if (cgraph_dump_file)
265 fprintf (cgraph_dump_file, "\nInitial 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");
272 /* Propagate reachability flag and lower representation of all reachable
273 functions. In the future, lowering will introduce new functions and
274 new entry points on the way (by template instantiation and virtual
275 method table generation for instance). */
276 while (cgraph_nodes_queue)
278 struct cgraph_edge *edge;
279 tree decl = cgraph_nodes_queue->decl;
281 node = cgraph_nodes_queue;
282 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
284 if (node->lowered || !node->reachable || !DECL_SAVED_TREE (decl))
285 abort ();
287 cgraph_analyze_function (node);
289 for (edge = node->callees; edge; edge = edge->next_callee)
290 if (!edge->callee->reachable)
291 cgraph_mark_reachable_node (edge->callee);
293 cgraph_varpool_assemble_pending_decls ();
296 /* Collect entry points to the unit. */
298 if (cgraph_dump_file)
300 fprintf (cgraph_dump_file, "\nUnit entry points:");
301 for (node = cgraph_nodes; node; node = node->next)
302 if (node->needed && DECL_SAVED_TREE (node->decl))
303 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
304 fprintf (cgraph_dump_file, "\n");
305 dump_cgraph (cgraph_dump_file);
308 if (cgraph_dump_file)
309 fprintf (cgraph_dump_file, "\nReclaiming functions:");
311 for (node = cgraph_nodes; node; node = node->next)
313 tree decl = node->decl;
315 if (!node->reachable && DECL_SAVED_TREE (decl))
317 cgraph_remove_node (node);
318 if (cgraph_dump_file)
319 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
322 if (cgraph_dump_file)
323 fprintf (cgraph_dump_file, "\n");
324 ggc_collect ();
325 timevar_pop (TV_CGRAPH);
328 /* Figure out what functions we want to assemble. */
330 static void
331 cgraph_mark_functions_to_output (void)
333 struct cgraph_node *node;
335 for (node = cgraph_nodes; node; node = node->next)
337 tree decl = node->decl;
338 struct cgraph_edge *e;
339 if (node->output)
340 abort ();
342 for (e = node->callers; e; e = e->next_caller)
343 if (!e->inline_call)
344 break;
346 /* We need to output all local functions that are used and not
347 always inlined, as well as those that are reachable from
348 outside the current compilation unit. */
349 if (DECL_SAVED_TREE (decl)
350 && (node->needed
351 || (e && node->reachable))
352 && !TREE_ASM_WRITTEN (decl) && !node->origin
353 && !DECL_EXTERNAL (decl))
354 node->output = 1;
358 /* Optimize the function before expansion. */
360 static void
361 cgraph_optimize_function (struct cgraph_node *node)
363 tree decl = node->decl;
365 timevar_push (TV_INTEGRATION);
366 /* optimize_inline_calls avoids inlining of current_function_decl. */
367 current_function_decl = decl;
368 if (flag_inline_trees)
369 optimize_inline_calls (decl);
370 if (node->nested)
372 for (node = node->nested; node; node = node->next_nested)
373 cgraph_optimize_function (node);
375 timevar_pop (TV_INTEGRATION);
378 /* Expand function specified by NODE. */
380 static void
381 cgraph_expand_function (struct cgraph_node *node)
383 tree decl = node->decl;
384 struct cgraph_edge *e;
386 announce_function (decl);
388 cgraph_optimize_function (node);
390 /* Generate RTL for the body of DECL. Nested functions are expanded
391 via lang_expand_decl_stmt. */
392 (*lang_hooks.callgraph.expand_function) (decl);
394 if (!flag_unit_at_a_time)
396 if (!node->local.inlinable
397 || (!node->local.disregard_inline_limits
398 && !cgraph_default_inline_p (node)))
399 DECL_SAVED_TREE (node->decl) = NULL;
401 else
403 for (e = node->callers; e; e = e->next_caller)
404 if (e->inline_call)
405 break;
406 if (!e)
407 DECL_SAVED_TREE (decl) = NULL;
409 current_function_decl = NULL;
412 /* Fill array order with all nodes with output flag set in the reverse
413 topological order. */
414 static int
415 cgraph_postorder (struct cgraph_node **order)
417 struct cgraph_node *node, *node2;
418 int stack_size = 0;
419 int order_pos = 0;
420 struct cgraph_edge *edge, last;
422 struct cgraph_node **stack =
423 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
425 /* We have to deal with cycles nicely, so use a depth first traversal
426 output algorithm. Ignore the fact that some functions won't need
427 to be output and put them into order as well, so we get dependencies
428 right through intline functions. */
429 for (node = cgraph_nodes; node; node = node->next)
430 node->aux = NULL;
431 for (node = cgraph_nodes; node; node = node->next)
432 if (!node->aux)
434 node2 = node;
435 if (!node->callers)
436 node->aux = &last;
437 else
438 node->aux = node->callers;
439 while (node2)
441 while (node2->aux != &last)
443 edge = node2->aux;
444 if (edge->next_caller)
445 node2->aux = edge->next_caller;
446 else
447 node2->aux = &last;
448 if (!edge->caller->aux)
450 if (!edge->caller->callers)
451 edge->caller->aux = &last;
452 else
453 edge->caller->aux = edge->caller->callers;
454 stack[stack_size++] = node2;
455 node2 = edge->caller;
456 break;
459 if (node2->aux == &last)
461 order[order_pos++] = node2;
462 if (stack_size)
463 node2 = stack[--stack_size];
464 else
465 node2 = NULL;
469 free (stack);
470 return order_pos;
473 #define INLINED_TIMES(node) ((size_t)(node)->aux)
474 #define SET_INLINED_TIMES(node,times) ((node)->aux = (void *)(times))
476 /* Return list of nodes we decided to inline NODE into, set their output
477 flag and compute INLINED_TIMES.
479 We do simple backtracing to get INLINED_TIMES right. This should not be
480 expensive as we limit the amount of inlining. Alternatively we may first
481 discover set of nodes, topologically sort these and propagate
482 INLINED_TIMES */
484 static int
485 cgraph_inlined_into (struct cgraph_node *node, struct cgraph_node **array)
487 int nfound = 0;
488 struct cgraph_edge **stack;
489 struct cgraph_edge *e, *e1;
490 int sp;
491 int i;
493 /* Fast path: since we traverse in mostly topological order, we will likely
494 find no edges. */
495 for (e = node->callers; e; e = e->next_caller)
496 if (e->inline_call)
497 break;
499 if (!e)
500 return 0;
502 /* Allocate stack for back-tracking up callgraph. */
503 stack = xmalloc ((cgraph_n_nodes + 1) * sizeof (struct cgraph_edge));
504 sp = 0;
506 /* Push the first edge on to the stack. */
507 stack[sp++] = e;
509 while (sp)
511 struct cgraph_node *caller;
513 /* Look at the edge on the top of the stack. */
514 e = stack[sp - 1];
515 caller = e->caller;
517 /* Check if the caller destination has been visited yet. */
518 if (!caller->output)
520 array[nfound++] = e->caller;
521 /* Mark that we have visited the destination. */
522 caller->output = true;
523 SET_INLINED_TIMES (caller, 0);
525 SET_INLINED_TIMES (caller, INLINED_TIMES (caller) + 1);
527 for (e1 = caller->callers; e1; e1 = e1->next_caller)
528 if (e1->inline_call)
529 break;
530 if (e1)
531 stack[sp++] = e1;
532 else
534 while (true)
536 for (e1 = e->next_caller; e1; e1 = e1->next_caller)
537 if (e1->inline_call)
538 break;
540 if (e1)
542 stack[sp - 1] = e1;
543 break;
545 else
547 sp--;
548 if (!sp)
549 break;
550 e = stack[sp - 1];
556 free (stack);
559 if (cgraph_dump_file)
561 fprintf (cgraph_dump_file, "Found inline predecesors of %s:",
562 cgraph_node_name (node));
563 for (i = 0; i < nfound; i++)
565 fprintf (cgraph_dump_file, " %s", cgraph_node_name (array[i]));
566 if (INLINED_TIMES (array[i]) != 1)
567 fprintf (cgraph_dump_file, " (%i times)",
568 (int)INLINED_TIMES (array[i]));
570 fprintf (cgraph_dump_file, "\n");
573 return nfound;
576 /* Return list of nodes we decided to inline into NODE, set their output
577 flag and compute INLINED_TIMES.
579 This function is identical to cgraph_inlined_into with callers and callees
580 nodes swapped. */
582 static int
583 cgraph_inlined_callees (struct cgraph_node *node, struct cgraph_node **array)
585 int nfound = 0;
586 struct cgraph_edge **stack;
587 struct cgraph_edge *e, *e1;
588 int sp;
589 int i;
591 /* Fast path: since we traverse in mostly topological order, we will likely
592 find no edges. */
593 for (e = node->callees; e; e = e->next_callee)
594 if (e->inline_call)
595 break;
597 if (!e)
598 return 0;
600 /* Allocate stack for back-tracking up callgraph. */
601 stack = xmalloc ((cgraph_n_nodes + 1) * sizeof (struct cgraph_edge));
602 sp = 0;
604 /* Push the first edge on to the stack. */
605 stack[sp++] = e;
607 while (sp)
609 struct cgraph_node *callee;
611 /* Look at the edge on the top of the stack. */
612 e = stack[sp - 1];
613 callee = e->callee;
615 /* Check if the callee destination has been visited yet. */
616 if (!callee->output)
618 array[nfound++] = e->callee;
619 /* Mark that we have visited the destination. */
620 callee->output = true;
621 SET_INLINED_TIMES (callee, 0);
623 SET_INLINED_TIMES (callee, INLINED_TIMES (callee) + 1);
625 for (e1 = callee->callees; e1; e1 = e1->next_callee)
626 if (e1->inline_call)
627 break;
628 if (e1)
629 stack[sp++] = e1;
630 else
632 while (true)
634 for (e1 = e->next_callee; e1; e1 = e1->next_callee)
635 if (e1->inline_call)
636 break;
638 if (e1)
640 stack[sp - 1] = e1;
641 break;
643 else
645 sp--;
646 if (!sp)
647 break;
648 e = stack[sp - 1];
654 free (stack);
656 if (cgraph_dump_file)
658 fprintf (cgraph_dump_file, "Found inline successors of %s:",
659 cgraph_node_name (node));
660 for (i = 0; i < nfound; i++)
662 fprintf (cgraph_dump_file, " %s", cgraph_node_name (array[i]));
663 if (INLINED_TIMES (array[i]) != 1)
664 fprintf (cgraph_dump_file, " (%i times)",
665 (int)INLINED_TIMES (array[i]));
667 fprintf (cgraph_dump_file, "\n");
670 return nfound;
673 /* Estimate size of the function after inlining WHAT into TO. */
675 static int
676 cgraph_estimate_size_after_inlining (int times, struct cgraph_node *to,
677 struct cgraph_node *what)
679 return (what->global.insns - INSNS_PER_CALL) *times + to->global.insns;
682 /* Estimate the growth caused by inlining NODE into all callees. */
684 static int
685 cgraph_estimate_growth (struct cgraph_node *node)
687 int growth = 0;
688 int calls_saved = 0;
689 int clones_added = 0;
690 struct cgraph_edge *e;
692 for (e = node->callers; e; e = e->next_caller)
693 if (!e->inline_call)
695 growth += ((cgraph_estimate_size_after_inlining (1, e->caller, node)
697 e->caller->global.insns) *e->caller->global.cloned_times);
698 calls_saved += e->caller->global.cloned_times;
699 clones_added += e->caller->global.cloned_times;
702 /* ??? Wrong for self recursive functions or cases where we decide to not
703 inline for different reasons, but it is not big deal as in that case
704 we will keep the body around, but we will also avoid some inlining. */
705 if (!node->needed && !node->origin && !DECL_EXTERNAL (node->decl))
706 growth -= node->global.insns, clones_added--;
708 if (!calls_saved)
709 calls_saved = 1;
711 return growth;
714 /* Update insn sizes after inlining WHAT into TO that is already inlined into
715 all nodes in INLINED array. */
717 static void
718 cgraph_mark_inline (struct cgraph_node *to, struct cgraph_node *what,
719 struct cgraph_node **inlined, int ninlined,
720 struct cgraph_node **inlined_callees,
721 int ninlined_callees)
723 int i;
724 int times = 0;
725 int clones = 0;
726 struct cgraph_edge *e;
727 bool called = false;
728 int new_insns;
730 for (e = what->callers; e; e = e->next_caller)
732 if (e->caller == to)
734 if (e->inline_call)
735 abort ();
736 e->inline_call = true;
737 times++;
738 clones += e->caller->global.cloned_times;
740 else if (!e->inline_call)
741 called = true;
743 if (!times)
744 abort ();
745 ncalls_inlined += times;
747 new_insns = cgraph_estimate_size_after_inlining (times, to, what);
748 if (to->global.will_be_output)
749 overall_insns += new_insns - to->global.insns;
750 to->global.insns = new_insns;
752 if (!called && !what->needed && !what->origin
753 && !DECL_EXTERNAL (what->decl))
755 if (!what->global.will_be_output)
756 abort ();
757 clones--;
758 nfunctions_inlined++;
759 what->global.will_be_output = 0;
760 overall_insns -= what->global.insns;
762 what->global.cloned_times += clones;
763 for (i = 0; i < ninlined; i++)
765 new_insns =
766 cgraph_estimate_size_after_inlining (INLINED_TIMES (inlined[i]) *
767 times, inlined[i], what);
768 if (inlined[i]->global.will_be_output)
769 overall_insns += new_insns - inlined[i]->global.insns;
770 inlined[i]->global.insns = new_insns;
772 for (i = 0; i < ninlined_callees; i++)
774 inlined_callees[i]->global.cloned_times +=
775 INLINED_TIMES (inlined_callees[i]) * clones;
779 /* Return false when inlining WHAT into TO is not good idea as it would cause
780 too large growth of function bodies. */
782 static bool
783 cgraph_check_inline_limits (struct cgraph_node *to, struct cgraph_node *what,
784 struct cgraph_node **inlined, int ninlined)
786 int i;
787 int times = 0;
788 struct cgraph_edge *e;
789 int newsize;
790 int limit;
792 for (e = to->callees; e; e = e->next_callee)
793 if (e->callee == what)
794 times++;
796 /* When inlining large function body called once into small function,
797 take the inlined function as base for limiting the growth. */
798 if (to->local.self_insns > what->local.self_insns)
799 limit = to->local.self_insns;
800 else
801 limit = what->local.self_insns;
803 limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
805 newsize = cgraph_estimate_size_after_inlining (times, to, what);
806 if (newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
807 && newsize > limit)
808 return false;
809 for (i = 0; i < ninlined; i++)
811 newsize =
812 cgraph_estimate_size_after_inlining (INLINED_TIMES (inlined[i]) *
813 times, inlined[i], what);
814 if (newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
815 && newsize >
816 inlined[i]->local.self_insns *
817 (100 + PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH)) / 100)
818 return false;
820 return true;
823 /* Return true when function N is small enought to be inlined. */
825 static bool
826 cgraph_default_inline_p (struct cgraph_node *n)
828 if (!DECL_INLINE (n->decl) || !DECL_SAVED_TREE (n->decl))
829 return false;
830 if (DECL_DECLARED_INLINE_P (n->decl))
831 return n->global.insns < MAX_INLINE_INSNS_SINGLE;
832 else
833 return n->global.insns < MAX_INLINE_INSNS_AUTO;
836 /* We use greedy algorithm for inlining of small functions:
837 All inline candidates are put into prioritized heap based on estimated
838 growth of the overall number of instructions and then update the estimates.
840 INLINED and INLINED_CALEES are just pointers to arrays large enought
841 to be passed to cgraph_inlined_into and cgraph_inlined_callees. */
843 static void
844 cgraph_decide_inlining_of_small_functions (struct cgraph_node **inlined,
845 struct cgraph_node **inlined_callees)
847 int i;
848 struct cgraph_node *node;
849 fibheap_t heap = fibheap_new ();
850 struct fibnode **heap_node =
851 xcalloc (cgraph_max_uid, sizeof (struct fibnode *));
852 int ninlined, ninlined_callees;
853 int max_insns = ((HOST_WIDEST_INT) initial_insns
854 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
856 /* Put all inline candidates into the heap. */
858 for (node = cgraph_nodes; node; node = node->next)
860 struct cgraph_edge *e;
862 if (!node->local.inlinable || !node->callers
863 || !cgraph_default_inline_p (node))
864 continue;
866 /* Rule out always_inline functions we dealt with earlier. */
867 for (e = node->callers; e; e = e->next_caller)
868 if (e->inline_call)
869 break;
870 if (e)
871 continue;
872 heap_node[node->uid] =
873 fibheap_insert (heap, cgraph_estimate_growth (node), node);
876 if (cgraph_dump_file)
877 fprintf (cgraph_dump_file, "\n\nDeciding on inlining: ");
878 while ((node = fibheap_extract_min (heap)) && overall_insns <= max_insns)
880 struct cgraph_edge *e;
881 int old_insns = overall_insns;
883 heap_node[node->uid] = NULL;
884 if (cgraph_dump_file)
885 fprintf (cgraph_dump_file, "Considering %s %i insns, growth %i.\n",
886 cgraph_node_name (node), node->global.insns,
887 cgraph_estimate_growth (node));
888 if (!cgraph_default_inline_p (node))
890 if (cgraph_dump_file)
891 fprintf (cgraph_dump_file, "Function too large.\n");
892 continue;
894 ninlined_callees = cgraph_inlined_callees (node, inlined_callees);
895 for (e = node->callers; e; e = e->next_caller)
896 if (!e->inline_call && e->caller != node)
898 ninlined = cgraph_inlined_into (e->caller, inlined);
899 if (e->callee->output
900 || !cgraph_check_inline_limits (e->caller, node, inlined,
901 ninlined))
903 for (i = 0; i < ninlined; i++)
904 inlined[i]->output = 0, node->aux = 0;
905 if (cgraph_dump_file)
906 fprintf (cgraph_dump_file, "Not inlining into %s\n",
907 cgraph_node_name (e->caller));
908 continue;
910 cgraph_mark_inline (e->caller, node, inlined, ninlined,
911 inlined_callees, ninlined_callees);
912 if (heap_node[e->caller->uid])
913 fibheap_replace_key (heap, heap_node[e->caller->uid],
914 cgraph_estimate_growth (e->caller));
916 /* Size of the functions we updated into has changed, so update
917 the keys. */
918 for (i = 0; i < ninlined; i++)
920 inlined[i]->output = 0, node->aux = 0;
921 if (heap_node[inlined[i]->uid])
922 fibheap_replace_key (heap, heap_node[inlined[i]->uid],
923 cgraph_estimate_growth (inlined[i]));
927 /* Similarly all functions called by function we just inlined
928 are now called more times; update keys. */
930 for (e = node->callees; e; e = e->next_callee)
931 if (!e->inline_call && heap_node[e->callee->uid])
932 fibheap_replace_key (heap, heap_node[e->callee->uid],
933 cgraph_estimate_growth (e->callee));
935 for (i = 0; i < ninlined_callees; i++)
937 struct cgraph_edge *e;
939 for (e = inlined_callees[i]->callees; e; e = e->next_callee)
940 if (!e->inline_call && heap_node[e->callee->uid])
941 fibheap_replace_key (heap, heap_node[e->callee->uid],
942 cgraph_estimate_growth (e->callee));
944 inlined_callees[i]->output = 0, node->aux = 0;
946 if (cgraph_dump_file)
947 fprintf (cgraph_dump_file,
948 "Created %i clones, Num insns:%i (%+i), %.2f%%.\n\n",
949 node->global.cloned_times - 1,
950 overall_insns, overall_insns - old_insns,
951 overall_insns * 100.0 / initial_insns);
953 if (cgraph_dump_file && !fibheap_empty (heap))
954 fprintf (cgraph_dump_file, "inline-unit-growth limit reached.\n");
955 fibheap_delete (heap);
956 free (heap_node);
959 /* Decide on the inlining. We do so in the topological order to avoid
960 expenses on updating datastructures. */
962 static void
963 cgraph_decide_inlining (void)
965 struct cgraph_node *node;
966 int nnodes;
967 struct cgraph_node **order =
968 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
969 struct cgraph_node **inlined =
970 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
971 struct cgraph_node **inlined_callees =
972 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
973 int ninlined;
974 int ninlined_callees;
975 int i, y;
977 for (node = cgraph_nodes; node; node = node->next)
978 initial_insns += node->local.self_insns;
979 overall_insns = initial_insns;
981 nnodes = cgraph_postorder (order);
983 for (node = cgraph_nodes; node; node = node->next)
984 node->aux = 0;
986 if (cgraph_dump_file)
987 fprintf (cgraph_dump_file, "\n\nDeciding on always_inline functions:\n");
989 /* In the first pass mark all always_inline edges. Do this with a priority
990 so no our decisions makes this impossible. */
991 for (i = nnodes - 1; i >= 0; i--)
993 struct cgraph_edge *e;
995 node = order[i];
997 for (e = node->callees; e; e = e->next_callee)
998 if (e->callee->local.disregard_inline_limits)
999 break;
1000 if (!e)
1001 continue;
1002 if (cgraph_dump_file)
1003 fprintf (cgraph_dump_file,
1004 "Considering %s %i insns (always inline)\n",
1005 cgraph_node_name (node), node->global.insns);
1006 ninlined = cgraph_inlined_into (order[i], inlined);
1007 for (; e; e = e->next_callee)
1009 if (e->inline_call || !e->callee->local.disregard_inline_limits)
1010 continue;
1011 if (e->callee->output || e->callee == node)
1012 continue;
1013 ninlined_callees =
1014 cgraph_inlined_callees (e->callee, inlined_callees);
1015 cgraph_mark_inline (node, e->callee, inlined, ninlined,
1016 inlined_callees, ninlined_callees);
1017 for (y = 0; y < ninlined_callees; y++)
1018 inlined_callees[y]->output = 0, node->aux = 0;
1019 if (cgraph_dump_file)
1020 fprintf (cgraph_dump_file, "Inlined %i times. Now %i insns\n\n",
1021 node->global.cloned_times, overall_insns);
1023 for (y = 0; y < ninlined; y++)
1024 inlined[y]->output = 0, node->aux = 0;
1027 cgraph_decide_inlining_of_small_functions (inlined, inlined_callees);
1029 if (cgraph_dump_file)
1030 fprintf (cgraph_dump_file, "\n\nFunctions to inline once:\n");
1032 /* And finally decide what functions are called once. */
1034 for (i = nnodes - 1; i >= 0; i--)
1036 node = order[i];
1038 if (node->callers && !node->callers->next_caller && !node->needed
1039 && node->local.inlinable && !node->callers->inline_call
1040 && !DECL_EXTERNAL (node->decl) && !DECL_COMDAT (node->decl))
1042 bool ok = true;
1043 struct cgraph_node *node1;
1045 /* Verify that we won't duplicate the caller. */
1046 for (node1 = node->callers->caller;
1047 node1->callers && node1->callers->inline_call
1048 && ok; node1 = node1->callers->caller)
1049 if (node1->callers->next_caller || node1->needed)
1050 ok = false;
1051 if (ok)
1053 if (cgraph_dump_file)
1054 fprintf (cgraph_dump_file,
1055 "Considering %s %i insns (called once)\n",
1056 cgraph_node_name (node), node->global.insns);
1057 ninlined = cgraph_inlined_into (node->callers->caller, inlined);
1058 if (cgraph_check_inline_limits
1059 (node->callers->caller, node, inlined, ninlined))
1061 ninlined_callees =
1062 cgraph_inlined_callees (node, inlined_callees);
1063 cgraph_mark_inline (node->callers->caller, node, inlined,
1064 ninlined, inlined_callees,
1065 ninlined_callees);
1066 for (y = 0; y < ninlined_callees; y++)
1067 inlined_callees[y]->output = 0, node->aux = 0;
1068 if (cgraph_dump_file)
1069 fprintf (cgraph_dump_file, "Inlined. Now %i insns\n\n", overall_insns);
1071 for (y = 0; y < ninlined; y++)
1072 inlined[y]->output = 0, node->aux = 0;
1077 if (cgraph_dump_file)
1078 fprintf (cgraph_dump_file,
1079 "\nInlined %i calls, elliminated %i functions, %i insns turned to %i insns.\n",
1080 ncalls_inlined, nfunctions_inlined, initial_insns,
1081 overall_insns);
1082 free (order);
1083 free (inlined);
1084 free (inlined_callees);
1087 /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */
1089 bool
1090 cgraph_inline_p (tree caller_decl, tree callee_decl)
1092 struct cgraph_node *caller = cgraph_node (caller_decl);
1093 struct cgraph_node *callee = cgraph_node (callee_decl);
1094 struct cgraph_edge *e;
1096 for (e = caller->callees; e; e = e->next_callee)
1097 if (e->callee == callee)
1098 return e->inline_call;
1099 /* We do not record builtins in the callgraph. Perhaps it would make more
1100 sense to do so and then prune out those not overwritten by explicit
1101 function body. */
1102 return false;
1104 /* Expand all functions that must be output.
1106 Attempt to topologically sort the nodes so function is output when
1107 all called functions are already assembled to allow data to be
1108 propagated across the callgraph. Use a stack to get smaller distance
1109 between a function and it's callees (later we may choose to use a more
1110 sophisticated algorithm for function reordering; we will likely want
1111 to use subsections to make the output functions appear in top-down
1112 order). */
1114 static void
1115 cgraph_expand_functions (void)
1117 struct cgraph_node *node;
1118 struct cgraph_node **order =
1119 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
1120 int order_pos = 0;
1121 int i;
1123 cgraph_mark_functions_to_output ();
1125 order_pos = cgraph_postorder (order);
1127 for (i = order_pos - 1; i >= 0; i--)
1129 node = order[i];
1130 if (node->output)
1132 if (!node->reachable)
1133 abort ();
1134 node->output = 0;
1135 cgraph_expand_function (node);
1138 free (order);
1141 /* Mark all local functions.
1143 A local function is one whose calls can occur only in the
1144 current compilation unit, so we change its calling convention.
1145 We simply mark all static functions whose address is not taken
1146 as local. */
1148 static void
1149 cgraph_mark_local_functions (void)
1151 struct cgraph_node *node;
1153 if (cgraph_dump_file)
1154 fprintf (cgraph_dump_file, "Marking local functions:");
1156 /* Figure out functions we want to assemble. */
1157 for (node = cgraph_nodes; node; node = node->next)
1159 node->local.local = (!node->needed
1160 && DECL_SAVED_TREE (node->decl)
1161 && !TREE_PUBLIC (node->decl));
1162 if (cgraph_dump_file && node->local.local)
1163 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
1165 if (cgraph_dump_file)
1166 fprintf (cgraph_dump_file, "\n");
1169 /* Perform simple optimizations based on callgraph. */
1171 void
1172 cgraph_optimize (void)
1174 if (!flag_unit_at_a_time)
1175 return;
1176 timevar_push (TV_CGRAPHOPT);
1177 if (!quiet_flag)
1178 fprintf (stderr, "Performing intraprocedural optimizations\n");
1179 if (cgraph_dump_file)
1181 fprintf (cgraph_dump_file, "Initial callgraph:");
1182 dump_cgraph (cgraph_dump_file);
1184 cgraph_mark_local_functions ();
1186 cgraph_decide_inlining ();
1188 cgraph_global_info_ready = true;
1189 if (cgraph_dump_file)
1191 fprintf (cgraph_dump_file, "Optimized callgraph:");
1192 dump_cgraph (cgraph_dump_file);
1194 timevar_pop (TV_CGRAPHOPT);
1195 if (!quiet_flag)
1196 fprintf (stderr, "Assembling functions:");
1198 /* Output everything. */
1199 cgraph_expand_functions ();
1200 if (cgraph_dump_file)
1202 fprintf (cgraph_dump_file, "Final callgraph:");
1203 dump_cgraph (cgraph_dump_file);