]>
Commit | Line | Data |
---|---|---|
cd6bca02 | 1 | /* Callgraph based interprocedural optimizations. |
aed6e608 | 2 | Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, |
3 | 2011 Free Software Foundation, Inc. | |
ae01b312 | 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 | |
8c4c00c1 | 10 | Software Foundation; either version 3, or (at your option) any later |
ae01b312 | 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 | |
8c4c00c1 | 19 | along with GCC; see the file COPYING3. If not see |
20 | <http://www.gnu.org/licenses/>. */ | |
ae01b312 | 21 | |
b0cdf642 | 22 | /* This module implements main driver of compilation process as well as |
cd6bca02 | 23 | few basic interprocedural optimizers. |
b0cdf642 | 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 | ||
b326746d | 35 | (There is one exception needed for implementing GCC extern inline |
36 | function.) | |
b0cdf642 | 37 | |
1d416bd7 | 38 | - varpool_finalize_variable |
b0cdf642 | 39 | |
7bd28bba | 40 | This function has same behavior as the above but is used for static |
b0cdf642 | 41 | variables. |
42 | ||
43 | - cgraph_finalize_compilation_unit | |
44 | ||
b326746d | 45 | This function is called once (source level) compilation unit is finalized |
46 | and it will no longer change. | |
b0cdf642 | 47 | |
851d9296 | 48 | In the call-graph construction and local function analysis takes |
49 | place here. Bodies of unreachable functions are released to | |
50 | conserve memory usage. | |
b0cdf642 | 51 | |
b326746d | 52 | The function can be called multiple times when multiple source level |
53 | compilation units are combined (such as in C frontend) | |
b0cdf642 | 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 | ||
b0cdf642 | 62 | - cgraph_mark_needed_node |
1d416bd7 | 63 | - varpool_mark_needed_node |
b0cdf642 | 64 | |
b326746d | 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 | |
ccd2f3d1 | 69 | used by C++ frontend to explicitly mark the keyed methods. |
b0cdf642 | 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 | ||
6329636b | 80 | Analyzing of all functions is deferred |
b0cdf642 | 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 | ||
ca67a72b | 91 | The intra-procedural information is produced and its existence |
b0cdf642 | 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 | |
6329636b | 106 | where reference has been optimized out. */ |
121f3051 | 107 | |
acc70efa | 108 | |
ae01b312 | 109 | #include "config.h" |
110 | #include "system.h" | |
111 | #include "coretypes.h" | |
112 | #include "tm.h" | |
113 | #include "tree.h" | |
b5530559 | 114 | #include "rtl.h" |
acc70efa | 115 | #include "tree-flow.h" |
ae01b312 | 116 | #include "tree-inline.h" |
117 | #include "langhooks.h" | |
c6224531 | 118 | #include "pointer-set.h" |
ae01b312 | 119 | #include "toplev.h" |
120 | #include "flags.h" | |
121 | #include "ggc.h" | |
122 | #include "debug.h" | |
123 | #include "target.h" | |
124 | #include "cgraph.h" | |
80a85d8a | 125 | #include "diagnostic.h" |
ce084dfc | 126 | #include "tree-pretty-print.h" |
127 | #include "gimple-pretty-print.h" | |
f79b6507 | 128 | #include "timevar.h" |
d7c6d889 | 129 | #include "params.h" |
130 | #include "fibheap.h" | |
611e5405 | 131 | #include "intl.h" |
b69eb0ff | 132 | #include "function.h" |
b5d36404 | 133 | #include "ipa-prop.h" |
75a70cf9 | 134 | #include "gimple.h" |
135 | #include "tree-iterator.h" | |
f1e2a033 | 136 | #include "tree-pass.h" |
bfec3452 | 137 | #include "tree-dump.h" |
c1dcd13c | 138 | #include "output.h" |
9ed5b1f5 | 139 | #include "coverage.h" |
c9036234 | 140 | #include "plugin.h" |
a41f2a28 | 141 | #include "ipa-inline.h" |
7771d558 | 142 | #include "ipa-utils.h" |
a0605d65 | 143 | #include "lto-streamer.h" |
d7c6d889 | 144 | |
a6868229 | 145 | static void cgraph_expand_all_functions (void); |
d9d9733a | 146 | static void cgraph_mark_functions_to_output (void); |
147 | static void cgraph_expand_function (struct cgraph_node *); | |
f788fff2 | 148 | static void cgraph_output_pending_asms (void); |
25bb88de | 149 | |
ecb08119 | 150 | FILE *cgraph_dump_file; |
121f3051 | 151 | |
28454517 | 152 | /* Used for vtable lookup in thunk adjusting. */ |
153 | static GTY (()) tree vtable_entry_type; | |
154 | ||
2c0b522d | 155 | /* Determine if function DECL is needed. That is, visible to something |
156 | either outside this translation unit, something magic in the system | |
6329636b | 157 | configury. */ |
2c0b522d | 158 | |
7bfefa9d | 159 | bool |
160 | cgraph_decide_is_function_needed (struct cgraph_node *node, tree decl) | |
2c0b522d | 161 | { |
3f82b628 | 162 | /* If the user told us it is used, then it must be so. */ |
05806473 | 163 | if (node->local.externally_visible) |
164 | return true; | |
165 | ||
3f82b628 | 166 | /* ??? If the assembler name is set by hand, it is possible to assemble |
167 | the name later after finalizing the function and the fact is noticed | |
168 | in assemble_name then. This is arguably a bug. */ | |
169 | if (DECL_ASSEMBLER_NAME_SET_P (decl) | |
170 | && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))) | |
171 | return true; | |
172 | ||
55680bef | 173 | /* With -fkeep-inline-functions we are keeping all inline functions except |
174 | for extern inline ones. */ | |
175 | if (flag_keep_inline_functions | |
176 | && DECL_DECLARED_INLINE_P (decl) | |
316ef6d8 | 177 | && !DECL_EXTERNAL (decl) |
cbd7f5a0 | 178 | && !DECL_DISREGARD_INLINE_LIMITS (decl)) |
55680bef | 179 | return true; |
180 | ||
2c0b522d | 181 | /* If we decided it was needed before, but at the time we didn't have |
182 | the body of the function available, then it's still needed. We have | |
183 | to go back and re-check its dependencies now. */ | |
184 | if (node->needed) | |
185 | return true; | |
186 | ||
187 | /* Externally visible functions must be output. The exception is | |
a0c938f0 | 188 | COMDAT functions that must be output only when they are needed. |
8baa9d15 | 189 | |
190 | When not optimizing, also output the static functions. (see | |
95da6220 | 191 | PR24561), but don't do so for always_inline functions, functions |
0f9238c0 | 192 | declared inline and nested functions. These were optimized out |
d3d410e1 | 193 | in the original implementation and it is unclear whether we want |
554f2707 | 194 | to change the behavior here. */ |
bba7ddf8 | 195 | if (((TREE_PUBLIC (decl) |
0f9238c0 | 196 | || (!optimize |
cbd7f5a0 | 197 | && !DECL_DISREGARD_INLINE_LIMITS (decl) |
d3d410e1 | 198 | && !DECL_DECLARED_INLINE_P (decl) |
0f9238c0 | 199 | && !(DECL_CONTEXT (decl) |
200 | && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))) | |
59dd4830 | 201 | && !flag_whole_program |
cbcf2791 | 202 | && !flag_lto) |
62eec3b4 | 203 | && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl)) |
2c0b522d | 204 | return true; |
205 | ||
2c0b522d | 206 | return false; |
207 | } | |
208 | ||
bdc40eb8 | 209 | /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these |
523c1122 | 210 | functions into callgraph in a way so they look like ordinary reachable |
211 | functions inserted into callgraph already at construction time. */ | |
212 | ||
213 | bool | |
214 | cgraph_process_new_functions (void) | |
215 | { | |
216 | bool output = false; | |
217 | tree fndecl; | |
218 | struct cgraph_node *node; | |
219 | ||
0cddb138 | 220 | varpool_analyze_pending_decls (); |
523c1122 | 221 | /* Note that this queue may grow as its being processed, as the new |
222 | functions may generate new ones. */ | |
223 | while (cgraph_new_nodes) | |
224 | { | |
225 | node = cgraph_new_nodes; | |
226 | fndecl = node->decl; | |
227 | cgraph_new_nodes = cgraph_new_nodes->next_needed; | |
228 | switch (cgraph_state) | |
229 | { | |
230 | case CGRAPH_STATE_CONSTRUCTION: | |
231 | /* At construction time we just need to finalize function and move | |
232 | it into reachable functions list. */ | |
233 | ||
234 | node->next_needed = NULL; | |
235 | cgraph_finalize_function (fndecl, false); | |
236 | cgraph_mark_reachable_node (node); | |
237 | output = true; | |
4f7a1122 | 238 | cgraph_call_function_insertion_hooks (node); |
523c1122 | 239 | break; |
240 | ||
241 | case CGRAPH_STATE_IPA: | |
f517b36e | 242 | case CGRAPH_STATE_IPA_SSA: |
523c1122 | 243 | /* When IPA optimization already started, do all essential |
244 | transformations that has been already performed on the whole | |
245 | cgraph but not on this function. */ | |
246 | ||
75a70cf9 | 247 | gimple_register_cfg_hooks (); |
523c1122 | 248 | if (!node->analyzed) |
249 | cgraph_analyze_function (node); | |
250 | push_cfun (DECL_STRUCT_FUNCTION (fndecl)); | |
251 | current_function_decl = fndecl; | |
f517b36e | 252 | if ((cgraph_state == CGRAPH_STATE_IPA_SSA |
253 | && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl))) | |
254 | /* When not optimizing, be sure we run early local passes anyway | |
255 | to expand OMP. */ | |
256 | || !optimize) | |
20099e35 | 257 | execute_pass_list (pass_early_local_passes.pass.sub); |
649597af | 258 | else |
a41f2a28 | 259 | compute_inline_parameters (node, true); |
523c1122 | 260 | free_dominance_info (CDI_POST_DOMINATORS); |
261 | free_dominance_info (CDI_DOMINATORS); | |
262 | pop_cfun (); | |
263 | current_function_decl = NULL; | |
4f7a1122 | 264 | cgraph_call_function_insertion_hooks (node); |
523c1122 | 265 | break; |
266 | ||
267 | case CGRAPH_STATE_EXPANSION: | |
268 | /* Functions created during expansion shall be compiled | |
269 | directly. */ | |
09fc9532 | 270 | node->process = 0; |
4f7a1122 | 271 | cgraph_call_function_insertion_hooks (node); |
523c1122 | 272 | cgraph_expand_function (node); |
273 | break; | |
274 | ||
275 | default: | |
276 | gcc_unreachable (); | |
277 | break; | |
278 | } | |
0cddb138 | 279 | varpool_analyze_pending_decls (); |
523c1122 | 280 | } |
281 | return output; | |
282 | } | |
283 | ||
9b8fb23a | 284 | /* As an GCC extension we allow redefinition of the function. The |
285 | semantics when both copies of bodies differ is not well defined. | |
286 | We replace the old body with new body so in unit at a time mode | |
287 | we always use new body, while in normal mode we may end up with | |
288 | old body inlined into some functions and new body expanded and | |
289 | inlined in others. | |
290 | ||
291 | ??? It may make more sense to use one body for inlining and other | |
292 | body for expanding the function but this is difficult to do. */ | |
293 | ||
294 | static void | |
295 | cgraph_reset_node (struct cgraph_node *node) | |
296 | { | |
09fc9532 | 297 | /* If node->process is set, then we have already begun whole-unit analysis. |
6329636b | 298 | This is *not* testing for whether we've already emitted the function. |
299 | That case can be sort-of legitimately seen with real function redefinition | |
300 | errors. I would argue that the front end should never present us with | |
301 | such a case, but don't enforce that for now. */ | |
09fc9532 | 302 | gcc_assert (!node->process); |
9b8fb23a | 303 | |
304 | /* Reset our data structures so we can analyze the function again. */ | |
305 | memset (&node->local, 0, sizeof (node->local)); | |
306 | memset (&node->global, 0, sizeof (node->global)); | |
307 | memset (&node->rtl, 0, sizeof (node->rtl)); | |
308 | node->analyzed = false; | |
9b8fb23a | 309 | node->local.finalized = false; |
310 | ||
9b8fb23a | 311 | cgraph_node_remove_callees (node); |
9b8fb23a | 312 | } |
c08871a9 | 313 | |
1e8e9920 | 314 | static void |
315 | cgraph_lower_function (struct cgraph_node *node) | |
316 | { | |
317 | if (node->lowered) | |
318 | return; | |
bfec3452 | 319 | |
320 | if (node->nested) | |
321 | lower_nested_functions (node->decl); | |
322 | gcc_assert (!node->nested); | |
323 | ||
1e8e9920 | 324 | tree_lowering_passes (node->decl); |
325 | node->lowered = true; | |
326 | } | |
327 | ||
28df663b | 328 | /* DECL has been parsed. Take it, queue it, compile it at the whim of the |
329 | logic in effect. If NESTED is true, then our caller cannot stand to have | |
330 | the garbage collector run at the moment. We would need to either create | |
331 | a new GC context, or just not compile right now. */ | |
ae01b312 | 332 | |
333 | void | |
28df663b | 334 | cgraph_finalize_function (tree decl, bool nested) |
ae01b312 | 335 | { |
5a90471f | 336 | struct cgraph_node *node = cgraph_get_create_node (decl); |
ae01b312 | 337 | |
c08871a9 | 338 | if (node->local.finalized) |
443089c1 | 339 | { |
340 | cgraph_reset_node (node); | |
341 | node->local.redefined_extern_inline = true; | |
342 | } | |
28df663b | 343 | |
c08871a9 | 344 | notice_global_symbol (decl); |
79bb87b4 | 345 | node->local.finalized = true; |
e27482aa | 346 | node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL; |
ae01b312 | 347 | |
7bfefa9d | 348 | if (cgraph_decide_is_function_needed (node, decl)) |
2c0b522d | 349 | cgraph_mark_needed_node (node); |
350 | ||
ecda6e51 | 351 | /* Since we reclaim unreachable nodes at the end of every language |
3f82b628 | 352 | level unit, we need to be conservative about possible entry points |
353 | there. */ | |
1e3aebec | 354 | if ((TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl)) |
355 | || DECL_STATIC_CONSTRUCTOR (decl) | |
d050bafd | 356 | || DECL_STATIC_DESTRUCTOR (decl) |
357 | /* COMDAT virtual functions may be referenced by vtable from | |
0a10fd82 | 358 | other compilation unit. Still we want to devirtualize calls |
d050bafd | 359 | to those so we need to analyze them. |
360 | FIXME: We should introduce may edges for this purpose and update | |
361 | their handling in unreachable function removal and inliner too. */ | |
91bf9d9a | 362 | || (DECL_VIRTUAL_P (decl) |
363 | && optimize && (DECL_COMDAT (decl) || DECL_EXTERNAL (decl)))) | |
3f82b628 | 364 | cgraph_mark_reachable_node (node); |
365 | ||
2c0b522d | 366 | /* If we've not yet emitted decl, tell the debug info about it. */ |
28df663b | 367 | if (!TREE_ASM_WRITTEN (decl)) |
2c0b522d | 368 | (*debug_hooks->deferred_inline_function) (decl); |
4e8871a0 | 369 | |
b69eb0ff | 370 | /* Possibly warn about unused parameters. */ |
371 | if (warn_unused_parameter) | |
372 | do_warn_unused_parameter (decl); | |
6329636b | 373 | |
374 | if (!nested) | |
375 | ggc_collect (); | |
ae01b312 | 376 | } |
377 | ||
0da03d11 | 378 | /* C99 extern inline keywords allow changing of declaration after function |
379 | has been finalized. We need to re-decide if we want to mark the function as | |
380 | needed then. */ | |
381 | ||
382 | void | |
383 | cgraph_mark_if_needed (tree decl) | |
384 | { | |
fd6a3c41 | 385 | struct cgraph_node *node = cgraph_get_node (decl); |
7bfefa9d | 386 | if (node->local.finalized && cgraph_decide_is_function_needed (node, decl)) |
0da03d11 | 387 | cgraph_mark_needed_node (node); |
388 | } | |
389 | ||
ccf4ab6b | 390 | /* Return TRUE if NODE2 is equivalent to NODE or its clone. */ |
391 | static bool | |
392 | clone_of_p (struct cgraph_node *node, struct cgraph_node *node2) | |
393 | { | |
394 | while (node != node2 && node2) | |
395 | node2 = node2->clone_of; | |
396 | return node2 != NULL; | |
397 | } | |
398 | ||
1a036a3b | 399 | /* Verify edge E count and frequency. */ |
400 | ||
401 | static bool | |
402 | verify_edge_count_and_frequency (struct cgraph_edge *e) | |
403 | { | |
404 | bool error_found = false; | |
405 | if (e->count < 0) | |
406 | { | |
407 | error ("caller edge count is negative"); | |
408 | error_found = true; | |
409 | } | |
410 | if (e->frequency < 0) | |
411 | { | |
412 | error ("caller edge frequency is negative"); | |
413 | error_found = true; | |
414 | } | |
415 | if (e->frequency > CGRAPH_FREQ_MAX) | |
416 | { | |
417 | error ("caller edge frequency is too large"); | |
418 | error_found = true; | |
419 | } | |
420 | if (gimple_has_body_p (e->caller->decl) | |
421 | && !e->caller->global.inlined_to | |
8bae3ea4 | 422 | /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out. |
423 | Remove this once edges are actualy removed from the function at that time. */ | |
424 | && (e->frequency | |
425 | || (inline_edge_summary_vec | |
426 | && !inline_edge_summary (e)->predicate)) | |
1a036a3b | 427 | && (e->frequency |
428 | != compute_call_stmt_bb_frequency (e->caller->decl, | |
429 | gimple_bb (e->call_stmt)))) | |
430 | { | |
0a10fd82 | 431 | error ("caller edge frequency %i does not match BB frequency %i", |
1a036a3b | 432 | e->frequency, |
433 | compute_call_stmt_bb_frequency (e->caller->decl, | |
434 | gimple_bb (e->call_stmt))); | |
435 | error_found = true; | |
436 | } | |
437 | return error_found; | |
438 | } | |
439 | ||
7b29dd2f | 440 | /* Switch to THIS_CFUN if needed and print STMT to stderr. */ |
441 | static void | |
442 | cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt) | |
443 | { | |
444 | /* debug_gimple_stmt needs correct cfun */ | |
445 | if (cfun != this_cfun) | |
446 | set_cfun (this_cfun); | |
447 | debug_gimple_stmt (stmt); | |
448 | } | |
449 | ||
b0cdf642 | 450 | /* Verify cgraph nodes of given cgraph node. */ |
4b987fac | 451 | DEBUG_FUNCTION void |
b0cdf642 | 452 | verify_cgraph_node (struct cgraph_node *node) |
453 | { | |
454 | struct cgraph_edge *e; | |
e27482aa | 455 | struct function *this_cfun = DECL_STRUCT_FUNCTION (node->decl); |
456 | basic_block this_block; | |
75a70cf9 | 457 | gimple_stmt_iterator gsi; |
9bfec7c2 | 458 | bool error_found = false; |
b0cdf642 | 459 | |
852f689e | 460 | if (seen_error ()) |
bd09cd3e | 461 | return; |
462 | ||
b0cdf642 | 463 | timevar_push (TV_CGRAPH_VERIFY); |
b0cdf642 | 464 | for (e = node->callees; e; e = e->next_callee) |
465 | if (e->aux) | |
466 | { | |
0a81f5a0 | 467 | error ("aux field set for edge %s->%s", |
abd3e6b5 | 468 | identifier_to_locale (cgraph_node_name (e->caller)), |
469 | identifier_to_locale (cgraph_node_name (e->callee))); | |
b0cdf642 | 470 | error_found = true; |
471 | } | |
a2cb9b3b | 472 | if (node->count < 0) |
473 | { | |
bf776685 | 474 | error ("execution count is negative"); |
a2cb9b3b | 475 | error_found = true; |
476 | } | |
59dd4830 | 477 | if (node->global.inlined_to && node->local.externally_visible) |
478 | { | |
bf776685 | 479 | error ("externally visible inline clone"); |
59dd4830 | 480 | error_found = true; |
481 | } | |
482 | if (node->global.inlined_to && node->address_taken) | |
483 | { | |
bf776685 | 484 | error ("inline clone with address taken"); |
59dd4830 | 485 | error_found = true; |
486 | } | |
487 | if (node->global.inlined_to && node->needed) | |
488 | { | |
bf776685 | 489 | error ("inline clone is needed"); |
59dd4830 | 490 | error_found = true; |
491 | } | |
799c8711 | 492 | for (e = node->indirect_calls; e; e = e->next_callee) |
493 | { | |
494 | if (e->aux) | |
495 | { | |
496 | error ("aux field set for indirect edge from %s", | |
497 | identifier_to_locale (cgraph_node_name (e->caller))); | |
498 | error_found = true; | |
499 | } | |
500 | if (!e->indirect_unknown_callee | |
501 | || !e->indirect_info) | |
502 | { | |
503 | error ("An indirect edge from %s is not marked as indirect or has " | |
504 | "associated indirect_info, the corresponding statement is: ", | |
505 | identifier_to_locale (cgraph_node_name (e->caller))); | |
7b29dd2f | 506 | cgraph_debug_gimple_stmt (this_cfun, e->call_stmt); |
799c8711 | 507 | error_found = true; |
508 | } | |
509 | } | |
b0cdf642 | 510 | for (e = node->callers; e; e = e->next_caller) |
511 | { | |
1a036a3b | 512 | if (verify_edge_count_and_frequency (e)) |
513 | error_found = true; | |
b0cdf642 | 514 | if (!e->inline_failed) |
515 | { | |
516 | if (node->global.inlined_to | |
517 | != (e->caller->global.inlined_to | |
518 | ? e->caller->global.inlined_to : e->caller)) | |
519 | { | |
0a81f5a0 | 520 | error ("inlined_to pointer is wrong"); |
b0cdf642 | 521 | error_found = true; |
522 | } | |
523 | if (node->callers->next_caller) | |
524 | { | |
0a81f5a0 | 525 | error ("multiple inline callers"); |
b0cdf642 | 526 | error_found = true; |
527 | } | |
528 | } | |
529 | else | |
530 | if (node->global.inlined_to) | |
531 | { | |
0a81f5a0 | 532 | error ("inlined_to pointer set for noninline callers"); |
b0cdf642 | 533 | error_found = true; |
534 | } | |
535 | } | |
1a036a3b | 536 | for (e = node->indirect_calls; e; e = e->next_callee) |
537 | if (verify_edge_count_and_frequency (e)) | |
538 | error_found = true; | |
b0cdf642 | 539 | if (!node->callers && node->global.inlined_to) |
540 | { | |
5cd75817 | 541 | error ("inlined_to pointer is set but no predecessors found"); |
b0cdf642 | 542 | error_found = true; |
543 | } | |
544 | if (node->global.inlined_to == node) | |
545 | { | |
0a81f5a0 | 546 | error ("inlined_to pointer refers to itself"); |
b0cdf642 | 547 | error_found = true; |
548 | } | |
549 | ||
7019fd3f | 550 | if (!cgraph_get_node (node->decl)) |
b0cdf642 | 551 | { |
0f6439b9 | 552 | error ("node not found in cgraph_hash"); |
b0cdf642 | 553 | error_found = true; |
554 | } | |
a0c938f0 | 555 | |
ccf4ab6b | 556 | if (node->clone_of) |
557 | { | |
558 | struct cgraph_node *n; | |
559 | for (n = node->clone_of->clones; n; n = n->next_sibling_clone) | |
560 | if (n == node) | |
561 | break; | |
562 | if (!n) | |
563 | { | |
564 | error ("node has wrong clone_of"); | |
565 | error_found = true; | |
566 | } | |
567 | } | |
568 | if (node->clones) | |
569 | { | |
570 | struct cgraph_node *n; | |
571 | for (n = node->clones; n; n = n->next_sibling_clone) | |
572 | if (n->clone_of != node) | |
573 | break; | |
574 | if (n) | |
575 | { | |
576 | error ("node has wrong clone list"); | |
577 | error_found = true; | |
578 | } | |
579 | } | |
580 | if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of) | |
581 | { | |
582 | error ("node is in clone list but it is not clone"); | |
583 | error_found = true; | |
584 | } | |
585 | if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node) | |
586 | { | |
587 | error ("node has wrong prev_clone pointer"); | |
588 | error_found = true; | |
589 | } | |
590 | if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node) | |
591 | { | |
592 | error ("double linked list of clones corrupted"); | |
593 | error_found = true; | |
594 | } | |
c524ac5d | 595 | if (node->same_comdat_group) |
596 | { | |
597 | struct cgraph_node *n = node->same_comdat_group; | |
598 | ||
599 | if (!DECL_ONE_ONLY (node->decl)) | |
600 | { | |
601 | error ("non-DECL_ONE_ONLY node in a same_comdat_group list"); | |
602 | error_found = true; | |
603 | } | |
604 | if (n == node) | |
605 | { | |
606 | error ("node is alone in a comdat group"); | |
607 | error_found = true; | |
608 | } | |
609 | do | |
610 | { | |
611 | if (!n->same_comdat_group) | |
612 | { | |
613 | error ("same_comdat_group is not a circular list"); | |
614 | error_found = true; | |
615 | break; | |
616 | } | |
617 | n = n->same_comdat_group; | |
618 | } | |
619 | while (n != node); | |
620 | } | |
ccf4ab6b | 621 | |
91bf9d9a | 622 | if (node->analyzed && node->thunk.thunk_p) |
623 | { | |
624 | if (!node->callees) | |
625 | { | |
626 | error ("No edge out of thunk node"); | |
627 | error_found = true; | |
628 | } | |
629 | else if (node->callees->next_callee) | |
630 | { | |
631 | error ("More than one edge out of thunk node"); | |
632 | error_found = true; | |
633 | } | |
634 | if (gimple_has_body_p (node->decl)) | |
635 | { | |
636 | error ("Thunk is not supposed to have body"); | |
637 | error_found = true; | |
638 | } | |
639 | } | |
640 | else if (node->analyzed && gimple_has_body_p (node->decl) | |
641 | && !TREE_ASM_WRITTEN (node->decl) | |
642 | && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to) | |
643 | && !flag_wpa) | |
b0cdf642 | 644 | { |
e27482aa | 645 | if (this_cfun->cfg) |
646 | { | |
647 | /* The nodes we're interested in are never shared, so walk | |
648 | the tree ignoring duplicates. */ | |
e7c352d1 | 649 | struct pointer_set_t *visited_nodes = pointer_set_create (); |
e27482aa | 650 | /* Reach the trees by walking over the CFG, and note the |
651 | enclosing basic-blocks in the call edges. */ | |
652 | FOR_EACH_BB_FN (this_block, this_cfun) | |
75a70cf9 | 653 | for (gsi = gsi_start_bb (this_block); |
654 | !gsi_end_p (gsi); | |
655 | gsi_next (&gsi)) | |
9bfec7c2 | 656 | { |
75a70cf9 | 657 | gimple stmt = gsi_stmt (gsi); |
799c8711 | 658 | if (is_gimple_call (stmt)) |
9bfec7c2 | 659 | { |
660 | struct cgraph_edge *e = cgraph_edge (node, stmt); | |
799c8711 | 661 | tree decl = gimple_call_fndecl (stmt); |
9bfec7c2 | 662 | if (e) |
663 | { | |
664 | if (e->aux) | |
665 | { | |
0a81f5a0 | 666 | error ("shared call_stmt:"); |
7b29dd2f | 667 | cgraph_debug_gimple_stmt (this_cfun, stmt); |
9bfec7c2 | 668 | error_found = true; |
669 | } | |
799c8711 | 670 | if (!e->indirect_unknown_callee) |
28454517 | 671 | { |
799c8711 | 672 | if (e->callee->same_body_alias) |
673 | { | |
674 | error ("edge points to same body alias:"); | |
675 | debug_tree (e->callee->decl); | |
676 | error_found = true; | |
677 | } | |
e748b31d | 678 | else if (!e->callee->global.inlined_to |
799c8711 | 679 | && decl |
e748b31d | 680 | && cgraph_get_node (decl) |
681 | && (e->callee->former_clone_of | |
682 | != cgraph_get_node (decl)->decl) | |
fd6a3c41 | 683 | && !clone_of_p (cgraph_get_node (decl), |
799c8711 | 684 | e->callee)) |
685 | { | |
686 | error ("edge points to wrong declaration:"); | |
687 | debug_tree (e->callee->decl); | |
688 | fprintf (stderr," Instead of:"); | |
689 | debug_tree (decl); | |
690 | error_found = true; | |
691 | } | |
28454517 | 692 | } |
799c8711 | 693 | else if (decl) |
9bfec7c2 | 694 | { |
799c8711 | 695 | error ("an indirect edge with unknown callee " |
696 | "corresponding to a call_stmt with " | |
697 | "a known declaration:"); | |
ee3f5fc0 | 698 | error_found = true; |
7b29dd2f | 699 | cgraph_debug_gimple_stmt (this_cfun, e->call_stmt); |
9bfec7c2 | 700 | } |
701 | e->aux = (void *)1; | |
702 | } | |
799c8711 | 703 | else if (decl) |
9bfec7c2 | 704 | { |
0a81f5a0 | 705 | error ("missing callgraph edge for call stmt:"); |
7b29dd2f | 706 | cgraph_debug_gimple_stmt (this_cfun, stmt); |
9bfec7c2 | 707 | error_found = true; |
708 | } | |
709 | } | |
710 | } | |
e27482aa | 711 | pointer_set_destroy (visited_nodes); |
e27482aa | 712 | } |
713 | else | |
714 | /* No CFG available?! */ | |
715 | gcc_unreachable (); | |
716 | ||
b0cdf642 | 717 | for (e = node->callees; e; e = e->next_callee) |
718 | { | |
799c8711 | 719 | if (!e->aux) |
b0cdf642 | 720 | { |
0a81f5a0 | 721 | error ("edge %s->%s has no corresponding call_stmt", |
abd3e6b5 | 722 | identifier_to_locale (cgraph_node_name (e->caller)), |
723 | identifier_to_locale (cgraph_node_name (e->callee))); | |
7b29dd2f | 724 | cgraph_debug_gimple_stmt (this_cfun, e->call_stmt); |
b0cdf642 | 725 | error_found = true; |
726 | } | |
727 | e->aux = 0; | |
728 | } | |
799c8711 | 729 | for (e = node->indirect_calls; e; e = e->next_callee) |
730 | { | |
731 | if (!e->aux) | |
732 | { | |
733 | error ("an indirect edge from %s has no corresponding call_stmt", | |
734 | identifier_to_locale (cgraph_node_name (e->caller))); | |
7b29dd2f | 735 | cgraph_debug_gimple_stmt (this_cfun, e->call_stmt); |
799c8711 | 736 | error_found = true; |
737 | } | |
738 | e->aux = 0; | |
739 | } | |
b0cdf642 | 740 | } |
741 | if (error_found) | |
742 | { | |
743 | dump_cgraph_node (stderr, node); | |
0a81f5a0 | 744 | internal_error ("verify_cgraph_node failed"); |
b0cdf642 | 745 | } |
746 | timevar_pop (TV_CGRAPH_VERIFY); | |
747 | } | |
748 | ||
749 | /* Verify whole cgraph structure. */ | |
4b987fac | 750 | DEBUG_FUNCTION void |
b0cdf642 | 751 | verify_cgraph (void) |
752 | { | |
753 | struct cgraph_node *node; | |
754 | ||
852f689e | 755 | if (seen_error ()) |
8ec2a798 | 756 | return; |
757 | ||
b0cdf642 | 758 | for (node = cgraph_nodes; node; node = node->next) |
759 | verify_cgraph_node (node); | |
760 | } | |
761 | ||
56af936e | 762 | /* Output all asm statements we have stored up to be output. */ |
763 | ||
764 | static void | |
765 | cgraph_output_pending_asms (void) | |
766 | { | |
767 | struct cgraph_asm_node *can; | |
768 | ||
852f689e | 769 | if (seen_error ()) |
56af936e | 770 | return; |
771 | ||
772 | for (can = cgraph_asm_nodes; can; can = can->next) | |
773 | assemble_asm (can->asm_str); | |
774 | cgraph_asm_nodes = NULL; | |
775 | } | |
776 | ||
0785e435 | 777 | /* Analyze the function scheduled to be output. */ |
222bc9b9 | 778 | void |
0785e435 | 779 | cgraph_analyze_function (struct cgraph_node *node) |
780 | { | |
bfec3452 | 781 | tree save = current_function_decl; |
0785e435 | 782 | tree decl = node->decl; |
783 | ||
91bf9d9a | 784 | if (node->thunk.thunk_p) |
785 | { | |
786 | cgraph_create_edge (node, cgraph_get_node (node->thunk.alias), | |
787 | NULL, 0, CGRAPH_FREQ_BASE); | |
788 | } | |
789 | else | |
790 | { | |
791 | current_function_decl = decl; | |
792 | push_cfun (DECL_STRUCT_FUNCTION (decl)); | |
bfec3452 | 793 | |
91bf9d9a | 794 | assign_assembler_name_if_neeeded (node->decl); |
6816d0c4 | 795 | |
91bf9d9a | 796 | /* Make sure to gimplify bodies only once. During analyzing a |
797 | function we lower it, which will require gimplified nested | |
798 | functions, so we can end up here with an already gimplified | |
799 | body. */ | |
800 | if (!gimple_body (decl)) | |
801 | gimplify_function_tree (decl); | |
802 | dump_function (TDI_generic, decl); | |
bfec3452 | 803 | |
91bf9d9a | 804 | cgraph_lower_function (node); |
805 | pop_cfun (); | |
806 | } | |
6e8d6e86 | 807 | node->analyzed = true; |
0785e435 | 808 | |
bfec3452 | 809 | current_function_decl = save; |
0785e435 | 810 | } |
811 | ||
d05db70d | 812 | /* Process attributes common for vars and functions. */ |
813 | ||
814 | static void | |
815 | process_common_attributes (tree decl) | |
816 | { | |
817 | tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)); | |
818 | ||
819 | if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl))) | |
820 | { | |
821 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes, | |
822 | "%<weakref%> attribute should be accompanied with" | |
823 | " an %<alias%> attribute"); | |
824 | DECL_WEAK (decl) = 0; | |
40b32d93 | 825 | DECL_ATTRIBUTES (decl) = remove_attribute ("weakref", |
826 | DECL_ATTRIBUTES (decl)); | |
d05db70d | 827 | } |
828 | } | |
829 | ||
05806473 | 830 | /* Look for externally_visible and used attributes and mark cgraph nodes |
831 | accordingly. | |
832 | ||
833 | We cannot mark the nodes at the point the attributes are processed (in | |
834 | handle_*_attribute) because the copy of the declarations available at that | |
835 | point may not be canonical. For example, in: | |
836 | ||
837 | void f(); | |
838 | void f() __attribute__((used)); | |
839 | ||
840 | the declaration we see in handle_used_attribute will be the second | |
841 | declaration -- but the front end will subsequently merge that declaration | |
842 | with the original declaration and discard the second declaration. | |
843 | ||
844 | Furthermore, we can't mark these nodes in cgraph_finalize_function because: | |
845 | ||
846 | void f() {} | |
847 | void f() __attribute__((externally_visible)); | |
848 | ||
849 | is valid. | |
850 | ||
851 | So, we walk the nodes at the end of the translation unit, applying the | |
852 | attributes at that point. */ | |
853 | ||
854 | static void | |
855 | process_function_and_variable_attributes (struct cgraph_node *first, | |
1d416bd7 | 856 | struct varpool_node *first_var) |
05806473 | 857 | { |
858 | struct cgraph_node *node; | |
1d416bd7 | 859 | struct varpool_node *vnode; |
05806473 | 860 | |
861 | for (node = cgraph_nodes; node != first; node = node->next) | |
862 | { | |
863 | tree decl = node->decl; | |
83a23b05 | 864 | if (DECL_PRESERVE_P (decl)) |
0b49f8f8 | 865 | cgraph_mark_needed_node (node); |
62433d51 | 866 | if (TARGET_DLLIMPORT_DECL_ATTRIBUTES |
867 | && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)) | |
868 | && TREE_PUBLIC (node->decl)) | |
869 | { | |
870 | if (node->local.finalized) | |
871 | cgraph_mark_needed_node (node); | |
872 | } | |
873 | else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl))) | |
05806473 | 874 | { |
ba12ea31 | 875 | if (! TREE_PUBLIC (node->decl)) |
712d2297 | 876 | warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes, |
877 | "%<externally_visible%>" | |
878 | " attribute have effect only on public objects"); | |
59dd4830 | 879 | else if (node->local.finalized) |
880 | cgraph_mark_needed_node (node); | |
05806473 | 881 | } |
40b32d93 | 882 | if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)) |
883 | && node->local.finalized) | |
884 | { | |
885 | warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes, | |
886 | "%<weakref%> attribute ignored" | |
887 | " because function is defined"); | |
888 | DECL_WEAK (decl) = 0; | |
889 | DECL_ATTRIBUTES (decl) = remove_attribute ("weakref", | |
890 | DECL_ATTRIBUTES (decl)); | |
891 | } | |
d05db70d | 892 | process_common_attributes (decl); |
05806473 | 893 | } |
1d416bd7 | 894 | for (vnode = varpool_nodes; vnode != first_var; vnode = vnode->next) |
05806473 | 895 | { |
896 | tree decl = vnode->decl; | |
83a23b05 | 897 | if (DECL_PRESERVE_P (decl)) |
05806473 | 898 | { |
22671757 | 899 | vnode->force_output = true; |
05806473 | 900 | if (vnode->finalized) |
1d416bd7 | 901 | varpool_mark_needed_node (vnode); |
05806473 | 902 | } |
62433d51 | 903 | if (TARGET_DLLIMPORT_DECL_ATTRIBUTES |
904 | && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)) | |
0d9d5d69 | 905 | && TREE_PUBLIC (vnode->decl)) |
62433d51 | 906 | { |
907 | if (vnode->finalized) | |
908 | varpool_mark_needed_node (vnode); | |
909 | } | |
910 | else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl))) | |
05806473 | 911 | { |
ba12ea31 | 912 | if (! TREE_PUBLIC (vnode->decl)) |
712d2297 | 913 | warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes, |
914 | "%<externally_visible%>" | |
915 | " attribute have effect only on public objects"); | |
59dd4830 | 916 | else if (vnode->finalized) |
917 | varpool_mark_needed_node (vnode); | |
05806473 | 918 | } |
40b32d93 | 919 | if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)) |
920 | && vnode->finalized | |
921 | && DECL_INITIAL (decl)) | |
922 | { | |
923 | warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes, | |
924 | "%<weakref%> attribute ignored" | |
925 | " because variable is initialized"); | |
926 | DECL_WEAK (decl) = 0; | |
927 | DECL_ATTRIBUTES (decl) = remove_attribute ("weakref", | |
928 | DECL_ATTRIBUTES (decl)); | |
929 | } | |
d05db70d | 930 | process_common_attributes (decl); |
05806473 | 931 | } |
932 | } | |
933 | ||
aeeb194b | 934 | /* Process CGRAPH_NODES_NEEDED queue, analyze each function (and transitively |
935 | each reachable functions) and build cgraph. | |
936 | The function can be called multiple times after inserting new nodes | |
0d424440 | 937 | into beginning of queue. Just the new part of queue is re-scanned then. */ |
ae01b312 | 938 | |
aeeb194b | 939 | static void |
940 | cgraph_analyze_functions (void) | |
ae01b312 | 941 | { |
c1dcd13c | 942 | /* Keep track of already processed nodes when called multiple times for |
06b27565 | 943 | intermodule optimization. */ |
c1dcd13c | 944 | static struct cgraph_node *first_analyzed; |
c17d0de1 | 945 | struct cgraph_node *first_processed = first_analyzed; |
1d416bd7 | 946 | static struct varpool_node *first_analyzed_var; |
aeeb194b | 947 | struct cgraph_node *node, *next; |
ae01b312 | 948 | |
f1c35659 | 949 | bitmap_obstack_initialize (NULL); |
c17d0de1 | 950 | process_function_and_variable_attributes (first_processed, |
951 | first_analyzed_var); | |
952 | first_processed = cgraph_nodes; | |
1d416bd7 | 953 | first_analyzed_var = varpool_nodes; |
954 | varpool_analyze_pending_decls (); | |
f79b6507 | 955 | if (cgraph_dump_file) |
ae01b312 | 956 | { |
e4200070 | 957 | fprintf (cgraph_dump_file, "Initial entry points:"); |
c1dcd13c | 958 | for (node = cgraph_nodes; node != first_analyzed; node = node->next) |
1a1a827a | 959 | if (node->needed) |
f79b6507 | 960 | fprintf (cgraph_dump_file, " %s", cgraph_node_name (node)); |
961 | fprintf (cgraph_dump_file, "\n"); | |
ae01b312 | 962 | } |
aeeb194b | 963 | cgraph_process_new_functions (); |
ae01b312 | 964 | |
e6d2b2d8 | 965 | /* Propagate reachability flag and lower representation of all reachable |
966 | functions. In the future, lowering will introduce new functions and | |
967 | new entry points on the way (by template instantiation and virtual | |
968 | method table generation for instance). */ | |
3d7bfc56 | 969 | while (cgraph_nodes_queue) |
ae01b312 | 970 | { |
0785e435 | 971 | struct cgraph_edge *edge; |
3d7bfc56 | 972 | tree decl = cgraph_nodes_queue->decl; |
973 | ||
974 | node = cgraph_nodes_queue; | |
d87976fb | 975 | cgraph_nodes_queue = cgraph_nodes_queue->next_needed; |
b0cdf642 | 976 | node->next_needed = NULL; |
ae01b312 | 977 | |
638531ad | 978 | /* ??? It is possible to create extern inline function and later using |
bbd5cba2 | 979 | weak alias attribute to kill its body. See |
638531ad | 980 | gcc.c-torture/compile/20011119-1.c */ |
91bf9d9a | 981 | if (!DECL_STRUCT_FUNCTION (decl) |
982 | && !node->thunk.thunk_p) | |
9b8fb23a | 983 | { |
984 | cgraph_reset_node (node); | |
443089c1 | 985 | node->local.redefined_extern_inline = true; |
9b8fb23a | 986 | continue; |
987 | } | |
638531ad | 988 | |
7bfefa9d | 989 | if (!node->analyzed) |
990 | cgraph_analyze_function (node); | |
2c0b522d | 991 | |
ae01b312 | 992 | for (edge = node->callees; edge; edge = edge->next_callee) |
0785e435 | 993 | if (!edge->callee->reachable) |
2c0b522d | 994 | cgraph_mark_reachable_node (edge->callee); |
91bf9d9a | 995 | for (edge = node->callers; edge; edge = edge->next_caller) |
996 | if (!edge->caller->reachable && edge->caller->thunk.thunk_p) | |
997 | cgraph_mark_reachable_node (edge->caller); | |
2c0b522d | 998 | |
61c2c7b1 | 999 | if (node->same_comdat_group) |
1000 | { | |
1001 | for (next = node->same_comdat_group; | |
1002 | next != node; | |
1003 | next = next->same_comdat_group) | |
1004 | cgraph_mark_reachable_node (next); | |
1005 | } | |
1006 | ||
d544ceff | 1007 | /* If decl is a clone of an abstract function, mark that abstract |
1008 | function so that we don't release its body. The DECL_INITIAL() of that | |
fd6a3c41 | 1009 | abstract function declaration will be later needed to output debug |
1010 | info. */ | |
d544ceff | 1011 | if (DECL_ABSTRACT_ORIGIN (decl)) |
1012 | { | |
fd6a3c41 | 1013 | struct cgraph_node *origin_node; |
1014 | origin_node = cgraph_get_node (DECL_ABSTRACT_ORIGIN (decl)); | |
d544ceff | 1015 | origin_node->abstract_and_needed = true; |
1016 | } | |
1017 | ||
c17d0de1 | 1018 | /* We finalize local static variables during constructing callgraph |
1019 | edges. Process their attributes too. */ | |
1020 | process_function_and_variable_attributes (first_processed, | |
1021 | first_analyzed_var); | |
1022 | first_processed = cgraph_nodes; | |
1d416bd7 | 1023 | first_analyzed_var = varpool_nodes; |
1024 | varpool_analyze_pending_decls (); | |
aeeb194b | 1025 | cgraph_process_new_functions (); |
ae01b312 | 1026 | } |
2c0b522d | 1027 | |
aa5e06c7 | 1028 | /* Collect entry points to the unit. */ |
f79b6507 | 1029 | if (cgraph_dump_file) |
3d7bfc56 | 1030 | { |
e4200070 | 1031 | fprintf (cgraph_dump_file, "Unit entry points:"); |
c1dcd13c | 1032 | for (node = cgraph_nodes; node != first_analyzed; node = node->next) |
1a1a827a | 1033 | if (node->needed) |
f79b6507 | 1034 | fprintf (cgraph_dump_file, " %s", cgraph_node_name (node)); |
e4200070 | 1035 | fprintf (cgraph_dump_file, "\n\nInitial "); |
0785e435 | 1036 | dump_cgraph (cgraph_dump_file); |
7410370b | 1037 | dump_varpool (cgraph_dump_file); |
3d7bfc56 | 1038 | } |
e6d2b2d8 | 1039 | |
f79b6507 | 1040 | if (cgraph_dump_file) |
1041 | fprintf (cgraph_dump_file, "\nReclaiming functions:"); | |
ae01b312 | 1042 | |
f4ec5ce1 | 1043 | for (node = cgraph_nodes; node != first_analyzed; node = next) |
ae01b312 | 1044 | { |
1045 | tree decl = node->decl; | |
f4ec5ce1 | 1046 | next = node->next; |
ae01b312 | 1047 | |
91bf9d9a | 1048 | if (node->local.finalized && !gimple_has_body_p (decl) |
1049 | && !node->thunk.thunk_p) | |
a0c938f0 | 1050 | cgraph_reset_node (node); |
9b8fb23a | 1051 | |
91bf9d9a | 1052 | if (!node->reachable |
1053 | && (gimple_has_body_p (decl) || node->thunk.thunk_p)) | |
ae01b312 | 1054 | { |
f79b6507 | 1055 | if (cgraph_dump_file) |
1056 | fprintf (cgraph_dump_file, " %s", cgraph_node_name (node)); | |
b0cdf642 | 1057 | cgraph_remove_node (node); |
9b8fb23a | 1058 | continue; |
ae01b312 | 1059 | } |
bc5cab3b | 1060 | else |
1061 | node->next_needed = NULL; | |
91bf9d9a | 1062 | gcc_assert (!node->local.finalized || node->thunk.thunk_p |
1063 | || gimple_has_body_p (decl)); | |
9b8fb23a | 1064 | gcc_assert (node->analyzed == node->local.finalized); |
ae01b312 | 1065 | } |
f79b6507 | 1066 | if (cgraph_dump_file) |
e4200070 | 1067 | { |
1068 | fprintf (cgraph_dump_file, "\n\nReclaimed "); | |
1069 | dump_cgraph (cgraph_dump_file); | |
7410370b | 1070 | dump_varpool (cgraph_dump_file); |
e4200070 | 1071 | } |
f1c35659 | 1072 | bitmap_obstack_release (NULL); |
c1dcd13c | 1073 | first_analyzed = cgraph_nodes; |
ae01b312 | 1074 | ggc_collect (); |
aeeb194b | 1075 | } |
1076 | ||
8f69fd82 | 1077 | |
aeeb194b | 1078 | /* Analyze the whole compilation unit once it is parsed completely. */ |
1079 | ||
1080 | void | |
1081 | cgraph_finalize_compilation_unit (void) | |
1082 | { | |
9929334e | 1083 | timevar_push (TV_CGRAPH); |
1084 | ||
a0605d65 | 1085 | /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */ |
1086 | if (flag_lto) | |
1087 | lto_streamer_hooks_init (); | |
1088 | ||
bb903e9c | 1089 | /* If we're here there's no current function anymore. Some frontends |
1090 | are lazy in clearing these. */ | |
1091 | current_function_decl = NULL; | |
1092 | set_cfun (NULL); | |
1093 | ||
bfec3452 | 1094 | /* Do not skip analyzing the functions if there were errors, we |
1095 | miss diagnostics for following functions otherwise. */ | |
aeeb194b | 1096 | |
8f69fd82 | 1097 | /* Emit size functions we didn't inline. */ |
4189e677 | 1098 | finalize_size_functions (); |
8f69fd82 | 1099 | |
9929334e | 1100 | /* Mark alias targets necessary and emit diagnostics. */ |
1101 | finish_aliases_1 (); | |
1102 | ||
aeeb194b | 1103 | if (!quiet_flag) |
1104 | { | |
1105 | fprintf (stderr, "\nAnalyzing compilation unit\n"); | |
1106 | fflush (stderr); | |
1107 | } | |
1108 | ||
ec4791a8 | 1109 | if (flag_dump_passes) |
1110 | dump_passes (); | |
1111 | ||
9929334e | 1112 | /* Gimplify and lower all functions, compute reachability and |
1113 | remove unreachable nodes. */ | |
1114 | cgraph_analyze_functions (); | |
1115 | ||
8f69fd82 | 1116 | /* Mark alias targets necessary and emit diagnostics. */ |
1117 | finish_aliases_1 (); | |
1118 | ||
9929334e | 1119 | /* Gimplify and lower thunks. */ |
aeeb194b | 1120 | cgraph_analyze_functions (); |
bfec3452 | 1121 | |
9929334e | 1122 | /* Finally drive the pass manager. */ |
bfec3452 | 1123 | cgraph_optimize (); |
9929334e | 1124 | |
1125 | timevar_pop (TV_CGRAPH); | |
ae01b312 | 1126 | } |
9ed5b1f5 | 1127 | |
1128 | ||
ae01b312 | 1129 | /* Figure out what functions we want to assemble. */ |
1130 | ||
1131 | static void | |
d9d9733a | 1132 | cgraph_mark_functions_to_output (void) |
ae01b312 | 1133 | { |
1134 | struct cgraph_node *node; | |
61c2c7b1 | 1135 | #ifdef ENABLE_CHECKING |
1136 | bool check_same_comdat_groups = false; | |
1137 | ||
1138 | for (node = cgraph_nodes; node; node = node->next) | |
1139 | gcc_assert (!node->process); | |
1140 | #endif | |
ae01b312 | 1141 | |
ae01b312 | 1142 | for (node = cgraph_nodes; node; node = node->next) |
1143 | { | |
1144 | tree decl = node->decl; | |
d7c6d889 | 1145 | struct cgraph_edge *e; |
a0c938f0 | 1146 | |
61c2c7b1 | 1147 | gcc_assert (!node->process || node->same_comdat_group); |
1148 | if (node->process) | |
1149 | continue; | |
d7c6d889 | 1150 | |
1151 | for (e = node->callers; e; e = e->next_caller) | |
611e5405 | 1152 | if (e->inline_failed) |
d7c6d889 | 1153 | break; |
ae01b312 | 1154 | |
e6d2b2d8 | 1155 | /* We need to output all local functions that are used and not |
1156 | always inlined, as well as those that are reachable from | |
1157 | outside the current compilation unit. */ | |
1a1a827a | 1158 | if (node->analyzed |
91bf9d9a | 1159 | && !node->thunk.thunk_p |
b0cdf642 | 1160 | && !node->global.inlined_to |
1e3aebec | 1161 | && (!cgraph_only_called_directly_p (node) |
d7c6d889 | 1162 | || (e && node->reachable)) |
4ee9c684 | 1163 | && !TREE_ASM_WRITTEN (decl) |
ae01b312 | 1164 | && !DECL_EXTERNAL (decl)) |
61c2c7b1 | 1165 | { |
1166 | node->process = 1; | |
1167 | if (node->same_comdat_group) | |
1168 | { | |
1169 | struct cgraph_node *next; | |
1170 | for (next = node->same_comdat_group; | |
1171 | next != node; | |
1172 | next = next->same_comdat_group) | |
91bf9d9a | 1173 | if (!next->thunk.thunk_p) |
1174 | next->process = 1; | |
61c2c7b1 | 1175 | } |
1176 | } | |
1177 | else if (node->same_comdat_group) | |
1178 | { | |
1179 | #ifdef ENABLE_CHECKING | |
1180 | check_same_comdat_groups = true; | |
1181 | #endif | |
1182 | } | |
cc636d56 | 1183 | else |
9cee7c3f | 1184 | { |
1185 | /* We should've reclaimed all functions that are not needed. */ | |
1186 | #ifdef ENABLE_CHECKING | |
75a70cf9 | 1187 | if (!node->global.inlined_to |
1a1a827a | 1188 | && gimple_has_body_p (decl) |
08843223 | 1189 | /* FIXME: in ltrans unit when offline copy is outside partition but inline copies |
1190 | are inside partition, we can end up not removing the body since we no longer | |
1191 | have analyzed node pointing to it. */ | |
1192 | && !node->in_other_partition | |
9cee7c3f | 1193 | && !DECL_EXTERNAL (decl)) |
1194 | { | |
1195 | dump_cgraph_node (stderr, node); | |
1196 | internal_error ("failed to reclaim unneeded function"); | |
1197 | } | |
1198 | #endif | |
75a70cf9 | 1199 | gcc_assert (node->global.inlined_to |
1a1a827a | 1200 | || !gimple_has_body_p (decl) |
08843223 | 1201 | || node->in_other_partition |
9cee7c3f | 1202 | || DECL_EXTERNAL (decl)); |
1203 | ||
1204 | } | |
a0c938f0 | 1205 | |
961e3b13 | 1206 | } |
61c2c7b1 | 1207 | #ifdef ENABLE_CHECKING |
1208 | if (check_same_comdat_groups) | |
1209 | for (node = cgraph_nodes; node; node = node->next) | |
1210 | if (node->same_comdat_group && !node->process) | |
1211 | { | |
1212 | tree decl = node->decl; | |
1213 | if (!node->global.inlined_to | |
1214 | && gimple_has_body_p (decl) | |
08843223 | 1215 | /* FIXME: in ltrans unit when offline copy is outside partition but inline copies |
1216 | are inside partition, we can end up not removing the body since we no longer | |
1217 | have analyzed node pointing to it. */ | |
1218 | && !node->in_other_partition | |
61c2c7b1 | 1219 | && !DECL_EXTERNAL (decl)) |
1220 | { | |
1221 | dump_cgraph_node (stderr, node); | |
1222 | internal_error ("failed to reclaim unneeded function"); | |
1223 | } | |
1224 | } | |
1225 | #endif | |
961e3b13 | 1226 | } |
1227 | ||
28454517 | 1228 | /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function |
1229 | in lowered gimple form. | |
1230 | ||
1231 | Set current_function_decl and cfun to newly constructed empty function body. | |
1232 | return basic block in the function body. */ | |
1233 | ||
1234 | static basic_block | |
1235 | init_lowered_empty_function (tree decl) | |
1236 | { | |
1237 | basic_block bb; | |
1238 | ||
1239 | current_function_decl = decl; | |
1240 | allocate_struct_function (decl, false); | |
1241 | gimple_register_cfg_hooks (); | |
1242 | init_empty_tree_cfg (); | |
1243 | init_tree_ssa (cfun); | |
1244 | init_ssa_operands (); | |
1245 | cfun->gimple_df->in_ssa_p = true; | |
1246 | DECL_INITIAL (decl) = make_node (BLOCK); | |
1247 | ||
1248 | DECL_SAVED_TREE (decl) = error_mark_node; | |
1249 | cfun->curr_properties |= | |
1250 | (PROP_gimple_lcf | PROP_gimple_leh | PROP_cfg | PROP_referenced_vars | | |
1251 | PROP_ssa); | |
1252 | ||
1253 | /* Create BB for body of the function and connect it properly. */ | |
1254 | bb = create_basic_block (NULL, (void *) 0, ENTRY_BLOCK_PTR); | |
1255 | make_edge (ENTRY_BLOCK_PTR, bb, 0); | |
1256 | make_edge (bb, EXIT_BLOCK_PTR, 0); | |
1257 | ||
1258 | return bb; | |
1259 | } | |
1260 | ||
1261 | /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable | |
1262 | offset indicated by VIRTUAL_OFFSET, if that is | |
1263 | non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and | |
1264 | zero for a result adjusting thunk. */ | |
1265 | ||
1266 | static tree | |
1267 | thunk_adjust (gimple_stmt_iterator * bsi, | |
1268 | tree ptr, bool this_adjusting, | |
1269 | HOST_WIDE_INT fixed_offset, tree virtual_offset) | |
1270 | { | |
1271 | gimple stmt; | |
1272 | tree ret; | |
1273 | ||
55d6cb23 | 1274 | if (this_adjusting |
1275 | && fixed_offset != 0) | |
28454517 | 1276 | { |
1277 | stmt = gimple_build_assign (ptr, | |
1278 | fold_build2_loc (input_location, | |
1279 | POINTER_PLUS_EXPR, | |
1280 | TREE_TYPE (ptr), ptr, | |
1281 | size_int (fixed_offset))); | |
1282 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); | |
1283 | } | |
1284 | ||
1285 | /* If there's a virtual offset, look up that value in the vtable and | |
1286 | adjust the pointer again. */ | |
1287 | if (virtual_offset) | |
1288 | { | |
1289 | tree vtabletmp; | |
1290 | tree vtabletmp2; | |
1291 | tree vtabletmp3; | |
1292 | tree offsettmp; | |
1293 | ||
1294 | if (!vtable_entry_type) | |
1295 | { | |
1296 | tree vfunc_type = make_node (FUNCTION_TYPE); | |
1297 | TREE_TYPE (vfunc_type) = integer_type_node; | |
1298 | TYPE_ARG_TYPES (vfunc_type) = NULL_TREE; | |
1299 | layout_type (vfunc_type); | |
1300 | ||
1301 | vtable_entry_type = build_pointer_type (vfunc_type); | |
1302 | } | |
1303 | ||
1304 | vtabletmp = | |
1305 | create_tmp_var (build_pointer_type | |
1306 | (build_pointer_type (vtable_entry_type)), "vptr"); | |
1307 | ||
1308 | /* The vptr is always at offset zero in the object. */ | |
1309 | stmt = gimple_build_assign (vtabletmp, | |
1310 | build1 (NOP_EXPR, TREE_TYPE (vtabletmp), | |
1311 | ptr)); | |
1312 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); | |
1313 | mark_symbols_for_renaming (stmt); | |
1314 | find_referenced_vars_in (stmt); | |
1315 | ||
1316 | /* Form the vtable address. */ | |
1317 | vtabletmp2 = create_tmp_var (TREE_TYPE (TREE_TYPE (vtabletmp)), | |
1318 | "vtableaddr"); | |
1319 | stmt = gimple_build_assign (vtabletmp2, | |
182cf5a9 | 1320 | build_simple_mem_ref (vtabletmp)); |
28454517 | 1321 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); |
1322 | mark_symbols_for_renaming (stmt); | |
1323 | find_referenced_vars_in (stmt); | |
1324 | ||
1325 | /* Find the entry with the vcall offset. */ | |
1326 | stmt = gimple_build_assign (vtabletmp2, | |
1327 | fold_build2_loc (input_location, | |
1328 | POINTER_PLUS_EXPR, | |
1329 | TREE_TYPE (vtabletmp2), | |
1330 | vtabletmp2, | |
1331 | fold_convert (sizetype, | |
1332 | virtual_offset))); | |
1333 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); | |
1334 | ||
1335 | /* Get the offset itself. */ | |
1336 | vtabletmp3 = create_tmp_var (TREE_TYPE (TREE_TYPE (vtabletmp2)), | |
1337 | "vcalloffset"); | |
1338 | stmt = gimple_build_assign (vtabletmp3, | |
182cf5a9 | 1339 | build_simple_mem_ref (vtabletmp2)); |
28454517 | 1340 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); |
1341 | mark_symbols_for_renaming (stmt); | |
1342 | find_referenced_vars_in (stmt); | |
1343 | ||
1344 | /* Cast to sizetype. */ | |
1345 | offsettmp = create_tmp_var (sizetype, "offset"); | |
1346 | stmt = gimple_build_assign (offsettmp, fold_convert (sizetype, vtabletmp3)); | |
1347 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); | |
1348 | mark_symbols_for_renaming (stmt); | |
1349 | find_referenced_vars_in (stmt); | |
1350 | ||
1351 | /* Adjust the `this' pointer. */ | |
1352 | ptr = fold_build2_loc (input_location, | |
1353 | POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr, | |
1354 | offsettmp); | |
1355 | } | |
1356 | ||
55d6cb23 | 1357 | if (!this_adjusting |
1358 | && fixed_offset != 0) | |
28454517 | 1359 | /* Adjust the pointer by the constant. */ |
1360 | { | |
1361 | tree ptrtmp; | |
1362 | ||
1363 | if (TREE_CODE (ptr) == VAR_DECL) | |
1364 | ptrtmp = ptr; | |
1365 | else | |
1366 | { | |
1367 | ptrtmp = create_tmp_var (TREE_TYPE (ptr), "ptr"); | |
1368 | stmt = gimple_build_assign (ptrtmp, ptr); | |
1369 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); | |
1370 | mark_symbols_for_renaming (stmt); | |
1371 | find_referenced_vars_in (stmt); | |
1372 | } | |
1373 | ptr = fold_build2_loc (input_location, | |
1374 | POINTER_PLUS_EXPR, TREE_TYPE (ptrtmp), ptrtmp, | |
1375 | size_int (fixed_offset)); | |
1376 | } | |
1377 | ||
1378 | /* Emit the statement and gimplify the adjustment expression. */ | |
1379 | ret = create_tmp_var (TREE_TYPE (ptr), "adjusted_this"); | |
1380 | stmt = gimple_build_assign (ret, ptr); | |
1381 | mark_symbols_for_renaming (stmt); | |
1382 | find_referenced_vars_in (stmt); | |
1383 | gsi_insert_after (bsi, stmt, GSI_NEW_STMT); | |
1384 | ||
1385 | return ret; | |
1386 | } | |
1387 | ||
1388 | /* Produce assembler for thunk NODE. */ | |
1389 | ||
1390 | static void | |
1391 | assemble_thunk (struct cgraph_node *node) | |
1392 | { | |
1393 | bool this_adjusting = node->thunk.this_adjusting; | |
1394 | HOST_WIDE_INT fixed_offset = node->thunk.fixed_offset; | |
1395 | HOST_WIDE_INT virtual_value = node->thunk.virtual_value; | |
1396 | tree virtual_offset = NULL; | |
1397 | tree alias = node->thunk.alias; | |
1398 | tree thunk_fndecl = node->decl; | |
1399 | tree a = DECL_ARGUMENTS (thunk_fndecl); | |
1400 | ||
1401 | current_function_decl = thunk_fndecl; | |
1402 | ||
aed6e608 | 1403 | /* Ensure thunks are emitted in their correct sections. */ |
1404 | resolve_unique_section (thunk_fndecl, 0, flag_function_sections); | |
1405 | ||
28454517 | 1406 | if (this_adjusting |
1407 | && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset, | |
1408 | virtual_value, alias)) | |
1409 | { | |
1410 | const char *fnname; | |
1411 | tree fn_block; | |
1412 | ||
1413 | DECL_RESULT (thunk_fndecl) | |
1414 | = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl), | |
1415 | RESULT_DECL, 0, integer_type_node); | |
22ea3b47 | 1416 | fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl)); |
28454517 | 1417 | |
1418 | /* The back end expects DECL_INITIAL to contain a BLOCK, so we | |
1419 | create one. */ | |
1420 | fn_block = make_node (BLOCK); | |
1421 | BLOCK_VARS (fn_block) = a; | |
1422 | DECL_INITIAL (thunk_fndecl) = fn_block; | |
1423 | init_function_start (thunk_fndecl); | |
1424 | cfun->is_thunk = 1; | |
1425 | assemble_start_function (thunk_fndecl, fnname); | |
1426 | ||
1427 | targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl, | |
1428 | fixed_offset, virtual_value, alias); | |
1429 | ||
1430 | assemble_end_function (thunk_fndecl, fnname); | |
1431 | init_insn_lengths (); | |
1432 | free_after_compilation (cfun); | |
1433 | set_cfun (NULL); | |
1434 | TREE_ASM_WRITTEN (thunk_fndecl) = 1; | |
91bf9d9a | 1435 | node->thunk.thunk_p = false; |
1436 | node->analyzed = false; | |
28454517 | 1437 | } |
1438 | else | |
1439 | { | |
1440 | tree restype; | |
1441 | basic_block bb, then_bb, else_bb, return_bb; | |
1442 | gimple_stmt_iterator bsi; | |
1443 | int nargs = 0; | |
1444 | tree arg; | |
1445 | int i; | |
1446 | tree resdecl; | |
1447 | tree restmp = NULL; | |
1448 | VEC(tree, heap) *vargs; | |
1449 | ||
1450 | gimple call; | |
1451 | gimple ret; | |
1452 | ||
1453 | DECL_IGNORED_P (thunk_fndecl) = 1; | |
1454 | bitmap_obstack_initialize (NULL); | |
1455 | ||
1456 | if (node->thunk.virtual_offset_p) | |
1457 | virtual_offset = size_int (virtual_value); | |
1458 | ||
1459 | /* Build the return declaration for the function. */ | |
1460 | restype = TREE_TYPE (TREE_TYPE (thunk_fndecl)); | |
1461 | if (DECL_RESULT (thunk_fndecl) == NULL_TREE) | |
1462 | { | |
1463 | resdecl = build_decl (input_location, RESULT_DECL, 0, restype); | |
1464 | DECL_ARTIFICIAL (resdecl) = 1; | |
1465 | DECL_IGNORED_P (resdecl) = 1; | |
1466 | DECL_RESULT (thunk_fndecl) = resdecl; | |
1467 | } | |
1468 | else | |
1469 | resdecl = DECL_RESULT (thunk_fndecl); | |
1470 | ||
1471 | bb = then_bb = else_bb = return_bb = init_lowered_empty_function (thunk_fndecl); | |
1472 | ||
1473 | bsi = gsi_start_bb (bb); | |
1474 | ||
1475 | /* Build call to the function being thunked. */ | |
1476 | if (!VOID_TYPE_P (restype)) | |
1477 | { | |
1478 | if (!is_gimple_reg_type (restype)) | |
1479 | { | |
1480 | restmp = resdecl; | |
2ab2ce89 | 1481 | add_local_decl (cfun, restmp); |
28454517 | 1482 | BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp; |
1483 | } | |
1484 | else | |
1485 | restmp = create_tmp_var_raw (restype, "retval"); | |
1486 | } | |
1487 | ||
1767a056 | 1488 | for (arg = a; arg; arg = DECL_CHAIN (arg)) |
28454517 | 1489 | nargs++; |
1490 | vargs = VEC_alloc (tree, heap, nargs); | |
1491 | if (this_adjusting) | |
1492 | VEC_quick_push (tree, vargs, | |
1493 | thunk_adjust (&bsi, | |
1494 | a, 1, fixed_offset, | |
1495 | virtual_offset)); | |
1496 | else | |
1497 | VEC_quick_push (tree, vargs, a); | |
1767a056 | 1498 | for (i = 1, arg = DECL_CHAIN (a); i < nargs; i++, arg = DECL_CHAIN (arg)) |
28454517 | 1499 | VEC_quick_push (tree, vargs, arg); |
1500 | call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs); | |
1501 | VEC_free (tree, heap, vargs); | |
1502 | gimple_call_set_cannot_inline (call, true); | |
1503 | gimple_call_set_from_thunk (call, true); | |
1504 | if (restmp) | |
1505 | gimple_call_set_lhs (call, restmp); | |
1506 | gsi_insert_after (&bsi, call, GSI_NEW_STMT); | |
1507 | mark_symbols_for_renaming (call); | |
1508 | find_referenced_vars_in (call); | |
1509 | update_stmt (call); | |
1510 | ||
1511 | if (restmp && !this_adjusting) | |
1512 | { | |
57ab8ec3 | 1513 | tree true_label = NULL_TREE; |
28454517 | 1514 | |
1515 | if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE) | |
1516 | { | |
1517 | gimple stmt; | |
1518 | /* If the return type is a pointer, we need to | |
1519 | protect against NULL. We know there will be an | |
1520 | adjustment, because that's why we're emitting a | |
1521 | thunk. */ | |
1522 | then_bb = create_basic_block (NULL, (void *) 0, bb); | |
1523 | return_bb = create_basic_block (NULL, (void *) 0, then_bb); | |
1524 | else_bb = create_basic_block (NULL, (void *) 0, else_bb); | |
1525 | remove_edge (single_succ_edge (bb)); | |
1526 | true_label = gimple_block_label (then_bb); | |
28454517 | 1527 | stmt = gimple_build_cond (NE_EXPR, restmp, |
385f3f36 | 1528 | build_zero_cst (TREE_TYPE (restmp)), |
28454517 | 1529 | NULL_TREE, NULL_TREE); |
1530 | gsi_insert_after (&bsi, stmt, GSI_NEW_STMT); | |
1531 | make_edge (bb, then_bb, EDGE_TRUE_VALUE); | |
1532 | make_edge (bb, else_bb, EDGE_FALSE_VALUE); | |
1533 | make_edge (return_bb, EXIT_BLOCK_PTR, 0); | |
1534 | make_edge (then_bb, return_bb, EDGE_FALLTHRU); | |
1535 | make_edge (else_bb, return_bb, EDGE_FALLTHRU); | |
1536 | bsi = gsi_last_bb (then_bb); | |
1537 | } | |
1538 | ||
1539 | restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0, | |
1540 | fixed_offset, virtual_offset); | |
1541 | if (true_label) | |
1542 | { | |
1543 | gimple stmt; | |
1544 | bsi = gsi_last_bb (else_bb); | |
385f3f36 | 1545 | stmt = gimple_build_assign (restmp, |
1546 | build_zero_cst (TREE_TYPE (restmp))); | |
28454517 | 1547 | gsi_insert_after (&bsi, stmt, GSI_NEW_STMT); |
1548 | bsi = gsi_last_bb (return_bb); | |
1549 | } | |
1550 | } | |
1551 | else | |
1552 | gimple_call_set_tail (call, true); | |
1553 | ||
1554 | /* Build return value. */ | |
1555 | ret = gimple_build_return (restmp); | |
1556 | gsi_insert_after (&bsi, ret, GSI_NEW_STMT); | |
1557 | ||
1558 | delete_unreachable_blocks (); | |
1559 | update_ssa (TODO_update_ssa); | |
1560 | ||
28454517 | 1561 | /* Since we want to emit the thunk, we explicitly mark its name as |
1562 | referenced. */ | |
91bf9d9a | 1563 | node->thunk.thunk_p = false; |
1564 | cgraph_node_remove_callees (node); | |
28454517 | 1565 | cgraph_add_new_function (thunk_fndecl, true); |
1566 | bitmap_obstack_release (NULL); | |
1567 | } | |
1568 | current_function_decl = NULL; | |
1569 | } | |
1570 | ||
91bf9d9a | 1571 | |
1572 | /* Assemble thunks asociated to NODE. */ | |
1573 | ||
1574 | static void | |
1575 | assemble_thunks (struct cgraph_node *node) | |
1576 | { | |
1577 | struct cgraph_edge *e; | |
1578 | for (e = node->callers; e;) | |
1579 | if (e->caller->thunk.thunk_p) | |
1580 | { | |
1581 | struct cgraph_node *thunk = e->caller; | |
1582 | ||
1583 | e = e->next_caller; | |
1584 | assemble_thunks (thunk); | |
1585 | assemble_thunk (thunk); | |
1586 | } | |
1587 | else | |
1588 | e = e->next_caller; | |
1589 | } | |
1590 | ||
ae01b312 | 1591 | /* Expand function specified by NODE. */ |
e6d2b2d8 | 1592 | |
ae01b312 | 1593 | static void |
d9d9733a | 1594 | cgraph_expand_function (struct cgraph_node *node) |
ae01b312 | 1595 | { |
1596 | tree decl = node->decl; | |
1597 | ||
b0cdf642 | 1598 | /* We ought to not compile any inline clones. */ |
cc636d56 | 1599 | gcc_assert (!node->global.inlined_to); |
b0cdf642 | 1600 | |
6329636b | 1601 | announce_function (decl); |
09fc9532 | 1602 | node->process = 0; |
ed772161 | 1603 | if (node->same_body) |
1604 | { | |
28454517 | 1605 | struct cgraph_node *alias, *next; |
ed772161 | 1606 | bool saved_alias = node->alias; |
28454517 | 1607 | for (alias = node->same_body; |
1608 | alias && alias->next; alias = alias->next) | |
1609 | ; | |
1610 | /* Walk aliases in the order they were created; it is possible that | |
0a10fd82 | 1611 | thunks refers to the aliases made earlier. */ |
28454517 | 1612 | for (; alias; alias = next) |
1613 | { | |
1614 | next = alias->previous; | |
1615 | if (!alias->thunk.thunk_p) | |
1616 | assemble_alias (alias->decl, | |
1617 | DECL_ASSEMBLER_NAME (alias->thunk.alias)); | |
28454517 | 1618 | } |
ed772161 | 1619 | node->alias = saved_alias; |
f7777314 | 1620 | cgraph_process_new_functions (); |
ed772161 | 1621 | } |
f7777314 | 1622 | |
91bf9d9a | 1623 | assemble_thunks (node); |
f7777314 | 1624 | gcc_assert (node->lowered); |
1625 | ||
1626 | /* Generate RTL for the body of DECL. */ | |
1627 | tree_rest_of_compilation (decl); | |
1628 | ||
1629 | /* Make sure that BE didn't give up on compiling. */ | |
1630 | gcc_assert (TREE_ASM_WRITTEN (decl)); | |
1631 | current_function_decl = NULL; | |
cc91b414 | 1632 | gcc_assert (!cgraph_preserve_function_body_p (node)); |
1a1a827a | 1633 | cgraph_release_function_body (node); |
1634 | /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer | |
1635 | points to the dead function body. */ | |
1636 | cgraph_node_remove_callees (node); | |
e1be32b8 | 1637 | |
1638 | cgraph_function_flags_ready = true; | |
ae01b312 | 1639 | } |
1640 | ||
b0cdf642 | 1641 | /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */ |
d7c6d889 | 1642 | |
1643 | bool | |
326a9581 | 1644 | cgraph_inline_p (struct cgraph_edge *e, cgraph_inline_failed_t *reason) |
d7c6d889 | 1645 | { |
b0cdf642 | 1646 | *reason = e->inline_failed; |
1647 | return !e->inline_failed; | |
d7c6d889 | 1648 | } |
b0cdf642 | 1649 | |
acc70efa | 1650 | |
acc70efa | 1651 | |
d9d9733a | 1652 | /* Expand all functions that must be output. |
1653 | ||
d7c6d889 | 1654 | Attempt to topologically sort the nodes so function is output when |
1655 | all called functions are already assembled to allow data to be | |
91c82c20 | 1656 | propagated across the callgraph. Use a stack to get smaller distance |
3927afe0 | 1657 | between a function and its callees (later we may choose to use a more |
d7c6d889 | 1658 | sophisticated algorithm for function reordering; we will likely want |
1659 | to use subsections to make the output functions appear in top-down | |
1660 | order). */ | |
1661 | ||
1662 | static void | |
a6868229 | 1663 | cgraph_expand_all_functions (void) |
d7c6d889 | 1664 | { |
1665 | struct cgraph_node *node; | |
4c36ffe6 | 1666 | struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes); |
c04e3894 | 1667 | int order_pos, new_order_pos = 0; |
d7c6d889 | 1668 | int i; |
1669 | ||
7771d558 | 1670 | order_pos = ipa_reverse_postorder (order); |
cc636d56 | 1671 | gcc_assert (order_pos == cgraph_n_nodes); |
d7c6d889 | 1672 | |
7bd28bba | 1673 | /* Garbage collector may remove inline clones we eliminate during |
b0cdf642 | 1674 | optimization. So we must be sure to not reference them. */ |
1675 | for (i = 0; i < order_pos; i++) | |
09fc9532 | 1676 | if (order[i]->process) |
b0cdf642 | 1677 | order[new_order_pos++] = order[i]; |
1678 | ||
1679 | for (i = new_order_pos - 1; i >= 0; i--) | |
d7c6d889 | 1680 | { |
1681 | node = order[i]; | |
09fc9532 | 1682 | if (node->process) |
d7c6d889 | 1683 | { |
cc636d56 | 1684 | gcc_assert (node->reachable); |
09fc9532 | 1685 | node->process = 0; |
d7c6d889 | 1686 | cgraph_expand_function (node); |
1687 | } | |
1688 | } | |
523c1122 | 1689 | cgraph_process_new_functions (); |
773c5ba7 | 1690 | |
d7c6d889 | 1691 | free (order); |
773c5ba7 | 1692 | |
d7c6d889 | 1693 | } |
1694 | ||
56af936e | 1695 | /* This is used to sort the node types by the cgraph order number. */ |
1696 | ||
0b09525f | 1697 | enum cgraph_order_sort_kind |
1698 | { | |
1699 | ORDER_UNDEFINED = 0, | |
1700 | ORDER_FUNCTION, | |
1701 | ORDER_VAR, | |
1702 | ORDER_ASM | |
1703 | }; | |
1704 | ||
56af936e | 1705 | struct cgraph_order_sort |
1706 | { | |
0b09525f | 1707 | enum cgraph_order_sort_kind kind; |
56af936e | 1708 | union |
1709 | { | |
1710 | struct cgraph_node *f; | |
1d416bd7 | 1711 | struct varpool_node *v; |
56af936e | 1712 | struct cgraph_asm_node *a; |
1713 | } u; | |
1714 | }; | |
1715 | ||
1716 | /* Output all functions, variables, and asm statements in the order | |
1717 | according to their order fields, which is the order in which they | |
1718 | appeared in the file. This implements -fno-toplevel-reorder. In | |
1719 | this mode we may output functions and variables which don't really | |
1720 | need to be output. */ | |
1721 | ||
1722 | static void | |
1723 | cgraph_output_in_order (void) | |
1724 | { | |
1725 | int max; | |
56af936e | 1726 | struct cgraph_order_sort *nodes; |
1727 | int i; | |
1728 | struct cgraph_node *pf; | |
1d416bd7 | 1729 | struct varpool_node *pv; |
56af936e | 1730 | struct cgraph_asm_node *pa; |
1731 | ||
1732 | max = cgraph_order; | |
3e1cde87 | 1733 | nodes = XCNEWVEC (struct cgraph_order_sort, max); |
56af936e | 1734 | |
1d416bd7 | 1735 | varpool_analyze_pending_decls (); |
56af936e | 1736 | |
1737 | for (pf = cgraph_nodes; pf; pf = pf->next) | |
1738 | { | |
91bf9d9a | 1739 | if (pf->process && !pf->thunk.thunk_p) |
56af936e | 1740 | { |
1741 | i = pf->order; | |
1742 | gcc_assert (nodes[i].kind == ORDER_UNDEFINED); | |
1743 | nodes[i].kind = ORDER_FUNCTION; | |
1744 | nodes[i].u.f = pf; | |
1745 | } | |
1746 | } | |
1747 | ||
1d416bd7 | 1748 | for (pv = varpool_nodes_queue; pv; pv = pv->next_needed) |
56af936e | 1749 | { |
1750 | i = pv->order; | |
1751 | gcc_assert (nodes[i].kind == ORDER_UNDEFINED); | |
1752 | nodes[i].kind = ORDER_VAR; | |
1753 | nodes[i].u.v = pv; | |
1754 | } | |
1755 | ||
1756 | for (pa = cgraph_asm_nodes; pa; pa = pa->next) | |
1757 | { | |
1758 | i = pa->order; | |
1759 | gcc_assert (nodes[i].kind == ORDER_UNDEFINED); | |
1760 | nodes[i].kind = ORDER_ASM; | |
1761 | nodes[i].u.a = pa; | |
1762 | } | |
56af936e | 1763 | |
304e5318 | 1764 | /* In toplevel reorder mode we output all statics; mark them as needed. */ |
1765 | for (i = 0; i < max; ++i) | |
1766 | { | |
1767 | if (nodes[i].kind == ORDER_VAR) | |
1768 | { | |
1769 | varpool_mark_needed_node (nodes[i].u.v); | |
1770 | } | |
1771 | } | |
1772 | varpool_empty_needed_queue (); | |
1773 | ||
91da0f1c | 1774 | for (i = 0; i < max; ++i) |
1775 | if (nodes[i].kind == ORDER_VAR) | |
1776 | varpool_finalize_named_section_flags (nodes[i].u.v); | |
1777 | ||
56af936e | 1778 | for (i = 0; i < max; ++i) |
1779 | { | |
1780 | switch (nodes[i].kind) | |
1781 | { | |
1782 | case ORDER_FUNCTION: | |
09fc9532 | 1783 | nodes[i].u.f->process = 0; |
56af936e | 1784 | cgraph_expand_function (nodes[i].u.f); |
1785 | break; | |
1786 | ||
1787 | case ORDER_VAR: | |
1d416bd7 | 1788 | varpool_assemble_decl (nodes[i].u.v); |
56af936e | 1789 | break; |
1790 | ||
1791 | case ORDER_ASM: | |
1792 | assemble_asm (nodes[i].u.a->asm_str); | |
1793 | break; | |
1794 | ||
1795 | case ORDER_UNDEFINED: | |
1796 | break; | |
1797 | ||
1798 | default: | |
1799 | gcc_unreachable (); | |
1800 | } | |
1801 | } | |
4b4ea2db | 1802 | |
1803 | cgraph_asm_nodes = NULL; | |
3e1cde87 | 1804 | free (nodes); |
56af936e | 1805 | } |
1806 | ||
b0cdf642 | 1807 | /* Return true when function body of DECL still needs to be kept around |
1808 | for later re-use. */ | |
1809 | bool | |
cc91b414 | 1810 | cgraph_preserve_function_body_p (struct cgraph_node *node) |
b0cdf642 | 1811 | { |
8d8c4c8d | 1812 | gcc_assert (cgraph_global_info_ready); |
cc91b414 | 1813 | gcc_assert (!node->same_body_alias); |
1814 | ||
b0cdf642 | 1815 | /* Look if there is any clone around. */ |
ccf4ab6b | 1816 | if (node->clones) |
1817 | return true; | |
b0cdf642 | 1818 | return false; |
1819 | } | |
1820 | ||
77fce4cd | 1821 | static void |
1822 | ipa_passes (void) | |
1823 | { | |
87d4aa85 | 1824 | set_cfun (NULL); |
4b14adf9 | 1825 | current_function_decl = NULL; |
75a70cf9 | 1826 | gimple_register_cfg_hooks (); |
77fce4cd | 1827 | bitmap_obstack_initialize (NULL); |
59dd4830 | 1828 | |
c9036234 | 1829 | invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL); |
1830 | ||
59dd4830 | 1831 | if (!in_lto_p) |
7b2e8956 | 1832 | { |
1833 | execute_ipa_pass_list (all_small_ipa_passes); | |
1834 | if (seen_error ()) | |
1835 | return; | |
1836 | } | |
9ed5b1f5 | 1837 | |
7bfefa9d | 1838 | /* If pass_all_early_optimizations was not scheduled, the state of |
1839 | the cgraph will not be properly updated. Update it now. */ | |
1840 | if (cgraph_state < CGRAPH_STATE_IPA_SSA) | |
1841 | cgraph_state = CGRAPH_STATE_IPA_SSA; | |
9ed5b1f5 | 1842 | |
7bfefa9d | 1843 | if (!in_lto_p) |
1844 | { | |
1845 | /* Generate coverage variables and constructors. */ | |
1846 | coverage_finish (); | |
1847 | ||
1848 | /* Process new functions added. */ | |
1849 | set_cfun (NULL); | |
1850 | current_function_decl = NULL; | |
1851 | cgraph_process_new_functions (); | |
7bfefa9d | 1852 | |
c9036234 | 1853 | execute_ipa_summary_passes |
1854 | ((struct ipa_opt_pass_d *) all_regular_ipa_passes); | |
8867b500 | 1855 | } |
23433d72 | 1856 | |
1857 | /* Some targets need to handle LTO assembler output specially. */ | |
1858 | if (flag_generate_lto) | |
1859 | targetm.asm_out.lto_start (); | |
1860 | ||
7bfefa9d | 1861 | execute_ipa_summary_passes ((struct ipa_opt_pass_d *) all_lto_gen_passes); |
1862 | ||
1863 | if (!in_lto_p) | |
1864 | ipa_write_summaries (); | |
1865 | ||
23433d72 | 1866 | if (flag_generate_lto) |
1867 | targetm.asm_out.lto_end (); | |
1868 | ||
8867b500 | 1869 | if (!flag_ltrans) |
1870 | execute_ipa_pass_list (all_regular_ipa_passes); | |
c9036234 | 1871 | invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL); |
9ed5b1f5 | 1872 | |
77fce4cd | 1873 | bitmap_obstack_release (NULL); |
1874 | } | |
1875 | ||
34e5cced | 1876 | |
ae01b312 | 1877 | /* Perform simple optimizations based on callgraph. */ |
1878 | ||
7bfefa9d | 1879 | void |
d9d9733a | 1880 | cgraph_optimize (void) |
ae01b312 | 1881 | { |
852f689e | 1882 | if (seen_error ()) |
cb2b5570 | 1883 | return; |
1884 | ||
b0cdf642 | 1885 | #ifdef ENABLE_CHECKING |
1886 | verify_cgraph (); | |
1887 | #endif | |
a861fe52 | 1888 | |
c1dcd13c | 1889 | /* Frontend may output common variables after the unit has been finalized. |
1890 | It is safe to deal with them here as they are always zero initialized. */ | |
1d416bd7 | 1891 | varpool_analyze_pending_decls (); |
e9f08e82 | 1892 | |
f79b6507 | 1893 | timevar_push (TV_CGRAPHOPT); |
51949610 | 1894 | if (pre_ipa_mem_report) |
1895 | { | |
1896 | fprintf (stderr, "Memory consumption before IPA\n"); | |
1897 | dump_memory_report (false); | |
1898 | } | |
d7c6d889 | 1899 | if (!quiet_flag) |
cd6bca02 | 1900 | fprintf (stderr, "Performing interprocedural optimizations\n"); |
523c1122 | 1901 | cgraph_state = CGRAPH_STATE_IPA; |
c04e3894 | 1902 | |
be4d0974 | 1903 | /* Don't run the IPA passes if there was any error or sorry messages. */ |
852f689e | 1904 | if (!seen_error ()) |
be4d0974 | 1905 | ipa_passes (); |
1906 | ||
34e5cced | 1907 | /* Do nothing else if any IPA pass found errors. */ |
852f689e | 1908 | if (seen_error ()) |
021c1c18 | 1909 | { |
1910 | timevar_pop (TV_CGRAPHOPT); | |
1911 | return; | |
1912 | } | |
34e5cced | 1913 | |
e1be32b8 | 1914 | /* This pass remove bodies of extern inline functions we never inlined. |
1915 | Do this later so other IPA passes see what is really going on. */ | |
1916 | cgraph_remove_unreachable_nodes (false, dump_file); | |
80a85d8a | 1917 | cgraph_global_info_ready = true; |
f79b6507 | 1918 | if (cgraph_dump_file) |
1919 | { | |
e4200070 | 1920 | fprintf (cgraph_dump_file, "Optimized "); |
f79b6507 | 1921 | dump_cgraph (cgraph_dump_file); |
c1dcd13c | 1922 | dump_varpool (cgraph_dump_file); |
f79b6507 | 1923 | } |
51949610 | 1924 | if (post_ipa_mem_report) |
1925 | { | |
defa2fa6 | 1926 | fprintf (stderr, "Memory consumption after IPA\n"); |
51949610 | 1927 | dump_memory_report (false); |
1928 | } | |
f79b6507 | 1929 | timevar_pop (TV_CGRAPHOPT); |
ae01b312 | 1930 | |
d7c6d889 | 1931 | /* Output everything. */ |
47306a5d | 1932 | (*debug_hooks->assembly_start) (); |
e4200070 | 1933 | if (!quiet_flag) |
1934 | fprintf (stderr, "Assembling functions:\n"); | |
b0cdf642 | 1935 | #ifdef ENABLE_CHECKING |
1936 | verify_cgraph (); | |
1937 | #endif | |
56af936e | 1938 | |
ccf4ab6b | 1939 | cgraph_materialize_all_clones (); |
acc70efa | 1940 | cgraph_mark_functions_to_output (); |
c1dcd13c | 1941 | |
523c1122 | 1942 | cgraph_state = CGRAPH_STATE_EXPANSION; |
56af936e | 1943 | if (!flag_toplevel_reorder) |
1944 | cgraph_output_in_order (); | |
1945 | else | |
1946 | { | |
1947 | cgraph_output_pending_asms (); | |
1948 | ||
1949 | cgraph_expand_all_functions (); | |
1d416bd7 | 1950 | varpool_remove_unreferenced_decls (); |
56af936e | 1951 | |
1d416bd7 | 1952 | varpool_assemble_pending_decls (); |
56af936e | 1953 | } |
523c1122 | 1954 | cgraph_process_new_functions (); |
1955 | cgraph_state = CGRAPH_STATE_FINISHED; | |
c1dcd13c | 1956 | |
f79b6507 | 1957 | if (cgraph_dump_file) |
1958 | { | |
e4200070 | 1959 | fprintf (cgraph_dump_file, "\nFinal "); |
f79b6507 | 1960 | dump_cgraph (cgraph_dump_file); |
7410370b | 1961 | dump_varpool (cgraph_dump_file); |
f79b6507 | 1962 | } |
b0cdf642 | 1963 | #ifdef ENABLE_CHECKING |
1964 | verify_cgraph (); | |
4ee9c684 | 1965 | /* Double check that all inline clones are gone and that all |
1966 | function bodies have been released from memory. */ | |
852f689e | 1967 | if (!seen_error ()) |
4ee9c684 | 1968 | { |
1969 | struct cgraph_node *node; | |
1970 | bool error_found = false; | |
1971 | ||
1972 | for (node = cgraph_nodes; node; node = node->next) | |
1973 | if (node->analyzed | |
1974 | && (node->global.inlined_to | |
1a1a827a | 1975 | || gimple_has_body_p (node->decl))) |
4ee9c684 | 1976 | { |
1977 | error_found = true; | |
1978 | dump_cgraph_node (stderr, node); | |
a0c938f0 | 1979 | } |
4ee9c684 | 1980 | if (error_found) |
c04e3894 | 1981 | internal_error ("nodes with unreleased memory found"); |
4ee9c684 | 1982 | } |
b0cdf642 | 1983 | #endif |
ae01b312 | 1984 | } |
34e5cced | 1985 | |
121f3051 | 1986 | void |
1987 | init_cgraph (void) | |
1988 | { | |
01ec0a6c | 1989 | if (!cgraph_dump_file) |
1990 | cgraph_dump_file = dump_begin (TDI_cgraph, NULL); | |
121f3051 | 1991 | } |
b5d36404 | 1992 | |
a0c938f0 | 1993 | /* The edges representing the callers of the NEW_VERSION node were |
b5d36404 | 1994 | fixed by cgraph_function_versioning (), now the call_expr in their |
1995 | respective tree code should be updated to call the NEW_VERSION. */ | |
1996 | ||
1997 | static void | |
1998 | update_call_expr (struct cgraph_node *new_version) | |
1999 | { | |
2000 | struct cgraph_edge *e; | |
2001 | ||
2002 | gcc_assert (new_version); | |
75a70cf9 | 2003 | |
2004 | /* Update the call expr on the edges to call the new version. */ | |
b5d36404 | 2005 | for (e = new_version->callers; e; e = e->next_caller) |
e03a95e7 | 2006 | { |
2007 | struct function *inner_function = DECL_STRUCT_FUNCTION (e->caller->decl); | |
2008 | gimple_call_set_fndecl (e->call_stmt, new_version->decl); | |
e38def9c | 2009 | maybe_clean_eh_stmt_fn (inner_function, e->call_stmt); |
e03a95e7 | 2010 | } |
b5d36404 | 2011 | } |
2012 | ||
2013 | ||
2014 | /* Create a new cgraph node which is the new version of | |
2015 | OLD_VERSION node. REDIRECT_CALLERS holds the callers | |
2016 | edges which should be redirected to point to | |
2017 | NEW_VERSION. ALL the callees edges of OLD_VERSION | |
2018 | are cloned to the new version node. Return the new | |
b06ab5fa | 2019 | version node. |
2020 | ||
2021 | If non-NULL BLOCK_TO_COPY determine what basic blocks | |
2022 | was copied to prevent duplications of calls that are dead | |
2023 | in the clone. */ | |
b5d36404 | 2024 | |
2025 | static struct cgraph_node * | |
2026 | cgraph_copy_node_for_versioning (struct cgraph_node *old_version, | |
4460a647 | 2027 | tree new_decl, |
b06ab5fa | 2028 | VEC(cgraph_edge_p,heap) *redirect_callers, |
2029 | bitmap bbs_to_copy) | |
2030 | { | |
b5d36404 | 2031 | struct cgraph_node *new_version; |
32936803 | 2032 | struct cgraph_edge *e; |
b5d36404 | 2033 | unsigned i; |
2034 | ||
2035 | gcc_assert (old_version); | |
a0c938f0 | 2036 | |
5a90471f | 2037 | new_version = cgraph_create_node (new_decl); |
b5d36404 | 2038 | |
2039 | new_version->analyzed = true; | |
2040 | new_version->local = old_version->local; | |
a70a5e2c | 2041 | new_version->local.externally_visible = false; |
2042 | new_version->local.local = true; | |
b5d36404 | 2043 | new_version->global = old_version->global; |
a93f1c3b | 2044 | new_version->rtl = old_version->rtl; |
b5d36404 | 2045 | new_version->reachable = true; |
2046 | new_version->count = old_version->count; | |
2047 | ||
a70a5e2c | 2048 | for (e = old_version->callees; e; e=e->next_callee) |
b06ab5fa | 2049 | if (!bbs_to_copy |
2050 | || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index)) | |
2051 | cgraph_clone_edge (e, new_version, e->call_stmt, | |
2052 | e->lto_stmt_uid, REG_BR_PROB_BASE, | |
2053 | CGRAPH_FREQ_BASE, | |
0835ad03 | 2054 | true); |
a70a5e2c | 2055 | for (e = old_version->indirect_calls; e; e=e->next_callee) |
b06ab5fa | 2056 | if (!bbs_to_copy |
2057 | || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index)) | |
2058 | cgraph_clone_edge (e, new_version, e->call_stmt, | |
2059 | e->lto_stmt_uid, REG_BR_PROB_BASE, | |
2060 | CGRAPH_FREQ_BASE, | |
0835ad03 | 2061 | true); |
48148244 | 2062 | FOR_EACH_VEC_ELT (cgraph_edge_p, redirect_callers, i, e) |
4460a647 | 2063 | { |
2064 | /* Redirect calls to the old version node to point to its new | |
2065 | version. */ | |
2066 | cgraph_redirect_edge_callee (e, new_version); | |
2067 | } | |
b5d36404 | 2068 | |
2069 | return new_version; | |
2070 | } | |
2071 | ||
2072 | /* Perform function versioning. | |
a0c938f0 | 2073 | Function versioning includes copying of the tree and |
b5d36404 | 2074 | a callgraph update (creating a new cgraph node and updating |
2075 | its callees and callers). | |
2076 | ||
2077 | REDIRECT_CALLERS varray includes the edges to be redirected | |
2078 | to the new version. | |
2079 | ||
2080 | TREE_MAP is a mapping of tree nodes we want to replace with | |
2081 | new ones (according to results of prior analysis). | |
2082 | OLD_VERSION_NODE is the node that is versioned. | |
48e1416a | 2083 | It returns the new version's cgraph node. |
b06ab5fa | 2084 | If non-NULL ARGS_TO_SKIP determine function parameters to remove |
2085 | from new version. | |
2086 | If non-NULL BLOCK_TO_COPY determine what basic blocks to copy. | |
2087 | If non_NULL NEW_ENTRY determine new entry BB of the clone. */ | |
b5d36404 | 2088 | |
2089 | struct cgraph_node * | |
2090 | cgraph_function_versioning (struct cgraph_node *old_version_node, | |
4460a647 | 2091 | VEC(cgraph_edge_p,heap) *redirect_callers, |
ccf4ab6b | 2092 | VEC (ipa_replace_map_p,gc)* tree_map, |
a70a5e2c | 2093 | bitmap args_to_skip, |
b06ab5fa | 2094 | bitmap bbs_to_copy, |
2095 | basic_block new_entry_block, | |
a70a5e2c | 2096 | const char *clone_name) |
b5d36404 | 2097 | { |
2098 | tree old_decl = old_version_node->decl; | |
2099 | struct cgraph_node *new_version_node = NULL; | |
2100 | tree new_decl; | |
2101 | ||
2102 | if (!tree_versionable_function_p (old_decl)) | |
2103 | return NULL; | |
2104 | ||
3c97c75d | 2105 | gcc_assert (old_version_node->local.can_change_signature || !args_to_skip); |
2106 | ||
b5d36404 | 2107 | /* Make a new FUNCTION_DECL tree node for the |
2108 | new version. */ | |
5afe38fe | 2109 | if (!args_to_skip) |
2110 | new_decl = copy_node (old_decl); | |
2111 | else | |
2112 | new_decl = build_function_decl_skip_args (old_decl, args_to_skip); | |
b5d36404 | 2113 | |
df0b8dfb | 2114 | /* Generate a new name for the new version. */ |
2115 | DECL_NAME (new_decl) = clone_function_name (old_decl, clone_name); | |
2116 | SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl)); | |
2117 | SET_DECL_RTL (new_decl, NULL); | |
2118 | ||
b5d36404 | 2119 | /* Create the new version's call-graph node. |
2120 | and update the edges of the new node. */ | |
2121 | new_version_node = | |
2122 | cgraph_copy_node_for_versioning (old_version_node, new_decl, | |
b06ab5fa | 2123 | redirect_callers, bbs_to_copy); |
b5d36404 | 2124 | |
2125 | /* Copy the OLD_VERSION_NODE function tree to the new version. */ | |
b06ab5fa | 2126 | tree_function_versioning (old_decl, new_decl, tree_map, false, args_to_skip, |
2127 | bbs_to_copy, new_entry_block); | |
b5d36404 | 2128 | |
a0c938f0 | 2129 | /* Update the new version's properties. |
e03a95e7 | 2130 | Make The new version visible only within this translation unit. Make sure |
2131 | that is not weak also. | |
a0c938f0 | 2132 | ??? We cannot use COMDAT linkage because there is no |
b5d36404 | 2133 | ABI support for this. */ |
6137cc9f | 2134 | cgraph_make_decl_local (new_version_node->decl); |
f014e39d | 2135 | DECL_VIRTUAL_P (new_version_node->decl) = 0; |
b5d36404 | 2136 | new_version_node->local.externally_visible = 0; |
2137 | new_version_node->local.local = 1; | |
2138 | new_version_node->lowered = true; | |
f014e39d | 2139 | |
e03a95e7 | 2140 | /* Update the call_expr on the edges to call the new version node. */ |
2141 | update_call_expr (new_version_node); | |
48e1416a | 2142 | |
50828ed8 | 2143 | cgraph_call_function_insertion_hooks (new_version_node); |
b5d36404 | 2144 | return new_version_node; |
2145 | } | |
469679ab | 2146 | |
ccf4ab6b | 2147 | /* Given virtual clone, turn it into actual clone. */ |
2148 | static void | |
2149 | cgraph_materialize_clone (struct cgraph_node *node) | |
2150 | { | |
2151 | bitmap_obstack_initialize (NULL); | |
e748b31d | 2152 | node->former_clone_of = node->clone_of->decl; |
2153 | if (node->clone_of->former_clone_of) | |
2154 | node->former_clone_of = node->clone_of->former_clone_of; | |
ccf4ab6b | 2155 | /* Copy the OLD_VERSION_NODE function tree to the new version. */ |
2156 | tree_function_versioning (node->clone_of->decl, node->decl, | |
2157 | node->clone.tree_map, true, | |
b06ab5fa | 2158 | node->clone.args_to_skip, NULL, NULL); |
e20422ea | 2159 | if (cgraph_dump_file) |
2160 | { | |
2161 | dump_function_to_file (node->clone_of->decl, cgraph_dump_file, dump_flags); | |
2162 | dump_function_to_file (node->decl, cgraph_dump_file, dump_flags); | |
2163 | } | |
ccf4ab6b | 2164 | |
2165 | /* Function is no longer clone. */ | |
2166 | if (node->next_sibling_clone) | |
2167 | node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone; | |
2168 | if (node->prev_sibling_clone) | |
2169 | node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone; | |
2170 | else | |
2171 | node->clone_of->clones = node->next_sibling_clone; | |
2172 | node->next_sibling_clone = NULL; | |
2173 | node->prev_sibling_clone = NULL; | |
6d1cc52c | 2174 | if (!node->clone_of->analyzed && !node->clone_of->clones) |
7d6a1ec8 | 2175 | { |
2176 | cgraph_release_function_body (node->clone_of); | |
2177 | cgraph_node_remove_callees (node->clone_of); | |
2178 | ipa_remove_all_references (&node->clone_of->ref_list); | |
2179 | } | |
ccf4ab6b | 2180 | node->clone_of = NULL; |
2181 | bitmap_obstack_release (NULL); | |
2182 | } | |
2183 | ||
c596d830 | 2184 | /* If necessary, change the function declaration in the call statement |
2185 | associated with E so that it corresponds to the edge callee. */ | |
2186 | ||
2187 | gimple | |
2188 | cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e) | |
2189 | { | |
2190 | tree decl = gimple_call_fndecl (e->call_stmt); | |
2191 | gimple new_stmt; | |
3fd0ca33 | 2192 | gimple_stmt_iterator gsi; |
2193 | bool gsi_computed = false; | |
1f449108 | 2194 | #ifdef ENABLE_CHECKING |
2195 | struct cgraph_node *node; | |
2196 | #endif | |
c596d830 | 2197 | |
1caef38b | 2198 | if (e->indirect_unknown_callee |
2199 | || decl == e->callee->decl | |
c596d830 | 2200 | /* Don't update call from same body alias to the real function. */ |
1caef38b | 2201 | || (decl && cgraph_get_node (decl) == cgraph_get_node (e->callee->decl))) |
c596d830 | 2202 | return e->call_stmt; |
2203 | ||
1f449108 | 2204 | #ifdef ENABLE_CHECKING |
1caef38b | 2205 | if (decl) |
2206 | { | |
2207 | node = cgraph_get_node (decl); | |
2208 | gcc_assert (!node || !node->clone.combined_args_to_skip); | |
2209 | } | |
1f449108 | 2210 | #endif |
e748b31d | 2211 | |
c596d830 | 2212 | if (cgraph_dump_file) |
2213 | { | |
2214 | fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ", | |
2215 | cgraph_node_name (e->caller), e->caller->uid, | |
2216 | cgraph_node_name (e->callee), e->callee->uid); | |
2217 | print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags); | |
e748b31d | 2218 | if (e->callee->clone.combined_args_to_skip) |
91aba934 | 2219 | { |
2220 | fprintf (cgraph_dump_file, " combined args to skip: "); | |
2221 | dump_bitmap (cgraph_dump_file, | |
2222 | e->callee->clone.combined_args_to_skip); | |
e748b31d | 2223 | } |
c596d830 | 2224 | } |
2225 | ||
9bab6a70 | 2226 | if (e->indirect_info && |
2227 | e->indirect_info->thunk_delta != 0 | |
3fd0ca33 | 2228 | && (!e->callee->clone.combined_args_to_skip |
2229 | || !bitmap_bit_p (e->callee->clone.combined_args_to_skip, 0))) | |
2230 | { | |
2231 | if (cgraph_dump_file) | |
9bab6a70 | 2232 | fprintf (cgraph_dump_file, " Thunk delta is " |
2233 | HOST_WIDE_INT_PRINT_DEC "\n", e->indirect_info->thunk_delta); | |
3fd0ca33 | 2234 | gsi = gsi_for_stmt (e->call_stmt); |
2235 | gsi_computed = true; | |
9bab6a70 | 2236 | gimple_adjust_this_by_delta (&gsi, |
2237 | build_int_cst (sizetype, | |
2238 | e->indirect_info->thunk_delta)); | |
2239 | e->indirect_info->thunk_delta = 0; | |
3fd0ca33 | 2240 | } |
2241 | ||
c596d830 | 2242 | if (e->callee->clone.combined_args_to_skip) |
91aba934 | 2243 | { |
092cd838 | 2244 | int lp_nr; |
91aba934 | 2245 | |
2246 | new_stmt | |
2247 | = gimple_call_copy_skip_args (e->call_stmt, | |
2248 | e->callee->clone.combined_args_to_skip); | |
75c7f5a5 | 2249 | gimple_call_set_fndecl (new_stmt, e->callee->decl); |
91aba934 | 2250 | |
2251 | if (gimple_vdef (new_stmt) | |
2252 | && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME) | |
2253 | SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt; | |
2254 | ||
3fd0ca33 | 2255 | if (!gsi_computed) |
2256 | gsi = gsi_for_stmt (e->call_stmt); | |
9126b675 | 2257 | gsi_replace (&gsi, new_stmt, false); |
092cd838 | 2258 | /* We need to defer cleaning EH info on the new statement to |
2259 | fixup-cfg. We may not have dominator information at this point | |
2260 | and thus would end up with unreachable blocks and have no way | |
2261 | to communicate that we need to run CFG cleanup then. */ | |
2262 | lp_nr = lookup_stmt_eh_lp (e->call_stmt); | |
2263 | if (lp_nr != 0) | |
2264 | { | |
2265 | remove_stmt_from_eh_lp (e->call_stmt); | |
2266 | add_stmt_to_eh_lp (new_stmt, lp_nr); | |
2267 | } | |
91aba934 | 2268 | } |
c596d830 | 2269 | else |
75c7f5a5 | 2270 | { |
2271 | new_stmt = e->call_stmt; | |
2272 | gimple_call_set_fndecl (new_stmt, e->callee->decl); | |
2273 | update_stmt (new_stmt); | |
2274 | } | |
c596d830 | 2275 | |
c596d830 | 2276 | cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt); |
2277 | ||
2278 | if (cgraph_dump_file) | |
2279 | { | |
2280 | fprintf (cgraph_dump_file, " updated to:"); | |
2281 | print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags); | |
2282 | } | |
2283 | return new_stmt; | |
2284 | } | |
2285 | ||
ccf4ab6b | 2286 | /* Once all functions from compilation unit are in memory, produce all clones |
c596d830 | 2287 | and update all calls. We might also do this on demand if we don't want to |
2288 | bring all functions to memory prior compilation, but current WHOPR | |
2289 | implementation does that and it is is bit easier to keep everything right in | |
2290 | this order. */ | |
ccf4ab6b | 2291 | void |
2292 | cgraph_materialize_all_clones (void) | |
2293 | { | |
2294 | struct cgraph_node *node; | |
2295 | bool stabilized = false; | |
2296 | ||
2297 | if (cgraph_dump_file) | |
2298 | fprintf (cgraph_dump_file, "Materializing clones\n"); | |
2299 | #ifdef ENABLE_CHECKING | |
2300 | verify_cgraph (); | |
2301 | #endif | |
2302 | ||
2303 | /* We can also do topological order, but number of iterations should be | |
2304 | bounded by number of IPA passes since single IPA pass is probably not | |
2305 | going to create clones of clones it created itself. */ | |
2306 | while (!stabilized) | |
2307 | { | |
2308 | stabilized = true; | |
2309 | for (node = cgraph_nodes; node; node = node->next) | |
2310 | { | |
2311 | if (node->clone_of && node->decl != node->clone_of->decl | |
2312 | && !gimple_has_body_p (node->decl)) | |
2313 | { | |
2314 | if (gimple_has_body_p (node->clone_of->decl)) | |
2315 | { | |
2316 | if (cgraph_dump_file) | |
e20422ea | 2317 | { |
0a10fd82 | 2318 | fprintf (cgraph_dump_file, "cloning %s to %s\n", |
e20422ea | 2319 | cgraph_node_name (node->clone_of), |
2320 | cgraph_node_name (node)); | |
2321 | if (node->clone.tree_map) | |
2322 | { | |
2323 | unsigned int i; | |
2324 | fprintf (cgraph_dump_file, " replace map: "); | |
2325 | for (i = 0; i < VEC_length (ipa_replace_map_p, | |
2326 | node->clone.tree_map); | |
2327 | i++) | |
2328 | { | |
2329 | struct ipa_replace_map *replace_info; | |
2330 | replace_info = VEC_index (ipa_replace_map_p, | |
2331 | node->clone.tree_map, | |
2332 | i); | |
2333 | print_generic_expr (cgraph_dump_file, replace_info->old_tree, 0); | |
2334 | fprintf (cgraph_dump_file, " -> "); | |
2335 | print_generic_expr (cgraph_dump_file, replace_info->new_tree, 0); | |
2336 | fprintf (cgraph_dump_file, "%s%s;", | |
2337 | replace_info->replace_p ? "(replace)":"", | |
2338 | replace_info->ref_p ? "(ref)":""); | |
2339 | } | |
2340 | fprintf (cgraph_dump_file, "\n"); | |
2341 | } | |
2342 | if (node->clone.args_to_skip) | |
2343 | { | |
2344 | fprintf (cgraph_dump_file, " args_to_skip: "); | |
2345 | dump_bitmap (cgraph_dump_file, node->clone.args_to_skip); | |
2346 | } | |
2347 | if (node->clone.args_to_skip) | |
2348 | { | |
2349 | fprintf (cgraph_dump_file, " combined_args_to_skip:"); | |
2350 | dump_bitmap (cgraph_dump_file, node->clone.combined_args_to_skip); | |
2351 | } | |
2352 | } | |
ccf4ab6b | 2353 | cgraph_materialize_clone (node); |
a510bd8d | 2354 | stabilized = false; |
ccf4ab6b | 2355 | } |
ccf4ab6b | 2356 | } |
2357 | } | |
2358 | } | |
ee3f5fc0 | 2359 | for (node = cgraph_nodes; node; node = node->next) |
2360 | if (!node->analyzed && node->callees) | |
2361 | cgraph_node_remove_callees (node); | |
c596d830 | 2362 | if (cgraph_dump_file) |
2363 | fprintf (cgraph_dump_file, "Materialization Call site updates done.\n"); | |
947781ac | 2364 | #ifdef ENABLE_CHECKING |
2365 | verify_cgraph (); | |
2366 | #endif | |
ccf4ab6b | 2367 | cgraph_remove_unreachable_nodes (false, cgraph_dump_file); |
2368 | } | |
2369 | ||
a861fe52 | 2370 | #include "gt-cgraphunit.h" |