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
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
27 #include "tree-inline.h"
28 #include "langhooks.h"
36 #include "diagnostic.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
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. */
69 cgraph_finalize_function (tree decl
, tree body ATTRIBUTE_UNUSED
)
71 struct cgraph_node
*node
= cgraph_node (decl
);
74 node
->local
.finalized
= true;
76 /* Function now has DECL_SAVED_TREE set. Enqueue it into cgraph_nodes_queue
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
)
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
;
118 cgraph_expand_function (n
);
121 (*debug_hooks
->deferred_inline_function
) (decl
);
124 /* Walk tree and record all calls. Called via walk_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
))
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
,
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
))
166 /* Create cgraph edges for function calls inside BODY from DECL. */
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. */
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. */
217 cgraph_finalize_compilation_unit (void)
219 struct cgraph_node
*node
;
221 if (!flag_unit_at_a_time
)
224 cgraph_varpool_assemble_pending_decls ();
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
))
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");
290 timevar_pop (TV_CGRAPH
);
293 /* Figure out what functions we want to assemble. */
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
;
307 for (e
= node
->callers
; e
; e
= e
->next_caller
)
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
)
316 || (e
&& node
->reachable
))
317 && !TREE_ASM_WRITTEN (decl
) && !node
->origin
318 && !DECL_EXTERNAL (decl
))
323 /* Optimize the function before expansion. */
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
);
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. */
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
;
368 for (e
= node
->callers
; e
; e
= e
->next_caller
)
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. */
380 cgraph_postorder (struct cgraph_node
**order
)
382 struct cgraph_node
*node
, *node2
;
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
)
396 for (node
= cgraph_nodes
; node
; node
= node
->next
)
403 node
->aux
= node
->callers
;
406 while (node2
->aux
!= &last
)
409 if (edge
->next_caller
)
410 node2
->aux
= edge
->next_caller
;
413 if (!edge
->caller
->aux
)
415 if (!edge
->caller
->callers
)
416 edge
->caller
->aux
= &last
;
418 edge
->caller
->aux
= edge
->caller
->callers
;
419 stack
[stack_size
++] = node2
;
420 node2
= edge
->caller
;
424 if (node2
->aux
== &last
)
426 order
[order_pos
++] = node2
;
428 node2
= stack
[--stack_size
];
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
450 cgraph_inlined_into (struct cgraph_node
*node
, struct cgraph_node
**array
)
453 struct cgraph_edge
**stack
;
454 struct cgraph_edge
*e
, *e1
;
458 /* Fast path: since we traverse in mostly topological order, we will likely
460 for (e
= node
->callers
; e
; e
= e
->next_caller
)
467 /* Allocate stack for back-tracking up callgraph. */
468 stack
= xmalloc ((cgraph_n_nodes
+ 1) * sizeof (struct cgraph_edge
));
471 /* Push the first edge on to the stack. */
476 struct cgraph_node
*caller
;
478 /* Look at the edge on the top of the stack. */
482 /* Check if the caller destination has been visited yet. */
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
)
501 for (e1
= e
->next_caller
; e1
; e1
= e1
->next_caller
)
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");
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
548 cgraph_inlined_callees (struct cgraph_node
*node
, struct cgraph_node
**array
)
551 struct cgraph_edge
**stack
;
552 struct cgraph_edge
*e
, *e1
;
556 /* Fast path: since we traverse in mostly topological order, we will likely
558 for (e
= node
->callees
; e
; e
= e
->next_callee
)
565 /* Allocate stack for back-tracking up callgraph. */
566 stack
= xmalloc ((cgraph_n_nodes
+ 1) * sizeof (struct cgraph_edge
));
569 /* Push the first edge on to the stack. */
574 struct cgraph_node
*callee
;
576 /* Look at the edge on the top of the stack. */
580 /* Check if the callee destination has been visited yet. */
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
)
599 for (e1
= e
->next_callee
; e1
; e1
= e1
->next_callee
)
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");
638 /* Estimate size of the function after inlining WHAT into TO. */
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. */
650 cgraph_estimate_growth (struct cgraph_node
*node
)
654 int clones_added
= 0;
655 struct cgraph_edge
*e
;
657 for (e
= node
->callers
; e
; e
= e
->next_caller
)
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
--;
679 /* Update insn sizes after inlining WHAT into TO that is already inlined into
680 all nodes in INLINED array. */
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
)
691 struct cgraph_edge
*e
;
695 for (e
= what
->callers
; e
; e
= e
->next_caller
)
701 e
->inline_call
= true;
703 clones
+= e
->caller
->global
.cloned_times
;
705 else if (!e
->inline_call
)
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
)
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
++)
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. */
748 cgraph_check_inline_limits (struct cgraph_node
*to
, struct cgraph_node
*what
,
749 struct cgraph_node
**inlined
, int ninlined
)
753 struct cgraph_edge
*e
;
757 for (e
= to
->callees
; e
; e
= e
->next_callee
)
758 if (e
->callee
== what
)
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
;
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
)
774 for (i
= 0; i
< ninlined
; i
++)
777 cgraph_estimate_size_after_inlining (INLINED_TIMES (inlined
[i
]) *
778 times
, inlined
[i
], what
);
779 if (newsize
> PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS
)
781 inlined
[i
]->local
.self_insns
*
782 (100 + PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH
)) / 100)
788 /* Return true when function N is small enought to be inlined. */
791 cgraph_default_inline_p (struct cgraph_node
*n
)
793 if (!DECL_INLINE (n
->decl
) || !DECL_SAVED_TREE (n
->decl
))
795 if (DECL_DECLARED_INLINE_P (n
->decl
))
796 return n
->global
.insns
< MAX_INLINE_INSNS_SINGLE
;
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. */
809 cgraph_decide_inlining_of_small_functions (struct cgraph_node
**inlined
,
810 struct cgraph_node
**inlined_callees
)
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
))
831 /* Rule out always_inline functions we dealt with earlier. */
832 for (e
= node
->callers
; e
; e
= e
->next_caller
)
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");
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
,
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
));
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
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
);
924 /* Decide on the inlining. We do so in the topological order to avoid
925 expenses on updating datastructures. */
928 cgraph_decide_inlining (void)
930 struct cgraph_node
*node
;
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
*));
939 int ninlined_callees
;
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
)
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
;
962 for (e
= node
->callees
; e
; e
= e
->next_callee
)
963 if (e
->callee
->local
.disregard_inline_limits
)
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
)
976 if (e
->callee
->output
|| e
->callee
== node
)
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
--)
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
))
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
)
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
))
1027 cgraph_inlined_callees (node
, inlined_callees
);
1028 cgraph_mark_inline (node
->callers
->caller
, node
, inlined
,
1029 ninlined
, inlined_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
,
1049 free (inlined_callees
);
1052 /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */
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
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
1080 cgraph_expand_functions (void)
1082 struct cgraph_node
*node
;
1083 struct cgraph_node
**order
=
1084 xcalloc (cgraph_n_nodes
, sizeof (struct cgraph_node
*));
1088 cgraph_mark_functions_to_output ();
1090 order_pos
= cgraph_postorder (order
);
1092 for (i
= order_pos
- 1; i
>= 0; i
--)
1097 if (!node
->reachable
)
1100 cgraph_expand_function (node
);
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
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. */
1137 cgraph_optimize (void)
1139 if (!flag_unit_at_a_time
)
1141 timevar_push (TV_CGRAPHOPT
);
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
);
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
);