]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cgraph.c
* output.h (__gcc_host_wide_int__): Move to hwint.h.
[thirdparty/gcc.git] / gcc / cgraph.c
1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
3 2011, 2012 Free Software Foundation, Inc.
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
10 Software Foundation; either version 3, or (at your option) any later
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
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* This file contains basic routines manipulating call graph
23
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. */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "tree-inline.h"
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"
40 #include "basic-block.h"
41 #include "cgraph.h"
42 #include "intl.h"
43 #include "gimple.h"
44 #include "tree-dump.h"
45 #include "tree-flow.h"
46 #include "value-prof.h"
47 #include "except.h"
48 #include "diagnostic-core.h"
49 #include "rtl.h"
50 #include "ipa-utils.h"
51 #include "lto-streamer.h"
52 #include "ipa-inline.h"
53 #include "cfgloop.h"
54 #include "gimple-pretty-print.h"
55
56 static void cgraph_node_remove_callers (struct cgraph_node *node);
57 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
58 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
59
60 /* Queue of cgraph nodes scheduled to be lowered. */
61 symtab_node x_cgraph_nodes_queue;
62 #define cgraph_nodes_queue ((struct cgraph_node *)x_cgraph_nodes_queue)
63
64 /* Number of nodes in existence. */
65 int cgraph_n_nodes;
66
67 /* Maximal uid used in cgraph nodes. */
68 int cgraph_max_uid;
69
70 /* Maximal uid used in cgraph edges. */
71 int cgraph_edge_max_uid;
72
73 /* Set when whole unit has been analyzed so we can access global info. */
74 bool cgraph_global_info_ready = false;
75
76 /* What state callgraph is in right now. */
77 enum cgraph_state cgraph_state = CGRAPH_STATE_PARSING;
78
79 /* Set when the cgraph is fully build and the basic flags are computed. */
80 bool cgraph_function_flags_ready = false;
81
82 /* List of hooks triggered on cgraph_edge events. */
83 struct cgraph_edge_hook_list {
84 cgraph_edge_hook hook;
85 void *data;
86 struct cgraph_edge_hook_list *next;
87 };
88
89 /* List of hooks triggered on cgraph_node events. */
90 struct cgraph_node_hook_list {
91 cgraph_node_hook hook;
92 void *data;
93 struct cgraph_node_hook_list *next;
94 };
95
96 /* List of hooks triggered on events involving two cgraph_edges. */
97 struct cgraph_2edge_hook_list {
98 cgraph_2edge_hook hook;
99 void *data;
100 struct cgraph_2edge_hook_list *next;
101 };
102
103 /* List of hooks triggered on events involving two cgraph_nodes. */
104 struct cgraph_2node_hook_list {
105 cgraph_2node_hook hook;
106 void *data;
107 struct cgraph_2node_hook_list *next;
108 };
109
110 /* List of hooks triggered when an edge is removed. */
111 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
112 /* List of hooks triggered when a node is removed. */
113 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
114 /* List of hooks triggered when an edge is duplicated. */
115 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
116 /* List of hooks triggered when a node is duplicated. */
117 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
118 /* List of hooks triggered when an function is inserted. */
119 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
120
121 /* Head of a linked list of unused (freed) call graph nodes.
122 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
123 static GTY(()) struct cgraph_node *free_nodes;
124 /* Head of a linked list of unused (freed) call graph edges.
125 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
126 static GTY(()) struct cgraph_edge *free_edges;
127
128 /* Did procss_same_body_aliases run? */
129 bool same_body_aliases_done;
130
131 /* Macros to access the next item in the list of free cgraph nodes and
132 edges. */
133 #define NEXT_FREE_NODE(NODE) cgraph ((NODE)->symbol.next)
134 #define SET_NEXT_FREE_NODE(NODE,NODE2) ((NODE))->symbol.next = (symtab_node)NODE2
135 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
136
137 /* Register HOOK to be called with DATA on each removed edge. */
138 struct cgraph_edge_hook_list *
139 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
140 {
141 struct cgraph_edge_hook_list *entry;
142 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
143
144 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
145 entry->hook = hook;
146 entry->data = data;
147 entry->next = NULL;
148 while (*ptr)
149 ptr = &(*ptr)->next;
150 *ptr = entry;
151 return entry;
152 }
153
154 /* Remove ENTRY from the list of hooks called on removing edges. */
155 void
156 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
157 {
158 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
159
160 while (*ptr != entry)
161 ptr = &(*ptr)->next;
162 *ptr = entry->next;
163 free (entry);
164 }
165
166 /* Call all edge removal hooks. */
167 static void
168 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
169 {
170 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
171 while (entry)
172 {
173 entry->hook (e, entry->data);
174 entry = entry->next;
175 }
176 }
177
178 /* Register HOOK to be called with DATA on each removed node. */
179 struct cgraph_node_hook_list *
180 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
181 {
182 struct cgraph_node_hook_list *entry;
183 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
184
185 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
186 entry->hook = hook;
187 entry->data = data;
188 entry->next = NULL;
189 while (*ptr)
190 ptr = &(*ptr)->next;
191 *ptr = entry;
192 return entry;
193 }
194
195 /* Remove ENTRY from the list of hooks called on removing nodes. */
196 void
197 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
198 {
199 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
200
201 while (*ptr != entry)
202 ptr = &(*ptr)->next;
203 *ptr = entry->next;
204 free (entry);
205 }
206
207 /* Call all node removal hooks. */
208 static void
209 cgraph_call_node_removal_hooks (struct cgraph_node *node)
210 {
211 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
212 while (entry)
213 {
214 entry->hook (node, entry->data);
215 entry = entry->next;
216 }
217 }
218
219 /* Register HOOK to be called with DATA on each inserted node. */
220 struct cgraph_node_hook_list *
221 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
222 {
223 struct cgraph_node_hook_list *entry;
224 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
225
226 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
227 entry->hook = hook;
228 entry->data = data;
229 entry->next = NULL;
230 while (*ptr)
231 ptr = &(*ptr)->next;
232 *ptr = entry;
233 return entry;
234 }
235
236 /* Remove ENTRY from the list of hooks called on inserted nodes. */
237 void
238 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
239 {
240 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
241
242 while (*ptr != entry)
243 ptr = &(*ptr)->next;
244 *ptr = entry->next;
245 free (entry);
246 }
247
248 /* Call all node insertion hooks. */
249 void
250 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
251 {
252 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
253 while (entry)
254 {
255 entry->hook (node, entry->data);
256 entry = entry->next;
257 }
258 }
259
260 /* Register HOOK to be called with DATA on each duplicated edge. */
261 struct cgraph_2edge_hook_list *
262 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
263 {
264 struct cgraph_2edge_hook_list *entry;
265 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
266
267 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
268 entry->hook = hook;
269 entry->data = data;
270 entry->next = NULL;
271 while (*ptr)
272 ptr = &(*ptr)->next;
273 *ptr = entry;
274 return entry;
275 }
276
277 /* Remove ENTRY from the list of hooks called on duplicating edges. */
278 void
279 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
280 {
281 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
282
283 while (*ptr != entry)
284 ptr = &(*ptr)->next;
285 *ptr = entry->next;
286 free (entry);
287 }
288
289 /* Call all edge duplication hooks. */
290 void
291 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
292 struct cgraph_edge *cs2)
293 {
294 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
295 while (entry)
296 {
297 entry->hook (cs1, cs2, entry->data);
298 entry = entry->next;
299 }
300 }
301
302 /* Register HOOK to be called with DATA on each duplicated node. */
303 struct cgraph_2node_hook_list *
304 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
305 {
306 struct cgraph_2node_hook_list *entry;
307 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
308
309 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
310 entry->hook = hook;
311 entry->data = data;
312 entry->next = NULL;
313 while (*ptr)
314 ptr = &(*ptr)->next;
315 *ptr = entry;
316 return entry;
317 }
318
319 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
320 void
321 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
322 {
323 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
324
325 while (*ptr != entry)
326 ptr = &(*ptr)->next;
327 *ptr = entry->next;
328 free (entry);
329 }
330
331 /* Call all node duplication hooks. */
332 void
333 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
334 struct cgraph_node *node2)
335 {
336 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
337 while (entry)
338 {
339 entry->hook (node1, node2, entry->data);
340 entry = entry->next;
341 }
342 }
343
344 /* Allocate new callgraph node. */
345
346 static inline struct cgraph_node *
347 cgraph_allocate_node (void)
348 {
349 struct cgraph_node *node;
350
351 if (free_nodes)
352 {
353 node = free_nodes;
354 free_nodes = NEXT_FREE_NODE (node);
355 }
356 else
357 {
358 node = ggc_alloc_cleared_cgraph_node ();
359 node->uid = cgraph_max_uid++;
360 }
361
362 return node;
363 }
364
365 /* Allocate new callgraph node and insert it into basic data structures. */
366
367 struct cgraph_node *
368 cgraph_create_empty_node (void)
369 {
370 struct cgraph_node *node = cgraph_allocate_node ();
371
372 node->symbol.type = SYMTAB_FUNCTION;
373 node->frequency = NODE_FREQUENCY_NORMAL;
374 node->count_materialization_scale = REG_BR_PROB_BASE;
375 cgraph_n_nodes++;
376 return node;
377 }
378
379 /* Return cgraph node assigned to DECL. Create new one when needed. */
380
381 struct cgraph_node *
382 cgraph_create_node (tree decl)
383 {
384 struct cgraph_node *node = cgraph_create_empty_node ();
385 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
386
387 node->symbol.decl = decl;
388 symtab_register_node ((symtab_node) node);
389
390 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
391 {
392 node->origin = cgraph_get_create_node (DECL_CONTEXT (decl));
393 node->next_nested = node->origin->nested;
394 node->origin->nested = node;
395 }
396 return node;
397 }
398
399 /* Try to find a call graph node for declaration DECL and if it does not exist,
400 create it. */
401
402 struct cgraph_node *
403 cgraph_get_create_node (tree decl)
404 {
405 struct cgraph_node *node;
406
407 node = cgraph_get_node (decl);
408 if (node)
409 return node;
410
411 return cgraph_create_node (decl);
412 }
413
414 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
415 the function body is associated with (not necessarily cgraph_node (DECL). */
416
417 struct cgraph_node *
418 cgraph_create_function_alias (tree alias, tree decl)
419 {
420 struct cgraph_node *alias_node;
421
422 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
423 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
424 alias_node = cgraph_get_create_node (alias);
425 gcc_assert (!alias_node->local.finalized);
426 alias_node->thunk.alias = decl;
427 alias_node->local.finalized = true;
428 alias_node->alias = 1;
429 return alias_node;
430 }
431
432 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
433 and NULL otherwise.
434 Same body aliases are output whenever the body of DECL is output,
435 and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL). */
436
437 struct cgraph_node *
438 cgraph_same_body_alias (struct cgraph_node *decl_node ATTRIBUTE_UNUSED, tree alias, tree decl)
439 {
440 struct cgraph_node *n;
441 #ifndef ASM_OUTPUT_DEF
442 /* If aliases aren't supported by the assembler, fail. */
443 return NULL;
444 #endif
445 /* Langhooks can create same body aliases of symbols not defined.
446 Those are useless. Drop them on the floor. */
447 if (cgraph_global_info_ready)
448 return NULL;
449
450 n = cgraph_create_function_alias (alias, decl);
451 n->same_body_alias = true;
452 if (same_body_aliases_done)
453 ipa_record_reference ((symtab_node)n, (symtab_node)cgraph_get_node (decl),
454 IPA_REF_ALIAS, NULL);
455 return n;
456 }
457
458 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
459 aliases DECL with an adjustments made into the first parameter.
460 See comments in thunk_adjust for detail on the parameters. */
461
462 struct cgraph_node *
463 cgraph_add_thunk (struct cgraph_node *decl_node ATTRIBUTE_UNUSED,
464 tree alias, tree decl ATTRIBUTE_UNUSED,
465 bool this_adjusting,
466 HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
467 tree virtual_offset,
468 tree real_alias)
469 {
470 struct cgraph_node *node;
471
472 node = cgraph_get_node (alias);
473 if (node)
474 {
475 gcc_assert (node->local.finalized);
476 gcc_assert (!node->alias);
477 gcc_assert (!node->thunk.thunk_p);
478 cgraph_remove_node (node);
479 }
480
481 node = cgraph_create_node (alias);
482 gcc_checking_assert (!virtual_offset
483 || double_int_equal_p
484 (tree_to_double_int (virtual_offset),
485 shwi_to_double_int (virtual_value)));
486 node->thunk.fixed_offset = fixed_offset;
487 node->thunk.this_adjusting = this_adjusting;
488 node->thunk.virtual_value = virtual_value;
489 node->thunk.virtual_offset_p = virtual_offset != NULL;
490 node->thunk.alias = real_alias;
491 node->thunk.thunk_p = true;
492 node->local.finalized = true;
493
494 return node;
495 }
496
497 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
498 Return NULL if there's no such node. */
499
500 struct cgraph_node *
501 cgraph_node_for_asm (tree asmname)
502 {
503 symtab_node node = symtab_node_for_asm (asmname);
504
505 /* We do not want to look at inline clones. */
506 for (node = symtab_node_for_asm (asmname); node; node = node->symbol.next_sharing_asm_name)
507 if (symtab_function_p (node) && !cgraph(node)->global.inlined_to)
508 return cgraph (node);
509 return NULL;
510 }
511
512 /* Returns a hash value for X (which really is a die_struct). */
513
514 static hashval_t
515 edge_hash (const void *x)
516 {
517 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
518 }
519
520 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
521
522 static int
523 edge_eq (const void *x, const void *y)
524 {
525 return ((const struct cgraph_edge *) x)->call_stmt == y;
526 }
527
528 /* Add call graph edge E to call site hash of its caller. */
529
530 static inline void
531 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
532 {
533 void **slot;
534 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
535 e->call_stmt,
536 htab_hash_pointer (e->call_stmt),
537 INSERT);
538 gcc_assert (!*slot);
539 *slot = e;
540 }
541
542 /* Return the callgraph edge representing the GIMPLE_CALL statement
543 CALL_STMT. */
544
545 struct cgraph_edge *
546 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
547 {
548 struct cgraph_edge *e, *e2;
549 int n = 0;
550
551 if (node->call_site_hash)
552 return (struct cgraph_edge *)
553 htab_find_with_hash (node->call_site_hash, call_stmt,
554 htab_hash_pointer (call_stmt));
555
556 /* This loop may turn out to be performance problem. In such case adding
557 hashtables into call nodes with very many edges is probably best
558 solution. It is not good idea to add pointer into CALL_EXPR itself
559 because we want to make possible having multiple cgraph nodes representing
560 different clones of the same body before the body is actually cloned. */
561 for (e = node->callees; e; e = e->next_callee)
562 {
563 if (e->call_stmt == call_stmt)
564 break;
565 n++;
566 }
567
568 if (!e)
569 for (e = node->indirect_calls; e; e = e->next_callee)
570 {
571 if (e->call_stmt == call_stmt)
572 break;
573 n++;
574 }
575
576 if (n > 100)
577 {
578 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
579 for (e2 = node->callees; e2; e2 = e2->next_callee)
580 cgraph_add_edge_to_call_site_hash (e2);
581 for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
582 cgraph_add_edge_to_call_site_hash (e2);
583 }
584
585 return e;
586 }
587
588
589 /* Change field call_stmt of edge E to NEW_STMT. */
590
591 void
592 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
593 {
594 tree decl;
595
596 if (e->caller->call_site_hash)
597 {
598 htab_remove_elt_with_hash (e->caller->call_site_hash,
599 e->call_stmt,
600 htab_hash_pointer (e->call_stmt));
601 }
602
603 e->call_stmt = new_stmt;
604 if (e->indirect_unknown_callee
605 && (decl = gimple_call_fndecl (new_stmt)))
606 {
607 /* Constant propagation (and possibly also inlining?) can turn an
608 indirect call into a direct one. */
609 struct cgraph_node *new_callee = cgraph_get_node (decl);
610
611 gcc_checking_assert (new_callee);
612 cgraph_make_edge_direct (e, new_callee);
613 }
614
615 push_cfun (DECL_STRUCT_FUNCTION (e->caller->symbol.decl));
616 e->can_throw_external = stmt_can_throw_external (new_stmt);
617 pop_cfun ();
618 if (e->caller->call_site_hash)
619 cgraph_add_edge_to_call_site_hash (e);
620 }
621
622 /* Allocate a cgraph_edge structure and fill it with data according to the
623 parameters of which only CALLEE can be NULL (when creating an indirect call
624 edge). */
625
626 static struct cgraph_edge *
627 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
628 gimple call_stmt, gcov_type count, int freq)
629 {
630 struct cgraph_edge *edge;
631
632 /* LTO does not actually have access to the call_stmt since these
633 have not been loaded yet. */
634 if (call_stmt)
635 {
636 /* This is a rather expensive check possibly triggering
637 construction of call stmt hashtable. */
638 gcc_checking_assert (!cgraph_edge (caller, call_stmt));
639
640 gcc_assert (is_gimple_call (call_stmt));
641 }
642
643 if (free_edges)
644 {
645 edge = free_edges;
646 free_edges = NEXT_FREE_EDGE (edge);
647 }
648 else
649 {
650 edge = ggc_alloc_cgraph_edge ();
651 edge->uid = cgraph_edge_max_uid++;
652 }
653
654 edge->aux = NULL;
655 edge->caller = caller;
656 edge->callee = callee;
657 edge->prev_caller = NULL;
658 edge->next_caller = NULL;
659 edge->prev_callee = NULL;
660 edge->next_callee = NULL;
661
662 edge->count = count;
663 gcc_assert (count >= 0);
664 edge->frequency = freq;
665 gcc_assert (freq >= 0);
666 gcc_assert (freq <= CGRAPH_FREQ_MAX);
667
668 edge->call_stmt = call_stmt;
669 push_cfun (DECL_STRUCT_FUNCTION (caller->symbol.decl));
670 edge->can_throw_external
671 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
672 pop_cfun ();
673 if (call_stmt
674 && callee && callee->symbol.decl
675 && !gimple_check_call_matching_types (call_stmt, callee->symbol.decl))
676 edge->call_stmt_cannot_inline_p = true;
677 else
678 edge->call_stmt_cannot_inline_p = false;
679 if (call_stmt && caller->call_site_hash)
680 cgraph_add_edge_to_call_site_hash (edge);
681
682 edge->indirect_info = NULL;
683 edge->indirect_inlining_edge = 0;
684
685 return edge;
686 }
687
688 /* Create edge from CALLER to CALLEE in the cgraph. */
689
690 struct cgraph_edge *
691 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
692 gimple call_stmt, gcov_type count, int freq)
693 {
694 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
695 count, freq);
696
697 edge->indirect_unknown_callee = 0;
698 initialize_inline_failed (edge);
699
700 edge->next_caller = callee->callers;
701 if (callee->callers)
702 callee->callers->prev_caller = edge;
703 edge->next_callee = caller->callees;
704 if (caller->callees)
705 caller->callees->prev_callee = edge;
706 caller->callees = edge;
707 callee->callers = edge;
708
709 return edge;
710 }
711
712 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
713
714 struct cgraph_indirect_call_info *
715 cgraph_allocate_init_indirect_info (void)
716 {
717 struct cgraph_indirect_call_info *ii;
718
719 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
720 ii->param_index = -1;
721 return ii;
722 }
723
724 /* Create an indirect edge with a yet-undetermined callee where the call
725 statement destination is a formal parameter of the caller with index
726 PARAM_INDEX. */
727
728 struct cgraph_edge *
729 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
730 int ecf_flags,
731 gcov_type count, int freq)
732 {
733 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
734 count, freq);
735
736 edge->indirect_unknown_callee = 1;
737 initialize_inline_failed (edge);
738
739 edge->indirect_info = cgraph_allocate_init_indirect_info ();
740 edge->indirect_info->ecf_flags = ecf_flags;
741
742 edge->next_callee = caller->indirect_calls;
743 if (caller->indirect_calls)
744 caller->indirect_calls->prev_callee = edge;
745 caller->indirect_calls = edge;
746
747 return edge;
748 }
749
750 /* Remove the edge E from the list of the callers of the callee. */
751
752 static inline void
753 cgraph_edge_remove_callee (struct cgraph_edge *e)
754 {
755 gcc_assert (!e->indirect_unknown_callee);
756 if (e->prev_caller)
757 e->prev_caller->next_caller = e->next_caller;
758 if (e->next_caller)
759 e->next_caller->prev_caller = e->prev_caller;
760 if (!e->prev_caller)
761 e->callee->callers = e->next_caller;
762 }
763
764 /* Remove the edge E from the list of the callees of the caller. */
765
766 static inline void
767 cgraph_edge_remove_caller (struct cgraph_edge *e)
768 {
769 if (e->prev_callee)
770 e->prev_callee->next_callee = e->next_callee;
771 if (e->next_callee)
772 e->next_callee->prev_callee = e->prev_callee;
773 if (!e->prev_callee)
774 {
775 if (e->indirect_unknown_callee)
776 e->caller->indirect_calls = e->next_callee;
777 else
778 e->caller->callees = e->next_callee;
779 }
780 if (e->caller->call_site_hash)
781 htab_remove_elt_with_hash (e->caller->call_site_hash,
782 e->call_stmt,
783 htab_hash_pointer (e->call_stmt));
784 }
785
786 /* Put the edge onto the free list. */
787
788 static void
789 cgraph_free_edge (struct cgraph_edge *e)
790 {
791 int uid = e->uid;
792
793 /* Clear out the edge so we do not dangle pointers. */
794 memset (e, 0, sizeof (*e));
795 e->uid = uid;
796 NEXT_FREE_EDGE (e) = free_edges;
797 free_edges = e;
798 }
799
800 /* Remove the edge E in the cgraph. */
801
802 void
803 cgraph_remove_edge (struct cgraph_edge *e)
804 {
805 /* Call all edge removal hooks. */
806 cgraph_call_edge_removal_hooks (e);
807
808 if (!e->indirect_unknown_callee)
809 /* Remove from callers list of the callee. */
810 cgraph_edge_remove_callee (e);
811
812 /* Remove from callees list of the callers. */
813 cgraph_edge_remove_caller (e);
814
815 /* Put the edge onto the free list. */
816 cgraph_free_edge (e);
817 }
818
819 /* Set callee of call graph edge E and add it to the corresponding set of
820 callers. */
821
822 static void
823 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
824 {
825 e->prev_caller = NULL;
826 if (n->callers)
827 n->callers->prev_caller = e;
828 e->next_caller = n->callers;
829 n->callers = e;
830 e->callee = n;
831 }
832
833 /* Redirect callee of E to N. The function does not update underlying
834 call expression. */
835
836 void
837 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
838 {
839 /* Remove from callers list of the current callee. */
840 cgraph_edge_remove_callee (e);
841
842 /* Insert to callers list of the new callee. */
843 cgraph_set_edge_callee (e, n);
844 }
845
846 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
847 CALLEE. DELTA is an integer constant that is to be added to the this
848 pointer (first parameter) to compensate for skipping a thunk adjustment. */
849
850 void
851 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
852 {
853 edge->indirect_unknown_callee = 0;
854
855 /* Get the edge out of the indirect edge list. */
856 if (edge->prev_callee)
857 edge->prev_callee->next_callee = edge->next_callee;
858 if (edge->next_callee)
859 edge->next_callee->prev_callee = edge->prev_callee;
860 if (!edge->prev_callee)
861 edge->caller->indirect_calls = edge->next_callee;
862
863 /* Put it into the normal callee list */
864 edge->prev_callee = NULL;
865 edge->next_callee = edge->caller->callees;
866 if (edge->caller->callees)
867 edge->caller->callees->prev_callee = edge;
868 edge->caller->callees = edge;
869
870 /* Insert to callers list of the new callee. */
871 cgraph_set_edge_callee (edge, callee);
872
873 if (edge->call_stmt)
874 edge->call_stmt_cannot_inline_p
875 = !gimple_check_call_matching_types (edge->call_stmt, callee->symbol.decl);
876
877 /* We need to re-determine the inlining status of the edge. */
878 initialize_inline_failed (edge);
879 }
880
881 /* If necessary, change the function declaration in the call statement
882 associated with E so that it corresponds to the edge callee. */
883
884 gimple
885 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
886 {
887 tree decl = gimple_call_fndecl (e->call_stmt);
888 gimple new_stmt;
889 gimple_stmt_iterator gsi;
890 #ifdef ENABLE_CHECKING
891 struct cgraph_node *node;
892 #endif
893
894 if (e->indirect_unknown_callee
895 || decl == e->callee->symbol.decl)
896 return e->call_stmt;
897
898 #ifdef ENABLE_CHECKING
899 if (decl)
900 {
901 node = cgraph_get_node (decl);
902 gcc_assert (!node || !node->clone.combined_args_to_skip);
903 }
904 #endif
905
906 if (cgraph_dump_file)
907 {
908 fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
909 xstrdup (cgraph_node_name (e->caller)), e->caller->uid,
910 xstrdup (cgraph_node_name (e->callee)), e->callee->uid);
911 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
912 if (e->callee->clone.combined_args_to_skip)
913 {
914 fprintf (cgraph_dump_file, " combined args to skip: ");
915 dump_bitmap (cgraph_dump_file,
916 e->callee->clone.combined_args_to_skip);
917 }
918 }
919
920 if (e->callee->clone.combined_args_to_skip)
921 {
922 int lp_nr;
923
924 new_stmt
925 = gimple_call_copy_skip_args (e->call_stmt,
926 e->callee->clone.combined_args_to_skip);
927 gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl);
928
929 if (gimple_vdef (new_stmt)
930 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
931 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
932
933 gsi = gsi_for_stmt (e->call_stmt);
934 gsi_replace (&gsi, new_stmt, false);
935 /* We need to defer cleaning EH info on the new statement to
936 fixup-cfg. We may not have dominator information at this point
937 and thus would end up with unreachable blocks and have no way
938 to communicate that we need to run CFG cleanup then. */
939 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
940 if (lp_nr != 0)
941 {
942 remove_stmt_from_eh_lp (e->call_stmt);
943 add_stmt_to_eh_lp (new_stmt, lp_nr);
944 }
945 }
946 else
947 {
948 new_stmt = e->call_stmt;
949 gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl);
950 update_stmt (new_stmt);
951 }
952
953 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt);
954
955 if (cgraph_dump_file)
956 {
957 fprintf (cgraph_dump_file, " updated to:");
958 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
959 }
960 return new_stmt;
961 }
962
963 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
964 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
965 of OLD_STMT if it was previously call statement.
966 If NEW_STMT is NULL, the call has been dropped without any
967 replacement. */
968
969 static void
970 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
971 gimple old_stmt, tree old_call,
972 gimple new_stmt)
973 {
974 tree new_call = (new_stmt && is_gimple_call (new_stmt))
975 ? gimple_call_fndecl (new_stmt) : 0;
976
977 /* We are seeing indirect calls, then there is nothing to update. */
978 if (!new_call && !old_call)
979 return;
980 /* See if we turned indirect call into direct call or folded call to one builtin
981 into different builtin. */
982 if (old_call != new_call)
983 {
984 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
985 struct cgraph_edge *ne = NULL;
986 gcov_type count;
987 int frequency;
988
989 if (e)
990 {
991 /* See if the edge is already there and has the correct callee. It
992 might be so because of indirect inlining has already updated
993 it. We also might've cloned and redirected the edge. */
994 if (new_call && e->callee)
995 {
996 struct cgraph_node *callee = e->callee;
997 while (callee)
998 {
999 if (callee->symbol.decl == new_call
1000 || callee->former_clone_of == new_call)
1001 return;
1002 callee = callee->clone_of;
1003 }
1004 }
1005
1006 /* Otherwise remove edge and create new one; we can't simply redirect
1007 since function has changed, so inline plan and other information
1008 attached to edge is invalid. */
1009 count = e->count;
1010 frequency = e->frequency;
1011 cgraph_remove_edge (e);
1012 }
1013 else if (new_call)
1014 {
1015 /* We are seeing new direct call; compute profile info based on BB. */
1016 basic_block bb = gimple_bb (new_stmt);
1017 count = bb->count;
1018 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1019 bb);
1020 }
1021
1022 if (new_call)
1023 {
1024 ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1025 new_stmt, count, frequency);
1026 gcc_assert (ne->inline_failed);
1027 }
1028 }
1029 /* We only updated the call stmt; update pointer in cgraph edge.. */
1030 else if (old_stmt != new_stmt)
1031 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1032 }
1033
1034 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1035 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1036 of OLD_STMT before it was updated (updating can happen inplace). */
1037
1038 void
1039 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1040 {
1041 struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1042 struct cgraph_node *node;
1043
1044 gcc_checking_assert (orig);
1045 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1046 if (orig->clones)
1047 for (node = orig->clones; node != orig;)
1048 {
1049 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1050 if (node->clones)
1051 node = node->clones;
1052 else if (node->next_sibling_clone)
1053 node = node->next_sibling_clone;
1054 else
1055 {
1056 while (node != orig && !node->next_sibling_clone)
1057 node = node->clone_of;
1058 if (node != orig)
1059 node = node->next_sibling_clone;
1060 }
1061 }
1062 }
1063
1064
1065 /* Remove all callees from the node. */
1066
1067 void
1068 cgraph_node_remove_callees (struct cgraph_node *node)
1069 {
1070 struct cgraph_edge *e, *f;
1071
1072 /* It is sufficient to remove the edges from the lists of callers of
1073 the callees. The callee list of the node can be zapped with one
1074 assignment. */
1075 for (e = node->callees; e; e = f)
1076 {
1077 f = e->next_callee;
1078 cgraph_call_edge_removal_hooks (e);
1079 if (!e->indirect_unknown_callee)
1080 cgraph_edge_remove_callee (e);
1081 cgraph_free_edge (e);
1082 }
1083 for (e = node->indirect_calls; e; e = f)
1084 {
1085 f = e->next_callee;
1086 cgraph_call_edge_removal_hooks (e);
1087 if (!e->indirect_unknown_callee)
1088 cgraph_edge_remove_callee (e);
1089 cgraph_free_edge (e);
1090 }
1091 node->indirect_calls = NULL;
1092 node->callees = NULL;
1093 if (node->call_site_hash)
1094 {
1095 htab_delete (node->call_site_hash);
1096 node->call_site_hash = NULL;
1097 }
1098 }
1099
1100 /* Remove all callers from the node. */
1101
1102 static void
1103 cgraph_node_remove_callers (struct cgraph_node *node)
1104 {
1105 struct cgraph_edge *e, *f;
1106
1107 /* It is sufficient to remove the edges from the lists of callees of
1108 the callers. The caller list of the node can be zapped with one
1109 assignment. */
1110 for (e = node->callers; e; e = f)
1111 {
1112 f = e->next_caller;
1113 cgraph_call_edge_removal_hooks (e);
1114 cgraph_edge_remove_caller (e);
1115 cgraph_free_edge (e);
1116 }
1117 node->callers = NULL;
1118 }
1119
1120 /* Release memory used to represent body of function NODE. */
1121
1122 void
1123 cgraph_release_function_body (struct cgraph_node *node)
1124 {
1125 if (DECL_STRUCT_FUNCTION (node->symbol.decl))
1126 {
1127 tree old_decl = current_function_decl;
1128 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1129 if (cfun->cfg
1130 && current_loops)
1131 {
1132 cfun->curr_properties &= ~PROP_loops;
1133 loop_optimizer_finalize ();
1134 }
1135 if (cfun->gimple_df)
1136 {
1137 current_function_decl = node->symbol.decl;
1138 delete_tree_ssa ();
1139 delete_tree_cfg_annotations ();
1140 cfun->eh = NULL;
1141 current_function_decl = old_decl;
1142 }
1143 if (cfun->cfg)
1144 {
1145 gcc_assert (dom_computed[0] == DOM_NONE);
1146 gcc_assert (dom_computed[1] == DOM_NONE);
1147 clear_edges ();
1148 }
1149 if (cfun->value_histograms)
1150 free_histograms ();
1151 pop_cfun();
1152 gimple_set_body (node->symbol.decl, NULL);
1153 VEC_free (ipa_opt_pass, heap,
1154 node->ipa_transforms_to_apply);
1155 /* Struct function hangs a lot of data that would leak if we didn't
1156 removed all pointers to it. */
1157 ggc_free (DECL_STRUCT_FUNCTION (node->symbol.decl));
1158 DECL_STRUCT_FUNCTION (node->symbol.decl) = NULL;
1159 }
1160 DECL_SAVED_TREE (node->symbol.decl) = NULL;
1161 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1162 of its associated function function declaration because it's
1163 needed to emit debug info later. */
1164 if (!node->abstract_and_needed && DECL_INITIAL (node->symbol.decl))
1165 DECL_INITIAL (node->symbol.decl) = error_mark_node;
1166 }
1167
1168 /* Remove the node from cgraph. */
1169
1170 void
1171 cgraph_remove_node (struct cgraph_node *node)
1172 {
1173 struct cgraph_node *n;
1174 int uid = node->uid;
1175
1176 cgraph_call_node_removal_hooks (node);
1177 cgraph_node_remove_callers (node);
1178 cgraph_node_remove_callees (node);
1179 VEC_free (ipa_opt_pass, heap,
1180 node->ipa_transforms_to_apply);
1181
1182 /* Incremental inlining access removed nodes stored in the postorder list.
1183 */
1184 node->symbol.force_output = false;
1185 for (n = node->nested; n; n = n->next_nested)
1186 n->origin = NULL;
1187 node->nested = NULL;
1188 if (node->origin)
1189 {
1190 struct cgraph_node **node2 = &node->origin->nested;
1191
1192 while (*node2 != node)
1193 node2 = &(*node2)->next_nested;
1194 *node2 = node->next_nested;
1195 }
1196 symtab_unregister_node ((symtab_node)node);
1197 if (node->prev_sibling_clone)
1198 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1199 else if (node->clone_of)
1200 node->clone_of->clones = node->next_sibling_clone;
1201 if (node->next_sibling_clone)
1202 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1203 if (node->clones)
1204 {
1205 struct cgraph_node *n, *next;
1206
1207 if (node->clone_of)
1208 {
1209 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1210 n->clone_of = node->clone_of;
1211 n->clone_of = node->clone_of;
1212 n->next_sibling_clone = node->clone_of->clones;
1213 if (node->clone_of->clones)
1214 node->clone_of->clones->prev_sibling_clone = n;
1215 node->clone_of->clones = node->clones;
1216 }
1217 else
1218 {
1219 /* We are removing node with clones. This makes clones inconsistent,
1220 but assume they will be removed subsequently and just keep clone
1221 tree intact. This can happen in unreachable function removal since
1222 we remove unreachable functions in random order, not by bottom-up
1223 walk of clone trees. */
1224 for (n = node->clones; n; n = next)
1225 {
1226 next = n->next_sibling_clone;
1227 n->next_sibling_clone = NULL;
1228 n->prev_sibling_clone = NULL;
1229 n->clone_of = NULL;
1230 }
1231 }
1232 }
1233
1234 /* While all the clones are removed after being proceeded, the function
1235 itself is kept in the cgraph even after it is compiled. Check whether
1236 we are done with this body and reclaim it proactively if this is the case.
1237 */
1238 n = cgraph_get_node (node->symbol.decl);
1239 if (!n
1240 || (!n->clones && !n->clone_of && !n->global.inlined_to
1241 && (cgraph_global_info_ready
1242 && (TREE_ASM_WRITTEN (n->symbol.decl)
1243 || DECL_EXTERNAL (n->symbol.decl)
1244 || !n->analyzed
1245 || n->symbol.in_other_partition))))
1246 cgraph_release_function_body (node);
1247
1248 node->symbol.decl = NULL;
1249 if (node->call_site_hash)
1250 {
1251 htab_delete (node->call_site_hash);
1252 node->call_site_hash = NULL;
1253 }
1254 cgraph_n_nodes--;
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));
1259 node->symbol.type = SYMTAB_FUNCTION;
1260 node->uid = uid;
1261 SET_NEXT_FREE_NODE (node, free_nodes);
1262 free_nodes = node;
1263 }
1264
1265 /* Likewise indicate that a node is having address taken. */
1266
1267 void
1268 cgraph_mark_address_taken_node (struct cgraph_node *node)
1269 {
1270 gcc_assert (!node->global.inlined_to);
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. */
1277 node->symbol.address_taken = 1;
1278 node = cgraph_function_or_thunk_node (node, NULL);
1279 node->symbol.address_taken = 1;
1280 }
1281
1282 /* Return local info for the compiled function. */
1283
1284 struct cgraph_local_info *
1285 cgraph_local_info (tree decl)
1286 {
1287 struct cgraph_node *node;
1288
1289 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1290 node = cgraph_get_node (decl);
1291 if (!node)
1292 return NULL;
1293 return &node->local;
1294 }
1295
1296 /* Return local info for the compiled function. */
1297
1298 struct cgraph_global_info *
1299 cgraph_global_info (tree decl)
1300 {
1301 struct cgraph_node *node;
1302
1303 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1304 node = cgraph_get_node (decl);
1305 if (!node)
1306 return NULL;
1307 return &node->global;
1308 }
1309
1310 /* Return local info for the compiled function. */
1311
1312 struct cgraph_rtl_info *
1313 cgraph_rtl_info (tree decl)
1314 {
1315 struct cgraph_node *node;
1316
1317 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1318 node = cgraph_get_node (decl);
1319 if (!node
1320 || (decl != current_function_decl
1321 && !TREE_ASM_WRITTEN (node->symbol.decl)))
1322 return NULL;
1323 return &node->rtl;
1324 }
1325
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
1344 /* Names used to print out the availability enum. */
1345 const char * const cgraph_availability_names[] =
1346 {"unset", "not_available", "overwritable", "available", "local"};
1347
1348
1349 /* Dump call graph node NODE to file F. */
1350
1351 void
1352 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1353 {
1354 struct cgraph_edge *edge;
1355 int indirect_calls_count = 0;
1356
1357 dump_symtab_base (f, (symtab_node) node);
1358
1359 if (node->global.inlined_to)
1360 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1361 xstrdup (cgraph_node_name (node)),
1362 node->symbol.order,
1363 xstrdup (cgraph_node_name (node->global.inlined_to)),
1364 node->global.inlined_to->symbol.order);
1365 if (node->clone_of)
1366 fprintf (f, " Clone of %s/%i\n",
1367 cgraph_node_asm_name (node->clone_of),
1368 node->clone_of->symbol.order);
1369 if (cgraph_function_flags_ready)
1370 fprintf (f, " Availability: %s\n",
1371 cgraph_availability_names [cgraph_function_body_availability (node)]);
1372
1373 fprintf (f, " Function flags:");
1374 if (node->analyzed)
1375 fprintf (f, " analyzed");
1376 if (node->count)
1377 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1378 (HOST_WIDEST_INT)node->count);
1379 if (node->origin)
1380 fprintf (f, " nested in: %s", cgraph_node_asm_name (node->origin));
1381 if (gimple_has_body_p (node->symbol.decl))
1382 fprintf (f, " body");
1383 if (node->process)
1384 fprintf (f, " process");
1385 if (node->local.local)
1386 fprintf (f, " local");
1387 if (node->local.finalized)
1388 fprintf (f, " finalized");
1389 if (node->local.redefined_extern_inline)
1390 fprintf (f, " redefined_extern_inline");
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");
1395 else if (node->alias)
1396 fprintf (f, " alias");
1397 if (node->tm_clone)
1398 fprintf (f, " tm_clone");
1399
1400 fprintf (f, "\n");
1401
1402 if (node->thunk.thunk_p)
1403 {
1404 fprintf (f, " Thunk of %s (asm: %s) fixed offset %i virtual value %i has "
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 }
1412 if (node->alias && node->thunk.alias)
1413 {
1414 fprintf (f, " Alias of %s",
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 }
1421
1422 fprintf (f, " Called by: ");
1423
1424 for (edge = node->callers; edge; edge = edge->next_caller)
1425 {
1426 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->caller),
1427 edge->caller->symbol.order);
1428 if (edge->count)
1429 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1430 (HOST_WIDEST_INT)edge->count);
1431 if (edge->frequency)
1432 fprintf (f, "(%.2f per call) ",
1433 edge->frequency / (double)CGRAPH_FREQ_BASE);
1434 if (!edge->inline_failed)
1435 fprintf(f, "(inlined) ");
1436 if (edge->indirect_inlining_edge)
1437 fprintf(f, "(indirect_inlining) ");
1438 if (edge->can_throw_external)
1439 fprintf(f, "(can throw external) ");
1440 }
1441
1442 fprintf (f, "\n Calls: ");
1443 for (edge = node->callees; edge; edge = edge->next_callee)
1444 {
1445 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->callee),
1446 edge->callee->symbol.order);
1447 if (!edge->inline_failed)
1448 fprintf(f, "(inlined) ");
1449 if (edge->indirect_inlining_edge)
1450 fprintf(f, "(indirect_inlining) ");
1451 if (edge->count)
1452 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1453 (HOST_WIDEST_INT)edge->count);
1454 if (edge->frequency)
1455 fprintf (f, "(%.2f per call) ",
1456 edge->frequency / (double)CGRAPH_FREQ_BASE);
1457 if (edge->can_throw_external)
1458 fprintf(f, "(can throw external) ");
1459 }
1460 fprintf (f, "\n");
1461
1462 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1463 indirect_calls_count++;
1464 if (indirect_calls_count)
1465 fprintf (f, " Has %i outgoing edges for indirect calls.\n",
1466 indirect_calls_count);
1467 }
1468
1469
1470 /* Dump call graph node NODE to stderr. */
1471
1472 DEBUG_FUNCTION void
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. */
1480
1481 void
1482 dump_cgraph (FILE *f)
1483 {
1484 struct cgraph_node *node;
1485
1486 fprintf (f, "callgraph:\n\n");
1487 FOR_EACH_FUNCTION (node)
1488 dump_cgraph_node (f, node);
1489 }
1490
1491
1492 /* Dump the call graph to stderr. */
1493
1494 DEBUG_FUNCTION void
1495 debug_cgraph (void)
1496 {
1497 dump_cgraph (stderr);
1498 }
1499
1500 /* Return true when the DECL can possibly be inlined. */
1501 bool
1502 cgraph_function_possibly_inlined_p (tree decl)
1503 {
1504 if (!cgraph_global_info_ready)
1505 return !DECL_UNINLINABLE (decl);
1506 return DECL_POSSIBLY_INLINED (decl);
1507 }
1508
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 }
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);
1529 if (!node->analyzed)
1530 avail = AVAIL_NOT_AVAILABLE;
1531 else if (node->local.local)
1532 avail = AVAIL_LOCAL;
1533 else if (!node->symbol.externally_visible)
1534 avail = AVAIL_AVAILABLE;
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
1537 behaviour on replacing inline function by different body. */
1538 else if (DECL_DECLARED_INLINE_P (node->symbol.decl))
1539 avail = AVAIL_AVAILABLE;
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
1545 the inlinability hooks completely eliminated).
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
1549 bit. */
1550
1551 else if (decl_replaceable_p (node->symbol.decl)
1552 && !DECL_EXTERNAL (node->symbol.decl))
1553 avail = AVAIL_OVERWRITABLE;
1554 else avail = AVAIL_AVAILABLE;
1555
1556 return avail;
1557 }
1558
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 {
1564 return !(!node->symbol.force_output
1565 && ((DECL_COMDAT (node->symbol.decl)
1566 && !node->symbol.same_comdat_group)
1567 || !node->symbol.externally_visible));
1568 }
1569
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 {
1577 return (!node->symbol.address_taken
1578 && !cgraph_for_node_and_aliases (node,
1579 cgraph_node_cannot_be_local_p_1,
1580 NULL, true));
1581 }
1582
1583 /* Call calback on NODE, thunks and aliases associated 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;
1594 int i;
1595 struct ipa_ref *ref;
1596
1597 if (callback (node, data))
1598 return true;
1599 for (e = node->callers; e; e = e->next_caller)
1600 if (e->caller->thunk.thunk_p
1601 && (include_overwritable
1602 || cgraph_function_body_availability (e->caller) > AVAIL_OVERWRITABLE))
1603 if (cgraph_for_node_thunks_and_aliases (e->caller, callback, data,
1604 include_overwritable))
1605 return true;
1606 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
1607 if (ref->use == IPA_REF_ALIAS)
1608 {
1609 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1610 if (include_overwritable
1611 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
1612 if (cgraph_for_node_thunks_and_aliases (alias, callback, data,
1613 include_overwritable))
1614 return true;
1615 }
1616 return false;
1617 }
1618
1619 /* Call calback on NODE and aliases associated 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,
1627 bool include_overwritable)
1628 {
1629 int i;
1630 struct ipa_ref *ref;
1631
1632 if (callback (node, data))
1633 return true;
1634 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
1635 if (ref->use == IPA_REF_ALIAS)
1636 {
1637 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1638 if (include_overwritable
1639 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
1640 if (cgraph_for_node_and_aliases (alias, callback, data,
1641 include_overwritable))
1642 return true;
1643 }
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));
1653 if (DECL_COMDAT (node->symbol.decl) || DECL_EXTERNAL (node->symbol.decl))
1654 {
1655 symtab_make_decl_local (node->symbol.decl);
1656
1657 node->symbol.externally_visible = false;
1658 node->local.local = true;
1659 node->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
1660 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
1661 }
1662 return false;
1663 }
1664
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 {
1679 struct cgraph_edge *e;
1680
1681 TREE_NOTHROW (node->symbol.decl) = data != NULL;
1682
1683 if (data != NULL)
1684 for (e = node->callers; e; e = e->next_caller)
1685 e->can_throw_external = false;
1686 return false;
1687 }
1688
1689 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
1690 if any to NOTHROW. */
1691
1692 void
1693 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
1694 {
1695 cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1,
1696 (void *)(size_t)nothrow, false);
1697 }
1698
1699 /* Worker to set const flag. */
1700
1701 static bool
1702 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
1703 {
1704 /* Static constructors and destructors without a side effect can be
1705 optimized out. */
1706 if (data && !((size_t)data & 2))
1707 {
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;
1712 }
1713 TREE_READONLY (node->symbol.decl) = data != NULL;
1714 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
1715 return false;
1716 }
1717
1718 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
1719 if any to READONLY. */
1720
1721 void
1722 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
1723 {
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
1735 optimized out. */
1736 if (data && !((size_t)data & 2))
1737 {
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;
1742 }
1743 DECL_PURE_P (node->symbol.decl) = data != NULL;
1744 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
1745 return false;
1746 }
1747
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)
1753 {
1754 cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1,
1755 (void *)(size_t)(pure + (int)looping * 2),
1756 false);
1757 }
1758
1759 /* Data used by cgraph_propagate_frequency. */
1760
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;
1778 for (edge = node->callers;
1779 edge && (d->maybe_unlikely_executed || d->maybe_executed_once
1780 || d->only_called_at_startup || d->only_called_at_exit);
1781 edge = edge->next_caller)
1782 {
1783 if (edge->caller != node)
1784 {
1785 d->only_called_at_startup &= edge->caller->only_called_at_startup;
1786 /* It makes sense to put main() together with the static constructors.
1787 It will be executed for sure, but rest of functions called from
1788 main are definitely not at startup only. */
1789 if (MAIN_NAME_P (DECL_NAME (edge->caller->symbol.decl)))
1790 d->only_called_at_startup = 0;
1791 d->only_called_at_exit &= edge->caller->only_called_at_exit;
1792 }
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))
1801 fprintf (dump_file, " Called by %s that is executed once\n",
1802 cgraph_node_name (edge->caller));
1803 d->maybe_unlikely_executed = false;
1804 if (inline_edge_summary (edge)->loop_depth)
1805 {
1806 d->maybe_executed_once = false;
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))
1814 fprintf (dump_file, " Called by %s that is normal or hot\n",
1815 cgraph_node_name (edge->caller));
1816 d->maybe_unlikely_executed = false;
1817 d->maybe_executed_once = false;
1818 break;
1819 }
1820 }
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)
1841 && !node->only_called_at_startup)
1842 {
1843 node->only_called_at_startup = true;
1844 if (dump_file)
1845 fprintf (dump_file, "Node %s promoted to only called at startup.\n",
1846 cgraph_node_name (node));
1847 changed = true;
1848 }
1849 if ((d.only_called_at_exit && !d.only_called_at_startup)
1850 && !node->only_called_at_exit)
1851 {
1852 node->only_called_at_exit = true;
1853 if (dump_file)
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;
1862 if (d.maybe_unlikely_executed)
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 }
1870 else if (d.maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
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;
1879 }
1880
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 {
1887 int flags = flags_from_decl_or_type (node->symbol.decl);
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 {
1903 if (cgraph_node_cannot_return (e->caller))
1904 return true;
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
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 {
1924 gcc_assert (!node->global.inlined_to);
1925 /* Extern inlines can always go, we will use the external definition. */
1926 if (DECL_EXTERNAL (node->symbol.decl))
1927 return true;
1928 /* When function is needed, we can not remove it. */
1929 if (node->symbol.force_output || node->symbol.used_from_other_partition)
1930 return false;
1931 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)
1932 || DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1933 return false;
1934 /* Only COMDAT functions can be removed if externally visible. */
1935 if (node->symbol.externally_visible
1936 && (!DECL_COMDAT (node->symbol.decl)
1937 || symtab_used_from_object_file_p ((symtab_node) node)))
1938 return false;
1939 return true;
1940 }
1941
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. */
1957 if (DECL_EXTERNAL (node->symbol.decl))
1958 return true;
1959 if (node->symbol.address_taken)
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 {
1969 return symtab_used_from_object_file_p ((symtab_node) node);
1970 }
1971
1972 /* Return true when function NODE can be expected to be removed
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
1981 all uses of COMDAT function does not make it necessarily disappear from
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 {
1989 gcc_assert (!node->global.inlined_to);
1990 if (cgraph_for_node_and_aliases (node, used_from_object_file_p, NULL, true))
1991 return false;
1992 if (!in_lto_p && !flag_whole_program)
1993 return cgraph_only_called_directly_p (node);
1994 else
1995 {
1996 if (DECL_EXTERNAL (node->symbol.decl))
1997 return true;
1998 return cgraph_can_remove_if_no_direct_calls_p (node);
1999 }
2000 }
2001
2002
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
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 actually 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 }
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 }
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 }
2456 #include "gt-cgraph.h"