]>
Commit | Line | Data |
---|---|---|
a418679d | 1 | /* Callgraph based interprocedural optimizations. |
66647d44 | 2 | Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 |
cac67c08 | 3 | Free Software Foundation, Inc. |
1c4a429a JH |
4 | Contributed by Jan Hubicka |
5 | ||
6 | This file is part of GCC. | |
7 | ||
8 | GCC is free software; you can redistribute it and/or modify it under | |
9 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 10 | Software Foundation; either version 3, or (at your option) any later |
1c4a429a JH |
11 | version. |
12 | ||
13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
19 | along with GCC; see the file COPYING3. If not see |
20 | <http://www.gnu.org/licenses/>. */ | |
1c4a429a | 21 | |
18c6ada9 | 22 | /* This module implements main driver of compilation process as well as |
a418679d | 23 | few basic interprocedural optimizers. |
18c6ada9 JH |
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 | ||
efe75b6f JH |
35 | (There is one exception needed for implementing GCC extern inline |
36 | function.) | |
18c6ada9 | 37 | |
8a4a83ed | 38 | - varpool_finalize_variable |
18c6ada9 | 39 | |
1ae58c30 | 40 | This function has same behavior as the above but is used for static |
18c6ada9 JH |
41 | variables. |
42 | ||
43 | - cgraph_finalize_compilation_unit | |
44 | ||
efe75b6f JH |
45 | This function is called once (source level) compilation unit is finalized |
46 | and it will no longer change. | |
18c6ada9 | 47 | |
7e8b322a | 48 | In the the call-graph construction and local function |
18c6ada9 JH |
49 | analysis takes place here. Bodies of unreachable functions are released |
50 | to conserve memory usage. | |
51 | ||
efe75b6f JH |
52 | The function can be called multiple times when multiple source level |
53 | compilation units are combined (such as in C frontend) | |
18c6ada9 JH |
54 | |
55 | - cgraph_optimize | |
56 | ||
57 | In this unit-at-a-time compilation the intra procedural analysis takes | |
58 | place here. In particular the static functions whose address is never | |
59 | taken are marked as local. Backend can then use this information to | |
60 | modify calling conventions, do better inlining or similar optimizations. | |
61 | ||
18c6ada9 | 62 | - cgraph_mark_needed_node |
8a4a83ed | 63 | - varpool_mark_needed_node |
18c6ada9 | 64 | |
efe75b6f JH |
65 | When function or variable is referenced by some hidden way the call-graph |
66 | data structure must be updated accordingly by this function. | |
67 | There should be little need to call this function and all the references | |
68 | should be made explicit to cgraph code. At present these functions are | |
dbb23ff7 | 69 | used by C++ frontend to explicitly mark the keyed methods. |
18c6ada9 JH |
70 | |
71 | - analyze_expr callback | |
72 | ||
73 | This function is responsible for lowering tree nodes not understood by | |
74 | generic code into understandable ones or alternatively marking | |
75 | callgraph and varpool nodes referenced by the as needed. | |
76 | ||
77 | ??? On the tree-ssa genericizing should take place here and we will avoid | |
78 | need for these hooks (replacing them by genericizing hook) | |
79 | ||
7e8b322a | 80 | Analyzing of all functions is deferred |
18c6ada9 JH |
81 | to cgraph_finalize_compilation_unit and expansion into cgraph_optimize. |
82 | ||
83 | In cgraph_finalize_compilation_unit the reachable functions are | |
84 | analyzed. During analysis the call-graph edges from reachable | |
85 | functions are constructed and their destinations are marked as | |
86 | reachable. References to functions and variables are discovered too | |
87 | and variables found to be needed output to the assembly file. Via | |
88 | mark_referenced call in assemble_variable functions referenced by | |
89 | static variables are noticed too. | |
90 | ||
e1990f69 | 91 | The intra-procedural information is produced and its existence |
18c6ada9 JH |
92 | indicated by global_info_ready. Once this flag is set it is impossible |
93 | to change function from !reachable to reachable and thus | |
94 | assemble_variable no longer call mark_referenced. | |
95 | ||
96 | Finally the call-graph is topologically sorted and all reachable functions | |
97 | that has not been completely inlined or are not external are output. | |
98 | ||
99 | ??? It is possible that reference to function or variable is optimized | |
100 | out. We can not deal with this nicely because topological order is not | |
101 | suitable for it. For tree-ssa we may consider another pass doing | |
102 | optimization and re-discovering reachable functions. | |
103 | ||
104 | ??? Reorganize code so variables are output very last and only if they | |
105 | really has been referenced by produced code, so we catch more cases | |
7e8b322a | 106 | where reference has been optimized out. */ |
9b3e897d | 107 | |
6674a6ce | 108 | |
1c4a429a JH |
109 | #include "config.h" |
110 | #include "system.h" | |
111 | #include "coretypes.h" | |
112 | #include "tm.h" | |
113 | #include "tree.h" | |
c9b9aa64 | 114 | #include "rtl.h" |
6674a6ce | 115 | #include "tree-flow.h" |
1c4a429a JH |
116 | #include "tree-inline.h" |
117 | #include "langhooks.h" | |
0c58f841 | 118 | #include "pointer-set.h" |
1c4a429a JH |
119 | #include "toplev.h" |
120 | #include "flags.h" | |
121 | #include "ggc.h" | |
122 | #include "debug.h" | |
123 | #include "target.h" | |
124 | #include "cgraph.h" | |
dafc5b82 | 125 | #include "diagnostic.h" |
a194aa56 | 126 | #include "timevar.h" |
b58b1157 JH |
127 | #include "params.h" |
128 | #include "fibheap.h" | |
dc0bfe6a | 129 | #include "intl.h" |
902edd36 | 130 | #include "function.h" |
57fb5341 | 131 | #include "ipa-prop.h" |
726a989a RB |
132 | #include "gimple.h" |
133 | #include "tree-iterator.h" | |
b4861090 | 134 | #include "tree-pass.h" |
a406865a | 135 | #include "tree-dump.h" |
cd9c7bd2 | 136 | #include "output.h" |
3baf459d | 137 | #include "coverage.h" |
b58b1157 | 138 | |
a20af5b8 | 139 | static void cgraph_expand_all_functions (void); |
db0e878d AJ |
140 | static void cgraph_mark_functions_to_output (void); |
141 | static void cgraph_expand_function (struct cgraph_node *); | |
21c4a6a7 | 142 | static void cgraph_output_pending_asms (void); |
a406865a | 143 | static void cgraph_analyze_function (struct cgraph_node *); |
7dff32e6 | 144 | |
9b3e897d PB |
145 | static FILE *cgraph_dump_file; |
146 | ||
873c7164 MM |
147 | /* A vector of FUNCTION_DECLs declared as static constructors. */ |
148 | static GTY (()) VEC(tree, gc) *static_ctors; | |
149 | /* A vector of FUNCTION_DECLs declared as static destructors. */ | |
150 | static GTY (()) VEC(tree, gc) *static_dtors; | |
7be82279 JH |
151 | |
152 | /* When target does not have ctors and dtors, we call all constructor | |
c80b4100 | 153 | and destructor by special initialization/destruction function |
7be82279 JH |
154 | recognized by collect2. |
155 | ||
156 | When we are going to build this function, collect all constructors and | |
157 | destructors and turn them into normal functions. */ | |
158 | ||
159 | static void | |
160 | record_cdtor_fn (tree fndecl) | |
161 | { | |
bd85fcee JH |
162 | struct cgraph_node *node; |
163 | if (targetm.have_ctors_dtors | |
164 | || (!DECL_STATIC_CONSTRUCTOR (fndecl) | |
165 | && !DECL_STATIC_DESTRUCTOR (fndecl))) | |
7be82279 JH |
166 | return; |
167 | ||
168 | if (DECL_STATIC_CONSTRUCTOR (fndecl)) | |
169 | { | |
873c7164 | 170 | VEC_safe_push (tree, gc, static_ctors, fndecl); |
7be82279 | 171 | DECL_STATIC_CONSTRUCTOR (fndecl) = 0; |
7be82279 JH |
172 | } |
173 | if (DECL_STATIC_DESTRUCTOR (fndecl)) | |
174 | { | |
873c7164 | 175 | VEC_safe_push (tree, gc, static_dtors, fndecl); |
7be82279 | 176 | DECL_STATIC_DESTRUCTOR (fndecl) = 0; |
7be82279 | 177 | } |
bd85fcee JH |
178 | node = cgraph_node (fndecl); |
179 | node->local.disregard_inline_limits = 1; | |
180 | cgraph_mark_reachable_node (node); | |
7be82279 JH |
181 | } |
182 | ||
873c7164 MM |
183 | /* Define global constructors/destructor functions for the CDTORS, of |
184 | which they are LEN. The CDTORS are sorted by initialization | |
185 | priority. If CTOR_P is true, these are constructors; otherwise, | |
186 | they are destructors. */ | |
187 | ||
7be82279 | 188 | static void |
873c7164 | 189 | build_cdtor (bool ctor_p, tree *cdtors, size_t len) |
7be82279 | 190 | { |
873c7164 | 191 | size_t i; |
7be82279 | 192 | |
873c7164 MM |
193 | i = 0; |
194 | while (i < len) | |
195 | { | |
196 | tree body; | |
197 | tree fn; | |
198 | priority_type priority; | |
199 | ||
200 | priority = 0; | |
201 | body = NULL_TREE; | |
202 | /* Find the next batch of constructors/destructors with the same | |
203 | initialization priority. */ | |
204 | do | |
205 | { | |
206 | priority_type p; | |
207 | fn = cdtors[i]; | |
208 | p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn); | |
209 | if (!body) | |
210 | priority = p; | |
211 | else if (p != priority) | |
212 | break; | |
db3927fb AH |
213 | append_to_statement_list (build_function_call_expr (UNKNOWN_LOCATION, |
214 | fn, 0), | |
873c7164 MM |
215 | &body); |
216 | ++i; | |
217 | } | |
218 | while (i < len); | |
219 | gcc_assert (body != NULL_TREE); | |
220 | /* Generate a function to call all the function of like | |
221 | priority. */ | |
222 | cgraph_build_static_cdtor (ctor_p ? 'I' : 'D', body, priority); | |
223 | } | |
224 | } | |
225 | ||
226 | /* Comparison function for qsort. P1 and P2 are actually of type | |
227 | "tree *" and point to static constructors. DECL_INIT_PRIORITY is | |
228 | used to determine the sort order. */ | |
7be82279 | 229 | |
873c7164 MM |
230 | static int |
231 | compare_ctor (const void *p1, const void *p2) | |
232 | { | |
233 | tree f1; | |
234 | tree f2; | |
235 | int priority1; | |
236 | int priority2; | |
237 | ||
238 | f1 = *(const tree *)p1; | |
239 | f2 = *(const tree *)p2; | |
240 | priority1 = DECL_INIT_PRIORITY (f1); | |
241 | priority2 = DECL_INIT_PRIORITY (f2); | |
242 | ||
243 | if (priority1 < priority2) | |
244 | return -1; | |
245 | else if (priority1 > priority2) | |
246 | return 1; | |
247 | else | |
248 | /* Ensure a stable sort. */ | |
249 | return (const tree *)p1 - (const tree *)p2; | |
250 | } | |
251 | ||
252 | /* Comparison function for qsort. P1 and P2 are actually of type | |
253 | "tree *" and point to static destructors. DECL_FINI_PRIORITY is | |
254 | used to determine the sort order. */ | |
7be82279 | 255 | |
873c7164 MM |
256 | static int |
257 | compare_dtor (const void *p1, const void *p2) | |
258 | { | |
259 | tree f1; | |
260 | tree f2; | |
261 | int priority1; | |
262 | int priority2; | |
263 | ||
264 | f1 = *(const tree *)p1; | |
265 | f2 = *(const tree *)p2; | |
266 | priority1 = DECL_FINI_PRIORITY (f1); | |
267 | priority2 = DECL_FINI_PRIORITY (f2); | |
268 | ||
269 | if (priority1 < priority2) | |
270 | return -1; | |
271 | else if (priority1 > priority2) | |
272 | return 1; | |
273 | else | |
274 | /* Ensure a stable sort. */ | |
275 | return (const tree *)p1 - (const tree *)p2; | |
7be82279 JH |
276 | } |
277 | ||
278 | /* Generate functions to call static constructors and destructors | |
279 | for targets that do not support .ctors/.dtors sections. These | |
280 | functions have magic names which are detected by collect2. */ | |
281 | ||
282 | static void | |
283 | cgraph_build_cdtor_fns (void) | |
284 | { | |
873c7164 | 285 | if (!VEC_empty (tree, static_ctors)) |
7be82279 | 286 | { |
873c7164 MM |
287 | gcc_assert (!targetm.have_ctors_dtors); |
288 | qsort (VEC_address (tree, static_ctors), | |
289 | VEC_length (tree, static_ctors), | |
290 | sizeof (tree), | |
291 | compare_ctor); | |
292 | build_cdtor (/*ctor_p=*/true, | |
293 | VEC_address (tree, static_ctors), | |
294 | VEC_length (tree, static_ctors)); | |
295 | VEC_truncate (tree, static_ctors, 0); | |
7be82279 | 296 | } |
873c7164 MM |
297 | |
298 | if (!VEC_empty (tree, static_dtors)) | |
7be82279 | 299 | { |
873c7164 MM |
300 | gcc_assert (!targetm.have_ctors_dtors); |
301 | qsort (VEC_address (tree, static_dtors), | |
302 | VEC_length (tree, static_dtors), | |
303 | sizeof (tree), | |
304 | compare_dtor); | |
305 | build_cdtor (/*ctor_p=*/false, | |
306 | VEC_address (tree, static_dtors), | |
307 | VEC_length (tree, static_dtors)); | |
308 | VEC_truncate (tree, static_dtors, 0); | |
7be82279 JH |
309 | } |
310 | } | |
311 | ||
8dafba3c RH |
312 | /* Determine if function DECL is needed. That is, visible to something |
313 | either outside this translation unit, something magic in the system | |
7e8b322a | 314 | configury. */ |
8dafba3c | 315 | |
d7f09764 DN |
316 | bool |
317 | cgraph_decide_is_function_needed (struct cgraph_node *node, tree decl) | |
8dafba3c | 318 | { |
e7d6beb0 | 319 | /* If the user told us it is used, then it must be so. */ |
386b46cf JH |
320 | if (node->local.externally_visible) |
321 | return true; | |
322 | ||
e7d6beb0 JH |
323 | /* ??? If the assembler name is set by hand, it is possible to assemble |
324 | the name later after finalizing the function and the fact is noticed | |
325 | in assemble_name then. This is arguably a bug. */ | |
326 | if (DECL_ASSEMBLER_NAME_SET_P (decl) | |
327 | && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))) | |
328 | return true; | |
329 | ||
a1d31187 JH |
330 | /* With -fkeep-inline-functions we are keeping all inline functions except |
331 | for extern inline ones. */ | |
332 | if (flag_keep_inline_functions | |
333 | && DECL_DECLARED_INLINE_P (decl) | |
b521dcbe JH |
334 | && !DECL_EXTERNAL (decl) |
335 | && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))) | |
a1d31187 JH |
336 | return true; |
337 | ||
8dafba3c RH |
338 | /* If we decided it was needed before, but at the time we didn't have |
339 | the body of the function available, then it's still needed. We have | |
340 | to go back and re-check its dependencies now. */ | |
341 | if (node->needed) | |
342 | return true; | |
343 | ||
344 | /* Externally visible functions must be output. The exception is | |
c22cacf3 | 345 | COMDAT functions that must be output only when they are needed. |
04f77d0f JH |
346 | |
347 | When not optimizing, also output the static functions. (see | |
46f5f7f2 | 348 | PR24561), but don't do so for always_inline functions, functions |
b633db7b JH |
349 | declared inline and nested functions. These was optimized out |
350 | in the original implementation and it is unclear whether we want | |
6fc0bb99 | 351 | to change the behavior here. */ |
5d342ef9 | 352 | if (((TREE_PUBLIC (decl) |
b633db7b JH |
353 | || (!optimize && !node->local.disregard_inline_limits |
354 | && !DECL_DECLARED_INLINE_P (decl) | |
355 | && !node->origin)) | |
b20996ff JH |
356 | && !flag_whole_program |
357 | && !flag_lto | |
358 | && !flag_whopr) | |
ce91e74c | 359 | && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl)) |
8dafba3c RH |
360 | return true; |
361 | ||
362 | /* Constructors and destructors are reachable from the runtime by | |
363 | some mechanism. */ | |
364 | if (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)) | |
365 | return true; | |
366 | ||
8dafba3c RH |
367 | return false; |
368 | } | |
369 | ||
d60ab196 | 370 | /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these |
f45e0ad1 JH |
371 | functions into callgraph in a way so they look like ordinary reachable |
372 | functions inserted into callgraph already at construction time. */ | |
373 | ||
374 | bool | |
375 | cgraph_process_new_functions (void) | |
376 | { | |
377 | bool output = false; | |
378 | tree fndecl; | |
379 | struct cgraph_node *node; | |
380 | ||
381 | /* Note that this queue may grow as its being processed, as the new | |
382 | functions may generate new ones. */ | |
383 | while (cgraph_new_nodes) | |
384 | { | |
385 | node = cgraph_new_nodes; | |
386 | fndecl = node->decl; | |
387 | cgraph_new_nodes = cgraph_new_nodes->next_needed; | |
388 | switch (cgraph_state) | |
389 | { | |
390 | case CGRAPH_STATE_CONSTRUCTION: | |
391 | /* At construction time we just need to finalize function and move | |
392 | it into reachable functions list. */ | |
393 | ||
394 | node->next_needed = NULL; | |
395 | cgraph_finalize_function (fndecl, false); | |
396 | cgraph_mark_reachable_node (node); | |
397 | output = true; | |
398 | break; | |
399 | ||
400 | case CGRAPH_STATE_IPA: | |
7a388ee4 | 401 | case CGRAPH_STATE_IPA_SSA: |
f45e0ad1 JH |
402 | /* When IPA optimization already started, do all essential |
403 | transformations that has been already performed on the whole | |
404 | cgraph but not on this function. */ | |
405 | ||
726a989a | 406 | gimple_register_cfg_hooks (); |
f45e0ad1 JH |
407 | if (!node->analyzed) |
408 | cgraph_analyze_function (node); | |
409 | push_cfun (DECL_STRUCT_FUNCTION (fndecl)); | |
410 | current_function_decl = fndecl; | |
1920df6c | 411 | compute_inline_parameters (node); |
7a388ee4 JH |
412 | if ((cgraph_state == CGRAPH_STATE_IPA_SSA |
413 | && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl))) | |
414 | /* When not optimizing, be sure we run early local passes anyway | |
415 | to expand OMP. */ | |
416 | || !optimize) | |
8ddbbcae | 417 | execute_pass_list (pass_early_local_passes.pass.sub); |
f45e0ad1 JH |
418 | free_dominance_info (CDI_POST_DOMINATORS); |
419 | free_dominance_info (CDI_DOMINATORS); | |
420 | pop_cfun (); | |
421 | current_function_decl = NULL; | |
422 | break; | |
423 | ||
424 | case CGRAPH_STATE_EXPANSION: | |
425 | /* Functions created during expansion shall be compiled | |
426 | directly. */ | |
257eb6e3 | 427 | node->process = 0; |
f45e0ad1 JH |
428 | cgraph_expand_function (node); |
429 | break; | |
430 | ||
431 | default: | |
432 | gcc_unreachable (); | |
433 | break; | |
434 | } | |
129a37fc | 435 | cgraph_call_function_insertion_hooks (node); |
f45e0ad1 JH |
436 | } |
437 | return output; | |
438 | } | |
439 | ||
d71cc23f JH |
440 | /* As an GCC extension we allow redefinition of the function. The |
441 | semantics when both copies of bodies differ is not well defined. | |
442 | We replace the old body with new body so in unit at a time mode | |
443 | we always use new body, while in normal mode we may end up with | |
444 | old body inlined into some functions and new body expanded and | |
445 | inlined in others. | |
446 | ||
447 | ??? It may make more sense to use one body for inlining and other | |
448 | body for expanding the function but this is difficult to do. */ | |
449 | ||
450 | static void | |
451 | cgraph_reset_node (struct cgraph_node *node) | |
452 | { | |
257eb6e3 | 453 | /* If node->process is set, then we have already begun whole-unit analysis. |
7e8b322a JH |
454 | This is *not* testing for whether we've already emitted the function. |
455 | That case can be sort-of legitimately seen with real function redefinition | |
456 | errors. I would argue that the front end should never present us with | |
457 | such a case, but don't enforce that for now. */ | |
257eb6e3 | 458 | gcc_assert (!node->process); |
d71cc23f JH |
459 | |
460 | /* Reset our data structures so we can analyze the function again. */ | |
461 | memset (&node->local, 0, sizeof (node->local)); | |
462 | memset (&node->global, 0, sizeof (node->global)); | |
463 | memset (&node->rtl, 0, sizeof (node->rtl)); | |
464 | node->analyzed = false; | |
465 | node->local.redefined_extern_inline = true; | |
466 | node->local.finalized = false; | |
467 | ||
d71cc23f JH |
468 | cgraph_node_remove_callees (node); |
469 | ||
470 | /* We may need to re-queue the node for assembling in case | |
b86b3ea3 RG |
471 | we already proceeded it and ignored as not needed or got |
472 | a re-declaration in IMA mode. */ | |
473 | if (node->reachable) | |
d71cc23f JH |
474 | { |
475 | struct cgraph_node *n; | |
476 | ||
477 | for (n = cgraph_nodes_queue; n; n = n->next_needed) | |
478 | if (n == node) | |
479 | break; | |
480 | if (!n) | |
481 | node->reachable = 0; | |
482 | } | |
483 | } | |
d853a20e | 484 | |
953ff289 DN |
485 | static void |
486 | cgraph_lower_function (struct cgraph_node *node) | |
487 | { | |
488 | if (node->lowered) | |
489 | return; | |
a406865a RG |
490 | |
491 | if (node->nested) | |
492 | lower_nested_functions (node->decl); | |
493 | gcc_assert (!node->nested); | |
494 | ||
953ff289 DN |
495 | tree_lowering_passes (node->decl); |
496 | node->lowered = true; | |
497 | } | |
498 | ||
6b00c969 RH |
499 | /* DECL has been parsed. Take it, queue it, compile it at the whim of the |
500 | logic in effect. If NESTED is true, then our caller cannot stand to have | |
501 | the garbage collector run at the moment. We would need to either create | |
502 | a new GC context, or just not compile right now. */ | |
1c4a429a JH |
503 | |
504 | void | |
6b00c969 | 505 | cgraph_finalize_function (tree decl, bool nested) |
1c4a429a JH |
506 | { |
507 | struct cgraph_node *node = cgraph_node (decl); | |
508 | ||
d853a20e | 509 | if (node->local.finalized) |
d71cc23f | 510 | cgraph_reset_node (node); |
6b00c969 | 511 | |
6bad2617 | 512 | node->pid = cgraph_max_pid ++; |
d853a20e | 513 | notice_global_symbol (decl); |
f6981e16 | 514 | node->local.finalized = true; |
e21aff8a | 515 | node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL; |
d88e5c37 | 516 | node->finalized_by_frontend = true; |
7be82279 | 517 | record_cdtor_fn (node->decl); |
1c4a429a | 518 | |
d7f09764 | 519 | if (cgraph_decide_is_function_needed (node, decl)) |
8dafba3c RH |
520 | cgraph_mark_needed_node (node); |
521 | ||
ff5c4582 | 522 | /* Since we reclaim unreachable nodes at the end of every language |
e7d6beb0 JH |
523 | level unit, we need to be conservative about possible entry points |
524 | there. */ | |
ce91e74c | 525 | if ((TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))) |
e7d6beb0 JH |
526 | cgraph_mark_reachable_node (node); |
527 | ||
8dafba3c | 528 | /* If we've not yet emitted decl, tell the debug info about it. */ |
6b00c969 | 529 | if (!TREE_ASM_WRITTEN (decl)) |
8dafba3c | 530 | (*debug_hooks->deferred_inline_function) (decl); |
d173e685 | 531 | |
902edd36 JH |
532 | /* Possibly warn about unused parameters. */ |
533 | if (warn_unused_parameter) | |
534 | do_warn_unused_parameter (decl); | |
7e8b322a JH |
535 | |
536 | if (!nested) | |
537 | ggc_collect (); | |
1c4a429a JH |
538 | } |
539 | ||
f0c882ab JH |
540 | /* C99 extern inline keywords allow changing of declaration after function |
541 | has been finalized. We need to re-decide if we want to mark the function as | |
542 | needed then. */ | |
543 | ||
544 | void | |
545 | cgraph_mark_if_needed (tree decl) | |
546 | { | |
547 | struct cgraph_node *node = cgraph_node (decl); | |
d7f09764 | 548 | if (node->local.finalized && cgraph_decide_is_function_needed (node, decl)) |
f0c882ab JH |
549 | cgraph_mark_needed_node (node); |
550 | } | |
551 | ||
9187e02d JH |
552 | /* Return TRUE if NODE2 is equivalent to NODE or its clone. */ |
553 | static bool | |
554 | clone_of_p (struct cgraph_node *node, struct cgraph_node *node2) | |
555 | { | |
556 | while (node != node2 && node2) | |
557 | node2 = node2->clone_of; | |
558 | return node2 != NULL; | |
559 | } | |
560 | ||
18c6ada9 JH |
561 | /* Verify cgraph nodes of given cgraph node. */ |
562 | void | |
563 | verify_cgraph_node (struct cgraph_node *node) | |
564 | { | |
565 | struct cgraph_edge *e; | |
e21aff8a | 566 | struct function *this_cfun = DECL_STRUCT_FUNCTION (node->decl); |
2bafad93 | 567 | struct function *saved_cfun = cfun; |
e21aff8a | 568 | basic_block this_block; |
726a989a | 569 | gimple_stmt_iterator gsi; |
e0704a46 | 570 | bool error_found = false; |
18c6ada9 | 571 | |
5771bd91 RG |
572 | if (errorcount || sorrycount) |
573 | return; | |
574 | ||
18c6ada9 | 575 | timevar_push (TV_CGRAPH_VERIFY); |
2bafad93 JJ |
576 | /* debug_generic_stmt needs correct cfun */ |
577 | set_cfun (this_cfun); | |
18c6ada9 JH |
578 | for (e = node->callees; e; e = e->next_callee) |
579 | if (e->aux) | |
580 | { | |
ab532386 | 581 | error ("aux field set for edge %s->%s", |
4f1e4960 JM |
582 | identifier_to_locale (cgraph_node_name (e->caller)), |
583 | identifier_to_locale (cgraph_node_name (e->callee))); | |
18c6ada9 JH |
584 | error_found = true; |
585 | } | |
06191a23 JH |
586 | if (node->count < 0) |
587 | { | |
588 | error ("Execution count is negative"); | |
589 | error_found = true; | |
590 | } | |
b20996ff JH |
591 | if (node->global.inlined_to && node->local.externally_visible) |
592 | { | |
593 | error ("Externally visible inline clone"); | |
594 | error_found = true; | |
595 | } | |
596 | if (node->global.inlined_to && node->address_taken) | |
597 | { | |
598 | error ("Inline clone with address taken"); | |
599 | error_found = true; | |
600 | } | |
601 | if (node->global.inlined_to && node->needed) | |
602 | { | |
603 | error ("Inline clone is needed"); | |
604 | error_found = true; | |
605 | } | |
18c6ada9 JH |
606 | for (e = node->callers; e; e = e->next_caller) |
607 | { | |
06191a23 JH |
608 | if (e->count < 0) |
609 | { | |
610 | error ("caller edge count is negative"); | |
611 | error_found = true; | |
612 | } | |
45a80bb9 JH |
613 | if (e->frequency < 0) |
614 | { | |
615 | error ("caller edge frequency is negative"); | |
616 | error_found = true; | |
617 | } | |
618 | if (e->frequency > CGRAPH_FREQ_MAX) | |
619 | { | |
620 | error ("caller edge frequency is too large"); | |
621 | error_found = true; | |
622 | } | |
18c6ada9 JH |
623 | if (!e->inline_failed) |
624 | { | |
625 | if (node->global.inlined_to | |
626 | != (e->caller->global.inlined_to | |
627 | ? e->caller->global.inlined_to : e->caller)) | |
628 | { | |
ab532386 | 629 | error ("inlined_to pointer is wrong"); |
18c6ada9 JH |
630 | error_found = true; |
631 | } | |
632 | if (node->callers->next_caller) | |
633 | { | |
ab532386 | 634 | error ("multiple inline callers"); |
18c6ada9 JH |
635 | error_found = true; |
636 | } | |
637 | } | |
638 | else | |
639 | if (node->global.inlined_to) | |
640 | { | |
ab532386 | 641 | error ("inlined_to pointer set for noninline callers"); |
18c6ada9 JH |
642 | error_found = true; |
643 | } | |
644 | } | |
645 | if (!node->callers && node->global.inlined_to) | |
646 | { | |
95a52ebb | 647 | error ("inlined_to pointer is set but no predecessors found"); |
18c6ada9 JH |
648 | error_found = true; |
649 | } | |
650 | if (node->global.inlined_to == node) | |
651 | { | |
ab532386 | 652 | error ("inlined_to pointer refers to itself"); |
18c6ada9 JH |
653 | error_found = true; |
654 | } | |
655 | ||
69fb1284 | 656 | if (!cgraph_node (node->decl)) |
18c6ada9 | 657 | { |
69fb1284 | 658 | error ("node not found in cgraph_hash"); |
18c6ada9 JH |
659 | error_found = true; |
660 | } | |
c22cacf3 | 661 | |
9187e02d JH |
662 | if (node->clone_of) |
663 | { | |
664 | struct cgraph_node *n; | |
665 | for (n = node->clone_of->clones; n; n = n->next_sibling_clone) | |
666 | if (n == node) | |
667 | break; | |
668 | if (!n) | |
669 | { | |
670 | error ("node has wrong clone_of"); | |
671 | error_found = true; | |
672 | } | |
673 | } | |
674 | if (node->clones) | |
675 | { | |
676 | struct cgraph_node *n; | |
677 | for (n = node->clones; n; n = n->next_sibling_clone) | |
678 | if (n->clone_of != node) | |
679 | break; | |
680 | if (n) | |
681 | { | |
682 | error ("node has wrong clone list"); | |
683 | error_found = true; | |
684 | } | |
685 | } | |
686 | if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of) | |
687 | { | |
688 | error ("node is in clone list but it is not clone"); | |
689 | error_found = true; | |
690 | } | |
691 | if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node) | |
692 | { | |
693 | error ("node has wrong prev_clone pointer"); | |
694 | error_found = true; | |
695 | } | |
696 | if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node) | |
697 | { | |
698 | error ("double linked list of clones corrupted"); | |
699 | error_found = true; | |
700 | } | |
701 | ||
702 | if (node->analyzed && gimple_has_body_p (node->decl) | |
726a989a | 703 | && !TREE_ASM_WRITTEN (node->decl) |
d7f09764 DN |
704 | && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to) |
705 | && !flag_wpa) | |
18c6ada9 | 706 | { |
e21aff8a SB |
707 | if (this_cfun->cfg) |
708 | { | |
709 | /* The nodes we're interested in are never shared, so walk | |
710 | the tree ignoring duplicates. */ | |
2dee695b | 711 | struct pointer_set_t *visited_nodes = pointer_set_create (); |
e21aff8a SB |
712 | /* Reach the trees by walking over the CFG, and note the |
713 | enclosing basic-blocks in the call edges. */ | |
714 | FOR_EACH_BB_FN (this_block, this_cfun) | |
726a989a RB |
715 | for (gsi = gsi_start_bb (this_block); |
716 | !gsi_end_p (gsi); | |
717 | gsi_next (&gsi)) | |
e0704a46 | 718 | { |
726a989a | 719 | gimple stmt = gsi_stmt (gsi); |
e0704a46 | 720 | tree decl; |
726a989a | 721 | if (is_gimple_call (stmt) && (decl = gimple_call_fndecl (stmt))) |
e0704a46 JH |
722 | { |
723 | struct cgraph_edge *e = cgraph_edge (node, stmt); | |
724 | if (e) | |
725 | { | |
726 | if (e->aux) | |
727 | { | |
ab532386 | 728 | error ("shared call_stmt:"); |
726a989a | 729 | debug_gimple_stmt (stmt); |
e0704a46 JH |
730 | error_found = true; |
731 | } | |
9187e02d JH |
732 | if (!clone_of_p (cgraph_node (decl), e->callee) |
733 | && !e->callee->global.inlined_to) | |
e0704a46 | 734 | { |
ab532386 | 735 | error ("edge points to wrong declaration:"); |
e0704a46 JH |
736 | debug_tree (e->callee->decl); |
737 | fprintf (stderr," Instead of:"); | |
738 | debug_tree (decl); | |
739 | } | |
740 | e->aux = (void *)1; | |
741 | } | |
742 | else | |
743 | { | |
ab532386 | 744 | error ("missing callgraph edge for call stmt:"); |
726a989a | 745 | debug_gimple_stmt (stmt); |
e0704a46 JH |
746 | error_found = true; |
747 | } | |
748 | } | |
749 | } | |
e21aff8a | 750 | pointer_set_destroy (visited_nodes); |
e21aff8a SB |
751 | } |
752 | else | |
753 | /* No CFG available?! */ | |
754 | gcc_unreachable (); | |
755 | ||
18c6ada9 JH |
756 | for (e = node->callees; e; e = e->next_callee) |
757 | { | |
3e293154 | 758 | if (!e->aux && !e->indirect_call) |
18c6ada9 | 759 | { |
ab532386 | 760 | error ("edge %s->%s has no corresponding call_stmt", |
4f1e4960 JM |
761 | identifier_to_locale (cgraph_node_name (e->caller)), |
762 | identifier_to_locale (cgraph_node_name (e->callee))); | |
726a989a | 763 | debug_gimple_stmt (e->call_stmt); |
18c6ada9 JH |
764 | error_found = true; |
765 | } | |
766 | e->aux = 0; | |
767 | } | |
768 | } | |
769 | if (error_found) | |
770 | { | |
771 | dump_cgraph_node (stderr, node); | |
ab532386 | 772 | internal_error ("verify_cgraph_node failed"); |
18c6ada9 | 773 | } |
2bafad93 | 774 | set_cfun (saved_cfun); |
18c6ada9 JH |
775 | timevar_pop (TV_CGRAPH_VERIFY); |
776 | } | |
777 | ||
778 | /* Verify whole cgraph structure. */ | |
779 | void | |
780 | verify_cgraph (void) | |
781 | { | |
782 | struct cgraph_node *node; | |
783 | ||
89480522 JH |
784 | if (sorrycount || errorcount) |
785 | return; | |
786 | ||
18c6ada9 JH |
787 | for (node = cgraph_nodes; node; node = node->next) |
788 | verify_cgraph_node (node); | |
789 | } | |
790 | ||
474eccc6 ILT |
791 | /* Output all asm statements we have stored up to be output. */ |
792 | ||
793 | static void | |
794 | cgraph_output_pending_asms (void) | |
795 | { | |
796 | struct cgraph_asm_node *can; | |
797 | ||
798 | if (errorcount || sorrycount) | |
799 | return; | |
800 | ||
801 | for (can = cgraph_asm_nodes; can; can = can->next) | |
802 | assemble_asm (can->asm_str); | |
803 | cgraph_asm_nodes = NULL; | |
804 | } | |
805 | ||
e767b5be | 806 | /* Analyze the function scheduled to be output. */ |
a406865a | 807 | static void |
e767b5be JH |
808 | cgraph_analyze_function (struct cgraph_node *node) |
809 | { | |
a406865a | 810 | tree save = current_function_decl; |
e767b5be JH |
811 | tree decl = node->decl; |
812 | ||
25c84396 | 813 | current_function_decl = decl; |
e21aff8a | 814 | push_cfun (DECL_STRUCT_FUNCTION (decl)); |
a406865a RG |
815 | |
816 | /* Make sure to gimplify bodies only once. During analyzing a | |
817 | function we lower it, which will require gimplified nested | |
818 | functions, so we can end up here with an already gimplified | |
819 | body. */ | |
820 | if (!gimple_body (decl)) | |
821 | gimplify_function_tree (decl); | |
822 | dump_function (TDI_generic, decl); | |
823 | ||
e21aff8a | 824 | cgraph_lower_function (node); |
6a84c098 | 825 | node->analyzed = true; |
e767b5be | 826 | |
e21aff8a | 827 | pop_cfun (); |
a406865a | 828 | current_function_decl = save; |
e767b5be JH |
829 | } |
830 | ||
386b46cf JH |
831 | /* Look for externally_visible and used attributes and mark cgraph nodes |
832 | accordingly. | |
833 | ||
834 | We cannot mark the nodes at the point the attributes are processed (in | |
835 | handle_*_attribute) because the copy of the declarations available at that | |
836 | point may not be canonical. For example, in: | |
837 | ||
838 | void f(); | |
839 | void f() __attribute__((used)); | |
840 | ||
841 | the declaration we see in handle_used_attribute will be the second | |
842 | declaration -- but the front end will subsequently merge that declaration | |
843 | with the original declaration and discard the second declaration. | |
844 | ||
845 | Furthermore, we can't mark these nodes in cgraph_finalize_function because: | |
846 | ||
847 | void f() {} | |
848 | void f() __attribute__((externally_visible)); | |
849 | ||
850 | is valid. | |
851 | ||
852 | So, we walk the nodes at the end of the translation unit, applying the | |
853 | attributes at that point. */ | |
854 | ||
855 | static void | |
856 | process_function_and_variable_attributes (struct cgraph_node *first, | |
8a4a83ed | 857 | struct varpool_node *first_var) |
386b46cf JH |
858 | { |
859 | struct cgraph_node *node; | |
8a4a83ed | 860 | struct varpool_node *vnode; |
386b46cf JH |
861 | |
862 | for (node = cgraph_nodes; node != first; node = node->next) | |
863 | { | |
864 | tree decl = node->decl; | |
865 | if (lookup_attribute ("used", DECL_ATTRIBUTES (decl))) | |
866 | { | |
867 | mark_decl_referenced (decl); | |
868 | if (node->local.finalized) | |
869 | cgraph_mark_needed_node (node); | |
870 | } | |
871 | if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl))) | |
872 | { | |
343d4b27 | 873 | if (! TREE_PUBLIC (node->decl)) |
c5d75364 MLI |
874 | warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes, |
875 | "%<externally_visible%>" | |
876 | " attribute have effect only on public objects"); | |
b20996ff JH |
877 | else if (node->local.finalized) |
878 | cgraph_mark_needed_node (node); | |
386b46cf JH |
879 | } |
880 | } | |
8a4a83ed | 881 | for (vnode = varpool_nodes; vnode != first_var; vnode = vnode->next) |
386b46cf JH |
882 | { |
883 | tree decl = vnode->decl; | |
884 | if (lookup_attribute ("used", DECL_ATTRIBUTES (decl))) | |
885 | { | |
886 | mark_decl_referenced (decl); | |
887 | if (vnode->finalized) | |
8a4a83ed | 888 | varpool_mark_needed_node (vnode); |
386b46cf JH |
889 | } |
890 | if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl))) | |
891 | { | |
343d4b27 | 892 | if (! TREE_PUBLIC (vnode->decl)) |
c5d75364 MLI |
893 | warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes, |
894 | "%<externally_visible%>" | |
895 | " attribute have effect only on public objects"); | |
b20996ff JH |
896 | else if (vnode->finalized) |
897 | varpool_mark_needed_node (vnode); | |
386b46cf JH |
898 | } |
899 | } | |
900 | } | |
901 | ||
151e6f24 JH |
902 | /* Process CGRAPH_NODES_NEEDED queue, analyze each function (and transitively |
903 | each reachable functions) and build cgraph. | |
904 | The function can be called multiple times after inserting new nodes | |
88512ba0 | 905 | into beginning of queue. Just the new part of queue is re-scanned then. */ |
1c4a429a | 906 | |
151e6f24 JH |
907 | static void |
908 | cgraph_analyze_functions (void) | |
1c4a429a | 909 | { |
cd9c7bd2 | 910 | /* Keep track of already processed nodes when called multiple times for |
aabcd309 | 911 | intermodule optimization. */ |
cd9c7bd2 | 912 | static struct cgraph_node *first_analyzed; |
61e00a5e | 913 | struct cgraph_node *first_processed = first_analyzed; |
8a4a83ed | 914 | static struct varpool_node *first_analyzed_var; |
151e6f24 | 915 | struct cgraph_node *node, *next; |
1c4a429a | 916 | |
61e00a5e JH |
917 | process_function_and_variable_attributes (first_processed, |
918 | first_analyzed_var); | |
919 | first_processed = cgraph_nodes; | |
8a4a83ed JH |
920 | first_analyzed_var = varpool_nodes; |
921 | varpool_analyze_pending_decls (); | |
a194aa56 | 922 | if (cgraph_dump_file) |
1c4a429a | 923 | { |
7d82fe7c | 924 | fprintf (cgraph_dump_file, "Initial entry points:"); |
cd9c7bd2 | 925 | for (node = cgraph_nodes; node != first_analyzed; node = node->next) |
39ecc018 | 926 | if (node->needed) |
a194aa56 JH |
927 | fprintf (cgraph_dump_file, " %s", cgraph_node_name (node)); |
928 | fprintf (cgraph_dump_file, "\n"); | |
1c4a429a | 929 | } |
151e6f24 | 930 | cgraph_process_new_functions (); |
1c4a429a | 931 | |
7660e67e SB |
932 | /* Propagate reachability flag and lower representation of all reachable |
933 | functions. In the future, lowering will introduce new functions and | |
934 | new entry points on the way (by template instantiation and virtual | |
935 | method table generation for instance). */ | |
1668aabc | 936 | while (cgraph_nodes_queue) |
1c4a429a | 937 | { |
e767b5be | 938 | struct cgraph_edge *edge; |
1668aabc JH |
939 | tree decl = cgraph_nodes_queue->decl; |
940 | ||
941 | node = cgraph_nodes_queue; | |
8bd87c4e | 942 | cgraph_nodes_queue = cgraph_nodes_queue->next_needed; |
18c6ada9 | 943 | node->next_needed = NULL; |
1c4a429a | 944 | |
cd4dea62 | 945 | /* ??? It is possible to create extern inline function and later using |
9d203871 | 946 | weak alias attribute to kill its body. See |
cd4dea62 | 947 | gcc.c-torture/compile/20011119-1.c */ |
726a989a | 948 | if (!DECL_STRUCT_FUNCTION (decl)) |
d71cc23f JH |
949 | { |
950 | cgraph_reset_node (node); | |
951 | continue; | |
952 | } | |
cd4dea62 | 953 | |
d7f09764 DN |
954 | if (!node->analyzed) |
955 | cgraph_analyze_function (node); | |
8dafba3c | 956 | |
1c4a429a | 957 | for (edge = node->callees; edge; edge = edge->next_callee) |
e767b5be | 958 | if (!edge->callee->reachable) |
8dafba3c RH |
959 | cgraph_mark_reachable_node (edge->callee); |
960 | ||
6b20f353 DS |
961 | /* If decl is a clone of an abstract function, mark that abstract |
962 | function so that we don't release its body. The DECL_INITIAL() of that | |
963 | abstract function declaration will be later needed to output debug info. */ | |
964 | if (DECL_ABSTRACT_ORIGIN (decl)) | |
965 | { | |
966 | struct cgraph_node *origin_node = cgraph_node (DECL_ABSTRACT_ORIGIN (decl)); | |
967 | origin_node->abstract_and_needed = true; | |
968 | } | |
969 | ||
61e00a5e JH |
970 | /* We finalize local static variables during constructing callgraph |
971 | edges. Process their attributes too. */ | |
972 | process_function_and_variable_attributes (first_processed, | |
973 | first_analyzed_var); | |
974 | first_processed = cgraph_nodes; | |
8a4a83ed JH |
975 | first_analyzed_var = varpool_nodes; |
976 | varpool_analyze_pending_decls (); | |
151e6f24 | 977 | cgraph_process_new_functions (); |
1c4a429a | 978 | } |
8dafba3c | 979 | |
564738df | 980 | /* Collect entry points to the unit. */ |
a194aa56 | 981 | if (cgraph_dump_file) |
1668aabc | 982 | { |
7d82fe7c | 983 | fprintf (cgraph_dump_file, "Unit entry points:"); |
cd9c7bd2 | 984 | for (node = cgraph_nodes; node != first_analyzed; node = node->next) |
39ecc018 | 985 | if (node->needed) |
a194aa56 | 986 | fprintf (cgraph_dump_file, " %s", cgraph_node_name (node)); |
7d82fe7c | 987 | fprintf (cgraph_dump_file, "\n\nInitial "); |
e767b5be | 988 | dump_cgraph (cgraph_dump_file); |
1668aabc | 989 | } |
7660e67e | 990 | |
a194aa56 JH |
991 | if (cgraph_dump_file) |
992 | fprintf (cgraph_dump_file, "\nReclaiming functions:"); | |
1c4a429a | 993 | |
96fc428c | 994 | for (node = cgraph_nodes; node != first_analyzed; node = next) |
1c4a429a JH |
995 | { |
996 | tree decl = node->decl; | |
96fc428c | 997 | next = node->next; |
1c4a429a | 998 | |
39ecc018 | 999 | if (node->local.finalized && !gimple_has_body_p (decl)) |
c22cacf3 | 1000 | cgraph_reset_node (node); |
d71cc23f | 1001 | |
39ecc018 | 1002 | if (!node->reachable && gimple_has_body_p (decl)) |
1c4a429a | 1003 | { |
a194aa56 JH |
1004 | if (cgraph_dump_file) |
1005 | fprintf (cgraph_dump_file, " %s", cgraph_node_name (node)); | |
18c6ada9 | 1006 | cgraph_remove_node (node); |
d71cc23f | 1007 | continue; |
1c4a429a | 1008 | } |
9b0436b7 JH |
1009 | else |
1010 | node->next_needed = NULL; | |
39ecc018 | 1011 | gcc_assert (!node->local.finalized || gimple_has_body_p (decl)); |
d71cc23f | 1012 | gcc_assert (node->analyzed == node->local.finalized); |
1c4a429a | 1013 | } |
a194aa56 | 1014 | if (cgraph_dump_file) |
7d82fe7c KC |
1015 | { |
1016 | fprintf (cgraph_dump_file, "\n\nReclaimed "); | |
1017 | dump_cgraph (cgraph_dump_file); | |
1018 | } | |
cd9c7bd2 | 1019 | first_analyzed = cgraph_nodes; |
1c4a429a | 1020 | ggc_collect (); |
151e6f24 JH |
1021 | } |
1022 | ||
5f1a9ebb RG |
1023 | |
1024 | /* Emit thunks for every node in the cgraph. | |
1025 | FIXME: We really ought to emit thunks only for functions that are needed. */ | |
1026 | ||
1027 | static void | |
1028 | cgraph_emit_thunks (void) | |
1029 | { | |
1030 | struct cgraph_node *n; | |
1031 | ||
1032 | for (n = cgraph_nodes; n; n = n->next) | |
1033 | { | |
1034 | /* Only emit thunks on functions defined in this TU. | |
1035 | Note that this may emit more thunks than strictly necessary. | |
1036 | During optimization some nodes may disappear. It would be | |
1037 | nice to only emit thunks only for the functions that will be | |
1038 | emitted, but we cannot know that until the inliner and other | |
1039 | IPA passes have run (see the sequencing of the call to | |
1040 | cgraph_mark_functions_to_output in cgraph_optimize). */ | |
90097c67 RG |
1041 | if (n->reachable |
1042 | && !DECL_EXTERNAL (n->decl)) | |
5f1a9ebb RG |
1043 | lang_hooks.callgraph.emit_associated_thunks (n->decl); |
1044 | } | |
1045 | } | |
1046 | ||
1047 | ||
151e6f24 JH |
1048 | /* Analyze the whole compilation unit once it is parsed completely. */ |
1049 | ||
1050 | void | |
1051 | cgraph_finalize_compilation_unit (void) | |
1052 | { | |
90097c67 RG |
1053 | timevar_push (TV_CGRAPH); |
1054 | ||
a406865a RG |
1055 | /* Do not skip analyzing the functions if there were errors, we |
1056 | miss diagnostics for following functions otherwise. */ | |
151e6f24 | 1057 | |
5f1a9ebb | 1058 | /* Emit size functions we didn't inline. */ |
f82a627c | 1059 | finalize_size_functions (); |
5f1a9ebb | 1060 | |
5f1a9ebb RG |
1061 | /* Call functions declared with the "constructor" or "destructor" |
1062 | attribute. */ | |
1063 | cgraph_build_cdtor_fns (); | |
151e6f24 | 1064 | |
90097c67 RG |
1065 | /* Mark alias targets necessary and emit diagnostics. */ |
1066 | finish_aliases_1 (); | |
1067 | ||
151e6f24 JH |
1068 | if (!quiet_flag) |
1069 | { | |
1070 | fprintf (stderr, "\nAnalyzing compilation unit\n"); | |
1071 | fflush (stderr); | |
1072 | } | |
1073 | ||
90097c67 RG |
1074 | /* Gimplify and lower all functions, compute reachability and |
1075 | remove unreachable nodes. */ | |
1076 | cgraph_analyze_functions (); | |
1077 | ||
1078 | /* Emit thunks for reachable nodes, if needed. */ | |
1079 | if (lang_hooks.callgraph.emit_associated_thunks) | |
1080 | cgraph_emit_thunks (); | |
1081 | ||
5f1a9ebb RG |
1082 | /* Mark alias targets necessary and emit diagnostics. */ |
1083 | finish_aliases_1 (); | |
1084 | ||
90097c67 | 1085 | /* Gimplify and lower thunks. */ |
151e6f24 | 1086 | cgraph_analyze_functions (); |
a406865a | 1087 | |
90097c67 | 1088 | /* Finally drive the pass manager. */ |
a406865a | 1089 | cgraph_optimize (); |
90097c67 RG |
1090 | |
1091 | timevar_pop (TV_CGRAPH); | |
1c4a429a | 1092 | } |
3baf459d DN |
1093 | |
1094 | ||
1c4a429a JH |
1095 | /* Figure out what functions we want to assemble. */ |
1096 | ||
1097 | static void | |
db0e878d | 1098 | cgraph_mark_functions_to_output (void) |
1c4a429a JH |
1099 | { |
1100 | struct cgraph_node *node; | |
1101 | ||
1c4a429a JH |
1102 | for (node = cgraph_nodes; node; node = node->next) |
1103 | { | |
1104 | tree decl = node->decl; | |
b58b1157 | 1105 | struct cgraph_edge *e; |
c22cacf3 | 1106 | |
257eb6e3 | 1107 | gcc_assert (!node->process); |
b58b1157 JH |
1108 | |
1109 | for (e = node->callers; e; e = e->next_caller) | |
dc0bfe6a | 1110 | if (e->inline_failed) |
b58b1157 | 1111 | break; |
1c4a429a | 1112 | |
7660e67e SB |
1113 | /* We need to output all local functions that are used and not |
1114 | always inlined, as well as those that are reachable from | |
1115 | outside the current compilation unit. */ | |
39ecc018 | 1116 | if (node->analyzed |
18c6ada9 | 1117 | && !node->global.inlined_to |
1c4a429a | 1118 | && (node->needed |
b58b1157 | 1119 | || (e && node->reachable)) |
6de9cd9a | 1120 | && !TREE_ASM_WRITTEN (decl) |
1c4a429a | 1121 | && !DECL_EXTERNAL (decl)) |
257eb6e3 | 1122 | node->process = 1; |
341c100f | 1123 | else |
1a2caa7a NS |
1124 | { |
1125 | /* We should've reclaimed all functions that are not needed. */ | |
1126 | #ifdef ENABLE_CHECKING | |
726a989a | 1127 | if (!node->global.inlined_to |
39ecc018 | 1128 | && gimple_has_body_p (decl) |
1a2caa7a NS |
1129 | && !DECL_EXTERNAL (decl)) |
1130 | { | |
1131 | dump_cgraph_node (stderr, node); | |
1132 | internal_error ("failed to reclaim unneeded function"); | |
1133 | } | |
1134 | #endif | |
726a989a | 1135 | gcc_assert (node->global.inlined_to |
39ecc018 | 1136 | || !gimple_has_body_p (decl) |
1a2caa7a NS |
1137 | || DECL_EXTERNAL (decl)); |
1138 | ||
1139 | } | |
c22cacf3 | 1140 | |
18d13f34 JH |
1141 | } |
1142 | } | |
1143 | ||
1c4a429a | 1144 | /* Expand function specified by NODE. */ |
7660e67e | 1145 | |
1c4a429a | 1146 | static void |
db0e878d | 1147 | cgraph_expand_function (struct cgraph_node *node) |
1c4a429a JH |
1148 | { |
1149 | tree decl = node->decl; | |
1150 | ||
18c6ada9 | 1151 | /* We ought to not compile any inline clones. */ |
341c100f | 1152 | gcc_assert (!node->global.inlined_to); |
18c6ada9 | 1153 | |
7e8b322a | 1154 | announce_function (decl); |
257eb6e3 | 1155 | node->process = 0; |
18d13f34 | 1156 | |
2dee695b | 1157 | gcc_assert (node->lowered); |
776b966e | 1158 | |
a3546141 | 1159 | /* Generate RTL for the body of DECL. */ |
e89d6010 | 1160 | tree_rest_of_compilation (decl); |
18d13f34 | 1161 | |
6de9cd9a | 1162 | /* Make sure that BE didn't give up on compiling. */ |
f30cfcb1 | 1163 | gcc_assert (TREE_ASM_WRITTEN (decl)); |
1c4a429a | 1164 | current_function_decl = NULL; |
39ecc018 JH |
1165 | gcc_assert (!cgraph_preserve_function_body_p (decl)); |
1166 | cgraph_release_function_body (node); | |
1167 | /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer | |
1168 | points to the dead function body. */ | |
1169 | cgraph_node_remove_callees (node); | |
6b02a499 JH |
1170 | |
1171 | cgraph_function_flags_ready = true; | |
1c4a429a JH |
1172 | } |
1173 | ||
18c6ada9 | 1174 | /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */ |
b58b1157 JH |
1175 | |
1176 | bool | |
61a05df1 | 1177 | cgraph_inline_p (struct cgraph_edge *e, cgraph_inline_failed_t *reason) |
b58b1157 | 1178 | { |
18c6ada9 JH |
1179 | *reason = e->inline_failed; |
1180 | return !e->inline_failed; | |
b58b1157 | 1181 | } |
18c6ada9 | 1182 | |
6674a6ce | 1183 | |
6674a6ce | 1184 | |
db0e878d AJ |
1185 | /* Expand all functions that must be output. |
1186 | ||
b58b1157 JH |
1187 | Attempt to topologically sort the nodes so function is output when |
1188 | all called functions are already assembled to allow data to be | |
a98ebe2e | 1189 | propagated across the callgraph. Use a stack to get smaller distance |
d1a6adeb | 1190 | between a function and its callees (later we may choose to use a more |
b58b1157 JH |
1191 | sophisticated algorithm for function reordering; we will likely want |
1192 | to use subsections to make the output functions appear in top-down | |
1193 | order). */ | |
1194 | ||
1195 | static void | |
a20af5b8 | 1196 | cgraph_expand_all_functions (void) |
b58b1157 JH |
1197 | { |
1198 | struct cgraph_node *node; | |
5ed6ace5 | 1199 | struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes); |
f30cfcb1 | 1200 | int order_pos, new_order_pos = 0; |
b58b1157 JH |
1201 | int i; |
1202 | ||
b58b1157 | 1203 | order_pos = cgraph_postorder (order); |
341c100f | 1204 | gcc_assert (order_pos == cgraph_n_nodes); |
b58b1157 | 1205 | |
1ae58c30 | 1206 | /* Garbage collector may remove inline clones we eliminate during |
18c6ada9 JH |
1207 | optimization. So we must be sure to not reference them. */ |
1208 | for (i = 0; i < order_pos; i++) | |
257eb6e3 | 1209 | if (order[i]->process) |
18c6ada9 JH |
1210 | order[new_order_pos++] = order[i]; |
1211 | ||
1212 | for (i = new_order_pos - 1; i >= 0; i--) | |
b58b1157 JH |
1213 | { |
1214 | node = order[i]; | |
257eb6e3 | 1215 | if (node->process) |
b58b1157 | 1216 | { |
341c100f | 1217 | gcc_assert (node->reachable); |
257eb6e3 | 1218 | node->process = 0; |
b58b1157 JH |
1219 | cgraph_expand_function (node); |
1220 | } | |
1221 | } | |
f45e0ad1 | 1222 | cgraph_process_new_functions (); |
50674e96 | 1223 | |
b58b1157 | 1224 | free (order); |
50674e96 | 1225 | |
b58b1157 JH |
1226 | } |
1227 | ||
474eccc6 ILT |
1228 | /* This is used to sort the node types by the cgraph order number. */ |
1229 | ||
24b97832 ILT |
1230 | enum cgraph_order_sort_kind |
1231 | { | |
1232 | ORDER_UNDEFINED = 0, | |
1233 | ORDER_FUNCTION, | |
1234 | ORDER_VAR, | |
1235 | ORDER_ASM | |
1236 | }; | |
1237 | ||
474eccc6 ILT |
1238 | struct cgraph_order_sort |
1239 | { | |
24b97832 | 1240 | enum cgraph_order_sort_kind kind; |
474eccc6 ILT |
1241 | union |
1242 | { | |
1243 | struct cgraph_node *f; | |
8a4a83ed | 1244 | struct varpool_node *v; |
474eccc6 ILT |
1245 | struct cgraph_asm_node *a; |
1246 | } u; | |
1247 | }; | |
1248 | ||
1249 | /* Output all functions, variables, and asm statements in the order | |
1250 | according to their order fields, which is the order in which they | |
1251 | appeared in the file. This implements -fno-toplevel-reorder. In | |
1252 | this mode we may output functions and variables which don't really | |
1253 | need to be output. */ | |
1254 | ||
1255 | static void | |
1256 | cgraph_output_in_order (void) | |
1257 | { | |
1258 | int max; | |
1259 | size_t size; | |
1260 | struct cgraph_order_sort *nodes; | |
1261 | int i; | |
1262 | struct cgraph_node *pf; | |
8a4a83ed | 1263 | struct varpool_node *pv; |
474eccc6 ILT |
1264 | struct cgraph_asm_node *pa; |
1265 | ||
1266 | max = cgraph_order; | |
1267 | size = max * sizeof (struct cgraph_order_sort); | |
1268 | nodes = (struct cgraph_order_sort *) alloca (size); | |
1269 | memset (nodes, 0, size); | |
1270 | ||
8a4a83ed | 1271 | varpool_analyze_pending_decls (); |
474eccc6 ILT |
1272 | |
1273 | for (pf = cgraph_nodes; pf; pf = pf->next) | |
1274 | { | |
257eb6e3 | 1275 | if (pf->process) |
474eccc6 ILT |
1276 | { |
1277 | i = pf->order; | |
1278 | gcc_assert (nodes[i].kind == ORDER_UNDEFINED); | |
1279 | nodes[i].kind = ORDER_FUNCTION; | |
1280 | nodes[i].u.f = pf; | |
1281 | } | |
1282 | } | |
1283 | ||
8a4a83ed | 1284 | for (pv = varpool_nodes_queue; pv; pv = pv->next_needed) |
474eccc6 ILT |
1285 | { |
1286 | i = pv->order; | |
1287 | gcc_assert (nodes[i].kind == ORDER_UNDEFINED); | |
1288 | nodes[i].kind = ORDER_VAR; | |
1289 | nodes[i].u.v = pv; | |
1290 | } | |
1291 | ||
1292 | for (pa = cgraph_asm_nodes; pa; pa = pa->next) | |
1293 | { | |
1294 | i = pa->order; | |
1295 | gcc_assert (nodes[i].kind == ORDER_UNDEFINED); | |
1296 | nodes[i].kind = ORDER_ASM; | |
1297 | nodes[i].u.a = pa; | |
1298 | } | |
474eccc6 | 1299 | |
7386e3ee JH |
1300 | /* In toplevel reorder mode we output all statics; mark them as needed. */ |
1301 | for (i = 0; i < max; ++i) | |
1302 | { | |
1303 | if (nodes[i].kind == ORDER_VAR) | |
1304 | { | |
1305 | varpool_mark_needed_node (nodes[i].u.v); | |
1306 | } | |
1307 | } | |
1308 | varpool_empty_needed_queue (); | |
1309 | ||
474eccc6 ILT |
1310 | for (i = 0; i < max; ++i) |
1311 | { | |
1312 | switch (nodes[i].kind) | |
1313 | { | |
1314 | case ORDER_FUNCTION: | |
257eb6e3 | 1315 | nodes[i].u.f->process = 0; |
474eccc6 ILT |
1316 | cgraph_expand_function (nodes[i].u.f); |
1317 | break; | |
1318 | ||
1319 | case ORDER_VAR: | |
8a4a83ed | 1320 | varpool_assemble_decl (nodes[i].u.v); |
474eccc6 ILT |
1321 | break; |
1322 | ||
1323 | case ORDER_ASM: | |
1324 | assemble_asm (nodes[i].u.a->asm_str); | |
1325 | break; | |
1326 | ||
1327 | case ORDER_UNDEFINED: | |
1328 | break; | |
1329 | ||
1330 | default: | |
1331 | gcc_unreachable (); | |
1332 | } | |
1333 | } | |
e7b9eb2c ILT |
1334 | |
1335 | cgraph_asm_nodes = NULL; | |
474eccc6 ILT |
1336 | } |
1337 | ||
18c6ada9 JH |
1338 | /* Return true when function body of DECL still needs to be kept around |
1339 | for later re-use. */ | |
1340 | bool | |
1341 | cgraph_preserve_function_body_p (tree decl) | |
1342 | { | |
1343 | struct cgraph_node *node; | |
c37f4ba4 JH |
1344 | |
1345 | gcc_assert (cgraph_global_info_ready); | |
18c6ada9 | 1346 | /* Look if there is any clone around. */ |
9187e02d JH |
1347 | node = cgraph_node (decl); |
1348 | if (node->clones) | |
1349 | return true; | |
18c6ada9 JH |
1350 | return false; |
1351 | } | |
1352 | ||
ef330312 PB |
1353 | static void |
1354 | ipa_passes (void) | |
1355 | { | |
db2960f4 | 1356 | set_cfun (NULL); |
04b201a2 | 1357 | current_function_decl = NULL; |
726a989a | 1358 | gimple_register_cfg_hooks (); |
ef330312 | 1359 | bitmap_obstack_initialize (NULL); |
b20996ff JH |
1360 | |
1361 | if (!in_lto_p) | |
1362 | execute_ipa_pass_list (all_small_ipa_passes); | |
3baf459d | 1363 | |
d7f09764 DN |
1364 | /* If pass_all_early_optimizations was not scheduled, the state of |
1365 | the cgraph will not be properly updated. Update it now. */ | |
1366 | if (cgraph_state < CGRAPH_STATE_IPA_SSA) | |
1367 | cgraph_state = CGRAPH_STATE_IPA_SSA; | |
3baf459d | 1368 | |
d7f09764 DN |
1369 | if (!in_lto_p) |
1370 | { | |
1371 | /* Generate coverage variables and constructors. */ | |
1372 | coverage_finish (); | |
1373 | ||
1374 | /* Process new functions added. */ | |
1375 | set_cfun (NULL); | |
1376 | current_function_decl = NULL; | |
1377 | cgraph_process_new_functions (); | |
1378 | } | |
1379 | ||
1380 | execute_ipa_summary_passes ((struct ipa_opt_pass_d *) all_regular_ipa_passes); | |
1381 | execute_ipa_summary_passes ((struct ipa_opt_pass_d *) all_lto_gen_passes); | |
1382 | ||
1383 | if (!in_lto_p) | |
1384 | ipa_write_summaries (); | |
1385 | ||
1386 | execute_ipa_pass_list (all_regular_ipa_passes); | |
3baf459d | 1387 | |
ef330312 PB |
1388 | bitmap_obstack_release (NULL); |
1389 | } | |
1390 | ||
4537ec0c | 1391 | |
1c4a429a JH |
1392 | /* Perform simple optimizations based on callgraph. */ |
1393 | ||
d7f09764 | 1394 | void |
db0e878d | 1395 | cgraph_optimize (void) |
1c4a429a | 1396 | { |
413803d3 VR |
1397 | if (errorcount || sorrycount) |
1398 | return; | |
1399 | ||
18c6ada9 JH |
1400 | #ifdef ENABLE_CHECKING |
1401 | verify_cgraph (); | |
1402 | #endif | |
7be82279 | 1403 | |
cd9c7bd2 JH |
1404 | /* Frontend may output common variables after the unit has been finalized. |
1405 | It is safe to deal with them here as they are always zero initialized. */ | |
8a4a83ed | 1406 | varpool_analyze_pending_decls (); |
857e7259 | 1407 | |
a194aa56 | 1408 | timevar_push (TV_CGRAPHOPT); |
a5573239 JH |
1409 | if (pre_ipa_mem_report) |
1410 | { | |
1411 | fprintf (stderr, "Memory consumption before IPA\n"); | |
1412 | dump_memory_report (false); | |
1413 | } | |
b58b1157 | 1414 | if (!quiet_flag) |
a418679d | 1415 | fprintf (stderr, "Performing interprocedural optimizations\n"); |
f45e0ad1 | 1416 | cgraph_state = CGRAPH_STATE_IPA; |
f30cfcb1 | 1417 | |
7e2fe9d8 AP |
1418 | /* Don't run the IPA passes if there was any error or sorry messages. */ |
1419 | if (errorcount == 0 && sorrycount == 0) | |
1420 | ipa_passes (); | |
1421 | ||
4537ec0c DN |
1422 | /* Do nothing else if any IPA pass found errors. */ |
1423 | if (errorcount || sorrycount) | |
1424 | return; | |
1425 | ||
6b02a499 JH |
1426 | /* This pass remove bodies of extern inline functions we never inlined. |
1427 | Do this later so other IPA passes see what is really going on. */ | |
1428 | cgraph_remove_unreachable_nodes (false, dump_file); | |
dafc5b82 | 1429 | cgraph_global_info_ready = true; |
a194aa56 JH |
1430 | if (cgraph_dump_file) |
1431 | { | |
7d82fe7c | 1432 | fprintf (cgraph_dump_file, "Optimized "); |
a194aa56 | 1433 | dump_cgraph (cgraph_dump_file); |
cd9c7bd2 | 1434 | dump_varpool (cgraph_dump_file); |
a194aa56 | 1435 | } |
a5573239 JH |
1436 | if (post_ipa_mem_report) |
1437 | { | |
7fa982e5 | 1438 | fprintf (stderr, "Memory consumption after IPA\n"); |
a5573239 JH |
1439 | dump_memory_report (false); |
1440 | } | |
a194aa56 | 1441 | timevar_pop (TV_CGRAPHOPT); |
1c4a429a | 1442 | |
b58b1157 | 1443 | /* Output everything. */ |
3df9609a | 1444 | (*debug_hooks->assembly_start) (); |
7d82fe7c KC |
1445 | if (!quiet_flag) |
1446 | fprintf (stderr, "Assembling functions:\n"); | |
18c6ada9 JH |
1447 | #ifdef ENABLE_CHECKING |
1448 | verify_cgraph (); | |
1449 | #endif | |
474eccc6 | 1450 | |
9187e02d | 1451 | cgraph_materialize_all_clones (); |
6674a6ce | 1452 | cgraph_mark_functions_to_output (); |
cd9c7bd2 | 1453 | |
f45e0ad1 | 1454 | cgraph_state = CGRAPH_STATE_EXPANSION; |
474eccc6 ILT |
1455 | if (!flag_toplevel_reorder) |
1456 | cgraph_output_in_order (); | |
1457 | else | |
1458 | { | |
1459 | cgraph_output_pending_asms (); | |
1460 | ||
1461 | cgraph_expand_all_functions (); | |
8a4a83ed | 1462 | varpool_remove_unreferenced_decls (); |
474eccc6 | 1463 | |
8a4a83ed | 1464 | varpool_assemble_pending_decls (); |
474eccc6 | 1465 | } |
f45e0ad1 JH |
1466 | cgraph_process_new_functions (); |
1467 | cgraph_state = CGRAPH_STATE_FINISHED; | |
cd9c7bd2 | 1468 | |
a194aa56 JH |
1469 | if (cgraph_dump_file) |
1470 | { | |
7d82fe7c | 1471 | fprintf (cgraph_dump_file, "\nFinal "); |
a194aa56 JH |
1472 | dump_cgraph (cgraph_dump_file); |
1473 | } | |
18c6ada9 JH |
1474 | #ifdef ENABLE_CHECKING |
1475 | verify_cgraph (); | |
6de9cd9a DN |
1476 | /* Double check that all inline clones are gone and that all |
1477 | function bodies have been released from memory. */ | |
7e8b322a | 1478 | if (!(sorrycount || errorcount)) |
6de9cd9a DN |
1479 | { |
1480 | struct cgraph_node *node; | |
1481 | bool error_found = false; | |
1482 | ||
1483 | for (node = cgraph_nodes; node; node = node->next) | |
1484 | if (node->analyzed | |
1485 | && (node->global.inlined_to | |
39ecc018 | 1486 | || gimple_has_body_p (node->decl))) |
6de9cd9a DN |
1487 | { |
1488 | error_found = true; | |
1489 | dump_cgraph_node (stderr, node); | |
c22cacf3 | 1490 | } |
6de9cd9a | 1491 | if (error_found) |
f30cfcb1 | 1492 | internal_error ("nodes with unreleased memory found"); |
6de9cd9a | 1493 | } |
18c6ada9 | 1494 | #endif |
1c4a429a | 1495 | } |
4537ec0c DN |
1496 | |
1497 | ||
873c7164 MM |
1498 | /* Generate and emit a static constructor or destructor. WHICH must |
1499 | be one of 'I' (for a constructor) or 'D' (for a destructor). BODY | |
1500 | is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the | |
fa10beec | 1501 | initialization priority for this constructor or destructor. */ |
c9b9aa64 RH |
1502 | |
1503 | void | |
35b6fdcf | 1504 | cgraph_build_static_cdtor (char which, tree body, int priority) |
c9b9aa64 RH |
1505 | { |
1506 | static int counter = 0; | |
1507 | char which_buf[16]; | |
b785f485 | 1508 | tree decl, name, resdecl; |
c9b9aa64 | 1509 | |
873c7164 MM |
1510 | /* The priority is encoded in the constructor or destructor name. |
1511 | collect2 will sort the names and arrange that they are called at | |
1512 | program startup. */ | |
1513 | sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++); | |
5880f14f | 1514 | name = get_file_function_name (which_buf); |
c9b9aa64 | 1515 | |
c2255bc4 | 1516 | decl = build_decl (input_location, FUNCTION_DECL, name, |
c9b9aa64 RH |
1517 | build_function_type (void_type_node, void_list_node)); |
1518 | current_function_decl = decl; | |
1519 | ||
c2255bc4 AH |
1520 | resdecl = build_decl (input_location, |
1521 | RESULT_DECL, NULL_TREE, void_type_node); | |
b785f485 | 1522 | DECL_ARTIFICIAL (resdecl) = 1; |
b785f485 | 1523 | DECL_RESULT (decl) = resdecl; |
07485407 | 1524 | DECL_CONTEXT (resdecl) = decl; |
b785f485 | 1525 | |
182e0d71 | 1526 | allocate_struct_function (decl, false); |
c9b9aa64 RH |
1527 | |
1528 | TREE_STATIC (decl) = 1; | |
1529 | TREE_USED (decl) = 1; | |
1530 | DECL_ARTIFICIAL (decl) = 1; | |
c9b9aa64 RH |
1531 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1; |
1532 | DECL_SAVED_TREE (decl) = body; | |
1533 | TREE_PUBLIC (decl) = ! targetm.have_ctors_dtors; | |
1534 | DECL_UNINLINABLE (decl) = 1; | |
1535 | ||
1536 | DECL_INITIAL (decl) = make_node (BLOCK); | |
1537 | TREE_USED (DECL_INITIAL (decl)) = 1; | |
1538 | ||
1539 | DECL_SOURCE_LOCATION (decl) = input_location; | |
1540 | cfun->function_end_locus = input_location; | |
1541 | ||
341c100f NS |
1542 | switch (which) |
1543 | { | |
1544 | case 'I': | |
1545 | DECL_STATIC_CONSTRUCTOR (decl) = 1; | |
395a40e0 | 1546 | decl_init_priority_insert (decl, priority); |
341c100f NS |
1547 | break; |
1548 | case 'D': | |
1549 | DECL_STATIC_DESTRUCTOR (decl) = 1; | |
395a40e0 | 1550 | decl_fini_priority_insert (decl, priority); |
341c100f NS |
1551 | break; |
1552 | default: | |
1553 | gcc_unreachable (); | |
1554 | } | |
c9b9aa64 RH |
1555 | |
1556 | gimplify_function_tree (decl); | |
1557 | ||
f45e0ad1 JH |
1558 | cgraph_add_new_function (decl, false); |
1559 | cgraph_mark_needed_node (cgraph_node (decl)); | |
cac67c08 | 1560 | set_cfun (NULL); |
c9b9aa64 | 1561 | } |
9b3e897d PB |
1562 | |
1563 | void | |
1564 | init_cgraph (void) | |
1565 | { | |
1566 | cgraph_dump_file = dump_begin (TDI_cgraph, NULL); | |
1567 | } | |
57fb5341 | 1568 | |
c22cacf3 | 1569 | /* The edges representing the callers of the NEW_VERSION node were |
57fb5341 RL |
1570 | fixed by cgraph_function_versioning (), now the call_expr in their |
1571 | respective tree code should be updated to call the NEW_VERSION. */ | |
1572 | ||
1573 | static void | |
1574 | update_call_expr (struct cgraph_node *new_version) | |
1575 | { | |
1576 | struct cgraph_edge *e; | |
1577 | ||
1578 | gcc_assert (new_version); | |
726a989a RB |
1579 | |
1580 | /* Update the call expr on the edges to call the new version. */ | |
57fb5341 | 1581 | for (e = new_version->callers; e; e = e->next_caller) |
c0ab1df3 AP |
1582 | { |
1583 | struct function *inner_function = DECL_STRUCT_FUNCTION (e->caller->decl); | |
1584 | gimple_call_set_fndecl (e->call_stmt, new_version->decl); | |
1d65f45c | 1585 | maybe_clean_eh_stmt_fn (inner_function, e->call_stmt); |
c0ab1df3 | 1586 | } |
57fb5341 RL |
1587 | } |
1588 | ||
1589 | ||
1590 | /* Create a new cgraph node which is the new version of | |
1591 | OLD_VERSION node. REDIRECT_CALLERS holds the callers | |
1592 | edges which should be redirected to point to | |
1593 | NEW_VERSION. ALL the callees edges of OLD_VERSION | |
1594 | are cloned to the new version node. Return the new | |
1595 | version node. */ | |
1596 | ||
1597 | static struct cgraph_node * | |
1598 | cgraph_copy_node_for_versioning (struct cgraph_node *old_version, | |
b2c0ad40 KH |
1599 | tree new_decl, |
1600 | VEC(cgraph_edge_p,heap) *redirect_callers) | |
57fb5341 RL |
1601 | { |
1602 | struct cgraph_node *new_version; | |
1603 | struct cgraph_edge *e, *new_e; | |
1604 | struct cgraph_edge *next_callee; | |
1605 | unsigned i; | |
1606 | ||
1607 | gcc_assert (old_version); | |
c22cacf3 | 1608 | |
57fb5341 RL |
1609 | new_version = cgraph_node (new_decl); |
1610 | ||
1611 | new_version->analyzed = true; | |
1612 | new_version->local = old_version->local; | |
1613 | new_version->global = old_version->global; | |
1614 | new_version->rtl = new_version->rtl; | |
1615 | new_version->reachable = true; | |
1616 | new_version->count = old_version->count; | |
1617 | ||
1618 | /* Clone the old node callees. Recursive calls are | |
1619 | also cloned. */ | |
1620 | for (e = old_version->callees;e; e=e->next_callee) | |
1621 | { | |
d7f09764 DN |
1622 | new_e = cgraph_clone_edge (e, new_version, e->call_stmt, |
1623 | e->lto_stmt_uid, 0, e->frequency, | |
45a80bb9 | 1624 | e->loop_nest, true); |
57fb5341 RL |
1625 | new_e->count = e->count; |
1626 | } | |
1627 | /* Fix recursive calls. | |
1628 | If OLD_VERSION has a recursive call after the | |
1629 | previous edge cloning, the new version will have an edge | |
1630 | pointing to the old version, which is wrong; | |
1631 | Redirect it to point to the new version. */ | |
1632 | for (e = new_version->callees ; e; e = next_callee) | |
1633 | { | |
1634 | next_callee = e->next_callee; | |
1635 | if (e->callee == old_version) | |
1636 | cgraph_redirect_edge_callee (e, new_version); | |
c22cacf3 | 1637 | |
57fb5341 RL |
1638 | if (!next_callee) |
1639 | break; | |
1640 | } | |
b2c0ad40 KH |
1641 | for (i = 0; VEC_iterate (cgraph_edge_p, redirect_callers, i, e); i++) |
1642 | { | |
1643 | /* Redirect calls to the old version node to point to its new | |
1644 | version. */ | |
1645 | cgraph_redirect_edge_callee (e, new_version); | |
1646 | } | |
57fb5341 RL |
1647 | |
1648 | return new_version; | |
1649 | } | |
1650 | ||
1651 | /* Perform function versioning. | |
c22cacf3 | 1652 | Function versioning includes copying of the tree and |
57fb5341 RL |
1653 | a callgraph update (creating a new cgraph node and updating |
1654 | its callees and callers). | |
1655 | ||
1656 | REDIRECT_CALLERS varray includes the edges to be redirected | |
1657 | to the new version. | |
1658 | ||
1659 | TREE_MAP is a mapping of tree nodes we want to replace with | |
1660 | new ones (according to results of prior analysis). | |
1661 | OLD_VERSION_NODE is the node that is versioned. | |
c6f7cfc1 JH |
1662 | It returns the new version's cgraph node. |
1663 | ARGS_TO_SKIP lists arguments to be omitted from functions | |
1664 | */ | |
57fb5341 RL |
1665 | |
1666 | struct cgraph_node * | |
1667 | cgraph_function_versioning (struct cgraph_node *old_version_node, | |
b2c0ad40 | 1668 | VEC(cgraph_edge_p,heap) *redirect_callers, |
9187e02d | 1669 | VEC (ipa_replace_map_p,gc)* tree_map, |
c6f7cfc1 | 1670 | bitmap args_to_skip) |
57fb5341 RL |
1671 | { |
1672 | tree old_decl = old_version_node->decl; | |
1673 | struct cgraph_node *new_version_node = NULL; | |
1674 | tree new_decl; | |
1675 | ||
1676 | if (!tree_versionable_function_p (old_decl)) | |
1677 | return NULL; | |
1678 | ||
1679 | /* Make a new FUNCTION_DECL tree node for the | |
1680 | new version. */ | |
c6f7cfc1 JH |
1681 | if (!args_to_skip) |
1682 | new_decl = copy_node (old_decl); | |
1683 | else | |
1684 | new_decl = build_function_decl_skip_args (old_decl, args_to_skip); | |
57fb5341 RL |
1685 | |
1686 | /* Create the new version's call-graph node. | |
1687 | and update the edges of the new node. */ | |
1688 | new_version_node = | |
1689 | cgraph_copy_node_for_versioning (old_version_node, new_decl, | |
1690 | redirect_callers); | |
1691 | ||
1692 | /* Copy the OLD_VERSION_NODE function tree to the new version. */ | |
c6f7cfc1 | 1693 | tree_function_versioning (old_decl, new_decl, tree_map, false, args_to_skip); |
57fb5341 | 1694 | |
c22cacf3 | 1695 | /* Update the new version's properties. |
c0ab1df3 AP |
1696 | Make The new version visible only within this translation unit. Make sure |
1697 | that is not weak also. | |
c22cacf3 | 1698 | ??? We cannot use COMDAT linkage because there is no |
57fb5341 RL |
1699 | ABI support for this. */ |
1700 | DECL_EXTERNAL (new_version_node->decl) = 0; | |
fc26fae3 | 1701 | DECL_COMDAT_GROUP (new_version_node->decl) = NULL_TREE; |
57fb5341 RL |
1702 | TREE_PUBLIC (new_version_node->decl) = 0; |
1703 | DECL_COMDAT (new_version_node->decl) = 0; | |
c0ab1df3 | 1704 | DECL_WEAK (new_version_node->decl) = 0; |
e6e1c050 | 1705 | DECL_VIRTUAL_P (new_version_node->decl) = 0; |
57fb5341 RL |
1706 | new_version_node->local.externally_visible = 0; |
1707 | new_version_node->local.local = 1; | |
1708 | new_version_node->lowered = true; | |
e6e1c050 | 1709 | |
c0ab1df3 AP |
1710 | /* Update the call_expr on the edges to call the new version node. */ |
1711 | update_call_expr (new_version_node); | |
1712 | ||
129a37fc | 1713 | cgraph_call_function_insertion_hooks (new_version_node); |
57fb5341 RL |
1714 | return new_version_node; |
1715 | } | |
ea99e0be JH |
1716 | |
1717 | /* Produce separate function body for inline clones so the offline copy can be | |
1718 | modified without affecting them. */ | |
1719 | struct cgraph_node * | |
1720 | save_inline_function_body (struct cgraph_node *node) | |
1721 | { | |
9187e02d | 1722 | struct cgraph_node *first_clone, *n; |
ea99e0be JH |
1723 | |
1724 | gcc_assert (node == cgraph_node (node->decl)); | |
1725 | ||
1726 | cgraph_lower_function (node); | |
1727 | ||
9187e02d | 1728 | first_clone = node->clones; |
ea99e0be JH |
1729 | |
1730 | first_clone->decl = copy_node (node->decl); | |
ea99e0be JH |
1731 | cgraph_insert_node_to_hashtable (first_clone); |
1732 | gcc_assert (first_clone == cgraph_node (first_clone->decl)); | |
9187e02d JH |
1733 | if (first_clone->next_sibling_clone) |
1734 | { | |
1735 | for (n = first_clone->next_sibling_clone; n->next_sibling_clone; n = n->next_sibling_clone) | |
1736 | n->clone_of = first_clone; | |
1737 | n->clone_of = first_clone; | |
1738 | n->next_sibling_clone = first_clone->clones; | |
1739 | if (first_clone->clones) | |
1740 | first_clone->clones->prev_sibling_clone = n; | |
1741 | first_clone->clones = first_clone->next_sibling_clone; | |
1742 | first_clone->next_sibling_clone->prev_sibling_clone = NULL; | |
1743 | first_clone->next_sibling_clone = NULL; | |
1744 | gcc_assert (!first_clone->prev_sibling_clone); | |
1745 | } | |
1746 | first_clone->clone_of = NULL; | |
1747 | node->clones = NULL; | |
1748 | ||
1749 | if (first_clone->clones) | |
1750 | for (n = first_clone->clones; n != first_clone;) | |
1751 | { | |
1752 | gcc_assert (n->decl == node->decl); | |
1753 | n->decl = first_clone->decl; | |
1754 | if (n->clones) | |
1755 | n = n->clones; | |
1756 | else if (n->next_sibling_clone) | |
1757 | n = n->next_sibling_clone; | |
1758 | else | |
1759 | { | |
1760 | while (n != first_clone && !n->next_sibling_clone) | |
1761 | n = n->clone_of; | |
1762 | if (n != first_clone) | |
1763 | n = n->next_sibling_clone; | |
1764 | } | |
1765 | } | |
ea99e0be JH |
1766 | |
1767 | /* Copy the OLD_VERSION_NODE function tree to the new version. */ | |
c6f7cfc1 | 1768 | tree_function_versioning (node->decl, first_clone->decl, NULL, true, NULL); |
ea99e0be JH |
1769 | |
1770 | DECL_EXTERNAL (first_clone->decl) = 0; | |
fc26fae3 | 1771 | DECL_COMDAT_GROUP (first_clone->decl) = NULL_TREE; |
ea99e0be JH |
1772 | TREE_PUBLIC (first_clone->decl) = 0; |
1773 | DECL_COMDAT (first_clone->decl) = 0; | |
21ecdec5 JH |
1774 | VEC_free (ipa_opt_pass, heap, |
1775 | DECL_STRUCT_FUNCTION (first_clone->decl)->ipa_transforms_to_apply); | |
1776 | DECL_STRUCT_FUNCTION (first_clone->decl)->ipa_transforms_to_apply = NULL; | |
ea99e0be | 1777 | |
ea99e0be JH |
1778 | #ifdef ENABLE_CHECKING |
1779 | verify_cgraph_node (first_clone); | |
1780 | #endif | |
1781 | return first_clone; | |
1782 | } | |
7be82279 | 1783 | |
9187e02d JH |
1784 | /* Given virtual clone, turn it into actual clone. */ |
1785 | static void | |
1786 | cgraph_materialize_clone (struct cgraph_node *node) | |
1787 | { | |
1788 | bitmap_obstack_initialize (NULL); | |
1789 | /* Copy the OLD_VERSION_NODE function tree to the new version. */ | |
1790 | tree_function_versioning (node->clone_of->decl, node->decl, | |
1791 | node->clone.tree_map, true, | |
1792 | node->clone.args_to_skip); | |
08ad1d6d JH |
1793 | if (cgraph_dump_file) |
1794 | { | |
1795 | dump_function_to_file (node->clone_of->decl, cgraph_dump_file, dump_flags); | |
1796 | dump_function_to_file (node->decl, cgraph_dump_file, dump_flags); | |
1797 | } | |
9187e02d JH |
1798 | |
1799 | /* Function is no longer clone. */ | |
1800 | if (node->next_sibling_clone) | |
1801 | node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone; | |
1802 | if (node->prev_sibling_clone) | |
1803 | node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone; | |
1804 | else | |
1805 | node->clone_of->clones = node->next_sibling_clone; | |
1806 | node->next_sibling_clone = NULL; | |
1807 | node->prev_sibling_clone = NULL; | |
1808 | node->clone_of = NULL; | |
1809 | bitmap_obstack_release (NULL); | |
1810 | } | |
1811 | ||
1812 | /* Once all functions from compilation unit are in memory, produce all clones | |
1813 | and update all calls. | |
1814 | We might also do this on demand if we don't want to bring all functions to | |
1815 | memory prior compilation, but current WHOPR implementation does that and it is | |
1816 | is bit easier to keep everything right in this order. */ | |
1817 | void | |
1818 | cgraph_materialize_all_clones (void) | |
1819 | { | |
1820 | struct cgraph_node *node; | |
1821 | bool stabilized = false; | |
1822 | ||
1823 | if (cgraph_dump_file) | |
1824 | fprintf (cgraph_dump_file, "Materializing clones\n"); | |
1825 | #ifdef ENABLE_CHECKING | |
1826 | verify_cgraph (); | |
1827 | #endif | |
1828 | ||
1829 | /* We can also do topological order, but number of iterations should be | |
1830 | bounded by number of IPA passes since single IPA pass is probably not | |
1831 | going to create clones of clones it created itself. */ | |
1832 | while (!stabilized) | |
1833 | { | |
1834 | stabilized = true; | |
1835 | for (node = cgraph_nodes; node; node = node->next) | |
1836 | { | |
1837 | if (node->clone_of && node->decl != node->clone_of->decl | |
1838 | && !gimple_has_body_p (node->decl)) | |
1839 | { | |
1840 | if (gimple_has_body_p (node->clone_of->decl)) | |
1841 | { | |
1842 | if (cgraph_dump_file) | |
08ad1d6d JH |
1843 | { |
1844 | fprintf (cgraph_dump_file, "clonning %s to %s\n", | |
1845 | cgraph_node_name (node->clone_of), | |
1846 | cgraph_node_name (node)); | |
1847 | if (node->clone.tree_map) | |
1848 | { | |
1849 | unsigned int i; | |
1850 | fprintf (cgraph_dump_file, " replace map: "); | |
1851 | for (i = 0; i < VEC_length (ipa_replace_map_p, | |
1852 | node->clone.tree_map); | |
1853 | i++) | |
1854 | { | |
1855 | struct ipa_replace_map *replace_info; | |
1856 | replace_info = VEC_index (ipa_replace_map_p, | |
1857 | node->clone.tree_map, | |
1858 | i); | |
1859 | print_generic_expr (cgraph_dump_file, replace_info->old_tree, 0); | |
1860 | fprintf (cgraph_dump_file, " -> "); | |
1861 | print_generic_expr (cgraph_dump_file, replace_info->new_tree, 0); | |
1862 | fprintf (cgraph_dump_file, "%s%s;", | |
1863 | replace_info->replace_p ? "(replace)":"", | |
1864 | replace_info->ref_p ? "(ref)":""); | |
1865 | } | |
1866 | fprintf (cgraph_dump_file, "\n"); | |
1867 | } | |
1868 | if (node->clone.args_to_skip) | |
1869 | { | |
1870 | fprintf (cgraph_dump_file, " args_to_skip: "); | |
1871 | dump_bitmap (cgraph_dump_file, node->clone.args_to_skip); | |
1872 | } | |
1873 | if (node->clone.args_to_skip) | |
1874 | { | |
1875 | fprintf (cgraph_dump_file, " combined_args_to_skip:"); | |
1876 | dump_bitmap (cgraph_dump_file, node->clone.combined_args_to_skip); | |
1877 | } | |
1878 | } | |
9187e02d JH |
1879 | cgraph_materialize_clone (node); |
1880 | } | |
1881 | else | |
1882 | stabilized = false; | |
1883 | } | |
1884 | } | |
1885 | } | |
1886 | if (cgraph_dump_file) | |
1887 | fprintf (cgraph_dump_file, "Updating call sites\n"); | |
1888 | for (node = cgraph_nodes; node; node = node->next) | |
1889 | if (node->analyzed && gimple_has_body_p (node->decl) | |
1890 | && (!node->clone_of || node->clone_of->decl != node->decl)) | |
1891 | { | |
1892 | struct cgraph_edge *e; | |
1893 | ||
1894 | current_function_decl = node->decl; | |
1895 | push_cfun (DECL_STRUCT_FUNCTION (node->decl)); | |
1896 | for (e = node->callees; e; e = e->next_callee) | |
1897 | { | |
1898 | tree decl = gimple_call_fndecl (e->call_stmt); | |
9a23acef JH |
1899 | /* When function gets inlined, indirect inlining might've invented |
1900 | new edge for orginally indirect stmt. Since we are not | |
1901 | preserving clones in the original form, we must not update here | |
1902 | since other inline clones don't need to contain call to the same | |
1903 | call. Inliner will do the substitution for us later. */ | |
1904 | if (decl && decl != e->callee->decl) | |
9187e02d JH |
1905 | { |
1906 | gimple new_stmt; | |
1907 | gimple_stmt_iterator gsi; | |
1908 | ||
1909 | if (cgraph_dump_file) | |
1910 | { | |
1911 | fprintf (cgraph_dump_file, "updating call of %s in %s:", | |
1912 | cgraph_node_name (node), | |
1913 | cgraph_node_name (e->callee)); | |
1914 | print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags); | |
1915 | } | |
1916 | ||
08ad1d6d | 1917 | if (e->callee->clone.combined_args_to_skip) |
9187e02d | 1918 | new_stmt = gimple_call_copy_skip_args (e->call_stmt, |
08ad1d6d | 1919 | e->callee->clone.combined_args_to_skip); |
9187e02d JH |
1920 | else |
1921 | new_stmt = e->call_stmt; | |
1922 | if (gimple_vdef (new_stmt) | |
1923 | && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME) | |
1924 | SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt; | |
1925 | gimple_call_set_fndecl (new_stmt, e->callee->decl); | |
1926 | ||
1927 | gsi = gsi_for_stmt (e->call_stmt); | |
1928 | gsi_replace (&gsi, new_stmt, true); | |
1929 | ||
1930 | /* Update EH information too, just in case. */ | |
1d65f45c | 1931 | maybe_clean_or_replace_eh_stmt (e->call_stmt, new_stmt); |
9187e02d JH |
1932 | |
1933 | cgraph_set_call_stmt_including_clones (node, e->call_stmt, new_stmt); | |
1934 | ||
1935 | if (cgraph_dump_file) | |
1936 | { | |
1937 | fprintf (cgraph_dump_file, " updated to:"); | |
1938 | print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags); | |
1939 | } | |
1940 | } | |
1941 | } | |
1942 | pop_cfun (); | |
1943 | current_function_decl = NULL; | |
1944 | #ifdef ENABLE_CHECKING | |
1945 | verify_cgraph_node (node); | |
1946 | #endif | |
1947 | } | |
9a23acef JH |
1948 | #ifdef ENABLE_CHECKING |
1949 | verify_cgraph (); | |
1950 | #endif | |
9187e02d JH |
1951 | cgraph_remove_unreachable_nodes (false, cgraph_dump_file); |
1952 | } | |
1953 | ||
7be82279 | 1954 | #include "gt-cgraphunit.h" |