]>
Commit | Line | Data |
---|---|---|
d7c6d889 | 1 | /* Callgraph based intraprocedural optimizations. |
dfbf3d71 | 2 | Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. |
ae01b312 | 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 | ||
b0cdf642 | 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 | ||
7bd28bba | 39 | This function has same behavior as the above but is used for static |
b0cdf642 | 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 | ||
ca67a72b | 108 | The intra-procedural information is produced and its existence |
b0cdf642 | 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 | |
c26a6416 | 145 | inside callgraph as so called inline plan. Refer to cgraph.c |
b0cdf642 | 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. */ | |
121f3051 | 166 | |
acc70efa | 167 | |
ae01b312 | 168 | #include "config.h" |
169 | #include "system.h" | |
170 | #include "coretypes.h" | |
171 | #include "tm.h" | |
172 | #include "tree.h" | |
b5530559 | 173 | #include "rtl.h" |
acc70efa | 174 | #include "tree-flow.h" |
ae01b312 | 175 | #include "tree-inline.h" |
176 | #include "langhooks.h" | |
c6224531 | 177 | #include "pointer-set.h" |
ae01b312 | 178 | #include "toplev.h" |
179 | #include "flags.h" | |
180 | #include "ggc.h" | |
181 | #include "debug.h" | |
182 | #include "target.h" | |
183 | #include "cgraph.h" | |
80a85d8a | 184 | #include "diagnostic.h" |
f79b6507 | 185 | #include "timevar.h" |
d7c6d889 | 186 | #include "params.h" |
187 | #include "fibheap.h" | |
188 | #include "c-common.h" | |
611e5405 | 189 | #include "intl.h" |
b69eb0ff | 190 | #include "function.h" |
acc70efa | 191 | #include "tree-gimple.h" |
d7c6d889 | 192 | |
193 | #define INSNS_PER_CALL 10 | |
ae01b312 | 194 | |
a6868229 | 195 | static void cgraph_expand_all_functions (void); |
d9d9733a | 196 | static void cgraph_mark_functions_to_output (void); |
197 | static void cgraph_expand_function (struct cgraph_node *); | |
198 | static tree record_call_1 (tree *, int *, void *); | |
58f1b650 | 199 | static void cgraph_mark_local_functions (void); |
2ff66ee0 | 200 | static bool cgraph_default_inline_p (struct cgraph_node *n); |
201 | static void cgraph_analyze_function (struct cgraph_node *node); | |
19489abd | 202 | static void cgraph_decide_inlining_incrementally (struct cgraph_node *); |
ae01b312 | 203 | |
d7c6d889 | 204 | /* Statistics we collect about inlining algorithm. */ |
205 | static int ncalls_inlined; | |
206 | static int nfunctions_inlined; | |
207 | static int initial_insns; | |
208 | static int overall_insns; | |
209 | ||
25bb88de | 210 | /* Records tree nodes seen in cgraph_create_edges. Simply using |
211 | walk_tree_without_duplicates doesn't guarantee each node is visited | |
212 | once because it gets a new htab upon each recursive call from | |
213 | record_calls_1. */ | |
c6224531 | 214 | static struct pointer_set_t *visited_nodes; |
25bb88de | 215 | |
121f3051 | 216 | static FILE *cgraph_dump_file; |
217 | ||
2c0b522d | 218 | /* Determine if function DECL is needed. That is, visible to something |
219 | either outside this translation unit, something magic in the system | |
220 | configury, or (if not doing unit-at-a-time) to something we havn't | |
221 | seen yet. */ | |
222 | ||
223 | static bool | |
224 | decide_is_function_needed (struct cgraph_node *node, tree decl) | |
225 | { | |
9d95b2b0 | 226 | tree origin; |
4ee9c684 | 227 | |
2c0b522d | 228 | /* If we decided it was needed before, but at the time we didn't have |
229 | the body of the function available, then it's still needed. We have | |
230 | to go back and re-check its dependencies now. */ | |
231 | if (node->needed) | |
232 | return true; | |
233 | ||
234 | /* Externally visible functions must be output. The exception is | |
235 | COMDAT functions that must be output only when they are needed. */ | |
236 | if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl)) | |
237 | return true; | |
238 | ||
239 | /* Constructors and destructors are reachable from the runtime by | |
240 | some mechanism. */ | |
241 | if (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)) | |
242 | return true; | |
243 | ||
244 | /* If the user told us it is used, then it must be so. */ | |
245 | if (lookup_attribute ("used", DECL_ATTRIBUTES (decl))) | |
246 | return true; | |
247 | ||
248 | /* ??? If the assembler name is set by hand, it is possible to assemble | |
249 | the name later after finalizing the function and the fact is noticed | |
250 | in assemble_name then. This is arguably a bug. */ | |
251 | if (DECL_ASSEMBLER_NAME_SET_P (decl) | |
252 | && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))) | |
253 | return true; | |
254 | ||
255 | if (flag_unit_at_a_time) | |
256 | return false; | |
257 | ||
258 | /* If not doing unit at a time, then we'll only defer this function | |
259 | if its marked for inlining. Otherwise we want to emit it now. */ | |
260 | ||
261 | /* "extern inline" functions are never output locally. */ | |
262 | if (DECL_EXTERNAL (decl)) | |
263 | return false; | |
4ee9c684 | 264 | /* Nested functions of extern inline function shall not be emit unless |
265 | we inlined the origin. */ | |
9d95b2b0 | 266 | for (origin = decl_function_context (decl); origin; |
267 | origin = decl_function_context (origin)) | |
268 | if (DECL_EXTERNAL (origin)) | |
4ee9c684 | 269 | return false; |
f024691d | 270 | /* We want to emit COMDAT functions only when absolutely necessary. */ |
c08871a9 | 271 | if (DECL_COMDAT (decl)) |
2c0b522d | 272 | return false; |
273 | if (!DECL_INLINE (decl) | |
274 | || (!node->local.disregard_inline_limits | |
275 | /* When declared inline, defer even the uninlinable functions. | |
e4200070 | 276 | This allows them to be eliminated when unused. */ |
2c0b522d | 277 | && !DECL_DECLARED_INLINE_P (decl) |
19489abd | 278 | && (!node->local.inlinable || !cgraph_default_inline_p (node)))) |
2c0b522d | 279 | return true; |
280 | ||
281 | return false; | |
282 | } | |
283 | ||
acc70efa | 284 | |
acc70efa | 285 | |
c08871a9 | 286 | /* When not doing unit-at-a-time, output all functions enqueued. |
287 | Return true when such a functions were found. */ | |
050e11c9 | 288 | |
289 | bool | |
c08871a9 | 290 | cgraph_assemble_pending_functions (void) |
291 | { | |
292 | bool output = false; | |
293 | ||
294 | if (flag_unit_at_a_time) | |
295 | return false; | |
296 | ||
297 | while (cgraph_nodes_queue) | |
298 | { | |
299 | struct cgraph_node *n = cgraph_nodes_queue; | |
300 | ||
301 | cgraph_nodes_queue = cgraph_nodes_queue->next_needed; | |
b0cdf642 | 302 | n->next_needed = NULL; |
4ee9c684 | 303 | if (!n->global.inlined_to && !DECL_EXTERNAL (n->decl)) |
050e11c9 | 304 | { |
305 | cgraph_expand_function (n); | |
306 | output = true; | |
307 | } | |
c08871a9 | 308 | } |
050e11c9 | 309 | |
c08871a9 | 310 | return output; |
311 | } | |
312 | ||
28df663b | 313 | /* DECL has been parsed. Take it, queue it, compile it at the whim of the |
314 | logic in effect. If NESTED is true, then our caller cannot stand to have | |
315 | the garbage collector run at the moment. We would need to either create | |
316 | a new GC context, or just not compile right now. */ | |
ae01b312 | 317 | |
318 | void | |
28df663b | 319 | cgraph_finalize_function (tree decl, bool nested) |
ae01b312 | 320 | { |
321 | struct cgraph_node *node = cgraph_node (decl); | |
322 | ||
c08871a9 | 323 | if (node->local.finalized) |
324 | { | |
325 | /* As an GCC extension we allow redefinition of the function. The | |
28df663b | 326 | semantics when both copies of bodies differ is not well defined. |
327 | We replace the old body with new body so in unit at a time mode | |
328 | we always use new body, while in normal mode we may end up with | |
329 | old body inlined into some functions and new body expanded and | |
330 | inlined in others. | |
c08871a9 | 331 | |
28df663b | 332 | ??? It may make more sense to use one body for inlining and other |
f024691d | 333 | body for expanding the function but this is difficult to do. */ |
28df663b | 334 | |
050e11c9 | 335 | /* If node->output is set, then this is a unit-at-a-time compilation |
336 | and we have already begun whole-unit analysis. This is *not* | |
337 | testing for whether we've already emitted the function. That | |
338 | case can be sort-of legitimately seen with real function | |
339 | redefinition errors. I would argue that the front end should | |
340 | never present us with such a case, but don't enforce that for now. */ | |
cc636d56 | 341 | gcc_assert (!node->output); |
28df663b | 342 | |
7bd28bba | 343 | /* Reset our data structures so we can analyze the function again. */ |
638531ad | 344 | memset (&node->local, 0, sizeof (node->local)); |
345 | memset (&node->global, 0, sizeof (node->global)); | |
346 | memset (&node->rtl, 0, sizeof (node->rtl)); | |
ec1e35b2 | 347 | node->analyzed = false; |
69435b7f | 348 | node->local.redefined_extern_inline = true; |
06bf91cb | 349 | |
350 | if (!flag_unit_at_a_time) | |
351 | { | |
352 | struct cgraph_node *n; | |
353 | ||
354 | for (n = cgraph_nodes; n; n = n->next) | |
355 | if (n->global.inlined_to == node) | |
356 | cgraph_remove_node (n); | |
357 | } | |
358 | ||
638531ad | 359 | while (node->callees) |
b0cdf642 | 360 | cgraph_remove_edge (node->callees); |
28df663b | 361 | |
638531ad | 362 | /* We may need to re-queue the node for assembling in case |
363 | we already proceeded it and ignored as not needed. */ | |
364 | if (node->reachable && !flag_unit_at_a_time) | |
c08871a9 | 365 | { |
638531ad | 366 | struct cgraph_node *n; |
367 | ||
368 | for (n = cgraph_nodes_queue; n; n = n->next_needed) | |
369 | if (n == node) | |
370 | break; | |
371 | if (!n) | |
372 | node->reachable = 0; | |
c08871a9 | 373 | } |
c08871a9 | 374 | } |
28df663b | 375 | |
c08871a9 | 376 | notice_global_symbol (decl); |
ae01b312 | 377 | node->decl = decl; |
79bb87b4 | 378 | node->local.finalized = true; |
9d95b2b0 | 379 | if (node->nested) |
380 | lower_nested_functions (decl); | |
381 | gcc_assert (!node->nested); | |
ae01b312 | 382 | |
2c0b522d | 383 | /* If not unit at a time, then we need to create the call graph |
384 | now, so that called functions can be queued and emitted now. */ | |
2ff66ee0 | 385 | if (!flag_unit_at_a_time) |
19489abd | 386 | { |
387 | cgraph_analyze_function (node); | |
388 | cgraph_decide_inlining_incrementally (node); | |
389 | } | |
2ff66ee0 | 390 | |
2c0b522d | 391 | if (decide_is_function_needed (node, decl)) |
392 | cgraph_mark_needed_node (node); | |
393 | ||
28df663b | 394 | /* If not unit at a time, go ahead and emit everything we've found |
395 | to be reachable at this time. */ | |
396 | if (!nested) | |
dc721f36 | 397 | { |
398 | if (!cgraph_assemble_pending_functions ()) | |
399 | ggc_collect (); | |
400 | } | |
3d7bfc56 | 401 | |
2c0b522d | 402 | /* If we've not yet emitted decl, tell the debug info about it. */ |
28df663b | 403 | if (!TREE_ASM_WRITTEN (decl)) |
2c0b522d | 404 | (*debug_hooks->deferred_inline_function) (decl); |
4e8871a0 | 405 | |
b69eb0ff | 406 | /* Possibly warn about unused parameters. */ |
407 | if (warn_unused_parameter) | |
408 | do_warn_unused_parameter (decl); | |
ae01b312 | 409 | } |
410 | ||
ae01b312 | 411 | /* Walk tree and record all calls. Called via walk_tree. */ |
412 | static tree | |
d9d9733a | 413 | record_call_1 (tree *tp, int *walk_subtrees, void *data) |
ae01b312 | 414 | { |
ec1e35b2 | 415 | tree t = *tp; |
416 | ||
417 | switch (TREE_CODE (t)) | |
ae01b312 | 418 | { |
ec1e35b2 | 419 | case VAR_DECL: |
420 | /* ??? Really, we should mark this decl as *potentially* referenced | |
421 | by this function and re-examine whether the decl is actually used | |
422 | after rtl has been generated. */ | |
423 | if (TREE_STATIC (t)) | |
caa6fdce | 424 | { |
425 | cgraph_varpool_mark_needed_node (cgraph_varpool_node (t)); | |
426 | if (lang_hooks.callgraph.analyze_expr) | |
427 | return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees, | |
428 | data); | |
429 | } | |
ec1e35b2 | 430 | break; |
431 | ||
432 | case ADDR_EXPR: | |
433 | if (flag_unit_at_a_time) | |
434 | { | |
435 | /* Record dereferences to the functions. This makes the | |
436 | functions reachable unconditionally. */ | |
437 | tree decl = TREE_OPERAND (*tp, 0); | |
438 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
439 | cgraph_mark_needed_node (cgraph_node (decl)); | |
440 | } | |
441 | break; | |
442 | ||
443 | case CALL_EXPR: | |
444 | { | |
445 | tree decl = get_callee_fndecl (*tp); | |
446 | if (decl && TREE_CODE (decl) == FUNCTION_DECL) | |
447 | { | |
b0cdf642 | 448 | cgraph_create_edge (data, cgraph_node (decl), *tp); |
ec1e35b2 | 449 | |
450 | /* When we see a function call, we don't want to look at the | |
451 | function reference in the ADDR_EXPR that is hanging from | |
452 | the CALL_EXPR we're examining here, because we would | |
453 | conclude incorrectly that the function's address could be | |
454 | taken by something that is not a function call. So only | |
455 | walk the function parameter list, skip the other subtrees. */ | |
456 | ||
457 | walk_tree (&TREE_OPERAND (*tp, 1), record_call_1, data, | |
458 | visited_nodes); | |
459 | *walk_subtrees = 0; | |
460 | } | |
461 | break; | |
462 | } | |
463 | ||
464 | default: | |
465 | /* Save some cycles by not walking types and declaration as we | |
466 | won't find anything useful there anyway. */ | |
ce45a448 | 467 | if (IS_TYPE_OR_DECL_P (*tp)) |
ae01b312 | 468 | { |
ae01b312 | 469 | *walk_subtrees = 0; |
ec1e35b2 | 470 | break; |
ae01b312 | 471 | } |
ec1e35b2 | 472 | |
473 | if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE) | |
dc24ddbd | 474 | return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees, data); |
ec1e35b2 | 475 | break; |
ae01b312 | 476 | } |
ec1e35b2 | 477 | |
ae01b312 | 478 | return NULL; |
479 | } | |
480 | ||
b0cdf642 | 481 | /* Create cgraph edges for function calls inside BODY from NODE. */ |
ae01b312 | 482 | |
483 | void | |
b0cdf642 | 484 | cgraph_create_edges (struct cgraph_node *node, tree body) |
ae01b312 | 485 | { |
e6d2b2d8 | 486 | /* The nodes we're interested in are never shared, so walk |
487 | the tree ignoring duplicates. */ | |
c6224531 | 488 | visited_nodes = pointer_set_create (); |
b0cdf642 | 489 | walk_tree (&body, record_call_1, node, visited_nodes); |
c6224531 | 490 | pointer_set_destroy (visited_nodes); |
25bb88de | 491 | visited_nodes = NULL; |
ae01b312 | 492 | } |
493 | ||
b0cdf642 | 494 | static bool error_found; |
495 | ||
ca67a72b | 496 | /* Callback of verify_cgraph_node. Check that all call_exprs have |
497 | cgraph nodes. */ | |
23f099a4 | 498 | |
b0cdf642 | 499 | static tree |
500 | verify_cgraph_node_1 (tree *tp, int *walk_subtrees, void *data) | |
501 | { | |
502 | tree t = *tp; | |
503 | tree decl; | |
504 | ||
505 | if (TREE_CODE (t) == CALL_EXPR && (decl = get_callee_fndecl (t))) | |
506 | { | |
507 | struct cgraph_edge *e = cgraph_edge (data, t); | |
508 | if (e) | |
509 | { | |
510 | if (e->aux) | |
511 | { | |
512 | error ("Shared call_expr:"); | |
513 | debug_tree (t); | |
514 | error_found = true; | |
515 | } | |
516 | if (e->callee->decl != cgraph_node (decl)->decl) | |
517 | { | |
518 | error ("Edge points to wrong declaration:"); | |
519 | debug_tree (e->callee->decl); | |
520 | fprintf (stderr," Instead of:"); | |
521 | debug_tree (decl); | |
522 | } | |
523 | e->aux = (void *)1; | |
524 | } | |
525 | else | |
526 | { | |
527 | error ("Missing callgraph edge for call expr:"); | |
528 | debug_tree (t); | |
529 | error_found = true; | |
530 | } | |
531 | } | |
23f099a4 | 532 | |
b0cdf642 | 533 | /* Save some cycles by not walking types and declaration as we |
534 | won't find anything useful there anyway. */ | |
ce45a448 | 535 | if (IS_TYPE_OR_DECL_P (*tp)) |
23f099a4 | 536 | *walk_subtrees = 0; |
537 | ||
b0cdf642 | 538 | return NULL_TREE; |
539 | } | |
540 | ||
541 | /* Verify cgraph nodes of given cgraph node. */ | |
542 | void | |
543 | verify_cgraph_node (struct cgraph_node *node) | |
544 | { | |
545 | struct cgraph_edge *e; | |
546 | struct cgraph_node *main_clone; | |
547 | ||
548 | timevar_push (TV_CGRAPH_VERIFY); | |
549 | error_found = false; | |
550 | for (e = node->callees; e; e = e->next_callee) | |
551 | if (e->aux) | |
552 | { | |
553 | error ("Aux field set for edge %s->%s", | |
554 | cgraph_node_name (e->caller), cgraph_node_name (e->callee)); | |
555 | error_found = true; | |
556 | } | |
557 | for (e = node->callers; e; e = e->next_caller) | |
558 | { | |
559 | if (!e->inline_failed) | |
560 | { | |
561 | if (node->global.inlined_to | |
562 | != (e->caller->global.inlined_to | |
563 | ? e->caller->global.inlined_to : e->caller)) | |
564 | { | |
565 | error ("Inlined_to pointer is wrong"); | |
566 | error_found = true; | |
567 | } | |
568 | if (node->callers->next_caller) | |
569 | { | |
570 | error ("Multiple inline callers"); | |
571 | error_found = true; | |
572 | } | |
573 | } | |
574 | else | |
575 | if (node->global.inlined_to) | |
576 | { | |
577 | error ("Inlined_to pointer set for noninline callers"); | |
578 | error_found = true; | |
579 | } | |
580 | } | |
581 | if (!node->callers && node->global.inlined_to) | |
582 | { | |
583 | error ("Inlined_to pointer is set but no predecesors found"); | |
584 | error_found = true; | |
585 | } | |
586 | if (node->global.inlined_to == node) | |
587 | { | |
588 | error ("Inlined_to pointer reffers to itself"); | |
589 | error_found = true; | |
590 | } | |
591 | ||
592 | for (main_clone = cgraph_node (node->decl); main_clone; | |
593 | main_clone = main_clone->next_clone) | |
594 | if (main_clone == node) | |
595 | break; | |
596 | if (!node) | |
597 | { | |
598 | error ("Node not found in DECL_ASSEMBLER_NAME hash"); | |
599 | error_found = true; | |
600 | } | |
601 | ||
602 | if (node->analyzed | |
603 | && DECL_SAVED_TREE (node->decl) && !TREE_ASM_WRITTEN (node->decl) | |
604 | && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to)) | |
605 | { | |
606 | walk_tree_without_duplicates (&DECL_SAVED_TREE (node->decl), | |
607 | verify_cgraph_node_1, node); | |
608 | for (e = node->callees; e; e = e->next_callee) | |
609 | { | |
610 | if (!e->aux) | |
611 | { | |
612 | error ("Edge %s->%s has no corresponding call_expr", | |
613 | cgraph_node_name (e->caller), | |
614 | cgraph_node_name (e->callee)); | |
615 | error_found = true; | |
616 | } | |
617 | e->aux = 0; | |
618 | } | |
619 | } | |
620 | if (error_found) | |
621 | { | |
622 | dump_cgraph_node (stderr, node); | |
623 | internal_error ("verify_cgraph_node failed."); | |
624 | } | |
625 | timevar_pop (TV_CGRAPH_VERIFY); | |
626 | } | |
627 | ||
628 | /* Verify whole cgraph structure. */ | |
629 | void | |
630 | verify_cgraph (void) | |
631 | { | |
632 | struct cgraph_node *node; | |
633 | ||
8ec2a798 | 634 | if (sorrycount || errorcount) |
635 | return; | |
636 | ||
b0cdf642 | 637 | for (node = cgraph_nodes; node; node = node->next) |
638 | verify_cgraph_node (node); | |
639 | } | |
640 | ||
0785e435 | 641 | /* Analyze the function scheduled to be output. */ |
642 | static void | |
643 | cgraph_analyze_function (struct cgraph_node *node) | |
644 | { | |
645 | tree decl = node->decl; | |
611e5405 | 646 | struct cgraph_edge *e; |
0785e435 | 647 | |
ec1e35b2 | 648 | current_function_decl = decl; |
0785e435 | 649 | |
650 | /* First kill forward declaration so reverse inlining works properly. */ | |
b0cdf642 | 651 | cgraph_create_edges (node, DECL_SAVED_TREE (decl)); |
0785e435 | 652 | |
653 | node->local.inlinable = tree_inlinable_function_p (decl); | |
4ee9c684 | 654 | node->local.self_insns = estimate_num_insns (DECL_SAVED_TREE (decl)); |
0785e435 | 655 | if (node->local.inlinable) |
656 | node->local.disregard_inline_limits | |
dc24ddbd | 657 | = lang_hooks.tree_inlining.disregard_inline_limits (decl); |
611e5405 | 658 | for (e = node->callers; e; e = e->next_caller) |
b0cdf642 | 659 | { |
660 | if (node->local.redefined_extern_inline) | |
661 | e->inline_failed = N_("redefined extern inline functions are not " | |
662 | "considered for inlining"); | |
663 | else if (!node->local.inlinable) | |
664 | e->inline_failed = N_("function not inlinable"); | |
665 | else | |
666 | e->inline_failed = N_("function not considered for inlining"); | |
667 | } | |
99cf25d0 | 668 | if (flag_really_no_inline && !node->local.disregard_inline_limits) |
669 | node->local.inlinable = 0; | |
0785e435 | 670 | /* Inlining characteristics are maintained by the cgraph_mark_inline. */ |
671 | node->global.insns = node->local.self_insns; | |
0785e435 | 672 | |
ec1e35b2 | 673 | node->analyzed = true; |
c08871a9 | 674 | current_function_decl = NULL; |
0785e435 | 675 | } |
676 | ||
ae01b312 | 677 | /* Analyze the whole compilation unit once it is parsed completely. */ |
678 | ||
679 | void | |
d9d9733a | 680 | cgraph_finalize_compilation_unit (void) |
ae01b312 | 681 | { |
682 | struct cgraph_node *node; | |
ae01b312 | 683 | |
2ff66ee0 | 684 | if (!flag_unit_at_a_time) |
c08871a9 | 685 | { |
686 | cgraph_assemble_pending_functions (); | |
687 | return; | |
688 | } | |
2ff66ee0 | 689 | |
229dcfae | 690 | cgraph_varpool_assemble_pending_decls (); |
d7c6d889 | 691 | if (!quiet_flag) |
692 | fprintf (stderr, "\nAnalyzing compilation unit\n"); | |
229dcfae | 693 | |
f79b6507 | 694 | timevar_push (TV_CGRAPH); |
695 | if (cgraph_dump_file) | |
ae01b312 | 696 | { |
e4200070 | 697 | fprintf (cgraph_dump_file, "Initial entry points:"); |
3d7bfc56 | 698 | for (node = cgraph_nodes; node; node = node->next) |
699 | if (node->needed && DECL_SAVED_TREE (node->decl)) | |
f79b6507 | 700 | fprintf (cgraph_dump_file, " %s", cgraph_node_name (node)); |
701 | fprintf (cgraph_dump_file, "\n"); | |
ae01b312 | 702 | } |
703 | ||
e6d2b2d8 | 704 | /* Propagate reachability flag and lower representation of all reachable |
705 | functions. In the future, lowering will introduce new functions and | |
706 | new entry points on the way (by template instantiation and virtual | |
707 | method table generation for instance). */ | |
3d7bfc56 | 708 | while (cgraph_nodes_queue) |
ae01b312 | 709 | { |
0785e435 | 710 | struct cgraph_edge *edge; |
3d7bfc56 | 711 | tree decl = cgraph_nodes_queue->decl; |
712 | ||
713 | node = cgraph_nodes_queue; | |
d87976fb | 714 | cgraph_nodes_queue = cgraph_nodes_queue->next_needed; |
b0cdf642 | 715 | node->next_needed = NULL; |
ae01b312 | 716 | |
638531ad | 717 | /* ??? It is possible to create extern inline function and later using |
3927afe0 | 718 | weak alas attribute to kill its body. See |
638531ad | 719 | gcc.c-torture/compile/20011119-1.c */ |
720 | if (!DECL_SAVED_TREE (decl)) | |
721 | continue; | |
722 | ||
cc636d56 | 723 | gcc_assert (!node->analyzed && node->reachable); |
724 | gcc_assert (DECL_SAVED_TREE (decl)); | |
ae01b312 | 725 | |
0785e435 | 726 | cgraph_analyze_function (node); |
2c0b522d | 727 | |
ae01b312 | 728 | for (edge = node->callees; edge; edge = edge->next_callee) |
0785e435 | 729 | if (!edge->callee->reachable) |
2c0b522d | 730 | cgraph_mark_reachable_node (edge->callee); |
731 | ||
229dcfae | 732 | cgraph_varpool_assemble_pending_decls (); |
ae01b312 | 733 | } |
2c0b522d | 734 | |
3d7bfc56 | 735 | /* Collect entry points to the unit. */ |
736 | ||
f79b6507 | 737 | if (cgraph_dump_file) |
3d7bfc56 | 738 | { |
e4200070 | 739 | fprintf (cgraph_dump_file, "Unit entry points:"); |
3d7bfc56 | 740 | for (node = cgraph_nodes; node; node = node->next) |
741 | if (node->needed && DECL_SAVED_TREE (node->decl)) | |
f79b6507 | 742 | fprintf (cgraph_dump_file, " %s", cgraph_node_name (node)); |
e4200070 | 743 | fprintf (cgraph_dump_file, "\n\nInitial "); |
0785e435 | 744 | dump_cgraph (cgraph_dump_file); |
3d7bfc56 | 745 | } |
e6d2b2d8 | 746 | |
f79b6507 | 747 | if (cgraph_dump_file) |
748 | fprintf (cgraph_dump_file, "\nReclaiming functions:"); | |
ae01b312 | 749 | |
750 | for (node = cgraph_nodes; node; node = node->next) | |
751 | { | |
752 | tree decl = node->decl; | |
753 | ||
754 | if (!node->reachable && DECL_SAVED_TREE (decl)) | |
755 | { | |
f79b6507 | 756 | if (cgraph_dump_file) |
757 | fprintf (cgraph_dump_file, " %s", cgraph_node_name (node)); | |
b0cdf642 | 758 | cgraph_remove_node (node); |
ae01b312 | 759 | } |
bc5cab3b | 760 | else |
761 | node->next_needed = NULL; | |
ae01b312 | 762 | } |
f79b6507 | 763 | if (cgraph_dump_file) |
e4200070 | 764 | { |
765 | fprintf (cgraph_dump_file, "\n\nReclaimed "); | |
766 | dump_cgraph (cgraph_dump_file); | |
767 | } | |
ae01b312 | 768 | ggc_collect (); |
f79b6507 | 769 | timevar_pop (TV_CGRAPH); |
ae01b312 | 770 | } |
ae01b312 | 771 | /* Figure out what functions we want to assemble. */ |
772 | ||
773 | static void | |
d9d9733a | 774 | cgraph_mark_functions_to_output (void) |
ae01b312 | 775 | { |
776 | struct cgraph_node *node; | |
777 | ||
ae01b312 | 778 | for (node = cgraph_nodes; node; node = node->next) |
779 | { | |
780 | tree decl = node->decl; | |
d7c6d889 | 781 | struct cgraph_edge *e; |
cc636d56 | 782 | |
783 | gcc_assert (!node->output); | |
d7c6d889 | 784 | |
785 | for (e = node->callers; e; e = e->next_caller) | |
611e5405 | 786 | if (e->inline_failed) |
d7c6d889 | 787 | break; |
ae01b312 | 788 | |
e6d2b2d8 | 789 | /* We need to output all local functions that are used and not |
790 | always inlined, as well as those that are reachable from | |
791 | outside the current compilation unit. */ | |
ae01b312 | 792 | if (DECL_SAVED_TREE (decl) |
b0cdf642 | 793 | && !node->global.inlined_to |
ae01b312 | 794 | && (node->needed |
d7c6d889 | 795 | || (e && node->reachable)) |
4ee9c684 | 796 | && !TREE_ASM_WRITTEN (decl) |
ae01b312 | 797 | && !DECL_EXTERNAL (decl)) |
798 | node->output = 1; | |
cc636d56 | 799 | else |
9cee7c3f | 800 | { |
801 | /* We should've reclaimed all functions that are not needed. */ | |
802 | #ifdef ENABLE_CHECKING | |
803 | if (!node->global.inlined_to && DECL_SAVED_TREE (decl) | |
804 | && !DECL_EXTERNAL (decl)) | |
805 | { | |
806 | dump_cgraph_node (stderr, node); | |
807 | internal_error ("failed to reclaim unneeded function"); | |
808 | } | |
809 | #endif | |
810 | gcc_assert (node->global.inlined_to || !DECL_SAVED_TREE (decl) | |
811 | || DECL_EXTERNAL (decl)); | |
812 | ||
813 | } | |
814 | ||
961e3b13 | 815 | } |
816 | } | |
817 | ||
ae01b312 | 818 | /* Expand function specified by NODE. */ |
e6d2b2d8 | 819 | |
ae01b312 | 820 | static void |
d9d9733a | 821 | cgraph_expand_function (struct cgraph_node *node) |
ae01b312 | 822 | { |
823 | tree decl = node->decl; | |
824 | ||
b0cdf642 | 825 | /* We ought to not compile any inline clones. */ |
cc636d56 | 826 | gcc_assert (!node->global.inlined_to); |
b0cdf642 | 827 | |
28df663b | 828 | if (flag_unit_at_a_time) |
829 | announce_function (decl); | |
961e3b13 | 830 | |
794da2bb | 831 | /* Generate RTL for the body of DECL. */ |
dc24ddbd | 832 | lang_hooks.callgraph.expand_function (decl); |
961e3b13 | 833 | |
4ee9c684 | 834 | /* Make sure that BE didn't give up on compiling. */ |
835 | /* ??? Can happen with nested function of extern inline. */ | |
cc636d56 | 836 | gcc_assert (TREE_ASM_WRITTEN (node->decl)); |
b0cdf642 | 837 | |
ae01b312 | 838 | current_function_decl = NULL; |
8ec2a798 | 839 | if (!cgraph_preserve_function_body_p (node->decl)) |
4ee9c684 | 840 | { |
841 | DECL_SAVED_TREE (node->decl) = NULL; | |
842 | DECL_STRUCT_FUNCTION (node->decl) = NULL; | |
4ee9c684 | 843 | DECL_INITIAL (node->decl) = error_mark_node; |
7edd21a5 | 844 | /* Eliminate all call edges. This is important so the call_expr no longer |
8ec2a798 | 845 | points to the dead function body. */ |
846 | while (node->callees) | |
847 | cgraph_remove_edge (node->callees); | |
4ee9c684 | 848 | } |
ae01b312 | 849 | } |
850 | ||
d7c6d889 | 851 | /* Fill array order with all nodes with output flag set in the reverse |
852 | topological order. */ | |
611e5405 | 853 | |
d7c6d889 | 854 | static int |
855 | cgraph_postorder (struct cgraph_node **order) | |
ae01b312 | 856 | { |
857 | struct cgraph_node *node, *node2; | |
ae01b312 | 858 | int stack_size = 0; |
859 | int order_pos = 0; | |
860 | struct cgraph_edge *edge, last; | |
ae01b312 | 861 | |
d7c6d889 | 862 | struct cgraph_node **stack = |
746149b7 | 863 | xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *)); |
ae01b312 | 864 | |
e6d2b2d8 | 865 | /* We have to deal with cycles nicely, so use a depth first traversal |
866 | output algorithm. Ignore the fact that some functions won't need | |
867 | to be output and put them into order as well, so we get dependencies | |
4ee9c684 | 868 | right through intline functions. */ |
ae01b312 | 869 | for (node = cgraph_nodes; node; node = node->next) |
870 | node->aux = NULL; | |
871 | for (node = cgraph_nodes; node; node = node->next) | |
3d7bfc56 | 872 | if (!node->aux) |
ae01b312 | 873 | { |
874 | node2 = node; | |
875 | if (!node->callers) | |
876 | node->aux = &last; | |
877 | else | |
878 | node->aux = node->callers; | |
879 | while (node2) | |
880 | { | |
881 | while (node2->aux != &last) | |
882 | { | |
883 | edge = node2->aux; | |
884 | if (edge->next_caller) | |
885 | node2->aux = edge->next_caller; | |
886 | else | |
887 | node2->aux = &last; | |
888 | if (!edge->caller->aux) | |
889 | { | |
890 | if (!edge->caller->callers) | |
891 | edge->caller->aux = &last; | |
892 | else | |
893 | edge->caller->aux = edge->caller->callers; | |
894 | stack[stack_size++] = node2; | |
895 | node2 = edge->caller; | |
896 | break; | |
897 | } | |
898 | } | |
899 | if (node2->aux == &last) | |
900 | { | |
901 | order[order_pos++] = node2; | |
902 | if (stack_size) | |
903 | node2 = stack[--stack_size]; | |
904 | else | |
905 | node2 = NULL; | |
906 | } | |
907 | } | |
908 | } | |
d7c6d889 | 909 | free (stack); |
910 | return order_pos; | |
911 | } | |
912 | ||
acc70efa | 913 | |
bc5cab3b | 914 | /* Perform reachability analysis and reclaim all unreachable nodes. |
915 | This function also remove unneeded bodies of extern inline functions | |
916 | and thus needs to be done only after inlining decisions has been made. */ | |
917 | static bool | |
918 | cgraph_remove_unreachable_nodes (void) | |
919 | { | |
920 | struct cgraph_node *first = (void *) 1; | |
921 | struct cgraph_node *node; | |
922 | bool changed = false; | |
923 | int insns = 0; | |
924 | ||
b0cdf642 | 925 | #ifdef ENABLE_CHECKING |
926 | verify_cgraph (); | |
927 | #endif | |
bc5cab3b | 928 | if (cgraph_dump_file) |
929 | fprintf (cgraph_dump_file, "\nReclaiming functions:"); | |
930 | #ifdef ENABLE_CHECKING | |
931 | for (node = cgraph_nodes; node; node = node->next) | |
cc636d56 | 932 | gcc_assert (!node->aux); |
bc5cab3b | 933 | #endif |
934 | for (node = cgraph_nodes; node; node = node->next) | |
b33eaee0 | 935 | if (node->needed && !node->global.inlined_to |
936 | && (!DECL_EXTERNAL (node->decl) || !node->analyzed)) | |
bc5cab3b | 937 | { |
938 | node->aux = first; | |
939 | first = node; | |
940 | } | |
cc636d56 | 941 | else |
942 | gcc_assert (!node->aux); | |
bc5cab3b | 943 | |
944 | /* Perform reachability analysis. As a special case do not consider | |
945 | extern inline functions not inlined as live because we won't output | |
946 | them at all. */ | |
947 | while (first != (void *) 1) | |
948 | { | |
949 | struct cgraph_edge *e; | |
950 | node = first; | |
951 | first = first->aux; | |
952 | ||
953 | for (e = node->callees; e; e = e->next_callee) | |
954 | if (!e->callee->aux | |
955 | && node->analyzed | |
956 | && (!e->inline_failed || !e->callee->analyzed | |
957 | || !DECL_EXTERNAL (e->callee->decl))) | |
958 | { | |
959 | e->callee->aux = first; | |
960 | first = e->callee; | |
961 | } | |
962 | } | |
963 | ||
964 | /* Remove unreachable nodes. Extern inline functions need special care; | |
965 | Unreachable extern inline functions shall be removed. | |
966 | Reachable extern inline functions we never inlined shall get their bodies | |
4ee9c684 | 967 | eliminated. |
bc5cab3b | 968 | Reachable extern inline functions we sometimes inlined will be turned into |
969 | unanalyzed nodes so they look like for true extern functions to the rest | |
9c9bad97 | 970 | of code. Body of such functions is released via remove_node once the |
b0cdf642 | 971 | inline clones are eliminated. */ |
bc5cab3b | 972 | for (node = cgraph_nodes; node; node = node->next) |
973 | { | |
974 | if (!node->aux) | |
975 | { | |
976 | int local_insns; | |
977 | tree decl = node->decl; | |
978 | ||
b33eaee0 | 979 | node->global.inlined_to = NULL; |
cbc44df5 | 980 | if (DECL_STRUCT_FUNCTION (decl)) |
bc5cab3b | 981 | local_insns = node->local.self_insns; |
982 | else | |
983 | local_insns = 0; | |
984 | if (cgraph_dump_file) | |
985 | fprintf (cgraph_dump_file, " %s", cgraph_node_name (node)); | |
986 | if (!node->analyzed || !DECL_EXTERNAL (node->decl)) | |
987 | cgraph_remove_node (node); | |
988 | else | |
989 | { | |
990 | struct cgraph_edge *e; | |
991 | ||
992 | for (e = node->callers; e; e = e->next_caller) | |
993 | if (e->caller->aux) | |
994 | break; | |
995 | if (e || node->needed) | |
996 | { | |
b0cdf642 | 997 | struct cgraph_node *clone; |
998 | ||
999 | for (clone = node->next_clone; clone; | |
1000 | clone = clone->next_clone) | |
1001 | if (clone->aux) | |
1002 | break; | |
1003 | if (!clone) | |
1004 | { | |
1005 | DECL_SAVED_TREE (node->decl) = NULL; | |
1006 | DECL_STRUCT_FUNCTION (node->decl) = NULL; | |
b0cdf642 | 1007 | DECL_INITIAL (node->decl) = error_mark_node; |
1008 | } | |
bc5cab3b | 1009 | while (node->callees) |
b0cdf642 | 1010 | cgraph_remove_edge (node->callees); |
bc5cab3b | 1011 | node->analyzed = false; |
1012 | } | |
1013 | else | |
1014 | cgraph_remove_node (node); | |
1015 | } | |
1016 | if (!DECL_SAVED_TREE (decl)) | |
1017 | insns += local_insns; | |
1018 | changed = true; | |
1019 | } | |
1020 | } | |
1021 | for (node = cgraph_nodes; node; node = node->next) | |
1022 | node->aux = NULL; | |
1023 | if (cgraph_dump_file) | |
1024 | fprintf (cgraph_dump_file, "\nReclaimed %i insns", insns); | |
1025 | return changed; | |
1026 | } | |
1027 | ||
d7c6d889 | 1028 | /* Estimate size of the function after inlining WHAT into TO. */ |
1029 | ||
1030 | static int | |
d9d9733a | 1031 | cgraph_estimate_size_after_inlining (int times, struct cgraph_node *to, |
d7c6d889 | 1032 | struct cgraph_node *what) |
1033 | { | |
e4200070 | 1034 | return (what->global.insns - INSNS_PER_CALL) * times + to->global.insns; |
d7c6d889 | 1035 | } |
1036 | ||
1037 | /* Estimate the growth caused by inlining NODE into all callees. */ | |
1038 | ||
1039 | static int | |
1040 | cgraph_estimate_growth (struct cgraph_node *node) | |
1041 | { | |
1042 | int growth = 0; | |
d7c6d889 | 1043 | struct cgraph_edge *e; |
1044 | ||
1045 | for (e = node->callers; e; e = e->next_caller) | |
611e5405 | 1046 | if (e->inline_failed) |
b0cdf642 | 1047 | growth += (cgraph_estimate_size_after_inlining (1, e->caller, node) |
1048 | - e->caller->global.insns); | |
d7c6d889 | 1049 | |
1050 | /* ??? Wrong for self recursive functions or cases where we decide to not | |
1051 | inline for different reasons, but it is not big deal as in that case | |
1052 | we will keep the body around, but we will also avoid some inlining. */ | |
4ee9c684 | 1053 | if (!node->needed && !DECL_EXTERNAL (node->decl)) |
b0cdf642 | 1054 | growth -= node->global.insns; |
d7c6d889 | 1055 | |
1056 | return growth; | |
1057 | } | |
1058 | ||
b0cdf642 | 1059 | /* E is expected to be an edge being inlined. Clone destination node of |
1060 | the edge and redirect it to the new clone. | |
365db11e | 1061 | DUPLICATE is used for bookkeeping on whether we are actually creating new |
b0cdf642 | 1062 | clones or re-using node originally representing out-of-line function call. |
1063 | */ | |
1064 | void | |
1065 | cgraph_clone_inlined_nodes (struct cgraph_edge *e, bool duplicate) | |
1066 | { | |
1067 | struct cgraph_node *n; | |
1068 | ||
7bd28bba | 1069 | /* We may eliminate the need for out-of-line copy to be output. In that |
b0cdf642 | 1070 | case just go ahead and re-use it. */ |
1071 | if (!e->callee->callers->next_caller | |
1072 | && (!e->callee->needed || DECL_EXTERNAL (e->callee->decl)) | |
b0cdf642 | 1073 | && duplicate |
1074 | && flag_unit_at_a_time) | |
1075 | { | |
cc636d56 | 1076 | gcc_assert (!e->callee->global.inlined_to); |
b0cdf642 | 1077 | if (!DECL_EXTERNAL (e->callee->decl)) |
1078 | overall_insns -= e->callee->global.insns, nfunctions_inlined++; | |
1079 | duplicate = 0; | |
1080 | } | |
1081 | else if (duplicate) | |
1082 | { | |
1083 | n = cgraph_clone_node (e->callee); | |
1084 | cgraph_redirect_edge_callee (e, n); | |
1085 | } | |
80a85d8a | 1086 | |
b0cdf642 | 1087 | if (e->caller->global.inlined_to) |
1088 | e->callee->global.inlined_to = e->caller->global.inlined_to; | |
1089 | else | |
1090 | e->callee->global.inlined_to = e->caller; | |
1091 | ||
9c9bad97 | 1092 | /* Recursively clone all bodies. */ |
b0cdf642 | 1093 | for (e = e->callee->callees; e; e = e->next_callee) |
1094 | if (!e->inline_failed) | |
1095 | cgraph_clone_inlined_nodes (e, duplicate); | |
1096 | } | |
1097 | ||
1098 | /* Mark edge E as inlined and update callgraph accordingly. */ | |
1099 | ||
1100 | void | |
1101 | cgraph_mark_inline_edge (struct cgraph_edge *e) | |
d7c6d889 | 1102 | { |
b0cdf642 | 1103 | int old_insns = 0, new_insns = 0; |
1104 | struct cgraph_node *to = NULL, *what; | |
1105 | ||
cc636d56 | 1106 | gcc_assert (e->inline_failed); |
b0cdf642 | 1107 | e->inline_failed = NULL; |
d7c6d889 | 1108 | |
b0cdf642 | 1109 | if (!e->callee->global.inlined && flag_unit_at_a_time) |
e1232ce2 | 1110 | DECL_POSSIBLY_INLINED (e->callee->decl) = true; |
b0cdf642 | 1111 | e->callee->global.inlined = true; |
1112 | ||
1113 | cgraph_clone_inlined_nodes (e, true); | |
d7c6d889 | 1114 | |
b0cdf642 | 1115 | what = e->callee; |
d7c6d889 | 1116 | |
0bed3869 | 1117 | /* Now update size of caller and all functions caller is inlined into. */ |
b0cdf642 | 1118 | for (;e && !e->inline_failed; e = e->caller->callers) |
d7c6d889 | 1119 | { |
b0cdf642 | 1120 | old_insns = e->caller->global.insns; |
1121 | new_insns = cgraph_estimate_size_after_inlining (1, e->caller, | |
1122 | what); | |
cc636d56 | 1123 | gcc_assert (new_insns >= 0); |
b0cdf642 | 1124 | to = e->caller; |
1125 | to->global.insns = new_insns; | |
d7c6d889 | 1126 | } |
cc636d56 | 1127 | gcc_assert (what->global.inlined_to == to); |
b0cdf642 | 1128 | overall_insns += new_insns - old_insns; |
1129 | ncalls_inlined++; | |
1130 | } | |
1131 | ||
1132 | /* Mark all calls of EDGE->CALLEE inlined into EDGE->CALLER. | |
1133 | Return following unredirected edge in the list of callers | |
1134 | of EDGE->CALLEE */ | |
1135 | ||
1136 | static struct cgraph_edge * | |
1137 | cgraph_mark_inline (struct cgraph_edge *edge) | |
1138 | { | |
1139 | struct cgraph_node *to = edge->caller; | |
1140 | struct cgraph_node *what = edge->callee; | |
1141 | struct cgraph_edge *e, *next; | |
1142 | int times = 0; | |
1143 | ||
9c9bad97 | 1144 | /* Look for all calls, mark them inline and clone recursively |
b0cdf642 | 1145 | all inlined functions. */ |
1146 | for (e = what->callers; e; e = next) | |
d7c6d889 | 1147 | { |
b0cdf642 | 1148 | next = e->next_caller; |
1149 | if (e->caller == to && e->inline_failed) | |
1150 | { | |
1151 | cgraph_mark_inline_edge (e); | |
1152 | if (e == edge) | |
1153 | edge = next; | |
cc636d56 | 1154 | times++; |
b0cdf642 | 1155 | } |
d7c6d889 | 1156 | } |
cc636d56 | 1157 | gcc_assert (times); |
b0cdf642 | 1158 | return edge; |
d7c6d889 | 1159 | } |
1160 | ||
b0cdf642 | 1161 | /* Return false when inlining WHAT into TO is not good idea |
1162 | as it would cause too large growth of function bodies. */ | |
d7c6d889 | 1163 | |
1164 | static bool | |
d9d9733a | 1165 | cgraph_check_inline_limits (struct cgraph_node *to, struct cgraph_node *what, |
611e5405 | 1166 | const char **reason) |
80a85d8a | 1167 | { |
d7c6d889 | 1168 | int times = 0; |
1169 | struct cgraph_edge *e; | |
1170 | int newsize; | |
1171 | int limit; | |
1172 | ||
b0cdf642 | 1173 | if (to->global.inlined_to) |
1174 | to = to->global.inlined_to; | |
1175 | ||
d7c6d889 | 1176 | for (e = to->callees; e; e = e->next_callee) |
1177 | if (e->callee == what) | |
1178 | times++; | |
1179 | ||
1180 | /* When inlining large function body called once into small function, | |
1181 | take the inlined function as base for limiting the growth. */ | |
1182 | if (to->local.self_insns > what->local.self_insns) | |
1183 | limit = to->local.self_insns; | |
1184 | else | |
1185 | limit = what->local.self_insns; | |
1186 | ||
1187 | limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100; | |
1188 | ||
1189 | newsize = cgraph_estimate_size_after_inlining (times, to, what); | |
1190 | if (newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS) | |
1191 | && newsize > limit) | |
611e5405 | 1192 | { |
b0cdf642 | 1193 | if (reason) |
1194 | *reason = N_("--param large-function-growth limit reached"); | |
611e5405 | 1195 | return false; |
1196 | } | |
d7c6d889 | 1197 | return true; |
1198 | } | |
1199 | ||
e4200070 | 1200 | /* Return true when function N is small enough to be inlined. */ |
d7c6d889 | 1201 | |
1202 | static bool | |
1203 | cgraph_default_inline_p (struct cgraph_node *n) | |
1204 | { | |
1205 | if (!DECL_INLINE (n->decl) || !DECL_SAVED_TREE (n->decl)) | |
1206 | return false; | |
746149b7 | 1207 | if (DECL_DECLARED_INLINE_P (n->decl)) |
d7c6d889 | 1208 | return n->global.insns < MAX_INLINE_INSNS_SINGLE; |
746149b7 | 1209 | else |
1210 | return n->global.insns < MAX_INLINE_INSNS_AUTO; | |
d7c6d889 | 1211 | } |
1212 | ||
b0cdf642 | 1213 | /* Return true when inlining WHAT would create recursive inlining. |
1214 | We call recursive inlining all cases where same function appears more than | |
9c9bad97 | 1215 | once in the single recursion nest path in the inline graph. */ |
b0cdf642 | 1216 | |
1217 | static bool | |
1218 | cgraph_recursive_inlining_p (struct cgraph_node *to, | |
1219 | struct cgraph_node *what, | |
1220 | const char **reason) | |
1221 | { | |
4ee9c684 | 1222 | bool recursive; |
1223 | if (to->global.inlined_to) | |
1224 | recursive = what->decl == to->global.inlined_to->decl; | |
1225 | else | |
1226 | recursive = what->decl == to->decl; | |
365db11e | 1227 | /* Marking recursive function inline has sane semantic and thus we should |
4ee9c684 | 1228 | not warn on it. */ |
1229 | if (recursive && reason) | |
b0cdf642 | 1230 | *reason = (what->local.disregard_inline_limits |
1231 | ? N_("recursive inlining") : ""); | |
4ee9c684 | 1232 | return recursive; |
b0cdf642 | 1233 | } |
1234 | ||
1235 | /* Recompute heap nodes for each of callees. */ | |
1236 | static void | |
1237 | update_callee_keys (fibheap_t heap, struct fibnode **heap_node, | |
1238 | struct cgraph_node *node) | |
1239 | { | |
1240 | struct cgraph_edge *e; | |
1241 | ||
1242 | for (e = node->callees; e; e = e->next_callee) | |
1243 | if (e->inline_failed && heap_node[e->callee->uid]) | |
1244 | fibheap_replace_key (heap, heap_node[e->callee->uid], | |
1245 | cgraph_estimate_growth (e->callee)); | |
1246 | else if (!e->inline_failed) | |
1247 | update_callee_keys (heap, heap_node, e->callee); | |
1248 | } | |
1249 | ||
4ee9c684 | 1250 | /* Enqueue all recursive calls from NODE into queue linked via aux pointers |
1251 | in between FIRST and LAST. WHERE is used for bookkeeping while looking | |
1252 | int calls inlined within NODE. */ | |
1253 | static void | |
1254 | lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where, | |
1255 | struct cgraph_edge **first, struct cgraph_edge **last) | |
1256 | { | |
1257 | struct cgraph_edge *e; | |
1258 | for (e = where->callees; e; e = e->next_callee) | |
1259 | if (e->callee == node) | |
1260 | { | |
1261 | if (!*first) | |
1262 | *first = e; | |
1263 | else | |
1264 | (*last)->aux = e; | |
1265 | *last = e; | |
1266 | } | |
1267 | for (e = where->callees; e; e = e->next_callee) | |
1268 | if (!e->inline_failed) | |
1269 | lookup_recursive_calls (node, e->callee, first, last); | |
1270 | } | |
1271 | ||
1272 | /* Decide on recursive inlining: in the case function has recursive calls, | |
1273 | inline until body size reaches given argument. */ | |
1274 | static void | |
1275 | cgraph_decide_recursive_inlining (struct cgraph_node *node) | |
1276 | { | |
1277 | int limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO); | |
1278 | int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO); | |
1279 | struct cgraph_edge *first_call = NULL, *last_call = NULL; | |
1280 | struct cgraph_edge *last_in_current_depth; | |
1281 | struct cgraph_edge *e; | |
1282 | struct cgraph_node *master_clone; | |
1283 | int depth = 0; | |
1284 | int n = 0; | |
1285 | ||
1286 | if (DECL_DECLARED_INLINE_P (node->decl)) | |
1287 | { | |
1288 | limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE); | |
1289 | max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH); | |
1290 | } | |
1291 | ||
91275768 | 1292 | /* Make sure that function is small enough to be considered for inlining. */ |
4ee9c684 | 1293 | if (!max_depth |
1294 | || cgraph_estimate_size_after_inlining (1, node, node) >= limit) | |
1295 | return; | |
1296 | lookup_recursive_calls (node, node, &first_call, &last_call); | |
1297 | if (!first_call) | |
1298 | return; | |
1299 | ||
1300 | if (cgraph_dump_file) | |
1301 | fprintf (cgraph_dump_file, | |
1302 | "\nPerforming recursive inlining on %s\n", | |
1303 | cgraph_node_name (node)); | |
1304 | ||
1305 | /* We need original clone to copy around. */ | |
1306 | master_clone = cgraph_clone_node (node); | |
1307 | master_clone->needed = true; | |
1308 | for (e = master_clone->callees; e; e = e->next_callee) | |
1309 | if (!e->inline_failed) | |
1310 | cgraph_clone_inlined_nodes (e, true); | |
1311 | ||
1312 | /* Do the inlining and update list of recursive call during process. */ | |
1313 | last_in_current_depth = last_call; | |
1314 | while (first_call | |
1315 | && cgraph_estimate_size_after_inlining (1, node, master_clone) <= limit) | |
1316 | { | |
1317 | struct cgraph_edge *curr = first_call; | |
1318 | ||
1319 | first_call = first_call->aux; | |
1320 | curr->aux = NULL; | |
1321 | ||
1322 | cgraph_redirect_edge_callee (curr, master_clone); | |
1323 | cgraph_mark_inline_edge (curr); | |
1324 | lookup_recursive_calls (node, curr->callee, &first_call, &last_call); | |
1325 | ||
1326 | if (last_in_current_depth | |
1327 | && ++depth >= max_depth) | |
1328 | break; | |
1329 | n++; | |
1330 | } | |
1331 | ||
1332 | /* Cleanup queue pointers. */ | |
1333 | while (first_call) | |
1334 | { | |
1335 | struct cgraph_edge *next = first_call->aux; | |
1336 | first_call->aux = NULL; | |
1337 | first_call = next; | |
1338 | } | |
1339 | if (cgraph_dump_file) | |
1340 | fprintf (cgraph_dump_file, | |
1341 | "\n Inlined %i times, body grown from %i to %i insns\n", n, | |
1342 | master_clone->global.insns, node->global.insns); | |
1343 | ||
1344 | /* Remove master clone we used for inlining. We rely that clones inlined | |
1345 | into master clone gets queued just before master clone so we don't | |
1346 | need recursion. */ | |
1347 | for (node = cgraph_nodes; node != master_clone; | |
1348 | node = node->next) | |
1349 | if (node->global.inlined_to == master_clone) | |
1350 | cgraph_remove_node (node); | |
1351 | cgraph_remove_node (master_clone); | |
1352 | } | |
1353 | ||
611e5405 | 1354 | /* Set inline_failed for all callers of given function to REASON. */ |
1355 | ||
1356 | static void | |
1357 | cgraph_set_inline_failed (struct cgraph_node *node, const char *reason) | |
1358 | { | |
1359 | struct cgraph_edge *e; | |
1360 | ||
1361 | if (cgraph_dump_file) | |
1362 | fprintf (cgraph_dump_file, "Inlining failed: %s\n", reason); | |
1363 | for (e = node->callers; e; e = e->next_caller) | |
1364 | if (e->inline_failed) | |
1365 | e->inline_failed = reason; | |
1366 | } | |
1367 | ||
d7c6d889 | 1368 | /* We use greedy algorithm for inlining of small functions: |
1369 | All inline candidates are put into prioritized heap based on estimated | |
1370 | growth of the overall number of instructions and then update the estimates. | |
d9d9733a | 1371 | |
40e55fbb | 1372 | INLINED and INLINED_CALEES are just pointers to arrays large enough |
d7c6d889 | 1373 | to be passed to cgraph_inlined_into and cgraph_inlined_callees. */ |
1374 | ||
1375 | static void | |
b0cdf642 | 1376 | cgraph_decide_inlining_of_small_functions (void) |
d7c6d889 | 1377 | { |
80a85d8a | 1378 | struct cgraph_node *node; |
d7c6d889 | 1379 | fibheap_t heap = fibheap_new (); |
1380 | struct fibnode **heap_node = | |
746149b7 | 1381 | xcalloc (cgraph_max_uid, sizeof (struct fibnode *)); |
d7c6d889 | 1382 | int max_insns = ((HOST_WIDEST_INT) initial_insns |
1383 | * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100); | |
80a85d8a | 1384 | |
d7c6d889 | 1385 | /* Put all inline candidates into the heap. */ |
80a85d8a | 1386 | |
80a85d8a | 1387 | for (node = cgraph_nodes; node; node = node->next) |
1388 | { | |
d7c6d889 | 1389 | if (!node->local.inlinable || !node->callers |
611e5405 | 1390 | || node->local.disregard_inline_limits) |
d7c6d889 | 1391 | continue; |
1392 | ||
611e5405 | 1393 | if (!cgraph_default_inline_p (node)) |
1394 | { | |
1395 | cgraph_set_inline_failed (node, | |
1396 | N_("--param max-inline-insns-single limit reached")); | |
1397 | continue; | |
1398 | } | |
d7c6d889 | 1399 | heap_node[node->uid] = |
1400 | fibheap_insert (heap, cgraph_estimate_growth (node), node); | |
80a85d8a | 1401 | } |
d7c6d889 | 1402 | |
f79b6507 | 1403 | if (cgraph_dump_file) |
e4200070 | 1404 | fprintf (cgraph_dump_file, "\nDeciding on smaller functions:\n"); |
611e5405 | 1405 | while (overall_insns <= max_insns && (node = fibheap_extract_min (heap))) |
d7c6d889 | 1406 | { |
b0cdf642 | 1407 | struct cgraph_edge *e, *next; |
d7c6d889 | 1408 | int old_insns = overall_insns; |
1409 | ||
1410 | heap_node[node->uid] = NULL; | |
1411 | if (cgraph_dump_file) | |
e4200070 | 1412 | fprintf (cgraph_dump_file, |
1413 | "\nConsidering %s with %i insns\n" | |
1414 | " Estimated growth is %+i insns.\n", | |
d7c6d889 | 1415 | cgraph_node_name (node), node->global.insns, |
1416 | cgraph_estimate_growth (node)); | |
1417 | if (!cgraph_default_inline_p (node)) | |
1418 | { | |
611e5405 | 1419 | cgraph_set_inline_failed (node, |
1420 | N_("--param max-inline-insns-single limit reached after inlining into the callee")); | |
d7c6d889 | 1421 | continue; |
1422 | } | |
b0cdf642 | 1423 | for (e = node->callers; e; e = next) |
1424 | { | |
1425 | next = e->next_caller; | |
1426 | if (e->inline_failed) | |
1427 | { | |
1428 | struct cgraph_node *where; | |
1429 | ||
1430 | if (cgraph_recursive_inlining_p (e->caller, e->callee, | |
1431 | &e->inline_failed) | |
1432 | || !cgraph_check_inline_limits (e->caller, e->callee, | |
1433 | &e->inline_failed)) | |
1434 | { | |
1435 | if (cgraph_dump_file) | |
1436 | fprintf (cgraph_dump_file, " Not inlining into %s:%s.\n", | |
1437 | cgraph_node_name (e->caller), e->inline_failed); | |
1438 | continue; | |
1439 | } | |
1440 | next = cgraph_mark_inline (e); | |
1441 | where = e->caller; | |
1442 | if (where->global.inlined_to) | |
1443 | where = where->global.inlined_to; | |
1444 | ||
1445 | if (heap_node[where->uid]) | |
1446 | fibheap_replace_key (heap, heap_node[where->uid], | |
1447 | cgraph_estimate_growth (where)); | |
1448 | ||
1449 | if (cgraph_dump_file) | |
1450 | fprintf (cgraph_dump_file, | |
1451 | " Inlined into %s which now has %i insns.\n", | |
1452 | cgraph_node_name (e->caller), | |
1453 | e->caller->global.insns); | |
1454 | } | |
1455 | } | |
d7c6d889 | 1456 | |
4ee9c684 | 1457 | cgraph_decide_recursive_inlining (node); |
1458 | ||
e4200070 | 1459 | /* Similarly all functions called by the function we just inlined |
d7c6d889 | 1460 | are now called more times; update keys. */ |
b0cdf642 | 1461 | update_callee_keys (heap, heap_node, node); |
d7c6d889 | 1462 | |
d7c6d889 | 1463 | if (cgraph_dump_file) |
e4200070 | 1464 | fprintf (cgraph_dump_file, |
b0cdf642 | 1465 | " Inlined for a net change of %+i insns.\n", |
1466 | overall_insns - old_insns); | |
d7c6d889 | 1467 | } |
611e5405 | 1468 | while ((node = fibheap_extract_min (heap)) != NULL) |
1469 | if (!node->local.disregard_inline_limits) | |
1470 | cgraph_set_inline_failed (node, N_("--param inline-unit-growth limit reached")); | |
d7c6d889 | 1471 | fibheap_delete (heap); |
1472 | free (heap_node); | |
80a85d8a | 1473 | } |
1474 | ||
d7c6d889 | 1475 | /* Decide on the inlining. We do so in the topological order to avoid |
7bd28bba | 1476 | expenses on updating data structures. */ |
961e3b13 | 1477 | |
1478 | static void | |
d7c6d889 | 1479 | cgraph_decide_inlining (void) |
961e3b13 | 1480 | { |
d7c6d889 | 1481 | struct cgraph_node *node; |
1482 | int nnodes; | |
1483 | struct cgraph_node **order = | |
746149b7 | 1484 | xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *)); |
261e29f4 | 1485 | int old_insns = 0; |
b0cdf642 | 1486 | int i; |
961e3b13 | 1487 | |
d7c6d889 | 1488 | for (node = cgraph_nodes; node; node = node->next) |
0785e435 | 1489 | initial_insns += node->local.self_insns; |
d7c6d889 | 1490 | overall_insns = initial_insns; |
1491 | ||
1492 | nnodes = cgraph_postorder (order); | |
961e3b13 | 1493 | |
e4200070 | 1494 | if (cgraph_dump_file) |
1495 | fprintf (cgraph_dump_file, | |
1496 | "\nDeciding on inlining. Starting with %i insns.\n", | |
1497 | initial_insns); | |
1498 | ||
961e3b13 | 1499 | for (node = cgraph_nodes; node; node = node->next) |
d7c6d889 | 1500 | node->aux = 0; |
1501 | ||
1502 | if (cgraph_dump_file) | |
e4200070 | 1503 | fprintf (cgraph_dump_file, "\nInlining always_inline functions:\n"); |
d7c6d889 | 1504 | |
1505 | /* In the first pass mark all always_inline edges. Do this with a priority | |
e4200070 | 1506 | so none of our later choices will make this impossible. */ |
d7c6d889 | 1507 | for (i = nnodes - 1; i >= 0; i--) |
1508 | { | |
4ee9c684 | 1509 | struct cgraph_edge *e, *next; |
d7c6d889 | 1510 | |
1511 | node = order[i]; | |
1512 | ||
4ee9c684 | 1513 | if (!node->local.disregard_inline_limits) |
d7c6d889 | 1514 | continue; |
1515 | if (cgraph_dump_file) | |
1516 | fprintf (cgraph_dump_file, | |
e4200070 | 1517 | "\nConsidering %s %i insns (always inline)\n", |
e5afa947 | 1518 | cgraph_node_name (node), node->global.insns); |
4ee9c684 | 1519 | old_insns = overall_insns; |
1520 | for (e = node->callers; e; e = next) | |
d7c6d889 | 1521 | { |
4ee9c684 | 1522 | next = e->next_caller; |
1523 | if (!e->inline_failed) | |
b0cdf642 | 1524 | continue; |
4ee9c684 | 1525 | if (cgraph_recursive_inlining_p (e->caller, e->callee, |
b0cdf642 | 1526 | &e->inline_failed)) |
1527 | continue; | |
4ee9c684 | 1528 | cgraph_mark_inline_edge (e); |
d7c6d889 | 1529 | if (cgraph_dump_file) |
e4200070 | 1530 | fprintf (cgraph_dump_file, |
1531 | " Inlined into %s which now has %i insns.\n", | |
e5afa947 | 1532 | cgraph_node_name (e->caller), |
1533 | e->caller->global.insns); | |
d7c6d889 | 1534 | } |
4ee9c684 | 1535 | if (cgraph_dump_file) |
1536 | fprintf (cgraph_dump_file, | |
1537 | " Inlined for a net change of %+i insns.\n", | |
1538 | overall_insns - old_insns); | |
d7c6d889 | 1539 | } |
1540 | ||
99cf25d0 | 1541 | if (!flag_really_no_inline) |
1542 | { | |
b0cdf642 | 1543 | cgraph_decide_inlining_of_small_functions (); |
d7c6d889 | 1544 | |
99cf25d0 | 1545 | if (cgraph_dump_file) |
1546 | fprintf (cgraph_dump_file, "\nDeciding on functions called once:\n"); | |
d7c6d889 | 1547 | |
99cf25d0 | 1548 | /* And finally decide what functions are called once. */ |
d7c6d889 | 1549 | |
99cf25d0 | 1550 | for (i = nnodes - 1; i >= 0; i--) |
961e3b13 | 1551 | { |
99cf25d0 | 1552 | node = order[i]; |
1553 | ||
1554 | if (node->callers && !node->callers->next_caller && !node->needed | |
611e5405 | 1555 | && node->local.inlinable && node->callers->inline_failed |
99cf25d0 | 1556 | && !DECL_EXTERNAL (node->decl) && !DECL_COMDAT (node->decl)) |
961e3b13 | 1557 | { |
99cf25d0 | 1558 | bool ok = true; |
1559 | struct cgraph_node *node1; | |
1560 | ||
1561 | /* Verify that we won't duplicate the caller. */ | |
1562 | for (node1 = node->callers->caller; | |
fea157ce | 1563 | node1->callers && !node1->callers->inline_failed |
99cf25d0 | 1564 | && ok; node1 = node1->callers->caller) |
1565 | if (node1->callers->next_caller || node1->needed) | |
1566 | ok = false; | |
1567 | if (ok) | |
d7c6d889 | 1568 | { |
d7c6d889 | 1569 | if (cgraph_dump_file) |
e4200070 | 1570 | fprintf (cgraph_dump_file, |
99cf25d0 | 1571 | "\nConsidering %s %i insns.\n" |
1572 | " Called once from %s %i insns.\n", | |
1573 | cgraph_node_name (node), node->global.insns, | |
e4200070 | 1574 | cgraph_node_name (node->callers->caller), |
99cf25d0 | 1575 | node->callers->caller->global.insns); |
b0cdf642 | 1576 | |
99cf25d0 | 1577 | old_insns = overall_insns; |
611e5405 | 1578 | |
b0cdf642 | 1579 | if (cgraph_check_inline_limits (node->callers->caller, node, |
1580 | NULL)) | |
99cf25d0 | 1581 | { |
b0cdf642 | 1582 | cgraph_mark_inline (node->callers); |
99cf25d0 | 1583 | if (cgraph_dump_file) |
1584 | fprintf (cgraph_dump_file, | |
1585 | " Inlined into %s which now has %i insns" | |
1586 | " for a net change of %+i insns.\n", | |
1587 | cgraph_node_name (node->callers->caller), | |
1588 | node->callers->caller->global.insns, | |
1589 | overall_insns - old_insns); | |
1590 | } | |
1591 | else | |
1592 | { | |
1593 | if (cgraph_dump_file) | |
1594 | fprintf (cgraph_dump_file, | |
1595 | " Inline limit reached, not inlined.\n"); | |
1596 | } | |
d7c6d889 | 1597 | } |
961e3b13 | 1598 | } |
1599 | } | |
99cf25d0 | 1600 | } |
b0cdf642 | 1601 | |
1602 | /* We will never output extern functions we didn't inline. | |
1603 | ??? Perhaps we can prevent accounting of growth of external | |
1604 | inline functions. */ | |
bc5cab3b | 1605 | cgraph_remove_unreachable_nodes (); |
9f01464b | 1606 | |
1607 | if (cgraph_dump_file) | |
1608 | fprintf (cgraph_dump_file, | |
1609 | "\nInlined %i calls, eliminated %i functions, " | |
1610 | "%i insns turned to %i insns.\n\n", | |
1611 | ncalls_inlined, nfunctions_inlined, initial_insns, | |
1612 | overall_insns); | |
1613 | free (order); | |
961e3b13 | 1614 | } |
1615 | ||
19489abd | 1616 | /* Decide on the inlining. We do so in the topological order to avoid |
7bd28bba | 1617 | expenses on updating data structures. */ |
19489abd | 1618 | |
1619 | static void | |
1620 | cgraph_decide_inlining_incrementally (struct cgraph_node *node) | |
1621 | { | |
1622 | struct cgraph_edge *e; | |
19489abd | 1623 | |
1624 | /* First of all look for always inline functions. */ | |
1625 | for (e = node->callees; e; e = e->next_callee) | |
b0cdf642 | 1626 | if (e->callee->local.disregard_inline_limits |
1627 | && e->inline_failed | |
1628 | && !cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed) | |
611e5405 | 1629 | /* ??? It is possible that renaming variable removed the function body |
1630 | in duplicate_decls. See gcc.c-torture/compile/20011119-2.c */ | |
1631 | && DECL_SAVED_TREE (e->callee->decl)) | |
b0cdf642 | 1632 | cgraph_mark_inline (e); |
19489abd | 1633 | |
b0cdf642 | 1634 | /* Now do the automatic inlining. */ |
99cf25d0 | 1635 | if (!flag_really_no_inline) |
b0cdf642 | 1636 | for (e = node->callees; e; e = e->next_callee) |
1637 | if (e->callee->local.inlinable | |
1638 | && e->inline_failed | |
1639 | && !e->callee->local.disregard_inline_limits | |
1640 | && !cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed) | |
1641 | && cgraph_check_inline_limits (node, e->callee, &e->inline_failed) | |
1642 | && DECL_SAVED_TREE (e->callee->decl)) | |
1643 | { | |
1644 | if (cgraph_default_inline_p (e->callee)) | |
1645 | cgraph_mark_inline (e); | |
1646 | else | |
1647 | e->inline_failed | |
1648 | = N_("--param max-inline-insns-single limit reached"); | |
1649 | } | |
19489abd | 1650 | } |
1651 | ||
1652 | ||
b0cdf642 | 1653 | /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */ |
d7c6d889 | 1654 | |
1655 | bool | |
b0cdf642 | 1656 | cgraph_inline_p (struct cgraph_edge *e, const char **reason) |
d7c6d889 | 1657 | { |
b0cdf642 | 1658 | *reason = e->inline_failed; |
1659 | return !e->inline_failed; | |
d7c6d889 | 1660 | } |
b0cdf642 | 1661 | |
acc70efa | 1662 | |
acc70efa | 1663 | |
d9d9733a | 1664 | /* Expand all functions that must be output. |
1665 | ||
d7c6d889 | 1666 | Attempt to topologically sort the nodes so function is output when |
1667 | all called functions are already assembled to allow data to be | |
91c82c20 | 1668 | propagated across the callgraph. Use a stack to get smaller distance |
3927afe0 | 1669 | between a function and its callees (later we may choose to use a more |
d7c6d889 | 1670 | sophisticated algorithm for function reordering; we will likely want |
1671 | to use subsections to make the output functions appear in top-down | |
1672 | order). */ | |
1673 | ||
1674 | static void | |
a6868229 | 1675 | cgraph_expand_all_functions (void) |
d7c6d889 | 1676 | { |
1677 | struct cgraph_node *node; | |
1678 | struct cgraph_node **order = | |
746149b7 | 1679 | xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *)); |
b0cdf642 | 1680 | int order_pos = 0, new_order_pos = 0; |
d7c6d889 | 1681 | int i; |
1682 | ||
d7c6d889 | 1683 | order_pos = cgraph_postorder (order); |
cc636d56 | 1684 | gcc_assert (order_pos == cgraph_n_nodes); |
d7c6d889 | 1685 | |
7bd28bba | 1686 | /* Garbage collector may remove inline clones we eliminate during |
b0cdf642 | 1687 | optimization. So we must be sure to not reference them. */ |
1688 | for (i = 0; i < order_pos; i++) | |
1689 | if (order[i]->output) | |
1690 | order[new_order_pos++] = order[i]; | |
1691 | ||
1692 | for (i = new_order_pos - 1; i >= 0; i--) | |
d7c6d889 | 1693 | { |
1694 | node = order[i]; | |
1695 | if (node->output) | |
1696 | { | |
cc636d56 | 1697 | gcc_assert (node->reachable); |
d7c6d889 | 1698 | node->output = 0; |
1699 | cgraph_expand_function (node); | |
1700 | } | |
1701 | } | |
1702 | free (order); | |
1703 | } | |
1704 | ||
58f1b650 | 1705 | /* Mark all local functions. |
acc70efa | 1706 | |
1707 | A local function is one whose calls can occur only in the current | |
1708 | compilation unit and all its calls are explicit, so we can change | |
1709 | its calling convention. We simply mark all static functions whose | |
58f1b650 | 1710 | address is not taken as local. */ |
d7c6d889 | 1711 | |
1712 | static void | |
58f1b650 | 1713 | cgraph_mark_local_functions (void) |
d7c6d889 | 1714 | { |
1715 | struct cgraph_node *node; | |
1716 | ||
d7c6d889 | 1717 | /* Figure out functions we want to assemble. */ |
1718 | for (node = cgraph_nodes; node; node = node->next) | |
1719 | { | |
1720 | node->local.local = (!node->needed | |
1721 | && DECL_SAVED_TREE (node->decl) | |
1722 | && !TREE_PUBLIC (node->decl)); | |
d7c6d889 | 1723 | } |
acc70efa | 1724 | |
d7c6d889 | 1725 | if (cgraph_dump_file) |
acc70efa | 1726 | { |
1727 | fprintf (cgraph_dump_file, "\nMarking local functions:"); | |
1728 | for (node = cgraph_nodes; node; node = node->next) | |
1729 | if (node->local.local) | |
1730 | fprintf (cgraph_dump_file, " %s", cgraph_node_name (node)); | |
1731 | fprintf (cgraph_dump_file, "\n\n"); | |
58f1b650 | 1732 | } |
acc70efa | 1733 | } |
80a85d8a | 1734 | |
b0cdf642 | 1735 | /* Return true when function body of DECL still needs to be kept around |
1736 | for later re-use. */ | |
1737 | bool | |
1738 | cgraph_preserve_function_body_p (tree decl) | |
1739 | { | |
1740 | struct cgraph_node *node; | |
1741 | /* Keep the body; we're going to dump it. */ | |
0f9005dd | 1742 | if (dump_enabled_p (TDI_tree_all)) |
b0cdf642 | 1743 | return true; |
1744 | if (!cgraph_global_info_ready) | |
1745 | return (DECL_INLINE (decl) && !flag_really_no_inline); | |
1746 | /* Look if there is any clone around. */ | |
1747 | for (node = cgraph_node (decl); node; node = node->next_clone) | |
1748 | if (node->global.inlined_to) | |
1749 | return true; | |
1750 | return false; | |
1751 | } | |
1752 | ||
ae01b312 | 1753 | /* Perform simple optimizations based on callgraph. */ |
1754 | ||
1755 | void | |
d9d9733a | 1756 | cgraph_optimize (void) |
ae01b312 | 1757 | { |
b0cdf642 | 1758 | #ifdef ENABLE_CHECKING |
1759 | verify_cgraph (); | |
1760 | #endif | |
2ff66ee0 | 1761 | if (!flag_unit_at_a_time) |
1762 | return; | |
e9f08e82 | 1763 | |
1764 | process_pending_assemble_externals (); | |
1765 | ||
f79b6507 | 1766 | timevar_push (TV_CGRAPHOPT); |
d7c6d889 | 1767 | if (!quiet_flag) |
1768 | fprintf (stderr, "Performing intraprocedural optimizations\n"); | |
e4200070 | 1769 | |
58f1b650 | 1770 | cgraph_mark_local_functions (); |
f79b6507 | 1771 | if (cgraph_dump_file) |
1772 | { | |
e4200070 | 1773 | fprintf (cgraph_dump_file, "Marked "); |
f79b6507 | 1774 | dump_cgraph (cgraph_dump_file); |
1775 | } | |
80a85d8a | 1776 | |
b0cdf642 | 1777 | if (flag_inline_trees) |
1778 | cgraph_decide_inlining (); | |
80a85d8a | 1779 | cgraph_global_info_ready = true; |
f79b6507 | 1780 | if (cgraph_dump_file) |
1781 | { | |
e4200070 | 1782 | fprintf (cgraph_dump_file, "Optimized "); |
f79b6507 | 1783 | dump_cgraph (cgraph_dump_file); |
1784 | } | |
1785 | timevar_pop (TV_CGRAPHOPT); | |
ae01b312 | 1786 | |
d7c6d889 | 1787 | /* Output everything. */ |
e4200070 | 1788 | if (!quiet_flag) |
1789 | fprintf (stderr, "Assembling functions:\n"); | |
b0cdf642 | 1790 | #ifdef ENABLE_CHECKING |
1791 | verify_cgraph (); | |
1792 | #endif | |
acc70efa | 1793 | |
acc70efa | 1794 | cgraph_mark_functions_to_output (); |
1795 | ||
a6868229 | 1796 | cgraph_expand_all_functions (); |
f79b6507 | 1797 | if (cgraph_dump_file) |
1798 | { | |
e4200070 | 1799 | fprintf (cgraph_dump_file, "\nFinal "); |
f79b6507 | 1800 | dump_cgraph (cgraph_dump_file); |
1801 | } | |
b0cdf642 | 1802 | #ifdef ENABLE_CHECKING |
1803 | verify_cgraph (); | |
4ee9c684 | 1804 | /* Double check that all inline clones are gone and that all |
1805 | function bodies have been released from memory. */ | |
1806 | if (flag_unit_at_a_time | |
0f9005dd | 1807 | && !dump_enabled_p (TDI_tree_all) |
4ee9c684 | 1808 | && !(sorrycount || errorcount)) |
1809 | { | |
1810 | struct cgraph_node *node; | |
1811 | bool error_found = false; | |
1812 | ||
1813 | for (node = cgraph_nodes; node; node = node->next) | |
1814 | if (node->analyzed | |
1815 | && (node->global.inlined_to | |
1816 | || DECL_SAVED_TREE (node->decl))) | |
1817 | { | |
1818 | error_found = true; | |
1819 | dump_cgraph_node (stderr, node); | |
1820 | } | |
1821 | if (error_found) | |
1822 | internal_error ("Nodes with no released memory found."); | |
1823 | } | |
b0cdf642 | 1824 | #endif |
ae01b312 | 1825 | } |
b5530559 | 1826 | |
1827 | /* Generate and emit a static constructor or destructor. WHICH must be | |
1828 | one of 'I' or 'D'. BODY should be a STATEMENT_LIST containing | |
1829 | GENERIC statements. */ | |
1830 | ||
1831 | void | |
c5344b58 | 1832 | cgraph_build_static_cdtor (char which, tree body, int priority) |
b5530559 | 1833 | { |
1834 | static int counter = 0; | |
1835 | char which_buf[16]; | |
540edea7 | 1836 | tree decl, name, resdecl; |
b5530559 | 1837 | |
1838 | sprintf (which_buf, "%c_%d", which, counter++); | |
1839 | name = get_file_function_name_long (which_buf); | |
1840 | ||
1841 | decl = build_decl (FUNCTION_DECL, name, | |
1842 | build_function_type (void_type_node, void_list_node)); | |
1843 | current_function_decl = decl; | |
1844 | ||
540edea7 | 1845 | resdecl = build_decl (RESULT_DECL, NULL_TREE, void_type_node); |
1846 | DECL_ARTIFICIAL (resdecl) = 1; | |
1847 | DECL_IGNORED_P (resdecl) = 1; | |
1848 | DECL_RESULT (decl) = resdecl; | |
1849 | ||
b5530559 | 1850 | allocate_struct_function (decl); |
1851 | ||
1852 | TREE_STATIC (decl) = 1; | |
1853 | TREE_USED (decl) = 1; | |
1854 | DECL_ARTIFICIAL (decl) = 1; | |
1855 | DECL_IGNORED_P (decl) = 1; | |
1856 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1; | |
1857 | DECL_SAVED_TREE (decl) = body; | |
1858 | TREE_PUBLIC (decl) = ! targetm.have_ctors_dtors; | |
1859 | DECL_UNINLINABLE (decl) = 1; | |
1860 | ||
1861 | DECL_INITIAL (decl) = make_node (BLOCK); | |
1862 | TREE_USED (DECL_INITIAL (decl)) = 1; | |
1863 | ||
1864 | DECL_SOURCE_LOCATION (decl) = input_location; | |
1865 | cfun->function_end_locus = input_location; | |
1866 | ||
cc636d56 | 1867 | switch (which) |
1868 | { | |
1869 | case 'I': | |
1870 | DECL_STATIC_CONSTRUCTOR (decl) = 1; | |
1871 | break; | |
1872 | case 'D': | |
1873 | DECL_STATIC_DESTRUCTOR (decl) = 1; | |
1874 | break; | |
1875 | default: | |
1876 | gcc_unreachable (); | |
1877 | } | |
b5530559 | 1878 | |
1879 | gimplify_function_tree (decl); | |
1880 | ||
1881 | /* ??? We will get called LATE in the compilation process. */ | |
1882 | if (cgraph_global_info_ready) | |
6148a911 | 1883 | tree_rest_of_compilation (decl); |
b5530559 | 1884 | else |
1885 | cgraph_finalize_function (decl, 0); | |
1886 | ||
1887 | if (targetm.have_ctors_dtors) | |
1888 | { | |
1889 | void (*fn) (rtx, int); | |
1890 | ||
1891 | if (which == 'I') | |
1892 | fn = targetm.asm_out.constructor; | |
1893 | else | |
1894 | fn = targetm.asm_out.destructor; | |
c5344b58 | 1895 | fn (XEXP (DECL_RTL (decl), 0), priority); |
b5530559 | 1896 | } |
1897 | } | |
121f3051 | 1898 | |
1899 | void | |
1900 | init_cgraph (void) | |
1901 | { | |
1902 | cgraph_dump_file = dump_begin (TDI_cgraph, NULL); | |
1903 | } |