]>
Commit | Line | Data |
---|---|---|
b58b1157 | 1 | /* Callgraph based intraprocedural optimizations. |
b684a3df | 2 | Copyright (C) 2003, 2004 Free Software Foundation, Inc. |
1c4a429a JH |
3 | Contributed by Jan Hubicka |
4 | ||
5 | This file is part of GCC. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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. */ | |
21 | ||
18c6ada9 JH |
22 | /* This module implements main driver of compilation process as well as |
23 | few basic intraprocedural optimizers. | |
24 | ||
25 | The main scope of this file is to act as an interface in between | |
26 | tree based frontends and the backend (and middle end) | |
27 | ||
28 | The front-end is supposed to use following functionality: | |
29 | ||
30 | - cgraph_finalize_function | |
31 | ||
32 | This function is called once front-end has parsed whole body of function | |
33 | and it is certain that the function body nor the declaration will change. | |
34 | ||
35 | (There is one exception needed for implementing GCC extern inline function.) | |
36 | ||
37 | - cgraph_varpool_finalize_variable | |
38 | ||
39 | This function has same behaviour as the above but is used for static | |
40 | variables. | |
41 | ||
42 | - cgraph_finalize_compilation_unit | |
43 | ||
44 | This function is called once compilation unit is finalized and it will | |
45 | no longer change. | |
46 | ||
47 | In the unit-at-a-time the call-graph construction and local function | |
48 | analysis takes place here. Bodies of unreachable functions are released | |
49 | to conserve memory usage. | |
50 | ||
51 | ??? The compilation unit in this point of view should be compilation | |
52 | unit as defined by the language - for instance C frontend allows multiple | |
53 | compilation units to be parsed at once and it should call function each | |
54 | time parsing is done so we save memory. | |
55 | ||
56 | - cgraph_optimize | |
57 | ||
58 | In this unit-at-a-time compilation the intra procedural analysis takes | |
59 | place here. In particular the static functions whose address is never | |
60 | taken are marked as local. Backend can then use this information to | |
61 | modify calling conventions, do better inlining or similar optimizations. | |
62 | ||
63 | - cgraph_assemble_pending_functions | |
64 | - cgraph_varpool_assemble_pending_variables | |
65 | ||
66 | In non-unit-at-a-time mode these functions can be used to force compilation | |
67 | of functions or variables that are known to be needed at given stage | |
68 | of compilation | |
69 | ||
70 | - cgraph_mark_needed_node | |
71 | - cgraph_varpool_mark_needed_node | |
72 | ||
73 | When function or variable is referenced by some hidden way (for instance | |
74 | via assembly code and marked by attribute "used"), the call-graph data structure | |
75 | must be updated accordingly by this function. | |
76 | ||
77 | - analyze_expr callback | |
78 | ||
79 | This function is responsible for lowering tree nodes not understood by | |
80 | generic code into understandable ones or alternatively marking | |
81 | callgraph and varpool nodes referenced by the as needed. | |
82 | ||
83 | ??? On the tree-ssa genericizing should take place here and we will avoid | |
84 | need for these hooks (replacing them by genericizing hook) | |
85 | ||
86 | - expand_function callback | |
87 | ||
88 | This function is used to expand function and pass it into RTL back-end. | |
89 | Front-end should not make any assumptions about when this function can be | |
90 | called. In particular cgraph_assemble_pending_functions, | |
91 | cgraph_varpool_assemble_pending_variables, cgraph_finalize_function, | |
92 | cgraph_varpool_finalize_function, cgraph_optimize can cause arbitrarily | |
93 | previously finalized functions to be expanded. | |
94 | ||
95 | We implement two compilation modes. | |
96 | ||
97 | - unit-at-a-time: In this mode analyzing of all functions is deferred | |
98 | to cgraph_finalize_compilation_unit and expansion into cgraph_optimize. | |
99 | ||
100 | In cgraph_finalize_compilation_unit the reachable functions are | |
101 | analyzed. During analysis the call-graph edges from reachable | |
102 | functions are constructed and their destinations are marked as | |
103 | reachable. References to functions and variables are discovered too | |
104 | and variables found to be needed output to the assembly file. Via | |
105 | mark_referenced call in assemble_variable functions referenced by | |
106 | static variables are noticed too. | |
107 | ||
108 | The intra-procedural information is produced and it's existence | |
109 | indicated by global_info_ready. Once this flag is set it is impossible | |
110 | to change function from !reachable to reachable and thus | |
111 | assemble_variable no longer call mark_referenced. | |
112 | ||
113 | Finally the call-graph is topologically sorted and all reachable functions | |
114 | that has not been completely inlined or are not external are output. | |
115 | ||
116 | ??? It is possible that reference to function or variable is optimized | |
117 | out. We can not deal with this nicely because topological order is not | |
118 | suitable for it. For tree-ssa we may consider another pass doing | |
119 | optimization and re-discovering reachable functions. | |
120 | ||
121 | ??? Reorganize code so variables are output very last and only if they | |
122 | really has been referenced by produced code, so we catch more cases | |
123 | where reference has been optimized out. | |
124 | ||
125 | - non-unit-at-a-time | |
126 | ||
127 | All functions are variables are output as early as possible to conserve | |
128 | memory consumption. This may or may not result in less memory used but | |
129 | it is still needed for some legacy code that rely on particular ordering | |
130 | of things output from the compiler. | |
131 | ||
132 | Varpool data structures are not used and variables are output directly. | |
133 | ||
134 | Functions are output early using call of | |
135 | cgraph_assemble_pending_function from cgraph_finalize_function. The | |
136 | decision on whether function is needed is made more conservative so | |
137 | uninlininable static functions are needed too. During the call-graph | |
138 | construction the edge destinations are not marked as reachable and it | |
139 | is completely relied upn assemble_variable to mark them. | |
140 | ||
141 | Inlining decision heuristics | |
142 | ??? Move this to separate file after tree-ssa merge. | |
143 | ||
144 | We separate inlining decisions from the inliner itself and store it | |
145 | inside callgraph as so called inline plan. Reffer to cgraph.c | |
146 | documentation about particular representation of inline plans in the | |
147 | callgraph | |
148 | ||
149 | The implementation of particular heuristics is separated from | |
150 | the rest of code to make it easier to replace it with more complicated | |
151 | implementation in the future. The rest of inlining code acts as a | |
152 | library aimed to modify the callgraph and verify that the parameters | |
153 | on code size growth fits. | |
154 | ||
155 | To mark given call inline, use cgraph_mark_inline function, the | |
156 | verification is performed by cgraph_default_inline_p and | |
157 | cgraph_check_inline_limits. | |
158 | ||
159 | The heuristics implements simple knapsack style algorithm ordering | |
160 | all functions by their "profitability" (estimated by code size growth) | |
161 | and inlining them in priority order. | |
162 | ||
163 | cgraph_decide_inlining implements heuristics taking whole callgraph | |
164 | into account, while cgraph_decide_inlining_incrementally considers | |
165 | only one function at a time and is used in non-unit-at-a-time mode. */ | |
1c4a429a JH |
166 | #include "config.h" |
167 | #include "system.h" | |
168 | #include "coretypes.h" | |
169 | #include "tm.h" | |
170 | #include "tree.h" | |
171 | #include "tree-inline.h" | |
172 | #include "langhooks.h" | |
173 | #include "hashtab.h" | |
174 | #include "toplev.h" | |
175 | #include "flags.h" | |
176 | #include "ggc.h" | |
177 | #include "debug.h" | |
178 | #include "target.h" | |
179 | #include "cgraph.h" | |
dafc5b82 | 180 | #include "diagnostic.h" |
a194aa56 | 181 | #include "timevar.h" |
b58b1157 JH |
182 | #include "params.h" |
183 | #include "fibheap.h" | |
184 | #include "c-common.h" | |
dc0bfe6a | 185 | #include "intl.h" |
902edd36 | 186 | #include "function.h" |
b58b1157 JH |
187 | |
188 | #define INSNS_PER_CALL 10 | |
1c4a429a | 189 | |
a20af5b8 | 190 | static void cgraph_expand_all_functions (void); |
db0e878d AJ |
191 | static void cgraph_mark_functions_to_output (void); |
192 | static void cgraph_expand_function (struct cgraph_node *); | |
193 | static tree record_call_1 (tree *, int *, void *); | |
194 | static void cgraph_mark_local_functions (void); | |
4a46cbfb JH |
195 | static bool cgraph_default_inline_p (struct cgraph_node *n); |
196 | static void cgraph_analyze_function (struct cgraph_node *node); | |
d4d1ebc1 | 197 | static void cgraph_decide_inlining_incrementally (struct cgraph_node *); |
1c4a429a | 198 | |
b58b1157 JH |
199 | /* Statistics we collect about inlining algorithm. */ |
200 | static int ncalls_inlined; | |
201 | static int nfunctions_inlined; | |
202 | static int initial_insns; | |
203 | static int overall_insns; | |
204 | ||
7dff32e6 JS |
205 | /* Records tree nodes seen in cgraph_create_edges. Simply using |
206 | walk_tree_without_duplicates doesn't guarantee each node is visited | |
207 | once because it gets a new htab upon each recursive call from | |
208 | record_calls_1. */ | |
209 | static htab_t visited_nodes; | |
210 | ||
8dafba3c RH |
211 | /* Determine if function DECL is needed. That is, visible to something |
212 | either outside this translation unit, something magic in the system | |
213 | configury, or (if not doing unit-at-a-time) to something we havn't | |
214 | seen yet. */ | |
215 | ||
216 | static bool | |
217 | decide_is_function_needed (struct cgraph_node *node, tree decl) | |
218 | { | |
219 | /* If we decided it was needed before, but at the time we didn't have | |
220 | the body of the function available, then it's still needed. We have | |
221 | to go back and re-check its dependencies now. */ | |
222 | if (node->needed) | |
223 | return true; | |
224 | ||
225 | /* Externally visible functions must be output. The exception is | |
226 | COMDAT functions that must be output only when they are needed. */ | |
227 | if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl)) | |
228 | return true; | |
229 | ||
230 | /* Constructors and destructors are reachable from the runtime by | |
231 | some mechanism. */ | |
232 | if (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)) | |
233 | return true; | |
234 | ||
235 | /* If the user told us it is used, then it must be so. */ | |
236 | if (lookup_attribute ("used", DECL_ATTRIBUTES (decl))) | |
237 | return true; | |
238 | ||
239 | /* ??? If the assembler name is set by hand, it is possible to assemble | |
240 | the name later after finalizing the function and the fact is noticed | |
241 | in assemble_name then. This is arguably a bug. */ | |
242 | if (DECL_ASSEMBLER_NAME_SET_P (decl) | |
243 | && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))) | |
244 | return true; | |
245 | ||
246 | if (flag_unit_at_a_time) | |
247 | return false; | |
248 | ||
249 | /* If not doing unit at a time, then we'll only defer this function | |
250 | if its marked for inlining. Otherwise we want to emit it now. */ | |
251 | ||
252 | /* "extern inline" functions are never output locally. */ | |
253 | if (DECL_EXTERNAL (decl)) | |
254 | return false; | |
2067c116 | 255 | /* We want to emit COMDAT functions only when absolutely necessary. */ |
d853a20e | 256 | if (DECL_COMDAT (decl)) |
8dafba3c RH |
257 | return false; |
258 | if (!DECL_INLINE (decl) | |
259 | || (!node->local.disregard_inline_limits | |
260 | /* When declared inline, defer even the uninlinable functions. | |
7d82fe7c | 261 | This allows them to be eliminated when unused. */ |
8dafba3c | 262 | && !DECL_DECLARED_INLINE_P (decl) |
d4d1ebc1 | 263 | && (!node->local.inlinable || !cgraph_default_inline_p (node)))) |
8dafba3c RH |
264 | return true; |
265 | ||
266 | return false; | |
267 | } | |
268 | ||
d853a20e JH |
269 | /* When not doing unit-at-a-time, output all functions enqueued. |
270 | Return true when such a functions were found. */ | |
f6d1b84a RH |
271 | |
272 | bool | |
d853a20e JH |
273 | cgraph_assemble_pending_functions (void) |
274 | { | |
275 | bool output = false; | |
276 | ||
277 | if (flag_unit_at_a_time) | |
278 | return false; | |
279 | ||
280 | while (cgraph_nodes_queue) | |
281 | { | |
282 | struct cgraph_node *n = cgraph_nodes_queue; | |
283 | ||
284 | cgraph_nodes_queue = cgraph_nodes_queue->next_needed; | |
18c6ada9 JH |
285 | n->next_needed = NULL; |
286 | if (!n->origin && !n->global.inlined_to && !DECL_EXTERNAL (n->decl)) | |
f6d1b84a RH |
287 | { |
288 | cgraph_expand_function (n); | |
289 | output = true; | |
290 | } | |
d853a20e | 291 | } |
f6d1b84a | 292 | |
d853a20e JH |
293 | return output; |
294 | } | |
295 | ||
6b00c969 RH |
296 | /* DECL has been parsed. Take it, queue it, compile it at the whim of the |
297 | logic in effect. If NESTED is true, then our caller cannot stand to have | |
298 | the garbage collector run at the moment. We would need to either create | |
299 | a new GC context, or just not compile right now. */ | |
1c4a429a JH |
300 | |
301 | void | |
6b00c969 | 302 | cgraph_finalize_function (tree decl, bool nested) |
1c4a429a JH |
303 | { |
304 | struct cgraph_node *node = cgraph_node (decl); | |
305 | ||
d853a20e JH |
306 | if (node->local.finalized) |
307 | { | |
308 | /* As an GCC extension we allow redefinition of the function. The | |
6b00c969 RH |
309 | semantics when both copies of bodies differ is not well defined. |
310 | We replace the old body with new body so in unit at a time mode | |
311 | we always use new body, while in normal mode we may end up with | |
312 | old body inlined into some functions and new body expanded and | |
313 | inlined in others. | |
d853a20e | 314 | |
6b00c969 | 315 | ??? It may make more sense to use one body for inlining and other |
2067c116 | 316 | body for expanding the function but this is difficult to do. */ |
6b00c969 | 317 | |
f6d1b84a RH |
318 | /* If node->output is set, then this is a unit-at-a-time compilation |
319 | and we have already begun whole-unit analysis. This is *not* | |
320 | testing for whether we've already emitted the function. That | |
321 | case can be sort-of legitimately seen with real function | |
322 | redefinition errors. I would argue that the front end should | |
323 | never present us with such a case, but don't enforce that for now. */ | |
324 | if (node->output) | |
6b00c969 RH |
325 | abort (); |
326 | ||
327 | /* Reset our datastructures so we can analyze the function again. */ | |
cd4dea62 JH |
328 | memset (&node->local, 0, sizeof (node->local)); |
329 | memset (&node->global, 0, sizeof (node->global)); | |
330 | memset (&node->rtl, 0, sizeof (node->rtl)); | |
25c84396 | 331 | node->analyzed = false; |
95c755e9 | 332 | node->local.redefined_extern_inline = true; |
cd4dea62 | 333 | while (node->callees) |
18c6ada9 | 334 | cgraph_remove_edge (node->callees); |
6b00c969 | 335 | |
cd4dea62 JH |
336 | /* We may need to re-queue the node for assembling in case |
337 | we already proceeded it and ignored as not needed. */ | |
338 | if (node->reachable && !flag_unit_at_a_time) | |
d853a20e | 339 | { |
cd4dea62 JH |
340 | struct cgraph_node *n; |
341 | ||
342 | for (n = cgraph_nodes_queue; n; n = n->next_needed) | |
343 | if (n == node) | |
344 | break; | |
345 | if (!n) | |
346 | node->reachable = 0; | |
d853a20e | 347 | } |
d853a20e | 348 | } |
6b00c969 | 349 | |
d853a20e | 350 | notice_global_symbol (decl); |
1c4a429a | 351 | node->decl = decl; |
f6981e16 | 352 | node->local.finalized = true; |
1c4a429a | 353 | |
8dafba3c RH |
354 | /* If not unit at a time, then we need to create the call graph |
355 | now, so that called functions can be queued and emitted now. */ | |
4a46cbfb | 356 | if (!flag_unit_at_a_time) |
d4d1ebc1 JH |
357 | { |
358 | cgraph_analyze_function (node); | |
359 | cgraph_decide_inlining_incrementally (node); | |
360 | } | |
4a46cbfb | 361 | |
8dafba3c RH |
362 | if (decide_is_function_needed (node, decl)) |
363 | cgraph_mark_needed_node (node); | |
364 | ||
6b00c969 RH |
365 | /* If not unit at a time, go ahead and emit everything we've found |
366 | to be reachable at this time. */ | |
367 | if (!nested) | |
d34cb6a1 JH |
368 | { |
369 | if (!cgraph_assemble_pending_functions ()) | |
370 | ggc_collect (); | |
371 | } | |
1668aabc | 372 | |
8dafba3c | 373 | /* If we've not yet emitted decl, tell the debug info about it. */ |
6b00c969 | 374 | if (!TREE_ASM_WRITTEN (decl)) |
8dafba3c | 375 | (*debug_hooks->deferred_inline_function) (decl); |
d173e685 | 376 | |
18c6ada9 | 377 | /* We will never really output the function body, clear the STRUCT_FUNCTION array |
d173e685 JH |
378 | early then. */ |
379 | if (DECL_EXTERNAL (decl)) | |
1da326c3 | 380 | DECL_STRUCT_FUNCTION (decl) = NULL; |
902edd36 JH |
381 | |
382 | /* Possibly warn about unused parameters. */ | |
383 | if (warn_unused_parameter) | |
384 | do_warn_unused_parameter (decl); | |
1c4a429a JH |
385 | } |
386 | ||
1c4a429a JH |
387 | /* Walk tree and record all calls. Called via walk_tree. */ |
388 | static tree | |
db0e878d | 389 | record_call_1 (tree *tp, int *walk_subtrees, void *data) |
1c4a429a | 390 | { |
25c84396 RH |
391 | tree t = *tp; |
392 | ||
393 | switch (TREE_CODE (t)) | |
1c4a429a | 394 | { |
25c84396 RH |
395 | case VAR_DECL: |
396 | /* ??? Really, we should mark this decl as *potentially* referenced | |
397 | by this function and re-examine whether the decl is actually used | |
398 | after rtl has been generated. */ | |
399 | if (TREE_STATIC (t)) | |
400 | cgraph_varpool_mark_needed_node (cgraph_varpool_node (t)); | |
401 | break; | |
402 | ||
403 | case ADDR_EXPR: | |
404 | if (flag_unit_at_a_time) | |
405 | { | |
406 | /* Record dereferences to the functions. This makes the | |
407 | functions reachable unconditionally. */ | |
408 | tree decl = TREE_OPERAND (*tp, 0); | |
409 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
410 | cgraph_mark_needed_node (cgraph_node (decl)); | |
411 | } | |
412 | break; | |
413 | ||
414 | case CALL_EXPR: | |
415 | { | |
416 | tree decl = get_callee_fndecl (*tp); | |
417 | if (decl && TREE_CODE (decl) == FUNCTION_DECL) | |
418 | { | |
18c6ada9 | 419 | cgraph_create_edge (data, cgraph_node (decl), *tp); |
25c84396 RH |
420 | |
421 | /* When we see a function call, we don't want to look at the | |
422 | function reference in the ADDR_EXPR that is hanging from | |
423 | the CALL_EXPR we're examining here, because we would | |
424 | conclude incorrectly that the function's address could be | |
425 | taken by something that is not a function call. So only | |
426 | walk the function parameter list, skip the other subtrees. */ | |
427 | ||
428 | walk_tree (&TREE_OPERAND (*tp, 1), record_call_1, data, | |
429 | visited_nodes); | |
430 | *walk_subtrees = 0; | |
431 | } | |
432 | break; | |
433 | } | |
434 | ||
435 | default: | |
436 | /* Save some cycles by not walking types and declaration as we | |
437 | won't find anything useful there anyway. */ | |
438 | if (DECL_P (*tp) || TYPE_P (*tp)) | |
1c4a429a | 439 | { |
1c4a429a | 440 | *walk_subtrees = 0; |
25c84396 | 441 | break; |
1c4a429a | 442 | } |
25c84396 RH |
443 | |
444 | if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE) | |
ae2bcd98 | 445 | return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees, data); |
25c84396 | 446 | break; |
1c4a429a | 447 | } |
25c84396 | 448 | |
1c4a429a JH |
449 | return NULL; |
450 | } | |
451 | ||
18c6ada9 | 452 | /* Create cgraph edges for function calls inside BODY from NODE. */ |
1c4a429a JH |
453 | |
454 | void | |
18c6ada9 | 455 | cgraph_create_edges (struct cgraph_node *node, tree body) |
1c4a429a | 456 | { |
7660e67e SB |
457 | /* The nodes we're interested in are never shared, so walk |
458 | the tree ignoring duplicates. */ | |
7dff32e6 JS |
459 | visited_nodes = htab_create (37, htab_hash_pointer, |
460 | htab_eq_pointer, NULL); | |
18c6ada9 | 461 | walk_tree (&body, record_call_1, node, visited_nodes); |
7dff32e6 JS |
462 | htab_delete (visited_nodes); |
463 | visited_nodes = NULL; | |
1c4a429a JH |
464 | } |
465 | ||
18c6ada9 JH |
466 | static bool error_found; |
467 | ||
468 | /* Callbrack of verify_cgraph_node. Check that all call_exprs have cgraph nodes. */ | |
469 | static tree | |
470 | verify_cgraph_node_1 (tree *tp, int *walk_subtrees, void *data) | |
471 | { | |
472 | tree t = *tp; | |
473 | tree decl; | |
474 | ||
475 | if (TREE_CODE (t) == CALL_EXPR && (decl = get_callee_fndecl (t))) | |
476 | { | |
477 | struct cgraph_edge *e = cgraph_edge (data, t); | |
478 | if (e) | |
479 | { | |
480 | if (e->aux) | |
481 | { | |
482 | error ("Shared call_expr:"); | |
483 | debug_tree (t); | |
484 | error_found = true; | |
485 | } | |
486 | if (e->callee->decl != cgraph_node (decl)->decl) | |
487 | { | |
488 | error ("Edge points to wrong declaration:"); | |
489 | debug_tree (e->callee->decl); | |
490 | fprintf (stderr," Instead of:"); | |
491 | debug_tree (decl); | |
492 | } | |
493 | e->aux = (void *)1; | |
494 | } | |
495 | else | |
496 | { | |
497 | error ("Missing callgraph edge for call expr:"); | |
498 | debug_tree (t); | |
499 | error_found = true; | |
500 | } | |
501 | } | |
502 | /* Save some cycles by not walking types and declaration as we | |
503 | won't find anything useful there anyway. */ | |
504 | if (DECL_P (*tp) || TYPE_P (*tp)) | |
505 | { | |
506 | *walk_subtrees = 0; | |
507 | } | |
508 | return NULL_TREE; | |
509 | } | |
510 | ||
511 | /* Verify cgraph nodes of given cgraph node. */ | |
512 | void | |
513 | verify_cgraph_node (struct cgraph_node *node) | |
514 | { | |
515 | struct cgraph_edge *e; | |
516 | struct cgraph_node *main_clone; | |
517 | ||
518 | timevar_push (TV_CGRAPH_VERIFY); | |
519 | error_found = false; | |
520 | for (e = node->callees; e; e = e->next_callee) | |
521 | if (e->aux) | |
522 | { | |
523 | error ("Aux field set for edge %s->%s", | |
524 | cgraph_node_name (e->caller), cgraph_node_name (e->callee)); | |
525 | error_found = true; | |
526 | } | |
527 | for (e = node->callers; e; e = e->next_caller) | |
528 | { | |
529 | if (!e->inline_failed) | |
530 | { | |
531 | if (node->global.inlined_to | |
532 | != (e->caller->global.inlined_to | |
533 | ? e->caller->global.inlined_to : e->caller)) | |
534 | { | |
535 | error ("Inlined_to pointer is wrong"); | |
536 | error_found = true; | |
537 | } | |
538 | if (node->callers->next_caller) | |
539 | { | |
540 | error ("Multiple inline callers"); | |
541 | error_found = true; | |
542 | } | |
543 | } | |
544 | else | |
545 | if (node->global.inlined_to) | |
546 | { | |
547 | error ("Inlined_to pointer set for noninline callers"); | |
548 | error_found = true; | |
549 | } | |
550 | } | |
551 | if (!node->callers && node->global.inlined_to) | |
552 | { | |
553 | error ("Inlined_to pointer is set but no predecesors found"); | |
554 | error_found = true; | |
555 | } | |
556 | if (node->global.inlined_to == node) | |
557 | { | |
558 | error ("Inlined_to pointer reffers to itself"); | |
559 | error_found = true; | |
560 | } | |
561 | ||
562 | for (main_clone = cgraph_node (node->decl); main_clone; | |
563 | main_clone = main_clone->next_clone) | |
564 | if (main_clone == node) | |
565 | break; | |
566 | if (!node) | |
567 | { | |
568 | error ("Node not found in DECL_ASSEMBLER_NAME hash"); | |
569 | error_found = true; | |
570 | } | |
571 | ||
572 | if (node->analyzed | |
573 | && DECL_SAVED_TREE (node->decl) && !TREE_ASM_WRITTEN (node->decl) | |
574 | && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to)) | |
575 | { | |
576 | walk_tree_without_duplicates (&DECL_SAVED_TREE (node->decl), | |
577 | verify_cgraph_node_1, node); | |
578 | for (e = node->callees; e; e = e->next_callee) | |
579 | { | |
580 | if (!e->aux) | |
581 | { | |
582 | error ("Edge %s->%s has no corresponding call_expr", | |
583 | cgraph_node_name (e->caller), | |
584 | cgraph_node_name (e->callee)); | |
585 | error_found = true; | |
586 | } | |
587 | e->aux = 0; | |
588 | } | |
589 | } | |
590 | if (error_found) | |
591 | { | |
592 | dump_cgraph_node (stderr, node); | |
593 | internal_error ("verify_cgraph_node failed."); | |
594 | } | |
595 | timevar_pop (TV_CGRAPH_VERIFY); | |
596 | } | |
597 | ||
598 | /* Verify whole cgraph structure. */ | |
599 | void | |
600 | verify_cgraph (void) | |
601 | { | |
602 | struct cgraph_node *node; | |
603 | ||
604 | for (node = cgraph_nodes; node; node = node->next) | |
605 | verify_cgraph_node (node); | |
606 | } | |
607 | ||
e767b5be JH |
608 | /* Analyze the function scheduled to be output. */ |
609 | static void | |
610 | cgraph_analyze_function (struct cgraph_node *node) | |
611 | { | |
612 | tree decl = node->decl; | |
dc0bfe6a | 613 | struct cgraph_edge *e; |
e767b5be | 614 | |
25c84396 | 615 | current_function_decl = decl; |
e767b5be JH |
616 | |
617 | /* First kill forward declaration so reverse inlining works properly. */ | |
18c6ada9 | 618 | cgraph_create_edges (node, DECL_SAVED_TREE (decl)); |
e767b5be JH |
619 | |
620 | node->local.inlinable = tree_inlinable_function_p (decl); | |
a6f78652 ZW |
621 | if (!node->local.self_insns) |
622 | node->local.self_insns | |
ae2bcd98 | 623 | = lang_hooks.tree_inlining.estimate_num_insns (decl); |
e767b5be JH |
624 | if (node->local.inlinable) |
625 | node->local.disregard_inline_limits | |
ae2bcd98 | 626 | = lang_hooks.tree_inlining.disregard_inline_limits (decl); |
dc0bfe6a | 627 | for (e = node->callers; e; e = e->next_caller) |
18c6ada9 JH |
628 | { |
629 | if (node->local.redefined_extern_inline) | |
630 | e->inline_failed = N_("redefined extern inline functions are not " | |
631 | "considered for inlining"); | |
632 | else if (!node->local.inlinable) | |
633 | e->inline_failed = N_("function not inlinable"); | |
634 | else | |
635 | e->inline_failed = N_("function not considered for inlining"); | |
636 | } | |
b684a3df JH |
637 | if (flag_really_no_inline && !node->local.disregard_inline_limits) |
638 | node->local.inlinable = 0; | |
e767b5be JH |
639 | /* Inlining characteristics are maintained by the cgraph_mark_inline. */ |
640 | node->global.insns = node->local.self_insns; | |
e767b5be | 641 | |
25c84396 | 642 | node->analyzed = true; |
d853a20e | 643 | current_function_decl = NULL; |
e767b5be JH |
644 | } |
645 | ||
1c4a429a JH |
646 | /* Analyze the whole compilation unit once it is parsed completely. */ |
647 | ||
648 | void | |
db0e878d | 649 | cgraph_finalize_compilation_unit (void) |
1c4a429a JH |
650 | { |
651 | struct cgraph_node *node; | |
1c4a429a | 652 | |
4a46cbfb | 653 | if (!flag_unit_at_a_time) |
d853a20e JH |
654 | { |
655 | cgraph_assemble_pending_functions (); | |
656 | return; | |
657 | } | |
4a46cbfb | 658 | |
e69529cd | 659 | cgraph_varpool_assemble_pending_decls (); |
b58b1157 JH |
660 | if (!quiet_flag) |
661 | fprintf (stderr, "\nAnalyzing compilation unit\n"); | |
e69529cd | 662 | |
a194aa56 JH |
663 | timevar_push (TV_CGRAPH); |
664 | if (cgraph_dump_file) | |
1c4a429a | 665 | { |
7d82fe7c | 666 | fprintf (cgraph_dump_file, "Initial entry points:"); |
1668aabc JH |
667 | for (node = cgraph_nodes; node; node = node->next) |
668 | if (node->needed && DECL_SAVED_TREE (node->decl)) | |
a194aa56 JH |
669 | fprintf (cgraph_dump_file, " %s", cgraph_node_name (node)); |
670 | fprintf (cgraph_dump_file, "\n"); | |
1c4a429a JH |
671 | } |
672 | ||
7660e67e SB |
673 | /* Propagate reachability flag and lower representation of all reachable |
674 | functions. In the future, lowering will introduce new functions and | |
675 | new entry points on the way (by template instantiation and virtual | |
676 | method table generation for instance). */ | |
1668aabc | 677 | while (cgraph_nodes_queue) |
1c4a429a | 678 | { |
e767b5be | 679 | struct cgraph_edge *edge; |
1668aabc JH |
680 | tree decl = cgraph_nodes_queue->decl; |
681 | ||
682 | node = cgraph_nodes_queue; | |
8bd87c4e | 683 | cgraph_nodes_queue = cgraph_nodes_queue->next_needed; |
18c6ada9 | 684 | node->next_needed = NULL; |
1c4a429a | 685 | |
cd4dea62 | 686 | /* ??? It is possible to create extern inline function and later using |
d1a6adeb | 687 | weak alas attribute to kill its body. See |
cd4dea62 JH |
688 | gcc.c-torture/compile/20011119-1.c */ |
689 | if (!DECL_SAVED_TREE (decl)) | |
690 | continue; | |
691 | ||
25c84396 | 692 | if (node->analyzed || !node->reachable || !DECL_SAVED_TREE (decl)) |
1c4a429a JH |
693 | abort (); |
694 | ||
e767b5be | 695 | cgraph_analyze_function (node); |
8dafba3c | 696 | |
1c4a429a | 697 | for (edge = node->callees; edge; edge = edge->next_callee) |
e767b5be | 698 | if (!edge->callee->reachable) |
8dafba3c RH |
699 | cgraph_mark_reachable_node (edge->callee); |
700 | ||
e69529cd | 701 | cgraph_varpool_assemble_pending_decls (); |
1c4a429a | 702 | } |
8dafba3c | 703 | |
1668aabc JH |
704 | /* Collect entry points to the unit. */ |
705 | ||
a194aa56 | 706 | if (cgraph_dump_file) |
1668aabc | 707 | { |
7d82fe7c | 708 | fprintf (cgraph_dump_file, "Unit entry points:"); |
1668aabc JH |
709 | for (node = cgraph_nodes; node; node = node->next) |
710 | if (node->needed && DECL_SAVED_TREE (node->decl)) | |
a194aa56 | 711 | fprintf (cgraph_dump_file, " %s", cgraph_node_name (node)); |
7d82fe7c | 712 | fprintf (cgraph_dump_file, "\n\nInitial "); |
e767b5be | 713 | dump_cgraph (cgraph_dump_file); |
1668aabc | 714 | } |
7660e67e | 715 | |
a194aa56 JH |
716 | if (cgraph_dump_file) |
717 | fprintf (cgraph_dump_file, "\nReclaiming functions:"); | |
1c4a429a JH |
718 | |
719 | for (node = cgraph_nodes; node; node = node->next) | |
720 | { | |
721 | tree decl = node->decl; | |
722 | ||
723 | if (!node->reachable && DECL_SAVED_TREE (decl)) | |
724 | { | |
a194aa56 JH |
725 | if (cgraph_dump_file) |
726 | fprintf (cgraph_dump_file, " %s", cgraph_node_name (node)); | |
18c6ada9 | 727 | cgraph_remove_node (node); |
1c4a429a | 728 | } |
9b0436b7 JH |
729 | else |
730 | node->next_needed = NULL; | |
1c4a429a | 731 | } |
a194aa56 | 732 | if (cgraph_dump_file) |
7d82fe7c KC |
733 | { |
734 | fprintf (cgraph_dump_file, "\n\nReclaimed "); | |
735 | dump_cgraph (cgraph_dump_file); | |
736 | } | |
1c4a429a | 737 | ggc_collect (); |
a194aa56 | 738 | timevar_pop (TV_CGRAPH); |
1c4a429a JH |
739 | } |
740 | ||
741 | /* Figure out what functions we want to assemble. */ | |
742 | ||
743 | static void | |
db0e878d | 744 | cgraph_mark_functions_to_output (void) |
1c4a429a JH |
745 | { |
746 | struct cgraph_node *node; | |
747 | ||
1c4a429a JH |
748 | for (node = cgraph_nodes; node; node = node->next) |
749 | { | |
750 | tree decl = node->decl; | |
b58b1157 | 751 | struct cgraph_edge *e; |
dc0bfe6a | 752 | |
b58b1157 JH |
753 | if (node->output) |
754 | abort (); | |
755 | ||
756 | for (e = node->callers; e; e = e->next_caller) | |
dc0bfe6a | 757 | if (e->inline_failed) |
b58b1157 | 758 | break; |
1c4a429a | 759 | |
7660e67e SB |
760 | /* We need to output all local functions that are used and not |
761 | always inlined, as well as those that are reachable from | |
762 | outside the current compilation unit. */ | |
1c4a429a | 763 | if (DECL_SAVED_TREE (decl) |
18c6ada9 | 764 | && !node->global.inlined_to |
1c4a429a | 765 | && (node->needed |
b58b1157 | 766 | || (e && node->reachable)) |
1c4a429a JH |
767 | && !TREE_ASM_WRITTEN (decl) && !node->origin |
768 | && !DECL_EXTERNAL (decl)) | |
769 | node->output = 1; | |
18c6ada9 JH |
770 | /* We should've reclaimed all functions that are not needed. */ |
771 | else if (!node->global.inlined_to && DECL_SAVED_TREE (decl) | |
772 | && !node->origin && !DECL_EXTERNAL (decl)) | |
773 | { | |
774 | dump_cgraph_node (stderr, node); | |
775 | abort (); | |
776 | } | |
18d13f34 JH |
777 | } |
778 | } | |
779 | ||
1c4a429a | 780 | /* Expand function specified by NODE. */ |
7660e67e | 781 | |
1c4a429a | 782 | static void |
db0e878d | 783 | cgraph_expand_function (struct cgraph_node *node) |
1c4a429a JH |
784 | { |
785 | tree decl = node->decl; | |
786 | ||
18c6ada9 JH |
787 | /* We ought to not compile any inline clones. */ |
788 | if (node->global.inlined_to) | |
789 | abort (); | |
790 | ||
6b00c969 RH |
791 | if (flag_unit_at_a_time) |
792 | announce_function (decl); | |
18d13f34 | 793 | |
7660e67e SB |
794 | /* Generate RTL for the body of DECL. Nested functions are expanded |
795 | via lang_expand_decl_stmt. */ | |
ae2bcd98 | 796 | lang_hooks.callgraph.expand_function (decl); |
d173e685 JH |
797 | if (DECL_DEFER_OUTPUT (decl)) |
798 | abort (); | |
18d13f34 | 799 | |
18c6ada9 JH |
800 | /* Make sure that BE didn't gave up on compiling. */ |
801 | if (!TREE_ASM_WRITTEN (node->decl) | |
802 | && !(sorrycount || errorcount)) | |
803 | abort (); | |
804 | ||
1c4a429a JH |
805 | current_function_decl = NULL; |
806 | } | |
807 | ||
b58b1157 JH |
808 | /* Fill array order with all nodes with output flag set in the reverse |
809 | topological order. */ | |
dc0bfe6a | 810 | |
b58b1157 JH |
811 | static int |
812 | cgraph_postorder (struct cgraph_node **order) | |
1c4a429a JH |
813 | { |
814 | struct cgraph_node *node, *node2; | |
1c4a429a JH |
815 | int stack_size = 0; |
816 | int order_pos = 0; | |
817 | struct cgraph_edge *edge, last; | |
1c4a429a | 818 | |
b58b1157 | 819 | struct cgraph_node **stack = |
b3c3af2f | 820 | xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *)); |
1c4a429a | 821 | |
7660e67e SB |
822 | /* We have to deal with cycles nicely, so use a depth first traversal |
823 | output algorithm. Ignore the fact that some functions won't need | |
824 | to be output and put them into order as well, so we get dependencies | |
e0cc7f73 | 825 | right throughout inline functions. */ |
1c4a429a JH |
826 | for (node = cgraph_nodes; node; node = node->next) |
827 | node->aux = NULL; | |
828 | for (node = cgraph_nodes; node; node = node->next) | |
1668aabc | 829 | if (!node->aux) |
1c4a429a JH |
830 | { |
831 | node2 = node; | |
832 | if (!node->callers) | |
833 | node->aux = &last; | |
834 | else | |
835 | node->aux = node->callers; | |
836 | while (node2) | |
837 | { | |
838 | while (node2->aux != &last) | |
839 | { | |
840 | edge = node2->aux; | |
841 | if (edge->next_caller) | |
842 | node2->aux = edge->next_caller; | |
843 | else | |
844 | node2->aux = &last; | |
845 | if (!edge->caller->aux) | |
846 | { | |
847 | if (!edge->caller->callers) | |
848 | edge->caller->aux = &last; | |
849 | else | |
850 | edge->caller->aux = edge->caller->callers; | |
851 | stack[stack_size++] = node2; | |
852 | node2 = edge->caller; | |
853 | break; | |
854 | } | |
855 | } | |
856 | if (node2->aux == &last) | |
857 | { | |
858 | order[order_pos++] = node2; | |
859 | if (stack_size) | |
860 | node2 = stack[--stack_size]; | |
861 | else | |
862 | node2 = NULL; | |
863 | } | |
864 | } | |
865 | } | |
b58b1157 JH |
866 | free (stack); |
867 | return order_pos; | |
868 | } | |
869 | ||
9b0436b7 JH |
870 | /* Perform reachability analysis and reclaim all unreachable nodes. |
871 | This function also remove unneeded bodies of extern inline functions | |
872 | and thus needs to be done only after inlining decisions has been made. */ | |
873 | static bool | |
874 | cgraph_remove_unreachable_nodes (void) | |
875 | { | |
876 | struct cgraph_node *first = (void *) 1; | |
877 | struct cgraph_node *node; | |
878 | bool changed = false; | |
879 | int insns = 0; | |
880 | ||
18c6ada9 JH |
881 | #ifdef ENABLE_CHECKING |
882 | verify_cgraph (); | |
883 | #endif | |
9b0436b7 JH |
884 | if (cgraph_dump_file) |
885 | fprintf (cgraph_dump_file, "\nReclaiming functions:"); | |
886 | #ifdef ENABLE_CHECKING | |
887 | for (node = cgraph_nodes; node; node = node->next) | |
888 | if (node->aux) | |
889 | abort (); | |
890 | #endif | |
891 | for (node = cgraph_nodes; node; node = node->next) | |
892 | if (node->needed && (!DECL_EXTERNAL (node->decl) || !node->analyzed)) | |
893 | { | |
894 | node->aux = first; | |
895 | first = node; | |
896 | } | |
897 | else if (node->aux) | |
898 | abort (); | |
899 | ||
900 | /* Perform reachability analysis. As a special case do not consider | |
901 | extern inline functions not inlined as live because we won't output | |
902 | them at all. */ | |
903 | while (first != (void *) 1) | |
904 | { | |
905 | struct cgraph_edge *e; | |
906 | node = first; | |
907 | first = first->aux; | |
908 | ||
909 | for (e = node->callees; e; e = e->next_callee) | |
910 | if (!e->callee->aux | |
911 | && node->analyzed | |
912 | && (!e->inline_failed || !e->callee->analyzed | |
913 | || !DECL_EXTERNAL (e->callee->decl))) | |
914 | { | |
915 | e->callee->aux = first; | |
916 | first = e->callee; | |
917 | } | |
918 | } | |
919 | ||
920 | /* Remove unreachable nodes. Extern inline functions need special care; | |
921 | Unreachable extern inline functions shall be removed. | |
922 | Reachable extern inline functions we never inlined shall get their bodies | |
18c6ada9 | 923 | eliminated |
9b0436b7 JH |
924 | Reachable extern inline functions we sometimes inlined will be turned into |
925 | unanalyzed nodes so they look like for true extern functions to the rest | |
2b8a92de | 926 | of code. Body of such functions is released via remove_node once the |
18c6ada9 | 927 | inline clones are eliminated. */ |
9b0436b7 JH |
928 | for (node = cgraph_nodes; node; node = node->next) |
929 | { | |
930 | if (!node->aux) | |
931 | { | |
932 | int local_insns; | |
933 | tree decl = node->decl; | |
934 | ||
1da326c3 | 935 | if (DECL_STRUCT_FUNCTION (decl)) |
9b0436b7 JH |
936 | local_insns = node->local.self_insns; |
937 | else | |
938 | local_insns = 0; | |
939 | if (cgraph_dump_file) | |
940 | fprintf (cgraph_dump_file, " %s", cgraph_node_name (node)); | |
941 | if (!node->analyzed || !DECL_EXTERNAL (node->decl)) | |
942 | cgraph_remove_node (node); | |
943 | else | |
944 | { | |
945 | struct cgraph_edge *e; | |
946 | ||
947 | for (e = node->callers; e; e = e->next_caller) | |
948 | if (e->caller->aux) | |
949 | break; | |
950 | if (e || node->needed) | |
951 | { | |
18c6ada9 JH |
952 | struct cgraph_node *clone; |
953 | ||
954 | for (clone = node->next_clone; clone; | |
955 | clone = clone->next_clone) | |
956 | if (clone->aux) | |
957 | break; | |
958 | if (!clone) | |
959 | { | |
960 | DECL_SAVED_TREE (node->decl) = NULL; | |
961 | DECL_STRUCT_FUNCTION (node->decl) = NULL; | |
962 | DECL_ARGUMENTS (node->decl) = NULL; | |
963 | DECL_INITIAL (node->decl) = error_mark_node; | |
964 | } | |
9b0436b7 | 965 | while (node->callees) |
18c6ada9 | 966 | cgraph_remove_edge (node->callees); |
9b0436b7 JH |
967 | node->analyzed = false; |
968 | } | |
969 | else | |
970 | cgraph_remove_node (node); | |
971 | } | |
972 | if (!DECL_SAVED_TREE (decl)) | |
973 | insns += local_insns; | |
974 | changed = true; | |
975 | } | |
976 | } | |
977 | for (node = cgraph_nodes; node; node = node->next) | |
978 | node->aux = NULL; | |
979 | if (cgraph_dump_file) | |
980 | fprintf (cgraph_dump_file, "\nReclaimed %i insns", insns); | |
981 | return changed; | |
982 | } | |
983 | ||
b58b1157 JH |
984 | /* Estimate size of the function after inlining WHAT into TO. */ |
985 | ||
986 | static int | |
db0e878d | 987 | cgraph_estimate_size_after_inlining (int times, struct cgraph_node *to, |
b58b1157 JH |
988 | struct cgraph_node *what) |
989 | { | |
7d82fe7c | 990 | return (what->global.insns - INSNS_PER_CALL) * times + to->global.insns; |
b58b1157 JH |
991 | } |
992 | ||
993 | /* Estimate the growth caused by inlining NODE into all callees. */ | |
994 | ||
995 | static int | |
996 | cgraph_estimate_growth (struct cgraph_node *node) | |
997 | { | |
998 | int growth = 0; | |
b58b1157 JH |
999 | struct cgraph_edge *e; |
1000 | ||
1001 | for (e = node->callers; e; e = e->next_caller) | |
dc0bfe6a | 1002 | if (e->inline_failed) |
18c6ada9 JH |
1003 | growth += (cgraph_estimate_size_after_inlining (1, e->caller, node) |
1004 | - e->caller->global.insns); | |
b58b1157 JH |
1005 | |
1006 | /* ??? Wrong for self recursive functions or cases where we decide to not | |
1007 | inline for different reasons, but it is not big deal as in that case | |
1008 | we will keep the body around, but we will also avoid some inlining. */ | |
1009 | if (!node->needed && !node->origin && !DECL_EXTERNAL (node->decl)) | |
18c6ada9 | 1010 | growth -= node->global.insns; |
b58b1157 JH |
1011 | |
1012 | return growth; | |
1013 | } | |
1014 | ||
18c6ada9 JH |
1015 | /* E is expected to be an edge being inlined. Clone destination node of |
1016 | the edge and redirect it to the new clone. | |
1017 | DUPLICATE is used for bookeeping on whether we are actually creating new | |
1018 | clones or re-using node originally representing out-of-line function call. | |
1019 | */ | |
1020 | void | |
1021 | cgraph_clone_inlined_nodes (struct cgraph_edge *e, bool duplicate) | |
1022 | { | |
1023 | struct cgraph_node *n; | |
1024 | ||
1025 | /* We may elliminate the need for out-of-line copy to be output. In that | |
1026 | case just go ahead and re-use it. */ | |
1027 | if (!e->callee->callers->next_caller | |
1028 | && (!e->callee->needed || DECL_EXTERNAL (e->callee->decl)) | |
1029 | && !e->callee->origin | |
1030 | && duplicate | |
1031 | && flag_unit_at_a_time) | |
1032 | { | |
1033 | if (e->callee->global.inlined_to) | |
1034 | abort (); | |
1035 | if (!DECL_EXTERNAL (e->callee->decl)) | |
1036 | overall_insns -= e->callee->global.insns, nfunctions_inlined++; | |
1037 | duplicate = 0; | |
1038 | } | |
1039 | else if (duplicate) | |
1040 | { | |
1041 | n = cgraph_clone_node (e->callee); | |
1042 | cgraph_redirect_edge_callee (e, n); | |
1043 | } | |
dafc5b82 | 1044 | |
18c6ada9 JH |
1045 | if (e->caller->global.inlined_to) |
1046 | e->callee->global.inlined_to = e->caller->global.inlined_to; | |
1047 | else | |
1048 | e->callee->global.inlined_to = e->caller; | |
1049 | ||
2b8a92de | 1050 | /* Recursively clone all bodies. */ |
18c6ada9 JH |
1051 | for (e = e->callee->callees; e; e = e->next_callee) |
1052 | if (!e->inline_failed) | |
1053 | cgraph_clone_inlined_nodes (e, duplicate); | |
1054 | } | |
1055 | ||
1056 | /* Mark edge E as inlined and update callgraph accordingly. */ | |
1057 | ||
1058 | void | |
1059 | cgraph_mark_inline_edge (struct cgraph_edge *e) | |
b58b1157 | 1060 | { |
18c6ada9 JH |
1061 | int old_insns = 0, new_insns = 0; |
1062 | struct cgraph_node *to = NULL, *what; | |
1063 | ||
1064 | if (!e->inline_failed) | |
1065 | abort (); | |
1066 | e->inline_failed = NULL; | |
b58b1157 | 1067 | |
18c6ada9 | 1068 | if (!e->callee->global.inlined && flag_unit_at_a_time) |
b58b1157 | 1069 | { |
18c6ada9 JH |
1070 | void **slot; |
1071 | if (!cgraph_inline_hash) | |
1072 | cgraph_inline_hash = htab_create_ggc (42, htab_hash_pointer, | |
1073 | htab_eq_pointer, NULL); | |
1074 | slot = htab_find_slot (cgraph_inline_hash, | |
1075 | DECL_ASSEMBLER_NAME (e->callee->decl), INSERT); | |
1076 | *slot = DECL_ASSEMBLER_NAME (e->callee->decl); | |
b58b1157 | 1077 | } |
18c6ada9 JH |
1078 | e->callee->global.inlined = true; |
1079 | ||
1080 | cgraph_clone_inlined_nodes (e, true); | |
b58b1157 | 1081 | |
18c6ada9 | 1082 | what = e->callee; |
b58b1157 | 1083 | |
18c6ada9 JH |
1084 | /* Now update size of caller and all functions caller is inlined into. */ |
1085 | for (;e && !e->inline_failed; e = e->caller->callers) | |
b58b1157 | 1086 | { |
18c6ada9 JH |
1087 | old_insns = e->caller->global.insns; |
1088 | new_insns = cgraph_estimate_size_after_inlining (1, e->caller, | |
1089 | what); | |
1090 | if (new_insns < 0) | |
b58b1157 | 1091 | abort (); |
18c6ada9 JH |
1092 | to = e->caller; |
1093 | to->global.insns = new_insns; | |
b58b1157 | 1094 | } |
18c6ada9 JH |
1095 | if (what->global.inlined_to != to) |
1096 | abort (); | |
1097 | overall_insns += new_insns - old_insns; | |
1098 | ncalls_inlined++; | |
1099 | } | |
1100 | ||
1101 | /* Mark all calls of EDGE->CALLEE inlined into EDGE->CALLER. | |
1102 | Return following unredirected edge in the list of callers | |
1103 | of EDGE->CALLEE */ | |
1104 | ||
1105 | static struct cgraph_edge * | |
1106 | cgraph_mark_inline (struct cgraph_edge *edge) | |
1107 | { | |
1108 | struct cgraph_node *to = edge->caller; | |
1109 | struct cgraph_node *what = edge->callee; | |
1110 | struct cgraph_edge *e, *next; | |
1111 | int times = 0; | |
1112 | ||
2b8a92de | 1113 | /* Look for all calls, mark them inline and clone recursively |
18c6ada9 JH |
1114 | all inlined functions. */ |
1115 | for (e = what->callers; e; e = next) | |
b58b1157 | 1116 | { |
18c6ada9 JH |
1117 | next = e->next_caller; |
1118 | if (e->caller == to && e->inline_failed) | |
1119 | { | |
1120 | cgraph_mark_inline_edge (e); | |
1121 | if (e == edge) | |
1122 | edge = next; | |
1123 | times ++; | |
1124 | } | |
b58b1157 | 1125 | } |
18c6ada9 JH |
1126 | if (!times) |
1127 | abort (); | |
1128 | return edge; | |
b58b1157 JH |
1129 | } |
1130 | ||
18c6ada9 JH |
1131 | /* Return false when inlining WHAT into TO is not good idea |
1132 | as it would cause too large growth of function bodies. */ | |
b58b1157 JH |
1133 | |
1134 | static bool | |
db0e878d | 1135 | cgraph_check_inline_limits (struct cgraph_node *to, struct cgraph_node *what, |
dc0bfe6a | 1136 | const char **reason) |
dafc5b82 | 1137 | { |
b58b1157 JH |
1138 | int times = 0; |
1139 | struct cgraph_edge *e; | |
1140 | int newsize; | |
1141 | int limit; | |
1142 | ||
18c6ada9 JH |
1143 | if (to->global.inlined_to) |
1144 | to = to->global.inlined_to; | |
1145 | ||
b58b1157 JH |
1146 | for (e = to->callees; e; e = e->next_callee) |
1147 | if (e->callee == what) | |
1148 | times++; | |
1149 | ||
1150 | /* When inlining large function body called once into small function, | |
1151 | take the inlined function as base for limiting the growth. */ | |
1152 | if (to->local.self_insns > what->local.self_insns) | |
1153 | limit = to->local.self_insns; | |
1154 | else | |
1155 | limit = what->local.self_insns; | |
1156 | ||
1157 | limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100; | |
1158 | ||
1159 | newsize = cgraph_estimate_size_after_inlining (times, to, what); | |
1160 | if (newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS) | |
1161 | && newsize > limit) | |
dc0bfe6a | 1162 | { |
18c6ada9 JH |
1163 | if (reason) |
1164 | *reason = N_("--param large-function-growth limit reached"); | |
dc0bfe6a JH |
1165 | return false; |
1166 | } | |
b58b1157 JH |
1167 | return true; |
1168 | } | |
1169 | ||
7d82fe7c | 1170 | /* Return true when function N is small enough to be inlined. */ |
b58b1157 JH |
1171 | |
1172 | static bool | |
1173 | cgraph_default_inline_p (struct cgraph_node *n) | |
1174 | { | |
1175 | if (!DECL_INLINE (n->decl) || !DECL_SAVED_TREE (n->decl)) | |
1176 | return false; | |
b3c3af2f | 1177 | if (DECL_DECLARED_INLINE_P (n->decl)) |
b58b1157 | 1178 | return n->global.insns < MAX_INLINE_INSNS_SINGLE; |
b3c3af2f SB |
1179 | else |
1180 | return n->global.insns < MAX_INLINE_INSNS_AUTO; | |
b58b1157 JH |
1181 | } |
1182 | ||
18c6ada9 JH |
1183 | /* Return true when inlining WHAT would create recursive inlining. |
1184 | We call recursive inlining all cases where same function appears more than | |
2b8a92de | 1185 | once in the single recursion nest path in the inline graph. */ |
18c6ada9 JH |
1186 | |
1187 | static bool | |
1188 | cgraph_recursive_inlining_p (struct cgraph_node *to, | |
1189 | struct cgraph_node *what, | |
1190 | const char **reason) | |
1191 | { | |
1192 | struct cgraph_node *node; | |
1193 | ||
1194 | /* Walk TO and all functions TO is inlined in. */ | |
1195 | while (1) | |
1196 | { | |
1197 | /* We create recursive inlining either by inlining WHAT into something | |
1198 | already inlined in possibly different clone of WHAT. */ | |
1199 | if (what->decl == to->decl) | |
1200 | goto recursive; | |
1201 | /* Or by inlining WHAT into something that is already inlined in WHAT. */ | |
1202 | for (node = cgraph_node (to->decl); node; node = node->next_clone) | |
1203 | if (node->global.inlined_to == what) | |
1204 | goto recursive; | |
1205 | if (!to->callers || to->callers->inline_failed) | |
1206 | return false; | |
1207 | to = to->callers->caller; | |
1208 | } | |
1209 | recursive: | |
1210 | if (reason) | |
1211 | *reason = (what->local.disregard_inline_limits | |
1212 | ? N_("recursive inlining") : ""); | |
1213 | return true; | |
1214 | } | |
1215 | ||
1216 | /* Recompute heap nodes for each of callees. */ | |
1217 | static void | |
1218 | update_callee_keys (fibheap_t heap, struct fibnode **heap_node, | |
1219 | struct cgraph_node *node) | |
1220 | { | |
1221 | struct cgraph_edge *e; | |
1222 | ||
1223 | for (e = node->callees; e; e = e->next_callee) | |
1224 | if (e->inline_failed && heap_node[e->callee->uid]) | |
1225 | fibheap_replace_key (heap, heap_node[e->callee->uid], | |
1226 | cgraph_estimate_growth (e->callee)); | |
1227 | else if (!e->inline_failed) | |
1228 | update_callee_keys (heap, heap_node, e->callee); | |
1229 | } | |
1230 | ||
dc0bfe6a JH |
1231 | /* Set inline_failed for all callers of given function to REASON. */ |
1232 | ||
1233 | static void | |
1234 | cgraph_set_inline_failed (struct cgraph_node *node, const char *reason) | |
1235 | { | |
1236 | struct cgraph_edge *e; | |
1237 | ||
1238 | if (cgraph_dump_file) | |
1239 | fprintf (cgraph_dump_file, "Inlining failed: %s\n", reason); | |
1240 | for (e = node->callers; e; e = e->next_caller) | |
1241 | if (e->inline_failed) | |
1242 | e->inline_failed = reason; | |
1243 | } | |
1244 | ||
b58b1157 JH |
1245 | /* We use greedy algorithm for inlining of small functions: |
1246 | All inline candidates are put into prioritized heap based on estimated | |
1247 | growth of the overall number of instructions and then update the estimates. | |
db0e878d | 1248 | |
d91edf86 | 1249 | INLINED and INLINED_CALEES are just pointers to arrays large enough |
b58b1157 JH |
1250 | to be passed to cgraph_inlined_into and cgraph_inlined_callees. */ |
1251 | ||
1252 | static void | |
18c6ada9 | 1253 | cgraph_decide_inlining_of_small_functions (void) |
b58b1157 | 1254 | { |
dafc5b82 | 1255 | struct cgraph_node *node; |
b58b1157 JH |
1256 | fibheap_t heap = fibheap_new (); |
1257 | struct fibnode **heap_node = | |
b3c3af2f | 1258 | xcalloc (cgraph_max_uid, sizeof (struct fibnode *)); |
b58b1157 JH |
1259 | int max_insns = ((HOST_WIDEST_INT) initial_insns |
1260 | * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100); | |
dafc5b82 | 1261 | |
b58b1157 | 1262 | /* Put all inline candidates into the heap. */ |
dafc5b82 | 1263 | |
dafc5b82 JH |
1264 | for (node = cgraph_nodes; node; node = node->next) |
1265 | { | |
b58b1157 | 1266 | if (!node->local.inlinable || !node->callers |
dc0bfe6a | 1267 | || node->local.disregard_inline_limits) |
b58b1157 JH |
1268 | continue; |
1269 | ||
dc0bfe6a JH |
1270 | if (!cgraph_default_inline_p (node)) |
1271 | { | |
1272 | cgraph_set_inline_failed (node, | |
1273 | N_("--param max-inline-insns-single limit reached")); | |
1274 | continue; | |
1275 | } | |
b58b1157 JH |
1276 | heap_node[node->uid] = |
1277 | fibheap_insert (heap, cgraph_estimate_growth (node), node); | |
dafc5b82 | 1278 | } |
b58b1157 | 1279 | |
a194aa56 | 1280 | if (cgraph_dump_file) |
7d82fe7c | 1281 | fprintf (cgraph_dump_file, "\nDeciding on smaller functions:\n"); |
dc0bfe6a | 1282 | while (overall_insns <= max_insns && (node = fibheap_extract_min (heap))) |
b58b1157 | 1283 | { |
18c6ada9 | 1284 | struct cgraph_edge *e, *next; |
b58b1157 JH |
1285 | int old_insns = overall_insns; |
1286 | ||
1287 | heap_node[node->uid] = NULL; | |
1288 | if (cgraph_dump_file) | |
7d82fe7c KC |
1289 | fprintf (cgraph_dump_file, |
1290 | "\nConsidering %s with %i insns\n" | |
1291 | " Estimated growth is %+i insns.\n", | |
b58b1157 JH |
1292 | cgraph_node_name (node), node->global.insns, |
1293 | cgraph_estimate_growth (node)); | |
1294 | if (!cgraph_default_inline_p (node)) | |
1295 | { | |
dc0bfe6a JH |
1296 | cgraph_set_inline_failed (node, |
1297 | N_("--param max-inline-insns-single limit reached after inlining into the callee")); | |
b58b1157 JH |
1298 | continue; |
1299 | } | |
18c6ada9 JH |
1300 | for (e = node->callers; e; e = next) |
1301 | { | |
1302 | next = e->next_caller; | |
1303 | if (e->inline_failed) | |
1304 | { | |
1305 | struct cgraph_node *where; | |
1306 | ||
1307 | if (cgraph_recursive_inlining_p (e->caller, e->callee, | |
1308 | &e->inline_failed) | |
1309 | || !cgraph_check_inline_limits (e->caller, e->callee, | |
1310 | &e->inline_failed)) | |
1311 | { | |
1312 | if (cgraph_dump_file) | |
1313 | fprintf (cgraph_dump_file, " Not inlining into %s:%s.\n", | |
1314 | cgraph_node_name (e->caller), e->inline_failed); | |
1315 | continue; | |
1316 | } | |
1317 | next = cgraph_mark_inline (e); | |
1318 | where = e->caller; | |
1319 | if (where->global.inlined_to) | |
1320 | where = where->global.inlined_to; | |
1321 | ||
1322 | if (heap_node[where->uid]) | |
1323 | fibheap_replace_key (heap, heap_node[where->uid], | |
1324 | cgraph_estimate_growth (where)); | |
1325 | ||
1326 | if (cgraph_dump_file) | |
1327 | fprintf (cgraph_dump_file, | |
1328 | " Inlined into %s which now has %i insns.\n", | |
1329 | cgraph_node_name (e->caller), | |
1330 | e->caller->global.insns); | |
1331 | } | |
1332 | } | |
b58b1157 | 1333 | |
7d82fe7c | 1334 | /* Similarly all functions called by the function we just inlined |
b58b1157 | 1335 | are now called more times; update keys. */ |
18c6ada9 | 1336 | update_callee_keys (heap, heap_node, node); |
b58b1157 | 1337 | |
b58b1157 | 1338 | if (cgraph_dump_file) |
7d82fe7c | 1339 | fprintf (cgraph_dump_file, |
18c6ada9 JH |
1340 | " Inlined for a net change of %+i insns.\n", |
1341 | overall_insns - old_insns); | |
b58b1157 | 1342 | } |
dc0bfe6a JH |
1343 | while ((node = fibheap_extract_min (heap)) != NULL) |
1344 | if (!node->local.disregard_inline_limits) | |
1345 | cgraph_set_inline_failed (node, N_("--param inline-unit-growth limit reached")); | |
b58b1157 JH |
1346 | fibheap_delete (heap); |
1347 | free (heap_node); | |
dafc5b82 JH |
1348 | } |
1349 | ||
b58b1157 JH |
1350 | /* Decide on the inlining. We do so in the topological order to avoid |
1351 | expenses on updating datastructures. */ | |
18d13f34 JH |
1352 | |
1353 | static void | |
b58b1157 | 1354 | cgraph_decide_inlining (void) |
18d13f34 | 1355 | { |
b58b1157 JH |
1356 | struct cgraph_node *node; |
1357 | int nnodes; | |
1358 | struct cgraph_node **order = | |
b3c3af2f | 1359 | xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *)); |
de006bbd | 1360 | int old_insns = 0; |
18c6ada9 | 1361 | int i; |
18d13f34 | 1362 | |
b58b1157 | 1363 | for (node = cgraph_nodes; node; node = node->next) |
e767b5be | 1364 | initial_insns += node->local.self_insns; |
b58b1157 JH |
1365 | overall_insns = initial_insns; |
1366 | ||
1367 | nnodes = cgraph_postorder (order); | |
18d13f34 | 1368 | |
7d82fe7c KC |
1369 | if (cgraph_dump_file) |
1370 | fprintf (cgraph_dump_file, | |
1371 | "\nDeciding on inlining. Starting with %i insns.\n", | |
1372 | initial_insns); | |
1373 | ||
18d13f34 | 1374 | for (node = cgraph_nodes; node; node = node->next) |
b58b1157 JH |
1375 | node->aux = 0; |
1376 | ||
1377 | if (cgraph_dump_file) | |
7d82fe7c | 1378 | fprintf (cgraph_dump_file, "\nInlining always_inline functions:\n"); |
b58b1157 JH |
1379 | |
1380 | /* In the first pass mark all always_inline edges. Do this with a priority | |
7d82fe7c | 1381 | so none of our later choices will make this impossible. */ |
b58b1157 JH |
1382 | for (i = nnodes - 1; i >= 0; i--) |
1383 | { | |
1384 | struct cgraph_edge *e; | |
1385 | ||
1386 | node = order[i]; | |
1387 | ||
1388 | for (e = node->callees; e; e = e->next_callee) | |
b3c3af2f | 1389 | if (e->callee->local.disregard_inline_limits) |
b58b1157 JH |
1390 | break; |
1391 | if (!e) | |
1392 | continue; | |
1393 | if (cgraph_dump_file) | |
1394 | fprintf (cgraph_dump_file, | |
7d82fe7c KC |
1395 | "\nConsidering %s %i insns (always inline)\n", |
1396 | cgraph_node_name (e->callee), e->callee->global.insns); | |
b58b1157 JH |
1397 | for (; e; e = e->next_callee) |
1398 | { | |
7d82fe7c | 1399 | old_insns = overall_insns; |
18c6ada9 JH |
1400 | if (!e->inline_failed || !e->callee->local.disregard_inline_limits) |
1401 | continue; | |
1402 | if (cgraph_recursive_inlining_p (order[i], e->callee, | |
1403 | &e->inline_failed)) | |
1404 | continue; | |
1405 | cgraph_mark_inline (e); | |
b58b1157 | 1406 | if (cgraph_dump_file) |
7d82fe7c KC |
1407 | fprintf (cgraph_dump_file, |
1408 | " Inlined into %s which now has %i insns.\n", | |
1409 | cgraph_node_name (node->callees->caller), | |
1410 | node->callees->caller->global.insns); | |
b58b1157 | 1411 | } |
18c6ada9 JH |
1412 | if (cgraph_dump_file) |
1413 | fprintf (cgraph_dump_file, | |
1414 | " Inlined for a net change of %+i insns.\n", | |
1415 | overall_insns - old_insns); | |
b58b1157 JH |
1416 | } |
1417 | ||
b684a3df JH |
1418 | if (!flag_really_no_inline) |
1419 | { | |
18c6ada9 | 1420 | cgraph_decide_inlining_of_small_functions (); |
b58b1157 | 1421 | |
b684a3df JH |
1422 | if (cgraph_dump_file) |
1423 | fprintf (cgraph_dump_file, "\nDeciding on functions called once:\n"); | |
b58b1157 | 1424 | |
b684a3df | 1425 | /* And finally decide what functions are called once. */ |
b58b1157 | 1426 | |
b684a3df | 1427 | for (i = nnodes - 1; i >= 0; i--) |
18d13f34 | 1428 | { |
b684a3df JH |
1429 | node = order[i]; |
1430 | ||
1431 | if (node->callers && !node->callers->next_caller && !node->needed | |
dc0bfe6a | 1432 | && node->local.inlinable && node->callers->inline_failed |
b684a3df | 1433 | && !DECL_EXTERNAL (node->decl) && !DECL_COMDAT (node->decl)) |
18d13f34 | 1434 | { |
b684a3df JH |
1435 | bool ok = true; |
1436 | struct cgraph_node *node1; | |
1437 | ||
1438 | /* Verify that we won't duplicate the caller. */ | |
1439 | for (node1 = node->callers->caller; | |
6242fcd8 | 1440 | node1->callers && !node1->callers->inline_failed |
b684a3df JH |
1441 | && ok; node1 = node1->callers->caller) |
1442 | if (node1->callers->next_caller || node1->needed) | |
1443 | ok = false; | |
1444 | if (ok) | |
b58b1157 | 1445 | { |
b58b1157 | 1446 | if (cgraph_dump_file) |
7d82fe7c | 1447 | fprintf (cgraph_dump_file, |
b684a3df JH |
1448 | "\nConsidering %s %i insns.\n" |
1449 | " Called once from %s %i insns.\n", | |
1450 | cgraph_node_name (node), node->global.insns, | |
7d82fe7c | 1451 | cgraph_node_name (node->callers->caller), |
b684a3df | 1452 | node->callers->caller->global.insns); |
18c6ada9 | 1453 | |
b684a3df | 1454 | old_insns = overall_insns; |
dc0bfe6a | 1455 | |
18c6ada9 JH |
1456 | if (cgraph_check_inline_limits (node->callers->caller, node, |
1457 | NULL)) | |
b684a3df | 1458 | { |
18c6ada9 | 1459 | cgraph_mark_inline (node->callers); |
b684a3df JH |
1460 | if (cgraph_dump_file) |
1461 | fprintf (cgraph_dump_file, | |
1462 | " Inlined into %s which now has %i insns" | |
1463 | " for a net change of %+i insns.\n", | |
1464 | cgraph_node_name (node->callers->caller), | |
1465 | node->callers->caller->global.insns, | |
1466 | overall_insns - old_insns); | |
1467 | } | |
1468 | else | |
1469 | { | |
1470 | if (cgraph_dump_file) | |
1471 | fprintf (cgraph_dump_file, | |
1472 | " Inline limit reached, not inlined.\n"); | |
1473 | } | |
b58b1157 | 1474 | } |
18d13f34 JH |
1475 | } |
1476 | } | |
b684a3df | 1477 | } |
18c6ada9 JH |
1478 | |
1479 | /* We will never output extern functions we didn't inline. | |
1480 | ??? Perhaps we can prevent accounting of growth of external | |
1481 | inline functions. */ | |
9b0436b7 | 1482 | cgraph_remove_unreachable_nodes (); |
8b6bd5d7 JH |
1483 | |
1484 | if (cgraph_dump_file) | |
1485 | fprintf (cgraph_dump_file, | |
1486 | "\nInlined %i calls, eliminated %i functions, " | |
1487 | "%i insns turned to %i insns.\n\n", | |
1488 | ncalls_inlined, nfunctions_inlined, initial_insns, | |
1489 | overall_insns); | |
1490 | free (order); | |
18d13f34 JH |
1491 | } |
1492 | ||
d4d1ebc1 JH |
1493 | /* Decide on the inlining. We do so in the topological order to avoid |
1494 | expenses on updating datastructures. */ | |
1495 | ||
1496 | static void | |
1497 | cgraph_decide_inlining_incrementally (struct cgraph_node *node) | |
1498 | { | |
1499 | struct cgraph_edge *e; | |
d4d1ebc1 JH |
1500 | |
1501 | /* First of all look for always inline functions. */ | |
1502 | for (e = node->callees; e; e = e->next_callee) | |
18c6ada9 JH |
1503 | if (e->callee->local.disregard_inline_limits |
1504 | && e->inline_failed | |
1505 | && !cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed) | |
dc0bfe6a JH |
1506 | /* ??? It is possible that renaming variable removed the function body |
1507 | in duplicate_decls. See gcc.c-torture/compile/20011119-2.c */ | |
1508 | && DECL_SAVED_TREE (e->callee->decl)) | |
18c6ada9 | 1509 | cgraph_mark_inline (e); |
d4d1ebc1 | 1510 | |
18c6ada9 | 1511 | /* Now do the automatic inlining. */ |
b684a3df | 1512 | if (!flag_really_no_inline) |
18c6ada9 JH |
1513 | for (e = node->callees; e; e = e->next_callee) |
1514 | if (e->callee->local.inlinable | |
1515 | && e->inline_failed | |
1516 | && !e->callee->local.disregard_inline_limits | |
1517 | && !cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed) | |
1518 | && cgraph_check_inline_limits (node, e->callee, &e->inline_failed) | |
1519 | && DECL_SAVED_TREE (e->callee->decl)) | |
1520 | { | |
1521 | if (cgraph_default_inline_p (e->callee)) | |
1522 | cgraph_mark_inline (e); | |
1523 | else | |
1524 | e->inline_failed | |
1525 | = N_("--param max-inline-insns-single limit reached"); | |
1526 | } | |
d4d1ebc1 JH |
1527 | } |
1528 | ||
1529 | ||
18c6ada9 | 1530 | /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */ |
b58b1157 JH |
1531 | |
1532 | bool | |
18c6ada9 | 1533 | cgraph_inline_p (struct cgraph_edge *e, const char **reason) |
b58b1157 | 1534 | { |
18c6ada9 JH |
1535 | *reason = e->inline_failed; |
1536 | return !e->inline_failed; | |
b58b1157 | 1537 | } |
18c6ada9 | 1538 | |
db0e878d AJ |
1539 | /* Expand all functions that must be output. |
1540 | ||
b58b1157 JH |
1541 | Attempt to topologically sort the nodes so function is output when |
1542 | all called functions are already assembled to allow data to be | |
a98ebe2e | 1543 | propagated across the callgraph. Use a stack to get smaller distance |
d1a6adeb | 1544 | between a function and its callees (later we may choose to use a more |
b58b1157 JH |
1545 | sophisticated algorithm for function reordering; we will likely want |
1546 | to use subsections to make the output functions appear in top-down | |
1547 | order). */ | |
1548 | ||
1549 | static void | |
a20af5b8 | 1550 | cgraph_expand_all_functions (void) |
b58b1157 JH |
1551 | { |
1552 | struct cgraph_node *node; | |
1553 | struct cgraph_node **order = | |
b3c3af2f | 1554 | xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *)); |
18c6ada9 | 1555 | int order_pos = 0, new_order_pos = 0; |
b58b1157 JH |
1556 | int i; |
1557 | ||
1558 | cgraph_mark_functions_to_output (); | |
1559 | ||
1560 | order_pos = cgraph_postorder (order); | |
18c6ada9 JH |
1561 | if (order_pos != cgraph_n_nodes) |
1562 | abort (); | |
b58b1157 | 1563 | |
18c6ada9 JH |
1564 | /* Garbage collector may remove inline clones we elliminate during |
1565 | optimization. So we must be sure to not reference them. */ | |
1566 | for (i = 0; i < order_pos; i++) | |
1567 | if (order[i]->output) | |
1568 | order[new_order_pos++] = order[i]; | |
1569 | ||
1570 | for (i = new_order_pos - 1; i >= 0; i--) | |
b58b1157 JH |
1571 | { |
1572 | node = order[i]; | |
1573 | if (node->output) | |
1574 | { | |
1575 | if (!node->reachable) | |
1576 | abort (); | |
1577 | node->output = 0; | |
1578 | cgraph_expand_function (node); | |
1579 | } | |
1580 | } | |
1581 | free (order); | |
1582 | } | |
1583 | ||
1584 | /* Mark all local functions. | |
1585 | ||
a98ebe2e | 1586 | A local function is one whose calls can occur only in the |
d1a6adeb | 1587 | current compilation unit and all its calls are explicit, |
dc0bfe6a | 1588 | so we can change its calling convention. |
b58b1157 JH |
1589 | We simply mark all static functions whose address is not taken |
1590 | as local. */ | |
1591 | ||
1592 | static void | |
db0e878d | 1593 | cgraph_mark_local_functions (void) |
b58b1157 JH |
1594 | { |
1595 | struct cgraph_node *node; | |
1596 | ||
1597 | if (cgraph_dump_file) | |
7d82fe7c | 1598 | fprintf (cgraph_dump_file, "\nMarking local functions:"); |
b58b1157 JH |
1599 | |
1600 | /* Figure out functions we want to assemble. */ | |
1601 | for (node = cgraph_nodes; node; node = node->next) | |
1602 | { | |
1603 | node->local.local = (!node->needed | |
1604 | && DECL_SAVED_TREE (node->decl) | |
1605 | && !TREE_PUBLIC (node->decl)); | |
1606 | if (cgraph_dump_file && node->local.local) | |
1607 | fprintf (cgraph_dump_file, " %s", cgraph_node_name (node)); | |
1608 | } | |
1609 | if (cgraph_dump_file) | |
7d82fe7c | 1610 | fprintf (cgraph_dump_file, "\n\n"); |
b58b1157 | 1611 | } |
dafc5b82 | 1612 | |
18c6ada9 JH |
1613 | /* Return true when function body of DECL still needs to be kept around |
1614 | for later re-use. */ | |
1615 | bool | |
1616 | cgraph_preserve_function_body_p (tree decl) | |
1617 | { | |
1618 | struct cgraph_node *node; | |
1619 | /* Keep the body; we're going to dump it. */ | |
1620 | if (dump_enabled_p (TDI_all)) | |
1621 | return true; | |
1622 | if (!cgraph_global_info_ready) | |
1623 | return (DECL_INLINE (decl) && !flag_really_no_inline); | |
1624 | /* Look if there is any clone around. */ | |
1625 | for (node = cgraph_node (decl); node; node = node->next_clone) | |
1626 | if (node->global.inlined_to) | |
1627 | return true; | |
1628 | return false; | |
1629 | } | |
1630 | ||
1c4a429a JH |
1631 | /* Perform simple optimizations based on callgraph. */ |
1632 | ||
1633 | void | |
db0e878d | 1634 | cgraph_optimize (void) |
1c4a429a | 1635 | { |
18c6ada9 JH |
1636 | #ifdef ENABLE_CHECKING |
1637 | verify_cgraph (); | |
1638 | #endif | |
4a46cbfb JH |
1639 | if (!flag_unit_at_a_time) |
1640 | return; | |
a194aa56 | 1641 | timevar_push (TV_CGRAPHOPT); |
b58b1157 JH |
1642 | if (!quiet_flag) |
1643 | fprintf (stderr, "Performing intraprocedural optimizations\n"); | |
7d82fe7c KC |
1644 | |
1645 | cgraph_mark_local_functions (); | |
a194aa56 JH |
1646 | if (cgraph_dump_file) |
1647 | { | |
7d82fe7c | 1648 | fprintf (cgraph_dump_file, "Marked "); |
a194aa56 JH |
1649 | dump_cgraph (cgraph_dump_file); |
1650 | } | |
dafc5b82 | 1651 | |
18c6ada9 JH |
1652 | if (flag_inline_trees) |
1653 | cgraph_decide_inlining (); | |
dafc5b82 | 1654 | cgraph_global_info_ready = true; |
a194aa56 JH |
1655 | if (cgraph_dump_file) |
1656 | { | |
7d82fe7c | 1657 | fprintf (cgraph_dump_file, "Optimized "); |
a194aa56 JH |
1658 | dump_cgraph (cgraph_dump_file); |
1659 | } | |
1660 | timevar_pop (TV_CGRAPHOPT); | |
1c4a429a | 1661 | |
b58b1157 | 1662 | /* Output everything. */ |
7d82fe7c KC |
1663 | if (!quiet_flag) |
1664 | fprintf (stderr, "Assembling functions:\n"); | |
18c6ada9 JH |
1665 | #ifdef ENABLE_CHECKING |
1666 | verify_cgraph (); | |
1667 | #endif | |
a20af5b8 | 1668 | cgraph_expand_all_functions (); |
a194aa56 JH |
1669 | if (cgraph_dump_file) |
1670 | { | |
7d82fe7c | 1671 | fprintf (cgraph_dump_file, "\nFinal "); |
a194aa56 JH |
1672 | dump_cgraph (cgraph_dump_file); |
1673 | } | |
18c6ada9 JH |
1674 | #ifdef ENABLE_CHECKING |
1675 | verify_cgraph (); | |
1676 | #endif | |
1c4a429a | 1677 | } |