]>
Commit | Line | Data |
---|---|---|
833eb724 | 1 | /* Callgraph handling code. |
0704fb2e | 2 | Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, |
3 | 2011, 2012 Free Software Foundation, Inc. | |
833eb724 | 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 |
833eb724 | 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/>. */ | |
833eb724 | 21 | |
1d416bd7 | 22 | /* This file contains basic routines manipulating call graph |
a0c938f0 | 23 | |
da5e1e7c | 24 | The call-graph is a data structure designed for intra-procedural optimization. |
25 | It represents a multi-graph where nodes are functions and edges are call sites. */ | |
b0cdf642 | 26 | |
833eb724 | 27 | #include "config.h" |
28 | #include "system.h" | |
29 | #include "coretypes.h" | |
30 | #include "tm.h" | |
31 | #include "tree.h" | |
c1dcd13c | 32 | #include "tree-inline.h" |
833eb724 | 33 | #include "langhooks.h" |
34 | #include "hashtab.h" | |
35 | #include "toplev.h" | |
36 | #include "flags.h" | |
37 | #include "ggc.h" | |
38 | #include "debug.h" | |
39 | #include "target.h" | |
c1dcd13c | 40 | #include "basic-block.h" |
ae01b312 | 41 | #include "cgraph.h" |
229dcfae | 42 | #include "output.h" |
611e5405 | 43 | #include "intl.h" |
75a70cf9 | 44 | #include "gimple.h" |
77fce4cd | 45 | #include "tree-dump.h" |
f517b36e | 46 | #include "tree-flow.h" |
bfb4e08d | 47 | #include "value-prof.h" |
58d82cd0 | 48 | #include "except.h" |
852f689e | 49 | #include "diagnostic-core.h" |
6137cc9f | 50 | #include "rtl.h" |
2b2fe710 | 51 | #include "ipa-utils.h" |
bfca27e3 | 52 | #include "lto-streamer.h" |
cbd7f5a0 | 53 | #include "ipa-inline.h" |
79f958cb | 54 | #include "cfgloop.h" |
da5e1e7c | 55 | #include "gimple-pretty-print.h" |
639e4be4 | 56 | |
bb4c7a44 | 57 | static void cgraph_node_remove_callers (struct cgraph_node *node); |
58 | static inline void cgraph_edge_remove_caller (struct cgraph_edge *e); | |
59 | static inline void cgraph_edge_remove_callee (struct cgraph_edge *e); | |
60 | ||
3d7bfc56 | 61 | /* Queue of cgraph nodes scheduled to be lowered. */ |
7d0d0ce1 | 62 | symtab_node x_cgraph_nodes_queue; |
63 | #define cgraph_nodes_queue ((struct cgraph_node *)x_cgraph_nodes_queue) | |
3d7bfc56 | 64 | |
833eb724 | 65 | /* Number of nodes in existence. */ |
ae01b312 | 66 | int cgraph_n_nodes; |
833eb724 | 67 | |
d7c6d889 | 68 | /* Maximal uid used in cgraph nodes. */ |
69 | int cgraph_max_uid; | |
70 | ||
dd1c9157 | 71 | /* Maximal uid used in cgraph edges. */ |
72 | int cgraph_edge_max_uid; | |
73 | ||
80a85d8a | 74 | /* Set when whole unit has been analyzed so we can access global info. */ |
75 | bool cgraph_global_info_ready = false; | |
76 | ||
523c1122 | 77 | /* What state callgraph is in right now. */ |
ff2a5ada | 78 | enum cgraph_state cgraph_state = CGRAPH_STATE_PARSING; |
523c1122 | 79 | |
c1dcd13c | 80 | /* Set when the cgraph is fully build and the basic flags are computed. */ |
81 | bool cgraph_function_flags_ready = false; | |
82 | ||
0a10fd82 | 83 | /* List of hooks triggered on cgraph_edge events. */ |
dd1c9157 | 84 | struct cgraph_edge_hook_list { |
85 | cgraph_edge_hook hook; | |
86 | void *data; | |
87 | struct cgraph_edge_hook_list *next; | |
88 | }; | |
89 | ||
0a10fd82 | 90 | /* List of hooks triggered on cgraph_node events. */ |
dd1c9157 | 91 | struct cgraph_node_hook_list { |
92 | cgraph_node_hook hook; | |
93 | void *data; | |
94 | struct cgraph_node_hook_list *next; | |
95 | }; | |
96 | ||
0a10fd82 | 97 | /* List of hooks triggered on events involving two cgraph_edges. */ |
dd1c9157 | 98 | struct cgraph_2edge_hook_list { |
99 | cgraph_2edge_hook hook; | |
100 | void *data; | |
101 | struct cgraph_2edge_hook_list *next; | |
102 | }; | |
103 | ||
0a10fd82 | 104 | /* List of hooks triggered on events involving two cgraph_nodes. */ |
dd1c9157 | 105 | struct cgraph_2node_hook_list { |
106 | cgraph_2node_hook hook; | |
107 | void *data; | |
108 | struct cgraph_2node_hook_list *next; | |
109 | }; | |
110 | ||
111 | /* List of hooks triggered when an edge is removed. */ | |
112 | struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook; | |
113 | /* List of hooks triggered when a node is removed. */ | |
114 | struct cgraph_node_hook_list *first_cgraph_node_removal_hook; | |
115 | /* List of hooks triggered when an edge is duplicated. */ | |
116 | struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook; | |
117 | /* List of hooks triggered when a node is duplicated. */ | |
118 | struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook; | |
50828ed8 | 119 | /* List of hooks triggered when an function is inserted. */ |
120 | struct cgraph_node_hook_list *first_cgraph_function_insertion_hook; | |
dd1c9157 | 121 | |
8325d2de | 122 | /* Head of a linked list of unused (freed) call graph nodes. |
123 | Do not GTY((delete)) this list so UIDs gets reliably recycled. */ | |
124 | static GTY(()) struct cgraph_node *free_nodes; | |
e83fcabc | 125 | /* Head of a linked list of unused (freed) call graph edges. |
126 | Do not GTY((delete)) this list so UIDs gets reliably recycled. */ | |
127 | static GTY(()) struct cgraph_edge *free_edges; | |
128 | ||
c70f46b0 | 129 | /* Did procss_same_body_aliases run? */ |
130 | bool same_body_aliases_done; | |
131 | ||
8325d2de | 132 | /* Macros to access the next item in the list of free cgraph nodes and |
133 | edges. */ | |
0704fb2e | 134 | #define NEXT_FREE_NODE(NODE) cgraph ((NODE)->symbol.next) |
135 | #define SET_NEXT_FREE_NODE(NODE,NODE2) ((NODE))->symbol.next = (symtab_node)NODE2 | |
e83fcabc | 136 | #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller |
dd1c9157 | 137 | |
138 | /* Register HOOK to be called with DATA on each removed edge. */ | |
139 | struct cgraph_edge_hook_list * | |
140 | cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data) | |
141 | { | |
142 | struct cgraph_edge_hook_list *entry; | |
143 | struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook; | |
144 | ||
145 | entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry)); | |
146 | entry->hook = hook; | |
147 | entry->data = data; | |
148 | entry->next = NULL; | |
149 | while (*ptr) | |
150 | ptr = &(*ptr)->next; | |
151 | *ptr = entry; | |
152 | return entry; | |
153 | } | |
154 | ||
155 | /* Remove ENTRY from the list of hooks called on removing edges. */ | |
156 | void | |
157 | cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry) | |
158 | { | |
159 | struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook; | |
160 | ||
161 | while (*ptr != entry) | |
162 | ptr = &(*ptr)->next; | |
163 | *ptr = entry->next; | |
e83fcabc | 164 | free (entry); |
dd1c9157 | 165 | } |
166 | ||
167 | /* Call all edge removal hooks. */ | |
168 | static void | |
169 | cgraph_call_edge_removal_hooks (struct cgraph_edge *e) | |
170 | { | |
171 | struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook; | |
172 | while (entry) | |
173 | { | |
174 | entry->hook (e, entry->data); | |
175 | entry = entry->next; | |
176 | } | |
177 | } | |
178 | ||
179 | /* Register HOOK to be called with DATA on each removed node. */ | |
180 | struct cgraph_node_hook_list * | |
181 | cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data) | |
182 | { | |
183 | struct cgraph_node_hook_list *entry; | |
184 | struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook; | |
185 | ||
186 | entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry)); | |
187 | entry->hook = hook; | |
188 | entry->data = data; | |
189 | entry->next = NULL; | |
190 | while (*ptr) | |
191 | ptr = &(*ptr)->next; | |
192 | *ptr = entry; | |
193 | return entry; | |
194 | } | |
195 | ||
196 | /* Remove ENTRY from the list of hooks called on removing nodes. */ | |
197 | void | |
198 | cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry) | |
199 | { | |
200 | struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook; | |
201 | ||
202 | while (*ptr != entry) | |
203 | ptr = &(*ptr)->next; | |
204 | *ptr = entry->next; | |
e83fcabc | 205 | free (entry); |
dd1c9157 | 206 | } |
207 | ||
208 | /* Call all node removal hooks. */ | |
209 | static void | |
210 | cgraph_call_node_removal_hooks (struct cgraph_node *node) | |
211 | { | |
212 | struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook; | |
213 | while (entry) | |
214 | { | |
215 | entry->hook (node, entry->data); | |
216 | entry = entry->next; | |
217 | } | |
218 | } | |
219 | ||
1359ee7a | 220 | /* Register HOOK to be called with DATA on each inserted node. */ |
50828ed8 | 221 | struct cgraph_node_hook_list * |
222 | cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data) | |
223 | { | |
224 | struct cgraph_node_hook_list *entry; | |
225 | struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook; | |
226 | ||
227 | entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry)); | |
228 | entry->hook = hook; | |
229 | entry->data = data; | |
230 | entry->next = NULL; | |
231 | while (*ptr) | |
232 | ptr = &(*ptr)->next; | |
233 | *ptr = entry; | |
234 | return entry; | |
235 | } | |
236 | ||
1359ee7a | 237 | /* Remove ENTRY from the list of hooks called on inserted nodes. */ |
50828ed8 | 238 | void |
239 | cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry) | |
240 | { | |
241 | struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook; | |
242 | ||
243 | while (*ptr != entry) | |
244 | ptr = &(*ptr)->next; | |
245 | *ptr = entry->next; | |
e83fcabc | 246 | free (entry); |
50828ed8 | 247 | } |
248 | ||
1359ee7a | 249 | /* Call all node insertion hooks. */ |
50828ed8 | 250 | void |
251 | cgraph_call_function_insertion_hooks (struct cgraph_node *node) | |
252 | { | |
253 | struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook; | |
254 | while (entry) | |
255 | { | |
256 | entry->hook (node, entry->data); | |
257 | entry = entry->next; | |
258 | } | |
259 | } | |
260 | ||
dd1c9157 | 261 | /* Register HOOK to be called with DATA on each duplicated edge. */ |
262 | struct cgraph_2edge_hook_list * | |
263 | cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data) | |
264 | { | |
265 | struct cgraph_2edge_hook_list *entry; | |
266 | struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook; | |
267 | ||
268 | entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry)); | |
269 | entry->hook = hook; | |
270 | entry->data = data; | |
271 | entry->next = NULL; | |
272 | while (*ptr) | |
273 | ptr = &(*ptr)->next; | |
274 | *ptr = entry; | |
275 | return entry; | |
276 | } | |
277 | ||
278 | /* Remove ENTRY from the list of hooks called on duplicating edges. */ | |
279 | void | |
280 | cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry) | |
281 | { | |
282 | struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook; | |
283 | ||
284 | while (*ptr != entry) | |
285 | ptr = &(*ptr)->next; | |
286 | *ptr = entry->next; | |
e83fcabc | 287 | free (entry); |
dd1c9157 | 288 | } |
289 | ||
290 | /* Call all edge duplication hooks. */ | |
f41db742 | 291 | void |
dd1c9157 | 292 | cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1, |
293 | struct cgraph_edge *cs2) | |
294 | { | |
295 | struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook; | |
296 | while (entry) | |
297 | { | |
298 | entry->hook (cs1, cs2, entry->data); | |
299 | entry = entry->next; | |
300 | } | |
301 | } | |
302 | ||
303 | /* Register HOOK to be called with DATA on each duplicated node. */ | |
304 | struct cgraph_2node_hook_list * | |
305 | cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data) | |
306 | { | |
307 | struct cgraph_2node_hook_list *entry; | |
308 | struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook; | |
309 | ||
310 | entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry)); | |
311 | entry->hook = hook; | |
312 | entry->data = data; | |
313 | entry->next = NULL; | |
314 | while (*ptr) | |
315 | ptr = &(*ptr)->next; | |
316 | *ptr = entry; | |
317 | return entry; | |
318 | } | |
319 | ||
320 | /* Remove ENTRY from the list of hooks called on duplicating nodes. */ | |
321 | void | |
322 | cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry) | |
323 | { | |
324 | struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook; | |
325 | ||
326 | while (*ptr != entry) | |
327 | ptr = &(*ptr)->next; | |
328 | *ptr = entry->next; | |
e83fcabc | 329 | free (entry); |
dd1c9157 | 330 | } |
331 | ||
332 | /* Call all node duplication hooks. */ | |
ad687a96 | 333 | void |
dd1c9157 | 334 | cgraph_call_node_duplication_hooks (struct cgraph_node *node1, |
335 | struct cgraph_node *node2) | |
336 | { | |
337 | struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook; | |
338 | while (entry) | |
339 | { | |
340 | entry->hook (node1, node2, entry->data); | |
341 | entry = entry->next; | |
342 | } | |
343 | } | |
344 | ||
ed772161 | 345 | /* Allocate new callgraph node. */ |
a30b29a7 | 346 | |
ed772161 | 347 | static inline struct cgraph_node * |
348 | cgraph_allocate_node (void) | |
b0cdf642 | 349 | { |
350 | struct cgraph_node *node; | |
351 | ||
8325d2de | 352 | if (free_nodes) |
353 | { | |
354 | node = free_nodes; | |
355 | free_nodes = NEXT_FREE_NODE (node); | |
356 | } | |
357 | else | |
358 | { | |
ba72912a | 359 | node = ggc_alloc_cleared_cgraph_node (); |
8325d2de | 360 | node->uid = cgraph_max_uid++; |
361 | } | |
362 | ||
ed772161 | 363 | return node; |
364 | } | |
365 | ||
366 | /* Allocate new callgraph node and insert it into basic data structures. */ | |
367 | ||
f41db742 | 368 | struct cgraph_node * |
369 | cgraph_create_empty_node (void) | |
ed772161 | 370 | { |
371 | struct cgraph_node *node = cgraph_allocate_node (); | |
372 | ||
b6c89d32 | 373 | node->symbol.type = SYMTAB_FUNCTION; |
125b6d78 | 374 | node->frequency = NODE_FREQUENCY_NORMAL; |
c470c5a9 | 375 | node->count_materialization_scale = REG_BR_PROB_BASE; |
b0cdf642 | 376 | cgraph_n_nodes++; |
377 | return node; | |
378 | } | |
379 | ||
833eb724 | 380 | /* Return cgraph node assigned to DECL. Create new one when needed. */ |
a30b29a7 | 381 | |
ae01b312 | 382 | struct cgraph_node * |
5a90471f | 383 | cgraph_create_node (tree decl) |
833eb724 | 384 | { |
f41db742 | 385 | struct cgraph_node *node = cgraph_create_empty_node (); |
cc636d56 | 386 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL); |
639e4be4 | 387 | |
7d0d0ce1 | 388 | node->symbol.decl = decl; |
cfbe30aa | 389 | symtab_register_node ((symtab_node) node); |
390 | ||
964f7991 | 391 | if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL) |
833eb724 | 392 | { |
5a90471f | 393 | node->origin = cgraph_get_create_node (DECL_CONTEXT (decl)); |
833eb724 | 394 | node->next_nested = node->origin->nested; |
395 | node->origin->nested = node; | |
396 | } | |
397 | return node; | |
398 | } | |
399 | ||
5a90471f | 400 | /* Try to find a call graph node for declaration DECL and if it does not exist, |
401 | create it. */ | |
402 | ||
403 | struct cgraph_node * | |
404 | cgraph_get_create_node (tree decl) | |
405 | { | |
406 | struct cgraph_node *node; | |
407 | ||
408 | node = cgraph_get_node (decl); | |
409 | if (node) | |
410 | return node; | |
411 | ||
412 | return cgraph_create_node (decl); | |
413 | } | |
414 | ||
aa29908f | 415 | /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing |
416 | the function body is associated with (not neccesarily cgraph_node (DECL). */ | |
ed772161 | 417 | |
c70f46b0 | 418 | struct cgraph_node * |
419 | cgraph_create_function_alias (tree alias, tree decl) | |
ed772161 | 420 | { |
c70f46b0 | 421 | struct cgraph_node *alias_node; |
ed772161 | 422 | |
423 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL); | |
424 | gcc_assert (TREE_CODE (alias) == FUNCTION_DECL); | |
c70f46b0 | 425 | alias_node = cgraph_get_create_node (alias); |
426 | gcc_assert (!alias_node->local.finalized); | |
28454517 | 427 | alias_node->thunk.alias = decl; |
c70f46b0 | 428 | alias_node->local.finalized = true; |
429 | alias_node->alias = 1; | |
28454517 | 430 | return alias_node; |
431 | } | |
432 | ||
9ced88d0 | 433 | /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful |
5a90471f | 434 | and NULL otherwise. |
28454517 | 435 | Same body aliases are output whenever the body of DECL is output, |
5a90471f | 436 | and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL). */ |
28454517 | 437 | |
9ced88d0 | 438 | struct cgraph_node * |
c70f46b0 | 439 | cgraph_same_body_alias (struct cgraph_node *decl_node ATTRIBUTE_UNUSED, tree alias, tree decl) |
28454517 | 440 | { |
c70f46b0 | 441 | struct cgraph_node *n; |
28454517 | 442 | #ifndef ASM_OUTPUT_DEF |
443 | /* If aliases aren't supported by the assembler, fail. */ | |
9ced88d0 | 444 | return NULL; |
28454517 | 445 | #endif |
c70f46b0 | 446 | /* Langhooks can create same body aliases of symbols not defined. |
447 | Those are useless. Drop them on the floor. */ | |
448 | if (cgraph_global_info_ready) | |
449 | return NULL; | |
28454517 | 450 | |
c70f46b0 | 451 | n = cgraph_create_function_alias (alias, decl); |
452 | n->same_body_alias = true; | |
453 | if (same_body_aliases_done) | |
04ec15fa | 454 | ipa_record_reference ((symtab_node)n, (symtab_node)cgraph_get_node (decl), |
455 | IPA_REF_ALIAS, NULL); | |
c70f46b0 | 456 | return n; |
28454517 | 457 | } |
458 | ||
9ced88d0 | 459 | /* Add thunk alias into callgraph. The alias declaration is ALIAS and it |
0a10fd82 | 460 | aliases DECL with an adjustments made into the first parameter. |
9ced88d0 | 461 | See comments in thunk_adjust for detail on the parameters. */ |
462 | ||
463 | struct cgraph_node * | |
91bf9d9a | 464 | cgraph_add_thunk (struct cgraph_node *decl_node ATTRIBUTE_UNUSED, |
ff2a5ada | 465 | tree alias, tree decl ATTRIBUTE_UNUSED, |
aa29908f | 466 | bool this_adjusting, |
28454517 | 467 | HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value, |
468 | tree virtual_offset, | |
469 | tree real_alias) | |
470 | { | |
91bf9d9a | 471 | struct cgraph_node *node; |
28454517 | 472 | |
91bf9d9a | 473 | node = cgraph_get_node (alias); |
28454517 | 474 | if (node) |
475 | { | |
476 | gcc_assert (node->local.finalized); | |
74fa5f1c | 477 | gcc_assert (!node->alias); |
478 | gcc_assert (!node->thunk.thunk_p); | |
28454517 | 479 | cgraph_remove_node (node); |
480 | } | |
481 | ||
91bf9d9a | 482 | node = cgraph_create_node (alias); |
1b4345f7 | 483 | gcc_checking_assert (!virtual_offset |
a3f07b52 | 484 | || double_int_equal_p |
485 | (tree_to_double_int (virtual_offset), | |
486 | shwi_to_double_int (virtual_value))); | |
28454517 | 487 | node->thunk.fixed_offset = fixed_offset; |
488 | node->thunk.this_adjusting = this_adjusting; | |
489 | node->thunk.virtual_value = virtual_value; | |
490 | node->thunk.virtual_offset_p = virtual_offset != NULL; | |
491 | node->thunk.alias = real_alias; | |
492 | node->thunk.thunk_p = true; | |
91bf9d9a | 493 | node->local.finalized = true; |
494 | ||
9ced88d0 | 495 | return node; |
ed772161 | 496 | } |
497 | ||
8bc09a74 | 498 | /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME. |
499 | Return NULL if there's no such node. */ | |
500 | ||
501 | struct cgraph_node * | |
502 | cgraph_node_for_asm (tree asmname) | |
503 | { | |
cfbe30aa | 504 | symtab_node node = symtab_node_for_asm (asmname); |
8bc09a74 | 505 | |
cfbe30aa | 506 | /* We do not want to look at inline clones. */ |
507 | for (node = symtab_node_for_asm (asmname); node; node = node->symbol.next_sharing_asm_name) | |
508 | if (symtab_function_p (node) && !cgraph(node)->global.inlined_to) | |
509 | return cgraph (node); | |
8bc09a74 | 510 | return NULL; |
511 | } | |
512 | ||
8df22c5c | 513 | /* Returns a hash value for X (which really is a die_struct). */ |
514 | ||
515 | static hashval_t | |
516 | edge_hash (const void *x) | |
517 | { | |
aae87fc3 | 518 | return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt); |
8df22c5c | 519 | } |
520 | ||
521 | /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */ | |
522 | ||
523 | static int | |
524 | edge_eq (const void *x, const void *y) | |
525 | { | |
aae87fc3 | 526 | return ((const struct cgraph_edge *) x)->call_stmt == y; |
8df22c5c | 527 | } |
528 | ||
799c8711 | 529 | /* Add call graph edge E to call site hash of its caller. */ |
530 | ||
531 | static inline void | |
532 | cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e) | |
533 | { | |
534 | void **slot; | |
535 | slot = htab_find_slot_with_hash (e->caller->call_site_hash, | |
536 | e->call_stmt, | |
537 | htab_hash_pointer (e->call_stmt), | |
538 | INSERT); | |
539 | gcc_assert (!*slot); | |
540 | *slot = e; | |
541 | } | |
75a70cf9 | 542 | |
543 | /* Return the callgraph edge representing the GIMPLE_CALL statement | |
544 | CALL_STMT. */ | |
545 | ||
b0cdf642 | 546 | struct cgraph_edge * |
75a70cf9 | 547 | cgraph_edge (struct cgraph_node *node, gimple call_stmt) |
b0cdf642 | 548 | { |
8df22c5c | 549 | struct cgraph_edge *e, *e2; |
550 | int n = 0; | |
551 | ||
552 | if (node->call_site_hash) | |
364c0c59 | 553 | return (struct cgraph_edge *) |
554 | htab_find_with_hash (node->call_site_hash, call_stmt, | |
e0dc6f2b | 555 | htab_hash_pointer (call_stmt)); |
b0cdf642 | 556 | |
557 | /* This loop may turn out to be performance problem. In such case adding | |
558 | hashtables into call nodes with very many edges is probably best | |
9c9bad97 | 559 | solution. It is not good idea to add pointer into CALL_EXPR itself |
b0cdf642 | 560 | because we want to make possible having multiple cgraph nodes representing |
561 | different clones of the same body before the body is actually cloned. */ | |
799c8711 | 562 | for (e = node->callees; e; e = e->next_callee) |
8df22c5c | 563 | { |
564 | if (e->call_stmt == call_stmt) | |
565 | break; | |
566 | n++; | |
567 | } | |
75a70cf9 | 568 | |
799c8711 | 569 | if (!e) |
570 | for (e = node->indirect_calls; e; e = e->next_callee) | |
571 | { | |
572 | if (e->call_stmt == call_stmt) | |
573 | break; | |
574 | n++; | |
575 | } | |
576 | ||
8df22c5c | 577 | if (n > 100) |
578 | { | |
579 | node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL); | |
580 | for (e2 = node->callees; e2; e2 = e2->next_callee) | |
799c8711 | 581 | cgraph_add_edge_to_call_site_hash (e2); |
582 | for (e2 = node->indirect_calls; e2; e2 = e2->next_callee) | |
583 | cgraph_add_edge_to_call_site_hash (e2); | |
8df22c5c | 584 | } |
75a70cf9 | 585 | |
b0cdf642 | 586 | return e; |
587 | } | |
588 | ||
75a70cf9 | 589 | |
ccf4ab6b | 590 | /* Change field call_stmt of edge E to NEW_STMT. */ |
a30b29a7 | 591 | |
8df22c5c | 592 | void |
75a70cf9 | 593 | cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt) |
8df22c5c | 594 | { |
799c8711 | 595 | tree decl; |
596 | ||
8df22c5c | 597 | if (e->caller->call_site_hash) |
598 | { | |
599 | htab_remove_elt_with_hash (e->caller->call_site_hash, | |
600 | e->call_stmt, | |
601 | htab_hash_pointer (e->call_stmt)); | |
602 | } | |
799c8711 | 603 | |
8df22c5c | 604 | e->call_stmt = new_stmt; |
799c8711 | 605 | if (e->indirect_unknown_callee |
606 | && (decl = gimple_call_fndecl (new_stmt))) | |
607 | { | |
608 | /* Constant propagation (and possibly also inlining?) can turn an | |
609 | indirect call into a direct one. */ | |
5a90471f | 610 | struct cgraph_node *new_callee = cgraph_get_node (decl); |
799c8711 | 611 | |
5a90471f | 612 | gcc_checking_assert (new_callee); |
d4e80e2b | 613 | cgraph_make_edge_direct (e, new_callee); |
799c8711 | 614 | } |
615 | ||
7d0d0ce1 | 616 | push_cfun (DECL_STRUCT_FUNCTION (e->caller->symbol.decl)); |
17b28e52 | 617 | e->can_throw_external = stmt_can_throw_external (new_stmt); |
ecfab407 | 618 | pop_cfun (); |
8df22c5c | 619 | if (e->caller->call_site_hash) |
799c8711 | 620 | cgraph_add_edge_to_call_site_hash (e); |
8df22c5c | 621 | } |
622 | ||
799c8711 | 623 | /* Allocate a cgraph_edge structure and fill it with data according to the |
624 | parameters of which only CALLEE can be NULL (when creating an indirect call | |
625 | edge). */ | |
833eb724 | 626 | |
799c8711 | 627 | static struct cgraph_edge * |
628 | cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee, | |
0835ad03 | 629 | gimple call_stmt, gcov_type count, int freq) |
833eb724 | 630 | { |
e83fcabc | 631 | struct cgraph_edge *edge; |
b0cdf642 | 632 | |
7bfefa9d | 633 | /* LTO does not actually have access to the call_stmt since these |
634 | have not been loaded yet. */ | |
635 | if (call_stmt) | |
636 | { | |
0a10fd82 | 637 | /* This is a rather expensive check possibly triggering |
1b4345f7 | 638 | construction of call stmt hashtable. */ |
639 | gcc_checking_assert (!cgraph_edge (caller, call_stmt)); | |
b0cdf642 | 640 | |
7bfefa9d | 641 | gcc_assert (is_gimple_call (call_stmt)); |
642 | } | |
d7c6d889 | 643 | |
e83fcabc | 644 | if (free_edges) |
645 | { | |
646 | edge = free_edges; | |
647 | free_edges = NEXT_FREE_EDGE (edge); | |
648 | } | |
649 | else | |
650 | { | |
ba72912a | 651 | edge = ggc_alloc_cgraph_edge (); |
e83fcabc | 652 | edge->uid = cgraph_edge_max_uid++; |
653 | } | |
654 | ||
b0cdf642 | 655 | edge->aux = NULL; |
833eb724 | 656 | edge->caller = caller; |
657 | edge->callee = callee; | |
799c8711 | 658 | edge->prev_caller = NULL; |
659 | edge->next_caller = NULL; | |
660 | edge->prev_callee = NULL; | |
661 | edge->next_callee = NULL; | |
662 | ||
663 | edge->count = count; | |
664 | gcc_assert (count >= 0); | |
665 | edge->frequency = freq; | |
666 | gcc_assert (freq >= 0); | |
667 | gcc_assert (freq <= CGRAPH_FREQ_MAX); | |
799c8711 | 668 | |
9bfec7c2 | 669 | edge->call_stmt = call_stmt; |
7d0d0ce1 | 670 | push_cfun (DECL_STRUCT_FUNCTION (caller->symbol.decl)); |
0780673f | 671 | edge->can_throw_external |
672 | = call_stmt ? stmt_can_throw_external (call_stmt) : false; | |
ecfab407 | 673 | pop_cfun (); |
f883da84 | 674 | if (call_stmt |
7d0d0ce1 | 675 | && callee && callee->symbol.decl |
676 | && !gimple_check_call_matching_types (call_stmt, callee->symbol.decl)) | |
f883da84 | 677 | edge->call_stmt_cannot_inline_p = true; |
678 | else | |
679 | edge->call_stmt_cannot_inline_p = false; | |
799c8711 | 680 | if (call_stmt && caller->call_site_hash) |
681 | cgraph_add_edge_to_call_site_hash (edge); | |
682 | ||
683 | edge->indirect_info = NULL; | |
684 | edge->indirect_inlining_edge = 0; | |
685 | ||
686 | return edge; | |
687 | } | |
688 | ||
689 | /* Create edge from CALLER to CALLEE in the cgraph. */ | |
690 | ||
691 | struct cgraph_edge * | |
692 | cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee, | |
0835ad03 | 693 | gimple call_stmt, gcov_type count, int freq) |
799c8711 | 694 | { |
695 | struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt, | |
0835ad03 | 696 | count, freq); |
799c8711 | 697 | |
698 | edge->indirect_unknown_callee = 0; | |
699 | initialize_inline_failed (edge); | |
700 | ||
833eb724 | 701 | edge->next_caller = callee->callers; |
bb4c7a44 | 702 | if (callee->callers) |
703 | callee->callers->prev_caller = edge; | |
833eb724 | 704 | edge->next_callee = caller->callees; |
bb4c7a44 | 705 | if (caller->callees) |
706 | caller->callees->prev_callee = edge; | |
833eb724 | 707 | caller->callees = edge; |
708 | callee->callers = edge; | |
69428266 | 709 | |
799c8711 | 710 | return edge; |
711 | } | |
712 | ||
9bab6a70 | 713 | /* Allocate cgraph_indirect_call_info and set its fields to default values. */ |
714 | ||
715 | struct cgraph_indirect_call_info * | |
716 | cgraph_allocate_init_indirect_info (void) | |
717 | { | |
718 | struct cgraph_indirect_call_info *ii; | |
719 | ||
720 | ii = ggc_alloc_cleared_cgraph_indirect_call_info (); | |
721 | ii->param_index = -1; | |
722 | return ii; | |
723 | } | |
799c8711 | 724 | |
725 | /* Create an indirect edge with a yet-undetermined callee where the call | |
726 | statement destination is a formal parameter of the caller with index | |
727 | PARAM_INDEX. */ | |
728 | ||
729 | struct cgraph_edge * | |
730 | cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt, | |
f8b7e3ec | 731 | int ecf_flags, |
0835ad03 | 732 | gcov_type count, int freq) |
799c8711 | 733 | { |
734 | struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt, | |
0835ad03 | 735 | count, freq); |
799c8711 | 736 | |
737 | edge->indirect_unknown_callee = 1; | |
69428266 | 738 | initialize_inline_failed (edge); |
739 | ||
9bab6a70 | 740 | edge->indirect_info = cgraph_allocate_init_indirect_info (); |
f8b7e3ec | 741 | edge->indirect_info->ecf_flags = ecf_flags; |
799c8711 | 742 | |
743 | edge->next_callee = caller->indirect_calls; | |
744 | if (caller->indirect_calls) | |
745 | caller->indirect_calls->prev_callee = edge; | |
746 | caller->indirect_calls = edge; | |
747 | ||
833eb724 | 748 | return edge; |
749 | } | |
750 | ||
bb4c7a44 | 751 | /* Remove the edge E from the list of the callers of the callee. */ |
752 | ||
753 | static inline void | |
754 | cgraph_edge_remove_callee (struct cgraph_edge *e) | |
755 | { | |
799c8711 | 756 | gcc_assert (!e->indirect_unknown_callee); |
bb4c7a44 | 757 | if (e->prev_caller) |
758 | e->prev_caller->next_caller = e->next_caller; | |
759 | if (e->next_caller) | |
760 | e->next_caller->prev_caller = e->prev_caller; | |
761 | if (!e->prev_caller) | |
762 | e->callee->callers = e->next_caller; | |
763 | } | |
764 | ||
765 | /* Remove the edge E from the list of the callees of the caller. */ | |
766 | ||
767 | static inline void | |
768 | cgraph_edge_remove_caller (struct cgraph_edge *e) | |
769 | { | |
770 | if (e->prev_callee) | |
771 | e->prev_callee->next_callee = e->next_callee; | |
772 | if (e->next_callee) | |
773 | e->next_callee->prev_callee = e->prev_callee; | |
774 | if (!e->prev_callee) | |
799c8711 | 775 | { |
776 | if (e->indirect_unknown_callee) | |
777 | e->caller->indirect_calls = e->next_callee; | |
778 | else | |
779 | e->caller->callees = e->next_callee; | |
780 | } | |
8df22c5c | 781 | if (e->caller->call_site_hash) |
782 | htab_remove_elt_with_hash (e->caller->call_site_hash, | |
783 | e->call_stmt, | |
784 | htab_hash_pointer (e->call_stmt)); | |
bb4c7a44 | 785 | } |
786 | ||
e83fcabc | 787 | /* Put the edge onto the free list. */ |
788 | ||
789 | static void | |
790 | cgraph_free_edge (struct cgraph_edge *e) | |
791 | { | |
792 | int uid = e->uid; | |
793 | ||
794 | /* Clear out the edge so we do not dangle pointers. */ | |
74140efd | 795 | memset (e, 0, sizeof (*e)); |
e83fcabc | 796 | e->uid = uid; |
797 | NEXT_FREE_EDGE (e) = free_edges; | |
798 | free_edges = e; | |
799 | } | |
800 | ||
bb4c7a44 | 801 | /* Remove the edge E in the cgraph. */ |
833eb724 | 802 | |
4e570444 | 803 | void |
b0cdf642 | 804 | cgraph_remove_edge (struct cgraph_edge *e) |
833eb724 | 805 | { |
e83fcabc | 806 | /* Call all edge removal hooks. */ |
dd1c9157 | 807 | cgraph_call_edge_removal_hooks (e); |
e83fcabc | 808 | |
799c8711 | 809 | if (!e->indirect_unknown_callee) |
810 | /* Remove from callers list of the callee. */ | |
811 | cgraph_edge_remove_callee (e); | |
bb4c7a44 | 812 | |
813 | /* Remove from callees list of the callers. */ | |
814 | cgraph_edge_remove_caller (e); | |
e83fcabc | 815 | |
816 | /* Put the edge onto the free list. */ | |
817 | cgraph_free_edge (e); | |
833eb724 | 818 | } |
819 | ||
799c8711 | 820 | /* Set callee of call graph edge E and add it to the corresponding set of |
821 | callers. */ | |
822 | ||
823 | static void | |
824 | cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n) | |
825 | { | |
826 | e->prev_caller = NULL; | |
827 | if (n->callers) | |
828 | n->callers->prev_caller = e; | |
829 | e->next_caller = n->callers; | |
830 | n->callers = e; | |
831 | e->callee = n; | |
832 | } | |
833 | ||
b0cdf642 | 834 | /* Redirect callee of E to N. The function does not update underlying |
835 | call expression. */ | |
836 | ||
837 | void | |
838 | cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n) | |
839 | { | |
bb4c7a44 | 840 | /* Remove from callers list of the current callee. */ |
841 | cgraph_edge_remove_callee (e); | |
b0cdf642 | 842 | |
bb4c7a44 | 843 | /* Insert to callers list of the new callee. */ |
799c8711 | 844 | cgraph_set_edge_callee (e, n); |
845 | } | |
846 | ||
847 | /* Make an indirect EDGE with an unknown callee an ordinary edge leading to | |
9bab6a70 | 848 | CALLEE. DELTA is an integer constant that is to be added to the this |
849 | pointer (first parameter) to compensate for skipping a thunk adjustment. */ | |
799c8711 | 850 | |
851 | void | |
d4e80e2b | 852 | cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee) |
799c8711 | 853 | { |
854 | edge->indirect_unknown_callee = 0; | |
855 | ||
856 | /* Get the edge out of the indirect edge list. */ | |
857 | if (edge->prev_callee) | |
858 | edge->prev_callee->next_callee = edge->next_callee; | |
859 | if (edge->next_callee) | |
860 | edge->next_callee->prev_callee = edge->prev_callee; | |
861 | if (!edge->prev_callee) | |
862 | edge->caller->indirect_calls = edge->next_callee; | |
863 | ||
864 | /* Put it into the normal callee list */ | |
865 | edge->prev_callee = NULL; | |
866 | edge->next_callee = edge->caller->callees; | |
867 | if (edge->caller->callees) | |
868 | edge->caller->callees->prev_callee = edge; | |
869 | edge->caller->callees = edge; | |
870 | ||
871 | /* Insert to callers list of the new callee. */ | |
872 | cgraph_set_edge_callee (edge, callee); | |
873 | ||
f883da84 | 874 | if (edge->call_stmt) |
875 | edge->call_stmt_cannot_inline_p | |
7d0d0ce1 | 876 | = !gimple_check_call_matching_types (edge->call_stmt, callee->symbol.decl); |
a1942f0c | 877 | |
799c8711 | 878 | /* We need to re-determine the inlining status of the edge. */ |
879 | initialize_inline_failed (edge); | |
bb4c7a44 | 880 | } |
881 | ||
f41db742 | 882 | /* If necessary, change the function declaration in the call statement |
883 | associated with E so that it corresponds to the edge callee. */ | |
884 | ||
885 | gimple | |
886 | cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e) | |
887 | { | |
888 | tree decl = gimple_call_fndecl (e->call_stmt); | |
889 | gimple new_stmt; | |
890 | gimple_stmt_iterator gsi; | |
891 | #ifdef ENABLE_CHECKING | |
892 | struct cgraph_node *node; | |
893 | #endif | |
894 | ||
895 | if (e->indirect_unknown_callee | |
896 | || decl == e->callee->symbol.decl) | |
897 | return e->call_stmt; | |
898 | ||
899 | #ifdef ENABLE_CHECKING | |
900 | if (decl) | |
901 | { | |
902 | node = cgraph_get_node (decl); | |
903 | gcc_assert (!node || !node->clone.combined_args_to_skip); | |
904 | } | |
905 | #endif | |
906 | ||
907 | if (cgraph_dump_file) | |
908 | { | |
909 | fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ", | |
910 | xstrdup (cgraph_node_name (e->caller)), e->caller->uid, | |
911 | xstrdup (cgraph_node_name (e->callee)), e->callee->uid); | |
912 | print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags); | |
913 | if (e->callee->clone.combined_args_to_skip) | |
914 | { | |
915 | fprintf (cgraph_dump_file, " combined args to skip: "); | |
916 | dump_bitmap (cgraph_dump_file, | |
917 | e->callee->clone.combined_args_to_skip); | |
918 | } | |
919 | } | |
920 | ||
921 | if (e->callee->clone.combined_args_to_skip) | |
922 | { | |
923 | int lp_nr; | |
924 | ||
925 | new_stmt | |
926 | = gimple_call_copy_skip_args (e->call_stmt, | |
927 | e->callee->clone.combined_args_to_skip); | |
928 | gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl); | |
929 | ||
930 | if (gimple_vdef (new_stmt) | |
931 | && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME) | |
932 | SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt; | |
933 | ||
934 | gsi = gsi_for_stmt (e->call_stmt); | |
935 | gsi_replace (&gsi, new_stmt, false); | |
936 | /* We need to defer cleaning EH info on the new statement to | |
937 | fixup-cfg. We may not have dominator information at this point | |
938 | and thus would end up with unreachable blocks and have no way | |
939 | to communicate that we need to run CFG cleanup then. */ | |
940 | lp_nr = lookup_stmt_eh_lp (e->call_stmt); | |
941 | if (lp_nr != 0) | |
942 | { | |
943 | remove_stmt_from_eh_lp (e->call_stmt); | |
944 | add_stmt_to_eh_lp (new_stmt, lp_nr); | |
945 | } | |
946 | } | |
947 | else | |
948 | { | |
949 | new_stmt = e->call_stmt; | |
950 | gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl); | |
951 | update_stmt (new_stmt); | |
952 | } | |
953 | ||
954 | cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt); | |
955 | ||
956 | if (cgraph_dump_file) | |
957 | { | |
958 | fprintf (cgraph_dump_file, " updated to:"); | |
959 | print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags); | |
960 | } | |
961 | return new_stmt; | |
962 | } | |
75a70cf9 | 963 | |
964 | /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL | |
8d8c4f3e | 965 | OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl |
bb4322f9 | 966 | of OLD_STMT if it was previously call statement. |
967 | If NEW_STMT is NULL, the call has been dropped without any | |
968 | replacement. */ | |
117ef3d7 | 969 | |
ccf4ab6b | 970 | static void |
971 | cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node, | |
bb4322f9 | 972 | gimple old_stmt, tree old_call, |
973 | gimple new_stmt) | |
117ef3d7 | 974 | { |
bb4322f9 | 975 | tree new_call = (new_stmt && is_gimple_call (new_stmt)) |
976 | ? gimple_call_fndecl (new_stmt) : 0; | |
117ef3d7 | 977 | |
8d8c4f3e | 978 | /* We are seeing indirect calls, then there is nothing to update. */ |
979 | if (!new_call && !old_call) | |
980 | return; | |
981 | /* See if we turned indirect call into direct call or folded call to one builtin | |
0a10fd82 | 982 | into different builtin. */ |
117ef3d7 | 983 | if (old_call != new_call) |
984 | { | |
985 | struct cgraph_edge *e = cgraph_edge (node, old_stmt); | |
986 | struct cgraph_edge *ne = NULL; | |
8d8c4f3e | 987 | gcov_type count; |
988 | int frequency; | |
117ef3d7 | 989 | |
990 | if (e) | |
991 | { | |
799c8711 | 992 | /* See if the edge is already there and has the correct callee. It |
993 | might be so because of indirect inlining has already updated | |
b9ffeffb | 994 | it. We also might've cloned and redirected the edge. */ |
995 | if (new_call && e->callee) | |
996 | { | |
997 | struct cgraph_node *callee = e->callee; | |
998 | while (callee) | |
999 | { | |
7d0d0ce1 | 1000 | if (callee->symbol.decl == new_call |
b9ffeffb | 1001 | || callee->former_clone_of == new_call) |
1002 | return; | |
1003 | callee = callee->clone_of; | |
1004 | } | |
1005 | } | |
8d8c4f3e | 1006 | |
1007 | /* Otherwise remove edge and create new one; we can't simply redirect | |
1008 | since function has changed, so inline plan and other information | |
1009 | attached to edge is invalid. */ | |
8d8c4f3e | 1010 | count = e->count; |
1011 | frequency = e->frequency; | |
1c094d2f | 1012 | cgraph_remove_edge (e); |
8d8c4f3e | 1013 | } |
bb4322f9 | 1014 | else if (new_call) |
8d8c4f3e | 1015 | { |
1016 | /* We are seeing new direct call; compute profile info based on BB. */ | |
1017 | basic_block bb = gimple_bb (new_stmt); | |
1018 | count = bb->count; | |
1019 | frequency = compute_call_stmt_bb_frequency (current_function_decl, | |
1020 | bb); | |
117ef3d7 | 1021 | } |
117ef3d7 | 1022 | |
8d8c4f3e | 1023 | if (new_call) |
1024 | { | |
5a90471f | 1025 | ne = cgraph_create_edge (node, cgraph_get_create_node (new_call), |
0835ad03 | 1026 | new_stmt, count, frequency); |
8d8c4f3e | 1027 | gcc_assert (ne->inline_failed); |
1028 | } | |
117ef3d7 | 1029 | } |
8d8c4f3e | 1030 | /* We only updated the call stmt; update pointer in cgraph edge.. */ |
1031 | else if (old_stmt != new_stmt) | |
1032 | cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt); | |
117ef3d7 | 1033 | } |
1034 | ||
ccf4ab6b | 1035 | /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL |
8d8c4f3e | 1036 | OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl |
1037 | of OLD_STMT before it was updated (updating can happen inplace). */ | |
ccf4ab6b | 1038 | |
1039 | void | |
8d8c4f3e | 1040 | cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt) |
ccf4ab6b | 1041 | { |
5a90471f | 1042 | struct cgraph_node *orig = cgraph_get_node (cfun->decl); |
ccf4ab6b | 1043 | struct cgraph_node *node; |
1044 | ||
5a90471f | 1045 | gcc_checking_assert (orig); |
8d8c4f3e | 1046 | cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt); |
ccf4ab6b | 1047 | if (orig->clones) |
1048 | for (node = orig->clones; node != orig;) | |
1049 | { | |
8d8c4f3e | 1050 | cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt); |
ccf4ab6b | 1051 | if (node->clones) |
1052 | node = node->clones; | |
1053 | else if (node->next_sibling_clone) | |
1054 | node = node->next_sibling_clone; | |
1055 | else | |
1056 | { | |
1057 | while (node != orig && !node->next_sibling_clone) | |
1058 | node = node->clone_of; | |
1059 | if (node != orig) | |
1060 | node = node->next_sibling_clone; | |
1061 | } | |
1062 | } | |
1063 | } | |
1064 | ||
75a70cf9 | 1065 | |
bb4c7a44 | 1066 | /* Remove all callees from the node. */ |
1067 | ||
1068 | void | |
1069 | cgraph_node_remove_callees (struct cgraph_node *node) | |
1070 | { | |
74140efd | 1071 | struct cgraph_edge *e, *f; |
bb4c7a44 | 1072 | |
1073 | /* It is sufficient to remove the edges from the lists of callers of | |
1074 | the callees. The callee list of the node can be zapped with one | |
1075 | assignment. */ | |
74140efd | 1076 | for (e = node->callees; e; e = f) |
dd1c9157 | 1077 | { |
74140efd | 1078 | f = e->next_callee; |
dd1c9157 | 1079 | cgraph_call_edge_removal_hooks (e); |
799c8711 | 1080 | if (!e->indirect_unknown_callee) |
1081 | cgraph_edge_remove_callee (e); | |
e83fcabc | 1082 | cgraph_free_edge (e); |
dd1c9157 | 1083 | } |
f8b7e3ec | 1084 | for (e = node->indirect_calls; e; e = f) |
1085 | { | |
1086 | f = e->next_callee; | |
1087 | cgraph_call_edge_removal_hooks (e); | |
1088 | if (!e->indirect_unknown_callee) | |
1089 | cgraph_edge_remove_callee (e); | |
1090 | cgraph_free_edge (e); | |
1091 | } | |
1092 | node->indirect_calls = NULL; | |
bb4c7a44 | 1093 | node->callees = NULL; |
8df22c5c | 1094 | if (node->call_site_hash) |
1095 | { | |
1096 | htab_delete (node->call_site_hash); | |
1097 | node->call_site_hash = NULL; | |
1098 | } | |
bb4c7a44 | 1099 | } |
1100 | ||
1101 | /* Remove all callers from the node. */ | |
1102 | ||
1103 | static void | |
1104 | cgraph_node_remove_callers (struct cgraph_node *node) | |
1105 | { | |
74140efd | 1106 | struct cgraph_edge *e, *f; |
bb4c7a44 | 1107 | |
1108 | /* It is sufficient to remove the edges from the lists of callees of | |
1109 | the callers. The caller list of the node can be zapped with one | |
1110 | assignment. */ | |
74140efd | 1111 | for (e = node->callers; e; e = f) |
dd1c9157 | 1112 | { |
74140efd | 1113 | f = e->next_caller; |
dd1c9157 | 1114 | cgraph_call_edge_removal_hooks (e); |
1115 | cgraph_edge_remove_caller (e); | |
e83fcabc | 1116 | cgraph_free_edge (e); |
dd1c9157 | 1117 | } |
bb4c7a44 | 1118 | node->callers = NULL; |
b0cdf642 | 1119 | } |
1120 | ||
b62f482d | 1121 | /* Release memory used to represent body of function NODE. */ |
1122 | ||
1123 | void | |
1124 | cgraph_release_function_body (struct cgraph_node *node) | |
1125 | { | |
7d0d0ce1 | 1126 | if (DECL_STRUCT_FUNCTION (node->symbol.decl)) |
b62f482d | 1127 | { |
1128 | tree old_decl = current_function_decl; | |
7d0d0ce1 | 1129 | push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl)); |
79f958cb | 1130 | if (cfun->cfg |
1131 | && current_loops) | |
1132 | { | |
1133 | cfun->curr_properties &= ~PROP_loops; | |
1134 | loop_optimizer_finalize (); | |
1135 | } | |
5737913a | 1136 | if (cfun->gimple_df) |
1137 | { | |
7d0d0ce1 | 1138 | current_function_decl = node->symbol.decl; |
5737913a | 1139 | delete_tree_ssa (); |
1140 | delete_tree_cfg_annotations (); | |
1141 | cfun->eh = NULL; | |
1142 | current_function_decl = old_decl; | |
1143 | } | |
1144 | if (cfun->cfg) | |
1145 | { | |
1146 | gcc_assert (dom_computed[0] == DOM_NONE); | |
1147 | gcc_assert (dom_computed[1] == DOM_NONE); | |
1148 | clear_edges (); | |
1149 | } | |
bfb4e08d | 1150 | if (cfun->value_histograms) |
1151 | free_histograms (); | |
b62f482d | 1152 | pop_cfun(); |
7d0d0ce1 | 1153 | gimple_set_body (node->symbol.decl, NULL); |
5737913a | 1154 | VEC_free (ipa_opt_pass, heap, |
6d1cc52c | 1155 | node->ipa_transforms_to_apply); |
5737913a | 1156 | /* Struct function hangs a lot of data that would leak if we didn't |
1157 | removed all pointers to it. */ | |
7d0d0ce1 | 1158 | ggc_free (DECL_STRUCT_FUNCTION (node->symbol.decl)); |
1159 | DECL_STRUCT_FUNCTION (node->symbol.decl) = NULL; | |
b62f482d | 1160 | } |
7d0d0ce1 | 1161 | DECL_SAVED_TREE (node->symbol.decl) = NULL; |
d544ceff | 1162 | /* If the node is abstract and needed, then do not clear DECL_INITIAL |
1163 | of its associated function function declaration because it's | |
1164 | needed to emit debug info later. */ | |
1165 | if (!node->abstract_and_needed) | |
7d0d0ce1 | 1166 | DECL_INITIAL (node->symbol.decl) = error_mark_node; |
b62f482d | 1167 | } |
1168 | ||
961e3b13 | 1169 | /* Remove the node from cgraph. */ |
1170 | ||
1171 | void | |
eeba438a | 1172 | cgraph_remove_node (struct cgraph_node *node) |
961e3b13 | 1173 | { |
11b73810 | 1174 | struct cgraph_node *n; |
8325d2de | 1175 | int uid = node->uid; |
b0cdf642 | 1176 | |
dd1c9157 | 1177 | cgraph_call_node_removal_hooks (node); |
bb4c7a44 | 1178 | cgraph_node_remove_callers (node); |
1179 | cgraph_node_remove_callees (node); | |
6d1cc52c | 1180 | VEC_free (ipa_opt_pass, heap, |
1181 | node->ipa_transforms_to_apply); | |
28485874 | 1182 | |
f4ec5ce1 | 1183 | /* Incremental inlining access removed nodes stored in the postorder list. |
1184 | */ | |
da751785 | 1185 | node->symbol.force_output = false; |
11b73810 | 1186 | for (n = node->nested; n; n = n->next_nested) |
1187 | n->origin = NULL; | |
1188 | node->nested = NULL; | |
961e3b13 | 1189 | if (node->origin) |
1190 | { | |
1191 | struct cgraph_node **node2 = &node->origin->nested; | |
1192 | ||
1193 | while (*node2 != node) | |
1194 | node2 = &(*node2)->next_nested; | |
1195 | *node2 = node->next_nested; | |
1196 | } | |
0704fb2e | 1197 | symtab_unregister_node ((symtab_node)node); |
ccf4ab6b | 1198 | if (node->prev_sibling_clone) |
1199 | node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone; | |
1200 | else if (node->clone_of) | |
1201 | node->clone_of->clones = node->next_sibling_clone; | |
1202 | if (node->next_sibling_clone) | |
1203 | node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone; | |
1204 | if (node->clones) | |
b0cdf642 | 1205 | { |
ee3f5fc0 | 1206 | struct cgraph_node *n, *next; |
1207 | ||
1208 | if (node->clone_of) | |
1209 | { | |
1210 | for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone) | |
1211 | n->clone_of = node->clone_of; | |
1212 | n->clone_of = node->clone_of; | |
1213 | n->next_sibling_clone = node->clone_of->clones; | |
1214 | if (node->clone_of->clones) | |
1215 | node->clone_of->clones->prev_sibling_clone = n; | |
1216 | node->clone_of->clones = node->clones; | |
1217 | } | |
1218 | else | |
1219 | { | |
f41db742 | 1220 | /* We are removing node with clones. This makes clones inconsistent, |
ee3f5fc0 | 1221 | but assume they will be removed subsequently and just keep clone |
1222 | tree intact. This can happen in unreachable function removal since | |
1223 | we remove unreachable functions in random order, not by bottom-up | |
1224 | walk of clone trees. */ | |
1225 | for (n = node->clones; n; n = next) | |
1226 | { | |
1227 | next = n->next_sibling_clone; | |
1228 | n->next_sibling_clone = NULL; | |
1229 | n->prev_sibling_clone = NULL; | |
1230 | n->clone_of = NULL; | |
1231 | } | |
1232 | } | |
b0cdf642 | 1233 | } |
1234 | ||
a0c938f0 | 1235 | /* While all the clones are removed after being proceeded, the function |
5830087f | 1236 | itself is kept in the cgraph even after it is compiled. Check whether |
1237 | we are done with this body and reclaim it proactively if this is the case. | |
1238 | */ | |
cfbe30aa | 1239 | n = cgraph_get_node (node->symbol.decl); |
1240 | if (!n | |
1241 | || (!n->clones && !n->clone_of && !n->global.inlined_to | |
9e0baf4d | 1242 | && (cgraph_global_info_ready |
7d0d0ce1 | 1243 | && (TREE_ASM_WRITTEN (n->symbol.decl) |
1244 | || DECL_EXTERNAL (n->symbol.decl) | |
cfbe30aa | 1245 | || n->symbol.in_other_partition)))) |
b62f482d | 1246 | cgraph_release_function_body (node); |
cfbe30aa | 1247 | |
7d0d0ce1 | 1248 | node->symbol.decl = NULL; |
8df22c5c | 1249 | if (node->call_site_hash) |
1250 | { | |
1251 | htab_delete (node->call_site_hash); | |
1252 | node->call_site_hash = NULL; | |
1253 | } | |
b0cdf642 | 1254 | cgraph_n_nodes--; |
8325d2de | 1255 | |
1256 | /* Clear out the node to NULL all pointers and add the node to the free | |
1257 | list. */ | |
1258 | memset (node, 0, sizeof(*node)); | |
7d0d0ce1 | 1259 | node->symbol.type = SYMTAB_FUNCTION; |
8325d2de | 1260 | node->uid = uid; |
0704fb2e | 1261 | SET_NEXT_FREE_NODE (node, free_nodes); |
8325d2de | 1262 | free_nodes = node; |
961e3b13 | 1263 | } |
1264 | ||
2cb64f78 | 1265 | /* Likewise indicate that a node is having address taken. */ |
1266 | ||
1267 | void | |
1268 | cgraph_mark_address_taken_node (struct cgraph_node *node) | |
1269 | { | |
7f74ac6b | 1270 | gcc_assert (!node->global.inlined_to); |
c70f46b0 | 1271 | /* FIXME: address_taken flag is used both as a shortcut for testing whether |
1272 | IPA_REF_ADDR reference exists (and thus it should be set on node | |
1273 | representing alias we take address of) and as a test whether address | |
1274 | of the object was taken (and thus it should be set on node alias is | |
1275 | referring to). We should remove the first use and the remove the | |
1276 | following set. */ | |
7d0d0ce1 | 1277 | node->symbol.address_taken = 1; |
c70f46b0 | 1278 | node = cgraph_function_or_thunk_node (node, NULL); |
7d0d0ce1 | 1279 | node->symbol.address_taken = 1; |
2cb64f78 | 1280 | } |
1281 | ||
80a85d8a | 1282 | /* Return local info for the compiled function. */ |
1283 | ||
1284 | struct cgraph_local_info * | |
eeba438a | 1285 | cgraph_local_info (tree decl) |
80a85d8a | 1286 | { |
1287 | struct cgraph_node *node; | |
a0c938f0 | 1288 | |
cc636d56 | 1289 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL); |
924de091 | 1290 | node = cgraph_get_node (decl); |
1291 | if (!node) | |
1292 | return NULL; | |
80a85d8a | 1293 | return &node->local; |
1294 | } | |
1295 | ||
1296 | /* Return local info for the compiled function. */ | |
1297 | ||
1298 | struct cgraph_global_info * | |
eeba438a | 1299 | cgraph_global_info (tree decl) |
80a85d8a | 1300 | { |
1301 | struct cgraph_node *node; | |
a0c938f0 | 1302 | |
cc636d56 | 1303 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready); |
924de091 | 1304 | node = cgraph_get_node (decl); |
1305 | if (!node) | |
1306 | return NULL; | |
80a85d8a | 1307 | return &node->global; |
1308 | } | |
1309 | ||
28992b23 | 1310 | /* Return local info for the compiled function. */ |
1311 | ||
1312 | struct cgraph_rtl_info * | |
eeba438a | 1313 | cgraph_rtl_info (tree decl) |
28992b23 | 1314 | { |
1315 | struct cgraph_node *node; | |
a0c938f0 | 1316 | |
cc636d56 | 1317 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL); |
924de091 | 1318 | node = cgraph_get_node (decl); |
1319 | if (!node | |
1320 | || (decl != current_function_decl | |
7d0d0ce1 | 1321 | && !TREE_ASM_WRITTEN (node->symbol.decl))) |
28992b23 | 1322 | return NULL; |
1323 | return &node->rtl; | |
1324 | } | |
1325 | ||
326a9581 | 1326 | /* Return a string describing the failure REASON. */ |
1327 | ||
1328 | const char* | |
1329 | cgraph_inline_failed_string (cgraph_inline_failed_t reason) | |
1330 | { | |
1331 | #undef DEFCIFCODE | |
1332 | #define DEFCIFCODE(code, string) string, | |
1333 | ||
1334 | static const char *cif_string_table[CIF_N_REASONS] = { | |
1335 | #include "cif-code.def" | |
1336 | }; | |
1337 | ||
1338 | /* Signedness of an enum type is implementation defined, so cast it | |
1339 | to unsigned before testing. */ | |
1340 | gcc_assert ((unsigned) reason < CIF_N_REASONS); | |
1341 | return cif_string_table[reason]; | |
1342 | } | |
1343 | ||
e1be32b8 | 1344 | /* Names used to print out the availability enum. */ |
1d416bd7 | 1345 | const char * const cgraph_availability_names[] = |
f0b5f617 | 1346 | {"unset", "not_available", "overwritable", "available", "local"}; |
e1be32b8 | 1347 | |
e83c4efa | 1348 | |
1349 | /* Dump call graph node NODE to file F. */ | |
1350 | ||
b0cdf642 | 1351 | void |
1352 | dump_cgraph_node (FILE *f, struct cgraph_node *node) | |
1353 | { | |
1354 | struct cgraph_edge *edge; | |
799c8711 | 1355 | int indirect_calls_count = 0; |
1356 | ||
18841b0c | 1357 | dump_symtab_base (f, (symtab_node) node); |
1358 | ||
b0cdf642 | 1359 | if (node->global.inlined_to) |
18841b0c | 1360 | fprintf (f, " Function %s/%i is inline copy in %s/%i\n", |
a690dc32 | 1361 | xstrdup (cgraph_node_name (node)), |
18841b0c | 1362 | node->symbol.order, |
a690dc32 | 1363 | xstrdup (cgraph_node_name (node->global.inlined_to)), |
18841b0c | 1364 | node->global.inlined_to->symbol.order); |
ccf4ab6b | 1365 | if (node->clone_of) |
18841b0c | 1366 | fprintf (f, " Clone of %s/%i\n", |
1367 | cgraph_node_asm_name (node->clone_of), | |
1368 | node->clone_of->symbol.order); | |
e1be32b8 | 1369 | if (cgraph_function_flags_ready) |
18841b0c | 1370 | fprintf (f, " Availability: %s\n", |
1d416bd7 | 1371 | cgraph_availability_names [cgraph_function_body_availability (node)]); |
18841b0c | 1372 | |
1373 | fprintf (f, " Function flags:"); | |
08843223 | 1374 | if (node->analyzed) |
1375 | fprintf (f, " analyzed"); | |
edc6a4c0 | 1376 | if (node->count) |
1377 | fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x", | |
1378 | (HOST_WIDEST_INT)node->count); | |
b0cdf642 | 1379 | if (node->origin) |
18841b0c | 1380 | fprintf (f, " nested in: %s", cgraph_node_asm_name (node->origin)); |
7d0d0ce1 | 1381 | if (gimple_has_body_p (node->symbol.decl)) |
75a70cf9 | 1382 | fprintf (f, " body"); |
09fc9532 | 1383 | if (node->process) |
1384 | fprintf (f, " process"); | |
b0cdf642 | 1385 | if (node->local.local) |
1386 | fprintf (f, " local"); | |
3f82b628 | 1387 | if (node->local.finalized) |
1388 | fprintf (f, " finalized"); | |
3f82b628 | 1389 | if (node->local.redefined_extern_inline) |
1390 | fprintf (f, " redefined_extern_inline"); | |
0f9fb931 | 1391 | if (node->only_called_at_startup) |
1392 | fprintf (f, " only_called_at_startup"); | |
1393 | if (node->only_called_at_exit) | |
1394 | fprintf (f, " only_called_at_exit"); | |
badeded8 | 1395 | else if (node->alias) |
1396 | fprintf (f, " alias"); | |
4c0315d0 | 1397 | if (node->tm_clone) |
1398 | fprintf (f, " tm_clone"); | |
b0cdf642 | 1399 | |
91bf9d9a | 1400 | fprintf (f, "\n"); |
1401 | ||
1402 | if (node->thunk.thunk_p) | |
1403 | { | |
18841b0c | 1404 | fprintf (f, " Thunk of %s (asm: %s) fixed offset %i virtual value %i has " |
91bf9d9a | 1405 | "virtual offset %i)\n", |
1406 | lang_hooks.decl_printable_name (node->thunk.alias, 2), | |
1407 | IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)), | |
1408 | (int)node->thunk.fixed_offset, | |
1409 | (int)node->thunk.virtual_value, | |
1410 | (int)node->thunk.virtual_offset_p); | |
1411 | } | |
c70f46b0 | 1412 | if (node->alias && node->thunk.alias) |
1413 | { | |
18841b0c | 1414 | fprintf (f, " Alias of %s", |
c70f46b0 | 1415 | lang_hooks.decl_printable_name (node->thunk.alias, 2)); |
1416 | if (DECL_ASSEMBLER_NAME_SET_P (node->thunk.alias)) | |
1417 | fprintf (f, " (asm: %s)", | |
1418 | IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias))); | |
1419 | fprintf (f, "\n"); | |
1420 | } | |
91bf9d9a | 1421 | |
18841b0c | 1422 | fprintf (f, " Called by: "); |
91bf9d9a | 1423 | |
b0cdf642 | 1424 | for (edge = node->callers; edge; edge = edge->next_caller) |
1425 | { | |
18841b0c | 1426 | fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->caller), |
1427 | edge->caller->symbol.order); | |
edc6a4c0 | 1428 | if (edge->count) |
1429 | fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ", | |
1430 | (HOST_WIDEST_INT)edge->count); | |
4ae20857 | 1431 | if (edge->frequency) |
1432 | fprintf (f, "(%.2f per call) ", | |
1433 | edge->frequency / (double)CGRAPH_FREQ_BASE); | |
b0cdf642 | 1434 | if (!edge->inline_failed) |
1435 | fprintf(f, "(inlined) "); | |
799c8711 | 1436 | if (edge->indirect_inlining_edge) |
1437 | fprintf(f, "(indirect_inlining) "); | |
17b28e52 | 1438 | if (edge->can_throw_external) |
1439 | fprintf(f, "(can throw external) "); | |
b0cdf642 | 1440 | } |
1441 | ||
18841b0c | 1442 | fprintf (f, "\n Calls: "); |
b0cdf642 | 1443 | for (edge = node->callees; edge; edge = edge->next_callee) |
1444 | { | |
18841b0c | 1445 | fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->callee), |
1446 | edge->callee->symbol.order); | |
b0cdf642 | 1447 | if (!edge->inline_failed) |
1448 | fprintf(f, "(inlined) "); | |
799c8711 | 1449 | if (edge->indirect_inlining_edge) |
1450 | fprintf(f, "(indirect_inlining) "); | |
e1be32b8 | 1451 | if (edge->count) |
1452 | fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ", | |
1453 | (HOST_WIDEST_INT)edge->count); | |
4ae20857 | 1454 | if (edge->frequency) |
1455 | fprintf (f, "(%.2f per call) ", | |
1456 | edge->frequency / (double)CGRAPH_FREQ_BASE); | |
ecfab407 | 1457 | if (edge->can_throw_external) |
1458 | fprintf(f, "(can throw external) "); | |
b0cdf642 | 1459 | } |
1460 | fprintf (f, "\n"); | |
28454517 | 1461 | |
799c8711 | 1462 | for (edge = node->indirect_calls; edge; edge = edge->next_callee) |
1463 | indirect_calls_count++; | |
1464 | if (indirect_calls_count) | |
18841b0c | 1465 | fprintf (f, " Has %i outgoing edges for indirect calls.\n", |
799c8711 | 1466 | indirect_calls_count); |
b0cdf642 | 1467 | } |
1468 | ||
e83c4efa | 1469 | |
1470 | /* Dump call graph node NODE to stderr. */ | |
1471 | ||
4b987fac | 1472 | DEBUG_FUNCTION void |
e83c4efa | 1473 | debug_cgraph_node (struct cgraph_node *node) |
1474 | { | |
1475 | dump_cgraph_node (stderr, node); | |
1476 | } | |
1477 | ||
1478 | ||
1479 | /* Dump the callgraph to file F. */ | |
833eb724 | 1480 | |
1481 | void | |
eeba438a | 1482 | dump_cgraph (FILE *f) |
833eb724 | 1483 | { |
1484 | struct cgraph_node *node; | |
1485 | ||
e4200070 | 1486 | fprintf (f, "callgraph:\n\n"); |
7c455d87 | 1487 | FOR_EACH_FUNCTION (node) |
b0cdf642 | 1488 | dump_cgraph_node (f, node); |
833eb724 | 1489 | } |
639e4be4 | 1490 | |
e83c4efa | 1491 | |
1492 | /* Dump the call graph to stderr. */ | |
1493 | ||
4b987fac | 1494 | DEBUG_FUNCTION void |
e83c4efa | 1495 | debug_cgraph (void) |
1496 | { | |
1497 | dump_cgraph (stderr); | |
1498 | } | |
1499 | ||
5bd74231 | 1500 | /* Return true when the DECL can possibly be inlined. */ |
1501 | bool | |
1502 | cgraph_function_possibly_inlined_p (tree decl) | |
1503 | { | |
5bd74231 | 1504 | if (!cgraph_global_info_ready) |
1c2f0012 | 1505 | return !DECL_UNINLINABLE (decl); |
e1232ce2 | 1506 | return DECL_POSSIBLY_INLINED (decl); |
b0cdf642 | 1507 | } |
1508 | ||
9d95b2b0 | 1509 | /* NODE is no longer nested function; update cgraph accordingly. */ |
1510 | void | |
1511 | cgraph_unnest_node (struct cgraph_node *node) | |
1512 | { | |
1513 | struct cgraph_node **node2 = &node->origin->nested; | |
1514 | gcc_assert (node->origin); | |
1515 | ||
1516 | while (*node2 != node) | |
1517 | node2 = &(*node2)->next_nested; | |
1518 | *node2 = node->next_nested; | |
1519 | node->origin = NULL; | |
1520 | } | |
e1be32b8 | 1521 | |
1522 | /* Return function availability. See cgraph.h for description of individual | |
1523 | return values. */ | |
1524 | enum availability | |
1525 | cgraph_function_body_availability (struct cgraph_node *node) | |
1526 | { | |
1527 | enum availability avail; | |
1528 | gcc_assert (cgraph_function_flags_ready); | |
79a0f5ea | 1529 | if (!node->analyzed) |
e1be32b8 | 1530 | avail = AVAIL_NOT_AVAILABLE; |
1531 | else if (node->local.local) | |
1532 | avail = AVAIL_LOCAL; | |
7d0d0ce1 | 1533 | else if (!node->symbol.externally_visible) |
e1be32b8 | 1534 | avail = AVAIL_AVAILABLE; |
0a10fd82 | 1535 | /* Inline functions are safe to be analyzed even if their symbol can |
1536 | be overwritten at runtime. It is not meaningful to enforce any sane | |
cc76f102 | 1537 | behaviour on replacing inline function by different body. */ |
7d0d0ce1 | 1538 | else if (DECL_DECLARED_INLINE_P (node->symbol.decl)) |
cc76f102 | 1539 | avail = AVAIL_AVAILABLE; |
e1be32b8 | 1540 | |
1541 | /* If the function can be overwritten, return OVERWRITABLE. Take | |
1542 | care at least of two notable extensions - the COMDAT functions | |
1543 | used to share template instantiations in C++ (this is symmetric | |
1544 | to code cp_cannot_inline_tree_fn and probably shall be shared and | |
ecda6e51 | 1545 | the inlinability hooks completely eliminated). |
e1be32b8 | 1546 | |
1547 | ??? Does the C++ one definition rule allow us to always return | |
1548 | AVAIL_AVAILABLE here? That would be good reason to preserve this | |
cc76f102 | 1549 | bit. */ |
1550 | ||
7d0d0ce1 | 1551 | else if (decl_replaceable_p (node->symbol.decl) |
1552 | && !DECL_EXTERNAL (node->symbol.decl)) | |
e1be32b8 | 1553 | avail = AVAIL_OVERWRITABLE; |
1554 | else avail = AVAIL_AVAILABLE; | |
1555 | ||
1556 | return avail; | |
1557 | } | |
1558 | ||
74fa5f1c | 1559 | /* Worker for cgraph_node_can_be_local_p. */ |
1560 | static bool | |
1561 | cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node, | |
1562 | void *data ATTRIBUTE_UNUSED) | |
1563 | { | |
8efa224a | 1564 | return !(!node->symbol.force_output |
7d0d0ce1 | 1565 | && ((DECL_COMDAT (node->symbol.decl) |
1566 | && !node->symbol.same_comdat_group) | |
1567 | || !node->symbol.externally_visible)); | |
74fa5f1c | 1568 | } |
1569 | ||
9967e578 | 1570 | /* Return true if NODE can be made local for API change. |
1571 | Extern inline functions and C++ COMDAT functions can be made local | |
1572 | at the expense of possible code size growth if function is used in multiple | |
1573 | compilation units. */ | |
1574 | bool | |
1575 | cgraph_node_can_be_local_p (struct cgraph_node *node) | |
1576 | { | |
7d0d0ce1 | 1577 | return (!node->symbol.address_taken |
74fa5f1c | 1578 | && !cgraph_for_node_and_aliases (node, |
1579 | cgraph_node_cannot_be_local_p_1, | |
1580 | NULL, true)); | |
9967e578 | 1581 | } |
1582 | ||
74fa5f1c | 1583 | /* Call calback on NODE, thunks and aliases asociated to NODE. |
1584 | When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are | |
1585 | skipped. */ | |
1586 | ||
1587 | bool | |
1588 | cgraph_for_node_thunks_and_aliases (struct cgraph_node *node, | |
1589 | bool (*callback) (struct cgraph_node *, void *), | |
1590 | void *data, | |
1591 | bool include_overwritable) | |
1592 | { | |
1593 | struct cgraph_edge *e; | |
c70f46b0 | 1594 | int i; |
1595 | struct ipa_ref *ref; | |
74fa5f1c | 1596 | |
1597 | if (callback (node, data)) | |
1598 | return true; | |
74fa5f1c | 1599 | for (e = node->callers; e; e = e->next_caller) |
1600 | if (e->caller->thunk.thunk_p | |
1601 | && (include_overwritable | |
badeded8 | 1602 | || cgraph_function_body_availability (e->caller) > AVAIL_OVERWRITABLE)) |
794fd282 | 1603 | if (cgraph_for_node_thunks_and_aliases (e->caller, callback, data, |
1604 | include_overwritable)) | |
1605 | return true; | |
04ec15fa | 1606 | for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++) |
c70f46b0 | 1607 | if (ref->use == IPA_REF_ALIAS) |
1608 | { | |
04ec15fa | 1609 | struct cgraph_node *alias = ipa_ref_referring_node (ref); |
c70f46b0 | 1610 | if (include_overwritable |
1611 | || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE) | |
794fd282 | 1612 | if (cgraph_for_node_thunks_and_aliases (alias, callback, data, |
1613 | include_overwritable)) | |
1614 | return true; | |
c70f46b0 | 1615 | } |
74fa5f1c | 1616 | return false; |
1617 | } | |
1618 | ||
1619 | /* Call calback on NODE and aliases asociated to NODE. | |
1620 | When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are | |
1621 | skipped. */ | |
1622 | ||
1623 | bool | |
1624 | cgraph_for_node_and_aliases (struct cgraph_node *node, | |
1625 | bool (*callback) (struct cgraph_node *, void *), | |
1626 | void *data, | |
c70f46b0 | 1627 | bool include_overwritable) |
9967e578 | 1628 | { |
c70f46b0 | 1629 | int i; |
1630 | struct ipa_ref *ref; | |
74fa5f1c | 1631 | |
1632 | if (callback (node, data)) | |
1633 | return true; | |
04ec15fa | 1634 | for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++) |
c70f46b0 | 1635 | if (ref->use == IPA_REF_ALIAS) |
1636 | { | |
04ec15fa | 1637 | struct cgraph_node *alias = ipa_ref_referring_node (ref); |
c70f46b0 | 1638 | if (include_overwritable |
1639 | || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE) | |
794fd282 | 1640 | if (cgraph_for_node_and_aliases (alias, callback, data, |
1641 | include_overwritable)) | |
1642 | return true; | |
c70f46b0 | 1643 | } |
74fa5f1c | 1644 | return false; |
1645 | } | |
1646 | ||
1647 | /* Worker to bring NODE local. */ | |
1648 | ||
1649 | static bool | |
1650 | cgraph_make_node_local_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED) | |
1651 | { | |
1652 | gcc_checking_assert (cgraph_node_can_be_local_p (node)); | |
7d0d0ce1 | 1653 | if (DECL_COMDAT (node->symbol.decl) || DECL_EXTERNAL (node->symbol.decl)) |
9967e578 | 1654 | { |
cf951b1a | 1655 | symtab_make_decl_local (node->symbol.decl); |
6137cc9f | 1656 | |
7d0d0ce1 | 1657 | node->symbol.externally_visible = false; |
9967e578 | 1658 | node->local.local = true; |
7d0d0ce1 | 1659 | node->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY; |
9967e578 | 1660 | gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL); |
1661 | } | |
74fa5f1c | 1662 | return false; |
9967e578 | 1663 | } |
1664 | ||
74fa5f1c | 1665 | /* Bring NODE local. */ |
1666 | ||
1667 | void | |
1668 | cgraph_make_node_local (struct cgraph_node *node) | |
1669 | { | |
1670 | cgraph_for_node_thunks_and_aliases (node, cgraph_make_node_local_1, | |
1671 | NULL, true); | |
1672 | } | |
1673 | ||
1674 | /* Worker to set nothrow flag. */ | |
1675 | ||
1676 | static bool | |
1677 | cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data) | |
1678 | { | |
8c1fce46 | 1679 | struct cgraph_edge *e; |
1680 | ||
7d0d0ce1 | 1681 | TREE_NOTHROW (node->symbol.decl) = data != NULL; |
8c1fce46 | 1682 | |
1683 | if (data != NULL) | |
1684 | for (e = node->callers; e; e = e->next_caller) | |
1685 | e->can_throw_external = false; | |
74fa5f1c | 1686 | return false; |
1687 | } | |
1688 | ||
1689 | /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE | |
16fcf0f4 | 1690 | if any to NOTHROW. */ |
1691 | ||
1692 | void | |
1693 | cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow) | |
1694 | { | |
74fa5f1c | 1695 | cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1, |
1696 | (void *)(size_t)nothrow, false); | |
16fcf0f4 | 1697 | } |
1698 | ||
74fa5f1c | 1699 | /* Worker to set const flag. */ |
16fcf0f4 | 1700 | |
74fa5f1c | 1701 | static bool |
1702 | cgraph_set_const_flag_1 (struct cgraph_node *node, void *data) | |
16fcf0f4 | 1703 | { |
7f74ac6b | 1704 | /* Static constructors and destructors without a side effect can be |
1705 | optimized out. */ | |
74fa5f1c | 1706 | if (data && !((size_t)data & 2)) |
7f74ac6b | 1707 | { |
7d0d0ce1 | 1708 | if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)) |
1709 | DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0; | |
1710 | if (DECL_STATIC_DESTRUCTOR (node->symbol.decl)) | |
1711 | DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0; | |
7f74ac6b | 1712 | } |
7d0d0ce1 | 1713 | TREE_READONLY (node->symbol.decl) = data != NULL; |
1714 | DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0; | |
74fa5f1c | 1715 | return false; |
16fcf0f4 | 1716 | } |
1717 | ||
74fa5f1c | 1718 | /* Set TREE_READONLY on NODE's decl and on aliases of NODE |
1719 | if any to READONLY. */ | |
16fcf0f4 | 1720 | |
1721 | void | |
74fa5f1c | 1722 | cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping) |
16fcf0f4 | 1723 | { |
74fa5f1c | 1724 | cgraph_for_node_thunks_and_aliases (node, cgraph_set_const_flag_1, |
1725 | (void *)(size_t)(readonly + (int)looping * 2), | |
1726 | false); | |
1727 | } | |
1728 | ||
1729 | /* Worker to set pure flag. */ | |
1730 | ||
1731 | static bool | |
1732 | cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data) | |
1733 | { | |
1734 | /* Static pureructors and destructors without a side effect can be | |
7f74ac6b | 1735 | optimized out. */ |
74fa5f1c | 1736 | if (data && !((size_t)data & 2)) |
7f74ac6b | 1737 | { |
7d0d0ce1 | 1738 | if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)) |
1739 | DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0; | |
1740 | if (DECL_STATIC_DESTRUCTOR (node->symbol.decl)) | |
1741 | DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0; | |
7f74ac6b | 1742 | } |
7d0d0ce1 | 1743 | DECL_PURE_P (node->symbol.decl) = data != NULL; |
1744 | DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0; | |
74fa5f1c | 1745 | return false; |
16fcf0f4 | 1746 | } |
1747 | ||
74fa5f1c | 1748 | /* Set DECL_PURE_P on NODE's decl and on aliases of NODE |
1749 | if any to PURE. */ | |
1750 | ||
1751 | void | |
1752 | cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping) | |
5ea60108 | 1753 | { |
74fa5f1c | 1754 | cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1, |
1755 | (void *)(size_t)(pure + (int)looping * 2), | |
1756 | false); | |
1757 | } | |
0f9fb931 | 1758 | |
74fa5f1c | 1759 | /* Data used by cgraph_propagate_frequency. */ |
0f9fb931 | 1760 | |
74fa5f1c | 1761 | struct cgraph_propagate_frequency_data |
1762 | { | |
1763 | bool maybe_unlikely_executed; | |
1764 | bool maybe_executed_once; | |
1765 | bool only_called_at_startup; | |
1766 | bool only_called_at_exit; | |
1767 | }; | |
1768 | ||
1769 | /* Worker for cgraph_propagate_frequency_1. */ | |
1770 | ||
1771 | static bool | |
1772 | cgraph_propagate_frequency_1 (struct cgraph_node *node, void *data) | |
1773 | { | |
1774 | struct cgraph_propagate_frequency_data *d; | |
1775 | struct cgraph_edge *edge; | |
1776 | ||
1777 | d = (struct cgraph_propagate_frequency_data *)data; | |
5ea60108 | 1778 | for (edge = node->callers; |
74fa5f1c | 1779 | edge && (d->maybe_unlikely_executed || d->maybe_executed_once |
1780 | || d->only_called_at_startup || d->only_called_at_exit); | |
5ea60108 | 1781 | edge = edge->next_caller) |
1782 | { | |
0f9fb931 | 1783 | if (edge->caller != node) |
1784 | { | |
74fa5f1c | 1785 | d->only_called_at_startup &= edge->caller->only_called_at_startup; |
0a10fd82 | 1786 | /* It makes sense to put main() together with the static constructors. |
0f9fb931 | 1787 | It will be executed for sure, but rest of functions called from |
0a10fd82 | 1788 | main are definitely not at startup only. */ |
7d0d0ce1 | 1789 | if (MAIN_NAME_P (DECL_NAME (edge->caller->symbol.decl))) |
74fa5f1c | 1790 | d->only_called_at_startup = 0; |
1791 | d->only_called_at_exit &= edge->caller->only_called_at_exit; | |
0f9fb931 | 1792 | } |
5ea60108 | 1793 | if (!edge->frequency) |
1794 | continue; | |
1795 | switch (edge->caller->frequency) | |
1796 | { | |
1797 | case NODE_FREQUENCY_UNLIKELY_EXECUTED: | |
1798 | break; | |
1799 | case NODE_FREQUENCY_EXECUTED_ONCE: | |
1800 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
0f9fb931 | 1801 | fprintf (dump_file, " Called by %s that is executed once\n", |
aa29908f | 1802 | cgraph_node_name (edge->caller)); |
74fa5f1c | 1803 | d->maybe_unlikely_executed = false; |
0835ad03 | 1804 | if (inline_edge_summary (edge)->loop_depth) |
5ea60108 | 1805 | { |
74fa5f1c | 1806 | d->maybe_executed_once = false; |
5ea60108 | 1807 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1808 | fprintf (dump_file, " Called in loop\n"); | |
1809 | } | |
1810 | break; | |
1811 | case NODE_FREQUENCY_HOT: | |
1812 | case NODE_FREQUENCY_NORMAL: | |
1813 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
0f9fb931 | 1814 | fprintf (dump_file, " Called by %s that is normal or hot\n", |
aa29908f | 1815 | cgraph_node_name (edge->caller)); |
74fa5f1c | 1816 | d->maybe_unlikely_executed = false; |
1817 | d->maybe_executed_once = false; | |
5ea60108 | 1818 | break; |
1819 | } | |
1820 | } | |
74fa5f1c | 1821 | return edge != NULL; |
1822 | } | |
1823 | ||
1824 | /* See if the frequency of NODE can be updated based on frequencies of its | |
1825 | callers. */ | |
1826 | bool | |
1827 | cgraph_propagate_frequency (struct cgraph_node *node) | |
1828 | { | |
1829 | struct cgraph_propagate_frequency_data d = {true, true, true, true}; | |
1830 | bool changed = false; | |
1831 | ||
1832 | if (!node->local.local) | |
1833 | return false; | |
1834 | gcc_assert (node->analyzed); | |
1835 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1836 | fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node)); | |
1837 | ||
1838 | cgraph_for_node_and_aliases (node, cgraph_propagate_frequency_1, &d, true); | |
1839 | ||
1840 | if ((d.only_called_at_startup && !d.only_called_at_exit) | |
0f9fb931 | 1841 | && !node->only_called_at_startup) |
1842 | { | |
1843 | node->only_called_at_startup = true; | |
5ea60108 | 1844 | if (dump_file) |
0f9fb931 | 1845 | fprintf (dump_file, "Node %s promoted to only called at startup.\n", |
1846 | cgraph_node_name (node)); | |
1847 | changed = true; | |
1848 | } | |
74fa5f1c | 1849 | if ((d.only_called_at_exit && !d.only_called_at_startup) |
0f9fb931 | 1850 | && !node->only_called_at_exit) |
1851 | { | |
1852 | node->only_called_at_exit = true; | |
5ea60108 | 1853 | if (dump_file) |
0f9fb931 | 1854 | fprintf (dump_file, "Node %s promoted to only called at exit.\n", |
1855 | cgraph_node_name (node)); | |
1856 | changed = true; | |
1857 | } | |
1858 | /* These come either from profile or user hints; never update them. */ | |
1859 | if (node->frequency == NODE_FREQUENCY_HOT | |
1860 | || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED) | |
1861 | return changed; | |
74fa5f1c | 1862 | if (d.maybe_unlikely_executed) |
0f9fb931 | 1863 | { |
1864 | node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED; | |
1865 | if (dump_file) | |
1866 | fprintf (dump_file, "Node %s promoted to unlikely executed.\n", | |
1867 | cgraph_node_name (node)); | |
1868 | changed = true; | |
1869 | } | |
74fa5f1c | 1870 | else if (d.maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE) |
0f9fb931 | 1871 | { |
1872 | node->frequency = NODE_FREQUENCY_EXECUTED_ONCE; | |
1873 | if (dump_file) | |
1874 | fprintf (dump_file, "Node %s promoted to executed once.\n", | |
1875 | cgraph_node_name (node)); | |
1876 | changed = true; | |
1877 | } | |
1878 | return changed; | |
5ea60108 | 1879 | } |
1880 | ||
fc94a528 | 1881 | /* Return true when NODE can not return or throw and thus |
1882 | it is safe to ignore its side effects for IPA analysis. */ | |
1883 | ||
1884 | bool | |
1885 | cgraph_node_cannot_return (struct cgraph_node *node) | |
1886 | { | |
7d0d0ce1 | 1887 | int flags = flags_from_decl_or_type (node->symbol.decl); |
fc94a528 | 1888 | if (!flag_exceptions) |
1889 | return (flags & ECF_NORETURN) != 0; | |
1890 | else | |
1891 | return ((flags & (ECF_NORETURN | ECF_NOTHROW)) | |
1892 | == (ECF_NORETURN | ECF_NOTHROW)); | |
1893 | } | |
1894 | ||
1895 | /* Return true when call of E can not lead to return from caller | |
1896 | and thus it is safe to ignore its side effects for IPA analysis | |
1897 | when computing side effects of the caller. | |
1898 | FIXME: We could actually mark all edges that have no reaching | |
1899 | patch to EXIT_BLOCK_PTR or throw to get better results. */ | |
1900 | bool | |
1901 | cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e) | |
1902 | { | |
023a28e1 | 1903 | if (cgraph_node_cannot_return (e->caller)) |
1904 | return true; | |
fc94a528 | 1905 | if (e->indirect_unknown_callee) |
1906 | { | |
1907 | int flags = e->indirect_info->ecf_flags; | |
1908 | if (!flag_exceptions) | |
1909 | return (flags & ECF_NORETURN) != 0; | |
1910 | else | |
1911 | return ((flags & (ECF_NORETURN | ECF_NOTHROW)) | |
1912 | == (ECF_NORETURN | ECF_NOTHROW)); | |
1913 | } | |
1914 | else | |
1915 | return cgraph_node_cannot_return (e->callee); | |
1916 | } | |
1917 | ||
1e3aebec | 1918 | /* Return true when function NODE can be removed from callgraph |
1919 | if all direct calls are eliminated. */ | |
1920 | ||
1921 | bool | |
1922 | cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node) | |
1923 | { | |
7f74ac6b | 1924 | gcc_assert (!node->global.inlined_to); |
1925 | /* Extern inlines can always go, we will use the external definition. */ | |
7d0d0ce1 | 1926 | if (DECL_EXTERNAL (node->symbol.decl)) |
7f74ac6b | 1927 | return true; |
1e3aebec | 1928 | /* When function is needed, we can not remove it. */ |
8efa224a | 1929 | if (node->symbol.force_output || node->symbol.used_from_other_partition) |
1e3aebec | 1930 | return false; |
7d0d0ce1 | 1931 | if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl) |
1932 | || DECL_STATIC_DESTRUCTOR (node->symbol.decl)) | |
7f74ac6b | 1933 | return false; |
1e3aebec | 1934 | /* Only COMDAT functions can be removed if externally visible. */ |
7d0d0ce1 | 1935 | if (node->symbol.externally_visible |
1936 | && (!DECL_COMDAT (node->symbol.decl) | |
cf951b1a | 1937 | || symtab_used_from_object_file_p ((symtab_node) node))) |
1e3aebec | 1938 | return false; |
1e3aebec | 1939 | return true; |
1940 | } | |
1941 | ||
794fd282 | 1942 | /* Worker for cgraph_can_remove_if_no_direct_calls_p. */ |
1943 | ||
1944 | static bool | |
1945 | nonremovable_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED) | |
1946 | { | |
1947 | return !cgraph_can_remove_if_no_direct_calls_and_refs_p (node); | |
1948 | } | |
1949 | ||
1950 | /* Return true when function NODE and its aliases can be removed from callgraph | |
1951 | if all direct calls are eliminated. */ | |
1952 | ||
1953 | bool | |
1954 | cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node *node) | |
1955 | { | |
1956 | /* Extern inlines can always go, we will use the external definition. */ | |
7d0d0ce1 | 1957 | if (DECL_EXTERNAL (node->symbol.decl)) |
794fd282 | 1958 | return true; |
7d0d0ce1 | 1959 | if (node->symbol.address_taken) |
794fd282 | 1960 | return false; |
1961 | return !cgraph_for_node_and_aliases (node, nonremovable_p, NULL, true); | |
1962 | } | |
1963 | ||
1964 | /* Worker for cgraph_can_remove_if_no_direct_calls_p. */ | |
1965 | ||
1966 | static bool | |
1967 | used_from_object_file_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED) | |
1968 | { | |
cf951b1a | 1969 | return symtab_used_from_object_file_p ((symtab_node) node); |
794fd282 | 1970 | } |
1971 | ||
0a10fd82 | 1972 | /* Return true when function NODE can be expected to be removed |
69b18a8b | 1973 | from program when direct calls in this compilation unit are removed. |
1974 | ||
1975 | As a special case COMDAT functions are | |
1976 | cgraph_can_remove_if_no_direct_calls_p while the are not | |
1977 | cgraph_only_called_directly_p (it is possible they are called from other | |
1978 | unit) | |
1979 | ||
1980 | This function behaves as cgraph_only_called_directly_p because eliminating | |
0a10fd82 | 1981 | all uses of COMDAT function does not make it necessarily disappear from |
69b18a8b | 1982 | the program unless we are compiling whole program or we do LTO. In this |
1983 | case we know we win since dynamic linking will not really discard the | |
1984 | linkonce section. */ | |
1985 | ||
1986 | bool | |
1987 | cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node) | |
1988 | { | |
7f74ac6b | 1989 | gcc_assert (!node->global.inlined_to); |
794fd282 | 1990 | if (cgraph_for_node_and_aliases (node, used_from_object_file_p, NULL, true)) |
69b18a8b | 1991 | return false; |
1992 | if (!in_lto_p && !flag_whole_program) | |
1993 | return cgraph_only_called_directly_p (node); | |
1994 | else | |
7f74ac6b | 1995 | { |
7d0d0ce1 | 1996 | if (DECL_EXTERNAL (node->symbol.decl)) |
7f74ac6b | 1997 | return true; |
1998 | return cgraph_can_remove_if_no_direct_calls_p (node); | |
1999 | } | |
69b18a8b | 2000 | } |
2001 | ||
9ced88d0 | 2002 | |
74fa5f1c | 2003 | /* Worker for cgraph_only_called_directly_p. */ |
2004 | ||
2005 | static bool | |
2006 | cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED) | |
2007 | { | |
2008 | return !cgraph_only_called_directly_or_aliased_p (node); | |
2009 | } | |
2010 | ||
2011 | /* Return true when function NODE and all its aliases are only called | |
2012 | directly. | |
2013 | i.e. it is not externally visible, address was not taken and | |
2014 | it is not used in any other non-standard way. */ | |
2015 | ||
2016 | bool | |
2017 | cgraph_only_called_directly_p (struct cgraph_node *node) | |
2018 | { | |
2019 | gcc_assert (cgraph_function_or_thunk_node (node, NULL) == node); | |
2020 | return !cgraph_for_node_and_aliases (node, cgraph_not_only_called_directly_p_1, | |
2021 | NULL, true); | |
2022 | } | |
2023 | ||
2024 | ||
2025 | /* Collect all callers of NODE. Worker for collect_callers_of_node. */ | |
2026 | ||
2027 | static bool | |
2028 | collect_callers_of_node_1 (struct cgraph_node *node, void *data) | |
2029 | { | |
2030 | VEC (cgraph_edge_p, heap) ** redirect_callers = (VEC (cgraph_edge_p, heap) **)data; | |
2031 | struct cgraph_edge *cs; | |
2032 | enum availability avail; | |
2033 | cgraph_function_or_thunk_node (node, &avail); | |
2034 | ||
2035 | if (avail > AVAIL_OVERWRITABLE) | |
2036 | for (cs = node->callers; cs != NULL; cs = cs->next_caller) | |
2037 | if (!cs->indirect_inlining_edge) | |
2038 | VEC_safe_push (cgraph_edge_p, heap, *redirect_callers, cs); | |
2039 | return false; | |
2040 | } | |
2041 | ||
2042 | /* Collect all callers of NODE and its aliases that are known to lead to NODE | |
2043 | (i.e. are not overwritable). */ | |
2044 | ||
2045 | VEC (cgraph_edge_p, heap) * | |
2046 | collect_callers_of_node (struct cgraph_node *node) | |
2047 | { | |
2048 | VEC (cgraph_edge_p, heap) * redirect_callers = NULL; | |
2049 | cgraph_for_node_and_aliases (node, collect_callers_of_node_1, | |
2050 | &redirect_callers, false); | |
2051 | return redirect_callers; | |
2052 | } | |
2053 | ||
da5e1e7c | 2054 | /* Return TRUE if NODE2 is equivalent to NODE or its clone. */ |
2055 | static bool | |
2056 | clone_of_p (struct cgraph_node *node, struct cgraph_node *node2) | |
2057 | { | |
2058 | node = cgraph_function_or_thunk_node (node, NULL); | |
2059 | node2 = cgraph_function_or_thunk_node (node2, NULL); | |
2060 | while (node != node2 && node2) | |
2061 | node2 = node2->clone_of; | |
2062 | return node2 != NULL; | |
2063 | } | |
2064 | ||
2065 | /* Verify edge E count and frequency. */ | |
2066 | ||
2067 | static bool | |
2068 | verify_edge_count_and_frequency (struct cgraph_edge *e) | |
2069 | { | |
2070 | bool error_found = false; | |
2071 | if (e->count < 0) | |
2072 | { | |
2073 | error ("caller edge count is negative"); | |
2074 | error_found = true; | |
2075 | } | |
2076 | if (e->frequency < 0) | |
2077 | { | |
2078 | error ("caller edge frequency is negative"); | |
2079 | error_found = true; | |
2080 | } | |
2081 | if (e->frequency > CGRAPH_FREQ_MAX) | |
2082 | { | |
2083 | error ("caller edge frequency is too large"); | |
2084 | error_found = true; | |
2085 | } | |
2086 | if (gimple_has_body_p (e->caller->symbol.decl) | |
2087 | && !e->caller->global.inlined_to | |
2088 | /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out. | |
2089 | Remove this once edges are actualy removed from the function at that time. */ | |
2090 | && (e->frequency | |
2091 | || (inline_edge_summary_vec | |
2092 | && ((VEC_length(inline_edge_summary_t, inline_edge_summary_vec) | |
2093 | <= (unsigned) e->uid) | |
2094 | || !inline_edge_summary (e)->predicate))) | |
2095 | && (e->frequency | |
2096 | != compute_call_stmt_bb_frequency (e->caller->symbol.decl, | |
2097 | gimple_bb (e->call_stmt)))) | |
2098 | { | |
2099 | error ("caller edge frequency %i does not match BB frequency %i", | |
2100 | e->frequency, | |
2101 | compute_call_stmt_bb_frequency (e->caller->symbol.decl, | |
2102 | gimple_bb (e->call_stmt))); | |
2103 | error_found = true; | |
2104 | } | |
2105 | return error_found; | |
2106 | } | |
2107 | ||
2108 | /* Switch to THIS_CFUN if needed and print STMT to stderr. */ | |
2109 | static void | |
2110 | cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt) | |
2111 | { | |
2112 | /* debug_gimple_stmt needs correct cfun */ | |
2113 | if (cfun != this_cfun) | |
2114 | set_cfun (this_cfun); | |
2115 | debug_gimple_stmt (stmt); | |
2116 | } | |
2117 | ||
2118 | /* Verify that call graph edge E corresponds to DECL from the associated | |
2119 | statement. Return true if the verification should fail. */ | |
2120 | ||
2121 | static bool | |
2122 | verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl) | |
2123 | { | |
2124 | struct cgraph_node *node; | |
2125 | ||
2126 | if (!decl || e->callee->global.inlined_to) | |
2127 | return false; | |
2128 | node = cgraph_get_node (decl); | |
2129 | ||
2130 | /* We do not know if a node from a different partition is an alias or what it | |
2131 | aliases and therefore cannot do the former_clone_of check reliably. */ | |
2132 | if (!node || node->symbol.in_other_partition) | |
2133 | return false; | |
2134 | node = cgraph_function_or_thunk_node (node, NULL); | |
2135 | ||
2136 | if ((e->callee->former_clone_of != node->symbol.decl | |
2137 | && (!node->same_body_alias | |
2138 | || e->callee->former_clone_of != node->thunk.alias)) | |
2139 | /* IPA-CP sometimes redirect edge to clone and then back to the former | |
2140 | function. This ping-pong has to go, eventually. */ | |
2141 | && (node != cgraph_function_or_thunk_node (e->callee, NULL)) | |
2142 | && !clone_of_p (node, e->callee) | |
2143 | /* If decl is a same body alias of some other decl, allow e->callee to be | |
2144 | a clone of a clone of that other decl too. */ | |
2145 | && (!node->same_body_alias | |
2146 | || !clone_of_p (cgraph_get_node (node->thunk.alias), e->callee))) | |
2147 | return true; | |
2148 | else | |
2149 | return false; | |
2150 | } | |
2151 | ||
2152 | /* Verify cgraph nodes of given cgraph node. */ | |
2153 | DEBUG_FUNCTION void | |
2154 | verify_cgraph_node (struct cgraph_node *node) | |
2155 | { | |
2156 | struct cgraph_edge *e; | |
2157 | struct function *this_cfun = DECL_STRUCT_FUNCTION (node->symbol.decl); | |
2158 | basic_block this_block; | |
2159 | gimple_stmt_iterator gsi; | |
2160 | bool error_found = false; | |
2161 | ||
2162 | if (seen_error ()) | |
2163 | return; | |
2164 | ||
2165 | timevar_push (TV_CGRAPH_VERIFY); | |
2166 | error_found |= verify_symtab_base ((symtab_node) node); | |
2167 | for (e = node->callees; e; e = e->next_callee) | |
2168 | if (e->aux) | |
2169 | { | |
2170 | error ("aux field set for edge %s->%s", | |
2171 | identifier_to_locale (cgraph_node_name (e->caller)), | |
2172 | identifier_to_locale (cgraph_node_name (e->callee))); | |
2173 | error_found = true; | |
2174 | } | |
2175 | if (node->count < 0) | |
2176 | { | |
2177 | error ("execution count is negative"); | |
2178 | error_found = true; | |
2179 | } | |
cf951b1a | 2180 | if (node->global.inlined_to && node->symbol.same_comdat_group) |
2181 | { | |
2182 | error ("inline clone in same comdat group list"); | |
2183 | error_found = true; | |
2184 | } | |
da5e1e7c | 2185 | if (node->global.inlined_to && node->symbol.externally_visible) |
2186 | { | |
2187 | error ("externally visible inline clone"); | |
2188 | error_found = true; | |
2189 | } | |
2190 | if (node->global.inlined_to && node->symbol.address_taken) | |
2191 | { | |
2192 | error ("inline clone with address taken"); | |
2193 | error_found = true; | |
2194 | } | |
2195 | if (node->global.inlined_to && node->symbol.force_output) | |
2196 | { | |
2197 | error ("inline clone is forced to output"); | |
2198 | error_found = true; | |
2199 | } | |
2200 | for (e = node->indirect_calls; e; e = e->next_callee) | |
2201 | { | |
2202 | if (e->aux) | |
2203 | { | |
2204 | error ("aux field set for indirect edge from %s", | |
2205 | identifier_to_locale (cgraph_node_name (e->caller))); | |
2206 | error_found = true; | |
2207 | } | |
2208 | if (!e->indirect_unknown_callee | |
2209 | || !e->indirect_info) | |
2210 | { | |
2211 | error ("An indirect edge from %s is not marked as indirect or has " | |
2212 | "associated indirect_info, the corresponding statement is: ", | |
2213 | identifier_to_locale (cgraph_node_name (e->caller))); | |
2214 | cgraph_debug_gimple_stmt (this_cfun, e->call_stmt); | |
2215 | error_found = true; | |
2216 | } | |
2217 | } | |
2218 | for (e = node->callers; e; e = e->next_caller) | |
2219 | { | |
2220 | if (verify_edge_count_and_frequency (e)) | |
2221 | error_found = true; | |
2222 | if (!e->inline_failed) | |
2223 | { | |
2224 | if (node->global.inlined_to | |
2225 | != (e->caller->global.inlined_to | |
2226 | ? e->caller->global.inlined_to : e->caller)) | |
2227 | { | |
2228 | error ("inlined_to pointer is wrong"); | |
2229 | error_found = true; | |
2230 | } | |
2231 | if (node->callers->next_caller) | |
2232 | { | |
2233 | error ("multiple inline callers"); | |
2234 | error_found = true; | |
2235 | } | |
2236 | } | |
2237 | else | |
2238 | if (node->global.inlined_to) | |
2239 | { | |
2240 | error ("inlined_to pointer set for noninline callers"); | |
2241 | error_found = true; | |
2242 | } | |
2243 | } | |
2244 | for (e = node->indirect_calls; e; e = e->next_callee) | |
2245 | if (verify_edge_count_and_frequency (e)) | |
2246 | error_found = true; | |
2247 | if (!node->callers && node->global.inlined_to) | |
2248 | { | |
2249 | error ("inlined_to pointer is set but no predecessors found"); | |
2250 | error_found = true; | |
2251 | } | |
2252 | if (node->global.inlined_to == node) | |
2253 | { | |
2254 | error ("inlined_to pointer refers to itself"); | |
2255 | error_found = true; | |
2256 | } | |
2257 | ||
2258 | if (node->clone_of) | |
2259 | { | |
2260 | struct cgraph_node *n; | |
2261 | for (n = node->clone_of->clones; n; n = n->next_sibling_clone) | |
2262 | if (n == node) | |
2263 | break; | |
2264 | if (!n) | |
2265 | { | |
2266 | error ("node has wrong clone_of"); | |
2267 | error_found = true; | |
2268 | } | |
2269 | } | |
2270 | if (node->clones) | |
2271 | { | |
2272 | struct cgraph_node *n; | |
2273 | for (n = node->clones; n; n = n->next_sibling_clone) | |
2274 | if (n->clone_of != node) | |
2275 | break; | |
2276 | if (n) | |
2277 | { | |
2278 | error ("node has wrong clone list"); | |
2279 | error_found = true; | |
2280 | } | |
2281 | } | |
2282 | if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of) | |
2283 | { | |
2284 | error ("node is in clone list but it is not clone"); | |
2285 | error_found = true; | |
2286 | } | |
2287 | if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node) | |
2288 | { | |
2289 | error ("node has wrong prev_clone pointer"); | |
2290 | error_found = true; | |
2291 | } | |
2292 | if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node) | |
2293 | { | |
2294 | error ("double linked list of clones corrupted"); | |
2295 | error_found = true; | |
2296 | } | |
2297 | ||
2298 | if (node->analyzed && node->alias) | |
2299 | { | |
2300 | bool ref_found = false; | |
2301 | int i; | |
2302 | struct ipa_ref *ref; | |
2303 | ||
2304 | if (node->callees) | |
2305 | { | |
2306 | error ("Alias has call edges"); | |
2307 | error_found = true; | |
2308 | } | |
2309 | for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, | |
2310 | i, ref); i++) | |
2311 | if (ref->use != IPA_REF_ALIAS) | |
2312 | { | |
2313 | error ("Alias has non-alias reference"); | |
2314 | error_found = true; | |
2315 | } | |
2316 | else if (ref_found) | |
2317 | { | |
2318 | error ("Alias has more than one alias reference"); | |
2319 | error_found = true; | |
2320 | } | |
2321 | else | |
2322 | ref_found = true; | |
2323 | if (!ref_found) | |
2324 | { | |
2325 | error ("Analyzed alias has no reference"); | |
2326 | error_found = true; | |
2327 | } | |
2328 | } | |
2329 | if (node->analyzed && node->thunk.thunk_p) | |
2330 | { | |
2331 | if (!node->callees) | |
2332 | { | |
2333 | error ("No edge out of thunk node"); | |
2334 | error_found = true; | |
2335 | } | |
2336 | else if (node->callees->next_callee) | |
2337 | { | |
2338 | error ("More than one edge out of thunk node"); | |
2339 | error_found = true; | |
2340 | } | |
2341 | if (gimple_has_body_p (node->symbol.decl)) | |
2342 | { | |
2343 | error ("Thunk is not supposed to have body"); | |
2344 | error_found = true; | |
2345 | } | |
2346 | } | |
2347 | else if (node->analyzed && gimple_has_body_p (node->symbol.decl) | |
2348 | && !TREE_ASM_WRITTEN (node->symbol.decl) | |
2349 | && (!DECL_EXTERNAL (node->symbol.decl) || node->global.inlined_to) | |
2350 | && !flag_wpa) | |
2351 | { | |
2352 | if (this_cfun->cfg) | |
2353 | { | |
2354 | /* The nodes we're interested in are never shared, so walk | |
2355 | the tree ignoring duplicates. */ | |
2356 | struct pointer_set_t *visited_nodes = pointer_set_create (); | |
2357 | /* Reach the trees by walking over the CFG, and note the | |
2358 | enclosing basic-blocks in the call edges. */ | |
2359 | FOR_EACH_BB_FN (this_block, this_cfun) | |
2360 | for (gsi = gsi_start_bb (this_block); | |
2361 | !gsi_end_p (gsi); | |
2362 | gsi_next (&gsi)) | |
2363 | { | |
2364 | gimple stmt = gsi_stmt (gsi); | |
2365 | if (is_gimple_call (stmt)) | |
2366 | { | |
2367 | struct cgraph_edge *e = cgraph_edge (node, stmt); | |
2368 | tree decl = gimple_call_fndecl (stmt); | |
2369 | if (e) | |
2370 | { | |
2371 | if (e->aux) | |
2372 | { | |
2373 | error ("shared call_stmt:"); | |
2374 | cgraph_debug_gimple_stmt (this_cfun, stmt); | |
2375 | error_found = true; | |
2376 | } | |
2377 | if (!e->indirect_unknown_callee) | |
2378 | { | |
2379 | if (verify_edge_corresponds_to_fndecl (e, decl)) | |
2380 | { | |
2381 | error ("edge points to wrong declaration:"); | |
2382 | debug_tree (e->callee->symbol.decl); | |
2383 | fprintf (stderr," Instead of:"); | |
2384 | debug_tree (decl); | |
2385 | error_found = true; | |
2386 | } | |
2387 | } | |
2388 | else if (decl) | |
2389 | { | |
2390 | error ("an indirect edge with unknown callee " | |
2391 | "corresponding to a call_stmt with " | |
2392 | "a known declaration:"); | |
2393 | error_found = true; | |
2394 | cgraph_debug_gimple_stmt (this_cfun, e->call_stmt); | |
2395 | } | |
2396 | e->aux = (void *)1; | |
2397 | } | |
2398 | else if (decl) | |
2399 | { | |
2400 | error ("missing callgraph edge for call stmt:"); | |
2401 | cgraph_debug_gimple_stmt (this_cfun, stmt); | |
2402 | error_found = true; | |
2403 | } | |
2404 | } | |
2405 | } | |
2406 | pointer_set_destroy (visited_nodes); | |
2407 | } | |
2408 | else | |
2409 | /* No CFG available?! */ | |
2410 | gcc_unreachable (); | |
2411 | ||
2412 | for (e = node->callees; e; e = e->next_callee) | |
2413 | { | |
2414 | if (!e->aux) | |
2415 | { | |
2416 | error ("edge %s->%s has no corresponding call_stmt", | |
2417 | identifier_to_locale (cgraph_node_name (e->caller)), | |
2418 | identifier_to_locale (cgraph_node_name (e->callee))); | |
2419 | cgraph_debug_gimple_stmt (this_cfun, e->call_stmt); | |
2420 | error_found = true; | |
2421 | } | |
2422 | e->aux = 0; | |
2423 | } | |
2424 | for (e = node->indirect_calls; e; e = e->next_callee) | |
2425 | { | |
2426 | if (!e->aux) | |
2427 | { | |
2428 | error ("an indirect edge from %s has no corresponding call_stmt", | |
2429 | identifier_to_locale (cgraph_node_name (e->caller))); | |
2430 | cgraph_debug_gimple_stmt (this_cfun, e->call_stmt); | |
2431 | error_found = true; | |
2432 | } | |
2433 | e->aux = 0; | |
2434 | } | |
2435 | } | |
2436 | if (error_found) | |
2437 | { | |
2438 | dump_cgraph_node (stderr, node); | |
2439 | internal_error ("verify_cgraph_node failed"); | |
2440 | } | |
2441 | timevar_pop (TV_CGRAPH_VERIFY); | |
2442 | } | |
2443 | ||
2444 | /* Verify whole cgraph structure. */ | |
2445 | DEBUG_FUNCTION void | |
2446 | verify_cgraph (void) | |
2447 | { | |
2448 | struct cgraph_node *node; | |
2449 | ||
2450 | if (seen_error ()) | |
2451 | return; | |
2452 | ||
2453 | FOR_EACH_FUNCTION (node) | |
2454 | verify_cgraph_node (node); | |
2455 | } | |
639e4be4 | 2456 | #include "gt-cgraph.h" |